CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/kerberos/model/encryption_key.rb
Views: 11765
1
# -*- coding: binary -*-
2
3
module Rex
4
module Proto
5
module Kerberos
6
module Model
7
# This class provides a representation of a Kerberos EncryptionKey data
8
# definition
9
class EncryptionKey < Element
10
11
# @!attribute key
12
# @return [Integer] The type of encryption key
13
attr_accessor :type
14
# @!attribute value
15
# @return [String] the key itself
16
attr_accessor :value
17
18
def ==(other)
19
type == other.type &&
20
value == other.value
21
end
22
23
# Decodes a Rex::Proto::Kerberos::Model::EncryptionKey
24
#
25
# @param input [String, OpenSSL::ASN1::Sequence] the input to decode from
26
# @return [self] if decoding succeeds
27
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
28
def decode(input)
29
case input
30
when String
31
decode_string(input)
32
when OpenSSL::ASN1::Sequence
33
decode_asn1(input)
34
else
35
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode EncryptionKey, invalid input'
36
end
37
38
self
39
end
40
41
# Encodes a Rex::Proto::Kerberos::Model::EncryptionKey into an
42
# ASN.1 String
43
#
44
# @return [String]
45
def encode
46
elems = []
47
elems << OpenSSL::ASN1::ASN1Data.new([encode_type], 0, :CONTEXT_SPECIFIC)
48
elems << OpenSSL::ASN1::ASN1Data.new([encode_value], 1, :CONTEXT_SPECIFIC)
49
seq = OpenSSL::ASN1::Sequence.new(elems)
50
51
seq.to_der
52
end
53
54
private
55
56
# Decodes a Rex::Proto::Kerberos::Model::EncryptionKey from an String
57
#
58
# @param input [String] the input to decode from
59
def decode_string(input)
60
asn1 = OpenSSL::ASN1.decode(input)
61
62
decode_asn1(asn1)
63
end
64
65
# Decodes a Rex::Proto::Kerberos::Model::EncryptionKey from an
66
# OpenSSL::ASN1::Sequence
67
#
68
# @param input [OpenSSL::ASN1::Sequence] the input to decode from
69
def decode_asn1(input)
70
seq_values = input.value
71
self.type = decode_type(seq_values[0])
72
self.value = decode_value(seq_values[1])
73
end
74
75
# Decodes the type from an OpenSSL::ASN1::ASN1Data
76
#
77
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
78
# @return [Integer]
79
def decode_type(input)
80
input.value[0].value.to_i
81
end
82
83
# Decodes the value from an OpenSSL::ASN1::ASN1Data
84
#
85
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
86
# @return [String]
87
def decode_value(input)
88
input.value[0].value
89
end
90
91
# Encodes the type field
92
#
93
# @return [OpenSSL::ASN1::Integer]
94
def encode_type
95
bn = OpenSSL::BN.new(type.to_s)
96
int = OpenSSL::ASN1::Integer.new(bn)
97
98
int
99
end
100
101
# Encodes the value field
102
#
103
# @return [OpenSSL::ASN1::OctetString]
104
def encode_value
105
OpenSSL::ASN1::OctetString.new(value)
106
end
107
end
108
end
109
end
110
end
111
end
112
113