2
0

ocsp_cl.c 11 KB

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