ocsp_srv.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/x509v3.h>
  15. #include <openssl/ocsp.h>
  16. #include "ocsp_lcl.h"
  17. /*
  18. * Utility functions related to sending OCSP responses and extracting
  19. * relevant information from the request.
  20. */
  21. int OCSP_request_onereq_count(OCSP_REQUEST *req)
  22. {
  23. return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
  24. }
  25. OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
  26. {
  27. return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
  28. }
  29. OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
  30. {
  31. return one->reqCert;
  32. }
  33. int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
  34. ASN1_OCTET_STRING **pikeyHash,
  35. ASN1_INTEGER **pserial, OCSP_CERTID *cid)
  36. {
  37. if (!cid)
  38. return 0;
  39. if (pmd)
  40. *pmd = cid->hashAlgorithm.algorithm;
  41. if (piNameHash)
  42. *piNameHash = &cid->issuerNameHash;
  43. if (pikeyHash)
  44. *pikeyHash = &cid->issuerKeyHash;
  45. if (pserial)
  46. *pserial = &cid->serialNumber;
  47. return 1;
  48. }
  49. int OCSP_request_is_signed(OCSP_REQUEST *req)
  50. {
  51. if (req->optionalSignature)
  52. return 1;
  53. return 0;
  54. }
  55. /* Create an OCSP response and encode an optional basic response */
  56. OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
  57. {
  58. OCSP_RESPONSE *rsp = NULL;
  59. if ((rsp = OCSP_RESPONSE_new()) == NULL)
  60. goto err;
  61. if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
  62. goto err;
  63. if (!bs)
  64. return rsp;
  65. if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
  66. goto err;
  67. rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
  68. if (!ASN1_item_pack
  69. (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
  70. goto err;
  71. return rsp;
  72. err:
  73. OCSP_RESPONSE_free(rsp);
  74. return NULL;
  75. }
  76. OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
  77. OCSP_CERTID *cid,
  78. int status, int reason,
  79. ASN1_TIME *revtime,
  80. ASN1_TIME *thisupd,
  81. ASN1_TIME *nextupd)
  82. {
  83. OCSP_SINGLERESP *single = NULL;
  84. OCSP_CERTSTATUS *cs;
  85. OCSP_REVOKEDINFO *ri;
  86. if (rsp->tbsResponseData.responses == NULL
  87. && (rsp->tbsResponseData.responses
  88. = sk_OCSP_SINGLERESP_new_null()) == NULL)
  89. goto err;
  90. if ((single = OCSP_SINGLERESP_new()) == NULL)
  91. goto err;
  92. if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
  93. goto err;
  94. if (nextupd &&
  95. !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
  96. goto err;
  97. OCSP_CERTID_free(single->certId);
  98. if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
  99. goto err;
  100. cs = single->certStatus;
  101. switch (cs->type = status) {
  102. case V_OCSP_CERTSTATUS_REVOKED:
  103. if (!revtime) {
  104. OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
  105. goto err;
  106. }
  107. if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
  108. goto err;
  109. if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
  110. goto err;
  111. if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
  112. if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
  113. goto err;
  114. if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
  115. goto err;
  116. }
  117. break;
  118. case V_OCSP_CERTSTATUS_GOOD:
  119. if ((cs->value.good = ASN1_NULL_new()) == NULL)
  120. goto err;
  121. break;
  122. case V_OCSP_CERTSTATUS_UNKNOWN:
  123. if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
  124. goto err;
  125. break;
  126. default:
  127. goto err;
  128. }
  129. if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
  130. goto err;
  131. return single;
  132. err:
  133. OCSP_SINGLERESP_free(single);
  134. return NULL;
  135. }
  136. /* Add a certificate to an OCSP request */
  137. int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
  138. {
  139. if (resp->certs == NULL
  140. && (resp->certs = sk_X509_new_null()) == NULL)
  141. return 0;
  142. if (!sk_X509_push(resp->certs, cert))
  143. return 0;
  144. X509_up_ref(cert);
  145. return 1;
  146. }
  147. int OCSP_basic_sign(OCSP_BASICRESP *brsp,
  148. X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
  149. STACK_OF(X509) *certs, unsigned long flags)
  150. {
  151. int i;
  152. OCSP_RESPID *rid;
  153. if (!X509_check_private_key(signer, key)) {
  154. OCSPerr(OCSP_F_OCSP_BASIC_SIGN,
  155. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  156. goto err;
  157. }
  158. if (!(flags & OCSP_NOCERTS)) {
  159. if (!OCSP_basic_add1_cert(brsp, signer))
  160. goto err;
  161. for (i = 0; i < sk_X509_num(certs); i++) {
  162. X509 *tmpcert = sk_X509_value(certs, i);
  163. if (!OCSP_basic_add1_cert(brsp, tmpcert))
  164. goto err;
  165. }
  166. }
  167. rid = &brsp->tbsResponseData.responderId;
  168. if (flags & OCSP_RESPID_KEY) {
  169. if (!OCSP_RESPID_set_by_key(rid, signer))
  170. goto err;
  171. } else if (!OCSP_RESPID_set_by_name(rid, signer)) {
  172. goto err;
  173. }
  174. if (!(flags & OCSP_NOTIME) &&
  175. !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
  176. goto err;
  177. /*
  178. * Right now, I think that not doing double hashing is the right thing.
  179. * -- Richard Levitte
  180. */
  181. if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0))
  182. goto err;
  183. return 1;
  184. err:
  185. return 0;
  186. }
  187. int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert)
  188. {
  189. if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert)))
  190. return 0;
  191. respid->type = V_OCSP_RESPID_NAME;
  192. return 1;
  193. }
  194. int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert)
  195. {
  196. ASN1_OCTET_STRING *byKey = NULL;
  197. unsigned char md[SHA_DIGEST_LENGTH];
  198. /* RFC2560 requires SHA1 */
  199. if (!X509_pubkey_digest(cert, EVP_sha1(), md, NULL))
  200. return 0;
  201. byKey = ASN1_OCTET_STRING_new();
  202. if (byKey == NULL)
  203. return 0;
  204. if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) {
  205. ASN1_OCTET_STRING_free(byKey);
  206. return 0;
  207. }
  208. respid->type = V_OCSP_RESPID_KEY;
  209. respid->value.byKey = byKey;
  210. return 1;
  211. }
  212. int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert)
  213. {
  214. if (respid->type == V_OCSP_RESPID_KEY) {
  215. unsigned char md[SHA_DIGEST_LENGTH];
  216. if (respid->value.byKey == NULL)
  217. return 0;
  218. /* RFC2560 requires SHA1 */
  219. if (!X509_pubkey_digest(cert, EVP_sha1(), md, NULL))
  220. return 0;
  221. return (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH)
  222. && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md,
  223. SHA_DIGEST_LENGTH) == 0);
  224. } else if(respid->type == V_OCSP_RESPID_NAME) {
  225. if (respid->value.byName == NULL)
  226. return 0;
  227. return X509_NAME_cmp(respid->value.byName,
  228. X509_get_subject_name(cert)) == 0;
  229. }
  230. return 0;
  231. }