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/checksum.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 Checksum definition.
8
class Checksum < Element
9
10
# @!attribute type
11
# @return [Integer] The algorithm used to generate the checksum
12
attr_accessor :type
13
# @!attribute checksum
14
# @return [String] The checksum itself
15
attr_accessor :checksum
16
17
# Decodes the Rex::Proto::Kerberos::Model::Checksum from an input
18
#
19
# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
20
# @return [self] if decoding succeeds
21
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
22
def decode(input)
23
case input
24
when String
25
decode_string(input)
26
when OpenSSL::ASN1::ASN1Data
27
decode_asn1(input)
28
else
29
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode Checksum, invalid input'
30
end
31
32
self
33
end
34
35
# Encodes a Rex::Proto::Kerberos::Model::Checksum into an ASN.1 String
36
#
37
# @return [String]
38
def encode
39
elems = []
40
elems << OpenSSL::ASN1::ASN1Data.new([encode_type], 0, :CONTEXT_SPECIFIC)
41
elems << OpenSSL::ASN1::ASN1Data.new([encode_checksum], 1, :CONTEXT_SPECIFIC)
42
43
seq = OpenSSL::ASN1::Sequence.new(elems)
44
45
seq.to_der
46
end
47
48
private
49
50
# Encodes the type field
51
#
52
# @return [OpenSSL::ASN1::Integer]
53
def encode_type
54
bn = OpenSSL::BN.new(type.to_s)
55
int = OpenSSL::ASN1::Integer.new(bn)
56
57
int
58
end
59
60
# Encodes the checksum field
61
#
62
# @return [OpenSSL::ASN1::OctetString]
63
def encode_checksum
64
OpenSSL::ASN1::OctetString.new(checksum)
65
end
66
67
# Decodes a Rex::Proto::Kerberos::Model::Checksum from an String
68
#
69
# @param input [String] the input to decode from
70
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
71
def decode_string(input)
72
asn1 = OpenSSL::ASN1.decode(input)
73
74
decode_asn1(asn1)
75
rescue OpenSSL::ASN1::ASN1Error
76
raise Rex::Proto::Kerberos::Model::Error::KerberosDecodingError
77
end
78
79
# Decodes a Rex::Proto::Kerberos::Model::Checksum from an
80
# OpenSSL::ASN1::Sequence
81
#
82
# @param input [OpenSSL::ASN1::Sequence] the input to decode from
83
# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed
84
def decode_asn1(input)
85
seq_values = input.value
86
87
seq_values.each do |val|
88
case val.tag
89
when 0
90
self.type = decode_type(val)
91
when 1
92
self.checksum = decode_checksum(val)
93
else
94
raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode KdcRequestBody SEQUENCE'
95
end
96
end
97
end
98
99
# Decodes the type field
100
#
101
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
102
# @return [Integer]
103
def decode_type(input)
104
input.value[0].value.to_i
105
end
106
107
# Decodes the checksum field
108
#
109
# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
110
# @return [String]
111
def decode_checksum(input)
112
input.value[0].value
113
end
114
115
end
116
end
117
end
118
end
119
end
120
121