v3_utl.c 38 KB

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