v3_utl.c 38 KB

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