1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <ctype.h>
16 #include <string.h>
17
18 #include <openssl/asn1.h>
19 #include <openssl/asn1t.h>
20 #include <openssl/buf.h>
21 #include <openssl/err.h>
22 #include <openssl/mem.h>
23 #include <openssl/obj.h>
24 #include <openssl/stack.h>
25 #include <openssl/x509.h>
26 #include <cstdint>
27
28 #include "../asn1/internal.h"
29 #include "../internal.h"
30 #include "internal.h"
31
32
33 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
34 DEFINE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
35
36 // Maximum length of X509_NAME: much larger than anything we should
37 // ever see in practice.
38
39 #define X509_NAME_MAX (1024 * 1024)
40
41 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
42 long len, const ASN1_ITEM *it, int opt,
43 ASN1_TLC *ctx);
44
45 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
46 const ASN1_ITEM *it);
47 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
48 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
49
50 static int x509_name_encode(X509_NAME *a);
51 static int x509_name_canon(X509_NAME *a);
52 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
53 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
54 unsigned char **in);
55
56 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
57 ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
58 ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_ANY_AS_STRING),
59 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
60
61 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X509_NAME_ENTRY)
62 IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_NAME_ENTRY)
63
64 // For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
65 // declare two template wrappers for this
66
67 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF,
68 0, RDNS,
69 X509_NAME_ENTRY)
70 ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
71
72 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
73 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
74 ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
75
76 // Normally that's where it would end: we'd have two nested STACK structures
77 // representing the ASN1. Unfortunately X509_NAME uses a completely different
78 // form and caches encodings so we have to process the internal form and
79 // convert to the external form.
80
81 static const ASN1_EXTERN_FUNCS x509_name_ff = {
82 x509_name_ex_new,
83 x509_name_ex_free,
84 x509_name_ex_d2i,
85 x509_name_ex_i2d,
86 };
87
IMPLEMENT_EXTERN_ASN1(X509_NAME,V_ASN1_SEQUENCE,x509_name_ff)88 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
89
90 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
91
92 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
93
94 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) {
95 X509_NAME *ret = NULL;
96 ret = reinterpret_cast<X509_NAME *>(OPENSSL_malloc(sizeof(X509_NAME)));
97 if (!ret) {
98 goto memerr;
99 }
100 if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) {
101 goto memerr;
102 }
103 if ((ret->bytes = BUF_MEM_new()) == NULL) {
104 goto memerr;
105 }
106 ret->canon_enc = NULL;
107 ret->canon_enclen = 0;
108 ret->modified = 1;
109 *val = (ASN1_VALUE *)ret;
110 return 1;
111
112 memerr:
113 if (ret) {
114 if (ret->entries) {
115 sk_X509_NAME_ENTRY_free(ret->entries);
116 }
117 OPENSSL_free(ret);
118 }
119 return 0;
120 }
121
x509_name_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)122 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
123 X509_NAME *a;
124 if (!pval || !*pval) {
125 return;
126 }
127 a = (X509_NAME *)*pval;
128
129 BUF_MEM_free(a->bytes);
130 sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
131 if (a->canon_enc) {
132 OPENSSL_free(a->canon_enc);
133 }
134 OPENSSL_free(a);
135 *pval = NULL;
136 }
137
local_sk_X509_NAME_ENTRY_free(STACK_OF (X509_NAME_ENTRY)* ne)138 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) {
139 sk_X509_NAME_ENTRY_free(ne);
140 }
141
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF (X509_NAME_ENTRY)* ne)142 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) {
143 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
144 }
145
x509_name_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_ITEM * it,int opt,ASN1_TLC * ctx)146 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
147 long len, const ASN1_ITEM *it, int opt,
148 ASN1_TLC *ctx) {
149 const unsigned char *p = *in, *q;
150 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
151 X509_NAME *nm = NULL;
152 size_t i, j;
153 int ret;
154 STACK_OF(X509_NAME_ENTRY) *entries;
155 X509_NAME_ENTRY *entry;
156 // Bound the size of an X509_NAME we are willing to parse.
157 if (len > X509_NAME_MAX) {
158 len = X509_NAME_MAX;
159 }
160 q = p;
161
162 // Get internal representation of Name
163 ASN1_VALUE *intname_val = NULL;
164 ret = ASN1_item_ex_d2i(&intname_val, &p, len,
165 ASN1_ITEM_rptr(X509_NAME_INTERNAL), /*tag=*/-1,
166 /*aclass=*/0, opt, /*buf=*/NULL);
167 if (ret <= 0) {
168 return ret;
169 }
170 intname = (STACK_OF(STACK_OF_X509_NAME_ENTRY) *)intname_val;
171
172 if (*val) {
173 x509_name_ex_free(val, NULL);
174 }
175 ASN1_VALUE *nm_val = NULL;
176 if (!x509_name_ex_new(&nm_val, NULL)) {
177 goto err;
178 }
179 nm = (X509_NAME *)nm_val;
180 // We've decoded it: now cache encoding
181 if (!BUF_MEM_grow(nm->bytes, p - q)) {
182 goto err;
183 }
184 OPENSSL_memcpy(nm->bytes->data, q, p - q);
185
186 // Convert internal representation to X509_NAME structure
187 for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname); i++) {
188 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname, i);
189 for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
190 entry = sk_X509_NAME_ENTRY_value(entries, j);
191 entry->set = (int)i;
192 if (!sk_X509_NAME_ENTRY_push(nm->entries, entry)) {
193 goto err;
194 }
195 (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
196 }
197 }
198 ret = x509_name_canon(nm);
199 if (!ret) {
200 goto err;
201 }
202 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
203 nm->modified = 0;
204 *val = (ASN1_VALUE *)nm;
205 *in = p;
206 return ret;
207 err:
208 X509_NAME_free(nm);
209 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
210 local_sk_X509_NAME_ENTRY_pop_free);
211 OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
212 return 0;
213 }
214
x509_name_ex_i2d(ASN1_VALUE ** val,unsigned char ** out,const ASN1_ITEM * it)215 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
216 const ASN1_ITEM *it) {
217 X509_NAME *a = (X509_NAME *)*val;
218 if (a->modified && (!x509_name_encode(a) || !x509_name_canon(a))) {
219 return -1;
220 }
221 int ret = a->bytes->length;
222 if (out != NULL) {
223 OPENSSL_memcpy(*out, a->bytes->data, ret);
224 *out += ret;
225 }
226 return ret;
227 }
228
x509_name_encode(X509_NAME * a)229 static int x509_name_encode(X509_NAME *a) {
230 int len;
231 unsigned char *p;
232 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
233 X509_NAME_ENTRY *entry;
234 int set = -1;
235 size_t i;
236 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname =
237 sk_STACK_OF_X509_NAME_ENTRY_new_null();
238
239 {
240 if (!intname) {
241 goto err;
242 }
243 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
244 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
245 if (entry->set != set) {
246 entries = sk_X509_NAME_ENTRY_new_null();
247 if (!entries) {
248 goto err;
249 }
250 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
251 sk_X509_NAME_ENTRY_free(entries);
252 goto err;
253 }
254 set = entry->set;
255 }
256 if (!sk_X509_NAME_ENTRY_push(entries, entry)) {
257 goto err;
258 }
259 }
260 ASN1_VALUE *intname_val = (ASN1_VALUE *)intname;
261 len =
262 ASN1_item_ex_i2d(&intname_val, NULL, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
263 /*tag=*/-1, /*aclass=*/0);
264 if (len <= 0) {
265 goto err;
266 }
267 if (!BUF_MEM_grow(a->bytes, len)) {
268 goto err;
269 }
270 p = (unsigned char *)a->bytes->data;
271 if (ASN1_item_ex_i2d(&intname_val, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
272 /*tag=*/-1, /*aclass=*/0) <= 0) {
273 goto err;
274 }
275 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
276 local_sk_X509_NAME_ENTRY_free);
277 a->modified = 0;
278 return 1;
279 }
280
281 err:
282 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, local_sk_X509_NAME_ENTRY_free);
283 return 0;
284 }
285
286 // This function generates the canonical encoding of the Name structure. In
287 // it all strings are converted to UTF8, leading, trailing and multiple
288 // spaces collapsed, converted to lower case and the leading SEQUENCE header
289 // removed. In future we could also normalize the UTF8 too. By doing this
290 // comparison of Name structures can be rapidly perfomed by just using
291 // OPENSSL_memcmp() of the canonical encoding. By omitting the leading SEQUENCE
292 // name constraints of type dirName can also be checked with a simple
293 // OPENSSL_memcmp().
294
x509_name_canon(X509_NAME * a)295 static int x509_name_canon(X509_NAME *a) {
296 unsigned char *p;
297 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
298 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
299 X509_NAME_ENTRY *entry, *tmpentry = NULL;
300 int set = -1, ret = 0, len;
301 size_t i;
302
303 if (a->canon_enc) {
304 OPENSSL_free(a->canon_enc);
305 a->canon_enc = NULL;
306 }
307 // Special case: empty X509_NAME => null encoding
308 if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
309 a->canon_enclen = 0;
310 return 1;
311 }
312 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
313 if (!intname) {
314 goto err;
315 }
316 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
317 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
318 if (entry->set != set) {
319 entries = sk_X509_NAME_ENTRY_new_null();
320 if (!entries) {
321 goto err;
322 }
323 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
324 sk_X509_NAME_ENTRY_free(entries);
325 goto err;
326 }
327 set = entry->set;
328 }
329 tmpentry = X509_NAME_ENTRY_new();
330 if (tmpentry == NULL) {
331 goto err;
332 }
333 tmpentry->object = OBJ_dup(entry->object);
334 if (!asn1_string_canon(tmpentry->value, entry->value)) {
335 goto err;
336 }
337 if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
338 goto err;
339 }
340 tmpentry = NULL;
341 }
342
343 // Finally generate encoding
344
345 len = i2d_name_canon(intname, NULL);
346 if (len < 0) {
347 goto err;
348 }
349 a->canon_enclen = len;
350
351 p = reinterpret_cast<uint8_t *>(OPENSSL_malloc(a->canon_enclen));
352
353 if (!p) {
354 goto err;
355 }
356
357 a->canon_enc = p;
358
359 i2d_name_canon(intname, &p);
360
361 ret = 1;
362
363 err:
364
365 if (tmpentry) {
366 X509_NAME_ENTRY_free(tmpentry);
367 }
368 if (intname) {
369 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
370 local_sk_X509_NAME_ENTRY_pop_free);
371 }
372 return ret;
373 }
374
375 // Bitmap of all the types of string that will be canonicalized.
376
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
asn1_string_canon(ASN1_STRING * out,ASN1_STRING * in)382 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) {
383 unsigned char *to, *from;
384 int len, i;
385
386 // If type not in bitmask just copy string across
387 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
388 if (!ASN1_STRING_copy(out, in)) {
389 return 0;
390 }
391 return 1;
392 }
393
394 out->type = V_ASN1_UTF8STRING;
395 out->length = ASN1_STRING_to_UTF8(&out->data, in);
396 if (out->length == -1) {
397 return 0;
398 }
399
400 to = out->data;
401 from = to;
402
403 len = out->length;
404
405 // Convert string in place to canonical form.
406
407 // Ignore leading spaces
408 while ((len > 0) && OPENSSL_isspace(*from)) {
409 from++;
410 len--;
411 }
412
413 to = from + len;
414
415 // Ignore trailing spaces
416 while ((len > 0) && OPENSSL_isspace(to[-1])) {
417 to--;
418 len--;
419 }
420
421 to = out->data;
422
423 i = 0;
424 while (i < len) {
425 // Collapse multiple spaces
426 if (OPENSSL_isspace(*from)) {
427 // Copy one space across
428 *to++ = ' ';
429 // Ignore subsequent spaces. Note: don't need to check len here
430 // because we know the last character is a non-space so we can't
431 // overflow.
432 do {
433 from++;
434 i++;
435 } while (OPENSSL_isspace(*from));
436 } else {
437 *to++ = OPENSSL_tolower(*from);
438 from++;
439 i++;
440 }
441 }
442
443 out->length = to - out->data;
444
445 return 1;
446 }
447
i2d_name_canon(STACK_OF (STACK_OF_X509_NAME_ENTRY)* _intname,unsigned char ** in)448 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
449 unsigned char **in) {
450 int len, ltmp;
451 size_t i;
452 ASN1_VALUE *v;
453 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
454
455 len = 0;
456 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
457 v = sk_ASN1_VALUE_value(intname, i);
458 ltmp = ASN1_item_ex_i2d(&v, in, ASN1_ITEM_rptr(X509_NAME_ENTRIES),
459 /*tag=*/-1, /*aclass=*/0);
460 if (ltmp < 0) {
461 return ltmp;
462 }
463 len += ltmp;
464 }
465 return len;
466 }
467
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)468 int X509_NAME_set(X509_NAME **xn, X509_NAME *name) {
469 if ((name = X509_NAME_dup(name)) == NULL) {
470 return 0;
471 }
472 X509_NAME_free(*xn);
473 *xn = name;
474 return 1;
475 }
476
X509_NAME_ENTRY_set(const X509_NAME_ENTRY * ne)477 int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) { return ne->set; }
478
X509_NAME_get0_der(X509_NAME * nm,const unsigned char ** out_der,size_t * out_der_len)479 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **out_der,
480 size_t *out_der_len) {
481 // Make sure encoding is valid
482 if (i2d_X509_NAME(nm, NULL) <= 0) {
483 return 0;
484 }
485 if (out_der != NULL) {
486 *out_der = (unsigned char *)nm->bytes->data;
487 }
488 if (out_der_len != NULL) {
489 *out_der_len = nm->bytes->length;
490 }
491 return 1;
492 }
493
x509_marshal_name(CBB * out,X509_NAME * in)494 int x509_marshal_name(CBB *out, X509_NAME *in) {
495 int len = i2d_X509_NAME(in, nullptr);
496 if (len <= 0) {
497 return 0;
498 }
499 uint8_t *ptr;
500 return CBB_add_space(out, &ptr, len) && i2d_X509_NAME(in, &ptr) == len;
501 }
502