Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/kerberos/model/checksum.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a Kerberos Checksum definition.7class Checksum < Element89# @!attribute type10# @return [Integer] The algorithm used to generate the checksum11attr_accessor :type12# @!attribute checksum13# @return [String] The checksum itself14attr_accessor :checksum1516# Decodes the Rex::Proto::Kerberos::Model::Checksum from an input17#18# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from19# @return [self] if decoding succeeds20# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed21def decode(input)22case input23when String24decode_string(input)25when OpenSSL::ASN1::ASN1Data26decode_asn1(input)27else28raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode Checksum, invalid input'29end3031self32end3334# Encodes a Rex::Proto::Kerberos::Model::Checksum into an ASN.1 String35#36# @return [String]37def encode38elems = []39elems << OpenSSL::ASN1::ASN1Data.new([encode_type], 0, :CONTEXT_SPECIFIC)40elems << OpenSSL::ASN1::ASN1Data.new([encode_checksum], 1, :CONTEXT_SPECIFIC)4142seq = OpenSSL::ASN1::Sequence.new(elems)4344seq.to_der45end4647private4849# Encodes the type field50#51# @return [OpenSSL::ASN1::Integer]52def encode_type53bn = OpenSSL::BN.new(type.to_s)54int = OpenSSL::ASN1::Integer.new(bn)5556int57end5859# Encodes the checksum field60#61# @return [OpenSSL::ASN1::OctetString]62def encode_checksum63OpenSSL::ASN1::OctetString.new(checksum)64end6566# Decodes a Rex::Proto::Kerberos::Model::Checksum from an String67#68# @param input [String] the input to decode from69# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed70def decode_string(input)71asn1 = OpenSSL::ASN1.decode(input)7273decode_asn1(asn1)74rescue OpenSSL::ASN1::ASN1Error75raise Rex::Proto::Kerberos::Model::Error::KerberosDecodingError76end7778# Decodes a Rex::Proto::Kerberos::Model::Checksum from an79# OpenSSL::ASN1::Sequence80#81# @param input [OpenSSL::ASN1::Sequence] the input to decode from82# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed83def decode_asn1(input)84seq_values = input.value8586seq_values.each do |val|87case val.tag88when 089self.type = decode_type(val)90when 191self.checksum = decode_checksum(val)92else93raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode KdcRequestBody SEQUENCE'94end95end96end9798# Decodes the type field99#100# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from101# @return [Integer]102def decode_type(input)103input.value[0].value.to_i104end105106# Decodes the checksum field107#108# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from109# @return [String]110def decode_checksum(input)111input.value[0].value112end113114end115end116end117end118end119120121