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/authorization_data.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a Kerberos AuthorizationData data7# definition.8class AuthorizationData < Element9# @!attribute elements10# @return [Array<Hash{Symbol => Integer, String)}>] The type of the authorization data11# @option [Integer] :type12# @option [String] :data13attr_accessor :elements1415# Decodes the Rex::Proto::Kerberos::Model::AuthorizationData from an input16#17# @param input [String, OpenSSL::ASN1::Sequence] the input to decode from18# @return [self] if decoding succeeds19# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed20def decode(input)21case input22when String23decode_string(input)24when OpenSSL::ASN1::ASN1Data25decode_asn1(input)26else27raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode AuthorizationData, invalid input'28end2930self31end3233# Encodes a Rex::Proto::Kerberos::Model::AuthorizationData into an ASN.1 String34#35# @return [String]36def encode37seqs = []38elements.each do |elem|39elems = []40type_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_type(elem[:type])], 0, :CONTEXT_SPECIFIC)41elems << type_asn142data_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_data(elem[:data])], 1, :CONTEXT_SPECIFIC)43elems << data_asn144seqs << OpenSSL::ASN1::Sequence.new(elems)45end4647seq = OpenSSL::ASN1::Sequence.new(seqs)4849seq.to_der50end5152# Decodes a Rex::Proto::Kerberos::Model::AuthorizationData from an String53#54# @param input [String] the input to decode from55def decode_string(input)56asn1 = OpenSSL::ASN1.decode(input)5758decode_asn1(asn1)59end6061# Decodes a Rex::Proto::Kerberos::Model::AuthorizationData62#63# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from64# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed65#66# TransitedEncoding ::= SEQUENCE {67# ad-type [0] Int32 -- must be registered --,68# ad-data [1] OCTET STRING69# }70def decode_asn1(input)71self.elements = []72input.each do |elem|73element = {}74elem.value.each do |val|75case val.tag76when 0 # ad-type [0] Int3277element[:type] = decode_type(val)78when 1 # ad-data [1] OCTET STRING79element[:data] = decode_data(val)80else81raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode AuthorizationData SEQUENCE'82end83end84self.elements << element85end86end8788# Encrypts the Rex::Proto::Kerberos::Model::AuthorizationData89#90# @param etype [Integer] the crypto schema to encrypt91# @param key [String] the key to encrypt92# @return [String] the encrypted result93# @raise [NotImplementedError] if encryption schema isn't supported94def encrypt(etype, key)95data = self.encode9697encryptor = Rex::Proto::Kerberos::Crypto::Encryption::from_etype(etype)98encryptor.encrypt(data, key, 5)99end100101102private103104# Decodes the type from an OpenSSL::ASN1::ASN1Data105#106# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from107# @return [Integer]108def decode_type(input)109input.value[0].value.to_i110end111112# Encodes the type113#114# @return [OpenSSL::ASN1::Integer]115def encode_type(type)116bn = OpenSSL::BN.new(type.to_s)117int = OpenSSL::ASN1::Integer.new(bn)118119int120end121122# Decodes the value from an OpenSSL::ASN1::ASN1Data123#124# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from125# @return [String]126def decode_data(input)127input.value[0].value128end129130# Encodes the data131#132# @return [OpenSSL::ASN1::OctetString]133def encode_data(data)134OpenSSL::ASN1::OctetString.new(data)135end136end137end138end139end140end141142143