v3_utl.c 37 KB

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