2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "crypto/ctype.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include "crypto/x509.h"
16 #include "crypto/asn1.h"
17 #include "x509_local.h"
19 DEFINE_STACK_OF(X509_NAME_ENTRY
)
20 DEFINE_STACK_OF(ASN1_VALUE
)
23 * Maximum length of X509_NAME: much larger than anything we should
24 * ever see in practice.
27 #define X509_NAME_MAX (1024 * 1024)
29 static int x509_name_ex_d2i(ASN1_VALUE
**val
,
30 const unsigned char **in
, long len
,
32 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
);
34 static int x509_name_ex_i2d(const ASN1_VALUE
**val
, unsigned char **out
,
35 const ASN1_ITEM
*it
, int tag
, int aclass
);
36 static int x509_name_ex_new(ASN1_VALUE
**val
, const ASN1_ITEM
*it
);
37 static void x509_name_ex_free(ASN1_VALUE
**val
, const ASN1_ITEM
*it
);
39 static int x509_name_encode(X509_NAME
*a
);
40 static int x509_name_canon(X509_NAME
*a
);
41 static int asn1_string_canon(ASN1_STRING
*out
, const ASN1_STRING
*in
);
42 static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY
) * intname
,
45 static int x509_name_ex_print(BIO
*out
, const ASN1_VALUE
**pval
,
47 const char *fname
, const ASN1_PCTX
*pctx
);
49 ASN1_SEQUENCE(X509_NAME_ENTRY
) = {
50 ASN1_SIMPLE(X509_NAME_ENTRY
, object
, ASN1_OBJECT
),
51 ASN1_SIMPLE(X509_NAME_ENTRY
, value
, ASN1_PRINTABLE
)
52 } ASN1_SEQUENCE_END(X509_NAME_ENTRY
)
54 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY
)
55 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY
)
58 * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
59 * declare two template wrappers for this
62 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES
) =
63 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF
, 0, RDNS
, X509_NAME_ENTRY
)
64 static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES
)
66 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL
) =
67 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF
, 0, Name
, X509_NAME_ENTRIES
)
68 static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL
)
71 * Normally that's where it would end: we'd have two nested STACK structures
72 * representing the ASN1. Unfortunately X509_NAME uses a completely different
73 * form and caches encodings so we have to process the internal form and
74 * convert to the external form.
77 static const ASN1_EXTERN_FUNCS x509_name_ff
= {
81 0, /* Default clear behaviour is OK */
87 IMPLEMENT_EXTERN_ASN1(X509_NAME
, V_ASN1_SEQUENCE
, x509_name_ff
)
89 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME
)
91 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME
)
93 static int x509_name_ex_new(ASN1_VALUE
**val
, const ASN1_ITEM
*it
)
95 X509_NAME
*ret
= OPENSSL_zalloc(sizeof(*ret
));
99 if ((ret
->entries
= sk_X509_NAME_ENTRY_new_null()) == NULL
)
101 if ((ret
->bytes
= BUF_MEM_new()) == NULL
)
104 *val
= (ASN1_VALUE
*)ret
;
108 ASN1err(ASN1_F_X509_NAME_EX_NEW
, ERR_R_MALLOC_FAILURE
);
110 sk_X509_NAME_ENTRY_free(ret
->entries
);
116 static void x509_name_ex_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
120 if (pval
== NULL
|| *pval
== NULL
)
122 a
= (X509_NAME
*)*pval
;
124 BUF_MEM_free(a
->bytes
);
125 sk_X509_NAME_ENTRY_pop_free(a
->entries
, X509_NAME_ENTRY_free
);
126 OPENSSL_free(a
->canon_enc
);
131 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY
) *ne
)
133 sk_X509_NAME_ENTRY_free(ne
);
136 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY
) *ne
)
138 sk_X509_NAME_ENTRY_pop_free(ne
, X509_NAME_ENTRY_free
);
141 static int x509_name_ex_d2i(ASN1_VALUE
**val
,
142 const unsigned char **in
, long len
,
143 const ASN1_ITEM
*it
, int tag
, int aclass
,
144 char opt
, ASN1_TLC
*ctx
)
146 const unsigned char *p
= *in
, *q
;
148 STACK_OF(STACK_OF_X509_NAME_ENTRY
) *s
;
160 STACK_OF(X509_NAME_ENTRY
) *entries
;
161 X509_NAME_ENTRY
*entry
;
163 if (len
> X509_NAME_MAX
)
167 /* Get internal representation of Name */
168 ret
= ASN1_item_ex_d2i(&intname
.a
,
169 &p
, len
, ASN1_ITEM_rptr(X509_NAME_INTERNAL
),
170 tag
, aclass
, opt
, ctx
);
176 x509_name_ex_free(val
, NULL
);
177 if (!x509_name_ex_new(&nm
.a
, NULL
))
179 /* We've decoded it: now cache encoding */
180 if (!BUF_MEM_grow(nm
.x
->bytes
, p
- q
))
182 memcpy(nm
.x
->bytes
->data
, q
, p
- q
);
184 /* Convert internal representation to X509_NAME structure */
185 for (i
= 0; i
< sk_STACK_OF_X509_NAME_ENTRY_num(intname
.s
); i
++) {
186 entries
= sk_STACK_OF_X509_NAME_ENTRY_value(intname
.s
, i
);
187 for (j
= 0; j
< sk_X509_NAME_ENTRY_num(entries
); j
++) {
188 entry
= sk_X509_NAME_ENTRY_value(entries
, j
);
190 if (!sk_X509_NAME_ENTRY_push(nm
.x
->entries
, entry
))
192 sk_X509_NAME_ENTRY_set(entries
, j
, NULL
);
195 ret
= x509_name_canon(nm
.x
);
198 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname
.s
,
199 local_sk_X509_NAME_ENTRY_free
);
207 X509_NAME_free(nm
.x
);
208 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname
.s
,
209 local_sk_X509_NAME_ENTRY_pop_free
);
210 ASN1err(ASN1_F_X509_NAME_EX_D2I
, ERR_R_NESTED_ASN1_ERROR
);
214 static int x509_name_ex_i2d(const ASN1_VALUE
**val
, unsigned char **out
,
215 const ASN1_ITEM
*it
, int tag
, int aclass
)
218 X509_NAME
*a
= (X509_NAME
*)*val
;
221 ret
= x509_name_encode(a
);
224 ret
= x509_name_canon(a
);
228 ret
= a
->bytes
->length
;
230 memcpy(*out
, a
->bytes
->data
, ret
);
236 static int x509_name_encode(X509_NAME
*a
)
239 STACK_OF(STACK_OF_X509_NAME_ENTRY
) *s
;
246 STACK_OF(X509_NAME_ENTRY
) *entries
= NULL
;
247 X509_NAME_ENTRY
*entry
;
250 intname
.s
= sk_STACK_OF_X509_NAME_ENTRY_new_null();
253 for (i
= 0; i
< sk_X509_NAME_ENTRY_num(a
->entries
); i
++) {
254 entry
= sk_X509_NAME_ENTRY_value(a
->entries
, i
);
255 if (entry
->set
!= set
) {
256 entries
= sk_X509_NAME_ENTRY_new_null();
259 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname
.s
, entries
)) {
260 sk_X509_NAME_ENTRY_free(entries
);
265 if (!sk_X509_NAME_ENTRY_push(entries
, entry
))
268 len
= ASN1_item_ex_i2d(&intname
.a
, NULL
,
269 ASN1_ITEM_rptr(X509_NAME_INTERNAL
), -1, -1);
270 if (!BUF_MEM_grow(a
->bytes
, len
))
272 p
= (unsigned char *)a
->bytes
->data
;
273 ASN1_item_ex_i2d(&intname
.a
,
274 &p
, ASN1_ITEM_rptr(X509_NAME_INTERNAL
), -1, -1);
275 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname
.s
,
276 local_sk_X509_NAME_ENTRY_free
);
280 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname
.s
,
281 local_sk_X509_NAME_ENTRY_free
);
282 ASN1err(ASN1_F_X509_NAME_ENCODE
, ERR_R_MALLOC_FAILURE
);
286 static int x509_name_ex_print(BIO
*out
, const ASN1_VALUE
**pval
,
288 const char *fname
, const ASN1_PCTX
*pctx
)
290 if (X509_NAME_print_ex(out
, (const X509_NAME
*)*pval
,
291 indent
, pctx
->nm_flags
) <= 0)
297 * This function generates the canonical encoding of the Name structure. In
298 * it all strings are converted to UTF8, leading, trailing and multiple
299 * spaces collapsed, converted to lower case and the leading SEQUENCE header
300 * removed. In future we could also normalize the UTF8 too. By doing this
301 * comparison of Name structures can be rapidly performed by just using
302 * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
303 * constraints of type dirName can also be checked with a simple memcmp().
306 static int x509_name_canon(X509_NAME
*a
)
309 STACK_OF(STACK_OF_X509_NAME_ENTRY
) *intname
;
310 STACK_OF(X509_NAME_ENTRY
) *entries
= NULL
;
311 X509_NAME_ENTRY
*entry
, *tmpentry
= NULL
;
312 int i
, set
= -1, ret
= 0, len
;
314 OPENSSL_free(a
->canon_enc
);
316 /* Special case: empty X509_NAME => null encoding */
317 if (sk_X509_NAME_ENTRY_num(a
->entries
) == 0) {
321 intname
= sk_STACK_OF_X509_NAME_ENTRY_new_null();
322 if (intname
== NULL
) {
323 X509err(X509_F_X509_NAME_CANON
, ERR_R_MALLOC_FAILURE
);
326 for (i
= 0; i
< sk_X509_NAME_ENTRY_num(a
->entries
); i
++) {
327 entry
= sk_X509_NAME_ENTRY_value(a
->entries
, i
);
328 if (entry
->set
!= set
) {
329 entries
= sk_X509_NAME_ENTRY_new_null();
332 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname
, entries
)) {
333 sk_X509_NAME_ENTRY_free(entries
);
334 X509err(X509_F_X509_NAME_CANON
, ERR_R_MALLOC_FAILURE
);
339 tmpentry
= X509_NAME_ENTRY_new();
340 if (tmpentry
== NULL
) {
341 X509err(X509_F_X509_NAME_CANON
, ERR_R_MALLOC_FAILURE
);
344 tmpentry
->object
= OBJ_dup(entry
->object
);
345 if (tmpentry
->object
== NULL
) {
346 X509err(X509_F_X509_NAME_CANON
, ERR_R_MALLOC_FAILURE
);
349 if (!asn1_string_canon(tmpentry
->value
, entry
->value
))
351 if (!sk_X509_NAME_ENTRY_push(entries
, tmpentry
)) {
352 X509err(X509_F_X509_NAME_CANON
, ERR_R_MALLOC_FAILURE
);
358 /* Finally generate encoding */
359 len
= i2d_name_canon(intname
, NULL
);
362 a
->canon_enclen
= len
;
364 p
= OPENSSL_malloc(a
->canon_enclen
);
366 X509err(X509_F_X509_NAME_CANON
, ERR_R_MALLOC_FAILURE
);
372 i2d_name_canon(intname
, &p
);
377 X509_NAME_ENTRY_free(tmpentry
);
378 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname
,
379 local_sk_X509_NAME_ENTRY_pop_free
);
383 /* Bitmap of all the types of string that will be canonicalized. */
385 #define ASN1_MASK_CANON \
386 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
387 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
388 | B_ASN1_VISIBLESTRING)
390 static int asn1_string_canon(ASN1_STRING
*out
, const ASN1_STRING
*in
)
392 unsigned char *to
, *from
;
395 /* If type not in bitmask just copy string across */
396 if (!(ASN1_tag2bit(in
->type
) & ASN1_MASK_CANON
)) {
397 if (!ASN1_STRING_copy(out
, in
))
402 out
->type
= V_ASN1_UTF8STRING
;
403 out
->length
= ASN1_STRING_to_UTF8(&out
->data
, in
);
404 if (out
->length
== -1)
413 * Convert string in place to canonical form. Ultimately we may need to
414 * handle a wider range of characters but for now ignore anything with
415 * MSB set and rely on the ossl_isspace() to fail on bad characters without
416 * needing isascii or range checks as well.
419 /* Ignore leading spaces */
420 while (len
> 0 && ossl_isspace(*from
)) {
427 /* Ignore trailing spaces */
428 while (len
> 0 && ossl_isspace(to
[-1])) {
437 /* If not ASCII set just copy across */
438 if (!ossl_isascii(*from
)) {
442 /* Collapse multiple spaces */
443 else if (ossl_isspace(*from
)) {
444 /* Copy one space across */
447 * Ignore subsequent spaces. Note: don't need to check len here
448 * because we know the last character is a non-space so we can't
455 while (ossl_isspace(*from
));
457 *to
++ = ossl_tolower(*from
);
463 out
->length
= to
- out
->data
;
469 static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY
) * _intname
,
474 STACK_OF(ASN1_VALUE
) *intname
= (STACK_OF(ASN1_VALUE
) *)_intname
;
477 for (i
= 0; i
< sk_ASN1_VALUE_num(intname
); i
++) {
478 v
= sk_ASN1_VALUE_value(intname
, i
);
479 ltmp
= ASN1_item_ex_i2d(&v
, in
,
480 ASN1_ITEM_rptr(X509_NAME_ENTRIES
), -1, -1);
488 int X509_NAME_set(X509_NAME
**xn
, const X509_NAME
*name
)
490 X509_NAME
*name_copy
;
494 if ((name_copy
= X509_NAME_dup(name
)) == NULL
)
501 int X509_NAME_print(BIO
*bp
, const X509_NAME
*name
, int obase
)
508 b
= X509_NAME_oneline(name
, NULL
, 0);
515 s
= b
+ 1; /* skip the first slash */
520 (ossl_isupper(s
[1]) && ((s
[2] == '=') ||
521 (ossl_isupper(s
[2]) && (s
[3] == '='))
525 if (BIO_write(bp
, c
, i
) != i
)
527 c
= s
+ 1; /* skip following slash */
529 if (BIO_write(bp
, ", ", 2) != 2)
543 X509err(X509_F_X509_NAME_PRINT
, ERR_R_BUF_LIB
);
548 int X509_NAME_get0_der(const X509_NAME
*nm
, const unsigned char **pder
,
551 /* Make sure encoding is valid */
552 if (i2d_X509_NAME(nm
, NULL
) <= 0)
555 *pder
= (unsigned char *)nm
->bytes
->data
;
557 *pderlen
= nm
->bytes
->length
;