class Rex::Proto::Kerberos::Model::EncryptedData

This class provides a representation of an encrypted message.

Attributes

cipher[RW]

@!attribute cipher

@return [String] The enciphered text
etype[RW]

@!attribute name_type

@return [Fixnum] The encryption algorithm
kvno[RW]

@!attribute kvno

@return [Fixnum] The version number of the key

Public Instance Methods

decode(input) click to toggle source

Decodes a Rex::Proto::Kerberos::Model::EncryptedData

@param input [String, OpenSSL::ASN1::Sequence] the input to decode from @return [self] @raise [RuntimeError] if decoding doesn't succeed

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 23
def decode(input)
  case input
  when String
    decode_string(input)
  when OpenSSL::ASN1::Sequence
    decode_asn1(input)
  else
    raise ::RuntimeError, 'Failed to decode EncryptedData Name, invalid input'
  end

  self
end
decrypt(key, msg_type) click to toggle source

Decrypts the cipher with etype encryption schema

@param key [String] the key to decrypt @param msg_type [Fixnum] the message type @return [String] the decrypted `cipher` @raise [RuntimeError] if decryption doesn't succeed @raise [NotImplementedError] if encryption isn't supported

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 64
def decrypt(key, msg_type)
  if cipher.nil? || cipher.empty?
    return ''
  end

  res = ''
  case etype
  when RC4_HMAC
    res = decrypt_rc4_hmac(cipher, key, msg_type)
    raise ::RuntimeError, 'EncryptedData failed to decrypt' if res.length < 8
    res = res[8, res.length - 1]
  else
    raise ::NotImplementedError, 'EncryptedData schema is not supported'
  end

  res
end
encode() click to toggle source

Encodes a Rex::Proto::Kerberos::Model::EncryptedData into an ASN.1 String

@return [String]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 39
def encode
  elems = []
  etype_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_etype], 0, :CONTEXT_SPECIFIC)
  elems << etype_asn1

  if kvno
    kvno_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_kvno], 1, :CONTEXT_SPECIFIC)
    elems << kvno_asn1
  end

  cipher_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_cipher], 2, :CONTEXT_SPECIFIC)
  elems << cipher_asn1

  seq = OpenSSL::ASN1::Sequence.new(elems)

  seq.to_der
end

Private Instance Methods

decode_asn1(input) click to toggle source

Decodes a Rex::Proto::Kerberos::Model::EncryptedData from an OpenSSL::ASN1::Sequence

@param input [OpenSSL::ASN1::Sequence] the input to decode from @raise [RuntimeError] if decoding doesn't succeed

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 125
def decode_asn1(input)
  seq_values = input.value

  seq_values.each do |val|
    case val.tag
    when 0
      self.etype = decode_etype(val)
    when 1
      self.kvno = decode_kvno(val)
    when 2
      self.cipher = decode_cipher(val)
    else
      raise ::RuntimeError, 'Failed to decode EncryptedData SEQUENCE'
    end
  end
end
decode_cipher(input) click to toggle source

Decodes the cipher from an OpenSSL::ASN1::ASN1Data

@param input [OpenSSL::ASN1::ASN1Data] the input to decode from @return [Sting]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 162
def decode_cipher(input)
  input.value[0].value
end
decode_etype(input) click to toggle source

Decodes the etype from an OpenSSL::ASN1::ASN1Data

@param input [OpenSSL::ASN1::ASN1Data] the input to decode from @return [Fixnum]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 146
def decode_etype(input)
  input.value[0].value.to_i
end
decode_kvno(input) click to toggle source

Decodes the kvno from an OpenSSL::ASN1::ASN1Data

@param input [OpenSSL::ASN1::ASN1Data] the input to decode from @return [Fixnum]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 154
def decode_kvno(input)
  input.value[0].value.to_i
end
decode_string(input) click to toggle source

Decodes a Rex::Proto::Kerberos::Model::EncryptedData from an String

@param input [String] the input to decode from

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 114
def decode_string(input)
  asn1 = OpenSSL::ASN1.decode(input)

  decode_asn1(asn1)
end
encode_cipher() click to toggle source

Encodes the cipher

@return [OpenSSL::ASN1::OctetString]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 107
def encode_cipher
  OpenSSL::ASN1::OctetString.new(cipher)
end
encode_etype() click to toggle source

Encodes the etype

@return [OpenSSL::ASN1::Integer]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 87
def encode_etype
  bn = OpenSSL::BN.new(etype.to_s)
  int = OpenSSL::ASN1::Integer.new(bn)

  int
end
encode_kvno() click to toggle source

Encodes the kvno

@raise [RuntimeError]

# File lib/rex/proto/kerberos/model/encrypted_data.rb, line 97
def encode_kvno
  bn = OpenSSL::BN.new(kvno.to_s)
  int = OpenSSL::ASN1::Integer.new(bn)

  int
end