krb5Token.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package GSSAPI
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/jcmturner/asn1"
  7. "github.com/jcmturner/gokrb5/asn1tools"
  8. "github.com/jcmturner/gokrb5/credentials"
  9. "github.com/jcmturner/gokrb5/crypto"
  10. "github.com/jcmturner/gokrb5/iana/chksumtype"
  11. "github.com/jcmturner/gokrb5/messages"
  12. "github.com/jcmturner/gokrb5/types"
  13. )
  14. const (
  15. TOK_ID_KRB_AP_REQ = "0100"
  16. TOK_ID_KRB_AP_REP = "0200"
  17. TOK_ID_KRB_ERROR = "0300"
  18. GSS_C_DELEG_FLAG = 1
  19. GSS_C_MUTUAL_FLAG = 2
  20. GSS_C_REPLAY_FLAG = 4
  21. GSS_C_SEQUENCE_FLAG = 8
  22. GSS_C_CONF_FLAG = 16
  23. GSS_C_INTEG_FLAG = 32
  24. )
  25. type MechToken struct {
  26. OID asn1.ObjectIdentifier
  27. TokID []byte
  28. APReq messages.APReq
  29. APRep messages.APRep
  30. KRBError messages.KRBError
  31. }
  32. func (m *MechToken) Unmarshal(b []byte) error {
  33. var oid asn1.ObjectIdentifier
  34. r, err := asn1.UnmarshalWithParams(b, &oid, fmt.Sprintf("application,explicit,tag:%v", 0))
  35. if err != nil {
  36. return fmt.Errorf("Error unmarshalling MechToken OID: %v", err)
  37. }
  38. m.OID = oid
  39. m.TokID = r[0:2]
  40. switch hex.EncodeToString(m.TokID) {
  41. case TOK_ID_KRB_AP_REQ:
  42. var a messages.APReq
  43. err = a.Unmarshal(r[2:])
  44. if err != nil {
  45. return fmt.Errorf("Error unmarshalling MechToken AP_REQ: %v", err)
  46. }
  47. m.APReq = a
  48. case TOK_ID_KRB_AP_REP:
  49. var a messages.APRep
  50. err = a.Unmarshal(r[2:])
  51. if err != nil {
  52. return fmt.Errorf("Error unmarshalling MechToken AP_REP: %v", err)
  53. }
  54. m.APRep = a
  55. case TOK_ID_KRB_ERROR:
  56. var a messages.KRBError
  57. err = a.Unmarshal(r[2:])
  58. if err != nil {
  59. return fmt.Errorf("Error unmarshalling MechToken KRBError: %v", err)
  60. }
  61. m.KRBError = a
  62. }
  63. return nil
  64. }
  65. func (m *MechToken) IsAPReq() bool {
  66. if hex.EncodeToString(m.TokID) == TOK_ID_KRB_AP_REQ {
  67. return true
  68. }
  69. return false
  70. }
  71. func (m *MechToken) IsAPRep() bool {
  72. if hex.EncodeToString(m.TokID) == TOK_ID_KRB_AP_REP {
  73. return true
  74. }
  75. return false
  76. }
  77. func (m *MechToken) IsKRBError() bool {
  78. if hex.EncodeToString(m.TokID) == TOK_ID_KRB_ERROR {
  79. return true
  80. }
  81. return false
  82. }
  83. // Create new kerberos AP_REQ MechToken
  84. func NewKRB5APREQMechToken(creds credentials.Credentials, tkt messages.Ticket, sessionKey types.EncryptionKey) ([]byte, error) {
  85. // Create the header
  86. b, _ := asn1.Marshal(MechTypeOID_Krb5)
  87. tb, _ := hex.DecodeString(TOK_ID_KRB_AP_REQ)
  88. b = append(b, tb...)
  89. // Add the token
  90. APReq, err := messages.NewAPReq(
  91. tkt,
  92. sessionKey,
  93. newAuthenticator(creds, sessionKey.KeyType),
  94. )
  95. tb, err = APReq.Marshal()
  96. if err != nil {
  97. return []byte{}, fmt.Errorf("Could not marshal AP_REQ: %v", err)
  98. }
  99. b = append(b, tb...)
  100. return asn1tools.AddASNAppTag(b, 0), nil
  101. }
  102. // Create new kerberos authenticator for kerberos MechToken
  103. func newAuthenticator(creds credentials.Credentials, keyType int) types.Authenticator {
  104. //RFC 4121 Section 4.1.1
  105. auth := types.NewAuthenticator(creds.Realm, creds.CName)
  106. etype, _ := crypto.GetEtype(keyType)
  107. auth.GenerateSeqNumberAndSubKey(keyType, etype.GetKeyByteSize())
  108. auth.Cksum = types.Checksum{
  109. CksumType: chksumtype.GSSAPI,
  110. Checksum: newAuthenticatorChksum([]int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}),
  111. }
  112. return auth
  113. }
  114. // Create new authenticator checksum for kerberos MechToken
  115. func newAuthenticatorChksum(flags []int) []byte {
  116. a := make([]byte, 24)
  117. binary.LittleEndian.PutUint32(a[:4], 16)
  118. for _, i := range flags {
  119. if i == GSS_C_DELEG_FLAG {
  120. x := make([]byte, 28-len(a))
  121. a = append(a, x...)
  122. }
  123. f := binary.LittleEndian.Uint32(a[20:24])
  124. f |= uint32(i)
  125. binary.LittleEndian.PutUint32(a[20:24], f)
  126. }
  127. return a
  128. }
  129. /*
  130. The authenticator checksum field SHALL have the following format:
  131. Octet Name Description
  132. -----------------------------------------------------------------
  133. 0..3 Lgth Number of octets in Bnd field; Represented
  134. in little-endian order; Currently contains
  135. hex value 10 00 00 00 (16).
  136. 4..19 Bnd Channel binding information, as described in
  137. section 4.1.1.2.
  138. 20..23 Flags Four-octet context-establishment flags in
  139. little-endian order as described in section
  140. 4.1.1.1.
  141. 24..25 DlgOpt The delegation option identifier (=1) in
  142. little-endian order [optional]. This field
  143. and the next two fields are present if and
  144. only if GSS_C_DELEG_FLAG is set as described
  145. in section 4.1.1.1.
  146. 26..27 Dlgth The length of the Deleg field in little-endian order [optional].
  147. 28..(n-1) Deleg A KRB_CRED message (n = Dlgth + 28) [optional].
  148. n..last Exts Extensions [optional].
  149. */