Path: blob/master/lib/rex/proto/kerberos/model/s4u_user_id.rb
31164 views
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of the S4UUserID structure7# as defined in the Kerberos protocol.8class S4uUserId < Element9# @!attribute nonce10# @return [Integer] The nonce in KDC-REQ-BODY11attr_accessor :nonce12# @!attribute cname13# @return [Rex::Proto::Kerberos::Model::PrincipalName, nil] The principal name (optional)14attr_accessor :cname15# @!attribute crealm16# @return [String] The realm17attr_accessor :crealm18# @!attribute subject_certificate19# @return [String, nil] The subject certificate (optional)20attr_accessor :subject_certificate21# @!attribute options22# @return [String, nil] The options (optional)23attr_accessor :options2425##26# //S4UUserID::= SEQUENCE {27# // nonce[0] UInt32, --the nonce in KDC - REQ - BODY28# // cname[1] PrincipalName OPTIONAL,29# // --Certificate mapping hints30# // crealm[2] Realm,31# // subject-certificate[3] OCTET STRING OPTIONAL,32# // options[4] BIT STRING OPTIONAL,33# // ...34# //}353637def initialize(name, impersonate_type, realm, nonce)38self.nonce = nonce39# Set cname name_type based on dMSA flag40self.cname = Rex::Proto::Kerberos::Model::PrincipalName.new(41name_type: impersonate_type == 'dmsa' ? NameType::NT_PRINCIPAL : NameType::NT_ENTERPRISE,42name_string: [name]43)44self.crealm = realm4546# Default options47self.options = impersonate_type == 'dmsa' ? ::Rex::Proto::Kerberos::Model::PaS4uX509UserOptions::UNCONDITIONAL_DELEGATION | ::Rex::Proto::Kerberos::Model::PaS4uX509UserOptions::SIGN_REPLY : ::Rex::Proto::Kerberos::Model::PaS4uX509UserOptions::SIGN_REPLY48end4950# Decodes the S4UUserID from an input51#52# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from53# @return [self] if decoding succeeds54# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed55def decode(input)56case input57when String58decode_string(input)59when OpenSSL::ASN1::ASN1Data60decode_asn1(input)61else62raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode S4UUserID, invalid input'63end6465self66end6768# Encodes the S4UUserID into an ASN.1 String69#70# @return [String]71def encode72elems = []73elems << OpenSSL::ASN1::ASN1Data.new([encode_nonce], 0, :CONTEXT_SPECIFIC)74elems << OpenSSL::ASN1::ASN1Data.new([encode_cname], 1, :CONTEXT_SPECIFIC) if cname75elems << OpenSSL::ASN1::ASN1Data.new([encode_crealm], 2, :CONTEXT_SPECIFIC)76elems << OpenSSL::ASN1::ASN1Data.new([encode_subject_certificate], 3, :CONTEXT_SPECIFIC) if subject_certificate77# Convert options to a byte array78options_bytes = [self.options].pack('N') # Pack as a big-endian unsigned 32-bit integer79elems << OpenSSL::ASN1::ASN1Data.new([OpenSSL::ASN1::BitString.new(options_bytes)], 4, :CONTEXT_SPECIFIC)808182seq = OpenSSL::ASN1::Sequence.new(elems)8384seq.to_der85end8687private8889# Encodes the nonce attribute90#91# @return [OpenSSL::ASN1::Integer]92def encode_nonce93OpenSSL::ASN1::Integer.new(nonce)94end9596# Encodes the cname attribute97#98# @return [String]99def encode_cname100cname.encode101end102103# Encodes the crealm attribute104#105# @return [OpenSSL::ASN1::GeneralString]106def encode_crealm107OpenSSL::ASN1::GeneralString.new(crealm)108end109110# Encodes the subject_certificate attribute111#112# @return [OpenSSL::ASN1::OctetString]113def encode_subject_certificate114OpenSSL::ASN1::OctetString.new(subject_certificate)115end116117# Encodes the options attribute118#119# @return [OpenSSL::ASN1::BitString]120def encode_options121OpenSSL::ASN1::BitString.new(options)122end123124# Decodes the S4UUserID from a String125#126# @param input [String] the input to decode from127def decode_string(input)128asn1 = OpenSSL::ASN1.decode(input)129130decode_asn1(asn1)131end132133# Decodes the S4UUserID from an OpenSSL::ASN1::Sequence134#135# @param input [OpenSSL::ASN1::Sequence] the input to decode from136def decode_asn1(input)137seq_values = input.value138139seq_values.each do |val|140case val.tag141when 0142self.nonce = val.value[0].value.to_i143when 1144self.cname = Rex::Proto::Kerberos::Model::PrincipalName.decode(val.value[0])145when 2146self.crealm = val.value[0].value147when 3148self.subject_certificate = val.value[0].value149when 4150self.options = val.value[0].value151else152raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode S4UUserID SEQUENCE'153end154end155end156end157end158end159end160end161162