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.
 
 
 
 
 
 

1314 líneas
32 KiB

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