Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

555 righe
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <ctype.h>
  57. #include <string.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/asn1t.h>
  60. #include <openssl/buf.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/obj.h>
  64. #include <openssl/stack.h>
  65. #include <openssl/x509.h>
  66. #include "../asn1/asn1_locl.h"
  67. #include "../internal.h"
  68. typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
  69. DEFINE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
  70. /*
  71. * Maximum length of X509_NAME: much larger than anything we should
  72. * ever see in practice.
  73. */
  74. #define X509_NAME_MAX (1024 * 1024)
  75. static int x509_name_ex_d2i(ASN1_VALUE **val,
  76. const unsigned char **in, long len,
  77. const ASN1_ITEM *it,
  78. int tag, int aclass, char opt, ASN1_TLC *ctx);
  79. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
  80. const ASN1_ITEM *it, int tag, int aclass);
  81. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
  82. static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
  83. static int x509_name_encode(X509_NAME *a);
  84. static int x509_name_canon(X509_NAME *a);
  85. static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
  86. static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
  87. unsigned char **in);
  88. ASN1_SEQUENCE(X509_NAME_ENTRY) = {
  89. ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
  90. ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
  91. } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
  92. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
  93. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
  94. /*
  95. * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
  96. * declare two template wrappers for this
  97. */
  98. ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
  99. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
  100. ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
  101. ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
  102. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
  103. ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
  104. /*
  105. * Normally that's where it would end: we'd have two nested STACK structures
  106. * representing the ASN1. Unfortunately X509_NAME uses a completely different
  107. * form and caches encodings so we have to process the internal form and
  108. * convert to the external form.
  109. */
  110. static const ASN1_EXTERN_FUNCS x509_name_ff = {
  111. NULL,
  112. x509_name_ex_new,
  113. x509_name_ex_free,
  114. 0, /* Default clear behaviour is OK */
  115. x509_name_ex_d2i,
  116. x509_name_ex_i2d,
  117. NULL,
  118. };
  119. IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
  120. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
  121. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
  122. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
  123. {
  124. X509_NAME *ret = NULL;
  125. ret = OPENSSL_malloc(sizeof(X509_NAME));
  126. if (!ret)
  127. goto memerr;
  128. if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
  129. goto memerr;
  130. if ((ret->bytes = BUF_MEM_new()) == NULL)
  131. goto memerr;
  132. ret->canon_enc = NULL;
  133. ret->canon_enclen = 0;
  134. ret->modified = 1;
  135. *val = (ASN1_VALUE *)ret;
  136. return 1;
  137. memerr:
  138. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  139. if (ret) {
  140. if (ret->entries)
  141. sk_X509_NAME_ENTRY_free(ret->entries);
  142. OPENSSL_free(ret);
  143. }
  144. return 0;
  145. }
  146. static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  147. {
  148. X509_NAME *a;
  149. if (!pval || !*pval)
  150. return;
  151. a = (X509_NAME *)*pval;
  152. BUF_MEM_free(a->bytes);
  153. sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
  154. if (a->canon_enc)
  155. OPENSSL_free(a->canon_enc);
  156. OPENSSL_free(a);
  157. *pval = NULL;
  158. }
  159. static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
  160. {
  161. sk_X509_NAME_ENTRY_free(ne);
  162. }
  163. static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
  164. {
  165. sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
  166. }
  167. static int x509_name_ex_d2i(ASN1_VALUE **val,
  168. const unsigned char **in, long len,
  169. const ASN1_ITEM *it, int tag, int aclass,
  170. char opt, ASN1_TLC *ctx)
  171. {
  172. const unsigned char *p = *in, *q;
  173. union {
  174. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  175. ASN1_VALUE *a;
  176. } intname = {
  177. NULL
  178. };
  179. union {
  180. X509_NAME *x;
  181. ASN1_VALUE *a;
  182. } nm = {
  183. NULL
  184. };
  185. size_t i, j;
  186. int ret;
  187. STACK_OF(X509_NAME_ENTRY) *entries;
  188. X509_NAME_ENTRY *entry;
  189. /* Bound the size of an X509_NAME we are willing to parse. */
  190. if (len > X509_NAME_MAX) {
  191. len = X509_NAME_MAX;
  192. }
  193. q = p;
  194. /* Get internal representation of Name */
  195. ret = ASN1_item_ex_d2i(&intname.a,
  196. &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
  197. tag, aclass, opt, ctx);
  198. if (ret <= 0)
  199. return ret;
  200. if (*val)
  201. x509_name_ex_free(val, NULL);
  202. if (!x509_name_ex_new(&nm.a, NULL))
  203. goto err;
  204. /* We've decoded it: now cache encoding */
  205. if (!BUF_MEM_grow(nm.x->bytes, p - q))
  206. goto err;
  207. OPENSSL_memcpy(nm.x->bytes->data, q, p - q);
  208. /* Convert internal representation to X509_NAME structure */
  209. for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
  210. entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
  211. for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
  212. entry = sk_X509_NAME_ENTRY_value(entries, j);
  213. entry->set = i;
  214. if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
  215. goto err;
  216. (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
  217. }
  218. }
  219. ret = x509_name_canon(nm.x);
  220. if (!ret)
  221. goto err;
  222. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  223. local_sk_X509_NAME_ENTRY_free);
  224. nm.x->modified = 0;
  225. *val = nm.a;
  226. *in = p;
  227. return ret;
  228. err:
  229. if (nm.x != NULL)
  230. X509_NAME_free(nm.x);
  231. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  232. local_sk_X509_NAME_ENTRY_pop_free);
  233. OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
  234. return 0;
  235. }
  236. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
  237. const ASN1_ITEM *it, int tag, int aclass)
  238. {
  239. int ret;
  240. X509_NAME *a = (X509_NAME *)*val;
  241. if (a->modified) {
  242. ret = x509_name_encode(a);
  243. if (ret < 0)
  244. return ret;
  245. ret = x509_name_canon(a);
  246. if (ret < 0)
  247. return ret;
  248. }
  249. ret = a->bytes->length;
  250. if (out != NULL) {
  251. OPENSSL_memcpy(*out, a->bytes->data, ret);
  252. *out += ret;
  253. }
  254. return ret;
  255. }
  256. static int x509_name_encode(X509_NAME *a)
  257. {
  258. union {
  259. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  260. ASN1_VALUE *a;
  261. } intname = {
  262. NULL
  263. };
  264. int len;
  265. unsigned char *p;
  266. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  267. X509_NAME_ENTRY *entry;
  268. int set = -1;
  269. size_t i;
  270. intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  271. if (!intname.s)
  272. goto memerr;
  273. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  274. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  275. if (entry->set != set) {
  276. entries = sk_X509_NAME_ENTRY_new_null();
  277. if (!entries)
  278. goto memerr;
  279. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
  280. sk_X509_NAME_ENTRY_free(entries);
  281. goto memerr;
  282. }
  283. set = entry->set;
  284. }
  285. if (!sk_X509_NAME_ENTRY_push(entries, entry))
  286. goto memerr;
  287. }
  288. len = ASN1_item_ex_i2d(&intname.a, NULL,
  289. ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  290. if (!BUF_MEM_grow(a->bytes, len))
  291. goto memerr;
  292. p = (unsigned char *)a->bytes->data;
  293. ASN1_item_ex_i2d(&intname.a,
  294. &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  295. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  296. local_sk_X509_NAME_ENTRY_free);
  297. a->modified = 0;
  298. return len;
  299. memerr:
  300. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  301. local_sk_X509_NAME_ENTRY_free);
  302. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  303. return -1;
  304. }
  305. /*
  306. * This function generates the canonical encoding of the Name structure. In
  307. * it all strings are converted to UTF8, leading, trailing and multiple
  308. * spaces collapsed, converted to lower case and the leading SEQUENCE header
  309. * removed. In future we could also normalize the UTF8 too. By doing this
  310. * comparison of Name structures can be rapidly perfomed by just using
  311. * OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
  312. * constraints of type dirName can also be checked with a simple OPENSSL_memcmp().
  313. */
  314. static int x509_name_canon(X509_NAME *a)
  315. {
  316. unsigned char *p;
  317. STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
  318. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  319. X509_NAME_ENTRY *entry, *tmpentry = NULL;
  320. int set = -1, ret = 0, len;
  321. size_t i;
  322. if (a->canon_enc) {
  323. OPENSSL_free(a->canon_enc);
  324. a->canon_enc = NULL;
  325. }
  326. /* Special case: empty X509_NAME => null encoding */
  327. if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
  328. a->canon_enclen = 0;
  329. return 1;
  330. }
  331. intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  332. if (!intname)
  333. goto err;
  334. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  335. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  336. if (entry->set != set) {
  337. entries = sk_X509_NAME_ENTRY_new_null();
  338. if (!entries)
  339. goto err;
  340. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
  341. sk_X509_NAME_ENTRY_free(entries);
  342. goto err;
  343. }
  344. set = entry->set;
  345. }
  346. tmpentry = X509_NAME_ENTRY_new();
  347. if (tmpentry == NULL)
  348. goto err;
  349. tmpentry->object = OBJ_dup(entry->object);
  350. if (!asn1_string_canon(tmpentry->value, entry->value))
  351. goto err;
  352. if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
  353. goto err;
  354. tmpentry = NULL;
  355. }
  356. /* Finally generate encoding */
  357. len = i2d_name_canon(intname, NULL);
  358. if (len < 0) {
  359. goto err;
  360. }
  361. a->canon_enclen = len;
  362. p = OPENSSL_malloc(a->canon_enclen);
  363. if (!p)
  364. goto err;
  365. a->canon_enc = p;
  366. i2d_name_canon(intname, &p);
  367. ret = 1;
  368. err:
  369. if (tmpentry)
  370. X509_NAME_ENTRY_free(tmpentry);
  371. if (intname)
  372. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
  373. local_sk_X509_NAME_ENTRY_pop_free);
  374. return ret;
  375. }
  376. /* Bitmap of all the types of string that will be canonicalized. */
  377. #define ASN1_MASK_CANON \
  378. (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
  379. | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
  380. | B_ASN1_VISIBLESTRING)
  381. static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
  382. {
  383. unsigned char *to, *from;
  384. int len, i;
  385. /* If type not in bitmask just copy string across */
  386. if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
  387. if (!ASN1_STRING_copy(out, in))
  388. return 0;
  389. return 1;
  390. }
  391. out->type = V_ASN1_UTF8STRING;
  392. out->length = ASN1_STRING_to_UTF8(&out->data, in);
  393. if (out->length == -1)
  394. return 0;
  395. to = out->data;
  396. from = to;
  397. len = out->length;
  398. /*
  399. * Convert string in place to canonical form. Ultimately we may need to
  400. * handle a wider range of characters but for now ignore anything with
  401. * MSB set and rely on the isspace() and tolower() functions.
  402. */
  403. /* Ignore leading spaces */
  404. while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
  405. from++;
  406. len--;
  407. }
  408. to = from + len;
  409. /* Ignore trailing spaces */
  410. while ((len > 0) && !(to[-1] & 0x80) && isspace(to[-1])) {
  411. to--;
  412. len--;
  413. }
  414. to = out->data;
  415. i = 0;
  416. while (i < len) {
  417. /* If MSB set just copy across */
  418. if (*from & 0x80) {
  419. *to++ = *from++;
  420. i++;
  421. }
  422. /* Collapse multiple spaces */
  423. else if (isspace(*from)) {
  424. /* Copy one space across */
  425. *to++ = ' ';
  426. /*
  427. * Ignore subsequent spaces. Note: don't need to check len here
  428. * because we know the last character is a non-space so we can't
  429. * overflow.
  430. */
  431. do {
  432. from++;
  433. i++;
  434. }
  435. while (!(*from & 0x80) && isspace(*from));
  436. } else {
  437. *to++ = OPENSSL_tolower(*from);
  438. from++;
  439. i++;
  440. }
  441. }
  442. out->length = to - out->data;
  443. return 1;
  444. }
  445. static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
  446. unsigned char **in)
  447. {
  448. int len, ltmp;
  449. size_t i;
  450. ASN1_VALUE *v;
  451. STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
  452. len = 0;
  453. for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
  454. v = sk_ASN1_VALUE_value(intname, i);
  455. ltmp = ASN1_item_ex_i2d(&v, in,
  456. ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
  457. if (ltmp < 0)
  458. return ltmp;
  459. len += ltmp;
  460. }
  461. return len;
  462. }
  463. int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
  464. {
  465. if ((name = X509_NAME_dup(name)) == NULL)
  466. return 0;
  467. X509_NAME_free(*xn);
  468. *xn = name;
  469. return 1;
  470. }
  471. IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)
  472. int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
  473. {
  474. return ne->set;
  475. }
  476. int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder,
  477. size_t *pderlen)
  478. {
  479. /* Make sure encoding is valid */
  480. if (i2d_X509_NAME(nm, NULL) <= 0)
  481. return 0;
  482. if (pder != NULL)
  483. *pder = (unsigned char *)nm->bytes->data;
  484. if (pderlen != NULL)
  485. *pderlen = nm->bytes->length;
  486. return 1;
  487. }