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/encrypted_data.rb
Views: 11765
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of an encrypted message.7class EncryptedData < Element8# @!attribute name_type9# @return [Integer] The encryption algorithm10attr_accessor :etype11# @!attribute kvno12# @return [Integer] The version number of the key13attr_accessor :kvno14# @!attribute cipher15# @return [String] The enciphered text16attr_accessor :cipher1718def ==(other)19etype == other.etype &&20kvno == other.kvno &&21cipher == other.cipher22end2324# Decodes a Rex::Proto::Kerberos::Model::EncryptedData25#26# @param input [String, OpenSSL::ASN1::Sequence] the input to decode from27# @return [self]28# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed29def decode(input)30case input31when String32decode_string(input)33when OpenSSL::ASN1::Sequence34decode_asn1(input)35else36raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode EncryptedData Name, invalid input'37end3839self40end4142# Encodes a Rex::Proto::Kerberos::Model::EncryptedData into an ASN.1 String43#44# @return [String]45def encode46elems = []47etype_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_etype], 0, :CONTEXT_SPECIFIC)48elems << etype_asn14950if kvno51kvno_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_kvno], 1, :CONTEXT_SPECIFIC)52elems << kvno_asn153end5455cipher_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_cipher], 2, :CONTEXT_SPECIFIC)56elems << cipher_asn15758seq = OpenSSL::ASN1::Sequence.new(elems)5960seq.to_der61end6263# Decrypts the cipher with etype encryption schema, presuming that the64# data is an ASN1 structure65#66# @param key [String] the key to decrypt67# @param msg_type [Integer] the message type68# @return [String] the decrypted `cipher`69# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decryption doesn't succeed70# @raise [NotImplementedError] if encryption isn't supported71def decrypt_asn1(key, msg_type)72if cipher.nil? || cipher.empty?73return ''74end7576encryptor = Rex::Proto::Kerberos::Crypto::Encryption::from_etype(etype)77encryptor.decrypt_asn1(cipher, key, msg_type)78end798081private8283# Encodes the etype84#85# @return [OpenSSL::ASN1::Integer]86def encode_etype87bn = OpenSSL::BN.new(etype.to_s)88int = OpenSSL::ASN1::Integer.new(bn)8990int91end9293# Encodes the kvno94#95# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError]96def encode_kvno97bn = OpenSSL::BN.new(kvno.to_s)98int = OpenSSL::ASN1::Integer.new(bn)99100int101rescue OpenSSL::ASN1::ASN1Error102raise Rex::Proto::Kerberos::Model::Error::KerberosDecodingError103end104105# Encodes the cipher106#107# @return [OpenSSL::ASN1::OctetString]108def encode_cipher109OpenSSL::ASN1::OctetString.new(cipher)110end111112# Decodes a Rex::Proto::Kerberos::Model::EncryptedData from an String113#114# @param input [String] the input to decode from115def decode_string(input)116asn1 = OpenSSL::ASN1.decode(input)117118decode_asn1(asn1)119end120121# Decodes a Rex::Proto::Kerberos::Model::EncryptedData from an122# OpenSSL::ASN1::Sequence123#124# @param input [OpenSSL::ASN1::Sequence] the input to decode from125# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed126def decode_asn1(input)127seq_values = input.value128129seq_values.each do |val|130case val.tag131when 0132self.etype = decode_etype(val)133when 1134self.kvno = decode_kvno(val)135when 2136self.cipher = decode_cipher(val)137else138raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode EncryptedData SEQUENCE'139end140end141end142143# Decodes the etype from an OpenSSL::ASN1::ASN1Data144#145# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from146# @return [Integer]147def decode_etype(input)148input.value[0].value.to_i149end150151# Decodes the kvno from an OpenSSL::ASN1::ASN1Data152#153# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from154# @return [Integer]155def decode_kvno(input)156input.value[0].value.to_i157end158159# Decodes the cipher from an OpenSSL::ASN1::ASN1Data160#161# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from162# @return [String]163def decode_cipher(input)164input.value[0].value165end166167end168end169end170end171end172173174