No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

529 líneas
15 KiB

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