25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

461 satır
14 KiB

  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. /* extension creation utilities */
  58. #include <ctype.h>
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/conf.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/x509.h>
  66. #include <openssl/x509v3.h>
  67. #include "../internal.h"
  68. static int v3_check_critical(char **value);
  69. static int v3_check_generic(char **value);
  70. static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
  71. int crit, char *value);
  72. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  73. int crit, int type,
  74. X509V3_CTX *ctx);
  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. OPENSSL_PUT_ERROR(X509V3, 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. OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION_NAME);
  122. return NULL;
  123. }
  124. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  125. OPENSSL_PUT_ERROR(X509V3, 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. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_EXTENSION_STRING);
  136. ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
  137. value);
  138. return NULL;
  139. }
  140. ext_struc = method->v2i(method, ctx, nval);
  141. if (*value != '@')
  142. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  143. if (!ext_struc)
  144. return NULL;
  145. } else if (method->s2i) {
  146. if (!(ext_struc = method->s2i(method, ctx, value)))
  147. return NULL;
  148. } else if (method->r2i) {
  149. if (!ctx->db || !ctx->db_meth) {
  150. OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE);
  151. return NULL;
  152. }
  153. if (!(ext_struc = method->r2i(method, ctx, value)))
  154. return NULL;
  155. } else {
  156. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
  157. ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
  158. return NULL;
  159. }
  160. ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
  161. if (method->it)
  162. ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
  163. else
  164. method->ext_free(ext_struc);
  165. return ext;
  166. }
  167. static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
  168. int ext_nid, int crit, void *ext_struc)
  169. {
  170. unsigned char *ext_der;
  171. int ext_len;
  172. ASN1_OCTET_STRING *ext_oct;
  173. X509_EXTENSION *ext;
  174. /* Convert internal representation to DER */
  175. if (method->it) {
  176. ext_der = NULL;
  177. ext_len =
  178. ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
  179. if (ext_len < 0)
  180. goto merr;
  181. } else {
  182. unsigned char *p;
  183. ext_len = method->i2d(ext_struc, NULL);
  184. if (!(ext_der = OPENSSL_malloc(ext_len)))
  185. goto merr;
  186. p = ext_der;
  187. method->i2d(ext_struc, &p);
  188. }
  189. if (!(ext_oct = M_ASN1_OCTET_STRING_new()))
  190. goto merr;
  191. ext_oct->data = ext_der;
  192. ext_oct->length = ext_len;
  193. ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
  194. if (!ext)
  195. goto merr;
  196. M_ASN1_OCTET_STRING_free(ext_oct);
  197. return ext;
  198. merr:
  199. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  200. return NULL;
  201. }
  202. /* Given an internal structure, nid and critical flag create an extension */
  203. X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
  204. {
  205. const X509V3_EXT_METHOD *method;
  206. if (!(method = X509V3_EXT_get_nid(ext_nid))) {
  207. OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION);
  208. return NULL;
  209. }
  210. return do_ext_i2d(method, ext_nid, crit, ext_struc);
  211. }
  212. /* Check the extension string for critical flag */
  213. static int v3_check_critical(char **value)
  214. {
  215. char *p = *value;
  216. if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
  217. return 0;
  218. p += 9;
  219. while (isspace((unsigned char)*p))
  220. p++;
  221. *value = p;
  222. return 1;
  223. }
  224. /* Check extension string for generic extension and return the type */
  225. static int v3_check_generic(char **value)
  226. {
  227. int gen_type = 0;
  228. char *p = *value;
  229. if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) {
  230. p += 4;
  231. gen_type = 1;
  232. } else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) {
  233. p += 5;
  234. gen_type = 2;
  235. } else
  236. return 0;
  237. while (isspace((unsigned char)*p))
  238. p++;
  239. *value = p;
  240. return gen_type;
  241. }
  242. /* Create a generic extension: for now just handle DER type */
  243. static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
  244. int crit, int gen_type,
  245. X509V3_CTX *ctx)
  246. {
  247. unsigned char *ext_der = NULL;
  248. long ext_len = 0;
  249. ASN1_OBJECT *obj = NULL;
  250. ASN1_OCTET_STRING *oct = NULL;
  251. X509_EXTENSION *extension = NULL;
  252. if (!(obj = OBJ_txt2obj(ext, 0))) {
  253. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NAME_ERROR);
  254. ERR_add_error_data(2, "name=", ext);
  255. goto err;
  256. }
  257. if (gen_type == 1)
  258. ext_der = string_to_hex(value, &ext_len);
  259. else if (gen_type == 2)
  260. ext_der = generic_asn1(value, ctx, &ext_len);
  261. if (ext_der == NULL) {
  262. OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
  263. ERR_add_error_data(2, "value=", value);
  264. goto err;
  265. }
  266. if (!(oct = M_ASN1_OCTET_STRING_new())) {
  267. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  268. goto err;
  269. }
  270. oct->data = ext_der;
  271. oct->length = ext_len;
  272. ext_der = NULL;
  273. extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
  274. err:
  275. ASN1_OBJECT_free(obj);
  276. M_ASN1_OCTET_STRING_free(oct);
  277. if (ext_der)
  278. OPENSSL_free(ext_der);
  279. return extension;
  280. }
  281. static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
  282. long *ext_len)
  283. {
  284. ASN1_TYPE *typ;
  285. unsigned char *ext_der = NULL;
  286. typ = ASN1_generate_v3(value, ctx);
  287. if (typ == NULL)
  288. return NULL;
  289. *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
  290. ASN1_TYPE_free(typ);
  291. return ext_der;
  292. }
  293. /*
  294. * This is the main function: add a bunch of extensions based on a config
  295. * file section to an extension STACK.
  296. */
  297. int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
  298. STACK_OF(X509_EXTENSION) **sk)
  299. {
  300. X509_EXTENSION *ext;
  301. STACK_OF(CONF_VALUE) *nval;
  302. CONF_VALUE *val;
  303. size_t i;
  304. if (!(nval = NCONF_get_section(conf, section)))
  305. return 0;
  306. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  307. val = sk_CONF_VALUE_value(nval, i);
  308. if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
  309. return 0;
  310. if (sk)
  311. X509v3_add_ext(sk, ext, -1);
  312. X509_EXTENSION_free(ext);
  313. }
  314. return 1;
  315. }
  316. /*
  317. * Convenience functions to add extensions to a certificate, CRL and request
  318. */
  319. int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  320. X509 *cert)
  321. {
  322. STACK_OF(X509_EXTENSION) **sk = NULL;
  323. if (cert)
  324. sk = &cert->cert_info->extensions;
  325. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  326. }
  327. /* Same as above but for a CRL */
  328. int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  329. X509_CRL *crl)
  330. {
  331. STACK_OF(X509_EXTENSION) **sk = NULL;
  332. if (crl)
  333. sk = &crl->crl->extensions;
  334. return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  335. }
  336. /* Add extensions to certificate request */
  337. int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
  338. X509_REQ *req)
  339. {
  340. STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
  341. int i;
  342. if (req)
  343. sk = &extlist;
  344. i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
  345. if (!i || !sk)
  346. return i;
  347. i = X509_REQ_add_extensions(req, extlist);
  348. sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
  349. return i;
  350. }
  351. /* Config database functions */
  352. char *X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
  353. {
  354. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
  355. OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED);
  356. return NULL;
  357. }
  358. if (ctx->db_meth->get_string)
  359. return ctx->db_meth->get_string(ctx->db, name, section);
  360. return NULL;
  361. }
  362. STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, char *section)
  363. {
  364. if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
  365. OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED);
  366. return NULL;
  367. }
  368. if (ctx->db_meth->get_section)
  369. return ctx->db_meth->get_section(ctx->db, section);
  370. return NULL;
  371. }
  372. void X509V3_string_free(X509V3_CTX *ctx, char *str)
  373. {
  374. if (!str)
  375. return;
  376. if (ctx->db_meth->free_string)
  377. ctx->db_meth->free_string(ctx->db, str);
  378. }
  379. void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
  380. {
  381. if (!section)
  382. return;
  383. if (ctx->db_meth->free_section)
  384. ctx->db_meth->free_section(ctx->db, section);
  385. }
  386. static char *nconf_get_string(void *db, char *section, char *value)
  387. {
  388. /* TODO(fork): this should return a const value. */
  389. return (char *)NCONF_get_string(db, section, value);
  390. }
  391. static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
  392. {
  393. return NCONF_get_section(db, section);
  394. }
  395. static const X509V3_CONF_METHOD nconf_method = {
  396. nconf_get_string,
  397. nconf_get_section,
  398. NULL,
  399. NULL
  400. };
  401. void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
  402. {
  403. ctx->db_meth = &nconf_method;
  404. ctx->db = conf;
  405. }
  406. void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
  407. X509_CRL *crl, int flags)
  408. {
  409. ctx->issuer_cert = issuer;
  410. ctx->subject_cert = subj;
  411. ctx->crl = crl;
  412. ctx->subject_req = req;
  413. ctx->flags = flags;
  414. }