blob: 52cd284493605b5e91997dbff238d47477e81c4b [file] [log] [blame]
David K. Bainbridgebd6b2882021-08-26 13:31:02 +00001package messages
2
3import (
4 "fmt"
5 "time"
6
7 "github.com/jcmturner/gofork/encoding/asn1"
8 "github.com/jcmturner/gokrb5/v8/iana/asnAppTag"
9 "github.com/jcmturner/gokrb5/v8/iana/msgtype"
10 "github.com/jcmturner/gokrb5/v8/krberror"
11 "github.com/jcmturner/gokrb5/v8/types"
12)
13
14// KRBSafe implements RFC 4120 KRB_SAFE: https://tools.ietf.org/html/rfc4120#section-5.6.1.
15type KRBSafe struct {
16 PVNO int `asn1:"explicit,tag:0"`
17 MsgType int `asn1:"explicit,tag:1"`
18 SafeBody KRBSafeBody `asn1:"explicit,tag:2"`
19 Cksum types.Checksum `asn1:"explicit,tag:3"`
20}
21
22// KRBSafeBody implements the KRB_SAFE_BODY of KRB_SAFE.
23type KRBSafeBody struct {
24 UserData []byte `asn1:"explicit,tag:0"`
25 Timestamp time.Time `asn1:"generalized,optional,explicit,tag:1"`
26 Usec int `asn1:"optional,explicit,tag:2"`
27 SequenceNumber int64 `asn1:"optional,explicit,tag:3"`
28 SAddress types.HostAddress `asn1:"explicit,tag:4"`
29 RAddress types.HostAddress `asn1:"optional,explicit,tag:5"`
30}
31
32// Unmarshal bytes b into the KRBSafe struct.
33func (s *KRBSafe) Unmarshal(b []byte) error {
34 _, err := asn1.UnmarshalWithParams(b, s, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.KRBSafe))
35 if err != nil {
36 return processUnmarshalReplyError(b, err)
37 }
38 expectedMsgType := msgtype.KRB_SAFE
39 if s.MsgType != expectedMsgType {
40 return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate a KRB_SAFE. Expected: %v; Actual: %v", expectedMsgType, s.MsgType)
41 }
42 return nil
43}