No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1328 líneas
37 KiB

  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. /* no domain/subdomain starts with '-' */
  784. if ((state & LABEL_START) != 0)
  785. return NULL;
  786. state |= LABEL_HYPHEN;
  787. } else
  788. return NULL;
  789. }
  790. /*
  791. * The final label must not end in a hyphen or ".", and
  792. * there must be at least two dots after the star.
  793. */
  794. if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
  795. return NULL;
  796. return star;
  797. }
  798. /* Compare using wildcards. */
  799. static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
  800. const unsigned char *subject, size_t subject_len,
  801. unsigned int flags)
  802. {
  803. const unsigned char *star = NULL;
  804. /*
  805. * Subject names starting with '.' can only match a wildcard pattern
  806. * via a subject sub-domain pattern suffix match.
  807. */
  808. if (!(subject_len > 1 && subject[0] == '.'))
  809. star = valid_star(pattern, pattern_len, flags);
  810. if (star == NULL)
  811. return equal_nocase(pattern, pattern_len,
  812. subject, subject_len, flags);
  813. return wildcard_match(pattern, star - pattern,
  814. star + 1, (pattern + pattern_len) - star - 1,
  815. subject, subject_len, flags);
  816. }
  817. /*
  818. * Compare an ASN1_STRING to a supplied string. If they match return 1. If
  819. * cmp_type > 0 only compare if string matches the type, otherwise convert it
  820. * to UTF8.
  821. */
  822. static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
  823. unsigned int flags, const char *b, size_t blen,
  824. char **peername)
  825. {
  826. int rv = 0;
  827. if (!a->data || !a->length)
  828. return 0;
  829. if (cmp_type > 0) {
  830. if (cmp_type != a->type)
  831. return 0;
  832. if (cmp_type == V_ASN1_IA5STRING)
  833. rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
  834. else if (a->length == (int)blen && !memcmp(a->data, b, blen))
  835. rv = 1;
  836. if (rv > 0 && peername)
  837. *peername = BUF_strndup((char *)a->data, a->length);
  838. } else {
  839. int astrlen;
  840. unsigned char *astr;
  841. astrlen = ASN1_STRING_to_UTF8(&astr, a);
  842. if (astrlen < 0)
  843. return -1;
  844. rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
  845. if (rv > 0 && peername)
  846. *peername = BUF_strndup((char *)astr, astrlen);
  847. OPENSSL_free(astr);
  848. }
  849. return rv;
  850. }
  851. static int do_x509_check(X509 *x, const char *chk, size_t chklen,
  852. unsigned int flags, int check_type, char **peername)
  853. {
  854. GENERAL_NAMES *gens = NULL;
  855. X509_NAME *name = NULL;
  856. size_t i;
  857. int j;
  858. int cnid = NID_undef;
  859. int alt_type;
  860. int san_present = 0;
  861. int rv = 0;
  862. equal_fn equal;
  863. /* See below, this flag is internal-only */
  864. flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
  865. if (check_type == GEN_EMAIL) {
  866. cnid = NID_pkcs9_emailAddress;
  867. alt_type = V_ASN1_IA5STRING;
  868. equal = equal_email;
  869. } else if (check_type == GEN_DNS) {
  870. cnid = NID_commonName;
  871. /* Implicit client-side DNS sub-domain pattern */
  872. if (chklen > 1 && chk[0] == '.')
  873. flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
  874. alt_type = V_ASN1_IA5STRING;
  875. if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
  876. equal = equal_nocase;
  877. else
  878. equal = equal_wildcard;
  879. } else {
  880. alt_type = V_ASN1_OCTET_STRING;
  881. equal = equal_case;
  882. }
  883. gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  884. if (gens) {
  885. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  886. GENERAL_NAME *gen;
  887. ASN1_STRING *cstr;
  888. gen = sk_GENERAL_NAME_value(gens, i);
  889. if (gen->type != check_type)
  890. continue;
  891. san_present = 1;
  892. if (check_type == GEN_EMAIL)
  893. cstr = gen->d.rfc822Name;
  894. else if (check_type == GEN_DNS)
  895. cstr = gen->d.dNSName;
  896. else
  897. cstr = gen->d.iPAddress;
  898. /* Positive on success, negative on error! */
  899. if ((rv = do_check_string(cstr, alt_type, equal, flags,
  900. chk, chklen, peername)) != 0)
  901. break;
  902. }
  903. GENERAL_NAMES_free(gens);
  904. if (rv != 0)
  905. return rv;
  906. if (cnid == NID_undef
  907. || (san_present
  908. && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
  909. return 0;
  910. }
  911. /* We're done if CN-ID is not pertinent */
  912. if (cnid == NID_undef)
  913. return 0;
  914. j = -1;
  915. name = X509_get_subject_name(x);
  916. while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) {
  917. X509_NAME_ENTRY *ne;
  918. ASN1_STRING *str;
  919. ne = X509_NAME_get_entry(name, j);
  920. str = X509_NAME_ENTRY_get_data(ne);
  921. /* Positive on success, negative on error! */
  922. if ((rv = do_check_string(str, -1, equal, flags,
  923. chk, chklen, peername)) != 0)
  924. return rv;
  925. }
  926. return 0;
  927. }
  928. int X509_check_host(X509 *x, const char *chk, size_t chklen,
  929. unsigned int flags, char **peername)
  930. {
  931. if (chk == NULL)
  932. return -2;
  933. if (memchr(chk, '\0', chklen))
  934. return -2;
  935. return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
  936. }
  937. int X509_check_email(X509 *x, const char *chk, size_t chklen,
  938. unsigned int flags)
  939. {
  940. if (chk == NULL)
  941. return -2;
  942. if (memchr(chk, '\0', chklen))
  943. return -2;
  944. return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
  945. }
  946. int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
  947. unsigned int flags)
  948. {
  949. if (chk == NULL)
  950. return -2;
  951. return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
  952. }
  953. int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
  954. {
  955. unsigned char ipout[16];
  956. size_t iplen;
  957. if (ipasc == NULL)
  958. return -2;
  959. iplen = (size_t)a2i_ipadd(ipout, ipasc);
  960. if (iplen == 0)
  961. return -2;
  962. return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
  963. }
  964. /*
  965. * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
  966. * with RFC3280.
  967. */
  968. ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
  969. {
  970. unsigned char ipout[16];
  971. ASN1_OCTET_STRING *ret;
  972. int iplen;
  973. /* If string contains a ':' assume IPv6 */
  974. iplen = a2i_ipadd(ipout, ipasc);
  975. if (!iplen)
  976. return NULL;
  977. ret = ASN1_OCTET_STRING_new();
  978. if (!ret)
  979. return NULL;
  980. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
  981. ASN1_OCTET_STRING_free(ret);
  982. return NULL;
  983. }
  984. return ret;
  985. }
  986. ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
  987. {
  988. ASN1_OCTET_STRING *ret = NULL;
  989. unsigned char ipout[32];
  990. char *iptmp = NULL, *p;
  991. int iplen1, iplen2;
  992. p = strchr(ipasc, '/');
  993. if (!p)
  994. return NULL;
  995. iptmp = BUF_strdup(ipasc);
  996. if (!iptmp)
  997. return NULL;
  998. p = iptmp + (p - ipasc);
  999. *p++ = 0;
  1000. iplen1 = a2i_ipadd(ipout, iptmp);
  1001. if (!iplen1)
  1002. goto err;
  1003. iplen2 = a2i_ipadd(ipout + iplen1, p);
  1004. OPENSSL_free(iptmp);
  1005. iptmp = NULL;
  1006. if (!iplen2 || (iplen1 != iplen2))
  1007. goto err;
  1008. ret = ASN1_OCTET_STRING_new();
  1009. if (!ret)
  1010. goto err;
  1011. if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
  1012. goto err;
  1013. return ret;
  1014. err:
  1015. if (iptmp)
  1016. OPENSSL_free(iptmp);
  1017. if (ret)
  1018. ASN1_OCTET_STRING_free(ret);
  1019. return NULL;
  1020. }
  1021. int a2i_ipadd(unsigned char *ipout, const char *ipasc)
  1022. {
  1023. /* If string contains a ':' assume IPv6 */
  1024. if (strchr(ipasc, ':')) {
  1025. if (!ipv6_from_asc(ipout, ipasc))
  1026. return 0;
  1027. return 16;
  1028. } else {
  1029. if (!ipv4_from_asc(ipout, ipasc))
  1030. return 0;
  1031. return 4;
  1032. }
  1033. }
  1034. static int ipv4_from_asc(unsigned char *v4, const char *in)
  1035. {
  1036. int a0, a1, a2, a3;
  1037. if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
  1038. return 0;
  1039. if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
  1040. || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
  1041. return 0;
  1042. v4[0] = a0;
  1043. v4[1] = a1;
  1044. v4[2] = a2;
  1045. v4[3] = a3;
  1046. return 1;
  1047. }
  1048. typedef struct {
  1049. /* Temporary store for IPV6 output */
  1050. unsigned char tmp[16];
  1051. /* Total number of bytes in tmp */
  1052. int total;
  1053. /* The position of a zero (corresponding to '::') */
  1054. int zero_pos;
  1055. /* Number of zeroes */
  1056. int zero_cnt;
  1057. } IPV6_STAT;
  1058. static int ipv6_from_asc(unsigned char *v6, const char *in)
  1059. {
  1060. IPV6_STAT v6stat;
  1061. v6stat.total = 0;
  1062. v6stat.zero_pos = -1;
  1063. v6stat.zero_cnt = 0;
  1064. /*
  1065. * Treat the IPv6 representation as a list of values separated by ':'.
  1066. * The presence of a '::' will parse as one, two or three zero length
  1067. * elements.
  1068. */
  1069. if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
  1070. return 0;
  1071. /* Now for some sanity checks */
  1072. if (v6stat.zero_pos == -1) {
  1073. /* If no '::' must have exactly 16 bytes */
  1074. if (v6stat.total != 16)
  1075. return 0;
  1076. } else {
  1077. /* If '::' must have less than 16 bytes */
  1078. if (v6stat.total == 16)
  1079. return 0;
  1080. /* More than three zeroes is an error */
  1081. if (v6stat.zero_cnt > 3)
  1082. return 0;
  1083. /* Can only have three zeroes if nothing else present */
  1084. else if (v6stat.zero_cnt == 3) {
  1085. if (v6stat.total > 0)
  1086. return 0;
  1087. }
  1088. /* Can only have two zeroes if at start or end */
  1089. else if (v6stat.zero_cnt == 2) {
  1090. if ((v6stat.zero_pos != 0)
  1091. && (v6stat.zero_pos != v6stat.total))
  1092. return 0;
  1093. } else
  1094. /* Can only have one zero if *not* start or end */
  1095. {
  1096. if ((v6stat.zero_pos == 0)
  1097. || (v6stat.zero_pos == v6stat.total))
  1098. return 0;
  1099. }
  1100. }
  1101. /* Format result */
  1102. if (v6stat.zero_pos >= 0) {
  1103. /* Copy initial part */
  1104. memcpy(v6, v6stat.tmp, v6stat.zero_pos);
  1105. /* Zero middle */
  1106. memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
  1107. /* Copy final part */
  1108. if (v6stat.total != v6stat.zero_pos)
  1109. memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
  1110. v6stat.tmp + v6stat.zero_pos,
  1111. v6stat.total - v6stat.zero_pos);
  1112. } else
  1113. memcpy(v6, v6stat.tmp, 16);
  1114. return 1;
  1115. }
  1116. static int ipv6_cb(const char *elem, int len, void *usr)
  1117. {
  1118. IPV6_STAT *s = usr;
  1119. /* Error if 16 bytes written */
  1120. if (s->total == 16)
  1121. return 0;
  1122. if (len == 0) {
  1123. /* Zero length element, corresponds to '::' */
  1124. if (s->zero_pos == -1)
  1125. s->zero_pos = s->total;
  1126. /* If we've already got a :: its an error */
  1127. else if (s->zero_pos != s->total)
  1128. return 0;
  1129. s->zero_cnt++;
  1130. } else {
  1131. /* If more than 4 characters could be final a.b.c.d form */
  1132. if (len > 4) {
  1133. /* Need at least 4 bytes left */
  1134. if (s->total > 12)
  1135. return 0;
  1136. /* Must be end of string */
  1137. if (elem[len])
  1138. return 0;
  1139. if (!ipv4_from_asc(s->tmp + s->total, elem))
  1140. return 0;
  1141. s->total += 4;
  1142. } else {
  1143. if (!ipv6_hex(s->tmp + s->total, elem, len))
  1144. return 0;
  1145. s->total += 2;
  1146. }
  1147. }
  1148. return 1;
  1149. }
  1150. /*
  1151. * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
  1152. */
  1153. static int ipv6_hex(unsigned char *out, const char *in, int inlen)
  1154. {
  1155. unsigned char c;
  1156. unsigned int num = 0;
  1157. if (inlen > 4)
  1158. return 0;
  1159. while (inlen--) {
  1160. c = *in++;
  1161. num <<= 4;
  1162. if ((c >= '0') && (c <= '9'))
  1163. num |= c - '0';
  1164. else if ((c >= 'A') && (c <= 'F'))
  1165. num |= c - 'A' + 10;
  1166. else if ((c >= 'a') && (c <= 'f'))
  1167. num |= c - 'a' + 10;
  1168. else
  1169. return 0;
  1170. }
  1171. out[0] = num >> 8;
  1172. out[1] = num & 0xff;
  1173. return 1;
  1174. }
  1175. int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF (CONF_VALUE) * dn_sk,
  1176. unsigned long chtype)
  1177. {
  1178. CONF_VALUE *v;
  1179. int mval;
  1180. size_t i;
  1181. char *p, *type;
  1182. if (!nm)
  1183. return 0;
  1184. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  1185. v = sk_CONF_VALUE_value(dn_sk, i);
  1186. type = v->name;
  1187. /*
  1188. * Skip past any leading X. X: X, etc to allow for multiple instances
  1189. */
  1190. for (p = type; *p; p++)
  1191. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1192. p++;
  1193. if (*p)
  1194. type = p;
  1195. break;
  1196. }
  1197. if (*type == '+') {
  1198. mval = -1;
  1199. type++;
  1200. } else
  1201. mval = 0;
  1202. if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
  1203. (unsigned char *)v->value, -1, -1,
  1204. mval))
  1205. return 0;
  1206. }
  1207. return 1;
  1208. }