ocsp_cl.c 11 KB

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