1
0

v3_utl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* v3_utl.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. /* X509 v3 extension utilities */
  60. #include <stdio.h>
  61. #include <ctype.h>
  62. #include "cryptlib.h"
  63. #include <openssl/conf.h>
  64. #include <openssl/x509v3.h>
  65. #include <openssl/bn.h>
  66. static char *strip_spaces(char *name);
  67. static int sk_strcmp(const char *const *a, const char *const *b);
  68. static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
  69. GENERAL_NAMES *gens);
  70. static void str_free(OPENSSL_STRING str);
  71. static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
  72. static int ipv4_from_asc(unsigned char *v4, const char *in);
  73. static int ipv6_from_asc(unsigned char *v6, const char *in);
  74. static int ipv6_cb(const char *elem, int len, void *usr);
  75. static int ipv6_hex(unsigned char *out, const char *in, int inlen);
  76. /* Add a CONF_VALUE name value pair to stack */
  77. int X509V3_add_value(const char *name, const char *value,
  78. STACK_OF(CONF_VALUE) **extlist)
  79. {
  80. CONF_VALUE *vtmp = NULL;
  81. char *tname = NULL, *tvalue = NULL;
  82. if (name && !(tname = BUF_strdup(name)))
  83. goto err;
  84. if (value && !(tvalue = BUF_strdup(value)))
  85. goto err;
  86. if (!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
  87. goto err;
  88. if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
  89. goto err;
  90. vtmp->section = NULL;
  91. vtmp->name = tname;
  92. vtmp->value = tvalue;
  93. if (!sk_CONF_VALUE_push(*extlist, vtmp))
  94. goto err;
  95. return 1;
  96. err:
  97. X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
  98. if (vtmp)
  99. OPENSSL_free(vtmp);
  100. if (tname)
  101. OPENSSL_free(tname);
  102. if (tvalue)
  103. OPENSSL_free(tvalue);
  104. return 0;
  105. }
  106. int X509V3_add_value_uchar(const char *name, const unsigned char *value,
  107. STACK_OF(CONF_VALUE) **extlist)
  108. {
  109. return X509V3_add_value(name, (const char *)value, extlist);
  110. }
  111. /* Free function for STACK_OF(CONF_VALUE) */
  112. void X509V3_conf_free(CONF_VALUE *conf)
  113. {
  114. if (!conf)
  115. return;
  116. if (conf->name)
  117. OPENSSL_free(conf->name);
  118. if (conf->value)
  119. OPENSSL_free(conf->value);
  120. if (conf->section)
  121. OPENSSL_free(conf->section);
  122. OPENSSL_free(conf);
  123. }
  124. int X509V3_add_value_bool(const char *name, int asn1_bool,
  125. STACK_OF(CONF_VALUE) **extlist)
  126. {
  127. if (asn1_bool)
  128. return X509V3_add_value(name, "TRUE", extlist);
  129. return X509V3_add_value(name, "FALSE", extlist);
  130. }
  131. int X509V3_add_value_bool_nf(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 1;
  137. }
  138. char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
  139. {
  140. BIGNUM *bntmp = NULL;
  141. char *strtmp = NULL;
  142. if (!a)
  143. return NULL;
  144. if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
  145. !(strtmp = BN_bn2dec(bntmp)))
  146. X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
  147. BN_free(bntmp);
  148. return strtmp;
  149. }
  150. char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
  151. {
  152. BIGNUM *bntmp = NULL;
  153. char *strtmp = NULL;
  154. if (!a)
  155. return NULL;
  156. if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
  157. !(strtmp = BN_bn2dec(bntmp)))
  158. X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  159. BN_free(bntmp);
  160. return strtmp;
  161. }
  162. ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
  163. {
  164. BIGNUM *bn = NULL;
  165. ASN1_INTEGER *aint;
  166. int isneg, ishex;
  167. int ret;
  168. if (!value) {
  169. X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
  170. return 0;
  171. }
  172. bn = BN_new();
  173. if (value[0] == '-') {
  174. value++;
  175. isneg = 1;
  176. } else
  177. isneg = 0;
  178. if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
  179. value += 2;
  180. ishex = 1;
  181. } else
  182. ishex = 0;
  183. if (ishex)
  184. ret = BN_hex2bn(&bn, value);
  185. else
  186. ret = BN_dec2bn(&bn, value);
  187. if (!ret || value[ret]) {
  188. BN_free(bn);
  189. X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
  190. return 0;
  191. }
  192. if (isneg && BN_is_zero(bn))
  193. isneg = 0;
  194. aint = BN_to_ASN1_INTEGER(bn, NULL);
  195. BN_free(bn);
  196. if (!aint) {
  197. X509V3err(X509V3_F_S2I_ASN1_INTEGER,
  198. X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
  199. return 0;
  200. }
  201. if (isneg)
  202. aint->type |= V_ASN1_NEG;
  203. return aint;
  204. }
  205. int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
  206. STACK_OF(CONF_VALUE) **extlist)
  207. {
  208. char *strtmp;
  209. int ret;
  210. if (!aint)
  211. return 1;
  212. if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
  213. return 0;
  214. ret = X509V3_add_value(name, strtmp, extlist);
  215. OPENSSL_free(strtmp);
  216. return ret;
  217. }
  218. int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
  219. {
  220. char *btmp;
  221. if (!(btmp = value->value))
  222. goto err;
  223. if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
  224. || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
  225. || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
  226. *asn1_bool = 0xff;
  227. return 1;
  228. } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
  229. || !strcmp(btmp, "N") || !strcmp(btmp, "n")
  230. || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
  231. *asn1_bool = 0;
  232. return 1;
  233. }
  234. err:
  235. X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
  236. X509V3_R_INVALID_BOOLEAN_STRING);
  237. X509V3_conf_err(value);
  238. return 0;
  239. }
  240. int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
  241. {
  242. ASN1_INTEGER *itmp;
  243. if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
  244. X509V3_conf_err(value);
  245. return 0;
  246. }
  247. *aint = itmp;
  248. return 1;
  249. }
  250. #define HDR_NAME 1
  251. #define HDR_VALUE 2
  252. /*
  253. * #define DEBUG
  254. */
  255. STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
  256. {
  257. char *p, *q, c;
  258. char *ntmp, *vtmp;
  259. STACK_OF(CONF_VALUE) *values = NULL;
  260. char *linebuf;
  261. int state;
  262. /* We are going to modify the line so copy it first */
  263. linebuf = BUF_strdup(line);
  264. if (linebuf == NULL) {
  265. X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE);
  266. goto err;
  267. }
  268. state = HDR_NAME;
  269. ntmp = NULL;
  270. /* Go through all characters */
  271. for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
  272. p++) {
  273. switch (state) {
  274. case HDR_NAME:
  275. if (c == ':') {
  276. state = HDR_VALUE;
  277. *p = 0;
  278. ntmp = strip_spaces(q);
  279. if (!ntmp) {
  280. X509V3err(X509V3_F_X509V3_PARSE_LIST,
  281. X509V3_R_INVALID_NULL_NAME);
  282. goto err;
  283. }
  284. q = p + 1;
  285. } else if (c == ',') {
  286. *p = 0;
  287. ntmp = strip_spaces(q);
  288. q = p + 1;
  289. #if 0
  290. printf("%s\n", ntmp);
  291. #endif
  292. if (!ntmp) {
  293. X509V3err(X509V3_F_X509V3_PARSE_LIST,
  294. X509V3_R_INVALID_NULL_NAME);
  295. goto err;
  296. }
  297. X509V3_add_value(ntmp, NULL, &values);
  298. }
  299. break;
  300. case HDR_VALUE:
  301. if (c == ',') {
  302. state = HDR_NAME;
  303. *p = 0;
  304. vtmp = strip_spaces(q);
  305. #if 0
  306. printf("%s\n", ntmp);
  307. #endif
  308. if (!vtmp) {
  309. X509V3err(X509V3_F_X509V3_PARSE_LIST,
  310. X509V3_R_INVALID_NULL_VALUE);
  311. goto err;
  312. }
  313. X509V3_add_value(ntmp, vtmp, &values);
  314. ntmp = NULL;
  315. q = p + 1;
  316. }
  317. }
  318. }
  319. if (state == HDR_VALUE) {
  320. vtmp = strip_spaces(q);
  321. #if 0
  322. printf("%s=%s\n", ntmp, vtmp);
  323. #endif
  324. if (!vtmp) {
  325. X509V3err(X509V3_F_X509V3_PARSE_LIST,
  326. X509V3_R_INVALID_NULL_VALUE);
  327. goto err;
  328. }
  329. X509V3_add_value(ntmp, vtmp, &values);
  330. } else {
  331. ntmp = strip_spaces(q);
  332. #if 0
  333. printf("%s\n", ntmp);
  334. #endif
  335. if (!ntmp) {
  336. X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
  337. goto err;
  338. }
  339. X509V3_add_value(ntmp, NULL, &values);
  340. }
  341. OPENSSL_free(linebuf);
  342. return values;
  343. err:
  344. OPENSSL_free(linebuf);
  345. sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
  346. return NULL;
  347. }
  348. /* Delete leading and trailing spaces from a string */
  349. static char *strip_spaces(char *name)
  350. {
  351. char *p, *q;
  352. /* Skip over leading spaces */
  353. p = name;
  354. while (*p && isspace((unsigned char)*p))
  355. p++;
  356. if (!*p)
  357. return NULL;
  358. q = p + strlen(p) - 1;
  359. while ((q != p) && isspace((unsigned char)*q))
  360. q--;
  361. if (p != q)
  362. q[1] = 0;
  363. if (!*p)
  364. return NULL;
  365. return p;
  366. }
  367. /* hex string utilities */
  368. /*
  369. * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
  370. * hex representation @@@ (Contents of buffer are always kept in ASCII, also
  371. * on EBCDIC machines)
  372. */
  373. char *hex_to_string(const unsigned char *buffer, long len)
  374. {
  375. char *tmp, *q;
  376. const unsigned char *p;
  377. int i;
  378. const static char hexdig[] = "0123456789ABCDEF";
  379. if (!buffer || !len)
  380. return NULL;
  381. if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
  382. X509V3err(X509V3_F_HEX_TO_STRING, ERR_R_MALLOC_FAILURE);
  383. return NULL;
  384. }
  385. q = tmp;
  386. for (i = 0, p = buffer; i < len; i++, p++) {
  387. *q++ = hexdig[(*p >> 4) & 0xf];
  388. *q++ = hexdig[*p & 0xf];
  389. *q++ = ':';
  390. }
  391. q[-1] = 0;
  392. #ifdef CHARSET_EBCDIC
  393. ebcdic2ascii(tmp, tmp, q - tmp - 1);
  394. #endif
  395. return tmp;
  396. }
  397. /*
  398. * Give a string of hex digits convert to a buffer
  399. */
  400. unsigned char *string_to_hex(const char *str, long *len)
  401. {
  402. unsigned char *hexbuf, *q;
  403. unsigned char ch, cl, *p;
  404. if (!str) {
  405. X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_INVALID_NULL_ARGUMENT);
  406. return NULL;
  407. }
  408. if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
  409. goto err;
  410. for (p = (unsigned char *)str, q = hexbuf; *p;) {
  411. ch = *p++;
  412. #ifdef CHARSET_EBCDIC
  413. ch = os_toebcdic[ch];
  414. #endif
  415. if (ch == ':')
  416. continue;
  417. cl = *p++;
  418. #ifdef CHARSET_EBCDIC
  419. cl = os_toebcdic[cl];
  420. #endif
  421. if (!cl) {
  422. X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ODD_NUMBER_OF_DIGITS);
  423. OPENSSL_free(hexbuf);
  424. return NULL;
  425. }
  426. if (isupper(ch))
  427. ch = tolower(ch);
  428. if (isupper(cl))
  429. cl = tolower(cl);
  430. if ((ch >= '0') && (ch <= '9'))
  431. ch -= '0';
  432. else if ((ch >= 'a') && (ch <= 'f'))
  433. ch -= 'a' - 10;
  434. else
  435. goto badhex;
  436. if ((cl >= '0') && (cl <= '9'))
  437. cl -= '0';
  438. else if ((cl >= 'a') && (cl <= 'f'))
  439. cl -= 'a' - 10;
  440. else
  441. goto badhex;
  442. *q++ = (ch << 4) | cl;
  443. }
  444. if (len)
  445. *len = q - hexbuf;
  446. return hexbuf;
  447. err:
  448. if (hexbuf)
  449. OPENSSL_free(hexbuf);
  450. X509V3err(X509V3_F_STRING_TO_HEX, ERR_R_MALLOC_FAILURE);
  451. return NULL;
  452. badhex:
  453. OPENSSL_free(hexbuf);
  454. X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ILLEGAL_HEX_DIGIT);
  455. return NULL;
  456. }
  457. /*
  458. * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
  459. */
  460. int name_cmp(const char *name, const char *cmp)
  461. {
  462. int len, ret;
  463. char c;
  464. len = strlen(cmp);
  465. if ((ret = strncmp(name, cmp, len)))
  466. return ret;
  467. c = name[len];
  468. if (!c || (c == '.'))
  469. return 0;
  470. return 1;
  471. }
  472. static int sk_strcmp(const char *const *a, const char *const *b)
  473. {
  474. return strcmp(*a, *b);
  475. }
  476. STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
  477. {
  478. GENERAL_NAMES *gens;
  479. STACK_OF(OPENSSL_STRING) *ret;
  480. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  481. ret = get_email(X509_get_subject_name(x), gens);
  482. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  483. return ret;
  484. }
  485. STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
  486. {
  487. AUTHORITY_INFO_ACCESS *info;
  488. STACK_OF(OPENSSL_STRING) *ret = NULL;
  489. int i;
  490. info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
  491. if (!info)
  492. return NULL;
  493. for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
  494. ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
  495. if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
  496. if (ad->location->type == GEN_URI) {
  497. if (!append_ia5
  498. (&ret, ad->location->d.uniformResourceIdentifier))
  499. break;
  500. }
  501. }
  502. }
  503. AUTHORITY_INFO_ACCESS_free(info);
  504. return ret;
  505. }
  506. STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
  507. {
  508. GENERAL_NAMES *gens;
  509. STACK_OF(X509_EXTENSION) *exts;
  510. STACK_OF(OPENSSL_STRING) *ret;
  511. exts = X509_REQ_get_extensions(x);
  512. gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
  513. ret = get_email(X509_REQ_get_subject_name(x), gens);
  514. sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
  515. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  516. return ret;
  517. }
  518. static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
  519. GENERAL_NAMES *gens)
  520. {
  521. STACK_OF(OPENSSL_STRING) *ret = NULL;
  522. X509_NAME_ENTRY *ne;
  523. ASN1_IA5STRING *email;
  524. GENERAL_NAME *gen;
  525. int i;
  526. /* Now add any email address(es) to STACK */
  527. i = -1;
  528. /* First supplied X509_NAME */
  529. while ((i = X509_NAME_get_index_by_NID(name,
  530. NID_pkcs9_emailAddress, i)) >= 0) {
  531. ne = X509_NAME_get_entry(name, i);
  532. email = X509_NAME_ENTRY_get_data(ne);
  533. if (!append_ia5(&ret, email))
  534. return NULL;
  535. }
  536. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  537. gen = sk_GENERAL_NAME_value(gens, i);
  538. if (gen->type != GEN_EMAIL)
  539. continue;
  540. if (!append_ia5(&ret, gen->d.ia5))
  541. return NULL;
  542. }
  543. return ret;
  544. }
  545. static void str_free(OPENSSL_STRING str)
  546. {
  547. OPENSSL_free(str);
  548. }
  549. static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
  550. {
  551. char *emtmp;
  552. /* First some sanity checks */
  553. if (email->type != V_ASN1_IA5STRING)
  554. return 1;
  555. if (!email->data || !email->length)
  556. return 1;
  557. if (!*sk)
  558. *sk = sk_OPENSSL_STRING_new(sk_strcmp);
  559. if (!*sk)
  560. return 0;
  561. /* Don't add duplicates */
  562. if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
  563. return 1;
  564. emtmp = BUF_strdup((char *)email->data);
  565. if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
  566. X509_email_free(*sk);
  567. *sk = NULL;
  568. return 0;
  569. }
  570. return 1;
  571. }
  572. void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
  573. {
  574. sk_OPENSSL_STRING_pop_free(sk, str_free);
  575. }
  576. /*
  577. * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
  578. * with RFC3280.
  579. */
  580. ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
  581. {
  582. unsigned char ipout[16];
  583. ASN1_OCTET_STRING *ret;
  584. int iplen;
  585. /* If string contains a ':' assume IPv6 */
  586. iplen = a2i_ipadd(ipout, ipasc);
  587. if (!iplen)
  588. return NULL;
  589. ret = ASN1_OCTET_STRING_new();
  590. if (!ret)
  591. return NULL;
  592. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
  593. ASN1_OCTET_STRING_free(ret);
  594. return NULL;
  595. }
  596. return ret;
  597. }
  598. ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
  599. {
  600. ASN1_OCTET_STRING *ret = NULL;
  601. unsigned char ipout[32];
  602. char *iptmp = NULL, *p;
  603. int iplen1, iplen2;
  604. p = strchr(ipasc, '/');
  605. if (!p)
  606. return NULL;
  607. iptmp = BUF_strdup(ipasc);
  608. if (!iptmp)
  609. return NULL;
  610. p = iptmp + (p - ipasc);
  611. *p++ = 0;
  612. iplen1 = a2i_ipadd(ipout, iptmp);
  613. if (!iplen1)
  614. goto err;
  615. iplen2 = a2i_ipadd(ipout + iplen1, p);
  616. OPENSSL_free(iptmp);
  617. iptmp = NULL;
  618. if (!iplen2 || (iplen1 != iplen2))
  619. goto err;
  620. ret = ASN1_OCTET_STRING_new();
  621. if (!ret)
  622. goto err;
  623. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
  624. goto err;
  625. return ret;
  626. err:
  627. if (iptmp)
  628. OPENSSL_free(iptmp);
  629. if (ret)
  630. ASN1_OCTET_STRING_free(ret);
  631. return NULL;
  632. }
  633. int a2i_ipadd(unsigned char *ipout, const char *ipasc)
  634. {
  635. /* If string contains a ':' assume IPv6 */
  636. if (strchr(ipasc, ':')) {
  637. if (!ipv6_from_asc(ipout, ipasc))
  638. return 0;
  639. return 16;
  640. } else {
  641. if (!ipv4_from_asc(ipout, ipasc))
  642. return 0;
  643. return 4;
  644. }
  645. }
  646. static int ipv4_from_asc(unsigned char *v4, const char *in)
  647. {
  648. int a0, a1, a2, a3;
  649. if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
  650. return 0;
  651. if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
  652. || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
  653. return 0;
  654. v4[0] = a0;
  655. v4[1] = a1;
  656. v4[2] = a2;
  657. v4[3] = a3;
  658. return 1;
  659. }
  660. typedef struct {
  661. /* Temporary store for IPV6 output */
  662. unsigned char tmp[16];
  663. /* Total number of bytes in tmp */
  664. int total;
  665. /* The position of a zero (corresponding to '::') */
  666. int zero_pos;
  667. /* Number of zeroes */
  668. int zero_cnt;
  669. } IPV6_STAT;
  670. static int ipv6_from_asc(unsigned char *v6, const char *in)
  671. {
  672. IPV6_STAT v6stat;
  673. v6stat.total = 0;
  674. v6stat.zero_pos = -1;
  675. v6stat.zero_cnt = 0;
  676. /*
  677. * Treat the IPv6 representation as a list of values separated by ':'.
  678. * The presence of a '::' will parse as one, two or three zero length
  679. * elements.
  680. */
  681. if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
  682. return 0;
  683. /* Now for some sanity checks */
  684. if (v6stat.zero_pos == -1) {
  685. /* If no '::' must have exactly 16 bytes */
  686. if (v6stat.total != 16)
  687. return 0;
  688. } else {
  689. /* If '::' must have less than 16 bytes */
  690. if (v6stat.total == 16)
  691. return 0;
  692. /* More than three zeroes is an error */
  693. if (v6stat.zero_cnt > 3)
  694. return 0;
  695. /* Can only have three zeroes if nothing else present */
  696. else if (v6stat.zero_cnt == 3) {
  697. if (v6stat.total > 0)
  698. return 0;
  699. }
  700. /* Can only have two zeroes if at start or end */
  701. else if (v6stat.zero_cnt == 2) {
  702. if ((v6stat.zero_pos != 0)
  703. && (v6stat.zero_pos != v6stat.total))
  704. return 0;
  705. } else
  706. /* Can only have one zero if *not* start or end */
  707. {
  708. if ((v6stat.zero_pos == 0)
  709. || (v6stat.zero_pos == v6stat.total))
  710. return 0;
  711. }
  712. }
  713. /* Format result */
  714. if (v6stat.zero_pos >= 0) {
  715. /* Copy initial part */
  716. memcpy(v6, v6stat.tmp, v6stat.zero_pos);
  717. /* Zero middle */
  718. memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
  719. /* Copy final part */
  720. if (v6stat.total != v6stat.zero_pos)
  721. memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
  722. v6stat.tmp + v6stat.zero_pos,
  723. v6stat.total - v6stat.zero_pos);
  724. } else
  725. memcpy(v6, v6stat.tmp, 16);
  726. return 1;
  727. }
  728. static int ipv6_cb(const char *elem, int len, void *usr)
  729. {
  730. IPV6_STAT *s = usr;
  731. /* Error if 16 bytes written */
  732. if (s->total == 16)
  733. return 0;
  734. if (len == 0) {
  735. /* Zero length element, corresponds to '::' */
  736. if (s->zero_pos == -1)
  737. s->zero_pos = s->total;
  738. /* If we've already got a :: its an error */
  739. else if (s->zero_pos != s->total)
  740. return 0;
  741. s->zero_cnt++;
  742. } else {
  743. /* If more than 4 characters could be final a.b.c.d form */
  744. if (len > 4) {
  745. /* Need at least 4 bytes left */
  746. if (s->total > 12)
  747. return 0;
  748. /* Must be end of string */
  749. if (elem[len])
  750. return 0;
  751. if (!ipv4_from_asc(s->tmp + s->total, elem))
  752. return 0;
  753. s->total += 4;
  754. } else {
  755. if (!ipv6_hex(s->tmp + s->total, elem, len))
  756. return 0;
  757. s->total += 2;
  758. }
  759. }
  760. return 1;
  761. }
  762. /*
  763. * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
  764. */
  765. static int ipv6_hex(unsigned char *out, const char *in, int inlen)
  766. {
  767. unsigned char c;
  768. unsigned int num = 0;
  769. if (inlen > 4)
  770. return 0;
  771. while (inlen--) {
  772. c = *in++;
  773. num <<= 4;
  774. if ((c >= '0') && (c <= '9'))
  775. num |= c - '0';
  776. else if ((c >= 'A') && (c <= 'F'))
  777. num |= c - 'A' + 10;
  778. else if ((c >= 'a') && (c <= 'f'))
  779. num |= c - 'a' + 10;
  780. else
  781. return 0;
  782. }
  783. out[0] = num >> 8;
  784. out[1] = num & 0xff;
  785. return 1;
  786. }
  787. int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
  788. unsigned long chtype)
  789. {
  790. CONF_VALUE *v;
  791. int i, mval;
  792. char *p, *type;
  793. if (!nm)
  794. return 0;
  795. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  796. v = sk_CONF_VALUE_value(dn_sk, i);
  797. type = v->name;
  798. /*
  799. * Skip past any leading X. X: X, etc to allow for multiple instances
  800. */
  801. for (p = type; *p; p++)
  802. #ifndef CHARSET_EBCDIC
  803. if ((*p == ':') || (*p == ',') || (*p == '.'))
  804. #else
  805. if ((*p == os_toascii[':']) || (*p == os_toascii[','])
  806. || (*p == os_toascii['.']))
  807. #endif
  808. {
  809. p++;
  810. if (*p)
  811. type = p;
  812. break;
  813. }
  814. #ifndef CHARSET_EBCDIC
  815. if (*type == '+')
  816. #else
  817. if (*type == os_toascii['+'])
  818. #endif
  819. {
  820. mval = -1;
  821. type++;
  822. } else
  823. mval = 0;
  824. if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
  825. (unsigned char *)v->value, -1, -1,
  826. mval))
  827. return 0;
  828. }
  829. return 1;
  830. }