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/authenticator.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of an Authenticator, sent with a7# ticket to the server to certify the client's knowledge of the encryption8# key in the ticket.9class Authenticator < Element10# @!attribute vno11# @return [Integer] The authenticator version number12attr_accessor :vno13# @!attribute crealm14# @return [String] The realm in which the client is registered15attr_accessor :crealm16# @!attribute cname17# @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the client's principal18# identifier19attr_accessor :cname20# @!attribute checksum21# @return [Rex::Proto::Kerberos::Model::Checksum] The checksum of the application data that22# accompanies the KRB_AP_REQ.23attr_accessor :checksum24# @!attribute cusec25# @return [Integer] The microsecond part of the client's timestamp26attr_accessor :cusec27# @!attribute ctime28# @return [Time] The current time of the client's host29attr_accessor :ctime30# @!attribute subkey31# @return [Rex::Proto::Kerberos::Model::EncryptionKey] the client's choice for an encryption32# key which is to be used to protect this specific application session33attr_accessor :subkey34# @!attribute enc_key_usage35# @return [Rex::Proto::Kerberos::Crypto::KeyUsage,Integer] The enc key usage number for this authenticator36attr_accessor :enc_key_usage37# @!attribute sequence_number38# @return [Integer] The initial sequence number to be used for future communications39attr_accessor :sequence_number4041# Decodes the Rex::Proto::Kerberos::Model::Authenticator from an input42#43# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from44# @return [self] if decoding succeeds45# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed46def decode(input)47case input48when String49decode_string(input)50when OpenSSL::ASN1::ASN1Data51decode_asn1(input)52else53raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode Authenticator, invalid input'54end5556self57end5859# Encodes the Rex::Proto::Kerberos::Model::Authenticator into an ASN.1 String60#61# @return [String]62def encode63elems = []64elems << OpenSSL::ASN1::ASN1Data.new([encode_vno], 0, :CONTEXT_SPECIFIC)65elems << OpenSSL::ASN1::ASN1Data.new([encode_crealm], 1, :CONTEXT_SPECIFIC)66elems << OpenSSL::ASN1::ASN1Data.new([encode_cname], 2, :CONTEXT_SPECIFIC)67elems << OpenSSL::ASN1::ASN1Data.new([encode_checksum], 3, :CONTEXT_SPECIFIC) if checksum68elems << OpenSSL::ASN1::ASN1Data.new([encode_cusec], 4, :CONTEXT_SPECIFIC)69elems << OpenSSL::ASN1::ASN1Data.new([encode_ctime], 5, :CONTEXT_SPECIFIC)70elems << OpenSSL::ASN1::ASN1Data.new([encode_subkey], 6, :CONTEXT_SPECIFIC) if subkey71elems << OpenSSL::ASN1::ASN1Data.new([encode_sequence_number], 7, :CONTEXT_SPECIFIC) if sequence_number7273seq = OpenSSL::ASN1::Sequence.new(elems)74seq_asn1 = OpenSSL::ASN1::ASN1Data.new([seq], AUTHENTICATOR, :APPLICATION)7576seq_asn1.to_der77end7879# Encrypts the Rex::Proto::Kerberos::Model::Authenticator80#81# @param etype [Integer] the crypto schema to encrypt82# @param key [String] the key to encrypt83# @return [String] the encrypted result84# @raise [NotImplementedError] if the encryption schema isn't supported85def encrypt(etype, key)86raise ::Rex::Proto::Kerberos::Model::Error::KerberosError, 'Missing enc_key_usage' unless enc_key_usage8788data = self.encode89encryptor = Rex::Proto::Kerberos::Crypto::Encryption::from_etype(etype)90encryptor.encrypt(data, key, enc_key_usage)91end929394private9596# Encodes the vno field97#98# @return [OpenSSL::ASN1::Integer]99def encode_vno100bn = OpenSSL::BN.new(vno.to_s)101int = OpenSSL::ASN1::Integer.new(bn)102103int104end105106# Encodes the crealm field107#108# @return [OpenSSL::ASN1::GeneralString]109def encode_crealm110OpenSSL::ASN1::GeneralString.new(crealm)111end112113# Encodes the cname field114#115# @return [String]116def encode_cname117cname.encode118end119120# Encodes the checksum field121#122# @return [String]123def encode_checksum124checksum.encode125end126127# Encodes the cusec field128#129# @return [OpenSSL::ASN1::Integer]130def encode_cusec131bn = OpenSSL::BN.new(cusec.to_s)132int = OpenSSL::ASN1::Integer.new(bn)133134int135end136137# Encodes the ctime field138#139# @return [OpenSSL::ASN1::GeneralizedTime]140def encode_ctime141OpenSSL::ASN1::GeneralizedTime.new(ctime)142end143144# Encodes the subkey field145#146# @return [String]147def encode_subkey148subkey.encode149end150151# Encodes the sequence_number field152#153# @return [OpenSSL::ASN1::Integer]154def encode_sequence_number155bn = OpenSSL::BN.new(sequence_number.to_s)156int = OpenSSL::ASN1::Integer.new(bn)157158int159end160161# Decodes a Rex::Proto::Kerberos::Model::Authenticator from an String162#163# @param input [String] the input to decode from164def decode_string(input)165asn1 = OpenSSL::ASN1.decode(input)166167decode_asn1(asn1)168end169170# Decodes a Rex::Proto::Kerberos::Model::Authenticator171#172# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from173# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed174def decode_asn1(input)175input.value[0].value.each do |val|176case val.tag177when 0178self.vno = decode_vno(val)179when 1180self.crealm = decode_crealm(val)181when 2182self.cname = decode_cname(val)183when 4184self.cusec = decode_cusec(val)185when 5186self.ctime = decode_ctime(val)187when 6188self.subkey = decode_subkey(val)189when 7190self.sequence_number = decode_sequence_number(val)191else192raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, "Failed to decode AUTHENTICATOR SEQUENCE (#{val.tag})"193end194end195end196# Decodes the vno from an OpenSSL::ASN1::ASN1Data197#198# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from199# @return [Integer]200def decode_vno(input)201input.value[0].value.to_i202end203204# Decodes the ctime field205#206# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from207# @return [Time]208def decode_ctime(input)209input.value[0].value210end211212# Decodes the cusec field213#214# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from215# @return [Integer]216def decode_cusec(input)217input.value[0].value218end219220# Decodes the crealm field221#222# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from223# @return [String]224def decode_crealm(input)225input.value[0].value226end227228# Decodes the cname field229#230# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from231# @return [Rex::Proto::Kerberos::Model::PrincipalName]232def decode_cname(input)233Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])234end235236# Decodes the sequence_number field237#238# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from239# @return [Integer]240def decode_sequence_number(input)241input.value[0].value.to_i242end243244# Decodes the subkey field245#246# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from247# @return [Integer]248def decode_subkey(input)249Rex::Proto::Kerberos::Model::EncryptionKey::decode(input.value[0])250end251end252end253end254end255end256257258