v3_conf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* v3_conf.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* extension creation utilities */
  60. #include <stdio.h>
  61. #include <ctype.h>
  62. #include "cryptlib.h"
  63. #include <openssl/conf.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/x509v3.h>
  66. static int v3_check_critical(char **value);
  67. static int v3_check_generic(char **value);
  68. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  69. int crit, char *value);
  70. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  71. int crit, int type,
  72. X509V3_CTX *ctx);
  73. static char *conf_lhash_get_string(void *db, char *section, char *value);
  74. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section);
  75. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  76. int ext_nid, int crit, void *ext_struc);
  77. static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
  78. long *ext_len);
  79. /* CONF *conf: Config file */
  80. /* char *name: Name */
  81. /* char *value: Value */
  82. X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, char *name,
  83. char *value)
  84. {
  85. int crit;
  86. int ext_type;
  87. X509_EXTENSION *ret;
  88. crit = v3_check_critical(&value);
  89. if ((ext_type = v3_check_generic(&value)))
  90. return v3_generic_extension(name, value, crit, ext_type, ctx);
  91. ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
  92. if (!ret) {
  93. X509V3err(X509V3_F_X509V3_EXT_NCONF, X509V3_R_ERROR_IN_EXTENSION);
  94. ERR_add_error_data(4, "name=", name, ", value=", value);
  95. }
  96. return ret;
  97. }
  98. /* CONF *conf: Config file */
  99. /* char *value: Value */
  100. X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  101. char *value)
  102. {
  103. int crit;
  104. int ext_type;
  105. crit = v3_check_critical(&value);
  106. if ((ext_type = v3_check_generic(&value)))
  107. return v3_generic_extension(OBJ_nid2sn(ext_nid),
  108. value, crit, ext_type, ctx);
  109. return do_ext_nconf(conf, ctx, ext_nid, crit, value);
  110. }
  111. /* CONF *conf: Config file */
  112. /* char *value: Value */
  113. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  114. int crit, char *value)
  115. {
  116. const X509V3_EXT_METHOD *method;
  117. X509_EXTENSION *ext;
  118. STACK_OF(CONF_VALUE) *nval;
  119. void *ext_struc;
  120. if (ext_nid == NID_undef) {
  121. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION_NAME);
  122. return NULL;
  123. }
  124. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  125. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION);
  126. return NULL;
  127. }
  128. /* Now get internal extension representation based on type */
  129. if (method->v2i) {
  130. if (*value == '@')
  131. nval = NCONF_get_section(conf, value + 1);
  132. else
  133. nval = X509V3_parse_list(value);
  134. if (sk_CONF_VALUE_num(nval) <= 0) {
  135. X509V3err(X509V3_F_DO_EXT_NCONF,
  136. X509V3_R_INVALID_EXTENSION_STRING);
  137. ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
  138. value);
  139. return NULL;
  140. }
  141. ext_struc = method->v2i(method, ctx, nval);
  142. if (*value != '@')
  143. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  144. if (!ext_struc)
  145. return NULL;
  146. } else if (method->s2i) {
  147. if (!(ext_struc = method->s2i(method, ctx, value)))
  148. return NULL;
  149. } else if (method->r2i) {
  150. if (!ctx->db || !ctx->db_meth) {
  151. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
  152. return NULL;
  153. }
  154. if (!(ext_struc = method->r2i(method, ctx, value)))
  155. return NULL;
  156. } else {
  157. X509V3err(X509V3_F_DO_EXT_NCONF,
  158. X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
  159. ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
  160. return NULL;
  161. }
  162. ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
  163. if (method->it)
  164. ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
  165. else
  166. method->ext_free(ext_struc);
  167. return ext;
  168. }
  169. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  170. int ext_nid, int crit, void *ext_struc)
  171. {
  172. unsigned char *ext_der = NULL;
  173. int ext_len;
  174. ASN1_OCTET_STRING *ext_oct = NULL;
  175. X509_EXTENSION *ext;
  176. /* Convert internal representation to DER */
  177. if (method->it) {
  178. ext_der = NULL;
  179. ext_len =
  180. ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
  181. if (ext_len < 0)
  182. goto merr;
  183. } else {
  184. unsigned char *p;
  185. ext_len = method->i2d(ext_struc, NULL);
  186. if (!(ext_der = OPENSSL_malloc(ext_len)))
  187. goto merr;
  188. p = ext_der;
  189. method->i2d(ext_struc, &p);
  190. }
  191. if (!(ext_oct = ASN1_OCTET_STRING_new()))
  192. goto merr;
  193. ext_oct->data = ext_der;
  194. ext_der = NULL;
  195. ext_oct->length = ext_len;
  196. ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
  197. if (!ext)
  198. goto merr;
  199. ASN1_OCTET_STRING_free(ext_oct);
  200. return ext;
  201. merr:
  202. X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
  203. OPENSSL_free(ext_der);
  204. ASN1_OCTET_STRING_free(ext_oct);
  205. return NULL;
  206. }
  207. /* Given an internal structure, nid and critical flag create an extension */
  208. X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
  209. {
  210. const X509V3_EXT_METHOD *method;
  211. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  212. X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
  213. return NULL;
  214. }
  215. return do_ext_i2d(method, ext_nid, crit, ext_struc);
  216. }
  217. /* Check the extension string for critical flag */
  218. static int v3_check_critical(char **value)
  219. {
  220. char *p = *value;
  221. if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
  222. return 0;
  223. p += 9;
  224. while (isspace((unsigned char)*p))
  225. p++;
  226. *value = p;
  227. return 1;
  228. }
  229. /* Check extension string for generic extension and return the type */
  230. static int v3_check_generic(char **value)
  231. {
  232. int gen_type = 0;
  233. char *p = *value;
  234. if ((strlen(p) >= 4) && strncmp(p, "DER:", 4) == 0) {
  235. p += 4;
  236. gen_type = 1;
  237. } else if ((strlen(p) >= 5) && strncmp(p, "ASN1:", 5) == 0) {
  238. p += 5;
  239. gen_type = 2;
  240. } else
  241. return 0;
  242. while (isspace((unsigned char)*p))
  243. p++;
  244. *value = p;
  245. return gen_type;
  246. }
  247. /* Create a generic extension: for now just handle DER type */
  248. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  249. int crit, int gen_type,
  250. X509V3_CTX *ctx)
  251. {
  252. unsigned char *ext_der = NULL;
  253. long ext_len = 0;
  254. ASN1_OBJECT *obj = NULL;
  255. ASN1_OCTET_STRING *oct = NULL;
  256. X509_EXTENSION *extension = NULL;
  257. if (!(obj = OBJ_txt2obj(ext, 0))) {
  258. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  259. X509V3_R_EXTENSION_NAME_ERROR);
  260. ERR_add_error_data(2, "name=", ext);
  261. goto err;
  262. }
  263. if (gen_type == 1)
  264. ext_der = string_to_hex(value, &ext_len);
  265. else if (gen_type == 2)
  266. ext_der = generic_asn1(value, ctx, &ext_len);
  267. if (ext_der == NULL) {
  268. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  269. X509V3_R_EXTENSION_VALUE_ERROR);
  270. ERR_add_error_data(2, "value=", value);
  271. goto err;
  272. }
  273. if (!(oct = ASN1_OCTET_STRING_new())) {
  274. X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
  275. goto err;
  276. }
  277. oct->data = ext_der;
  278. oct->length = ext_len;
  279. ext_der = NULL;
  280. extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
  281. err:
  282. ASN1_OBJECT_free(obj);
  283. ASN1_OCTET_STRING_free(oct);
  284. OPENSSL_free(ext_der);
  285. return extension;
  286. }
  287. static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
  288. long *ext_len)
  289. {
  290. ASN1_TYPE *typ;
  291. unsigned char *ext_der = NULL;
  292. typ = ASN1_generate_v3(value, ctx);
  293. if (typ == NULL)
  294. return NULL;
  295. *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
  296. ASN1_TYPE_free(typ);
  297. return ext_der;
  298. }
  299. static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext)
  300. {
  301. int idx;
  302. ASN1_OBJECT *obj;
  303. obj = X509_EXTENSION_get_object(dext);
  304. while ((idx = X509v3_get_ext_by_OBJ(sk, obj, -1)) >= 0) {
  305. X509_EXTENSION *tmpext = X509v3_get_ext(sk, idx);
  306. X509v3_delete_ext(sk, idx);
  307. X509_EXTENSION_free(tmpext);
  308. }
  309. }
  310. /*
  311. * This is the main function: add a bunch of extensions based on a config
  312. * file section to an extension STACK.
  313. */
  314. int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
  315. STACK_OF(X509_EXTENSION) **sk)
  316. {
  317. X509_EXTENSION *ext;
  318. STACK_OF(CONF_VALUE) *nval;
  319. CONF_VALUE *val;
  320. int i;
  321. if (!(nval = NCONF_get_section(conf, section)))
  322. return 0;
  323. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  324. val = sk_CONF_VALUE_value(nval, i);
  325. if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
  326. return 0;
  327. if (ctx->flags == X509V3_CTX_REPLACE)
  328. delete_ext(*sk, ext);
  329. if (sk)
  330. X509v3_add_ext(sk, ext, -1);
  331. X509_EXTENSION_free(ext);
  332. }
  333. return 1;
  334. }
  335. /*
  336. * Convenience functions to add extensions to a certificate, CRL and request
  337. */
  338. int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  339. X509 *cert)
  340. {
  341. STACK_OF(X509_EXTENSION) **sk = NULL;
  342. if (cert)
  343. sk = &cert->cert_info->extensions;
  344. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  345. }
  346. /* Same as above but for a CRL */
  347. int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  348. X509_CRL *crl)
  349. {
  350. STACK_OF(X509_EXTENSION) **sk = NULL;
  351. if (crl)
  352. sk = &crl->crl->extensions;
  353. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  354. }
  355. /* Add extensions to certificate request */
  356. int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  357. X509_REQ *req)
  358. {
  359. STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
  360. int i;
  361. if (req)
  362. sk = &extlist;
  363. i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  364. if (!i || !sk)
  365. return i;
  366. i = X509_REQ_add_extensions(req, extlist);
  367. sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
  368. return i;
  369. }
  370. /* Config database functions */
  371. char *X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
  372. {
  373. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
  374. X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
  375. return NULL;
  376. }
  377. if (ctx->db_meth->get_string)
  378. return ctx->db_meth->get_string(ctx->db, name, section);
  379. return NULL;
  380. }
  381. STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, char *section)
  382. {
  383. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
  384. X509V3err(X509V3_F_X509V3_GET_SECTION,
  385. X509V3_R_OPERATION_NOT_DEFINED);
  386. return NULL;
  387. }
  388. if (ctx->db_meth->get_section)
  389. return ctx->db_meth->get_section(ctx->db, section);
  390. return NULL;
  391. }
  392. void X509V3_string_free(X509V3_CTX *ctx, char *str)
  393. {
  394. if (!str)
  395. return;
  396. if (ctx->db_meth->free_string)
  397. ctx->db_meth->free_string(ctx->db, str);
  398. }
  399. void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
  400. {
  401. if (!section)
  402. return;
  403. if (ctx->db_meth->free_section)
  404. ctx->db_meth->free_section(ctx->db, section);
  405. }
  406. static char *nconf_get_string(void *db, char *section, char *value)
  407. {
  408. return NCONF_get_string(db, section, value);
  409. }
  410. static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
  411. {
  412. return NCONF_get_section(db, section);
  413. }
  414. static X509V3_CONF_METHOD nconf_method = {
  415. nconf_get_string,
  416. nconf_get_section,
  417. NULL,
  418. NULL
  419. };
  420. void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
  421. {
  422. ctx->db_meth = &nconf_method;
  423. ctx->db = conf;
  424. }
  425. void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
  426. X509_CRL *crl, int flags)
  427. {
  428. ctx->issuer_cert = issuer;
  429. ctx->subject_cert = subj;
  430. ctx->crl = crl;
  431. ctx->subject_req = req;
  432. ctx->flags = flags;
  433. }
  434. /* Old conf compatibility functions */
  435. X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  436. char *name, char *value)
  437. {
  438. CONF ctmp;
  439. CONF_set_nconf(&ctmp, conf);
  440. return X509V3_EXT_nconf(&ctmp, ctx, name, value);
  441. }
  442. /* LHASH *conf: Config file */
  443. /* char *value: Value */
  444. X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
  445. X509V3_CTX *ctx, int ext_nid, char *value)
  446. {
  447. CONF ctmp;
  448. CONF_set_nconf(&ctmp, conf);
  449. return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
  450. }
  451. static char *conf_lhash_get_string(void *db, char *section, char *value)
  452. {
  453. return CONF_get_string(db, section, value);
  454. }
  455. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section)
  456. {
  457. return CONF_get_section(db, section);
  458. }
  459. static X509V3_CONF_METHOD conf_lhash_method = {
  460. conf_lhash_get_string,
  461. conf_lhash_get_section,
  462. NULL,
  463. NULL
  464. };
  465. void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
  466. {
  467. ctx->db_meth = &conf_lhash_method;
  468. ctx->db = lhash;
  469. }
  470. int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  471. char *section, X509 *cert)
  472. {
  473. CONF ctmp;
  474. CONF_set_nconf(&ctmp, conf);
  475. return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
  476. }
  477. /* Same as above but for a CRL */
  478. int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  479. char *section, X509_CRL *crl)
  480. {
  481. CONF ctmp;
  482. CONF_set_nconf(&ctmp, conf);
  483. return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
  484. }
  485. /* Add extensions to certificate request */
  486. int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  487. char *section, X509_REQ *req)
  488. {
  489. CONF ctmp;
  490. CONF_set_nconf(&ctmp, conf);
  491. return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
  492. }