1 |
|
|
/* $OpenBSD: x_name.c,v 1.33 2017/01/29 17:49:22 beck Exp $ */ |
2 |
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 |
|
|
* All rights reserved. |
4 |
|
|
* |
5 |
|
|
* This package is an SSL implementation written |
6 |
|
|
* by Eric Young (eay@cryptsoft.com). |
7 |
|
|
* The implementation was written so as to conform with Netscapes SSL. |
8 |
|
|
* |
9 |
|
|
* This library is free for commercial and non-commercial use as long as |
10 |
|
|
* the following conditions are aheared to. The following conditions |
11 |
|
|
* apply to all code found in this distribution, be it the RC4, RSA, |
12 |
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 |
|
|
* included with this distribution is covered by the same copyright terms |
14 |
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 |
|
|
* |
16 |
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in |
17 |
|
|
* the code are not to be removed. |
18 |
|
|
* If this package is used in a product, Eric Young should be given attribution |
19 |
|
|
* as the author of the parts of the library used. |
20 |
|
|
* This can be in the form of a textual message at program startup or |
21 |
|
|
* in documentation (online or textual) provided with the package. |
22 |
|
|
* |
23 |
|
|
* Redistribution and use in source and binary forms, with or without |
24 |
|
|
* modification, are permitted provided that the following conditions |
25 |
|
|
* are met: |
26 |
|
|
* 1. Redistributions of source code must retain the copyright |
27 |
|
|
* notice, this list of conditions and the following disclaimer. |
28 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
29 |
|
|
* notice, this list of conditions and the following disclaimer in the |
30 |
|
|
* documentation and/or other materials provided with the distribution. |
31 |
|
|
* 3. All advertising materials mentioning features or use of this software |
32 |
|
|
* must display the following acknowledgement: |
33 |
|
|
* "This product includes cryptographic software written by |
34 |
|
|
* Eric Young (eay@cryptsoft.com)" |
35 |
|
|
* The word 'cryptographic' can be left out if the rouines from the library |
36 |
|
|
* being used are not cryptographic related :-). |
37 |
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from |
38 |
|
|
* the apps directory (application code) you must include an acknowledgement: |
39 |
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 |
|
|
* |
41 |
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 |
|
|
* SUCH DAMAGE. |
52 |
|
|
* |
53 |
|
|
* The licence and distribution terms for any publically available version or |
54 |
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be |
55 |
|
|
* copied and put under another distribution licence |
56 |
|
|
* [including the GNU Public Licence.] |
57 |
|
|
*/ |
58 |
|
|
|
59 |
|
|
#include <ctype.h> |
60 |
|
|
#include <stdio.h> |
61 |
|
|
#include <string.h> |
62 |
|
|
|
63 |
|
|
#include <openssl/asn1t.h> |
64 |
|
|
#include <openssl/err.h> |
65 |
|
|
#include <openssl/x509.h> |
66 |
|
|
|
67 |
|
|
#include "asn1_locl.h" |
68 |
|
|
|
69 |
|
|
typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY; |
70 |
|
|
DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY) |
71 |
|
|
|
72 |
|
|
static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, |
73 |
|
|
long len, const ASN1_ITEM *it, int tag, int aclass, char opt, |
74 |
|
|
ASN1_TLC *ctx); |
75 |
|
|
|
76 |
|
|
static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, |
77 |
|
|
const ASN1_ITEM *it, int tag, int aclass); |
78 |
|
|
static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it); |
79 |
|
|
static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it); |
80 |
|
|
|
81 |
|
|
static int x509_name_encode(X509_NAME *a); |
82 |
|
|
static int x509_name_canon(X509_NAME *a); |
83 |
|
|
static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in); |
84 |
|
|
static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname, |
85 |
|
|
unsigned char **in); |
86 |
|
|
|
87 |
|
|
static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, |
88 |
|
|
const char *fname, const ASN1_PCTX *pctx); |
89 |
|
|
|
90 |
|
|
static const ASN1_TEMPLATE X509_NAME_ENTRY_seq_tt[] = { |
91 |
|
|
{ |
92 |
|
|
.offset = offsetof(X509_NAME_ENTRY, object), |
93 |
|
|
.field_name = "object", |
94 |
|
|
.item = &ASN1_OBJECT_it, |
95 |
|
|
}, |
96 |
|
|
{ |
97 |
|
|
.offset = offsetof(X509_NAME_ENTRY, value), |
98 |
|
|
.field_name = "value", |
99 |
|
|
.item = &ASN1_PRINTABLE_it, |
100 |
|
|
}, |
101 |
|
|
}; |
102 |
|
|
|
103 |
|
|
const ASN1_ITEM X509_NAME_ENTRY_it = { |
104 |
|
|
.itype = ASN1_ITYPE_SEQUENCE, |
105 |
|
|
.utype = V_ASN1_SEQUENCE, |
106 |
|
|
.templates = X509_NAME_ENTRY_seq_tt, |
107 |
|
|
.tcount = sizeof(X509_NAME_ENTRY_seq_tt) / sizeof(ASN1_TEMPLATE), |
108 |
|
|
.size = sizeof(X509_NAME_ENTRY), |
109 |
|
|
.sname = "X509_NAME_ENTRY", |
110 |
|
|
}; |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
X509_NAME_ENTRY * |
114 |
|
|
d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **a, const unsigned char **in, long len) |
115 |
|
|
{ |
116 |
|
|
return (X509_NAME_ENTRY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
117 |
|
|
&X509_NAME_ENTRY_it); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
int |
121 |
|
|
i2d_X509_NAME_ENTRY(X509_NAME_ENTRY *a, unsigned char **out) |
122 |
|
|
{ |
123 |
|
|
return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_ENTRY_it); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
X509_NAME_ENTRY * |
127 |
|
|
X509_NAME_ENTRY_new(void) |
128 |
|
|
{ |
129 |
|
207422 |
return (X509_NAME_ENTRY *)ASN1_item_new(&X509_NAME_ENTRY_it); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
void |
133 |
|
|
X509_NAME_ENTRY_free(X509_NAME_ENTRY *a) |
134 |
|
|
{ |
135 |
|
414290 |
ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_ENTRY_it); |
136 |
|
207145 |
} |
137 |
|
|
|
138 |
|
|
X509_NAME_ENTRY * |
139 |
|
|
X509_NAME_ENTRY_dup(X509_NAME_ENTRY *x) |
140 |
|
|
{ |
141 |
|
374 |
return ASN1_item_dup(&X509_NAME_ENTRY_it, x); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } |
145 |
|
|
* so declare two template wrappers for this |
146 |
|
|
*/ |
147 |
|
|
|
148 |
|
|
static const ASN1_TEMPLATE X509_NAME_ENTRIES_item_tt = { |
149 |
|
|
.flags = ASN1_TFLG_SET_OF, |
150 |
|
|
.tag = 0, |
151 |
|
|
.offset = 0, |
152 |
|
|
.field_name = "RDNS", |
153 |
|
|
.item = &X509_NAME_ENTRY_it, |
154 |
|
|
}; |
155 |
|
|
|
156 |
|
|
const ASN1_ITEM X509_NAME_ENTRIES_it = { |
157 |
|
|
.itype = ASN1_ITYPE_PRIMITIVE, |
158 |
|
|
.utype = -1, |
159 |
|
|
.templates = &X509_NAME_ENTRIES_item_tt, |
160 |
|
|
.tcount = 0, |
161 |
|
|
.funcs = NULL, |
162 |
|
|
.size = 0, |
163 |
|
|
.sname = "X509_NAME_ENTRIES", |
164 |
|
|
}; |
165 |
|
|
|
166 |
|
|
static const ASN1_TEMPLATE X509_NAME_INTERNAL_item_tt = { |
167 |
|
|
.flags = ASN1_TFLG_SEQUENCE_OF, |
168 |
|
|
.tag = 0, |
169 |
|
|
.offset = 0, |
170 |
|
|
.field_name = "Name", |
171 |
|
|
.item = &X509_NAME_ENTRIES_it, |
172 |
|
|
}; |
173 |
|
|
|
174 |
|
|
const ASN1_ITEM X509_NAME_INTERNAL_it = { |
175 |
|
|
.itype = ASN1_ITYPE_PRIMITIVE, |
176 |
|
|
.utype = -1, |
177 |
|
|
.templates = &X509_NAME_INTERNAL_item_tt, |
178 |
|
|
.tcount = 0, |
179 |
|
|
.funcs = NULL, |
180 |
|
|
.size = 0, |
181 |
|
|
.sname = "X509_NAME_INTERNAL", |
182 |
|
|
}; |
183 |
|
|
|
184 |
|
|
/* Normally that's where it would end: we'd have two nested STACK structures |
185 |
|
|
* representing the ASN1. Unfortunately X509_NAME uses a completely different |
186 |
|
|
* form and caches encodings so we have to process the internal form and convert |
187 |
|
|
* to the external form. |
188 |
|
|
*/ |
189 |
|
|
|
190 |
|
|
const ASN1_EXTERN_FUNCS x509_name_ff = { |
191 |
|
|
NULL, |
192 |
|
|
x509_name_ex_new, |
193 |
|
|
x509_name_ex_free, |
194 |
|
|
0, /* Default clear behaviour is OK */ |
195 |
|
|
x509_name_ex_d2i, |
196 |
|
|
x509_name_ex_i2d, |
197 |
|
|
x509_name_ex_print |
198 |
|
|
}; |
199 |
|
|
|
200 |
|
|
const ASN1_ITEM X509_NAME_it = { |
201 |
|
|
.itype = ASN1_ITYPE_EXTERN, |
202 |
|
|
.utype = V_ASN1_SEQUENCE, |
203 |
|
|
.templates = NULL, |
204 |
|
|
.tcount = 0, |
205 |
|
|
.funcs = &x509_name_ff, |
206 |
|
|
.size = 0, |
207 |
|
|
.sname = "X509_NAME", |
208 |
|
|
}; |
209 |
|
|
|
210 |
|
|
X509_NAME * |
211 |
|
|
d2i_X509_NAME(X509_NAME **a, const unsigned char **in, long len) |
212 |
|
|
{ |
213 |
|
|
return (X509_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
214 |
|
|
&X509_NAME_it); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
int |
218 |
|
|
i2d_X509_NAME(X509_NAME *a, unsigned char **out) |
219 |
|
|
{ |
220 |
|
36 |
return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_it); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
X509_NAME * |
224 |
|
|
X509_NAME_new(void) |
225 |
|
|
{ |
226 |
|
190 |
return (X509_NAME *)ASN1_item_new(&X509_NAME_it); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
void |
230 |
|
|
X509_NAME_free(X509_NAME *a) |
231 |
|
|
{ |
232 |
|
540 |
ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_it); |
233 |
|
270 |
} |
234 |
|
|
|
235 |
|
|
X509_NAME * |
236 |
|
|
X509_NAME_dup(X509_NAME *x) |
237 |
|
|
{ |
238 |
|
308 |
return ASN1_item_dup(&X509_NAME_it, x); |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
static int |
242 |
|
|
x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) |
243 |
|
|
{ |
244 |
|
|
X509_NAME *ret = NULL; |
245 |
|
|
|
246 |
|
105792 |
ret = malloc(sizeof(X509_NAME)); |
247 |
✓✗ |
52896 |
if (!ret) |
248 |
|
|
goto memerr; |
249 |
✓✗ |
52896 |
if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) |
250 |
|
|
goto memerr; |
251 |
✓✗ |
52896 |
if ((ret->bytes = BUF_MEM_new()) == NULL) |
252 |
|
|
goto memerr; |
253 |
|
52896 |
ret->canon_enc = NULL; |
254 |
|
52896 |
ret->canon_enclen = 0; |
255 |
|
52896 |
ret->modified = 1; |
256 |
|
52896 |
*val = (ASN1_VALUE *)ret; |
257 |
|
52896 |
return 1; |
258 |
|
|
|
259 |
|
|
memerr: |
260 |
|
|
ASN1error(ERR_R_MALLOC_FAILURE); |
261 |
|
|
if (ret) { |
262 |
|
|
if (ret->entries) |
263 |
|
|
sk_X509_NAME_ENTRY_free(ret->entries); |
264 |
|
|
free(ret); |
265 |
|
|
} |
266 |
|
|
return 0; |
267 |
|
52896 |
} |
268 |
|
|
|
269 |
|
|
static void |
270 |
|
|
x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
271 |
|
|
{ |
272 |
|
|
X509_NAME *a; |
273 |
|
|
|
274 |
✓✗✗✓
|
158571 |
if (!pval || !*pval) |
275 |
|
|
return; |
276 |
|
52857 |
a = (X509_NAME *)*pval; |
277 |
|
|
|
278 |
|
52857 |
BUF_MEM_free(a->bytes); |
279 |
|
52857 |
sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free); |
280 |
|
52857 |
free(a->canon_enc); |
281 |
|
52857 |
free(a); |
282 |
|
52857 |
*pval = NULL; |
283 |
|
105714 |
} |
284 |
|
|
|
285 |
|
|
static int |
286 |
|
|
x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, |
287 |
|
|
const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
288 |
|
|
{ |
289 |
|
52726 |
const unsigned char *p = *in, *q; |
290 |
|
26363 |
union { |
291 |
|
|
STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; |
292 |
|
|
ASN1_VALUE *a; |
293 |
|
26363 |
} intname = {NULL}; |
294 |
|
26363 |
union { |
295 |
|
|
X509_NAME *x; |
296 |
|
|
ASN1_VALUE *a; |
297 |
|
26363 |
} nm = {NULL}; |
298 |
|
|
int i, j, ret; |
299 |
|
|
STACK_OF(X509_NAME_ENTRY) *entries; |
300 |
|
|
X509_NAME_ENTRY *entry; |
301 |
|
26363 |
q = p; |
302 |
|
|
|
303 |
|
|
/* Get internal representation of Name */ |
304 |
|
26363 |
ret = ASN1_item_ex_d2i(&intname.a, &p, len, |
305 |
|
|
&X509_NAME_INTERNAL_it, tag, aclass, opt, ctx); |
306 |
|
|
|
307 |
✗✓ |
26363 |
if (ret <= 0) |
308 |
|
|
return ret; |
309 |
|
|
|
310 |
✓✓ |
26363 |
if (*val) |
311 |
|
26201 |
x509_name_ex_free(val, NULL); |
312 |
✓✗ |
26363 |
if (!x509_name_ex_new(&nm.a, NULL)) |
313 |
|
|
goto err; |
314 |
|
|
/* We've decoded it: now cache encoding */ |
315 |
✓✗ |
26363 |
if (!BUF_MEM_grow(nm.x->bytes, p - q)) |
316 |
|
|
goto err; |
317 |
|
26363 |
memcpy(nm.x->bytes->data, q, p - q); |
318 |
|
|
|
319 |
|
|
/* Convert internal representation to X509_NAME structure */ |
320 |
✓✓ |
259496 |
for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) { |
321 |
|
103385 |
entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i); |
322 |
✓✓ |
413540 |
for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) { |
323 |
|
103385 |
entry = sk_X509_NAME_ENTRY_value(entries, j); |
324 |
|
103385 |
entry->set = i; |
325 |
✓✗ |
103385 |
if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry)) |
326 |
|
|
goto err; |
327 |
|
|
} |
328 |
|
103385 |
sk_X509_NAME_ENTRY_free(entries); |
329 |
|
|
} |
330 |
|
26363 |
sk_STACK_OF_X509_NAME_ENTRY_free(intname.s); |
331 |
|
26363 |
ret = x509_name_canon(nm.x); |
332 |
✓✗ |
26363 |
if (!ret) |
333 |
|
|
goto err; |
334 |
|
26363 |
nm.x->modified = 0; |
335 |
|
26363 |
*val = nm.a; |
336 |
|
26363 |
*in = p; |
337 |
|
26363 |
return ret; |
338 |
|
|
|
339 |
|
|
err: |
340 |
|
|
if (nm.x != NULL) |
341 |
|
|
X509_NAME_free(nm.x); |
342 |
|
|
ASN1error(ERR_R_NESTED_ASN1_ERROR); |
343 |
|
|
return 0; |
344 |
|
26363 |
} |
345 |
|
|
|
346 |
|
|
static int |
347 |
|
|
x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, |
348 |
|
|
int tag, int aclass) |
349 |
|
|
{ |
350 |
|
|
int ret; |
351 |
|
1936 |
X509_NAME *a = (X509_NAME *)*val; |
352 |
|
|
|
353 |
✓✓ |
968 |
if (a->modified) { |
354 |
|
101 |
ret = x509_name_encode(a); |
355 |
✗✓ |
101 |
if (ret < 0) |
356 |
|
|
return ret; |
357 |
|
101 |
ret = x509_name_canon(a); |
358 |
✗✓ |
101 |
if (ret < 0) |
359 |
|
|
return ret; |
360 |
|
|
} |
361 |
|
968 |
ret = a->bytes->length; |
362 |
✓✓ |
968 |
if (out != NULL) { |
363 |
|
327 |
memcpy(*out, a->bytes->data, ret); |
364 |
|
327 |
*out += ret; |
365 |
|
327 |
} |
366 |
|
968 |
return ret; |
367 |
|
968 |
} |
368 |
|
|
|
369 |
|
|
static void |
370 |
|
|
local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) |
371 |
|
|
{ |
372 |
|
358 |
sk_X509_NAME_ENTRY_free(ne); |
373 |
|
179 |
} |
374 |
|
|
|
375 |
|
|
static void |
376 |
|
|
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) |
377 |
|
|
{ |
378 |
|
207128 |
sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); |
379 |
|
103564 |
} |
380 |
|
|
|
381 |
|
|
static int |
382 |
|
|
x509_name_encode(X509_NAME *a) |
383 |
|
|
{ |
384 |
|
202 |
union { |
385 |
|
|
STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; |
386 |
|
|
ASN1_VALUE *a; |
387 |
|
101 |
} intname = {NULL}; |
388 |
|
|
int len; |
389 |
|
101 |
unsigned char *p; |
390 |
|
|
STACK_OF(X509_NAME_ENTRY) *entries = NULL; |
391 |
|
|
X509_NAME_ENTRY *entry; |
392 |
|
|
int i, set = -1; |
393 |
|
|
|
394 |
|
101 |
intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
395 |
✓✗ |
101 |
if (!intname.s) |
396 |
|
|
goto memerr; |
397 |
✓✓ |
560 |
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
398 |
|
179 |
entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
399 |
✓✗ |
179 |
if (entry->set != set) { |
400 |
|
179 |
entries = sk_X509_NAME_ENTRY_new_null(); |
401 |
✓✗ |
179 |
if (!entries) |
402 |
|
|
goto memerr; |
403 |
✓✗ |
179 |
if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, |
404 |
|
|
entries)) |
405 |
|
|
goto memerr; |
406 |
|
179 |
set = entry->set; |
407 |
|
179 |
} |
408 |
✓✗✓✗
|
358 |
if (entries == NULL /* if entry->set is bogusly -1 */ || |
409 |
|
179 |
!sk_X509_NAME_ENTRY_push(entries, entry)) |
410 |
|
|
goto memerr; |
411 |
|
|
} |
412 |
|
101 |
len = ASN1_item_ex_i2d(&intname.a, NULL, |
413 |
|
|
&X509_NAME_INTERNAL_it, -1, -1); |
414 |
✓✗ |
101 |
if (!BUF_MEM_grow(a->bytes, len)) |
415 |
|
|
goto memerr; |
416 |
|
101 |
p = (unsigned char *)a->bytes->data; |
417 |
|
101 |
ASN1_item_ex_i2d(&intname.a, &p, &X509_NAME_INTERNAL_it, |
418 |
|
|
-1, -1); |
419 |
|
101 |
sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
420 |
|
|
local_sk_X509_NAME_ENTRY_free); |
421 |
|
101 |
a->modified = 0; |
422 |
|
101 |
return len; |
423 |
|
|
|
424 |
|
|
memerr: |
425 |
|
|
sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
426 |
|
|
local_sk_X509_NAME_ENTRY_free); |
427 |
|
|
ASN1error(ERR_R_MALLOC_FAILURE); |
428 |
|
|
return -1; |
429 |
|
101 |
} |
430 |
|
|
|
431 |
|
|
static int |
432 |
|
|
x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, const char *fname, |
433 |
|
|
const ASN1_PCTX *pctx) |
434 |
|
|
{ |
435 |
|
|
if (X509_NAME_print_ex(out, (X509_NAME *)*pval, indent, |
436 |
|
|
pctx->nm_flags) <= 0) |
437 |
|
|
return 0; |
438 |
|
|
return 2; |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
/* This function generates the canonical encoding of the Name structure. |
442 |
|
|
* In it all strings are converted to UTF8, leading, trailing and |
443 |
|
|
* multiple spaces collapsed, converted to lower case and the leading |
444 |
|
|
* SEQUENCE header removed. |
445 |
|
|
* |
446 |
|
|
* In future we could also normalize the UTF8 too. |
447 |
|
|
* |
448 |
|
|
* By doing this comparison of Name structures can be rapidly |
449 |
|
|
* performed by just using memcmp() of the canonical encoding. |
450 |
|
|
* By omitting the leading SEQUENCE name constraints of type |
451 |
|
|
* dirName can also be checked with a simple memcmp(). |
452 |
|
|
*/ |
453 |
|
|
|
454 |
|
|
static int |
455 |
|
|
x509_name_canon(X509_NAME *a) |
456 |
|
|
{ |
457 |
|
52928 |
unsigned char *p; |
458 |
|
|
STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL; |
459 |
|
|
STACK_OF(X509_NAME_ENTRY) *entries = NULL; |
460 |
|
|
X509_NAME_ENTRY *entry, *tmpentry = NULL; |
461 |
|
|
int i, len, set = -1, ret = 0; |
462 |
|
|
|
463 |
✗✓ |
26464 |
if (a->canon_enc) { |
464 |
|
|
free(a->canon_enc); |
465 |
|
|
a->canon_enc = NULL; |
466 |
|
|
} |
467 |
|
|
/* Special case: empty X509_NAME => null encoding */ |
468 |
✗✓ |
26464 |
if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { |
469 |
|
|
a->canon_enclen = 0; |
470 |
|
|
return 1; |
471 |
|
|
} |
472 |
|
26464 |
intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
473 |
✓✗ |
26464 |
if (!intname) |
474 |
|
|
goto err; |
475 |
✓✓ |
260056 |
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
476 |
|
103564 |
entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
477 |
✓✗ |
103564 |
if (entry->set != set) { |
478 |
|
103564 |
entries = sk_X509_NAME_ENTRY_new_null(); |
479 |
✓✗ |
103564 |
if (!entries) |
480 |
|
|
goto err; |
481 |
✗✓ |
207128 |
if (sk_STACK_OF_X509_NAME_ENTRY_push(intname, |
482 |
|
103564 |
entries) == 0) { |
483 |
|
|
sk_X509_NAME_ENTRY_free(entries); |
484 |
|
|
goto err; |
485 |
|
|
} |
486 |
|
103564 |
set = entry->set; |
487 |
|
103564 |
} |
488 |
|
103564 |
tmpentry = X509_NAME_ENTRY_new(); |
489 |
✓✗ |
103564 |
if (tmpentry == NULL) |
490 |
|
|
goto err; |
491 |
|
103564 |
tmpentry->object = OBJ_dup(entry->object); |
492 |
✓✗ |
103564 |
if (tmpentry->object == NULL) |
493 |
|
|
goto err; |
494 |
✓✗ |
207128 |
if (!asn1_string_canon(tmpentry->value, entry->value)) |
495 |
|
|
goto err; |
496 |
✓✗ |
207128 |
if (entries == NULL /* if entry->set is bogusly -1 */ || |
497 |
|
103564 |
!sk_X509_NAME_ENTRY_push(entries, tmpentry)) |
498 |
|
|
goto err; |
499 |
|
|
tmpentry = NULL; |
500 |
|
|
} |
501 |
|
|
|
502 |
|
|
/* Finally generate encoding */ |
503 |
|
26464 |
len = i2d_name_canon(intname, NULL); |
504 |
✓✗ |
26464 |
if (len < 0) |
505 |
|
|
goto err; |
506 |
|
26464 |
p = malloc(len); |
507 |
✓✗ |
26464 |
if (p == NULL) |
508 |
|
|
goto err; |
509 |
|
26464 |
a->canon_enc = p; |
510 |
|
26464 |
a->canon_enclen = len; |
511 |
|
26464 |
i2d_name_canon(intname, &p); |
512 |
|
26464 |
ret = 1; |
513 |
|
|
|
514 |
|
|
err: |
515 |
✗✓ |
26464 |
if (tmpentry) |
516 |
|
|
X509_NAME_ENTRY_free(tmpentry); |
517 |
✓✗ |
26464 |
if (intname) |
518 |
|
26464 |
sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, |
519 |
|
|
local_sk_X509_NAME_ENTRY_pop_free); |
520 |
|
26464 |
return ret; |
521 |
|
26464 |
} |
522 |
|
|
|
523 |
|
|
/* Bitmap of all the types of string that will be canonicalized. */ |
524 |
|
|
|
525 |
|
|
#define ASN1_MASK_CANON \ |
526 |
|
|
(B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \ |
527 |
|
|
| B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \ |
528 |
|
|
| B_ASN1_VISIBLESTRING) |
529 |
|
|
|
530 |
|
|
|
531 |
|
|
static int |
532 |
|
|
asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) |
533 |
|
|
{ |
534 |
|
|
unsigned char *to, *from; |
535 |
|
|
int len, i; |
536 |
|
|
|
537 |
|
|
/* If type not in bitmask just copy string across */ |
538 |
✗✓ |
207128 |
if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) { |
539 |
|
|
if (!ASN1_STRING_copy(out, in)) |
540 |
|
|
return 0; |
541 |
|
|
return 1; |
542 |
|
|
} |
543 |
|
|
|
544 |
|
103564 |
out->type = V_ASN1_UTF8STRING; |
545 |
|
103564 |
out->length = ASN1_STRING_to_UTF8(&out->data, in); |
546 |
✗✓ |
103564 |
if (out->length == -1) |
547 |
|
|
return 0; |
548 |
|
|
|
549 |
|
103564 |
to = out->data; |
550 |
|
|
from = to; |
551 |
|
|
|
552 |
|
|
len = out->length; |
553 |
|
|
|
554 |
|
|
/* Convert string in place to canonical form. |
555 |
|
|
* Ultimately we may need to handle a wider range of characters |
556 |
|
|
* but for now ignore anything with MSB set and rely on the |
557 |
|
|
* isspace() and tolower() functions. |
558 |
|
|
*/ |
559 |
|
|
|
560 |
|
|
/* Ignore leading spaces */ |
561 |
✓✗✓✗ ✗✓ |
517820 |
while ((len > 0) && !(*from & 0x80) && isspace(*from)) { |
562 |
|
|
from++; |
563 |
|
|
len--; |
564 |
|
|
} |
565 |
|
|
|
566 |
|
103564 |
to = from + len - 1; |
567 |
|
|
|
568 |
|
|
/* Ignore trailing spaces */ |
569 |
✓✗✓✗ ✗✓ |
414256 |
while ((len > 0) && !(*to & 0x80) && isspace(*to)) { |
570 |
|
|
to--; |
571 |
|
|
len--; |
572 |
|
|
} |
573 |
|
|
|
574 |
|
103564 |
to = out->data; |
575 |
|
|
|
576 |
|
|
i = 0; |
577 |
✓✓ |
3609432 |
while (i < len) { |
578 |
|
|
/* If MSB set just copy across */ |
579 |
✓✓ |
1701152 |
if (*from & 0x80) { |
580 |
|
4128 |
*to++ = *from++; |
581 |
|
4128 |
i++; |
582 |
|
4128 |
} |
583 |
|
|
/* Collapse multiple spaces */ |
584 |
✓✓ |
1697024 |
else if (isspace(*from)) { |
585 |
|
|
/* Copy one space across */ |
586 |
|
146377 |
*to++ = ' '; |
587 |
|
|
/* Ignore subsequent spaces. Note: don't need to |
588 |
|
|
* check len here because we know the last |
589 |
|
|
* character is a non-space so we can't overflow. |
590 |
|
|
*/ |
591 |
|
146377 |
do { |
592 |
|
146377 |
from++; |
593 |
|
146377 |
i++; |
594 |
✓✗✗✓
|
292754 |
} while (!(*from & 0x80) && isspace(*from)); |
595 |
|
|
} else { |
596 |
|
1550647 |
*to++ = tolower(*from); |
597 |
|
1550647 |
from++; |
598 |
|
1550647 |
i++; |
599 |
|
|
} |
600 |
|
|
} |
601 |
|
|
|
602 |
|
103564 |
out->length = to - out->data; |
603 |
|
|
|
604 |
|
103564 |
return 1; |
605 |
|
103564 |
} |
606 |
|
|
|
607 |
|
|
static int |
608 |
|
|
i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, unsigned char **in) |
609 |
|
|
{ |
610 |
|
|
int i, len, ltmp; |
611 |
|
105856 |
ASN1_VALUE *v; |
612 |
|
52928 |
STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname; |
613 |
|
|
|
614 |
|
|
len = 0; |
615 |
✓✓ |
520112 |
for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { |
616 |
|
207128 |
v = sk_ASN1_VALUE_value(intname, i); |
617 |
|
207128 |
ltmp = ASN1_item_ex_i2d(&v, in, |
618 |
|
|
&X509_NAME_ENTRIES_it, -1, -1); |
619 |
✗✓ |
207128 |
if (ltmp < 0) |
620 |
|
|
return ltmp; |
621 |
|
207128 |
len += ltmp; |
622 |
|
|
} |
623 |
|
52928 |
return len; |
624 |
|
52928 |
} |
625 |
|
|
|
626 |
|
|
int |
627 |
|
|
X509_NAME_set(X509_NAME **xn, X509_NAME *name) |
628 |
|
|
{ |
629 |
|
|
X509_NAME *in; |
630 |
|
|
|
631 |
✗✓ |
288 |
if (!xn || !name) |
632 |
|
|
return (0); |
633 |
|
|
|
634 |
✓✗ |
144 |
if (*xn != name) { |
635 |
|
144 |
in = X509_NAME_dup(name); |
636 |
✓✗ |
144 |
if (in != NULL) { |
637 |
|
144 |
X509_NAME_free(*xn); |
638 |
|
144 |
*xn = in; |
639 |
|
144 |
} |
640 |
|
|
} |
641 |
|
144 |
return (*xn != NULL); |
642 |
|
144 |
} |