APReq.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package messages
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/jcmturner/gofork/encoding/asn1"
  6. "gopkg.in/jcmturner/gokrb5.v7/asn1tools"
  7. "gopkg.in/jcmturner/gokrb5.v7/crypto"
  8. "gopkg.in/jcmturner/gokrb5.v7/iana"
  9. "gopkg.in/jcmturner/gokrb5.v7/iana/asnAppTag"
  10. "gopkg.in/jcmturner/gokrb5.v7/iana/errorcode"
  11. "gopkg.in/jcmturner/gokrb5.v7/iana/keyusage"
  12. "gopkg.in/jcmturner/gokrb5.v7/iana/msgtype"
  13. "gopkg.in/jcmturner/gokrb5.v7/keytab"
  14. "gopkg.in/jcmturner/gokrb5.v7/krberror"
  15. "gopkg.in/jcmturner/gokrb5.v7/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. func (a *APReq) DecryptAuthenticator(sessionKey types.EncryptionKey) error {
  66. usage := authenticatorKeyUsage(a.Ticket.SName)
  67. ab, e := crypto.DecryptEncPart(a.EncryptedAuthenticator, sessionKey, uint32(usage))
  68. if e != nil {
  69. return fmt.Errorf("error decrypting authenticator: %v", e)
  70. }
  71. err := a.Authenticator.Unmarshal(ab)
  72. if err != nil {
  73. return fmt.Errorf("error unmarshaling authenticator: %v", err)
  74. }
  75. return nil
  76. }
  77. func authenticatorKeyUsage(pn types.PrincipalName) int {
  78. if pn.NameString[0] == "krbtgt" {
  79. return keyusage.TGS_REQ_PA_TGS_REQ_AP_REQ_AUTHENTICATOR
  80. }
  81. return keyusage.AP_REQ_AUTHENTICATOR
  82. }
  83. // Unmarshal bytes b into the APReq struct.
  84. func (a *APReq) Unmarshal(b []byte) error {
  85. var m marshalAPReq
  86. _, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.APREQ))
  87. if err != nil {
  88. return krberror.Errorf(err, krberror.EncodingError, "unmarshal error of AP_REQ")
  89. }
  90. if m.MsgType != msgtype.KRB_AP_REQ {
  91. return NewKRBError(types.PrincipalName{}, "", errorcode.KRB_AP_ERR_MSG_TYPE, errorcode.Lookup(errorcode.KRB_AP_ERR_MSG_TYPE))
  92. }
  93. a.PVNO = m.PVNO
  94. a.MsgType = m.MsgType
  95. a.APOptions = m.APOptions
  96. a.EncryptedAuthenticator = m.EncryptedAuthenticator
  97. a.Ticket, err = unmarshalTicket(m.Ticket.Bytes)
  98. if err != nil {
  99. return krberror.Errorf(err, krberror.EncodingError, "unmarshaling error of Ticket within AP_REQ")
  100. }
  101. return nil
  102. }
  103. // Marshal APReq struct.
  104. func (a *APReq) Marshal() ([]byte, error) {
  105. m := marshalAPReq{
  106. PVNO: a.PVNO,
  107. MsgType: a.MsgType,
  108. APOptions: a.APOptions,
  109. EncryptedAuthenticator: a.EncryptedAuthenticator,
  110. }
  111. var b []byte
  112. b, err := a.Ticket.Marshal()
  113. if err != nil {
  114. return b, err
  115. }
  116. m.Ticket = asn1.RawValue{
  117. Class: asn1.ClassContextSpecific,
  118. IsCompound: true,
  119. Tag: 3,
  120. Bytes: b,
  121. }
  122. mk, err := asn1.Marshal(m)
  123. if err != nil {
  124. return mk, krberror.Errorf(err, krberror.EncodingError, "marshaling error of AP_REQ")
  125. }
  126. mk = asn1tools.AddASNAppTag(mk, asnAppTag.APREQ)
  127. return mk, nil
  128. }
  129. // Verify an AP_REQ using service's keytab, spn and max acceptable clock skew duration.
  130. // The service ticket encrypted part and authenticator will be decrypted as part of this operation.
  131. func (a *APReq) Verify(kt *keytab.Keytab, d time.Duration, cAddr types.HostAddress) (bool, error) {
  132. // Decrypt ticket's encrypted part with service key
  133. //TODO decrypt with service's session key from its TGT is use-to-user. Need to figure out how to get TGT.
  134. //if types.IsFlagSet(&a.APOptions, flags.APOptionUseSessionKey) {
  135. // err := a.Ticket.Decrypt(tgt.DecryptedEncPart.Key)
  136. // if err != nil {
  137. // return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of ticket provided using session key")
  138. // }
  139. //} else {
  140. // err := a.Ticket.DecryptEncPart(*kt, &a.Ticket.SName)
  141. // if err != nil {
  142. // return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of service ticket provided")
  143. // }
  144. //}
  145. err := a.Ticket.DecryptEncPart(kt, &a.Ticket.SName)
  146. if err != nil {
  147. return false, krberror.Errorf(err, krberror.DecryptingError, "error decrypting encpart of service ticket provided")
  148. }
  149. // Check time validity of ticket
  150. ok, err := a.Ticket.Valid(d)
  151. if err != nil || !ok {
  152. return ok, err
  153. }
  154. // Check client's address is listed in the client addresses in the ticket
  155. if len(a.Ticket.DecryptedEncPart.CAddr) > 0 {
  156. //If client addresses are present check if any of them match the source IP that sent the APReq
  157. //If there is no match return KRB_AP_ERR_BADADDR error.
  158. if !types.HostAddressesContains(a.Ticket.DecryptedEncPart.CAddr, cAddr) {
  159. 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")
  160. }
  161. }
  162. // Decrypt authenticator with session key from ticket's encrypted part
  163. err = a.DecryptAuthenticator(a.Ticket.DecryptedEncPart.Key)
  164. if err != nil {
  165. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BAD_INTEGRITY, "could not decrypt authenticator")
  166. }
  167. // Check CName in authenticator is the same as that in the ticket
  168. if !a.Authenticator.CName.Equal(a.Ticket.DecryptedEncPart.CName) {
  169. return false, NewKRBError(a.Ticket.SName, a.Ticket.Realm, errorcode.KRB_AP_ERR_BADMATCH, "CName in Authenticator does not match that in service ticket")
  170. }
  171. // Check the clock skew between the client and the service server
  172. ct := a.Authenticator.CTime.Add(time.Duration(a.Authenticator.Cusec) * time.Microsecond)
  173. t := time.Now().UTC()
  174. if t.Sub(ct) > d || ct.Sub(t) > d {
  175. 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))
  176. }
  177. return true, nil
  178. }