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/principal_name.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 principal, an asset (e.g., a
8
# workstation user or a network server) on a network.
9
class PrincipalName < Element
10
11
# @!attribute name_type
12
# @return [Integer] The type of name
13
attr_accessor :name_type
14
# @!attribute name_string
15
# @return [Array<String>] A sequence of strings that form a name.
16
attr_accessor :name_string
17
18
def ==(other)
19
name_type == other.name_type &&
20
name_string == other.name_string
21
end
22
23
# Decodes a Rex::Proto::Kerberos::Model::PrincipalName
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 Principal Name, invalid input'
36
end
37
38
self
39
end
40
41
# Encodes a Rex::Proto::Kerberos::Model::PrincipalName into an
42
# ASN.1 String
43
#
44
# @return [String]
45
def encode
46
integer_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_name_type], 0, :CONTEXT_SPECIFIC)
47
string_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_name_string], 1, :CONTEXT_SPECIFIC)
48
seq = OpenSSL::ASN1::Sequence.new([integer_asn1, string_asn1])
49
50
seq.to_der
51
end
52
53
def to_s
54
name_string.join('/')
55
end
56
57
private
58
59
# Encodes the name_type
60
#
61
# @return [OpenSSL::ASN1::Integer]
62
def encode_name_type
63
int_bn = OpenSSL::BN.new(name_type.to_s)
64
int = OpenSSL::ASN1::Integer.new(int_bn)
65
66
int
67
end
68
69
# Encodes the name_string
70
#
71
# @return [OpenSSL::ASN1::Sequence]
72
def encode_name_string
73
strings = []
74
name_string.each do |s|
75
strings << OpenSSL::ASN1::GeneralString.new(s)
76
end
77
seq_string = OpenSSL::ASN1::Sequence.new(strings)
78
79
seq_string
80
end
81
82
# Decodes a Rex::Proto::Kerberos::Model::PrincipalName from an String
83
#
84
# @param input [String] the input to decode from
85
def decode_string(input)
86
asn1 = OpenSSL::ASN1.decode(input)
87
88
decode_asn1(asn1)
89
end
90
91
# Decodes a Rex::Proto::Kerberos::Model::PrincipalName from an
92
# OpenSSL::ASN1::Sequence
93
#
94
# @param input [OpenSSL::ASN1::Sequence] the input to decode from
95
def decode_asn1(input)
96
seq_values = input.value
97
self.name_type = decode_name_type(seq_values[0])
98
self.name_string = decode_name_string(seq_values[1])
99
end
100
101
# Decodes the name_type from an OpenSSL::ASN1::ASN1Data
102
#
103
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
104
# @return [Integer]
105
def decode_name_type(input)
106
input.value[0].value.to_i
107
end
108
109
# Decodes the name_string from an OpenSSL::ASN1::ASN1Data
110
#
111
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
112
# @return [Array<String>]
113
def decode_name_string(input)
114
strings = []
115
input.value[0].value.each do |v|
116
strings << v.value
117
end
118
119
strings
120
end
121
end
122
end
123
end
124
end
125
end
126
127