ASExchange.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package client
  2. import (
  3. "github.com/jcmturner/gokrb5/v8/crypto"
  4. "github.com/jcmturner/gokrb5/v8/crypto/etype"
  5. "github.com/jcmturner/gokrb5/v8/iana/errorcode"
  6. "github.com/jcmturner/gokrb5/v8/iana/keyusage"
  7. "github.com/jcmturner/gokrb5/v8/iana/patype"
  8. "github.com/jcmturner/gokrb5/v8/krberror"
  9. "github.com/jcmturner/gokrb5/v8/messages"
  10. "github.com/jcmturner/gokrb5/v8/types"
  11. )
  12. // ASExchange performs an AS exchange for the client to retrieve a TGT.
  13. func (cl *Client) ASExchange(realm string, ASReq messages.ASReq, referral int) (messages.ASRep, error) {
  14. if ok, err := cl.IsConfigured(); !ok {
  15. return messages.ASRep{}, krberror.Errorf(err, krberror.ConfigError, "AS Exchange cannot be performed")
  16. }
  17. // Set PAData if required
  18. err := setPAData(cl, nil, &ASReq)
  19. if err != nil {
  20. return messages.ASRep{}, krberror.Errorf(err, krberror.KRBMsgError, "AS Exchange Error: issue with setting PAData on AS_REQ")
  21. }
  22. b, err := ASReq.Marshal()
  23. if err != nil {
  24. return messages.ASRep{}, krberror.Errorf(err, krberror.EncodingError, "AS Exchange Error: failed marshaling AS_REQ")
  25. }
  26. var ASRep messages.ASRep
  27. rb, err := cl.sendToKDC(b, realm)
  28. if err != nil {
  29. if e, ok := err.(messages.KRBError); ok {
  30. switch e.ErrorCode {
  31. case errorcode.KDC_ERR_PREAUTH_REQUIRED, errorcode.KDC_ERR_PREAUTH_FAILED:
  32. // From now on assume this client will need to do this pre-auth and set the PAData
  33. cl.settings.assumePreAuthentication = true
  34. err = setPAData(cl, &e, &ASReq)
  35. if err != nil {
  36. return messages.ASRep{}, krberror.Errorf(err, krberror.KRBMsgError, "AS Exchange Error: failed setting AS_REQ PAData for pre-authentication required")
  37. }
  38. b, err := ASReq.Marshal()
  39. if err != nil {
  40. return messages.ASRep{}, krberror.Errorf(err, krberror.EncodingError, "AS Exchange Error: failed marshaling AS_REQ with PAData")
  41. }
  42. rb, err = cl.sendToKDC(b, realm)
  43. if err != nil {
  44. if _, ok := err.(messages.KRBError); ok {
  45. return messages.ASRep{}, krberror.Errorf(err, krberror.KDCError, "AS Exchange Error: kerberos error response from KDC")
  46. }
  47. return messages.ASRep{}, krberror.Errorf(err, krberror.NetworkingError, "AS Exchange Error: failed sending AS_REQ to KDC")
  48. }
  49. case errorcode.KDC_ERR_WRONG_REALM:
  50. // Client referral https://tools.ietf.org/html/rfc6806.html#section-7
  51. if referral > 5 {
  52. return messages.ASRep{}, krberror.Errorf(err, krberror.KRBMsgError, "maximum number of client referrals exceeded")
  53. }
  54. referral++
  55. return cl.ASExchange(e.CRealm, ASReq, referral)
  56. default:
  57. return messages.ASRep{}, krberror.Errorf(err, krberror.KDCError, "AS Exchange Error: kerberos error response from KDC")
  58. }
  59. } else {
  60. return messages.ASRep{}, krberror.Errorf(err, krberror.NetworkingError, "AS Exchange Error: failed sending AS_REQ to KDC")
  61. }
  62. }
  63. err = ASRep.Unmarshal(rb)
  64. if err != nil {
  65. return messages.ASRep{}, krberror.Errorf(err, krberror.EncodingError, "AS Exchange Error: failed to process the AS_REP")
  66. }
  67. if ok, err := ASRep.Verify(cl.Config, cl.Credentials, ASReq); !ok {
  68. return messages.ASRep{}, krberror.Errorf(err, krberror.KRBMsgError, "AS Exchange Error: AS_REP is not valid or client password/keytab incorrect")
  69. }
  70. return ASRep, nil
  71. }
  72. // setPAData adds pre-authentication data to the AS_REQ.
  73. func setPAData(cl *Client, krberr *messages.KRBError, ASReq *messages.ASReq) error {
  74. if !cl.settings.DisablePAFXFAST() {
  75. pa := types.PAData{PADataType: patype.PA_REQ_ENC_PA_REP}
  76. ASReq.PAData = append(ASReq.PAData, pa)
  77. }
  78. if cl.settings.AssumePreAuthentication() {
  79. // Identify the etype to use to encrypt the PA Data
  80. var et etype.EType
  81. var err error
  82. var key types.EncryptionKey
  83. var kvno int
  84. if krberr == nil {
  85. // This is not in response to an error from the KDC. It is preemptive or renewal
  86. // There is no KRB Error that tells us the etype to use
  87. etn := cl.settings.preAuthEType // Use the etype that may have previously been negotiated
  88. if etn == 0 {
  89. etn = int32(cl.Config.LibDefaults.PreferredPreauthTypes[0]) // Resort to config
  90. }
  91. et, err = crypto.GetEtype(etn)
  92. if err != nil {
  93. return krberror.Errorf(err, krberror.EncryptingError, "error getting etype for pre-auth encryption")
  94. }
  95. key, kvno, err = cl.Key(et, 0, nil)
  96. if err != nil {
  97. return krberror.Errorf(err, krberror.EncryptingError, "error getting key from credentials")
  98. }
  99. } else {
  100. // Get the etype to use from the PA data in the KRBError e-data
  101. et, err = preAuthEType(krberr)
  102. if err != nil {
  103. return krberror.Errorf(err, krberror.EncryptingError, "error getting etype for pre-auth encryption")
  104. }
  105. cl.settings.preAuthEType = et.GetETypeID() // Set the etype that has been defined for potential future use
  106. key, kvno, err = cl.Key(et, 0, krberr)
  107. if err != nil {
  108. return krberror.Errorf(err, krberror.EncryptingError, "error getting key from credentials")
  109. }
  110. }
  111. // Generate the PA data
  112. paTSb, err := types.GetPAEncTSEncAsnMarshalled()
  113. if err != nil {
  114. return krberror.Errorf(err, krberror.KRBMsgError, "error creating PAEncTSEnc for Pre-Authentication")
  115. }
  116. paEncTS, err := crypto.GetEncryptedData(paTSb, key, keyusage.AS_REQ_PA_ENC_TIMESTAMP, kvno)
  117. if err != nil {
  118. return krberror.Errorf(err, krberror.EncryptingError, "error encrypting pre-authentication timestamp")
  119. }
  120. pb, err := paEncTS.Marshal()
  121. if err != nil {
  122. return krberror.Errorf(err, krberror.EncodingError, "error marshaling the PAEncTSEnc encrypted data")
  123. }
  124. pa := types.PAData{
  125. PADataType: patype.PA_ENC_TIMESTAMP,
  126. PADataValue: pb,
  127. }
  128. // Look for and delete any exiting patype.PA_ENC_TIMESTAMP
  129. for i, pa := range ASReq.PAData {
  130. if pa.PADataType == patype.PA_ENC_TIMESTAMP {
  131. ASReq.PAData[i] = ASReq.PAData[len(ASReq.PAData)-1]
  132. ASReq.PAData = ASReq.PAData[:len(ASReq.PAData)-1]
  133. }
  134. }
  135. ASReq.PAData = append(ASReq.PAData, pa)
  136. }
  137. return nil
  138. }
  139. // preAuthEType establishes what encryption type to use for pre-authentication from the KRBError returned from the KDC.
  140. func preAuthEType(krberr *messages.KRBError) (etype etype.EType, err error) {
  141. //RFC 4120 5.2.7.5 covers the preference order of ETYPE-INFO2 and ETYPE-INFO.
  142. var etypeID int32
  143. var pas types.PADataSequence
  144. e := pas.Unmarshal(krberr.EData)
  145. if e != nil {
  146. err = krberror.Errorf(e, krberror.EncodingError, "error unmashalling KRBError data")
  147. return
  148. }
  149. Loop:
  150. for _, pa := range pas {
  151. switch pa.PADataType {
  152. case patype.PA_ETYPE_INFO2:
  153. info, e := pa.GetETypeInfo2()
  154. if e != nil {
  155. err = krberror.Errorf(e, krberror.EncodingError, "error unmashalling ETYPE-INFO2 data")
  156. return
  157. }
  158. etypeID = info[0].EType
  159. break Loop
  160. case patype.PA_ETYPE_INFO:
  161. info, e := pa.GetETypeInfo()
  162. if e != nil {
  163. err = krberror.Errorf(e, krberror.EncodingError, "error unmashalling ETYPE-INFO data")
  164. return
  165. }
  166. etypeID = info[0].EType
  167. }
  168. }
  169. etype, e = crypto.GetEtype(etypeID)
  170. if e != nil {
  171. err = krberror.Errorf(e, krberror.EncryptingError, "error creating etype")
  172. return
  173. }
  174. return etype, nil
  175. }