APReq.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package messages
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/jcmturner/gofork/encoding/asn1"
  6. "github.com/jcmturner/gokrb5/v8/asn1tools"
  7. "github.com/jcmturner/gokrb5/v8/crypto"
  8. "github.com/jcmturner/gokrb5/v8/iana"
  9. "github.com/jcmturner/gokrb5/v8/iana/asnAppTag"
  10. "github.com/jcmturner/gokrb5/v8/iana/errorcode"
  11. "github.com/jcmturner/gokrb5/v8/iana/keyusage"
  12. "github.com/jcmturner/gokrb5/v8/iana/msgtype"
  13. "github.com/jcmturner/gokrb5/v8/keytab"
  14. "github.com/jcmturner/gokrb5/v8/krberror"
  15. "github.com/jcmturner/gokrb5/v8/types"
  16. )
  17. type marshalAPReq struct {
  18. PVNO int `asn1:"explicit,tag:0"`
  19. MsgType int `asn1:"explicit,tag:1"`
  20. APOptions asn1.BitString `asn1:"explicit,tag:2"`
  21. // Ticket needs to be a raw value as it is wrapped in an APPLICATION tag
  22. Ticket asn1.RawValue `asn1:"explicit,tag:3"`
  23. EncryptedAuthenticator types.EncryptedData `asn1:"explicit,tag:4"`
  24. }
  25. // APReq implements RFC 4120 KRB_AP_REQ: https://tools.ietf.org/html/rfc4120#section-5.5.1.
  26. type APReq struct {
  27. PVNO int `asn1:"explicit,tag:0"`
  28. MsgType int `asn1:"explicit,tag:1"`
  29. APOptions asn1.BitString `asn1:"explicit,tag:2"`
  30. Ticket Ticket `asn1:"explicit,tag:3"`
  31. EncryptedAuthenticator types.EncryptedData `asn1:"explicit,tag:4"`
  32. Authenticator types.Authenticator `asn1:"optional"`
  33. }
  34. // NewAPReq generates a new KRB_AP_REQ struct.
  35. func NewAPReq(tkt Ticket, sessionKey types.EncryptionKey, auth types.Authenticator) (APReq, error) {
  36. var a APReq
  37. ed, err := encryptAuthenticator(auth, sessionKey, tkt)
  38. if err != nil {
  39. return a, krberror.Errorf(err, krberror.KRBMsgError, "error creating Authenticator for AP_REQ")
  40. }
  41. a = APReq{
  42. PVNO: iana.PVNO,
  43. MsgType: msgtype.KRB_AP_REQ,
  44. APOptions: types.NewKrbFlags(),
  45. Ticket: tkt,
  46. EncryptedAuthenticator: ed,
  47. }
  48. return a, nil
  49. }
  50. // Encrypt Authenticator
  51. func encryptAuthenticator(a types.Authenticator, sessionKey types.EncryptionKey, tkt Ticket) (types.EncryptedData, error) {
  52. var ed types.EncryptedData
  53. m, err := a.Marshal()
  54. if err != nil {
  55. return ed, krberror.Errorf(err, krberror.EncodingError, "marshaling error of EncryptedData form of Authenticator")
  56. }
  57. usage := authenticatorKeyUsage(tkt.SName)
  58. ed, err = crypto.GetEncryptedData(m, sessionKey, uint32(usage), tkt.EncPart.KVNO)
  59. if err != nil {
  60. return ed, krberror.Errorf(err, krberror.EncryptingError, "error encrypting Authenticator")
  61. }
  62. return ed, nil
  63. }
  64. // DecryptAuthenticator decrypts the Authenticator within the AP_REQ.
  65. // sessionKey may simply be the key within the decrypted EncPart of the ticket within the AP_REQ.
  66. func (a *APReq) DecryptAuthenticator(sessionKey types.EncryptionKey) error {
  67. usage := authenticatorKeyUsage(a.Ticket.SName)
  68. ab, e := crypto.DecryptEncPart(a.EncryptedAuthenticator, sessionKey, uint32(usage))
  69. if e != nil {
  70. return fmt.Errorf("error decrypting authenticator: %v", e)
  71. }
  72. err := a.Authenticator.Unmarshal(ab)
  73. if err != nil {
  74. return fmt.Errorf("error unmarshaling authenticator: %v", err)
  75. }
  76. return nil
  77. }
  78. func authenticatorKeyUsage(pn types.PrincipalName) int {
  79. if pn.NameString[0] == "krbtgt" {
  80. return keyusage.TGS_REQ_PA_TGS_REQ_AP_REQ_AUTHENTICATOR
  81. }
  82. return keyusage.AP_REQ_AUTHENTICATOR
  83. }
  84. // Unmarshal bytes b into the APReq struct.
  85. func (a *APReq) Unmarshal(b []byte) error {
  86. var m marshalAPReq
  87. _, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.APREQ))
  88. if err != nil {
  89. return krberror.Errorf(err, krberror.EncodingError, "unmarshal error of AP_REQ")
  90. }
  91. if m.MsgType != msgtype.KRB_AP_REQ {
  92. return NewKRBError(types.PrincipalName{}, "", errorcode.KRB_AP_ERR_MSG_TYPE, errorcode.Lookup(errorcode.KRB_AP_ERR_MSG_TYPE))
  93. }
  94. a.PVNO = m.PVNO
  95. a.MsgType = m.MsgType
  96. a.APOptions = m.APOptions
  97. a.EncryptedAuthenticator = m.EncryptedAuthenticator
  98. a.Ticket, err = unmarshalTicket(m.Ticket.Bytes)
  99. if err != nil {
  100. return krberror.Errorf(err, krberror.EncodingError, "unmarshaling error of Ticket within AP_REQ")
  101. }
  102. return nil
  103. }
  104. // Marshal APReq struct.
  105. func (a *APReq) Marshal() ([]byte, error) {
  106. m := marshalAPReq{
  107. PVNO: a.PVNO,
  108. MsgType: a.MsgType,
  109. APOptions: a.APOptions,
  110. EncryptedAuthenticator: a.EncryptedAuthenticator,
  111. }
  112. var b []byte
  113. b, err := a.Ticket.Marshal()
  114. if err != nil {
  115. return b, err
  116. }
  117. m.Ticket = asn1.RawValue{
  118. Class: asn1.ClassContextSpecific,
  119. IsCompound: true,
  120. Tag: 3,
  121. Bytes: b,
  122. }
  123. mk, err := asn1.Marshal(m)
  124. if err != nil {
  125. return mk, krberror.Errorf(err, krberror.EncodingError, "marshaling error of AP_REQ")
  126. }
  127. mk = asn1tools.AddASNAppTag(mk, asnAppTag.APREQ)
  128. return mk, nil
  129. }
  130. // Verify an AP_REQ using service's keytab, spn and max acceptable clock skew duration.
  131. // The service ticket encrypted part and authenticator will be decrypted as part of this operation.
  132. func (a *APReq) Verify(kt *keytab.Keytab, d time.Duration, cAddr types.HostAddress, snameOverride *types.PrincipalName) (bool, error) {
  133. // Decrypt ticket's encrypted part with service key
  134. //TODO decrypt with service's session key from its TGT is use-to-user. Need to figure out how to get TGT.
  135. //if types.IsFlagSet(&a.APOptions, flags.APOptionUseSessionKey) {
  136. // err := a.Ticket.Decrypt(tgt.DecryptedEncPart.Key)
  137. // if err != nil {
  138. // return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of ticket provided using session key")
  139. // }
  140. //} else {
  141. // err := a.Ticket.DecryptEncPart(*kt, &a.Ticket.SName)
  142. // if err != nil {
  143. // return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of service ticket provided")
  144. // }
  145. //}
  146. sname := &a.Ticket.SName
  147. if snameOverride != nil {
  148. sname = snameOverride
  149. }
  150. err := a.Ticket.DecryptEncPart(kt, sname)
  151. if err != nil {
  152. return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of service ticket provided")
  153. }
  154. // Check time validity of ticket
  155. ok, err := a.Ticket.Valid(d)
  156. if err != nil || !ok {
  157. return ok, err
  158. }
  159. // Check client's address is listed in the client addresses in the ticket
  160. if len(a.Ticket.DecryptedEncPart.CAddr) > 0 {
  161. //If client addresses are present check if any of them match the source IP that sent the APReq
  162. //If there is no match return KRB_AP_ERR_BADADDR error.
  163. if !types.HostAddressesContains(a.Ticket.DecryptedEncPart.CAddr, cAddr) {
  164. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BADADDR, "client address not within the list contained in the service ticket")
  165. }
  166. }
  167. // Decrypt authenticator with session key from ticket's encrypted part
  168. err = a.DecryptAuthenticator(a.Ticket.DecryptedEncPart.Key)
  169. if err != nil {
  170. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BAD_INTEGRITY, "could not decrypt authenticator")
  171. }
  172. // Check CName in authenticator is the same as that in the ticket
  173. if !a.Authenticator.CName.Equal(a.Ticket.DecryptedEncPart.CName) {
  174. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BADMATCH, "CName in Authenticator does not match that in service ticket")
  175. }
  176. // Check the clock skew between the client and the service server
  177. ct := a.Authenticator.CTime.Add(time.Duration(a.Authenticator.Cusec) * time.Microsecond)
  178. t := time.Now().UTC()
  179. if t.Sub(ct) > d || ct.Sub(t) > d {
  180. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_SKEW, fmt.Sprintf("clock skew with client too large. greater than %v seconds", d))
  181. }
  182. return true, nil
  183. }