KRBPriv.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package messages
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/jcmturner/gofork/encoding/asn1"
  6. "github.com/jcmturner/gokrb5/v8/asn1tools"
  7. "github.com/jcmturner/gokrb5/v8/crypto"
  8. "github.com/jcmturner/gokrb5/v8/iana"
  9. "github.com/jcmturner/gokrb5/v8/iana/asnAppTag"
  10. "github.com/jcmturner/gokrb5/v8/iana/keyusage"
  11. "github.com/jcmturner/gokrb5/v8/iana/msgtype"
  12. "github.com/jcmturner/gokrb5/v8/krberror"
  13. "github.com/jcmturner/gokrb5/v8/types"
  14. )
  15. // KRBPriv implements RFC 4120 type: https://tools.ietf.org/html/rfc4120#section-5.7.1.
  16. type KRBPriv struct {
  17. PVNO int `asn1:"explicit,tag:0"`
  18. MsgType int `asn1:"explicit,tag:1"`
  19. EncPart types.EncryptedData `asn1:"explicit,tag:3"`
  20. DecryptedEncPart EncKrbPrivPart `asn1:"optional,omitempty"` // Not part of ASN1 bytes so marked as optional so unmarshalling works
  21. }
  22. // EncKrbPrivPart is the encrypted part of KRB_PRIV.
  23. type EncKrbPrivPart 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. // NewKRBPriv returns a new KRBPriv type.
  32. func NewKRBPriv(part EncKrbPrivPart) KRBPriv {
  33. return KRBPriv{
  34. PVNO: iana.PVNO,
  35. MsgType: msgtype.KRB_PRIV,
  36. DecryptedEncPart: part,
  37. }
  38. }
  39. // Unmarshal bytes b into the KRBPriv struct.
  40. func (k *KRBPriv) Unmarshal(b []byte) error {
  41. _, err := asn1.UnmarshalWithParams(b, k, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.KRBPriv))
  42. if err != nil {
  43. return processUnmarshalReplyError(b, err)
  44. }
  45. expectedMsgType := msgtype.KRB_PRIV
  46. if k.MsgType != expectedMsgType {
  47. return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate a KRB_PRIV. Expected: %v; Actual: %v", expectedMsgType, k.MsgType)
  48. }
  49. return nil
  50. }
  51. // Unmarshal bytes b into the EncKrbPrivPart struct.
  52. func (k *EncKrbPrivPart) Unmarshal(b []byte) error {
  53. _, err := asn1.UnmarshalWithParams(b, k, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.EncKrbPrivPart))
  54. if err != nil {
  55. return krberror.Errorf(err, krberror.EncodingError, "KRB_PRIV unmarshal error")
  56. }
  57. return nil
  58. }
  59. // Marshal the KRBPriv.
  60. func (k *KRBPriv) Marshal() ([]byte, error) {
  61. tk := KRBPriv{
  62. PVNO: k.PVNO,
  63. MsgType: k.MsgType,
  64. EncPart: k.EncPart,
  65. }
  66. b, err := asn1.Marshal(tk)
  67. if err != nil {
  68. return []byte{}, err
  69. }
  70. b = asn1tools.AddASNAppTag(b, asnAppTag.KRBPriv)
  71. return b, nil
  72. }
  73. // EncryptEncPart encrypts the DecryptedEncPart within the KRBPriv.
  74. // Use to prepare for marshaling.
  75. func (k *KRBPriv) EncryptEncPart(key types.EncryptionKey) error {
  76. b, err := asn1.Marshal(k.DecryptedEncPart)
  77. if err != nil {
  78. return err
  79. }
  80. b = asn1tools.AddASNAppTag(b, asnAppTag.EncKrbPrivPart)
  81. k.EncPart, err = crypto.GetEncryptedData(b, key, keyusage.KRB_PRIV_ENCPART, 1)
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. // DecryptEncPart decrypts the encrypted part of the KRBPriv message.
  88. func (k *KRBPriv) DecryptEncPart(key types.EncryptionKey) error {
  89. b, err := crypto.DecryptEncPart(k.EncPart, key, keyusage.KRB_PRIV_ENCPART)
  90. if err != nil {
  91. return fmt.Errorf("error decrypting KRBPriv EncPart: %v", err)
  92. }
  93. err = k.DecryptedEncPart.Unmarshal(b)
  94. if err != nil {
  95. return fmt.Errorf("error unmarshaling encrypted part: %v", err)
  96. }
  97. return nil
  98. }