Authenticator.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package types
  2. import (
  3. "fmt"
  4. "github.com/jcmturner/asn1"
  5. "github.com/jcmturner/gokrb5/iana"
  6. "github.com/jcmturner/gokrb5/iana/asnAppTag"
  7. "github.com/jcmturner/gokrb5/iana/nametype"
  8. "time"
  9. "github.com/jcmturner/gokrb5/asn1tools"
  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, username string) Authenticator {
  40. t := time.Now()
  41. return Authenticator{
  42. AVNO: iana.PVNO,
  43. CRealm: realm,
  44. CName: PrincipalName{
  45. NameType: nametype.KRB_NT_PRINCIPAL,
  46. NameString: []string{username},
  47. },
  48. Cksum: Checksum{},
  49. Cusec: int((t.UnixNano() / int64(time.Microsecond)) - (t.Unix() * 1e6)),
  50. CTime: t,
  51. }
  52. }
  53. func (a *Authenticator) Unmarshal(b []byte) error {
  54. _, err := asn1.UnmarshalWithParams(b, a, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.Authenticator))
  55. return err
  56. }
  57. func (a *Authenticator) Marshal() ([]byte, error) {
  58. b, err := asn1.Marshal(*a)
  59. if err != nil {
  60. return nil, err
  61. }
  62. b = asn1tools.AddASNAppTag(b, asnAppTag.Authenticator)
  63. return b, nil
  64. }