Debian Codegraph @

pac/pac_type.go

raw symbols SCIP
1 package pac
2
3 import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7
8 "gopkg.in/jcmturner/gokrb5.v5/crypto"
9 "gopkg.in/jcmturner/gokrb5.v5/iana/keyusage"
10 "gopkg.in/jcmturner/gokrb5.v5/types"
11 "gopkg.in/jcmturner/rpc.v0/ndr"
12 )
13
14 // PACType implements: https://msdn.microsoft.com/en-us/library/cc237950.aspx
15 type PACType struct {
16 CBuffers uint32
17 Version uint32
18 Buffers []InfoBuffer
19 Data []byte
20 KerbValidationInfo *KerbValidationInfo
21 CredentialsInfo *CredentialsInfo
22 ServerChecksum *SignatureData
23 KDCChecksum *SignatureData
24 ClientInfo *ClientInfo
25 S4UDelegationInfo *S4UDelegationInfo
26 UPNDNSInfo *UPNDNSInfo
27 ClientClaimsInfo *ClientClaimsInfo
28 DeviceInfo *DeviceInfo
29 DeviceClaimsInfo *DeviceClaimsInfo
30 ZeroSigData []byte
31 }
32
33 // Unmarshal bytes into the PACType struct
34 func (pac *PACType) Unmarshal(b []byte) error {
35 var p int
36 var e binary.ByteOrder = binary.LittleEndian
37 pac.Data = b
38 zb := make([]byte, len(b), len(b))
39 copy(zb, b)
40 pac.ZeroSigData = zb
41 pac.CBuffers = ndr.ReadUint32(&b, &p, &e)
42 pac.Version = ndr.ReadUint32(&b, &p, &e)
43 buf := make([]InfoBuffer, pac.CBuffers, pac.CBuffers)
44 for i := range buf {
45 buf[i] = ReadPACInfoBuffer(&b, &p, &e)
46 }
47 pac.Buffers = buf
48 return nil
49 }
50
51 // ProcessPACInfoBuffers processes the PAC Info Buffers.
52 // https://msdn.microsoft.com/en-us/library/cc237954.aspx
53 func (pac *PACType) ProcessPACInfoBuffers(key types.EncryptionKey) error {
54 for _, buf := range pac.Buffers {
55 p := make([]byte, buf.CBBufferSize, buf.CBBufferSize)
56 copy(p, pac.Data[int(buf.Offset):int(buf.Offset)+int(buf.CBBufferSize)])
57 switch int(buf.ULType) {
58 case ulTypeKerbValidationInfo:
59 if pac.KerbValidationInfo != nil {
60 //Must ignore subsequent buffers of this type
61 continue
62 }
63 var k KerbValidationInfo
64 err := k.Unmarshal(p)
65 if err != nil {
66 return fmt.Errorf("error processing KerbValidationInfo: %v", err)
67 }
68 pac.KerbValidationInfo = &k
69 case ulTypeCredentials:
70 // Currently PAC parsing is only useful on the service side in gokrb5
71 // The CredentialsInfo are only useful when gokrb5 has implemented RFC4556 and only applied on the client side.
72 // Skipping CredentialsInfo - will be revisited under RFC4556 implementation.
73 continue
74 //if pac.CredentialsInfo != nil {
75 // //Must ignore subsequent buffers of this type
76 // continue
77 //}
78 //var k CredentialsInfo
79 //err := k.Unmarshal(p, key) // The encryption key used is the AS reply key only available to the client.
80 //if err != nil {
81 // return fmt.Errorf("error processing CredentialsInfo: %v", err)
82 //}
83 //pac.CredentialsInfo = &k
84 case ulTypePACServerSignatureData:
85 if pac.ServerChecksum != nil {
86 //Must ignore subsequent buffers of this type
87 continue
88 }
89 var k SignatureData
90 zb, err := k.Unmarshal(p)
91 copy(pac.ZeroSigData[int(buf.Offset):int(buf.Offset)+int(buf.CBBufferSize)], zb)
92 if err != nil {
93 return fmt.Errorf("error processing ServerChecksum: %v", err)
94 }
95 pac.ServerChecksum = &k
96 case ulTypePACKDCSignatureData:
97 if pac.KDCChecksum != nil {
98 //Must ignore subsequent buffers of this type
99 continue
100 }
101 var k SignatureData
102 zb, err := k.Unmarshal(p)
103 copy(pac.ZeroSigData[int(buf.Offset):int(buf.Offset)+int(buf.CBBufferSize)], zb)
104 if err != nil {
105 return fmt.Errorf("error processing KDCChecksum: %v", err)
106 }
107 pac.KDCChecksum = &k
108 case ulTypePACClientInfo:
109 if pac.ClientInfo != nil {
110 //Must ignore subsequent buffers of this type
111 continue
112 }
113 var k ClientInfo
114 err := k.Unmarshal(p)
115 if err != nil {
116 return fmt.Errorf("error processing ClientInfo: %v", err)
117 }
118 pac.ClientInfo = &k
119 case ulTypeS4UDelegationInfo:
120 if pac.S4UDelegationInfo != nil {
121 //Must ignore subsequent buffers of this type
122 continue
123 }
124 var k S4UDelegationInfo
125 err := k.Unmarshal(p)
126 if err != nil {
127 return fmt.Errorf("error processing S4U_DelegationInfo: %v", err)
128 }
129 pac.S4UDelegationInfo = &k
130 case ulTypeUPNDNSInfo:
131 if pac.UPNDNSInfo != nil {
132 //Must ignore subsequent buffers of this type
133 continue
134 }
135 var k UPNDNSInfo
136 err := k.Unmarshal(p)
137 if err != nil {
138 return fmt.Errorf("error processing UPN_DNSInfo: %v", err)
139 }
140 pac.UPNDNSInfo = &k
141 case ulTypePACClientClaimsInfo:
142 if pac.ClientClaimsInfo != nil || len(p) < 1 {
143 //Must ignore subsequent buffers of this type
144 continue
145 }
146 var k ClientClaimsInfo
147 err := k.Unmarshal(p)
148 if err != nil {
149 return fmt.Errorf("error processing ClientClaimsInfo: %v", err)
150 }
151 pac.ClientClaimsInfo = &k
152 case ulTypePACDeviceInfo:
153 if pac.DeviceInfo != nil {
154 //Must ignore subsequent buffers of this type
155 continue
156 }
157 var k DeviceInfo
158 err := k.Unmarshal(p)
159 if err != nil {
160 return fmt.Errorf("error processing DeviceInfo: %v", err)
161 }
162 pac.DeviceInfo = &k
163 case ulTypePACDeviceClaimsInfo:
164 if pac.DeviceClaimsInfo != nil {
165 //Must ignore subsequent buffers of this type
166 continue
167 }
168 var k DeviceClaimsInfo
169 err := k.Unmarshal(p)
170 if err != nil {
171 return fmt.Errorf("error processing DeviceClaimsInfo: %v", err)
172 }
173 pac.DeviceClaimsInfo = &k
174 }
175 }
176
177 if ok, err := pac.validate(key); !ok {
178 return err
179 }
180
181 return nil
182 }
183
184 func (pac *PACType) validate(key types.EncryptionKey) (bool, error) {
185 if pac.KerbValidationInfo == nil {
186 return false, errors.New("PAC Info Buffers does not contain a KerbValidationInfo")
187 }
188 if pac.ServerChecksum == nil {
189 return false, errors.New("PAC Info Buffers does not contain a ServerChecksum")
190 }
191 if pac.KDCChecksum == nil {
192 return false, errors.New("PAC Info Buffers does not contain a KDCChecksum")
193 }
194 if pac.ClientInfo == nil {
195 return false, errors.New("PAC Info Buffers does not contain a ClientInfo")
196 }
197 etype, err := crypto.GetChksumEtype(int32(pac.ServerChecksum.SignatureType))
198 if err != nil {
199 return false, err
200 }
201 if ok := etype.VerifyChecksum(key.KeyValue,
202 pac.ZeroSigData,
203 pac.ServerChecksum.Signature,
204 keyusage.KERB_NON_KERB_CKSUM_SALT); !ok {
205 return false, errors.New("PAC service checksum verification failed")
206 }
207
208 return true, nil
209 }