krb5Token.go 4.5 KB

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