v3_utl.c 38 KB

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