Authenticator.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Kerberos 5 data types.
  2. package types
  3. import (
  4. "fmt"
  5. "github.com/jcmturner/asn1"
  6. "github.com/jcmturner/gokrb5/asn1tools"
  7. "github.com/jcmturner/gokrb5/iana"
  8. "github.com/jcmturner/gokrb5/iana/asnAppTag"
  9. "time"
  10. )
  11. /*Authenticator ::= [APPLICATION 2] SEQUENCE {
  12. authenticator-vno [0] INTEGER (5),
  13. crealm [1] Realm,
  14. cname [2] PrincipalName,
  15. cksum [3] Checksum OPTIONAL,
  16. cusec [4] Microseconds,
  17. ctime [5] KerberosTime,
  18. subkey [6] EncryptionKey OPTIONAL,
  19. seq-number [7] UInt32 OPTIONAL,
  20. authorization-data [8] AuthorizationData OPTIONAL
  21. }
  22. cksum
  23. This field contains a checksum of the application data that
  24. accompanies the KRB_AP_REQ, computed using a key usage value of 10
  25. in normal application exchanges, or 6 when used in the TGS-REQ
  26. PA-TGS-REQ AP-DATA field.
  27. */
  28. type Authenticator struct {
  29. AVNO int `asn1:"explicit,tag:0"`
  30. CRealm string `asn1:"generalstring,explicit,tag:1"`
  31. CName PrincipalName `asn1:"explicit,tag:2"`
  32. Cksum Checksum `asn1:"explicit,optional,tag:3"`
  33. Cusec int `asn1:"explicit,tag:4"`
  34. CTime time.Time `asn1:"generalized,explicit,tag:5"`
  35. SubKey EncryptionKey `asn1:"explicit,optional,tag:6"`
  36. SeqNumber int `asn1:"explicit,optional,tag:7"`
  37. AuthorizationData AuthorizationData `asn1:"explicit,optional,tag:8"`
  38. }
  39. func NewAuthenticator(realm string, cname PrincipalName) Authenticator {
  40. t := time.Now().UTC()
  41. return Authenticator{
  42. AVNO: iana.PVNO,
  43. CRealm: realm,
  44. CName: cname,
  45. Cksum: Checksum{},
  46. Cusec: int((t.UnixNano() / int64(time.Microsecond)) - (t.Unix() * 1e6)),
  47. CTime: t,
  48. }
  49. }
  50. func (a *Authenticator) Unmarshal(b []byte) error {
  51. _, err := asn1.UnmarshalWithParams(b, a, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.Authenticator))
  52. return err
  53. }
  54. func (a *Authenticator) Marshal() ([]byte, error) {
  55. b, err := asn1.Marshal(*a)
  56. if err != nil {
  57. return nil, err
  58. }
  59. b = asn1tools.AddASNAppTag(b, asnAppTag.Authenticator)
  60. return b, nil
  61. }