v3_utl.c 21 KB

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