Debian Codegraph @

ntlm/av_pairs.go

raw symbols SCIP
1 //Copyright 2013 Thomson Reuters Global Resources. BSD License please see License file for more information
2
3 package ntlm
4
5 import (
6 "bytes"
7 "encoding/binary"
8 "encoding/hex"
9 "fmt"
10 )
11
12 type AvPairType uint16
13
14 // MS-NLMP - 2.2.2.1 AV_PAIR
15 const (
16 // Indicates that this is the last AV_PAIR in the list. AvLen MUST be 0. This type of information MUST be present in the AV pair list.
17 MsvAvEOL AvPairType = iota
18 // The server's NetBIOS computer name. The name MUST be in Unicode, and is not null-terminated. This type of information MUST be present in the AV_pair list.
19 MsvAvNbComputerName
20 // The server's NetBIOS domain name. The name MUST be in Unicode, and is not null-terminated. This type of information MUST be present in the AV_pair list.
21 MsvAvNbDomainName
22 // The fully qualified domain name (FQDN (1)) of the computer. The name MUST be in Unicode, and is not null-terminated.
23 MsvAvDnsComputerName
24 // The FQDN (2) of the domain. The name MUST be in Unicode, and is not null-terminate.
25 MsvAvDnsDomainName
26 // The FQDN (2) of the forest. The name MUST be in Unicode, and is not null-terminated.<11>
27 MsvAvDnsTreeName
28 // A 32-bit value indicating server or client configuration.
29 // 0x00000001: indicates to the client that the account authentication is constrained.
30 // 0x00000002: indicates that the client is providing message integrity in the MIC field (section 2.2.1.3) in the AUTHENTICATE_MESSAGE.<12>
31 // 0x00000004: indicates that the client is providing a target SPN generated from an untrusted source.<13>
32 MsvAvFlags
33 // A FILETIME structure ([MS-DTYP] section 2.3.1) in little-endian byte order that contains the server local time.<14>
34 MsvAvTimestamp
35 //A Restriction_Encoding (section 2.2.2.2) structure. The Value field contains a structure representing the integrity level of the security principal, as well as a MachineID created at computer startup to identify the calling machine.<15>
36 MsAvRestrictions
37 // The SPN of the target server. The name MUST be in Unicode and is not null-terminated.<16>
38 MsvAvTargetName
39 // annel bindings hash. The Value field contains an MD5 hash ([RFC4121] section 4.1.1.2) of a gss_channel_bindings_struct ([RFC2744] section 3.11).
40 // An all-zero value of the hash is used to indicate absence of channel bindings.<17>
41 MsvChannelBindings
42 )
43
44 // Helper struct that contains a list of AvPairs with helper methods for running through them
45 type AvPairs struct {
46 List []AvPair
47 }
48
49 func (p *AvPairs) AddAvPair(avId AvPairType, bytes []byte) {
50 a := &AvPair{AvId: avId, AvLen: uint16(len(bytes)), Value: bytes}
51 p.List = append(p.List, *a)
52 }
53
54 func ReadAvPairs(data []byte) *AvPairs {
55 pairs := new(AvPairs)
56
57 // Get the number of AvPairs and allocate enough AvPair structures to hold them
58 offset := 0
59 for i := 0; len(data) > 0 && i < 11; i++ {
60 pair := ReadAvPair(data, offset)
61 offset = offset + 4 + int(pair.AvLen)
62 pairs.List = append(pairs.List, *pair)
63 if pair.AvId == MsvAvEOL {
64 break
65 }
66 }
67
68 return pairs
69 }
70
71 func (p *AvPairs) Bytes() (result []byte) {
72 totalLength := 0
73 for i := range p.List {
74 a := p.List[i]
75 totalLength = totalLength + int(a.AvLen) + 4
76 }
77
78 result = make([]byte, 0, totalLength)
79 for i := range p.List {
80 a := p.List[i]
81 result = append(result, a.Bytes()...)
82 }
83
84 return result
85 }
86
87 func (p *AvPairs) String() string {
88 var buffer bytes.Buffer
89
90 buffer.WriteString(fmt.Sprintf("Av Pairs (Total %d pairs)\n", len(p.List)))
91
92 for i := range p.List {
93 buffer.WriteString(p.List[i].String())
94 buffer.WriteString("\n")
95 }
96
97 return buffer.String()
98 }
99
100 func (p *AvPairs) Find(avType AvPairType) (result *AvPair) {
101 for i := range p.List {
102 pair := p.List[i]
103 if avType == pair.AvId {
104 result = &pair
105 break
106 }
107 }
108 return
109 }
110
111 func (p *AvPairs) ByteValue(avType AvPairType) (result []byte) {
112 pair := p.Find(avType)
113 if pair != nil {
114 result = pair.Value
115 }
116 return
117 }
118
119 func (p *AvPairs) StringValue(avType AvPairType) (result string) {
120 pair := p.Find(avType)
121 if pair != nil {
122 result = pair.UnicodeStringValue()
123 }
124 return
125 }
126
127 // AvPair as described by MS-NLMP
128 type AvPair struct {
129 AvId AvPairType
130 AvLen uint16
131 Value []byte
132 }
133
134 func ReadAvPair(data []byte, offset int) *AvPair {
135 pair := new(AvPair)
136 pair.AvId = AvPairType(binary.LittleEndian.Uint16(data[offset : offset+2]))
137 pair.AvLen = binary.LittleEndian.Uint16(data[offset+2 : offset+4])
138 pair.Value = data[offset+4 : offset+4+int(pair.AvLen)]
139 return pair
140 }
141
142 func (a *AvPair) UnicodeStringValue() string {
143 return utf16ToString(a.Value)
144 }
145
146 func (a *AvPair) Bytes() (result []byte) {
147 result = make([]byte, 4, a.AvLen+4)
148 result[0] = byte(a.AvId)
149 result[1] = byte(a.AvId >> 8)
150 result[2] = byte(a.AvLen)
151 result[3] = byte(a.AvLen >> 8)
152 result = append(result, a.Value...)
153 return
154 }
155
156 func (a *AvPair) String() string {
157 var outString string
158
159 switch a.AvId {
160 case MsvAvEOL:
161 outString = "MsvAvEOL"
162 case MsvAvNbComputerName:
163 outString = "MsAvNbComputerName: " + a.UnicodeStringValue()
164 case MsvAvNbDomainName:
165 outString = "MsvAvNbDomainName: " + a.UnicodeStringValue()
166 case MsvAvDnsComputerName:
167 outString = "MsvAvDnsComputerName: " + a.UnicodeStringValue()
168 case MsvAvDnsDomainName:
169 outString = "MsvAvDnsDomainName: " + a.UnicodeStringValue()
170 case MsvAvDnsTreeName:
171 outString = "MsvAvDnsTreeName: " + a.UnicodeStringValue()
172 case MsvAvFlags:
173 outString = "MsvAvFlags: " + hex.EncodeToString(a.Value)
174 case MsvAvTimestamp:
175 outString = "MsvAvTimestamp: " + hex.EncodeToString(a.Value)
176 case MsAvRestrictions:
177 outString = "MsAvRestrictions: " + hex.EncodeToString(a.Value)
178 case MsvAvTargetName:
179 outString = "MsvAvTargetName: " + a.UnicodeStringValue()
180 case MsvChannelBindings:
181 outString = "MsvChannelBindings: " + hex.EncodeToString(a.Value)
182 default:
183 outString = fmt.Sprintf("unknown pair type: '%d'", a.AvId)
184 }
185
186 return outString
187 }