KRBSafe.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package messages
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/jcmturner/gofork/encoding/asn1"
  6. "gopkg.in/jcmturner/gokrb5.v7/iana/asnAppTag"
  7. "gopkg.in/jcmturner/gokrb5.v7/iana/msgtype"
  8. "gopkg.in/jcmturner/gokrb5.v7/krberror"
  9. "gopkg.in/jcmturner/gokrb5.v7/types"
  10. )
  11. /*
  12. KRB-SAFE ::= [APPLICATION 20] SEQUENCE {
  13. pvno [0] INTEGER (5),
  14. msg-type [1] INTEGER (20),
  15. safe-body [2] KRB-SAFE-BODY,
  16. cksum [3] Checksum
  17. }
  18. KRB-SAFE-BODY ::= SEQUENCE {
  19. user-data [0] OCTET STRING,
  20. timestamp [1] KerberosTime OPTIONAL,
  21. usec [2] Microseconds OPTIONAL,
  22. seq-number [3] UInt32 OPTIONAL,
  23. s-address [4] HostAddress,
  24. r-address [5] HostAddress OPTIONAL
  25. }
  26. */
  27. // KRBSafe implements RFC 4120 KRB_SAFE: https://tools.ietf.org/html/rfc4120#section-5.6.1.
  28. type KRBSafe struct {
  29. PVNO int `asn1:"explicit,tag:0"`
  30. MsgType int `asn1:"explicit,tag:1"`
  31. SafeBody KRBSafeBody `asn1:"explicit,tag:2"`
  32. Cksum types.Checksum `asn1:"explicit,tag:3"`
  33. }
  34. // KRBSafeBody implements the KRB_SAFE_BODY of KRB_SAFE.
  35. type KRBSafeBody struct {
  36. UserData []byte `asn1:"explicit,tag:0"`
  37. Timestamp time.Time `asn1:"generalized,optional,explicit,tag:1"`
  38. Usec int `asn1:"optional,explicit,tag:2"`
  39. SequenceNumber int64 `asn1:"optional,explicit,tag:3"`
  40. SAddress types.HostAddress `asn1:"explicit,tag:4"`
  41. RAddress types.HostAddress `asn1:"optional,explicit,tag:5"`
  42. }
  43. // Unmarshal bytes b into the KRBSafe struct.
  44. func (s *KRBSafe) Unmarshal(b []byte) error {
  45. _, err := asn1.UnmarshalWithParams(b, s, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.KRBSafe))
  46. if err != nil {
  47. return processUnmarshalReplyError(b, err)
  48. }
  49. expectedMsgType := msgtype.KRB_SAFE
  50. if s.MsgType != expectedMsgType {
  51. return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate a KRB_SAFE. Expected: %v; Actual: %v", expectedMsgType, s.MsgType)
  52. }
  53. return nil
  54. }