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/ap_rep.rb
Views: 11766
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 AP-REP (response) data
8
# definition
9
class ApRep < Element
10
# @!attribute pvno
11
# @return [Integer] The protocol version number
12
attr_accessor :pvno
13
# @!attribute msg_type
14
# @return [Integer] The type of a protocol message
15
attr_accessor :msg_type
16
# @!attribute enc_part
17
# @return [Rex::Proto::Kerberos::Model::EncryptedData] The encrypted part of the response
18
attr_accessor :enc_part
19
20
# Decodes the Rex::Proto::Kerberos::Model::ApRep from an input
21
#
22
# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
23
# @return [self] if decoding succeeds
24
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
25
def decode(input)
26
case input
27
when String
28
decode_string(input)
29
when OpenSSL::ASN1::ASN1Data
30
decode_asn1(input)
31
else
32
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode ApRep, invalid input'
33
end
34
35
self
36
end
37
38
# Encodes the Rex::Proto::Kerberos::Model::ApReq into an ASN.1 String
39
#
40
# @return [String]
41
def encode
42
to_asn1.to_der
43
end
44
45
# @return [OpenSSL::ASN1::ASN1Data] The ap_req ASN1Data
46
def to_asn1
47
elems = []
48
49
elems << OpenSSL::ASN1::ASN1Data.new([encode_pvno], 0, :CONTEXT_SPECIFIC)
50
elems << OpenSSL::ASN1::ASN1Data.new([encode_msg_type], 1, :CONTEXT_SPECIFIC)
51
elems << OpenSSL::ASN1::ASN1Data.new([encode_enc_part], 2, :CONTEXT_SPECIFIC)
52
53
seq = OpenSSL::ASN1::Sequence.new(elems)
54
55
seq_asn1 = OpenSSL::ASN1::ASN1Data.new([seq], AP_REP, :APPLICATION)
56
seq_asn1
57
end
58
59
def decrypt_enc_part(key)
60
data = enc_part.decrypt_asn1(key, Rex::Proto::Kerberos::Crypto::KeyUsage::AP_REP_ENCPART)
61
62
result = Rex::Proto::Kerberos::Model::EncApRepPart::decode(data)
63
end
64
65
private
66
67
# Encodes the pvno field
68
#
69
# @return [OpenSSL::ASN1::Integer]
70
def encode_pvno
71
bn = OpenSSL::BN.new(pvno.to_s)
72
int = OpenSSL::ASN1::Integer.new(bn)
73
74
int
75
end
76
77
# Encodes the msg_type field
78
#
79
# @return [OpenSSL::ASN1::Integer]
80
def encode_msg_type
81
bn = OpenSSL::BN.new(msg_type.to_s)
82
int = OpenSSL::ASN1::Integer.new(bn)
83
84
int
85
end
86
87
# Encodes the enc_part field
88
#
89
# @return [String]
90
def encode_enc_part
91
enc_part.encode
92
end
93
94
# Decodes a Rex::Proto::Kerberos::Model::ApRep from an String
95
#
96
# @param input [String] the input to decode from
97
def decode_string(input)
98
asn1 = OpenSSL::ASN1.decode(input)
99
100
decode_asn1(asn1)
101
end
102
103
# Decodes a Rex::Proto::Kerberos::Model::ApRep
104
#
105
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
106
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
107
def decode_asn1(input)
108
input.value[0].value.each do |val|
109
case val.tag
110
when 0
111
self.pvno = decode_pvno(val)
112
when 1
113
self.msg_type = decode_msg_type(val)
114
when 2
115
self.enc_part = decode_enc_part(val)
116
else
117
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, "Failed to decode AP-REP SEQUENCE (#{val.tag})"
118
end
119
end
120
end
121
122
# Decodes the pvno from an OpenSSL::ASN1::ASN1Data
123
#
124
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
125
# @return [Integer]
126
def decode_pvno(input)
127
input.value[0].value.to_i
128
end
129
130
# Decodes the msg_type from an OpenSSL::ASN1::ASN1Data
131
#
132
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
133
# @return [Integer]
134
def decode_msg_type(input)
135
input.value[0].value.to_i
136
end
137
138
# Decodes the enc_part
139
#
140
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
141
# @return [Rex::Proto::Kerberos::Model::EncryptedData]
142
def decode_enc_part(input)
143
Rex::Proto::Kerberos::Model::EncryptedData.decode(input.value[0])
144
end
145
end
146
end
147
end
148
end
149
end
150
151