Debian Codegraph @

messages/KDCRep.go

raw symbols SCIP
1 package messages
2
3 // Reference: https://www.ietf.org/rfc/rfc4120.txt
4 // Section: 5.4.2
5
6 import (
7 "fmt"
8 "time"
9
10 "github.com/jcmturner/gofork/encoding/asn1"
11 "gopkg.in/jcmturner/gokrb5.v5/config"
12 "gopkg.in/jcmturner/gokrb5.v5/credentials"
13 "gopkg.in/jcmturner/gokrb5.v5/crypto"
14 "gopkg.in/jcmturner/gokrb5.v5/iana/asnAppTag"
15 "gopkg.in/jcmturner/gokrb5.v5/iana/flags"
16 "gopkg.in/jcmturner/gokrb5.v5/iana/keyusage"
17 "gopkg.in/jcmturner/gokrb5.v5/iana/msgtype"
18 "gopkg.in/jcmturner/gokrb5.v5/iana/patype"
19 "gopkg.in/jcmturner/gokrb5.v5/krberror"
20 "gopkg.in/jcmturner/gokrb5.v5/types"
21 )
22
23 type marshalKDCRep struct {
24 PVNO int `asn1:"explicit,tag:0"`
25 MsgType int `asn1:"explicit,tag:1"`
26 PAData types.PADataSequence `asn1:"explicit,optional,tag:2"`
27 CRealm string `asn1:"generalstring,explicit,tag:3"`
28 CName types.PrincipalName `asn1:"explicit,tag:4"`
29 // Ticket needs to be a raw value as it is wrapped in an APPLICATION tag
30 Ticket asn1.RawValue `asn1:"explicit,tag:5"`
31 EncPart types.EncryptedData `asn1:"explicit,tag:6"`
32 }
33
34 // KDCRepFields represents the KRB_KDC_REP fields.
35 type KDCRepFields struct {
36 PVNO int
37 MsgType int
38 PAData []types.PAData
39 CRealm string
40 CName types.PrincipalName
41 Ticket Ticket
42 EncPart types.EncryptedData
43 DecryptedEncPart EncKDCRepPart
44 }
45
46 // ASRep implements RFC 4120 KRB_AS_REP: https://tools.ietf.org/html/rfc4120#section-5.4.2.
47 type ASRep struct {
48 KDCRepFields
49 }
50
51 // TGSRep implements RFC 4120 KRB_TGS_REP: https://tools.ietf.org/html/rfc4120#section-5.4.2.
52 type TGSRep struct {
53 KDCRepFields
54 }
55
56 // EncKDCRepPart is the encrypted part of KRB_KDC_REP.
57 type EncKDCRepPart struct {
58 Key types.EncryptionKey `asn1:"explicit,tag:0"`
59 LastReqs []LastReq `asn1:"explicit,tag:1"`
60 Nonce int `asn1:"explicit,tag:2"`
61 KeyExpiration time.Time `asn1:"generalized,explicit,optional,tag:3"`
62 Flags asn1.BitString `asn1:"explicit,tag:4"`
63 AuthTime time.Time `asn1:"generalized,explicit,tag:5"`
64 StartTime time.Time `asn1:"generalized,explicit,optional,tag:6"`
65 EndTime time.Time `asn1:"generalized,explicit,tag:7"`
66 RenewTill time.Time `asn1:"generalized,explicit,optional,tag:8"`
67 SRealm string `asn1:"generalstring,explicit,tag:9"`
68 SName types.PrincipalName `asn1:"explicit,tag:10"`
69 CAddr []types.HostAddress `asn1:"explicit,optional,tag:11"`
70 EncPAData types.PADataSequence `asn1:"explicit,optional,tag:12"`
71 }
72
73 // LastReq part of KRB_KDC_REP.
74 type LastReq struct {
75 LRType int32 `asn1:"explicit,tag:0"`
76 LRValue time.Time `asn1:"generalized,explicit,tag:1"`
77 }
78
79 // Unmarshal bytes b into the ASRep struct.
80 func (k *ASRep) Unmarshal(b []byte) error {
81 var m marshalKDCRep
82 _, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.ASREP))
83 if err != nil {
84 return processUnmarshalReplyError(b, err)
85 }
86 if m.MsgType != msgtype.KRB_AS_REP {
87 return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate an AS_REP. Expected: %v; Actual: %v", msgtype.KRB_AS_REP, m.MsgType)
88 }
89 //Process the raw ticket within
90 tkt, err := UnmarshalTicket(m.Ticket.Bytes)
91 if err != nil {
92 return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling Ticket within AS_REP")
93 }
94 k.KDCRepFields = KDCRepFields{
95 PVNO: m.PVNO,
96 MsgType: m.MsgType,
97 PAData: m.PAData,
98 CRealm: m.CRealm,
99 CName: m.CName,
100 Ticket: tkt,
101 EncPart: m.EncPart,
102 }
103 return nil
104 }
105
106 // Unmarshal bytes b into the TGSRep struct.
107 func (k *TGSRep) Unmarshal(b []byte) error {
108 var m marshalKDCRep
109 _, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.TGSREP))
110 if err != nil {
111 return processUnmarshalReplyError(b, err)
112 }
113 if m.MsgType != msgtype.KRB_TGS_REP {
114 return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate an TGS_REP. Expected: %v; Actual: %v", msgtype.KRB_TGS_REP, m.MsgType)
115 }
116 //Process the raw ticket within
117 tkt, err := UnmarshalTicket(m.Ticket.Bytes)
118 if err != nil {
119 return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling Ticket within TGS_REP")
120 }
121 k.KDCRepFields = KDCRepFields{
122 PVNO: m.PVNO,
123 MsgType: m.MsgType,
124 PAData: m.PAData,
125 CRealm: m.CRealm,
126 CName: m.CName,
127 Ticket: tkt,
128 EncPart: m.EncPart,
129 }
130 return nil
131 }
132
133 // Unmarshal bytes b into encrypted part of KRB_KDC_REP.
134 func (e *EncKDCRepPart) Unmarshal(b []byte) error {
135 _, err := asn1.UnmarshalWithParams(b, e, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.EncASRepPart))
136 if err != nil {
137 // Try using tag 26
138 _, err = asn1.UnmarshalWithParams(b, e, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.EncTGSRepPart))
139 if err != nil {
140 return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling encrypted part within KDC_REP")
141 }
142 }
143 return nil
144 }
145
146 // DecryptEncPart decrypts the encrypted part of an AS_REP.
147 func (k *ASRep) DecryptEncPart(c *credentials.Credentials) (types.EncryptionKey, error) {
148 var key types.EncryptionKey
149 var err error
150 if c.HasKeytab() {
151 key, err = c.Keytab.GetEncryptionKey(k.CName.NameString, k.CRealm, k.EncPart.KVNO, k.EncPart.EType)
152 if err != nil {
153 return key, krberror.Errorf(err, krberror.DecryptingError, "error decrypting AS_REP encrypted part")
154 }
155 }
156 if c.HasPassword() {
157 key, _, err = crypto.GetKeyFromPassword(c.Password, k.CName, k.CRealm, k.EncPart.EType, k.PAData)
158 if err != nil {
159 return key, krberror.Errorf(err, krberror.DecryptingError, "error decrypting AS_REP encrypted part")
160 }
161 }
162 if !c.HasKeytab() && !c.HasPassword() {
163 return key, krberror.NewErrorf(krberror.DecryptingError, "no secret available in credentials to preform decryption of AS_REP encrypted part")
164 }
165 b, err := crypto.DecryptEncPart(k.EncPart, key, keyusage.AS_REP_ENCPART)
166 if err != nil {
167 return key, krberror.Errorf(err, krberror.DecryptingError, "error decrypting AS_REP encrypted part")
168 }
169 var denc EncKDCRepPart
170 err = denc.Unmarshal(b)
171 if err != nil {
172 return key, krberror.Errorf(err, krberror.EncodingError, "error unmarshaling decrypted encpart of AS_REP")
173 }
174 k.DecryptedEncPart = denc
175 return key, nil
176 }
177
178 // IsValid checks the validity of AS_REP message.
179 func (k *ASRep) IsValid(cfg *config.Config, creds *credentials.Credentials, asReq ASReq) (bool, error) {
180 //Ref RFC 4120 Section 3.1.5
181 if k.CName.NameType != asReq.ReqBody.CName.NameType || k.CName.NameString == nil {
182 return false, krberror.NewErrorf(krberror.KRBMsgError, "CName in response does not match what was requested. Requested: %+v; Reply: %+v", asReq.ReqBody.CName, k.CName)
183 }
184 for i := range k.CName.NameString {
185 if k.CName.NameString[i] != asReq.ReqBody.CName.NameString[i] {
186 return false, krberror.NewErrorf(krberror.KRBMsgError, "CName in response does not match what was requested. Requested: %+v; Reply: %+v", asReq.ReqBody.CName, k.CName)
187 }
188 }
189 if k.CRealm != asReq.ReqBody.Realm {
190 return false, krberror.NewErrorf(krberror.KRBMsgError, "CRealm in response does not match what was requested. Requested: %s; Reply: %s", asReq.ReqBody.Realm, k.CRealm)
191 }
192 key, err := k.DecryptEncPart(creds)
193 if err != nil {
194 return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting EncPart of AS_REP")
195 }
196 if k.DecryptedEncPart.Nonce != asReq.ReqBody.Nonce {
197 return false, krberror.NewErrorf(krberror.KRBMsgError, "possible replay attack, nonce in response does not match that in request")
198 }
199 if k.DecryptedEncPart.SName.NameType != asReq.ReqBody.SName.NameType || k.DecryptedEncPart.SName.NameString == nil {
200 return false, krberror.NewErrorf(krberror.KRBMsgError, "SName in response does not match what was requested. Requested: %v; Reply: %v", asReq.ReqBody.SName, k.DecryptedEncPart.SName)
201 }
202 for i := range k.CName.NameString {
203 if k.DecryptedEncPart.SName.NameString[i] != asReq.ReqBody.SName.NameString[i] {
204 return false, krberror.NewErrorf(krberror.KRBMsgError, "SName in response does not match what was requested. Requested: %+v; Reply: %+v", asReq.ReqBody.SName, k.DecryptedEncPart.SName)
205 }
206 }
207 if k.DecryptedEncPart.SRealm != asReq.ReqBody.Realm {
208 return false, krberror.NewErrorf(krberror.KRBMsgError, "SRealm in response does not match what was requested. Requested: %s; Reply: %s", asReq.ReqBody.Realm, k.DecryptedEncPart.SRealm)
209 }
210 if len(asReq.ReqBody.Addresses) > 0 {
211 if !types.HostAddressesEqual(k.DecryptedEncPart.CAddr, asReq.ReqBody.Addresses) {
212 return false, krberror.NewErrorf(krberror.KRBMsgError, "addresses listed in the AS_REP does not match those listed in the AS_REQ")
213 }
214 }
215 t := time.Now().UTC()
216 if t.Sub(k.DecryptedEncPart.AuthTime) > cfg.LibDefaults.Clockskew || k.DecryptedEncPart.AuthTime.Sub(t) > cfg.LibDefaults.Clockskew {
217 return false, krberror.NewErrorf(krberror.KRBMsgError, "clock skew with KDC too large. Greater than %v seconds", cfg.LibDefaults.Clockskew.Seconds())
218 }
219 // RFC 6806 https://tools.ietf.org/html/rfc6806.html#section-11
220 if asReq.PAData.Contains(patype.PA_REQ_ENC_PA_REP) && types.IsFlagSet(&k.DecryptedEncPart.Flags, flags.EncPARep) {
221 if len(k.DecryptedEncPart.EncPAData) < 2 || !k.DecryptedEncPart.EncPAData.Contains(patype.PA_FX_FAST) {
222 return false, krberror.NewErrorf(krberror.KRBMsgError, "KDC did not respond appropriately to FAST negotiation")
223 }
224 for _, pa := range k.DecryptedEncPart.EncPAData {
225 if pa.PADataType == patype.PA_REQ_ENC_PA_REP {
226 var pafast types.PAReqEncPARep
227 err := pafast.Unmarshal(pa.PADataValue)
228 if err != nil {
229 return false, krberror.Errorf(err, krberror.EncodingError, "KDC FAST negotiation response error, could not unmarshal PA_REQ_ENC_PA_REP")
230 }
231 etype, err := crypto.GetChksumEtype(pafast.ChksumType)
232 if err != nil {
233 return false, krberror.Errorf(err, krberror.ChksumError, "KDC FAST negotiation response error")
234 }
235 ab, _ := asReq.Marshal()
236 if !etype.VerifyChecksum(key.KeyValue, ab, pafast.Chksum, keyusage.KEY_USAGE_AS_REQ) {
237 return false, krberror.Errorf(err, krberror.ChksumError, "KDC FAST negotiation response checksum invalid")
238 }
239 }
240 }
241 }
242 return true, nil
243 }
244
245 // DecryptEncPart decrypts the encrypted part of an TGS_REP.
246 func (k *TGSRep) DecryptEncPart(key types.EncryptionKey) error {
247 b, err := crypto.DecryptEncPart(k.EncPart, key, keyusage.TGS_REP_ENCPART_SESSION_KEY)
248 if err != nil {
249 return krberror.Errorf(err, krberror.DecryptingError, "error decrypting TGS_REP EncPart")
250 }
251 var denc EncKDCRepPart
252 err = denc.Unmarshal(b)
253 if err != nil {
254 return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling encrypted part")
255 }
256 k.DecryptedEncPart = denc
257 return nil
258 }
259
260 // IsValid checks the validity of the TGS_REP message.
261 func (k *TGSRep) IsValid(cfg *config.Config, tgsReq TGSReq) (bool, error) {
262 if k.CName.NameType != tgsReq.ReqBody.CName.NameType || k.CName.NameString == nil {
263 return false, krberror.NewErrorf(krberror.KRBMsgError, "CName type in response does not match what was requested. Requested: %+v; Reply: %+v", tgsReq.ReqBody.CName, k.CName)
264 }
265 for i := range k.CName.NameString {
266 if k.CName.NameString[i] != tgsReq.ReqBody.CName.NameString[i] {
267 return false, krberror.NewErrorf(krberror.KRBMsgError, "CName in response does not match what was requested. Requested: %+v; Reply: %+v", tgsReq.ReqBody.CName, k.CName)
268 }
269 }
270 if k.Ticket.Realm != tgsReq.ReqBody.Realm {
271 return false, krberror.NewErrorf(krberror.KRBMsgError, "realm in response ticket does not match what was requested. Requested: %s; Reply: %s", tgsReq.ReqBody.Realm, k.Ticket.Realm)
272 }
273 if k.DecryptedEncPart.Nonce != tgsReq.ReqBody.Nonce {
274 return false, krberror.NewErrorf(krberror.KRBMsgError, "possible replay attack, nonce in response does not match that in request")
275 }
276 //if k.Ticket.SName.NameType != tgsReq.ReqBody.SName.NameType || k.Ticket.SName.NameString == nil {
277 // return false, krberror.NewErrorf(krberror.KRBMsgError, "SName in response ticket does not match what was requested. Requested: %v; Reply: %v", tgsReq.ReqBody.SName, k.Ticket.SName)
278 //}
279 //for i := range k.Ticket.SName.NameString {
280 // if k.Ticket.SName.NameString[i] != tgsReq.ReqBody.SName.NameString[i] {
281 // return false, krberror.NewErrorf(krberror.KRBMsgError, "SName in response ticket does not match what was requested. Requested: %+v; Reply: %+v", tgsReq.ReqBody.SName, k.Ticket.SName)
282 // }
283 //}
284 //if k.DecryptedEncPart.SName.NameType != tgsReq.ReqBody.SName.NameType || k.DecryptedEncPart.SName.NameString == nil {
285 // return false, krberror.NewErrorf(krberror.KRBMsgError, "SName in response does not match what was requested. Requested: %v; Reply: %v", tgsReq.ReqBody.SName, k.DecryptedEncPart.SName)
286 //}
287 //for i := range k.DecryptedEncPart.SName.NameString {
288 // if k.DecryptedEncPart.SName.NameString[i] != tgsReq.ReqBody.SName.NameString[i] {
289 // return false, krberror.NewErrorf(krberror.KRBMsgError, "SName in response does not match what was requested. Requested: %+v; Reply: %+v", tgsReq.ReqBody.SName, k.DecryptedEncPart.SName)
290 // }
291 //}
292 if k.DecryptedEncPart.SRealm != tgsReq.ReqBody.Realm {
293 return false, krberror.NewErrorf(krberror.KRBMsgError, "SRealm in response does not match what was requested. Requested: %s; Reply: %s", tgsReq.ReqBody.Realm, k.DecryptedEncPart.SRealm)
294 }
295 if len(k.DecryptedEncPart.CAddr) > 0 {
296 if !types.HostAddressesEqual(k.DecryptedEncPart.CAddr, tgsReq.ReqBody.Addresses) {
297 return false, krberror.NewErrorf(krberror.KRBMsgError, "addresses listed in the TGS_REP does not match those listed in the TGS_REQ")
298 }
299 }
300 if time.Since(k.DecryptedEncPart.StartTime) > cfg.LibDefaults.Clockskew || k.DecryptedEncPart.StartTime.Sub(time.Now().UTC()) > cfg.LibDefaults.Clockskew {
301 if time.Since(k.DecryptedEncPart.AuthTime) > cfg.LibDefaults.Clockskew || k.DecryptedEncPart.AuthTime.Sub(time.Now().UTC()) > cfg.LibDefaults.Clockskew {
302 return false, krberror.NewErrorf(krberror.KRBMsgError, "clock skew with KDC too large. Greater than %v seconds.", cfg.LibDefaults.Clockskew.Seconds())
303 }
304 }
305 return true, nil
306 }