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/transited_encoding.rb
Views: 11766
1
# -*- coding: binary -*-
2
3
module Rex::Proto::Kerberos::Model
4
# This class provides a representation of a Kerberos ticket that helps
5
# a client authenticate to a service.
6
class TransitedEncoding < Element
7
8
# @return [Integer] [0] Int32 -- must be registered --
9
attr_accessor :tr_type
10
# @return [String] [1] OCTET STRING
11
attr_accessor :contents
12
13
14
# Decodes the Rex::Proto::Kerberos::Model::TransitedEncoding from an input
15
#
16
# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
17
# @return [self] if decoding succeeds
18
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
19
def decode(input)
20
case input
21
when String
22
decode_string(input)
23
when OpenSSL::ASN1::ASN1Data
24
decode_asn1(input)
25
else
26
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode TransitedEncoding, invalid input'
27
end
28
29
self
30
end
31
32
# Encodes a Rex::Proto::Kerberos::Model::TransitedEncoding into an ASN.1 String
33
#
34
# @return [String]
35
def encode
36
to_asn1.to_der
37
end
38
39
# Encodes a Rex::Proto::Kerberos::Model::TransitedEncoding into ASN.1
40
#
41
# @return [OpenSSL::ASN1::ASN1Data] The TransitedEncoding ASN1Data
42
def to_asn1
43
elems = []
44
elems << OpenSSL::ASN1::ASN1Data.new([encode_tr_type], 0, :CONTEXT_SPECIFIC)
45
elems << OpenSSL::ASN1::ASN1Data.new([encode_contents], 1, :CONTEXT_SPECIFIC)
46
47
OpenSSL::ASN1::Sequence.new(elems)
48
end
49
50
private
51
52
# Decodes a Rex::Proto::Kerberos::Model::TicketEncPart from an String
53
#
54
# @param input [String] the input to decode from
55
def decode_string(input)
56
asn1 = OpenSSL::ASN1.decode(input)
57
58
decode_asn1(asn1)
59
end
60
61
# Decodes a Rex::Proto::Kerberos::Model::TransitedEncoding
62
#
63
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
64
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
65
#
66
# TransitedEncoding ::= SEQUENCE {
67
# tr-type [0] Int32 -- must be registered --,
68
# contents [1] OCTET STRING
69
# }
70
def decode_asn1(input)
71
input.value.each do |val|
72
case val.tag
73
when 0 # tr-type [0] Int32 -- must be registered --,
74
self.tr_type = decode_tr_type(val)
75
when 1 # contents [1] OCTET STRING
76
self.contents = decode_contents(val)
77
else
78
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode TransitedEncoding SEQUENCE'
79
end
80
end
81
end
82
83
# Decodes the type from an OpenSSL::ASN1::ASN1Data
84
#
85
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
86
# @return [Integer]
87
def decode_tr_type(input)
88
input.value[0].value.to_i
89
end
90
91
# Encodes the type
92
#
93
# @return [OpenSSL::ASN1::Integer]
94
def encode_tr_type
95
bn = OpenSSL::BN.new(tr_type.to_s)
96
OpenSSL::ASN1::Integer.new(bn)
97
end
98
99
# Decodes the address from an OpenSSL::ASN1::ASN1Data
100
#
101
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
102
# @return [String]
103
def decode_contents(input)
104
input.value[0].value
105
end
106
107
# Encodes the contents
108
#
109
# @return [OpenSSL::ASN1::OctetString]
110
def encode_contents
111
OpenSSL::ASN1::OctetString.new(contents)
112
end
113
end
114
end
115
116