APReq.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /*AP-REQ ::= [APPLICATION 14] SEQUENCE {
  18. pvno [0] INTEGER (5),
  19. msg-type [1] INTEGER (14),
  20. ap-options [2] APOptions,
  21. ticket [3] Ticket,
  22. authenticator [4] EncryptedData -- Authenticator
  23. }
  24. APOptions ::= KerberosFlags
  25. -- reserved(0),
  26. -- use-session-key(1),
  27. -- mutual-required(2)*/
  28. type marshalAPReq struct {
  29. PVNO int `asn1:"explicit,tag:0"`
  30. MsgType int `asn1:"explicit,tag:1"`
  31. APOptions asn1.BitString `asn1:"explicit,tag:2"`
  32. // Ticket needs to be a raw value as it is wrapped in an APPLICATION tag
  33. Ticket asn1.RawValue `asn1:"explicit,tag:3"`
  34. EncryptedAuthenticator types.EncryptedData `asn1:"explicit,tag:4"`
  35. }
  36. // APReq implements RFC 4120 KRB_AP_REQ: https://tools.ietf.org/html/rfc4120#section-5.5.1.
  37. type APReq struct {
  38. PVNO int `asn1:"explicit,tag:0"`
  39. MsgType int `asn1:"explicit,tag:1"`
  40. APOptions asn1.BitString `asn1:"explicit,tag:2"`
  41. Ticket Ticket `asn1:"explicit,tag:3"`
  42. EncryptedAuthenticator types.EncryptedData `asn1:"explicit,tag:4"`
  43. Authenticator types.Authenticator `asn1:"optional"`
  44. }
  45. // NewAPReq generates a new KRB_AP_REQ struct.
  46. func NewAPReq(tkt Ticket, sessionKey types.EncryptionKey, auth types.Authenticator) (APReq, error) {
  47. var a APReq
  48. ed, err := encryptAuthenticator(auth, sessionKey, tkt)
  49. if err != nil {
  50. return a, krberror.Errorf(err, krberror.KRBMsgError, "error creating Authenticator for AP_REQ")
  51. }
  52. a = APReq{
  53. PVNO: iana.PVNO,
  54. MsgType: msgtype.KRB_AP_REQ,
  55. APOptions: types.NewKrbFlags(),
  56. Ticket: tkt,
  57. EncryptedAuthenticator: ed,
  58. }
  59. return a, nil
  60. }
  61. // Encrypt Authenticator
  62. func encryptAuthenticator(a types.Authenticator, sessionKey types.EncryptionKey, tkt Ticket) (types.EncryptedData, error) {
  63. var ed types.EncryptedData
  64. m, err := a.Marshal()
  65. if err != nil {
  66. return ed, krberror.Errorf(err, krberror.EncodingError, "marshaling error of EncryptedData form of Authenticator")
  67. }
  68. usage := authenticatorKeyUsage(tkt.SName)
  69. ed, err = crypto.GetEncryptedData(m, sessionKey, uint32(usage), tkt.EncPart.KVNO)
  70. if err != nil {
  71. return ed, krberror.Errorf(err, krberror.EncryptingError, "error encrypting Authenticator")
  72. }
  73. return ed, nil
  74. }
  75. // DecryptAuthenticator decrypts the Authenticator within the AP_REQ.
  76. // sessionKey may simply be the key within the decrypted EncPart of the ticket within the AP_REQ.
  77. func (a *APReq) DecryptAuthenticator(sessionKey types.EncryptionKey) error {
  78. usage := authenticatorKeyUsage(a.Ticket.SName)
  79. ab, e := crypto.DecryptEncPart(a.EncryptedAuthenticator, sessionKey, uint32(usage))
  80. if e != nil {
  81. return fmt.Errorf("error decrypting authenticator: %v", e)
  82. }
  83. err := a.Authenticator.Unmarshal(ab)
  84. if err != nil {
  85. return fmt.Errorf("error unmarshaling authenticator: %v", err)
  86. }
  87. return nil
  88. }
  89. func authenticatorKeyUsage(pn types.PrincipalName) int {
  90. if pn.NameString[0] == "krbtgt" {
  91. return keyusage.TGS_REQ_PA_TGS_REQ_AP_REQ_AUTHENTICATOR
  92. }
  93. return keyusage.AP_REQ_AUTHENTICATOR
  94. }
  95. // Unmarshal bytes b into the APReq struct.
  96. func (a *APReq) Unmarshal(b []byte) error {
  97. var m marshalAPReq
  98. _, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.APREQ))
  99. if err != nil {
  100. return krberror.Errorf(err, krberror.EncodingError, "unmarshal error of AP_REQ")
  101. }
  102. if m.MsgType != msgtype.KRB_AP_REQ {
  103. return NewKRBError(types.PrincipalName{}, "", errorcode.KRB_AP_ERR_MSG_TYPE, errorcode.Lookup(errorcode.KRB_AP_ERR_MSG_TYPE))
  104. }
  105. a.PVNO = m.PVNO
  106. a.MsgType = m.MsgType
  107. a.APOptions = m.APOptions
  108. a.EncryptedAuthenticator = m.EncryptedAuthenticator
  109. a.Ticket, err = unmarshalTicket(m.Ticket.Bytes)
  110. if err != nil {
  111. return krberror.Errorf(err, krberror.EncodingError, "unmarshaling error of Ticket within AP_REQ")
  112. }
  113. return nil
  114. }
  115. // Marshal APReq struct.
  116. func (a *APReq) Marshal() ([]byte, error) {
  117. m := marshalAPReq{
  118. PVNO: a.PVNO,
  119. MsgType: a.MsgType,
  120. APOptions: a.APOptions,
  121. EncryptedAuthenticator: a.EncryptedAuthenticator,
  122. }
  123. var b []byte
  124. b, err := a.Ticket.Marshal()
  125. if err != nil {
  126. return b, err
  127. }
  128. m.Ticket = asn1.RawValue{
  129. Class: asn1.ClassContextSpecific,
  130. IsCompound: true,
  131. Tag: 3,
  132. Bytes: b,
  133. }
  134. mk, err := asn1.Marshal(m)
  135. if err != nil {
  136. return mk, krberror.Errorf(err, krberror.EncodingError, "marshaling error of AP_REQ")
  137. }
  138. mk = asn1tools.AddASNAppTag(mk, asnAppTag.APREQ)
  139. return mk, nil
  140. }
  141. // Verify an AP_REQ using service's keytab, spn and max acceptable clock skew duration.
  142. // The service ticket encrypted part and authenticator will be decrypted as part of this operation.
  143. func (a *APReq) Verify(kt *keytab.Keytab, d time.Duration, cAddr types.HostAddress, snameOverride *types.PrincipalName) (bool, error) {
  144. // Decrypt ticket's encrypted part with service key
  145. //TODO decrypt with service's session key from its TGT is use-to-user. Need to figure out how to get TGT.
  146. //if types.IsFlagSet(&a.APOptions, flags.APOptionUseSessionKey) {
  147. // //If the USE-SESSION-KEY flag is set in the ap-options field, it indicates to
  148. // //the server that user-to-user authentication is in use, and that the ticket
  149. // //is encrypted in the session key from the server's TGT rather than in the server's secret key.
  150. // err := a.Ticket.Decrypt(tgt.DecryptedEncPart.Key)
  151. // if err != nil {
  152. // return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of ticket provided using session key")
  153. // }
  154. //} else {
  155. // // Because it is possible for the server to be registered in multiple
  156. // // realms, with different keys in each, the srealm field in the
  157. // // unencrypted portion of the ticket in the KRB_AP_REQ is used to
  158. // // specify which secret key the server should use to decrypt that
  159. // // ticket.The KRB_AP_ERR_NOKEY error code is returned if the server
  160. // // doesn't have the proper key to decipher the ticket.
  161. // // The ticket is decrypted using the version of the server's key
  162. // // specified by the ticket.
  163. // err := a.Ticket.DecryptEncPart(*kt, &a.Ticket.SName)
  164. // if err != nil {
  165. // return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of service ticket provided")
  166. // }
  167. //}
  168. sname := &a.Ticket.SName
  169. if snameOverride != nil {
  170. sname = snameOverride
  171. }
  172. err := a.Ticket.DecryptEncPart(kt, sname)
  173. if err != nil {
  174. return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of service ticket provided")
  175. }
  176. // Check time validity of ticket
  177. ok, err := a.Ticket.Valid(d)
  178. if err != nil || !ok {
  179. return ok, err
  180. }
  181. // Check client's address is listed in the client addresses in the ticket
  182. if len(a.Ticket.DecryptedEncPart.CAddr) > 0 {
  183. //The addresses in the ticket (if any) are then searched for an address matching the operating-system reported
  184. //address of the client. If no match is found or the server insists on ticket addresses but none are present in
  185. //the ticket, the KRB_AP_ERR_BADADDR error is returned.
  186. if !types.HostAddressesContains(a.Ticket.DecryptedEncPart.CAddr, cAddr) {
  187. 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")
  188. }
  189. }
  190. // Decrypt authenticator with session key from ticket's encrypted part
  191. err = a.DecryptAuthenticator(a.Ticket.DecryptedEncPart.Key)
  192. if err != nil {
  193. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BAD_INTEGRITY, "could not decrypt authenticator")
  194. }
  195. // Check CName in authenticator is the same as that in the ticket
  196. if !a.Authenticator.CName.Equal(a.Ticket.DecryptedEncPart.CName) {
  197. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BADMATCH, "CName in Authenticator does not match that in service ticket")
  198. }
  199. // Check the clock skew between the client and the service server
  200. ct := a.Authenticator.CTime.Add(time.Duration(a.Authenticator.Cusec) * time.Microsecond)
  201. t := time.Now().UTC()
  202. if t.Sub(ct) > d || ct.Sub(t) > d {
  203. 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))
  204. }
  205. return true, nil
  206. }