ocsp.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package ocsp parses OCSP responses as specified in RFC 2560. OCSP responses
  5. // are signed messages attesting to the validity of a certificate for a small
  6. // period of time. This is used to manage revocation for X.509 certificates.
  7. package ocsp // import "golang.org/x/crypto/ocsp"
  8. import (
  9. "crypto"
  10. "crypto/ecdsa"
  11. "crypto/elliptic"
  12. "crypto/rand"
  13. "crypto/rsa"
  14. "crypto/sha1"
  15. "crypto/x509"
  16. "crypto/x509/pkix"
  17. "encoding/asn1"
  18. "errors"
  19. "math/big"
  20. "time"
  21. )
  22. var idPKIXOCSPBasic = asn1.ObjectIdentifier([]int{1, 3, 6, 1, 5, 5, 7, 48, 1, 1})
  23. // These are internal structures that reflect the ASN.1 structure of an OCSP
  24. // response. See RFC 2560, section 4.2.
  25. const (
  26. ocspSuccess = 0
  27. ocspMalformed = 1
  28. ocspInternalError = 2
  29. ocspTryLater = 3
  30. ocspSigRequired = 4
  31. ocspUnauthorized = 5
  32. )
  33. type certID struct {
  34. HashAlgorithm pkix.AlgorithmIdentifier
  35. NameHash []byte
  36. IssuerKeyHash []byte
  37. SerialNumber *big.Int
  38. }
  39. // https://tools.ietf.org/html/rfc2560#section-4.1.1
  40. type ocspRequest struct {
  41. TBSRequest tbsRequest
  42. }
  43. type tbsRequest struct {
  44. Version int `asn1:"explicit,tag:0,default:0,optional"`
  45. RequestorName pkix.RDNSequence `asn1:"explicit,tag:1,optional"`
  46. RequestList []request
  47. }
  48. type request struct {
  49. Cert certID
  50. }
  51. type responseASN1 struct {
  52. Status asn1.Enumerated
  53. Response responseBytes `asn1:"explicit,tag:0"`
  54. }
  55. type responseBytes struct {
  56. ResponseType asn1.ObjectIdentifier
  57. Response []byte
  58. }
  59. type basicResponse struct {
  60. TBSResponseData responseData
  61. SignatureAlgorithm pkix.AlgorithmIdentifier
  62. Signature asn1.BitString
  63. Certificates []asn1.RawValue `asn1:"explicit,tag:0,optional"`
  64. }
  65. type responseData struct {
  66. Raw asn1.RawContent
  67. Version int `asn1:"optional,default:1,explicit,tag:0"`
  68. RawResponderName asn1.RawValue `asn1:"optional,explicit,tag:1"`
  69. KeyHash []byte `asn1:"optional,explicit,tag:2"`
  70. ProducedAt time.Time `asn1:"generalized"`
  71. Responses []singleResponse
  72. }
  73. type singleResponse struct {
  74. CertID certID
  75. Good asn1.Flag `asn1:"tag:0,optional"`
  76. Revoked revokedInfo `asn1:"explicit,tag:1,optional"`
  77. Unknown asn1.Flag `asn1:"tag:2,optional"`
  78. ThisUpdate time.Time `asn1:"generalized"`
  79. NextUpdate time.Time `asn1:"generalized,explicit,tag:0,optional"`
  80. }
  81. type revokedInfo struct {
  82. RevocationTime time.Time `asn1:"generalized"`
  83. Reason int `asn1:"explicit,tag:0,optional"`
  84. }
  85. var (
  86. oidSignatureMD2WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 2}
  87. oidSignatureMD5WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4}
  88. oidSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
  89. oidSignatureSHA256WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
  90. oidSignatureSHA384WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
  91. oidSignatureSHA512WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}
  92. oidSignatureDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3}
  93. oidSignatureDSAWithSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 4, 3, 2}
  94. oidSignatureECDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1}
  95. oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
  96. oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
  97. oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
  98. )
  99. var hashOIDs = map[crypto.Hash]asn1.ObjectIdentifier{
  100. crypto.SHA1: asn1.ObjectIdentifier([]int{1, 3, 14, 3, 2, 26}),
  101. crypto.SHA256: asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 1}),
  102. crypto.SHA384: asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 2}),
  103. crypto.SHA512: asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 3}),
  104. }
  105. // TODO(rlb): This is also from crypto/x509, so same comment as AGL's below
  106. var signatureAlgorithmDetails = []struct {
  107. algo x509.SignatureAlgorithm
  108. oid asn1.ObjectIdentifier
  109. pubKeyAlgo x509.PublicKeyAlgorithm
  110. hash crypto.Hash
  111. }{
  112. {x509.MD2WithRSA, oidSignatureMD2WithRSA, x509.RSA, crypto.Hash(0) /* no value for MD2 */},
  113. {x509.MD5WithRSA, oidSignatureMD5WithRSA, x509.RSA, crypto.MD5},
  114. {x509.SHA1WithRSA, oidSignatureSHA1WithRSA, x509.RSA, crypto.SHA1},
  115. {x509.SHA256WithRSA, oidSignatureSHA256WithRSA, x509.RSA, crypto.SHA256},
  116. {x509.SHA384WithRSA, oidSignatureSHA384WithRSA, x509.RSA, crypto.SHA384},
  117. {x509.SHA512WithRSA, oidSignatureSHA512WithRSA, x509.RSA, crypto.SHA512},
  118. {x509.DSAWithSHA1, oidSignatureDSAWithSHA1, x509.DSA, crypto.SHA1},
  119. {x509.DSAWithSHA256, oidSignatureDSAWithSHA256, x509.DSA, crypto.SHA256},
  120. {x509.ECDSAWithSHA1, oidSignatureECDSAWithSHA1, x509.ECDSA, crypto.SHA1},
  121. {x509.ECDSAWithSHA256, oidSignatureECDSAWithSHA256, x509.ECDSA, crypto.SHA256},
  122. {x509.ECDSAWithSHA384, oidSignatureECDSAWithSHA384, x509.ECDSA, crypto.SHA384},
  123. {x509.ECDSAWithSHA512, oidSignatureECDSAWithSHA512, x509.ECDSA, crypto.SHA512},
  124. }
  125. // TODO(rlb): This is also from crypto/x509, so same comment as AGL's below
  126. func signingParamsForPublicKey(pub interface{}, requestedSigAlgo x509.SignatureAlgorithm) (hashFunc crypto.Hash, sigAlgo pkix.AlgorithmIdentifier, err error) {
  127. var pubType x509.PublicKeyAlgorithm
  128. switch pub := pub.(type) {
  129. case *rsa.PublicKey:
  130. pubType = x509.RSA
  131. hashFunc = crypto.SHA256
  132. sigAlgo.Algorithm = oidSignatureSHA256WithRSA
  133. sigAlgo.Parameters = asn1.RawValue{
  134. Tag: 5,
  135. }
  136. case *ecdsa.PublicKey:
  137. pubType = x509.ECDSA
  138. switch pub.Curve {
  139. case elliptic.P224(), elliptic.P256():
  140. hashFunc = crypto.SHA256
  141. sigAlgo.Algorithm = oidSignatureECDSAWithSHA256
  142. case elliptic.P384():
  143. hashFunc = crypto.SHA384
  144. sigAlgo.Algorithm = oidSignatureECDSAWithSHA384
  145. case elliptic.P521():
  146. hashFunc = crypto.SHA512
  147. sigAlgo.Algorithm = oidSignatureECDSAWithSHA512
  148. default:
  149. err = errors.New("x509: unknown elliptic curve")
  150. }
  151. default:
  152. err = errors.New("x509: only RSA and ECDSA keys supported")
  153. }
  154. if err != nil {
  155. return
  156. }
  157. if requestedSigAlgo == 0 {
  158. return
  159. }
  160. found := false
  161. for _, details := range signatureAlgorithmDetails {
  162. if details.algo == requestedSigAlgo {
  163. if details.pubKeyAlgo != pubType {
  164. err = errors.New("x509: requested SignatureAlgorithm does not match private key type")
  165. return
  166. }
  167. sigAlgo.Algorithm, hashFunc = details.oid, details.hash
  168. if hashFunc == 0 {
  169. err = errors.New("x509: cannot sign with hash function requested")
  170. return
  171. }
  172. found = true
  173. break
  174. }
  175. }
  176. if !found {
  177. err = errors.New("x509: unknown SignatureAlgorithm")
  178. }
  179. return
  180. }
  181. // TODO(agl): this is taken from crypto/x509 and so should probably be exported
  182. // from crypto/x509 or crypto/x509/pkix.
  183. func getSignatureAlgorithmFromOID(oid asn1.ObjectIdentifier) x509.SignatureAlgorithm {
  184. for _, details := range signatureAlgorithmDetails {
  185. if oid.Equal(details.oid) {
  186. return details.algo
  187. }
  188. }
  189. return x509.UnknownSignatureAlgorithm
  190. }
  191. // TODO(rlb): This is not taken from crypto/x509, but it's of the same general form.
  192. func getHashAlgorithmFromOID(target asn1.ObjectIdentifier) crypto.Hash {
  193. for hash, oid := range hashOIDs {
  194. if oid.Equal(target) {
  195. return hash
  196. }
  197. }
  198. return crypto.Hash(0)
  199. }
  200. // This is the exposed reflection of the internal OCSP structures.
  201. const (
  202. // Good means that the certificate is valid.
  203. Good = iota
  204. // Revoked means that the certificate has been deliberately revoked.
  205. Revoked = iota
  206. // Unknown means that the OCSP responder doesn't know about the certificate.
  207. Unknown = iota
  208. // ServerFailed means that the OCSP responder failed to process the request.
  209. ServerFailed = iota
  210. )
  211. // Request represents an OCSP request. See RFC 2560.
  212. type Request struct {
  213. HashAlgorithm crypto.Hash
  214. IssuerNameHash []byte
  215. IssuerKeyHash []byte
  216. SerialNumber *big.Int
  217. }
  218. // Response represents an OCSP response. See RFC 2560.
  219. type Response struct {
  220. // Status is one of {Good, Revoked, Unknown, ServerFailed}
  221. Status int
  222. SerialNumber *big.Int
  223. ProducedAt, ThisUpdate, NextUpdate, RevokedAt time.Time
  224. RevocationReason int
  225. Certificate *x509.Certificate
  226. // TBSResponseData contains the raw bytes of the signed response. If
  227. // Certificate is nil then this can be used to verify Signature.
  228. TBSResponseData []byte
  229. Signature []byte
  230. SignatureAlgorithm x509.SignatureAlgorithm
  231. }
  232. // These are pre-serialized error responses for the various non-success codes
  233. // defined by OCSP. The Unauthorized code in particular can be used by an OCSP
  234. // responder that supports only pre-signed responses as a response to requests
  235. // for certificates with unknown status. See RFC 5019.
  236. var (
  237. MalformedRequestErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x01}
  238. InternalErrorErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x02}
  239. TryLaterErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x03}
  240. SigRequredErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x05}
  241. UnauthorizedErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x06}
  242. )
  243. // CheckSignatureFrom checks that the signature in resp is a valid signature
  244. // from issuer. This should only be used if resp.Certificate is nil. Otherwise,
  245. // the OCSP response contained an intermediate certificate that created the
  246. // signature. That signature is checked by ParseResponse and only
  247. // resp.Certificate remains to be validated.
  248. func (resp *Response) CheckSignatureFrom(issuer *x509.Certificate) error {
  249. return issuer.CheckSignature(resp.SignatureAlgorithm, resp.TBSResponseData, resp.Signature)
  250. }
  251. // ParseError results from an invalid OCSP response.
  252. type ParseError string
  253. func (p ParseError) Error() string {
  254. return string(p)
  255. }
  256. // ParseRequest parses an OCSP request in DER form. It only supports
  257. // requests for a single certificate. Signed requests are not supported.
  258. // If a request includes a signature, it will result in a ParseError.
  259. func ParseRequest(bytes []byte) (*Request, error) {
  260. var req ocspRequest
  261. rest, err := asn1.Unmarshal(bytes, &req)
  262. if err != nil {
  263. return nil, err
  264. }
  265. if len(rest) > 0 {
  266. return nil, ParseError("trailing data in OCSP request")
  267. }
  268. if len(req.TBSRequest.RequestList) == 0 {
  269. return nil, ParseError("OCSP request contains no request body")
  270. }
  271. innerRequest := req.TBSRequest.RequestList[0]
  272. hashFunc := getHashAlgorithmFromOID(innerRequest.Cert.HashAlgorithm.Algorithm)
  273. if hashFunc == crypto.Hash(0) {
  274. return nil, ParseError("OCSP request uses unknown hash function")
  275. }
  276. return &Request{
  277. HashAlgorithm: hashFunc,
  278. IssuerNameHash: innerRequest.Cert.NameHash,
  279. IssuerKeyHash: innerRequest.Cert.IssuerKeyHash,
  280. SerialNumber: innerRequest.Cert.SerialNumber,
  281. }, nil
  282. }
  283. // ParseResponse parses an OCSP response in DER form. It only supports
  284. // responses for a single certificate. If the response contains a certificate
  285. // then the signature over the response is checked. If issuer is not nil then
  286. // it will be used to validate the signature or embedded certificate. Invalid
  287. // signatures or parse failures will result in a ParseError.
  288. func ParseResponse(bytes []byte, issuer *x509.Certificate) (*Response, error) {
  289. var resp responseASN1
  290. rest, err := asn1.Unmarshal(bytes, &resp)
  291. if err != nil {
  292. return nil, err
  293. }
  294. if len(rest) > 0 {
  295. return nil, ParseError("trailing data in OCSP response")
  296. }
  297. ret := new(Response)
  298. if resp.Status != ocspSuccess {
  299. ret.Status = ServerFailed
  300. return ret, nil
  301. }
  302. if !resp.Response.ResponseType.Equal(idPKIXOCSPBasic) {
  303. return nil, ParseError("bad OCSP response type")
  304. }
  305. var basicResp basicResponse
  306. rest, err = asn1.Unmarshal(resp.Response.Response, &basicResp)
  307. if err != nil {
  308. return nil, err
  309. }
  310. if len(basicResp.Certificates) > 1 {
  311. return nil, ParseError("OCSP response contains bad number of certificates")
  312. }
  313. if len(basicResp.TBSResponseData.Responses) != 1 {
  314. return nil, ParseError("OCSP response contains bad number of responses")
  315. }
  316. ret.TBSResponseData = basicResp.TBSResponseData.Raw
  317. ret.Signature = basicResp.Signature.RightAlign()
  318. ret.SignatureAlgorithm = getSignatureAlgorithmFromOID(basicResp.SignatureAlgorithm.Algorithm)
  319. if len(basicResp.Certificates) > 0 {
  320. ret.Certificate, err = x509.ParseCertificate(basicResp.Certificates[0].FullBytes)
  321. if err != nil {
  322. return nil, err
  323. }
  324. if err := ret.CheckSignatureFrom(ret.Certificate); err != nil {
  325. return nil, ParseError("bad OCSP signature")
  326. }
  327. if issuer != nil {
  328. if err := issuer.CheckSignature(ret.Certificate.SignatureAlgorithm, ret.Certificate.RawTBSCertificate, ret.Certificate.Signature); err != nil {
  329. return nil, ParseError("bad signature on embedded certificate")
  330. }
  331. }
  332. } else if issuer != nil {
  333. if err := ret.CheckSignatureFrom(issuer); err != nil {
  334. return nil, ParseError("bad OCSP signature")
  335. }
  336. }
  337. r := basicResp.TBSResponseData.Responses[0]
  338. ret.SerialNumber = r.CertID.SerialNumber
  339. switch {
  340. case bool(r.Good):
  341. ret.Status = Good
  342. case bool(r.Unknown):
  343. ret.Status = Unknown
  344. default:
  345. ret.Status = Revoked
  346. ret.RevokedAt = r.Revoked.RevocationTime
  347. ret.RevocationReason = r.Revoked.Reason
  348. }
  349. ret.ProducedAt = basicResp.TBSResponseData.ProducedAt
  350. ret.ThisUpdate = r.ThisUpdate
  351. ret.NextUpdate = r.NextUpdate
  352. return ret, nil
  353. }
  354. // RequestOptions contains options for constructing OCSP requests.
  355. type RequestOptions struct {
  356. // Hash contains the hash function that should be used when
  357. // constructing the OCSP request. If zero, SHA-1 will be used.
  358. Hash crypto.Hash
  359. }
  360. func (opts *RequestOptions) hash() crypto.Hash {
  361. if opts == nil || opts.Hash == 0 {
  362. // SHA-1 is nearly universally used in OCSP.
  363. return crypto.SHA1
  364. }
  365. return opts.Hash
  366. }
  367. // CreateRequest returns a DER-encoded, OCSP request for the status of cert. If
  368. // opts is nil then sensible defaults are used.
  369. func CreateRequest(cert, issuer *x509.Certificate, opts *RequestOptions) ([]byte, error) {
  370. hashFunc := opts.hash()
  371. // OCSP seems to be the only place where these raw hash identifiers are
  372. // used. I took the following from
  373. // http://msdn.microsoft.com/en-us/library/ff635603.aspx
  374. var hashOID asn1.ObjectIdentifier
  375. hashOID, ok := hashOIDs[hashFunc]
  376. if !ok {
  377. return nil, x509.ErrUnsupportedAlgorithm
  378. }
  379. if !hashFunc.Available() {
  380. return nil, x509.ErrUnsupportedAlgorithm
  381. }
  382. h := opts.hash().New()
  383. var publicKeyInfo struct {
  384. Algorithm pkix.AlgorithmIdentifier
  385. PublicKey asn1.BitString
  386. }
  387. if _, err := asn1.Unmarshal(issuer.RawSubjectPublicKeyInfo, &publicKeyInfo); err != nil {
  388. return nil, err
  389. }
  390. h.Write(publicKeyInfo.PublicKey.RightAlign())
  391. issuerKeyHash := h.Sum(nil)
  392. h.Reset()
  393. h.Write(issuer.RawSubject)
  394. issuerNameHash := h.Sum(nil)
  395. return asn1.Marshal(ocspRequest{
  396. tbsRequest{
  397. Version: 0,
  398. RequestList: []request{
  399. {
  400. Cert: certID{
  401. pkix.AlgorithmIdentifier{
  402. Algorithm: hashOID,
  403. Parameters: asn1.RawValue{Tag: 5 /* ASN.1 NULL */},
  404. },
  405. issuerNameHash,
  406. issuerKeyHash,
  407. cert.SerialNumber,
  408. },
  409. },
  410. },
  411. },
  412. })
  413. }
  414. // CreateResponse returns a DER-encoded OCSP response with the specified contents.
  415. // The fields in the response are populated as follows:
  416. //
  417. // The responder cert is used to populate the ResponderName field, and the certificate
  418. // itself is provided alongside the OCSP response signature.
  419. //
  420. // The issuer cert is used to puplate the IssuerNameHash and IssuerKeyHash fields.
  421. // (SHA-1 is used for the hash function; this is not configurable.)
  422. //
  423. // The template is used to populate the SerialNumber, RevocationStatus, RevokedAt,
  424. // RevocationReason, ThisUpdate, and NextUpdate fields.
  425. //
  426. // The ProducedAt date is automatically set to the current date, to the nearest minute.
  427. func CreateResponse(issuer, responderCert *x509.Certificate, template Response, priv crypto.Signer) ([]byte, error) {
  428. var publicKeyInfo struct {
  429. Algorithm pkix.AlgorithmIdentifier
  430. PublicKey asn1.BitString
  431. }
  432. if _, err := asn1.Unmarshal(issuer.RawSubjectPublicKeyInfo, &publicKeyInfo); err != nil {
  433. return nil, err
  434. }
  435. h := sha1.New()
  436. h.Write(publicKeyInfo.PublicKey.RightAlign())
  437. issuerKeyHash := h.Sum(nil)
  438. h.Reset()
  439. h.Write(issuer.RawSubject)
  440. issuerNameHash := h.Sum(nil)
  441. innerResponse := singleResponse{
  442. CertID: certID{
  443. HashAlgorithm: pkix.AlgorithmIdentifier{
  444. Algorithm: hashOIDs[crypto.SHA1],
  445. Parameters: asn1.RawValue{Tag: 5 /* ASN.1 NULL */},
  446. },
  447. NameHash: issuerNameHash,
  448. IssuerKeyHash: issuerKeyHash,
  449. SerialNumber: template.SerialNumber,
  450. },
  451. ThisUpdate: template.ThisUpdate.UTC(),
  452. NextUpdate: template.NextUpdate.UTC(),
  453. }
  454. switch template.Status {
  455. case Good:
  456. innerResponse.Good = true
  457. case Unknown:
  458. innerResponse.Unknown = true
  459. case Revoked:
  460. innerResponse.Revoked = revokedInfo{
  461. RevocationTime: template.RevokedAt.UTC(),
  462. Reason: template.RevocationReason,
  463. }
  464. }
  465. responderName := asn1.RawValue{
  466. Class: 2, // context-specific
  467. Tag: 1, // explicit tag
  468. IsCompound: true,
  469. Bytes: responderCert.RawSubject,
  470. }
  471. tbsResponseData := responseData{
  472. Version: 0,
  473. RawResponderName: responderName,
  474. ProducedAt: time.Now().Truncate(time.Minute).UTC(),
  475. Responses: []singleResponse{innerResponse},
  476. }
  477. tbsResponseDataDER, err := asn1.Marshal(tbsResponseData)
  478. if err != nil {
  479. return nil, err
  480. }
  481. hashFunc, signatureAlgorithm, err := signingParamsForPublicKey(priv.Public(), template.SignatureAlgorithm)
  482. if err != nil {
  483. return nil, err
  484. }
  485. responseHash := hashFunc.New()
  486. responseHash.Write(tbsResponseDataDER)
  487. signature, err := priv.Sign(rand.Reader, responseHash.Sum(nil), hashFunc)
  488. if err != nil {
  489. return nil, err
  490. }
  491. response := basicResponse{
  492. TBSResponseData: tbsResponseData,
  493. SignatureAlgorithm: signatureAlgorithm,
  494. Signature: asn1.BitString{
  495. Bytes: signature,
  496. BitLength: 8 * len(signature),
  497. },
  498. }
  499. if template.Certificate != nil {
  500. response.Certificates = []asn1.RawValue{
  501. asn1.RawValue{FullBytes: template.Certificate.Raw},
  502. }
  503. }
  504. responseDER, err := asn1.Marshal(response)
  505. if err != nil {
  506. return nil, err
  507. }
  508. return asn1.Marshal(responseASN1{
  509. Status: ocspSuccess,
  510. Response: responseBytes{
  511. ResponseType: idPKIXOCSPBasic,
  512. Response: responseDER,
  513. },
  514. })
  515. }