ocsp.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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:"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 asn1.Enumerated `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. // The status values that can be expressed in OCSP. See RFC 6960.
  202. const (
  203. // Good means that the certificate is valid.
  204. Good = iota
  205. // Revoked means that the certificate has been deliberately revoked.
  206. Revoked = iota
  207. // Unknown means that the OCSP responder doesn't know about the certificate.
  208. Unknown = iota
  209. // ServerFailed means that the OCSP responder failed to process the request.
  210. ServerFailed = iota
  211. )
  212. // The enumerated reasons for revoking a certificate. See RFC 5280.
  213. const (
  214. Unspecified = iota
  215. KeyCompromise = iota
  216. CACompromise = iota
  217. AffiliationChanged = iota
  218. Superseded = iota
  219. CessationOfOperation = iota
  220. CertificateHold = iota
  221. _ = iota
  222. RemoveFromCRL = iota
  223. PrivilegeWithdrawn = iota
  224. AACompromise = iota
  225. )
  226. // Request represents an OCSP request. See RFC 2560.
  227. type Request struct {
  228. HashAlgorithm crypto.Hash
  229. IssuerNameHash []byte
  230. IssuerKeyHash []byte
  231. SerialNumber *big.Int
  232. }
  233. // Response represents an OCSP response. See RFC 2560.
  234. type Response struct {
  235. // Status is one of {Good, Revoked, Unknown, ServerFailed}
  236. Status int
  237. SerialNumber *big.Int
  238. ProducedAt, ThisUpdate, NextUpdate, RevokedAt time.Time
  239. RevocationReason int
  240. Certificate *x509.Certificate
  241. // TBSResponseData contains the raw bytes of the signed response. If
  242. // Certificate is nil then this can be used to verify Signature.
  243. TBSResponseData []byte
  244. Signature []byte
  245. SignatureAlgorithm x509.SignatureAlgorithm
  246. }
  247. // These are pre-serialized error responses for the various non-success codes
  248. // defined by OCSP. The Unauthorized code in particular can be used by an OCSP
  249. // responder that supports only pre-signed responses as a response to requests
  250. // for certificates with unknown status. See RFC 5019.
  251. var (
  252. MalformedRequestErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x01}
  253. InternalErrorErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x02}
  254. TryLaterErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x03}
  255. SigRequredErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x05}
  256. UnauthorizedErrorResponse = []byte{0x30, 0x03, 0x0A, 0x01, 0x06}
  257. )
  258. // CheckSignatureFrom checks that the signature in resp is a valid signature
  259. // from issuer. This should only be used if resp.Certificate is nil. Otherwise,
  260. // the OCSP response contained an intermediate certificate that created the
  261. // signature. That signature is checked by ParseResponse and only
  262. // resp.Certificate remains to be validated.
  263. func (resp *Response) CheckSignatureFrom(issuer *x509.Certificate) error {
  264. return issuer.CheckSignature(resp.SignatureAlgorithm, resp.TBSResponseData, resp.Signature)
  265. }
  266. // ParseError results from an invalid OCSP response.
  267. type ParseError string
  268. func (p ParseError) Error() string {
  269. return string(p)
  270. }
  271. // ParseRequest parses an OCSP request in DER form. It only supports
  272. // requests for a single certificate. Signed requests are not supported.
  273. // If a request includes a signature, it will result in a ParseError.
  274. func ParseRequest(bytes []byte) (*Request, error) {
  275. var req ocspRequest
  276. rest, err := asn1.Unmarshal(bytes, &req)
  277. if err != nil {
  278. return nil, err
  279. }
  280. if len(rest) > 0 {
  281. return nil, ParseError("trailing data in OCSP request")
  282. }
  283. if len(req.TBSRequest.RequestList) == 0 {
  284. return nil, ParseError("OCSP request contains no request body")
  285. }
  286. innerRequest := req.TBSRequest.RequestList[0]
  287. hashFunc := getHashAlgorithmFromOID(innerRequest.Cert.HashAlgorithm.Algorithm)
  288. if hashFunc == crypto.Hash(0) {
  289. return nil, ParseError("OCSP request uses unknown hash function")
  290. }
  291. return &Request{
  292. HashAlgorithm: hashFunc,
  293. IssuerNameHash: innerRequest.Cert.NameHash,
  294. IssuerKeyHash: innerRequest.Cert.IssuerKeyHash,
  295. SerialNumber: innerRequest.Cert.SerialNumber,
  296. }, nil
  297. }
  298. // ParseResponse parses an OCSP response in DER form. It only supports
  299. // responses for a single certificate. If the response contains a certificate
  300. // then the signature over the response is checked. If issuer is not nil then
  301. // it will be used to validate the signature or embedded certificate. Invalid
  302. // signatures or parse failures will result in a ParseError.
  303. func ParseResponse(bytes []byte, issuer *x509.Certificate) (*Response, error) {
  304. var resp responseASN1
  305. rest, err := asn1.Unmarshal(bytes, &resp)
  306. if err != nil {
  307. return nil, err
  308. }
  309. if len(rest) > 0 {
  310. return nil, ParseError("trailing data in OCSP response")
  311. }
  312. ret := new(Response)
  313. if resp.Status != ocspSuccess {
  314. ret.Status = ServerFailed
  315. return ret, nil
  316. }
  317. if !resp.Response.ResponseType.Equal(idPKIXOCSPBasic) {
  318. return nil, ParseError("bad OCSP response type")
  319. }
  320. var basicResp basicResponse
  321. rest, err = asn1.Unmarshal(resp.Response.Response, &basicResp)
  322. if err != nil {
  323. return nil, err
  324. }
  325. if len(basicResp.Certificates) > 1 {
  326. return nil, ParseError("OCSP response contains bad number of certificates")
  327. }
  328. if len(basicResp.TBSResponseData.Responses) != 1 {
  329. return nil, ParseError("OCSP response contains bad number of responses")
  330. }
  331. ret.TBSResponseData = basicResp.TBSResponseData.Raw
  332. ret.Signature = basicResp.Signature.RightAlign()
  333. ret.SignatureAlgorithm = getSignatureAlgorithmFromOID(basicResp.SignatureAlgorithm.Algorithm)
  334. if len(basicResp.Certificates) > 0 {
  335. ret.Certificate, err = x509.ParseCertificate(basicResp.Certificates[0].FullBytes)
  336. if err != nil {
  337. return nil, err
  338. }
  339. if err := ret.CheckSignatureFrom(ret.Certificate); err != nil {
  340. return nil, ParseError("bad OCSP signature")
  341. }
  342. if issuer != nil {
  343. if err := issuer.CheckSignature(ret.Certificate.SignatureAlgorithm, ret.Certificate.RawTBSCertificate, ret.Certificate.Signature); err != nil {
  344. return nil, ParseError("bad signature on embedded certificate")
  345. }
  346. }
  347. } else if issuer != nil {
  348. if err := ret.CheckSignatureFrom(issuer); err != nil {
  349. return nil, ParseError("bad OCSP signature")
  350. }
  351. }
  352. r := basicResp.TBSResponseData.Responses[0]
  353. ret.SerialNumber = r.CertID.SerialNumber
  354. switch {
  355. case bool(r.Good):
  356. ret.Status = Good
  357. case bool(r.Unknown):
  358. ret.Status = Unknown
  359. default:
  360. ret.Status = Revoked
  361. ret.RevokedAt = r.Revoked.RevocationTime
  362. ret.RevocationReason = int(r.Revoked.Reason)
  363. }
  364. ret.ProducedAt = basicResp.TBSResponseData.ProducedAt
  365. ret.ThisUpdate = r.ThisUpdate
  366. ret.NextUpdate = r.NextUpdate
  367. return ret, nil
  368. }
  369. // RequestOptions contains options for constructing OCSP requests.
  370. type RequestOptions struct {
  371. // Hash contains the hash function that should be used when
  372. // constructing the OCSP request. If zero, SHA-1 will be used.
  373. Hash crypto.Hash
  374. }
  375. func (opts *RequestOptions) hash() crypto.Hash {
  376. if opts == nil || opts.Hash == 0 {
  377. // SHA-1 is nearly universally used in OCSP.
  378. return crypto.SHA1
  379. }
  380. return opts.Hash
  381. }
  382. // CreateRequest returns a DER-encoded, OCSP request for the status of cert. If
  383. // opts is nil then sensible defaults are used.
  384. func CreateRequest(cert, issuer *x509.Certificate, opts *RequestOptions) ([]byte, error) {
  385. hashFunc := opts.hash()
  386. // OCSP seems to be the only place where these raw hash identifiers are
  387. // used. I took the following from
  388. // http://msdn.microsoft.com/en-us/library/ff635603.aspx
  389. var hashOID asn1.ObjectIdentifier
  390. hashOID, ok := hashOIDs[hashFunc]
  391. if !ok {
  392. return nil, x509.ErrUnsupportedAlgorithm
  393. }
  394. if !hashFunc.Available() {
  395. return nil, x509.ErrUnsupportedAlgorithm
  396. }
  397. h := opts.hash().New()
  398. var publicKeyInfo struct {
  399. Algorithm pkix.AlgorithmIdentifier
  400. PublicKey asn1.BitString
  401. }
  402. if _, err := asn1.Unmarshal(issuer.RawSubjectPublicKeyInfo, &publicKeyInfo); err != nil {
  403. return nil, err
  404. }
  405. h.Write(publicKeyInfo.PublicKey.RightAlign())
  406. issuerKeyHash := h.Sum(nil)
  407. h.Reset()
  408. h.Write(issuer.RawSubject)
  409. issuerNameHash := h.Sum(nil)
  410. return asn1.Marshal(ocspRequest{
  411. tbsRequest{
  412. Version: 0,
  413. RequestList: []request{
  414. {
  415. Cert: certID{
  416. pkix.AlgorithmIdentifier{
  417. Algorithm: hashOID,
  418. Parameters: asn1.RawValue{Tag: 5 /* ASN.1 NULL */},
  419. },
  420. issuerNameHash,
  421. issuerKeyHash,
  422. cert.SerialNumber,
  423. },
  424. },
  425. },
  426. },
  427. })
  428. }
  429. // CreateResponse returns a DER-encoded OCSP response with the specified contents.
  430. // The fields in the response are populated as follows:
  431. //
  432. // The responder cert is used to populate the ResponderName field, and the certificate
  433. // itself is provided alongside the OCSP response signature.
  434. //
  435. // The issuer cert is used to puplate the IssuerNameHash and IssuerKeyHash fields.
  436. // (SHA-1 is used for the hash function; this is not configurable.)
  437. //
  438. // The template is used to populate the SerialNumber, RevocationStatus, RevokedAt,
  439. // RevocationReason, ThisUpdate, and NextUpdate fields.
  440. //
  441. // The ProducedAt date is automatically set to the current date, to the nearest minute.
  442. func CreateResponse(issuer, responderCert *x509.Certificate, template Response, priv crypto.Signer) ([]byte, error) {
  443. var publicKeyInfo struct {
  444. Algorithm pkix.AlgorithmIdentifier
  445. PublicKey asn1.BitString
  446. }
  447. if _, err := asn1.Unmarshal(issuer.RawSubjectPublicKeyInfo, &publicKeyInfo); err != nil {
  448. return nil, err
  449. }
  450. h := sha1.New()
  451. h.Write(publicKeyInfo.PublicKey.RightAlign())
  452. issuerKeyHash := h.Sum(nil)
  453. h.Reset()
  454. h.Write(issuer.RawSubject)
  455. issuerNameHash := h.Sum(nil)
  456. innerResponse := singleResponse{
  457. CertID: certID{
  458. HashAlgorithm: pkix.AlgorithmIdentifier{
  459. Algorithm: hashOIDs[crypto.SHA1],
  460. Parameters: asn1.RawValue{Tag: 5 /* ASN.1 NULL */},
  461. },
  462. NameHash: issuerNameHash,
  463. IssuerKeyHash: issuerKeyHash,
  464. SerialNumber: template.SerialNumber,
  465. },
  466. ThisUpdate: template.ThisUpdate.UTC(),
  467. NextUpdate: template.NextUpdate.UTC(),
  468. }
  469. switch template.Status {
  470. case Good:
  471. innerResponse.Good = true
  472. case Unknown:
  473. innerResponse.Unknown = true
  474. case Revoked:
  475. innerResponse.Revoked = revokedInfo{
  476. RevocationTime: template.RevokedAt.UTC(),
  477. Reason: asn1.Enumerated(template.RevocationReason),
  478. }
  479. }
  480. responderName := asn1.RawValue{
  481. Class: 2, // context-specific
  482. Tag: 1, // explicit tag
  483. IsCompound: true,
  484. Bytes: responderCert.RawSubject,
  485. }
  486. tbsResponseData := responseData{
  487. Version: 0,
  488. RawResponderName: responderName,
  489. ProducedAt: time.Now().Truncate(time.Minute).UTC(),
  490. Responses: []singleResponse{innerResponse},
  491. }
  492. tbsResponseDataDER, err := asn1.Marshal(tbsResponseData)
  493. if err != nil {
  494. return nil, err
  495. }
  496. hashFunc, signatureAlgorithm, err := signingParamsForPublicKey(priv.Public(), template.SignatureAlgorithm)
  497. if err != nil {
  498. return nil, err
  499. }
  500. responseHash := hashFunc.New()
  501. responseHash.Write(tbsResponseDataDER)
  502. signature, err := priv.Sign(rand.Reader, responseHash.Sum(nil), hashFunc)
  503. if err != nil {
  504. return nil, err
  505. }
  506. response := basicResponse{
  507. TBSResponseData: tbsResponseData,
  508. SignatureAlgorithm: signatureAlgorithm,
  509. Signature: asn1.BitString{
  510. Bytes: signature,
  511. BitLength: 8 * len(signature),
  512. },
  513. }
  514. if template.Certificate != nil {
  515. response.Certificates = []asn1.RawValue{
  516. asn1.RawValue{FullBytes: template.Certificate.Raw},
  517. }
  518. }
  519. responseDER, err := asn1.Marshal(response)
  520. if err != nil {
  521. return nil, err
  522. }
  523. return asn1.Marshal(responseASN1{
  524. Status: ocspSuccess,
  525. Response: responseBytes{
  526. ResponseType: idPKIXOCSPBasic,
  527. Response: responseDER,
  528. },
  529. })
  530. }