Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1347 rader
38 KiB

  1. /* v3_utl.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2003 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. /* X509 v3 extension utilities */
  60. #include <ctype.h>
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <openssl/bn.h>
  64. #include <openssl/buf.h>
  65. #include <openssl/conf.h>
  66. #include <openssl/err.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/obj.h>
  69. #include <openssl/x509v3.h>
  70. #include "../conf/internal.h"
  71. #include "../internal.h"
  72. #include "internal.h"
  73. static char *strip_spaces(char *name);
  74. static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b);
  75. static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
  76. GENERAL_NAMES *gens);
  77. static void str_free(OPENSSL_STRING str);
  78. static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
  79. static int ipv4_from_asc(unsigned char *v4, const char *in);
  80. static int ipv6_from_asc(unsigned char *v6, const char *in);
  81. static int ipv6_cb(const char *elem, int len, void *usr);
  82. static int ipv6_hex(unsigned char *out, const char *in, int inlen);
  83. /* Add a CONF_VALUE name value pair to stack */
  84. int X509V3_add_value(const char *name, const char *value,
  85. STACK_OF(CONF_VALUE) **extlist)
  86. {
  87. CONF_VALUE *vtmp = NULL;
  88. char *tname = NULL, *tvalue = NULL;
  89. if (name && !(tname = BUF_strdup(name)))
  90. goto err;
  91. if (value && !(tvalue = BUF_strdup(value)))
  92. goto err;
  93. if (!(vtmp = CONF_VALUE_new()))
  94. goto err;
  95. if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
  96. goto err;
  97. vtmp->section = NULL;
  98. vtmp->name = tname;
  99. vtmp->value = tvalue;
  100. if (!sk_CONF_VALUE_push(*extlist, vtmp))
  101. goto err;
  102. return 1;
  103. err:
  104. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  105. if (vtmp)
  106. OPENSSL_free(vtmp);
  107. if (tname)
  108. OPENSSL_free(tname);
  109. if (tvalue)
  110. OPENSSL_free(tvalue);
  111. return 0;
  112. }
  113. int X509V3_add_value_uchar(const char *name, const unsigned char *value,
  114. STACK_OF(CONF_VALUE) **extlist)
  115. {
  116. return X509V3_add_value(name, (const char *)value, extlist);
  117. }
  118. /* Free function for STACK_OF(CONF_VALUE) */
  119. void X509V3_conf_free(CONF_VALUE *conf)
  120. {
  121. if (!conf)
  122. return;
  123. if (conf->name)
  124. OPENSSL_free(conf->name);
  125. if (conf->value)
  126. OPENSSL_free(conf->value);
  127. if (conf->section)
  128. OPENSSL_free(conf->section);
  129. OPENSSL_free(conf);
  130. }
  131. int X509V3_add_value_bool(const char *name, int asn1_bool,
  132. STACK_OF(CONF_VALUE) **extlist)
  133. {
  134. if (asn1_bool)
  135. return X509V3_add_value(name, "TRUE", extlist);
  136. return X509V3_add_value(name, "FALSE", extlist);
  137. }
  138. int X509V3_add_value_bool_nf(char *name, int asn1_bool,
  139. STACK_OF(CONF_VALUE) **extlist)
  140. {
  141. if (asn1_bool)
  142. return X509V3_add_value(name, "TRUE", extlist);
  143. return 1;
  144. }
  145. static char *bignum_to_string(const BIGNUM *bn)
  146. {
  147. char *tmp, *ret;
  148. size_t len;
  149. /*
  150. * Display large numbers in hex and small numbers in decimal. Converting to
  151. * decimal takes quadratic time and is no more useful than hex for large
  152. * numbers.
  153. */
  154. if (BN_num_bits(bn) < 32) {
  155. return BN_bn2dec(bn);
  156. }
  157. tmp = BN_bn2hex(bn);
  158. if (tmp == NULL) {
  159. return NULL;
  160. }
  161. len = strlen(tmp) + 3;
  162. ret = OPENSSL_malloc(len);
  163. if (ret == NULL) {
  164. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  165. OPENSSL_free(tmp);
  166. return NULL;
  167. }
  168. /* Prepend "0x", but place it after the "-" if negative. */
  169. if (tmp[0] == '-') {
  170. BUF_strlcpy(ret, "-0x", len);
  171. BUF_strlcat(ret, tmp + 1, len);
  172. } else {
  173. BUF_strlcpy(ret, "0x", len);
  174. BUF_strlcat(ret, tmp, len);
  175. }
  176. OPENSSL_free(tmp);
  177. return ret;
  178. }
  179. char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
  180. {
  181. BIGNUM *bntmp = NULL;
  182. char *strtmp = NULL;
  183. if (!a)
  184. return NULL;
  185. if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
  186. !(strtmp = bignum_to_string(bntmp)))
  187. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  188. BN_free(bntmp);
  189. return strtmp;
  190. }
  191. char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
  192. {
  193. BIGNUM *bntmp = NULL;
  194. char *strtmp = NULL;
  195. if (!a)
  196. return NULL;
  197. if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
  198. !(strtmp = bignum_to_string(bntmp)))
  199. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  200. BN_free(bntmp);
  201. return strtmp;
  202. }
  203. ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
  204. {
  205. BIGNUM *bn = NULL;
  206. ASN1_INTEGER *aint;
  207. int isneg, ishex;
  208. int ret;
  209. if (!value) {
  210. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
  211. return 0;
  212. }
  213. bn = BN_new();
  214. if (value[0] == '-') {
  215. value++;
  216. isneg = 1;
  217. } else
  218. isneg = 0;
  219. if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
  220. value += 2;
  221. ishex = 1;
  222. } else
  223. ishex = 0;
  224. if (ishex)
  225. ret = BN_hex2bn(&bn, value);
  226. else
  227. ret = BN_dec2bn(&bn, value);
  228. if (!ret || value[ret]) {
  229. BN_free(bn);
  230. OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR);
  231. return 0;
  232. }
  233. if (isneg && BN_is_zero(bn))
  234. isneg = 0;
  235. aint = BN_to_ASN1_INTEGER(bn, NULL);
  236. BN_free(bn);
  237. if (!aint) {
  238. OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
  239. return 0;
  240. }
  241. if (isneg)
  242. aint->type |= V_ASN1_NEG;
  243. return aint;
  244. }
  245. int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
  246. STACK_OF(CONF_VALUE) **extlist)
  247. {
  248. char *strtmp;
  249. int ret;
  250. if (!aint)
  251. return 1;
  252. if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
  253. return 0;
  254. ret = X509V3_add_value(name, strtmp, extlist);
  255. OPENSSL_free(strtmp);
  256. return ret;
  257. }
  258. int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
  259. {
  260. char *btmp;
  261. if (!(btmp = value->value))
  262. goto err;
  263. if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
  264. || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
  265. || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
  266. *asn1_bool = 0xff;
  267. return 1;
  268. } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
  269. || !strcmp(btmp, "N") || !strcmp(btmp, "n")
  270. || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
  271. *asn1_bool = 0;
  272. return 1;
  273. }
  274. err:
  275. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING);
  276. X509V3_conf_err(value);
  277. return 0;
  278. }
  279. int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
  280. {
  281. ASN1_INTEGER *itmp;
  282. if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
  283. X509V3_conf_err(value);
  284. return 0;
  285. }
  286. *aint = itmp;
  287. return 1;
  288. }
  289. #define HDR_NAME 1
  290. #define HDR_VALUE 2
  291. /*
  292. * #define DEBUG
  293. */
  294. STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
  295. {
  296. char *p, *q, c;
  297. char *ntmp, *vtmp;
  298. STACK_OF(CONF_VALUE) *values = NULL;
  299. char *linebuf;
  300. int state;
  301. /* We are going to modify the line so copy it first */
  302. linebuf = BUF_strdup(line);
  303. if (linebuf == NULL) {
  304. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  305. goto err;
  306. }
  307. state = HDR_NAME;
  308. ntmp = NULL;
  309. /* Go through all characters */
  310. for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
  311. p++) {
  312. switch (state) {
  313. case HDR_NAME:
  314. if (c == ':') {
  315. state = HDR_VALUE;
  316. *p = 0;
  317. ntmp = strip_spaces(q);
  318. if (!ntmp) {
  319. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
  320. goto err;
  321. }
  322. q = p + 1;
  323. } else if (c == ',') {
  324. *p = 0;
  325. ntmp = strip_spaces(q);
  326. q = p + 1;
  327. #if 0
  328. printf("%s\n", ntmp);
  329. #endif
  330. if (!ntmp) {
  331. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
  332. goto err;
  333. }
  334. X509V3_add_value(ntmp, NULL, &values);
  335. }
  336. break;
  337. case HDR_VALUE:
  338. if (c == ',') {
  339. state = HDR_NAME;
  340. *p = 0;
  341. vtmp = strip_spaces(q);
  342. #if 0
  343. printf("%s\n", ntmp);
  344. #endif
  345. if (!vtmp) {
  346. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
  347. goto err;
  348. }
  349. X509V3_add_value(ntmp, vtmp, &values);
  350. ntmp = NULL;
  351. q = p + 1;
  352. }
  353. }
  354. }
  355. if (state == HDR_VALUE) {
  356. vtmp = strip_spaces(q);
  357. #if 0
  358. printf("%s=%s\n", ntmp, vtmp);
  359. #endif
  360. if (!vtmp) {
  361. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
  362. goto err;
  363. }
  364. X509V3_add_value(ntmp, vtmp, &values);
  365. } else {
  366. ntmp = strip_spaces(q);
  367. #if 0
  368. printf("%s\n", ntmp);
  369. #endif
  370. if (!ntmp) {
  371. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
  372. goto err;
  373. }
  374. X509V3_add_value(ntmp, NULL, &values);
  375. }
  376. OPENSSL_free(linebuf);
  377. return values;
  378. err:
  379. OPENSSL_free(linebuf);
  380. sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
  381. return NULL;
  382. }
  383. /* Delete leading and trailing spaces from a string */
  384. static char *strip_spaces(char *name)
  385. {
  386. char *p, *q;
  387. /* Skip over leading spaces */
  388. p = name;
  389. while (*p && isspace((unsigned char)*p))
  390. p++;
  391. if (!*p)
  392. return NULL;
  393. q = p + strlen(p) - 1;
  394. while ((q != p) && isspace((unsigned char)*q))
  395. q--;
  396. if (p != q)
  397. q[1] = 0;
  398. if (!*p)
  399. return NULL;
  400. return p;
  401. }
  402. /* hex string utilities */
  403. /*
  404. * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
  405. * hex representation @@@ (Contents of buffer are always kept in ASCII, also
  406. * on EBCDIC machines)
  407. */
  408. char *x509v3_bytes_to_hex(const unsigned char *buffer, long len)
  409. {
  410. char *tmp, *q;
  411. const unsigned char *p;
  412. int i;
  413. static const char hexdig[] = "0123456789ABCDEF";
  414. if (!buffer || !len)
  415. return NULL;
  416. if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
  417. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  418. return NULL;
  419. }
  420. q = tmp;
  421. for (i = 0, p = buffer; i < len; i++, p++) {
  422. *q++ = hexdig[(*p >> 4) & 0xf];
  423. *q++ = hexdig[*p & 0xf];
  424. *q++ = ':';
  425. }
  426. q[-1] = 0;
  427. return tmp;
  428. }
  429. unsigned char *x509v3_hex_to_bytes(const char *str, long *len)
  430. {
  431. unsigned char *hexbuf, *q;
  432. unsigned char ch, cl, *p;
  433. if (!str) {
  434. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT);
  435. return NULL;
  436. }
  437. if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
  438. goto err;
  439. for (p = (unsigned char *)str, q = hexbuf; *p;) {
  440. ch = *p++;
  441. if (ch == ':')
  442. continue;
  443. cl = *p++;
  444. if (!cl) {
  445. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS);
  446. OPENSSL_free(hexbuf);
  447. return NULL;
  448. }
  449. if ((ch >= '0') && (ch <= '9'))
  450. ch -= '0';
  451. else if ((ch >= 'a') && (ch <= 'f'))
  452. ch -= 'a' - 10;
  453. else if ((ch >= 'A') && (ch <= 'F'))
  454. ch -= 'A' - 10;
  455. else
  456. goto badhex;
  457. if ((cl >= '0') && (cl <= '9'))
  458. cl -= '0';
  459. else if ((cl >= 'a') && (cl <= 'f'))
  460. cl -= 'a' - 10;
  461. else if ((cl >= 'A') && (cl <= 'F'))
  462. cl -= 'A' - 10;
  463. else
  464. goto badhex;
  465. *q++ = (ch << 4) | cl;
  466. }
  467. if (len)
  468. *len = q - hexbuf;
  469. return hexbuf;
  470. err:
  471. if (hexbuf)
  472. OPENSSL_free(hexbuf);
  473. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  474. return NULL;
  475. badhex:
  476. OPENSSL_free(hexbuf);
  477. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT);
  478. return NULL;
  479. }
  480. int x509v3_name_cmp(const char *name, const char *cmp)
  481. {
  482. int len, ret;
  483. char c;
  484. len = strlen(cmp);
  485. if ((ret = strncmp(name, cmp, len)))
  486. return ret;
  487. c = name[len];
  488. if (!c || (c == '.'))
  489. return 0;
  490. return 1;
  491. }
  492. static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b)
  493. {
  494. return strcmp(*a, *b);
  495. }
  496. STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
  497. {
  498. GENERAL_NAMES *gens;
  499. STACK_OF(OPENSSL_STRING) *ret;
  500. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  501. ret = get_email(X509_get_subject_name(x), gens);
  502. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  503. return ret;
  504. }
  505. STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
  506. {
  507. AUTHORITY_INFO_ACCESS *info;
  508. STACK_OF(OPENSSL_STRING) *ret = NULL;
  509. size_t i;
  510. info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
  511. if (!info)
  512. return NULL;
  513. for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
  514. ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
  515. if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
  516. if (ad->location->type == GEN_URI) {
  517. if (!append_ia5
  518. (&ret, ad->location->d.uniformResourceIdentifier))
  519. break;
  520. }
  521. }
  522. }
  523. AUTHORITY_INFO_ACCESS_free(info);
  524. return ret;
  525. }
  526. STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
  527. {
  528. GENERAL_NAMES *gens;
  529. STACK_OF(X509_EXTENSION) *exts;
  530. STACK_OF(OPENSSL_STRING) *ret;
  531. exts = X509_REQ_get_extensions(x);
  532. gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
  533. ret = get_email(X509_REQ_get_subject_name(x), gens);
  534. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  535. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  536. return ret;
  537. }
  538. static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
  539. GENERAL_NAMES *gens)
  540. {
  541. STACK_OF(OPENSSL_STRING) *ret = NULL;
  542. X509_NAME_ENTRY *ne;
  543. ASN1_IA5STRING *email;
  544. GENERAL_NAME *gen;
  545. int i;
  546. size_t j;
  547. /* Now add any email address(es) to STACK */
  548. i = -1;
  549. /* First supplied X509_NAME */
  550. while ((i = X509_NAME_get_index_by_NID(name,
  551. NID_pkcs9_emailAddress, i)) >= 0) {
  552. ne = X509_NAME_get_entry(name, i);
  553. email = X509_NAME_ENTRY_get_data(ne);
  554. if (!append_ia5(&ret, email))
  555. return NULL;
  556. }
  557. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  558. gen = sk_GENERAL_NAME_value(gens, j);
  559. if (gen->type != GEN_EMAIL)
  560. continue;
  561. if (!append_ia5(&ret, gen->d.ia5))
  562. return NULL;
  563. }
  564. return ret;
  565. }
  566. static void str_free(OPENSSL_STRING str)
  567. {
  568. OPENSSL_free(str);
  569. }
  570. static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
  571. {
  572. char *emtmp;
  573. /* First some sanity checks */
  574. if (email->type != V_ASN1_IA5STRING)
  575. return 1;
  576. if (!email->data || !email->length)
  577. return 1;
  578. if (!*sk)
  579. *sk = sk_OPENSSL_STRING_new(sk_strcmp);
  580. if (!*sk)
  581. return 0;
  582. /* Don't add duplicates */
  583. sk_OPENSSL_STRING_sort(*sk);
  584. if (sk_OPENSSL_STRING_find(*sk, NULL, (char *)email->data))
  585. return 1;
  586. emtmp = BUF_strdup((char *)email->data);
  587. if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
  588. X509_email_free(*sk);
  589. *sk = NULL;
  590. return 0;
  591. }
  592. return 1;
  593. }
  594. void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
  595. {
  596. sk_OPENSSL_STRING_pop_free(sk, str_free);
  597. }
  598. typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
  599. const unsigned char *subject, size_t subject_len,
  600. unsigned int flags);
  601. /* Skip pattern prefix to match "wildcard" subject */
  602. static void skip_prefix(const unsigned char **p, size_t *plen,
  603. const unsigned char *subject, size_t subject_len,
  604. unsigned int flags)
  605. {
  606. const unsigned char *pattern = *p;
  607. size_t pattern_len = *plen;
  608. /*
  609. * If subject starts with a leading '.' followed by more octets, and
  610. * pattern is longer, compare just an equal-length suffix with the
  611. * full subject (starting at the '.'), provided the prefix contains
  612. * no NULs.
  613. */
  614. if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
  615. return;
  616. while (pattern_len > subject_len && *pattern) {
  617. if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
  618. *pattern == '.')
  619. break;
  620. ++pattern;
  621. --pattern_len;
  622. }
  623. /* Skip if entire prefix acceptable */
  624. if (pattern_len == subject_len) {
  625. *p = pattern;
  626. *plen = pattern_len;
  627. }
  628. }
  629. /* Compare while ASCII ignoring case. */
  630. static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
  631. const unsigned char *subject, size_t subject_len,
  632. unsigned int flags)
  633. {
  634. skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
  635. if (pattern_len != subject_len)
  636. return 0;
  637. while (pattern_len) {
  638. unsigned char l = *pattern;
  639. unsigned char r = *subject;
  640. /* The pattern must not contain NUL characters. */
  641. if (l == 0)
  642. return 0;
  643. if (l != r) {
  644. if ('A' <= l && l <= 'Z')
  645. l = (l - 'A') + 'a';
  646. if ('A' <= r && r <= 'Z')
  647. r = (r - 'A') + 'a';
  648. if (l != r)
  649. return 0;
  650. }
  651. ++pattern;
  652. ++subject;
  653. --pattern_len;
  654. }
  655. return 1;
  656. }
  657. /* Compare using OPENSSL_memcmp. */
  658. static int equal_case(const unsigned char *pattern, size_t pattern_len,
  659. const unsigned char *subject, size_t subject_len,
  660. unsigned int flags)
  661. {
  662. skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
  663. if (pattern_len != subject_len)
  664. return 0;
  665. return !OPENSSL_memcmp(pattern, subject, pattern_len);
  666. }
  667. /*
  668. * RFC 5280, section 7.5, requires that only the domain is compared in a
  669. * case-insensitive manner.
  670. */
  671. static int equal_email(const unsigned char *a, size_t a_len,
  672. const unsigned char *b, size_t b_len,
  673. unsigned int unused_flags)
  674. {
  675. size_t i = a_len;
  676. if (a_len != b_len)
  677. return 0;
  678. /*
  679. * We search backwards for the '@' character, so that we do not have to
  680. * deal with quoted local-parts. The domain part is compared in a
  681. * case-insensitive manner.
  682. */
  683. while (i > 0) {
  684. --i;
  685. if (a[i] == '@' || b[i] == '@') {
  686. if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
  687. return 0;
  688. break;
  689. }
  690. }
  691. if (i == 0)
  692. i = a_len;
  693. return equal_case(a, i, b, i, 0);
  694. }
  695. /*
  696. * Compare the prefix and suffix with the subject, and check that the
  697. * characters in-between are valid.
  698. */
  699. static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
  700. const unsigned char *suffix, size_t suffix_len,
  701. const unsigned char *subject, size_t subject_len,
  702. unsigned int flags)
  703. {
  704. const unsigned char *wildcard_start;
  705. const unsigned char *wildcard_end;
  706. const unsigned char *p;
  707. int allow_multi = 0;
  708. int allow_idna = 0;
  709. if (subject_len < prefix_len + suffix_len)
  710. return 0;
  711. if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
  712. return 0;
  713. wildcard_start = subject + prefix_len;
  714. wildcard_end = subject + (subject_len - suffix_len);
  715. if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
  716. return 0;
  717. /*
  718. * If the wildcard makes up the entire first label, it must match at
  719. * least one character.
  720. */
  721. if (prefix_len == 0 && *suffix == '.') {
  722. if (wildcard_start == wildcard_end)
  723. return 0;
  724. allow_idna = 1;
  725. if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
  726. allow_multi = 1;
  727. }
  728. /* IDNA labels cannot match partial wildcards */
  729. if (!allow_idna &&
  730. subject_len >= 4
  731. && OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0)
  732. return 0;
  733. /* The wildcard may match a literal '*' */
  734. if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
  735. return 1;
  736. /*
  737. * Check that the part matched by the wildcard contains only
  738. * permitted characters and only matches a single label unless
  739. * allow_multi is set.
  740. */
  741. for (p = wildcard_start; p != wildcard_end; ++p)
  742. if (!(('0' <= *p && *p <= '9') ||
  743. ('A' <= *p && *p <= 'Z') ||
  744. ('a' <= *p && *p <= 'z') ||
  745. *p == '-' || (allow_multi && *p == '.')))
  746. return 0;
  747. return 1;
  748. }
  749. #define LABEL_START (1 << 0)
  750. #define LABEL_END (1 << 1)
  751. #define LABEL_HYPHEN (1 << 2)
  752. #define LABEL_IDNA (1 << 3)
  753. static const unsigned char *valid_star(const unsigned char *p, size_t len,
  754. unsigned int flags)
  755. {
  756. const unsigned char *star = 0;
  757. size_t i;
  758. int state = LABEL_START;
  759. int dots = 0;
  760. for (i = 0; i < len; ++i) {
  761. /*
  762. * Locate first and only legal wildcard, either at the start
  763. * or end of a non-IDNA first and not final label.
  764. */
  765. if (p[i] == '*') {
  766. int atstart = (state & LABEL_START);
  767. int atend = (i == len - 1 || p[i + 1] == '.');
  768. /*
  769. * At most one wildcard per pattern.
  770. * No wildcards in IDNA labels.
  771. * No wildcards after the first label.
  772. */
  773. if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
  774. return NULL;
  775. /* Only full-label '*.example.com' wildcards? */
  776. if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
  777. && (!atstart || !atend))
  778. return NULL;
  779. /* No 'foo*bar' wildcards */
  780. if (!atstart && !atend)
  781. return NULL;
  782. star = &p[i];
  783. state &= ~LABEL_START;
  784. } else if (('a' <= p[i] && p[i] <= 'z')
  785. || ('A' <= p[i] && p[i] <= 'Z')
  786. || ('0' <= p[i] && p[i] <= '9')) {
  787. if ((state & LABEL_START) != 0
  788. && len - i >= 4
  789. && OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0)
  790. state |= LABEL_IDNA;
  791. state &= ~(LABEL_HYPHEN | LABEL_START);
  792. } else if (p[i] == '.') {
  793. if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
  794. return NULL;
  795. state = LABEL_START;
  796. ++dots;
  797. } else if (p[i] == '-') {
  798. /* no domain/subdomain starts with '-' */
  799. if ((state & LABEL_START) != 0)
  800. return NULL;
  801. state |= LABEL_HYPHEN;
  802. } else
  803. return NULL;
  804. }
  805. /*
  806. * The final label must not end in a hyphen or ".", and
  807. * there must be at least two dots after the star.
  808. */
  809. if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
  810. return NULL;
  811. return star;
  812. }
  813. /* Compare using wildcards. */
  814. static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
  815. const unsigned char *subject, size_t subject_len,
  816. unsigned int flags)
  817. {
  818. const unsigned char *star = NULL;
  819. /*
  820. * Subject names starting with '.' can only match a wildcard pattern
  821. * via a subject sub-domain pattern suffix match.
  822. */
  823. if (!(subject_len > 1 && subject[0] == '.'))
  824. star = valid_star(pattern, pattern_len, flags);
  825. if (star == NULL)
  826. return equal_nocase(pattern, pattern_len,
  827. subject, subject_len, flags);
  828. return wildcard_match(pattern, star - pattern,
  829. star + 1, (pattern + pattern_len) - star - 1,
  830. subject, subject_len, flags);
  831. }
  832. /*
  833. * Compare an ASN1_STRING to a supplied string. If they match return 1. If
  834. * cmp_type > 0 only compare if string matches the type, otherwise convert it
  835. * to UTF8.
  836. */
  837. static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
  838. unsigned int flags, const char *b, size_t blen,
  839. char **peername)
  840. {
  841. int rv = 0;
  842. if (!a->data || !a->length)
  843. return 0;
  844. if (cmp_type > 0) {
  845. if (cmp_type != a->type)
  846. return 0;
  847. if (cmp_type == V_ASN1_IA5STRING)
  848. rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
  849. else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen))
  850. rv = 1;
  851. if (rv > 0 && peername)
  852. *peername = BUF_strndup((char *)a->data, a->length);
  853. } else {
  854. int astrlen;
  855. unsigned char *astr;
  856. astrlen = ASN1_STRING_to_UTF8(&astr, a);
  857. if (astrlen < 0)
  858. return -1;
  859. rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
  860. if (rv > 0 && peername)
  861. *peername = BUF_strndup((char *)astr, astrlen);
  862. OPENSSL_free(astr);
  863. }
  864. return rv;
  865. }
  866. static int do_x509_check(X509 *x, const char *chk, size_t chklen,
  867. unsigned int flags, int check_type, char **peername)
  868. {
  869. GENERAL_NAMES *gens = NULL;
  870. X509_NAME *name = NULL;
  871. size_t i;
  872. int j;
  873. int cnid = NID_undef;
  874. int alt_type;
  875. int san_present = 0;
  876. int rv = 0;
  877. equal_fn equal;
  878. /* See below, this flag is internal-only */
  879. flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
  880. if (check_type == GEN_EMAIL) {
  881. cnid = NID_pkcs9_emailAddress;
  882. alt_type = V_ASN1_IA5STRING;
  883. equal = equal_email;
  884. } else if (check_type == GEN_DNS) {
  885. cnid = NID_commonName;
  886. /* Implicit client-side DNS sub-domain pattern */
  887. if (chklen > 1 && chk[0] == '.')
  888. flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
  889. alt_type = V_ASN1_IA5STRING;
  890. if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
  891. equal = equal_nocase;
  892. else
  893. equal = equal_wildcard;
  894. } else {
  895. alt_type = V_ASN1_OCTET_STRING;
  896. equal = equal_case;
  897. }
  898. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  899. if (gens) {
  900. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  901. GENERAL_NAME *gen;
  902. ASN1_STRING *cstr;
  903. gen = sk_GENERAL_NAME_value(gens, i);
  904. if (gen->type != check_type)
  905. continue;
  906. san_present = 1;
  907. if (check_type == GEN_EMAIL)
  908. cstr = gen->d.rfc822Name;
  909. else if (check_type == GEN_DNS)
  910. cstr = gen->d.dNSName;
  911. else
  912. cstr = gen->d.iPAddress;
  913. /* Positive on success, negative on error! */
  914. if ((rv = do_check_string(cstr, alt_type, equal, flags,
  915. chk, chklen, peername)) != 0)
  916. break;
  917. }
  918. GENERAL_NAMES_free(gens);
  919. if (rv != 0)
  920. return rv;
  921. if (cnid == NID_undef
  922. || (san_present
  923. && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
  924. return 0;
  925. }
  926. /* We're done if CN-ID is not pertinent */
  927. if (cnid == NID_undef)
  928. return 0;
  929. j = -1;
  930. name = X509_get_subject_name(x);
  931. while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) {
  932. X509_NAME_ENTRY *ne;
  933. ASN1_STRING *str;
  934. ne = X509_NAME_get_entry(name, j);
  935. str = X509_NAME_ENTRY_get_data(ne);
  936. /* Positive on success, negative on error! */
  937. if ((rv = do_check_string(str, -1, equal, flags,
  938. chk, chklen, peername)) != 0)
  939. return rv;
  940. }
  941. return 0;
  942. }
  943. int X509_check_host(X509 *x, const char *chk, size_t chklen,
  944. unsigned int flags, char **peername)
  945. {
  946. if (chk == NULL)
  947. return -2;
  948. if (OPENSSL_memchr(chk, '\0', chklen))
  949. return -2;
  950. return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
  951. }
  952. int X509_check_email(X509 *x, const char *chk, size_t chklen,
  953. unsigned int flags)
  954. {
  955. if (chk == NULL)
  956. return -2;
  957. if (OPENSSL_memchr(chk, '\0', chklen))
  958. return -2;
  959. return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
  960. }
  961. int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
  962. unsigned int flags)
  963. {
  964. if (chk == NULL)
  965. return -2;
  966. return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
  967. }
  968. int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
  969. {
  970. unsigned char ipout[16];
  971. size_t iplen;
  972. if (ipasc == NULL)
  973. return -2;
  974. iplen = (size_t)a2i_ipadd(ipout, ipasc);
  975. if (iplen == 0)
  976. return -2;
  977. return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
  978. }
  979. /*
  980. * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
  981. * with RFC3280.
  982. */
  983. ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
  984. {
  985. unsigned char ipout[16];
  986. ASN1_OCTET_STRING *ret;
  987. int iplen;
  988. /* If string contains a ':' assume IPv6 */
  989. iplen = a2i_ipadd(ipout, ipasc);
  990. if (!iplen)
  991. return NULL;
  992. ret = ASN1_OCTET_STRING_new();
  993. if (!ret)
  994. return NULL;
  995. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
  996. ASN1_OCTET_STRING_free(ret);
  997. return NULL;
  998. }
  999. return ret;
  1000. }
  1001. ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
  1002. {
  1003. ASN1_OCTET_STRING *ret = NULL;
  1004. unsigned char ipout[32];
  1005. char *iptmp = NULL, *p;
  1006. int iplen1, iplen2;
  1007. p = strchr(ipasc, '/');
  1008. if (!p)
  1009. return NULL;
  1010. iptmp = BUF_strdup(ipasc);
  1011. if (!iptmp)
  1012. return NULL;
  1013. p = iptmp + (p - ipasc);
  1014. *p++ = 0;
  1015. iplen1 = a2i_ipadd(ipout, iptmp);
  1016. if (!iplen1)
  1017. goto err;
  1018. iplen2 = a2i_ipadd(ipout + iplen1, p);
  1019. OPENSSL_free(iptmp);
  1020. iptmp = NULL;
  1021. if (!iplen2 || (iplen1 != iplen2))
  1022. goto err;
  1023. ret = ASN1_OCTET_STRING_new();
  1024. if (!ret)
  1025. goto err;
  1026. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
  1027. goto err;
  1028. return ret;
  1029. err:
  1030. if (iptmp)
  1031. OPENSSL_free(iptmp);
  1032. if (ret)
  1033. ASN1_OCTET_STRING_free(ret);
  1034. return NULL;
  1035. }
  1036. int a2i_ipadd(unsigned char *ipout, const char *ipasc)
  1037. {
  1038. /* If string contains a ':' assume IPv6 */
  1039. if (strchr(ipasc, ':')) {
  1040. if (!ipv6_from_asc(ipout, ipasc))
  1041. return 0;
  1042. return 16;
  1043. } else {
  1044. if (!ipv4_from_asc(ipout, ipasc))
  1045. return 0;
  1046. return 4;
  1047. }
  1048. }
  1049. static int ipv4_from_asc(unsigned char *v4, const char *in)
  1050. {
  1051. int a0, a1, a2, a3;
  1052. if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
  1053. return 0;
  1054. if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
  1055. || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
  1056. return 0;
  1057. v4[0] = a0;
  1058. v4[1] = a1;
  1059. v4[2] = a2;
  1060. v4[3] = a3;
  1061. return 1;
  1062. }
  1063. typedef struct {
  1064. /* Temporary store for IPV6 output */
  1065. unsigned char tmp[16];
  1066. /* Total number of bytes in tmp */
  1067. int total;
  1068. /* The position of a zero (corresponding to '::') */
  1069. int zero_pos;
  1070. /* Number of zeroes */
  1071. int zero_cnt;
  1072. } IPV6_STAT;
  1073. static int ipv6_from_asc(unsigned char *v6, const char *in)
  1074. {
  1075. IPV6_STAT v6stat;
  1076. v6stat.total = 0;
  1077. v6stat.zero_pos = -1;
  1078. v6stat.zero_cnt = 0;
  1079. /*
  1080. * Treat the IPv6 representation as a list of values separated by ':'.
  1081. * The presence of a '::' will parse as one, two or three zero length
  1082. * elements.
  1083. */
  1084. if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
  1085. return 0;
  1086. /* Now for some sanity checks */
  1087. if (v6stat.zero_pos == -1) {
  1088. /* If no '::' must have exactly 16 bytes */
  1089. if (v6stat.total != 16)
  1090. return 0;
  1091. } else {
  1092. /* If '::' must have less than 16 bytes */
  1093. if (v6stat.total == 16)
  1094. return 0;
  1095. /* More than three zeroes is an error */
  1096. if (v6stat.zero_cnt > 3)
  1097. return 0;
  1098. /* Can only have three zeroes if nothing else present */
  1099. else if (v6stat.zero_cnt == 3) {
  1100. if (v6stat.total > 0)
  1101. return 0;
  1102. }
  1103. /* Can only have two zeroes if at start or end */
  1104. else if (v6stat.zero_cnt == 2) {
  1105. if ((v6stat.zero_pos != 0)
  1106. && (v6stat.zero_pos != v6stat.total))
  1107. return 0;
  1108. } else
  1109. /* Can only have one zero if *not* start or end */
  1110. {
  1111. if ((v6stat.zero_pos == 0)
  1112. || (v6stat.zero_pos == v6stat.total))
  1113. return 0;
  1114. }
  1115. }
  1116. /* Format result */
  1117. if (v6stat.zero_pos >= 0) {
  1118. /* Copy initial part */
  1119. OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos);
  1120. /* Zero middle */
  1121. OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
  1122. /* Copy final part */
  1123. if (v6stat.total != v6stat.zero_pos)
  1124. OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
  1125. v6stat.tmp + v6stat.zero_pos,
  1126. v6stat.total - v6stat.zero_pos);
  1127. } else
  1128. OPENSSL_memcpy(v6, v6stat.tmp, 16);
  1129. return 1;
  1130. }
  1131. static int ipv6_cb(const char *elem, int len, void *usr)
  1132. {
  1133. IPV6_STAT *s = usr;
  1134. /* Error if 16 bytes written */
  1135. if (s->total == 16)
  1136. return 0;
  1137. if (len == 0) {
  1138. /* Zero length element, corresponds to '::' */
  1139. if (s->zero_pos == -1)
  1140. s->zero_pos = s->total;
  1141. /* If we've already got a :: its an error */
  1142. else if (s->zero_pos != s->total)
  1143. return 0;
  1144. s->zero_cnt++;
  1145. } else {
  1146. /* If more than 4 characters could be final a.b.c.d form */
  1147. if (len > 4) {
  1148. /* Need at least 4 bytes left */
  1149. if (s->total > 12)
  1150. return 0;
  1151. /* Must be end of string */
  1152. if (elem[len])
  1153. return 0;
  1154. if (!ipv4_from_asc(s->tmp + s->total, elem))
  1155. return 0;
  1156. s->total += 4;
  1157. } else {
  1158. if (!ipv6_hex(s->tmp + s->total, elem, len))
  1159. return 0;
  1160. s->total += 2;
  1161. }
  1162. }
  1163. return 1;
  1164. }
  1165. /*
  1166. * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
  1167. */
  1168. static int ipv6_hex(unsigned char *out, const char *in, int inlen)
  1169. {
  1170. unsigned char c;
  1171. unsigned int num = 0;
  1172. if (inlen > 4)
  1173. return 0;
  1174. while (inlen--) {
  1175. c = *in++;
  1176. num <<= 4;
  1177. if ((c >= '0') && (c <= '9'))
  1178. num |= c - '0';
  1179. else if ((c >= 'A') && (c <= 'F'))
  1180. num |= c - 'A' + 10;
  1181. else if ((c >= 'a') && (c <= 'f'))
  1182. num |= c - 'a' + 10;
  1183. else
  1184. return 0;
  1185. }
  1186. out[0] = num >> 8;
  1187. out[1] = num & 0xff;
  1188. return 1;
  1189. }
  1190. int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF (CONF_VALUE) * dn_sk,
  1191. unsigned long chtype)
  1192. {
  1193. CONF_VALUE *v;
  1194. int mval;
  1195. size_t i;
  1196. char *p, *type;
  1197. if (!nm)
  1198. return 0;
  1199. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  1200. v = sk_CONF_VALUE_value(dn_sk, i);
  1201. type = v->name;
  1202. /*
  1203. * Skip past any leading X. X: X, etc to allow for multiple instances
  1204. */
  1205. for (p = type; *p; p++)
  1206. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1207. p++;
  1208. if (*p)
  1209. type = p;
  1210. break;
  1211. }
  1212. if (*type == '+') {
  1213. mval = -1;
  1214. type++;
  1215. } else
  1216. mval = 0;
  1217. if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
  1218. (unsigned char *)v->value, -1, -1,
  1219. mval))
  1220. return 0;
  1221. }
  1222. return 1;
  1223. }