v3_conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 (nval == NULL || 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. if (*value != '@')
  140. sk_CONF_VALUE_free(nval);
  141. return NULL;
  142. }
  143. ext_struc = method->v2i(method, ctx, nval);
  144. if (*value != '@')
  145. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  146. if (!ext_struc)
  147. return NULL;
  148. } else if (method->s2i) {
  149. if (!(ext_struc = method->s2i(method, ctx, value)))
  150. return NULL;
  151. } else if (method->r2i) {
  152. if (!ctx->db || !ctx->db_meth) {
  153. X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
  154. return NULL;
  155. }
  156. if (!(ext_struc = method->r2i(method, ctx, value)))
  157. return NULL;
  158. } else {
  159. X509V3err(X509V3_F_DO_EXT_NCONF,
  160. X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
  161. ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
  162. return NULL;
  163. }
  164. ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
  165. if (method->it)
  166. ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
  167. else
  168. method->ext_free(ext_struc);
  169. return ext;
  170. }
  171. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  172. int ext_nid, int crit, void *ext_struc)
  173. {
  174. unsigned char *ext_der;
  175. int ext_len;
  176. ASN1_OCTET_STRING *ext_oct;
  177. X509_EXTENSION *ext;
  178. /* Convert internal representation to DER */
  179. if (method->it) {
  180. ext_der = NULL;
  181. ext_len =
  182. ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
  183. if (ext_len < 0)
  184. goto merr;
  185. } else {
  186. unsigned char *p;
  187. ext_len = method->i2d(ext_struc, NULL);
  188. if (!(ext_der = OPENSSL_malloc(ext_len)))
  189. goto merr;
  190. p = ext_der;
  191. method->i2d(ext_struc, &p);
  192. }
  193. if (!(ext_oct = M_ASN1_OCTET_STRING_new()))
  194. goto merr;
  195. ext_oct->data = ext_der;
  196. ext_oct->length = ext_len;
  197. ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
  198. if (!ext)
  199. goto merr;
  200. M_ASN1_OCTET_STRING_free(ext_oct);
  201. return ext;
  202. merr:
  203. X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
  204. return NULL;
  205. }
  206. /* Given an internal structure, nid and critical flag create an extension */
  207. X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
  208. {
  209. const X509V3_EXT_METHOD *method;
  210. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  211. X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
  212. return NULL;
  213. }
  214. return do_ext_i2d(method, ext_nid, crit, ext_struc);
  215. }
  216. /* Check the extension string for critical flag */
  217. static int v3_check_critical(char **value)
  218. {
  219. char *p = *value;
  220. if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
  221. return 0;
  222. p += 9;
  223. while (isspace((unsigned char)*p))
  224. p++;
  225. *value = p;
  226. return 1;
  227. }
  228. /* Check extension string for generic extension and return the type */
  229. static int v3_check_generic(char **value)
  230. {
  231. int gen_type = 0;
  232. char *p = *value;
  233. if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) {
  234. p += 4;
  235. gen_type = 1;
  236. } else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) {
  237. p += 5;
  238. gen_type = 2;
  239. } else
  240. return 0;
  241. while (isspace((unsigned char)*p))
  242. p++;
  243. *value = p;
  244. return gen_type;
  245. }
  246. /* Create a generic extension: for now just handle DER type */
  247. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  248. int crit, int gen_type,
  249. X509V3_CTX *ctx)
  250. {
  251. unsigned char *ext_der = NULL;
  252. long ext_len;
  253. ASN1_OBJECT *obj = NULL;
  254. ASN1_OCTET_STRING *oct = NULL;
  255. X509_EXTENSION *extension = NULL;
  256. if (!(obj = OBJ_txt2obj(ext, 0))) {
  257. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  258. X509V3_R_EXTENSION_NAME_ERROR);
  259. ERR_add_error_data(2, "name=", ext);
  260. goto err;
  261. }
  262. if (gen_type == 1)
  263. ext_der = string_to_hex(value, &ext_len);
  264. else if (gen_type == 2)
  265. ext_der = generic_asn1(value, ctx, &ext_len);
  266. if (ext_der == NULL) {
  267. X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
  268. X509V3_R_EXTENSION_VALUE_ERROR);
  269. ERR_add_error_data(2, "value=", value);
  270. goto err;
  271. }
  272. if (!(oct = M_ASN1_OCTET_STRING_new())) {
  273. X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
  274. goto err;
  275. }
  276. oct->data = ext_der;
  277. oct->length = ext_len;
  278. ext_der = NULL;
  279. extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
  280. err:
  281. ASN1_OBJECT_free(obj);
  282. M_ASN1_OCTET_STRING_free(oct);
  283. if (ext_der)
  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. /*
  300. * This is the main function: add a bunch of extensions based on a config
  301. * file section to an extension STACK.
  302. */
  303. int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
  304. STACK_OF(X509_EXTENSION) **sk)
  305. {
  306. X509_EXTENSION *ext;
  307. STACK_OF(CONF_VALUE) *nval;
  308. CONF_VALUE *val;
  309. int i;
  310. if (!(nval = NCONF_get_section(conf, section)))
  311. return 0;
  312. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  313. val = sk_CONF_VALUE_value(nval, i);
  314. if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
  315. return 0;
  316. if (sk)
  317. X509v3_add_ext(sk, ext, -1);
  318. X509_EXTENSION_free(ext);
  319. }
  320. return 1;
  321. }
  322. /*
  323. * Convenience functions to add extensions to a certificate, CRL and request
  324. */
  325. int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  326. X509 *cert)
  327. {
  328. STACK_OF(X509_EXTENSION) **sk = NULL;
  329. if (cert)
  330. sk = &cert->cert_info->extensions;
  331. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  332. }
  333. /* Same as above but for a CRL */
  334. int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  335. X509_CRL *crl)
  336. {
  337. STACK_OF(X509_EXTENSION) **sk = NULL;
  338. if (crl)
  339. sk = &crl->crl->extensions;
  340. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  341. }
  342. /* Add extensions to certificate request */
  343. int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  344. X509_REQ *req)
  345. {
  346. STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
  347. int i;
  348. if (req)
  349. sk = &extlist;
  350. i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  351. if (!i || !sk)
  352. return i;
  353. i = X509_REQ_add_extensions(req, extlist);
  354. sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
  355. return i;
  356. }
  357. /* Config database functions */
  358. char *X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
  359. {
  360. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
  361. X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
  362. return NULL;
  363. }
  364. if (ctx->db_meth->get_string)
  365. return ctx->db_meth->get_string(ctx->db, name, section);
  366. return NULL;
  367. }
  368. STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, char *section)
  369. {
  370. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
  371. X509V3err(X509V3_F_X509V3_GET_SECTION,
  372. X509V3_R_OPERATION_NOT_DEFINED);
  373. return NULL;
  374. }
  375. if (ctx->db_meth->get_section)
  376. return ctx->db_meth->get_section(ctx->db, section);
  377. return NULL;
  378. }
  379. void X509V3_string_free(X509V3_CTX *ctx, char *str)
  380. {
  381. if (!str)
  382. return;
  383. if (ctx->db_meth->free_string)
  384. ctx->db_meth->free_string(ctx->db, str);
  385. }
  386. void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
  387. {
  388. if (!section)
  389. return;
  390. if (ctx->db_meth->free_section)
  391. ctx->db_meth->free_section(ctx->db, section);
  392. }
  393. static char *nconf_get_string(void *db, char *section, char *value)
  394. {
  395. return NCONF_get_string(db, section, value);
  396. }
  397. static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
  398. {
  399. return NCONF_get_section(db, section);
  400. }
  401. static X509V3_CONF_METHOD nconf_method = {
  402. nconf_get_string,
  403. nconf_get_section,
  404. NULL,
  405. NULL
  406. };
  407. void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
  408. {
  409. ctx->db_meth = &nconf_method;
  410. ctx->db = conf;
  411. }
  412. void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
  413. X509_CRL *crl, int flags)
  414. {
  415. ctx->issuer_cert = issuer;
  416. ctx->subject_cert = subj;
  417. ctx->crl = crl;
  418. ctx->subject_req = req;
  419. ctx->flags = flags;
  420. }
  421. /* Old conf compatibility functions */
  422. X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  423. char *name, char *value)
  424. {
  425. CONF ctmp;
  426. CONF_set_nconf(&ctmp, conf);
  427. return X509V3_EXT_nconf(&ctmp, ctx, name, value);
  428. }
  429. /* LHASH *conf: Config file */
  430. /* char *value: Value */
  431. X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
  432. X509V3_CTX *ctx, int ext_nid, char *value)
  433. {
  434. CONF ctmp;
  435. CONF_set_nconf(&ctmp, conf);
  436. return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
  437. }
  438. static char *conf_lhash_get_string(void *db, char *section, char *value)
  439. {
  440. return CONF_get_string(db, section, value);
  441. }
  442. static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section)
  443. {
  444. return CONF_get_section(db, section);
  445. }
  446. static X509V3_CONF_METHOD conf_lhash_method = {
  447. conf_lhash_get_string,
  448. conf_lhash_get_section,
  449. NULL,
  450. NULL
  451. };
  452. void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
  453. {
  454. ctx->db_meth = &conf_lhash_method;
  455. ctx->db = lhash;
  456. }
  457. int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  458. char *section, X509 *cert)
  459. {
  460. CONF ctmp;
  461. CONF_set_nconf(&ctmp, conf);
  462. return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
  463. }
  464. /* Same as above but for a CRL */
  465. int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  466. char *section, X509_CRL *crl)
  467. {
  468. CONF ctmp;
  469. CONF_set_nconf(&ctmp, conf);
  470. return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
  471. }
  472. /* Add extensions to certificate request */
  473. int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
  474. char *section, X509_REQ *req)
  475. {
  476. CONF ctmp;
  477. CONF_set_nconf(&ctmp, conf);
  478. return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
  479. }