KRBCred.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package messages
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/jcmturner/gofork/encoding/asn1"
  6. "github.com/jcmturner/gokrb5/v8/crypto"
  7. "github.com/jcmturner/gokrb5/v8/iana/asnAppTag"
  8. "github.com/jcmturner/gokrb5/v8/iana/keyusage"
  9. "github.com/jcmturner/gokrb5/v8/iana/msgtype"
  10. "github.com/jcmturner/gokrb5/v8/krberror"
  11. "github.com/jcmturner/gokrb5/v8/types"
  12. )
  13. type marshalKRBCred struct {
  14. PVNO int `asn1:"explicit,tag:0"`
  15. MsgType int `asn1:"explicit,tag:1"`
  16. Tickets asn1.RawValue `asn1:"explicit,tag:2"`
  17. EncPart types.EncryptedData `asn1:"explicit,tag:3"`
  18. }
  19. // KRBCred implements RFC 4120 KRB_CRED: https://tools.ietf.org/html/rfc4120#section-5.8.1.
  20. type KRBCred struct {
  21. PVNO int
  22. MsgType int
  23. Tickets []Ticket
  24. EncPart types.EncryptedData
  25. DecryptedEncPart EncKrbCredPart
  26. }
  27. // EncKrbCredPart is the encrypted part of KRB_CRED.
  28. type EncKrbCredPart struct {
  29. TicketInfo []KrbCredInfo `asn1:"explicit,tag:0"`
  30. Nouce int `asn1:"optional,explicit,tag:1"`
  31. Timestamp time.Time `asn1:"generalized,optional,explicit,tag:2"`
  32. Usec int `asn1:"optional,explicit,tag:3"`
  33. SAddress types.HostAddress `asn1:"optional,explicit,tag:4"`
  34. RAddress types.HostAddress `asn1:"optional,explicit,tag:5"`
  35. }
  36. // KrbCredInfo is the KRB_CRED_INFO part of KRB_CRED.
  37. type KrbCredInfo struct {
  38. Key types.EncryptionKey `asn1:"explicit,tag:0"`
  39. PRealm string `asn1:"generalstring,optional,explicit,tag:1"`
  40. PName types.PrincipalName `asn1:"optional,explicit,tag:2"`
  41. Flags asn1.BitString `asn1:"optional,explicit,tag:3"`
  42. AuthTime time.Time `asn1:"generalized,optional,explicit,tag:4"`
  43. StartTime time.Time `asn1:"generalized,optional,explicit,tag:5"`
  44. EndTime time.Time `asn1:"generalized,optional,explicit,tag:6"`
  45. RenewTill time.Time `asn1:"generalized,optional,explicit,tag:7"`
  46. SRealm string `asn1:"optional,explicit,ia5,tag:8"`
  47. SName types.PrincipalName `asn1:"optional,explicit,tag:9"`
  48. CAddr types.HostAddresses `asn1:"optional,explicit,tag:10"`
  49. }
  50. // Unmarshal bytes b into the KRBCred struct.
  51. func (k *KRBCred) Unmarshal(b []byte) error {
  52. var m marshalKRBCred
  53. _, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.KRBCred))
  54. if err != nil {
  55. return processUnmarshalReplyError(b, err)
  56. }
  57. expectedMsgType := msgtype.KRB_CRED
  58. if m.MsgType != expectedMsgType {
  59. return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate a KRB_CRED. Expected: %v; Actual: %v", expectedMsgType, m.MsgType)
  60. }
  61. k.PVNO = m.PVNO
  62. k.MsgType = m.MsgType
  63. k.EncPart = m.EncPart
  64. if len(m.Tickets.Bytes) > 0 {
  65. k.Tickets, err = unmarshalTicketsSequence(m.Tickets)
  66. if err != nil {
  67. return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling tickets within KRB_CRED")
  68. }
  69. }
  70. return nil
  71. }
  72. // DecryptEncPart decrypts the encrypted part of a KRB_CRED.
  73. func (k *KRBCred) DecryptEncPart(key types.EncryptionKey) error {
  74. b, err := crypto.DecryptEncPart(k.EncPart, key, keyusage.KRB_CRED_ENCPART)
  75. if err != nil {
  76. return krberror.Errorf(err, krberror.DecryptingError, "error decrypting KRB_CRED EncPart")
  77. }
  78. var denc EncKrbCredPart
  79. err = denc.Unmarshal(b)
  80. if err != nil {
  81. return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling encrypted part of KRB_CRED")
  82. }
  83. k.DecryptedEncPart = denc
  84. return nil
  85. }
  86. // Unmarshal bytes b into the encrypted part of KRB_CRED.
  87. func (k *EncKrbCredPart) Unmarshal(b []byte) error {
  88. _, err := asn1.UnmarshalWithParams(b, k, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.EncKrbCredPart))
  89. if err != nil {
  90. return krberror.Errorf(err, krberror.EncodingError, "error unmarshaling EncKrbCredPart")
  91. }
  92. return nil
  93. }