ocsp_ext.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* ocsp_ext.c */
  2. /*
  3. * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
  4. * project.
  5. */
  6. /*
  7. * History: This file was transfered to Richard Levitte from CertCo by Kathy
  8. * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
  9. * patch kit.
  10. */
  11. /* ====================================================================
  12. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * openssl-core@openssl.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include <stdio.h>
  65. #include <cryptlib.h>
  66. #include <openssl/objects.h>
  67. #include <openssl/x509.h>
  68. #include <openssl/ocsp.h>
  69. #include <openssl/rand.h>
  70. #include <openssl/x509v3.h>
  71. /* Standard wrapper functions for extensions */
  72. /* OCSP request extensions */
  73. int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
  74. {
  75. return (X509v3_get_ext_count(x->tbsRequest->requestExtensions));
  76. }
  77. int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
  78. {
  79. return (X509v3_get_ext_by_NID
  80. (x->tbsRequest->requestExtensions, nid, lastpos));
  81. }
  82. int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, ASN1_OBJECT *obj,
  83. int lastpos)
  84. {
  85. return (X509v3_get_ext_by_OBJ
  86. (x->tbsRequest->requestExtensions, obj, lastpos));
  87. }
  88. int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
  89. {
  90. return (X509v3_get_ext_by_critical
  91. (x->tbsRequest->requestExtensions, crit, lastpos));
  92. }
  93. X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
  94. {
  95. return (X509v3_get_ext(x->tbsRequest->requestExtensions, loc));
  96. }
  97. X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
  98. {
  99. return (X509v3_delete_ext(x->tbsRequest->requestExtensions, loc));
  100. }
  101. void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
  102. {
  103. return X509V3_get_d2i(x->tbsRequest->requestExtensions, nid, crit, idx);
  104. }
  105. int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
  106. unsigned long flags)
  107. {
  108. return X509V3_add1_i2d(&x->tbsRequest->requestExtensions, nid, value,
  109. crit, flags);
  110. }
  111. int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
  112. {
  113. return (X509v3_add_ext(&(x->tbsRequest->requestExtensions), ex, loc) !=
  114. NULL);
  115. }
  116. /* Single extensions */
  117. int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
  118. {
  119. return (X509v3_get_ext_count(x->singleRequestExtensions));
  120. }
  121. int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
  122. {
  123. return (X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos));
  124. }
  125. int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, ASN1_OBJECT *obj, int lastpos)
  126. {
  127. return (X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos));
  128. }
  129. int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
  130. {
  131. return (X509v3_get_ext_by_critical
  132. (x->singleRequestExtensions, crit, lastpos));
  133. }
  134. X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
  135. {
  136. return (X509v3_get_ext(x->singleRequestExtensions, loc));
  137. }
  138. X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
  139. {
  140. return (X509v3_delete_ext(x->singleRequestExtensions, loc));
  141. }
  142. void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
  143. {
  144. return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
  145. }
  146. int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
  147. unsigned long flags)
  148. {
  149. return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
  150. flags);
  151. }
  152. int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
  153. {
  154. return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
  155. }
  156. /* OCSP Basic response */
  157. int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
  158. {
  159. return (X509v3_get_ext_count(x->tbsResponseData->responseExtensions));
  160. }
  161. int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
  162. {
  163. return (X509v3_get_ext_by_NID
  164. (x->tbsResponseData->responseExtensions, nid, lastpos));
  165. }
  166. int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, ASN1_OBJECT *obj,
  167. int lastpos)
  168. {
  169. return (X509v3_get_ext_by_OBJ
  170. (x->tbsResponseData->responseExtensions, obj, lastpos));
  171. }
  172. int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
  173. int lastpos)
  174. {
  175. return (X509v3_get_ext_by_critical
  176. (x->tbsResponseData->responseExtensions, crit, lastpos));
  177. }
  178. X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
  179. {
  180. return (X509v3_get_ext(x->tbsResponseData->responseExtensions, loc));
  181. }
  182. X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
  183. {
  184. return (X509v3_delete_ext(x->tbsResponseData->responseExtensions, loc));
  185. }
  186. void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
  187. int *idx)
  188. {
  189. return X509V3_get_d2i(x->tbsResponseData->responseExtensions, nid, crit,
  190. idx);
  191. }
  192. int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
  193. int crit, unsigned long flags)
  194. {
  195. return X509V3_add1_i2d(&x->tbsResponseData->responseExtensions, nid,
  196. value, crit, flags);
  197. }
  198. int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
  199. {
  200. return (X509v3_add_ext(&(x->tbsResponseData->responseExtensions), ex, loc)
  201. != NULL);
  202. }
  203. /* OCSP single response extensions */
  204. int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
  205. {
  206. return (X509v3_get_ext_count(x->singleExtensions));
  207. }
  208. int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
  209. {
  210. return (X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos));
  211. }
  212. int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, ASN1_OBJECT *obj,
  213. int lastpos)
  214. {
  215. return (X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos));
  216. }
  217. int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
  218. int lastpos)
  219. {
  220. return (X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos));
  221. }
  222. X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
  223. {
  224. return (X509v3_get_ext(x->singleExtensions, loc));
  225. }
  226. X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
  227. {
  228. return (X509v3_delete_ext(x->singleExtensions, loc));
  229. }
  230. void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
  231. int *idx)
  232. {
  233. return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
  234. }
  235. int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
  236. int crit, unsigned long flags)
  237. {
  238. return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
  239. }
  240. int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
  241. {
  242. return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
  243. }
  244. /* also CRL Entry Extensions */
  245. ASN1_STRING *ASN1_STRING_encode(ASN1_STRING *s, i2d_of_void *i2d,
  246. void *data, STACK_OF(ASN1_OBJECT) *sk)
  247. {
  248. int i;
  249. unsigned char *p, *b = NULL;
  250. if (data) {
  251. if ((i = i2d(data, NULL)) <= 0)
  252. goto err;
  253. if (!(b = p = OPENSSL_malloc((unsigned int)i)))
  254. goto err;
  255. if (i2d(data, &p) <= 0)
  256. goto err;
  257. } else if (sk) {
  258. if ((i = i2d_ASN1_SET_OF_ASN1_OBJECT(sk, NULL,
  259. (I2D_OF(ASN1_OBJECT)) i2d,
  260. V_ASN1_SEQUENCE,
  261. V_ASN1_UNIVERSAL,
  262. IS_SEQUENCE)) <= 0)
  263. goto err;
  264. if (!(b = p = OPENSSL_malloc((unsigned int)i)))
  265. goto err;
  266. if (i2d_ASN1_SET_OF_ASN1_OBJECT(sk, &p, (I2D_OF(ASN1_OBJECT)) i2d,
  267. V_ASN1_SEQUENCE,
  268. V_ASN1_UNIVERSAL, IS_SEQUENCE) <= 0)
  269. goto err;
  270. } else {
  271. OCSPerr(OCSP_F_ASN1_STRING_ENCODE, OCSP_R_BAD_DATA);
  272. goto err;
  273. }
  274. if (!s && !(s = ASN1_STRING_new()))
  275. goto err;
  276. if (!(ASN1_STRING_set(s, b, i)))
  277. goto err;
  278. OPENSSL_free(b);
  279. return s;
  280. err:
  281. if (b)
  282. OPENSSL_free(b);
  283. return NULL;
  284. }
  285. /* Nonce handling functions */
  286. /*
  287. * Add a nonce to an extension stack. A nonce can be specificed or if NULL a
  288. * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
  289. * OCTET STRING containing the nonce, previous versions used the raw nonce.
  290. */
  291. static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
  292. unsigned char *val, int len)
  293. {
  294. unsigned char *tmpval;
  295. ASN1_OCTET_STRING os;
  296. int ret = 0;
  297. if (len <= 0)
  298. len = OCSP_DEFAULT_NONCE_LENGTH;
  299. /*
  300. * Create the OCTET STRING manually by writing out the header and
  301. * appending the content octets. This avoids an extra memory allocation
  302. * operation in some cases. Applications should *NOT* do this because it
  303. * relies on library internals.
  304. */
  305. os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
  306. os.data = OPENSSL_malloc(os.length);
  307. if (os.data == NULL)
  308. goto err;
  309. tmpval = os.data;
  310. ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
  311. if (val)
  312. memcpy(tmpval, val, len);
  313. else
  314. RAND_pseudo_bytes(tmpval, len);
  315. if (!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
  316. &os, 0, X509V3_ADD_REPLACE))
  317. goto err;
  318. ret = 1;
  319. err:
  320. if (os.data)
  321. OPENSSL_free(os.data);
  322. return ret;
  323. }
  324. /* Add nonce to an OCSP request */
  325. int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
  326. {
  327. return ocsp_add1_nonce(&req->tbsRequest->requestExtensions, val, len);
  328. }
  329. /* Same as above but for a response */
  330. int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
  331. {
  332. return ocsp_add1_nonce(&resp->tbsResponseData->responseExtensions, val,
  333. len);
  334. }
  335. /*-
  336. * Check nonce validity in a request and response.
  337. * Return value reflects result:
  338. * 1: nonces present and equal.
  339. * 2: nonces both absent.
  340. * 3: nonce present in response only.
  341. * 0: nonces both present and not equal.
  342. * -1: nonce in request only.
  343. *
  344. * For most responders clients can check return > 0.
  345. * If responder doesn't handle nonces return != 0 may be
  346. * necessary. return == 0 is always an error.
  347. */
  348. int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
  349. {
  350. /*
  351. * Since we are only interested in the presence or absence of
  352. * the nonce and comparing its value there is no need to use
  353. * the X509V3 routines: this way we can avoid them allocating an
  354. * ASN1_OCTET_STRING structure for the value which would be
  355. * freed immediately anyway.
  356. */
  357. int req_idx, resp_idx;
  358. X509_EXTENSION *req_ext, *resp_ext;
  359. req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
  360. resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
  361. /* Check both absent */
  362. if ((req_idx < 0) && (resp_idx < 0))
  363. return 2;
  364. /* Check in request only */
  365. if ((req_idx >= 0) && (resp_idx < 0))
  366. return -1;
  367. /* Check in response but not request */
  368. if ((req_idx < 0) && (resp_idx >= 0))
  369. return 3;
  370. /*
  371. * Otherwise nonce in request and response so retrieve the extensions
  372. */
  373. req_ext = OCSP_REQUEST_get_ext(req, req_idx);
  374. resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
  375. if (ASN1_OCTET_STRING_cmp(req_ext->value, resp_ext->value))
  376. return 0;
  377. return 1;
  378. }
  379. /*
  380. * Copy the nonce value (if any) from an OCSP request to a response.
  381. */
  382. int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
  383. {
  384. X509_EXTENSION *req_ext;
  385. int req_idx;
  386. /* Check for nonce in request */
  387. req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
  388. /* If no nonce that's OK */
  389. if (req_idx < 0)
  390. return 2;
  391. req_ext = OCSP_REQUEST_get_ext(req, req_idx);
  392. return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
  393. }
  394. X509_EXTENSION *OCSP_crlID_new(char *url, long *n, char *tim)
  395. {
  396. X509_EXTENSION *x = NULL;
  397. OCSP_CRLID *cid = NULL;
  398. if (!(cid = OCSP_CRLID_new()))
  399. goto err;
  400. if (url) {
  401. if (!(cid->crlUrl = ASN1_IA5STRING_new()))
  402. goto err;
  403. if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
  404. goto err;
  405. }
  406. if (n) {
  407. if (!(cid->crlNum = ASN1_INTEGER_new()))
  408. goto err;
  409. if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
  410. goto err;
  411. }
  412. if (tim) {
  413. if (!(cid->crlTime = ASN1_GENERALIZEDTIME_new()))
  414. goto err;
  415. if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
  416. goto err;
  417. }
  418. if (!(x = X509_EXTENSION_new()))
  419. goto err;
  420. if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_CrlID)))
  421. goto err;
  422. if (!(ASN1_STRING_encode_of(OCSP_CRLID, x->value, i2d_OCSP_CRLID, cid,
  423. NULL)))
  424. goto err;
  425. OCSP_CRLID_free(cid);
  426. return x;
  427. err:
  428. if (x)
  429. X509_EXTENSION_free(x);
  430. if (cid)
  431. OCSP_CRLID_free(cid);
  432. return NULL;
  433. }
  434. /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
  435. X509_EXTENSION *OCSP_accept_responses_new(char **oids)
  436. {
  437. int nid;
  438. STACK_OF(ASN1_OBJECT) *sk = NULL;
  439. ASN1_OBJECT *o = NULL;
  440. X509_EXTENSION *x = NULL;
  441. if (!(sk = sk_ASN1_OBJECT_new_null()))
  442. goto err;
  443. while (oids && *oids) {
  444. if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
  445. sk_ASN1_OBJECT_push(sk, o);
  446. oids++;
  447. }
  448. if (!(x = X509_EXTENSION_new()))
  449. goto err;
  450. if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_acceptableResponses)))
  451. goto err;
  452. if (!(ASN1_STRING_encode_of(ASN1_OBJECT, x->value, i2d_ASN1_OBJECT, NULL,
  453. sk)))
  454. goto err;
  455. sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
  456. return x;
  457. err:
  458. if (x)
  459. X509_EXTENSION_free(x);
  460. if (sk)
  461. sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
  462. return NULL;
  463. }
  464. /* ArchiveCutoff ::= GeneralizedTime */
  465. X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
  466. {
  467. X509_EXTENSION *x = NULL;
  468. ASN1_GENERALIZEDTIME *gt = NULL;
  469. if (!(gt = ASN1_GENERALIZEDTIME_new()))
  470. goto err;
  471. if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
  472. goto err;
  473. if (!(x = X509_EXTENSION_new()))
  474. goto err;
  475. if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_archiveCutoff)))
  476. goto err;
  477. if (!(ASN1_STRING_encode_of(ASN1_GENERALIZEDTIME, x->value,
  478. i2d_ASN1_GENERALIZEDTIME, gt, NULL)))
  479. goto err;
  480. ASN1_GENERALIZEDTIME_free(gt);
  481. return x;
  482. err:
  483. if (gt)
  484. ASN1_GENERALIZEDTIME_free(gt);
  485. if (x)
  486. X509_EXTENSION_free(x);
  487. return NULL;
  488. }
  489. /*
  490. * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
  491. * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This method
  492. * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
  493. */
  494. X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME *issuer, char **urls)
  495. {
  496. X509_EXTENSION *x = NULL;
  497. ASN1_IA5STRING *ia5 = NULL;
  498. OCSP_SERVICELOC *sloc = NULL;
  499. ACCESS_DESCRIPTION *ad = NULL;
  500. if (!(sloc = OCSP_SERVICELOC_new()))
  501. goto err;
  502. if (!(sloc->issuer = X509_NAME_dup(issuer)))
  503. goto err;
  504. if (urls && *urls && !(sloc->locator = sk_ACCESS_DESCRIPTION_new_null()))
  505. goto err;
  506. while (urls && *urls) {
  507. if (!(ad = ACCESS_DESCRIPTION_new()))
  508. goto err;
  509. if (!(ad->method = OBJ_nid2obj(NID_ad_OCSP)))
  510. goto err;
  511. if (!(ad->location = GENERAL_NAME_new()))
  512. goto err;
  513. if (!(ia5 = ASN1_IA5STRING_new()))
  514. goto err;
  515. if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
  516. goto err;
  517. ad->location->type = GEN_URI;
  518. ad->location->d.ia5 = ia5;
  519. if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
  520. goto err;
  521. urls++;
  522. }
  523. if (!(x = X509_EXTENSION_new()))
  524. goto err;
  525. if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_serviceLocator)))
  526. goto err;
  527. if (!(ASN1_STRING_encode_of(OCSP_SERVICELOC, x->value,
  528. i2d_OCSP_SERVICELOC, sloc, NULL)))
  529. goto err;
  530. OCSP_SERVICELOC_free(sloc);
  531. return x;
  532. err:
  533. if (x)
  534. X509_EXTENSION_free(x);
  535. if (sloc)
  536. OCSP_SERVICELOC_free(sloc);
  537. return NULL;
  538. }