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/ap_rep.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a Kerberos AP-REP (response) data7# definition8class ApRep < Element9# @!attribute pvno10# @return [Integer] The protocol version number11attr_accessor :pvno12# @!attribute msg_type13# @return [Integer] The type of a protocol message14attr_accessor :msg_type15# @!attribute enc_part16# @return [Rex::Proto::Kerberos::Model::EncryptedData] The encrypted part of the response17attr_accessor :enc_part1819# Decodes the Rex::Proto::Kerberos::Model::ApRep from an input20#21# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from22# @return [self] if decoding succeeds23# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed24def decode(input)25case input26when String27decode_string(input)28when OpenSSL::ASN1::ASN1Data29decode_asn1(input)30else31raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode ApRep, invalid input'32end3334self35end3637# Encodes the Rex::Proto::Kerberos::Model::ApReq into an ASN.1 String38#39# @return [String]40def encode41to_asn1.to_der42end4344# @return [OpenSSL::ASN1::ASN1Data] The ap_req ASN1Data45def to_asn146elems = []4748elems << OpenSSL::ASN1::ASN1Data.new([encode_pvno], 0, :CONTEXT_SPECIFIC)49elems << OpenSSL::ASN1::ASN1Data.new([encode_msg_type], 1, :CONTEXT_SPECIFIC)50elems << OpenSSL::ASN1::ASN1Data.new([encode_enc_part], 2, :CONTEXT_SPECIFIC)5152seq = OpenSSL::ASN1::Sequence.new(elems)5354seq_asn1 = OpenSSL::ASN1::ASN1Data.new([seq], AP_REP, :APPLICATION)55seq_asn156end5758def decrypt_enc_part(key)59data = enc_part.decrypt_asn1(key, Rex::Proto::Kerberos::Crypto::KeyUsage::AP_REP_ENCPART)6061result = Rex::Proto::Kerberos::Model::EncApRepPart::decode(data)62end6364private6566# Encodes the pvno field67#68# @return [OpenSSL::ASN1::Integer]69def encode_pvno70bn = OpenSSL::BN.new(pvno.to_s)71int = OpenSSL::ASN1::Integer.new(bn)7273int74end7576# Encodes the msg_type field77#78# @return [OpenSSL::ASN1::Integer]79def encode_msg_type80bn = OpenSSL::BN.new(msg_type.to_s)81int = OpenSSL::ASN1::Integer.new(bn)8283int84end8586# Encodes the enc_part field87#88# @return [String]89def encode_enc_part90enc_part.encode91end9293# Decodes a Rex::Proto::Kerberos::Model::ApRep from an String94#95# @param input [String] the input to decode from96def decode_string(input)97asn1 = OpenSSL::ASN1.decode(input)9899decode_asn1(asn1)100end101102# Decodes a Rex::Proto::Kerberos::Model::ApRep103#104# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from105# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed106def decode_asn1(input)107input.value[0].value.each do |val|108case val.tag109when 0110self.pvno = decode_pvno(val)111when 1112self.msg_type = decode_msg_type(val)113when 2114self.enc_part = decode_enc_part(val)115else116raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, "Failed to decode AP-REP SEQUENCE (#{val.tag})"117end118end119end120121# Decodes the pvno from an OpenSSL::ASN1::ASN1Data122#123# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from124# @return [Integer]125def decode_pvno(input)126input.value[0].value.to_i127end128129# Decodes the msg_type from an OpenSSL::ASN1::ASN1Data130#131# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from132# @return [Integer]133def decode_msg_type(input)134input.value[0].value.to_i135end136137# Decodes the enc_part138#139# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from140# @return [Rex::Proto::Kerberos::Model::EncryptedData]141def decode_enc_part(input)142Rex::Proto::Kerberos::Model::EncryptedData.decode(input.value[0])143end144end145end146end147end148end149150151