2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include "internal/x509_int.h"
16 #include "internal/asn1_int.h"
20 * Maximum length of X509_NAME: much larger than anything we should
21 * ever see in practice.
24 #define X509_NAME_MAX (1024 * 1024)
26 static int x509_name_ex_d2i(ASN1_VALUE **val,
27 const unsigned char **in, long len,
29 int tag, int aclass, char opt, ASN1_TLC *ctx);
31 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
32 const ASN1_ITEM *it, int tag, int aclass);
33 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
34 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
36 static int x509_name_encode(X509_NAME *a);
37 static int x509_name_canon(X509_NAME *a);
38 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
39 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
42 static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
44 const char *fname, const ASN1_PCTX *pctx);
46 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
47 ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
48 ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
49 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
51 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
52 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
55 * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
56 * declare two template wrappers for this
59 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
60 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
61 static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
63 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
64 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
65 static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
68 * Normally that's where it would end: we'd have two nested STACK structures
69 * representing the ASN1. Unfortunately X509_NAME uses a completely different
70 * form and caches encodings so we have to process the internal form and
71 * convert to the external form.
74 static const ASN1_EXTERN_FUNCS x509_name_ff = {
78 0, /* Default clear behaviour is OK */
84 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
86 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
88 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
90 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
92 X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret));
96 if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
98 if ((ret->bytes = BUF_MEM_new()) == NULL)
101 *val = (ASN1_VALUE *)ret;
105 ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
107 sk_X509_NAME_ENTRY_free(ret->entries);
113 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
119 a = (X509_NAME *)*pval;
121 BUF_MEM_free(a->bytes);
122 sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
123 OPENSSL_free(a->canon_enc);
128 static int x509_name_ex_d2i(ASN1_VALUE **val,
129 const unsigned char **in, long len,
130 const ASN1_ITEM *it, int tag, int aclass,
131 char opt, ASN1_TLC *ctx)
133 const unsigned char *p = *in, *q;
135 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
147 STACK_OF(X509_NAME_ENTRY) *entries;
148 X509_NAME_ENTRY *entry;
149 if (len > X509_NAME_MAX)
153 /* Get internal representation of Name */
154 ret = ASN1_item_ex_d2i(&intname.a,
155 &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
156 tag, aclass, opt, ctx);
162 x509_name_ex_free(val, NULL);
163 if (!x509_name_ex_new(&nm.a, NULL))
165 /* We've decoded it: now cache encoding */
166 if (!BUF_MEM_grow(nm.x->bytes, p - q))
168 memcpy(nm.x->bytes->data, q, p - q);
170 /* Convert internal representation to X509_NAME structure */
171 for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
172 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
173 for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
174 entry = sk_X509_NAME_ENTRY_value(entries, j);
176 if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
179 sk_X509_NAME_ENTRY_free(entries);
181 sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
182 ret = x509_name_canon(nm.x);
190 X509_NAME_free(nm.x);
191 ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
195 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
196 const ASN1_ITEM *it, int tag, int aclass)
199 X509_NAME *a = (X509_NAME *)*val;
201 ret = x509_name_encode(a);
204 ret = x509_name_canon(a);
208 ret = a->bytes->length;
210 memcpy(*out, a->bytes->data, ret);
216 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
218 sk_X509_NAME_ENTRY_free(ne);
221 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
223 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
226 static int x509_name_encode(X509_NAME *a)
229 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
236 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
237 X509_NAME_ENTRY *entry;
239 intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
242 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
243 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
244 if (entry->set != set) {
245 entries = sk_X509_NAME_ENTRY_new_null();
248 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries))
252 if (!sk_X509_NAME_ENTRY_push(entries, entry))
255 len = ASN1_item_ex_i2d(&intname.a, NULL,
256 ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
257 if (!BUF_MEM_grow(a->bytes, len))
259 p = (unsigned char *)a->bytes->data;
260 ASN1_item_ex_i2d(&intname.a,
261 &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
262 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
263 local_sk_X509_NAME_ENTRY_free);
267 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
268 local_sk_X509_NAME_ENTRY_free);
269 ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
273 static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
275 const char *fname, const ASN1_PCTX *pctx)
277 if (X509_NAME_print_ex(out, (X509_NAME *)*pval,
278 indent, pctx->nm_flags) <= 0)
284 * This function generates the canonical encoding of the Name structure. In
285 * it all strings are converted to UTF8, leading, trailing and multiple
286 * spaces collapsed, converted to lower case and the leading SEQUENCE header
287 * removed. In future we could also normalize the UTF8 too. By doing this
288 * comparison of Name structures can be rapidly performed by just using
289 * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
290 * constraints of type dirName can also be checked with a simple memcmp().
293 static int x509_name_canon(X509_NAME *a)
296 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
297 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
298 X509_NAME_ENTRY *entry, *tmpentry = NULL;
299 int i, set = -1, ret = 0, len;
301 OPENSSL_free(a->canon_enc);
303 /* Special case: empty X509_NAME => null encoding */
304 if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
308 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
311 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
312 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
313 if (entry->set != set) {
314 entries = sk_X509_NAME_ENTRY_new_null();
317 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries))
321 tmpentry = X509_NAME_ENTRY_new();
322 if (tmpentry == NULL)
324 tmpentry->object = OBJ_dup(entry->object);
325 if (!asn1_string_canon(tmpentry->value, entry->value))
327 if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
332 /* Finally generate encoding */
334 len = i2d_name_canon(intname, NULL);
337 a->canon_enclen = len;
339 p = OPENSSL_malloc(a->canon_enclen);
346 i2d_name_canon(intname, &p);
352 X509_NAME_ENTRY_free(tmpentry);
353 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
354 local_sk_X509_NAME_ENTRY_pop_free);
358 /* Bitmap of all the types of string that will be canonicalized. */
360 #define ASN1_MASK_CANON \
361 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
362 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
363 | B_ASN1_VISIBLESTRING)
365 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
367 unsigned char *to, *from;
370 /* If type not in bitmask just copy string across */
371 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
372 if (!ASN1_STRING_copy(out, in))
377 out->type = V_ASN1_UTF8STRING;
378 out->length = ASN1_STRING_to_UTF8(&out->data, in);
379 if (out->length == -1)
388 * Convert string in place to canonical form. Ultimately we may need to
389 * handle a wider range of characters but for now ignore anything with
390 * MSB set and rely on the isspace() and tolower() functions.
393 /* Ignore leading spaces */
394 while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
401 /* Ignore trailing spaces */
402 while ((len > 0) && !(*to & 0x80) && isspace(*to)) {
411 /* If MSB set just copy across */
416 /* Collapse multiple spaces */
417 else if (isspace(*from)) {
418 /* Copy one space across */
421 * Ignore subsequent spaces. Note: don't need to check len here
422 * because we know the last character is a non-space so we can't
429 while (!(*from & 0x80) && isspace(*from));
431 *to++ = tolower(*from);
437 out->length = to - out->data;
443 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
448 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
451 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
452 v = sk_ASN1_VALUE_value(intname, i);
453 ltmp = ASN1_item_ex_i2d(&v, in,
454 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
462 int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
470 in = X509_NAME_dup(name);
476 return (*xn != NULL);
479 int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
486 b = X509_NAME_oneline(name, NULL, 0);
493 s = b + 1; /* skip the first slash */
497 #ifndef CHARSET_EBCDIC
499 ((s[1] >= 'A') && (s[1] <= 'Z') && ((s[2] == '=') ||
506 (isupper(s[1]) && ((s[2] == '=') ||
507 (isupper(s[2]) && (s[3] == '='))
512 if (BIO_write(bp, c, i) != i)
514 c = s + 1; /* skip following slash */
516 if (BIO_write(bp, ", ", 2) != 2)
530 X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB);
535 int X509_NAME_get0_der(const unsigned char **pder, size_t *pderlen,
538 /* Make sure encoding is valid */
539 if (i2d_X509_NAME(nm, NULL) <= 0)
542 *pder = (unsigned char *)nm->bytes->data;
544 *pderlen = nm->bytes->length;