Authenticator.go 2.2 KB

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