ocsp_cl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <time.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/ocsp.h>
  18. #include "ocsp_local.h"
  19. /*
  20. * Utility functions related to sending OCSP requests and extracting relevant
  21. * information from the response.
  22. */
  23. /*
  24. * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
  25. * useful if we want to add extensions.
  26. */
  27. OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
  28. {
  29. OCSP_ONEREQ *one = NULL;
  30. if ((one = OCSP_ONEREQ_new()) == NULL)
  31. return NULL;
  32. OCSP_CERTID_free(one->reqCert);
  33. one->reqCert = cid;
  34. if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) {
  35. one->reqCert = NULL; /* do not free on error */
  36. OCSP_ONEREQ_free(one);
  37. return NULL;
  38. }
  39. return one;
  40. }
  41. /* Set requestorName from an X509_NAME structure */
  42. int OCSP_request_set1_name(OCSP_REQUEST *req, const X509_NAME *nm)
  43. {
  44. GENERAL_NAME *gen = GENERAL_NAME_new();
  45. if (gen == NULL)
  46. return 0;
  47. if (!X509_NAME_set(&gen->d.directoryName, nm)) {
  48. GENERAL_NAME_free(gen);
  49. return 0;
  50. }
  51. gen->type = GEN_DIRNAME;
  52. GENERAL_NAME_free(req->tbsRequest.requestorName);
  53. req->tbsRequest.requestorName = gen;
  54. return 1;
  55. }
  56. /* Add a certificate to an OCSP request */
  57. int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
  58. {
  59. if (req->optionalSignature == NULL
  60. && (req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
  61. return 0;
  62. if (cert == NULL)
  63. return 1;
  64. return ossl_x509_add_cert_new(&req->optionalSignature->certs, cert,
  65. X509_ADD_FLAG_UP_REF);
  66. }
  67. /*
  68. * Sign an OCSP request set the requestorName to the subject name of an
  69. * optional signers certificate and include one or more optional certificates
  70. * in the request. Behaves like PKCS7_sign().
  71. */
  72. int OCSP_request_sign(OCSP_REQUEST *req,
  73. X509 *signer,
  74. EVP_PKEY *key,
  75. const EVP_MD *dgst,
  76. STACK_OF(X509) *certs, unsigned long flags)
  77. {
  78. if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
  79. goto err;
  80. if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
  81. goto err;
  82. if (key != NULL) {
  83. if (!X509_check_private_key(signer, key)) {
  84. ERR_raise(ERR_LIB_OCSP,
  85. OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  86. goto err;
  87. }
  88. if (!OCSP_REQUEST_sign(req, key, dgst, signer->libctx, signer->propq))
  89. goto err;
  90. }
  91. if ((flags & OCSP_NOCERTS) == 0) {
  92. if (!OCSP_request_add1_cert(req, signer)
  93. || !X509_add_certs(req->optionalSignature->certs, certs,
  94. X509_ADD_FLAG_UP_REF))
  95. goto err;
  96. }
  97. return 1;
  98. err:
  99. OCSP_SIGNATURE_free(req->optionalSignature);
  100. req->optionalSignature = NULL;
  101. return 0;
  102. }
  103. /* Get response status */
  104. int OCSP_response_status(OCSP_RESPONSE *resp)
  105. {
  106. return ASN1_ENUMERATED_get(resp->responseStatus);
  107. }
  108. /*
  109. * Extract basic response from OCSP_RESPONSE or NULL if no basic response
  110. * present.
  111. */
  112. OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
  113. {
  114. OCSP_RESPBYTES *rb = resp->responseBytes;
  115. if (rb == NULL) {
  116. ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_RESPONSE_DATA);
  117. return NULL;
  118. }
  119. if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
  120. ERR_raise(ERR_LIB_OCSP, OCSP_R_NOT_BASIC_RESPONSE);
  121. return NULL;
  122. }
  123. return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
  124. }
  125. const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs)
  126. {
  127. return bs->signature;
  128. }
  129. const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs)
  130. {
  131. return &bs->signatureAlgorithm;
  132. }
  133. const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs)
  134. {
  135. return &bs->tbsResponseData;
  136. }
  137. /* Return number of OCSP_SINGLERESP responses present in a basic response */
  138. int OCSP_resp_count(OCSP_BASICRESP *bs)
  139. {
  140. if (bs == NULL)
  141. return -1;
  142. return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
  143. }
  144. /* Extract an OCSP_SINGLERESP response with a given index */
  145. OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
  146. {
  147. if (bs == NULL)
  148. return NULL;
  149. return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
  150. }
  151. const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP *bs)
  152. {
  153. return bs->tbsResponseData.producedAt;
  154. }
  155. const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs)
  156. {
  157. return bs->certs;
  158. }
  159. int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
  160. const ASN1_OCTET_STRING **pid,
  161. const X509_NAME **pname)
  162. {
  163. const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
  164. if (rid->type == V_OCSP_RESPID_NAME) {
  165. *pname = rid->value.byName;
  166. *pid = NULL;
  167. } else if (rid->type == V_OCSP_RESPID_KEY) {
  168. *pid = rid->value.byKey;
  169. *pname = NULL;
  170. } else {
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
  176. ASN1_OCTET_STRING **pid,
  177. X509_NAME **pname)
  178. {
  179. const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
  180. if (rid->type == V_OCSP_RESPID_NAME) {
  181. *pname = X509_NAME_dup(rid->value.byName);
  182. *pid = NULL;
  183. } else if (rid->type == V_OCSP_RESPID_KEY) {
  184. *pid = ASN1_OCTET_STRING_dup(rid->value.byKey);
  185. *pname = NULL;
  186. } else {
  187. return 0;
  188. }
  189. if (*pname == NULL && *pid == NULL)
  190. return 0;
  191. return 1;
  192. }
  193. /* Look single response matching a given certificate ID */
  194. int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
  195. {
  196. int i;
  197. STACK_OF(OCSP_SINGLERESP) *sresp;
  198. OCSP_SINGLERESP *single;
  199. if (bs == NULL)
  200. return -1;
  201. if (last < 0)
  202. last = 0;
  203. else
  204. last++;
  205. sresp = bs->tbsResponseData.responses;
  206. for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
  207. single = sk_OCSP_SINGLERESP_value(sresp, i);
  208. if (!OCSP_id_cmp(id, single->certId))
  209. return i;
  210. }
  211. return -1;
  212. }
  213. /*
  214. * Extract status information from an OCSP_SINGLERESP structure. Note: the
  215. * revtime and reason values are only set if the certificate status is
  216. * revoked. Returns numerical value of status.
  217. */
  218. int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
  219. ASN1_GENERALIZEDTIME **revtime,
  220. ASN1_GENERALIZEDTIME **thisupd,
  221. ASN1_GENERALIZEDTIME **nextupd)
  222. {
  223. int ret;
  224. OCSP_CERTSTATUS *cst;
  225. if (single == NULL)
  226. return -1;
  227. cst = single->certStatus;
  228. ret = cst->type;
  229. if (ret == V_OCSP_CERTSTATUS_REVOKED) {
  230. OCSP_REVOKEDINFO *rev = cst->value.revoked;
  231. if (revtime)
  232. *revtime = rev->revocationTime;
  233. if (reason) {
  234. if (rev->revocationReason)
  235. *reason = ASN1_ENUMERATED_get(rev->revocationReason);
  236. else
  237. *reason = -1;
  238. }
  239. }
  240. if (thisupd != NULL)
  241. *thisupd = single->thisUpdate;
  242. if (nextupd != NULL)
  243. *nextupd = single->nextUpdate;
  244. return ret;
  245. }
  246. /*
  247. * This function combines the previous ones: look up a certificate ID and if
  248. * found extract status information. Return 0 is successful.
  249. */
  250. int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
  251. int *reason,
  252. ASN1_GENERALIZEDTIME **revtime,
  253. ASN1_GENERALIZEDTIME **thisupd,
  254. ASN1_GENERALIZEDTIME **nextupd)
  255. {
  256. int i = OCSP_resp_find(bs, id, -1);
  257. OCSP_SINGLERESP *single;
  258. /* Maybe check for multiple responses and give an error? */
  259. if (i < 0)
  260. return 0;
  261. single = OCSP_resp_get0(bs, i);
  262. i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
  263. if (status != NULL)
  264. *status = i;
  265. return 1;
  266. }
  267. /*
  268. * Check validity of thisUpdate and nextUpdate fields. It is possible that
  269. * the request will take a few seconds to process and/or the time won't be
  270. * totally accurate. Therefore to avoid rejecting otherwise valid time we
  271. * allow the times to be within 'nsec' of the current time. Also to avoid
  272. * accepting very old responses without a nextUpdate field an optional maxage
  273. * parameter specifies the maximum age the thisUpdate field can be.
  274. */
  275. int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
  276. ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
  277. {
  278. int ret = 1;
  279. time_t t_now, t_tmp;
  280. time(&t_now);
  281. /* Check thisUpdate is valid and not more than nsec in the future */
  282. if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
  283. ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
  284. ret = 0;
  285. } else {
  286. t_tmp = t_now + nsec;
  287. if (X509_cmp_time(thisupd, &t_tmp) > 0) {
  288. ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_NOT_YET_VALID);
  289. ret = 0;
  290. }
  291. /*
  292. * If maxsec specified check thisUpdate is not more than maxsec in
  293. * the past
  294. */
  295. if (maxsec >= 0) {
  296. t_tmp = t_now - maxsec;
  297. if (X509_cmp_time(thisupd, &t_tmp) < 0) {
  298. ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_TOO_OLD);
  299. ret = 0;
  300. }
  301. }
  302. }
  303. if (nextupd == NULL)
  304. return ret;
  305. /* Check nextUpdate is valid and not more than nsec in the past */
  306. if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
  307. ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
  308. ret = 0;
  309. } else {
  310. t_tmp = t_now - nsec;
  311. if (X509_cmp_time(nextupd, &t_tmp) < 0) {
  312. ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_EXPIRED);
  313. ret = 0;
  314. }
  315. }
  316. /* Also don't allow nextUpdate to precede thisUpdate */
  317. if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
  318. ERR_raise(ERR_LIB_OCSP, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
  319. ret = 0;
  320. }
  321. return ret;
  322. }
  323. const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
  324. {
  325. return single->certId;
  326. }