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/kdc_request.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 KDC-REQ (request) data
8
# definition
9
class KdcRequest < 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 pa_data
17
# @return [Array<Rex::Proto::Kerberos::Model::PreAuthDataEntry>] Authentication information which may
18
# be needed before credentials can be issued or decrypted
19
attr_accessor :pa_data
20
# @!attribute req_body
21
# @return [Rex::Proto::Kerberos::Model:::KdcRequestBody] The request body
22
attr_accessor :req_body
23
24
# Decodes the Rex::Proto::Kerberos::Model::KdcRequest from an input
25
#
26
# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
27
# @return [self] if decoding succeeds
28
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
29
def decode(input)
30
case input
31
when String
32
decode_string(input)
33
when OpenSSL::ASN1::ASN1Data
34
decode_asn1(input)
35
else
36
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode KdcRequest, invalid input'
37
end
38
39
self
40
end
41
42
# Encodes the Rex::Proto::Kerberos::Model::KdcRequest into an ASN.1 String
43
#
44
# @return [String]
45
def encode
46
pvno_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_pvno], 1, :CONTEXT_SPECIFIC)
47
msg_type_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_msg_type], 2, :CONTEXT_SPECIFIC)
48
pa_data_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_pa_data], 3, :CONTEXT_SPECIFIC)
49
req_body_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_req_body], 4, :CONTEXT_SPECIFIC)
50
seq = OpenSSL::ASN1::Sequence.new([pvno_asn1, msg_type_asn1, pa_data_asn1, req_body_asn1])
51
seq_asn1 = OpenSSL::ASN1::ASN1Data.new([seq], msg_type, :APPLICATION)
52
seq_asn1.to_der
53
end
54
55
private
56
57
# Encodes the pvno field
58
#
59
# @return [OpenSSL::ASN1::Integer]
60
def encode_pvno
61
bn = OpenSSL::BN.new(pvno.to_s)
62
int = OpenSSL::ASN1::Integer.new(bn)
63
64
int
65
end
66
67
# Encodes the msg_type field
68
#
69
# @return [OpenSSL::ASN1::Integer]
70
def encode_msg_type
71
bn = OpenSSL::BN.new(msg_type.to_s)
72
int = OpenSSL::ASN1::Integer.new(bn)
73
74
int
75
end
76
77
# Encodes the pa_data field
78
#
79
# @return [String]
80
def encode_pa_data
81
elems = []
82
pa_data.each do |data|
83
elems << data.encode
84
end
85
86
OpenSSL::ASN1::Sequence.new(elems)
87
end
88
89
# Encodes the req_body field
90
#
91
# @return [String]
92
def encode_req_body
93
req_body.encode
94
end
95
96
# Decodes a Rex::Proto::Kerberos::Model::KdcRequest from an String
97
#
98
# @param input [String] the input to decode from
99
def decode_string(input)
100
asn1 = OpenSSL::ASN1.decode(input)
101
102
decode_asn1(asn1)
103
end
104
105
# Decodes a Rex::Proto::Kerberos::Model::KdcRequest
106
#
107
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
108
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
109
def decode_asn1(input)
110
input.value[0].value.each do |val|
111
case val.tag
112
when 1
113
self.pvno = decode_asn1_pvno(val)
114
when 2
115
self.msg_type = decode_asn1_msg_type(val)
116
when 3
117
self.pa_data = decode_asn1_pa_data(val)
118
when 4
119
self.req_body = decode_asn1_req_body(val)
120
else
121
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode KdcRequest SEQUENCE'
122
end
123
end
124
end
125
126
# Decodes the pvno from an OpenSSL::ASN1::ASN1Data
127
#
128
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
129
# @return [Integer]
130
def decode_asn1_pvno(input)
131
input.value[0].value.to_i
132
end
133
134
# Decodes the msg_type from an OpenSSL::ASN1::ASN1Data
135
#
136
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
137
# @return [Integer]
138
def decode_asn1_msg_type(input)
139
input.value[0].value.to_i
140
end
141
142
# Decodes the pa_data from an OpenSSL::ASN1::ASN1Data
143
#
144
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
145
# @return [Array<Rex::Proto::Kerberos::Model::PreAuthDataEntry>]
146
def decode_asn1_pa_data(input)
147
pre_auth = []
148
input.value[0].value.each do |pre_auth_data|
149
pre_auth << Rex::Proto::Kerberos::Model::PreAuthDataEntry.decode(pre_auth_data)
150
end
151
152
pre_auth
153
end
154
155
# Decodes the req_body from an OpenSSL::ASN1::ASN1Data
156
#
157
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
158
# @return [Rex::Proto::Kerberos::Model::KdcRequestBody]
159
def decode_asn1_req_body(input)
160
Rex::Proto::Kerberos::Model::KdcRequestBody.decode(input.value[0])
161
end
162
end
163
end
164
end
165
end
166
end
167
168