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/host_address.rb
Views: 11766
1
# -*- coding: binary -*-
2
3
# This class provides a representation for Kerberos pre authenticated
4
# data
5
# https://datatracker.ietf.org/doc/html/rfc4120#section-5.2.5
6
# HostAddress ::= SEQUENCE {
7
# addr-type [0] Int32,
8
# address [1] OCTET STRING
9
# }
10
class Rex::Proto::Kerberos::Model::HostAddress < Rex::Proto::Kerberos::Model::Element
11
# @!attribute type
12
# @return [Rex::Proto::Kerberos::Model::AddressType,Integer] The address addr-type
13
attr_accessor :type
14
# @!attribute address
15
# @return [String] The address value
16
attr_accessor :address
17
18
# Decodes a Rex::Proto::Kerberos::Model::HostAddress
19
#
20
# @param input [String, OpenSSL::ASN1::Sequence] the input to decode from
21
# @return [self] if decoding succeeds
22
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
23
def decode(input)
24
case input
25
when String
26
decode_string(input)
27
when OpenSSL::ASN1::Sequence
28
decode_asn1(input)
29
else
30
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode HostAddress, invalid input'
31
end
32
33
self
34
end
35
36
# Encodes a Rex::Proto::Kerberos::Model::HostAddress into an ASN.1 String
37
#
38
# @return [String]
39
def encode
40
to_asn1.to_der
41
end
42
43
# Encodes a Rex::Proto::Kerberos::Model::HostAddress into ASN.1
44
#
45
# @return [OpenSSL::ASN1::ASN1Data] The HostAddress ASN1Data
46
def to_asn1
47
type_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_type], 0, :CONTEXT_SPECIFIC)
48
address_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_address], 1, :CONTEXT_SPECIFIC)
49
seq = OpenSSL::ASN1::Sequence.new([type_asn1, address_asn1])
50
51
seq
52
end
53
54
private
55
56
# Encodes the type
57
#
58
# @return [OpenSSL::ASN1::Integer]
59
def encode_type
60
int_bn = OpenSSL::BN.new(type.to_s)
61
int = OpenSSL::ASN1::Integer.new(int_bn)
62
63
int
64
end
65
66
# Encodes the address
67
#
68
# @return [OpenSSL::ASN1::OctetString]
69
def encode_address
70
OpenSSL::ASN1::OctetString.new(address)
71
end
72
73
# Decodes a Rex::Proto::Kerberos::Model::HostAddress
74
#
75
# @param input [String] the input to decode from
76
def decode_string(input)
77
asn1 = OpenSSL::ASN1.decode(input)
78
79
decode_asn1(asn1)
80
end
81
82
# Decodes a Rex::Proto::Kerberos::Model::HostAddress from an
83
# OpenSSL::ASN1::Sequence
84
#
85
# @param input [OpenSSL::ASN1::Sequence] the input to decode from
86
def decode_asn1(input)
87
seq_values = input.value
88
self.type = decode_asn1_type(seq_values[0])
89
self.address = decode_asn1_address(seq_values[1])
90
end
91
92
# Decodes the type from an OpenSSL::ASN1::ASN1Data
93
#
94
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
95
# @return [Integer]
96
def decode_asn1_type(input)
97
input.value[0].value.to_i
98
end
99
100
# Decodes the address from an OpenSSL::ASN1::ASN1Data
101
#
102
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
103
# @return [Integer]
104
def decode_asn1_address(input)
105
input.value[0].value
106
end
107
end
108
109