ocsp_ext.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Copyright 2000-2023 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 "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/ocsp.h>
  14. #include "ocsp_local.h"
  15. #include <openssl/rand.h>
  16. #include <openssl/x509v3.h>
  17. /* Standard wrapper functions for extensions */
  18. /* OCSP request extensions */
  19. int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
  20. {
  21. return X509v3_get_ext_count(x->tbsRequest.requestExtensions);
  22. }
  23. int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
  24. {
  25. return (X509v3_get_ext_by_NID
  26. (x->tbsRequest.requestExtensions, nid, lastpos));
  27. }
  28. int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
  29. int lastpos)
  30. {
  31. return (X509v3_get_ext_by_OBJ
  32. (x->tbsRequest.requestExtensions, obj, lastpos));
  33. }
  34. int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
  35. {
  36. return (X509v3_get_ext_by_critical
  37. (x->tbsRequest.requestExtensions, crit, lastpos));
  38. }
  39. X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
  40. {
  41. return X509v3_get_ext(x->tbsRequest.requestExtensions, loc);
  42. }
  43. X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
  44. {
  45. return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc);
  46. }
  47. void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
  48. {
  49. return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx);
  50. }
  51. int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
  52. unsigned long flags)
  53. {
  54. return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value,
  55. crit, flags);
  56. }
  57. int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
  58. {
  59. return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) !=
  60. NULL);
  61. }
  62. /* Single extensions */
  63. int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
  64. {
  65. return X509v3_get_ext_count(x->singleRequestExtensions);
  66. }
  67. int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
  68. {
  69. return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos);
  70. }
  71. int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj,
  72. int lastpos)
  73. {
  74. return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos);
  75. }
  76. int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
  77. {
  78. return (X509v3_get_ext_by_critical
  79. (x->singleRequestExtensions, crit, lastpos));
  80. }
  81. X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
  82. {
  83. return X509v3_get_ext(x->singleRequestExtensions, loc);
  84. }
  85. X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
  86. {
  87. return X509v3_delete_ext(x->singleRequestExtensions, loc);
  88. }
  89. void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
  90. {
  91. return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
  92. }
  93. int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
  94. unsigned long flags)
  95. {
  96. return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
  97. flags);
  98. }
  99. int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
  100. {
  101. return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
  102. }
  103. /* OCSP Basic response */
  104. int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
  105. {
  106. return X509v3_get_ext_count(x->tbsResponseData.responseExtensions);
  107. }
  108. int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
  109. {
  110. return (X509v3_get_ext_by_NID
  111. (x->tbsResponseData.responseExtensions, nid, lastpos));
  112. }
  113. int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
  114. int lastpos)
  115. {
  116. return (X509v3_get_ext_by_OBJ
  117. (x->tbsResponseData.responseExtensions, obj, lastpos));
  118. }
  119. int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
  120. int lastpos)
  121. {
  122. return (X509v3_get_ext_by_critical
  123. (x->tbsResponseData.responseExtensions, crit, lastpos));
  124. }
  125. X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
  126. {
  127. return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc);
  128. }
  129. X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
  130. {
  131. return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc);
  132. }
  133. void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
  134. int *idx)
  135. {
  136. return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit,
  137. idx);
  138. }
  139. int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
  140. int crit, unsigned long flags)
  141. {
  142. return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid,
  143. value, crit, flags);
  144. }
  145. int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
  146. {
  147. return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc)
  148. != NULL);
  149. }
  150. /* OCSP single response extensions */
  151. int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
  152. {
  153. return X509v3_get_ext_count(x->singleExtensions);
  154. }
  155. int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
  156. {
  157. return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos);
  158. }
  159. int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
  160. int lastpos)
  161. {
  162. return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos);
  163. }
  164. int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
  165. int lastpos)
  166. {
  167. return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos);
  168. }
  169. X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
  170. {
  171. return X509v3_get_ext(x->singleExtensions, loc);
  172. }
  173. X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
  174. {
  175. return X509v3_delete_ext(x->singleExtensions, loc);
  176. }
  177. void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
  178. int *idx)
  179. {
  180. return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
  181. }
  182. int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
  183. int crit, unsigned long flags)
  184. {
  185. return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
  186. }
  187. int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
  188. {
  189. return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
  190. }
  191. /* also CRL Entry Extensions */
  192. /* Nonce handling functions */
  193. /*
  194. * Add a nonce to an extension stack. A nonce can be specified or if NULL a
  195. * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
  196. * OCTET STRING containing the nonce, previous versions used the raw nonce.
  197. */
  198. static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
  199. unsigned char *val, int len)
  200. {
  201. unsigned char *tmpval;
  202. ASN1_OCTET_STRING os;
  203. int ret = 0;
  204. if (len <= 0)
  205. len = OCSP_DEFAULT_NONCE_LENGTH;
  206. /*
  207. * Create the OCTET STRING manually by writing out the header and
  208. * appending the content octets. This avoids an extra memory allocation
  209. * operation in some cases. Applications should *NOT* do this because it
  210. * relies on library internals.
  211. */
  212. os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
  213. if (os.length < 0)
  214. return 0;
  215. os.data = OPENSSL_malloc(os.length);
  216. if (os.data == NULL)
  217. goto err;
  218. tmpval = os.data;
  219. ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
  220. if (val)
  221. memcpy(tmpval, val, len);
  222. else if (RAND_bytes(tmpval, len) <= 0)
  223. goto err;
  224. if (X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
  225. &os, 0, X509V3_ADD_REPLACE) <= 0)
  226. goto err;
  227. ret = 1;
  228. err:
  229. OPENSSL_free(os.data);
  230. return ret;
  231. }
  232. /* Add nonce to an OCSP request */
  233. int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
  234. {
  235. return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len);
  236. }
  237. /* Same as above but for a response */
  238. int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
  239. {
  240. return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val,
  241. len);
  242. }
  243. /*-
  244. * Check nonce validity in a request and response.
  245. * Return value reflects result:
  246. * 1: nonces present and equal.
  247. * 2: nonces both absent.
  248. * 3: nonce present in response only.
  249. * 0: nonces both present and not equal.
  250. * -1: nonce in request only.
  251. *
  252. * For most responders clients can check return > 0.
  253. * If responder doesn't handle nonces return != 0 may be
  254. * necessary. return == 0 is always an error.
  255. */
  256. int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
  257. {
  258. /*
  259. * Since we are only interested in the presence or absence of
  260. * the nonce and comparing its value there is no need to use
  261. * the X509V3 routines: this way we can avoid them allocating an
  262. * ASN1_OCTET_STRING structure for the value which would be
  263. * freed immediately anyway.
  264. */
  265. int req_idx, resp_idx;
  266. X509_EXTENSION *req_ext, *resp_ext;
  267. req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
  268. resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
  269. /* Check both absent */
  270. if ((req_idx < 0) && (resp_idx < 0))
  271. return 2;
  272. /* Check in request only */
  273. if ((req_idx >= 0) && (resp_idx < 0))
  274. return -1;
  275. /* Check in response but not request */
  276. if ((req_idx < 0) && (resp_idx >= 0))
  277. return 3;
  278. /*
  279. * Otherwise nonce in request and response so retrieve the extensions
  280. */
  281. req_ext = OCSP_REQUEST_get_ext(req, req_idx);
  282. resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
  283. if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext),
  284. X509_EXTENSION_get_data(resp_ext)))
  285. return 0;
  286. return 1;
  287. }
  288. /*
  289. * Copy the nonce value (if any) from an OCSP request to a response.
  290. */
  291. int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
  292. {
  293. X509_EXTENSION *req_ext;
  294. int req_idx;
  295. /* Check for nonce in request */
  296. req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
  297. /* If no nonce that's OK */
  298. if (req_idx < 0)
  299. return 2;
  300. req_ext = OCSP_REQUEST_get_ext(req, req_idx);
  301. return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
  302. }
  303. X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim)
  304. {
  305. X509_EXTENSION *x = NULL;
  306. OCSP_CRLID *cid = NULL;
  307. if ((cid = OCSP_CRLID_new()) == NULL)
  308. goto err;
  309. if (url) {
  310. if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL)
  311. goto err;
  312. if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
  313. goto err;
  314. }
  315. if (n) {
  316. if ((cid->crlNum = ASN1_INTEGER_new()) == NULL)
  317. goto err;
  318. if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
  319. goto err;
  320. }
  321. if (tim) {
  322. if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL)
  323. goto err;
  324. if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
  325. goto err;
  326. }
  327. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
  328. err:
  329. OCSP_CRLID_free(cid);
  330. return x;
  331. }
  332. /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
  333. X509_EXTENSION *OCSP_accept_responses_new(char **oids)
  334. {
  335. int nid;
  336. STACK_OF(ASN1_OBJECT) *sk = NULL;
  337. ASN1_OBJECT *o = NULL;
  338. X509_EXTENSION *x = NULL;
  339. if ((sk = sk_ASN1_OBJECT_new_null()) == NULL)
  340. goto err;
  341. while (oids && *oids) {
  342. if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
  343. sk_ASN1_OBJECT_push(sk, o);
  344. oids++;
  345. }
  346. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
  347. err:
  348. sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
  349. return x;
  350. }
  351. /* ArchiveCutoff ::= GeneralizedTime */
  352. X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
  353. {
  354. X509_EXTENSION *x = NULL;
  355. ASN1_GENERALIZEDTIME *gt = NULL;
  356. if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
  357. goto err;
  358. if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
  359. goto err;
  360. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
  361. err:
  362. ASN1_GENERALIZEDTIME_free(gt);
  363. return x;
  364. }
  365. /*
  366. * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
  367. * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This method
  368. * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
  369. */
  370. X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls)
  371. {
  372. X509_EXTENSION *x = NULL;
  373. ASN1_IA5STRING *ia5 = NULL;
  374. OCSP_SERVICELOC *sloc = NULL;
  375. ACCESS_DESCRIPTION *ad = NULL;
  376. if ((sloc = OCSP_SERVICELOC_new()) == NULL)
  377. goto err;
  378. X509_NAME_free(sloc->issuer);
  379. if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL)
  380. goto err;
  381. if (urls && *urls
  382. && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL)
  383. goto err;
  384. while (urls && *urls) {
  385. if ((ad = ACCESS_DESCRIPTION_new()) == NULL)
  386. goto err;
  387. if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL)
  388. goto err;
  389. if ((ia5 = ASN1_IA5STRING_new()) == NULL)
  390. goto err;
  391. if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
  392. goto err;
  393. /* ad->location is allocated inside ACCESS_DESCRIPTION_new */
  394. ad->location->type = GEN_URI;
  395. ad->location->d.ia5 = ia5;
  396. ia5 = NULL;
  397. if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
  398. goto err;
  399. ad = NULL;
  400. urls++;
  401. }
  402. x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
  403. err:
  404. ASN1_IA5STRING_free(ia5);
  405. ACCESS_DESCRIPTION_free(ad);
  406. OCSP_SERVICELOC_free(sloc);
  407. return x;
  408. }