Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/crypto/x509/x_name.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <stdio.h>
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"
18
19
/*
20
 * Maximum length of X509_NAME: much larger than anything we should
21
 * ever see in practice.
22
 */
23
24
1.62M
#define X509_NAME_MAX (1024 * 1024)
25
26
static int x509_name_ex_d2i(ASN1_VALUE **val,
27
                            const unsigned char **in, long len,
28
                            const ASN1_ITEM *it,
29
                            int tag, int aclass, char opt, ASN1_TLC *ctx);
30
31
static int x509_name_ex_i2d(const 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);
35
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, const ASN1_STRING *in);
39
static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
40
                          unsigned char **in);
41
42
static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
43
                              int indent,
44
                              const char *fname, const ASN1_PCTX *pctx);
45
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)
50
51
IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
52
IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
53
54
/*
55
 * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
56
 * declare two template wrappers for this
57
 */
58
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)
62
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)
66
67
/*
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.
72
 */
73
74
static const ASN1_EXTERN_FUNCS x509_name_ff = {
75
    NULL,
76
    x509_name_ex_new,
77
    x509_name_ex_free,
78
    0,                          /* Default clear behaviour is OK */
79
    x509_name_ex_d2i,
80
    x509_name_ex_i2d,
81
    x509_name_ex_print
82
};
83
84
IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
85
86
IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
87
88
IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
89
90
static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
91
1.58M
{
92
1.58M
    X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret));
93
94
1.58M
    if (ret == NULL)
95
0
        return 0;
96
1.58M
    if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) {
97
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
98
0
        goto err;
99
0
    }
100
1.58M
    if ((ret->bytes = BUF_MEM_new()) == NULL) {
101
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
102
0
        goto err;
103
0
    }
104
1.58M
    ret->modified = 1;
105
1.58M
    *val = (ASN1_VALUE *)ret;
106
1.58M
    return 1;
107
108
0
 err:
109
0
    if (ret) {
110
0
        sk_X509_NAME_ENTRY_free(ret->entries);
111
0
        OPENSSL_free(ret);
112
0
    }
113
0
    return 0;
114
1.58M
}
115
116
static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
117
3.30M
{
118
3.30M
    X509_NAME *a;
119
120
3.30M
    if (pval == NULL || *pval == NULL)
121
0
        return;
122
3.30M
    a = (X509_NAME *)*pval;
123
124
3.30M
    BUF_MEM_free(a->bytes);
125
3.30M
    sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
126
3.30M
    OPENSSL_free(a->canon_enc);
127
3.30M
    OPENSSL_free(a);
128
3.30M
    *pval = NULL;
129
3.30M
}
130
131
static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
132
65.1M
{
133
65.1M
    sk_X509_NAME_ENTRY_free(ne);
134
65.1M
}
135
136
static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
137
14.1M
{
138
14.1M
    sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
139
14.1M
}
140
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)
145
1.62M
{
146
1.62M
    const unsigned char *p = *in, *q;
147
1.62M
    union {
148
1.62M
        STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
149
1.62M
        ASN1_VALUE *a;
150
1.62M
    } intname = {
151
1.62M
        NULL
152
1.62M
    };
153
1.62M
    union {
154
1.62M
        X509_NAME *x;
155
1.62M
        ASN1_VALUE *a;
156
1.62M
    } nm = {
157
1.62M
        NULL
158
1.62M
    };
159
1.62M
    int i, j, ret;
160
1.62M
    STACK_OF(X509_NAME_ENTRY) *entries;
161
1.62M
    X509_NAME_ENTRY *entry;
162
163
1.62M
    if (len > X509_NAME_MAX)
164
0
        len = X509_NAME_MAX;
165
1.62M
    q = p;
166
167
    /* Get internal representation of Name */
168
1.62M
    ret = ASN1_item_ex_d2i(&intname.a,
169
1.62M
                           &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
170
1.62M
                           tag, aclass, opt, ctx);
171
172
1.62M
    if (ret <= 0)
173
197k
        return ret;
174
175
1.43M
    if (*val)
176
1.24M
        x509_name_ex_free(val, NULL);
177
1.43M
    if (!x509_name_ex_new(&nm.a, NULL))
178
0
        goto err;
179
    /* We've decoded it: now cache encoding */
180
1.43M
    if (!BUF_MEM_grow(nm.x->bytes, p - q))
181
0
        goto err;
182
1.43M
    memcpy(nm.x->bytes->data, q, p - q);
183
184
    /* Convert internal representation to X509_NAME structure */
185
73.9M
    for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
186
72.5M
        entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
187
85.3M
        for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
188
12.7M
            entry = sk_X509_NAME_ENTRY_value(entries, j);
189
12.7M
            entry->set = i;
190
12.7M
            if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
191
0
                goto err;
192
12.7M
            (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
193
12.7M
        }
194
72.5M
    }
195
1.43M
    ret = x509_name_canon(nm.x);
196
1.43M
    if (!ret)
197
18.9k
        goto err;
198
1.41M
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
199
1.41M
                                         local_sk_X509_NAME_ENTRY_free);
200
1.41M
    nm.x->modified = 0;
201
1.41M
    *val = nm.a;
202
1.41M
    *in = p;
203
1.41M
    return ret;
204
205
18.9k
 err:
206
18.9k
    if (nm.x != NULL)
207
18.9k
        X509_NAME_free(nm.x);
208
18.9k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
209
18.9k
                                         local_sk_X509_NAME_ENTRY_pop_free);
210
18.9k
    ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
211
18.9k
    return 0;
212
1.43M
}
213
214
static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out,
215
                            const ASN1_ITEM *it, int tag, int aclass)
216
594k
{
217
594k
    int ret;
218
594k
    X509_NAME *a = (X509_NAME *)*val;
219
220
594k
    if (a->modified) {
221
38.2k
        ret = x509_name_encode(a);
222
38.2k
        if (ret < 0)
223
0
            return ret;
224
38.2k
        ret = x509_name_canon(a);
225
38.2k
        if (!ret)
226
5.01k
            return -1;
227
38.2k
    }
228
589k
    ret = a->bytes->length;
229
589k
    if (out != NULL) {
230
122k
        memcpy(*out, a->bytes->data, ret);
231
122k
        *out += ret;
232
122k
    }
233
589k
    return ret;
234
594k
}
235
236
static int x509_name_encode(X509_NAME *a)
237
18.7k
{
238
18.7k
    union {
239
18.7k
        STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
240
18.7k
        const ASN1_VALUE *a;
241
18.7k
    } intname = {
242
18.7k
        NULL
243
18.7k
    };
244
18.7k
    int len;
245
18.7k
    unsigned char *p;
246
18.7k
    STACK_OF(X509_NAME_ENTRY) *entries = NULL;
247
18.7k
    X509_NAME_ENTRY *entry;
248
18.7k
    int i, set = -1;
249
250
18.7k
    intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
251
18.7k
    if (!intname.s)
252
0
        goto cerr;
253
1.52M
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
254
1.50M
        entry = sk_X509_NAME_ENTRY_value(a->entries, i);
255
1.50M
        if (entry->set != set) {
256
792k
            entries = sk_X509_NAME_ENTRY_new_null();
257
792k
            if (!entries)
258
0
                goto cerr;
259
792k
            if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
260
0
                sk_X509_NAME_ENTRY_free(entries);
261
0
                goto cerr;
262
0
            }
263
792k
            set = entry->set;
264
792k
        }
265
1.50M
        if (!sk_X509_NAME_ENTRY_push(entries, entry))
266
0
            goto cerr;
267
1.50M
    }
268
18.7k
    len = ASN1_item_ex_i2d(&intname.a, NULL,
269
18.7k
                           ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
270
18.7k
    if (!BUF_MEM_grow(a->bytes, len)) {
271
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
272
0
        goto err;
273
0
    }
274
18.7k
    p = (unsigned char *)a->bytes->data;
275
18.7k
    ASN1_item_ex_i2d(&intname.a,
276
18.7k
                     &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
277
18.7k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
278
18.7k
                                         local_sk_X509_NAME_ENTRY_free);
279
18.7k
    a->modified = 0;
280
18.7k
    return len;
281
0
 cerr:
282
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
283
0
 err:
284
0
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
285
0
                                         local_sk_X509_NAME_ENTRY_free);
286
0
    return -1;
287
0
}
288
289
static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
290
                              int indent,
291
                              const char *fname, const ASN1_PCTX *pctx)
292
38.0k
{
293
38.0k
    if (X509_NAME_print_ex(out, (const X509_NAME *)*pval,
294
38.0k
                           indent, pctx->nm_flags) <= 0)
295
112
        return 0;
296
37.9k
    return 2;
297
38.0k
}
298
299
/*
300
 * This function generates the canonical encoding of the Name structure. In
301
 * it all strings are converted to UTF8, leading, trailing and multiple
302
 * spaces collapsed, converted to lower case and the leading SEQUENCE header
303
 * removed. In future we could also normalize the UTF8 too. By doing this
304
 * comparison of Name structures can be rapidly performed by just using
305
 * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
306
 * constraints of type dirName can also be checked with a simple memcmp().
307
 * NOTE: For empty X509_NAME (NULL-DN), canon_enclen == 0 && canon_enc == NULL
308
 */
309
310
static int x509_name_canon(X509_NAME *a)
311
1.46M
{
312
1.46M
    unsigned char *p;
313
1.46M
    STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname;
314
1.46M
    STACK_OF(X509_NAME_ENTRY) *entries = NULL;
315
1.46M
    X509_NAME_ENTRY *entry, *tmpentry = NULL;
316
1.46M
    int i, set = -1, ret = 0, len;
317
318
1.46M
    OPENSSL_free(a->canon_enc);
319
1.46M
    a->canon_enc = NULL;
320
    /* Special case: empty X509_NAME => null encoding */
321
1.46M
    if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
322
1.09M
        a->canon_enclen = 0;
323
1.09M
        return 1;
324
1.09M
    }
325
373k
    intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
326
373k
    if (intname == NULL) {
327
0
        ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
328
0
        goto err;
329
0
    }
330
16.2M
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
331
15.8M
        entry = sk_X509_NAME_ENTRY_value(a->entries, i);
332
15.8M
        if (entry->set != set) {
333
5.44M
            entries = sk_X509_NAME_ENTRY_new_null();
334
5.44M
            if (entries == NULL)
335
0
                goto err;
336
5.44M
            if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
337
0
                sk_X509_NAME_ENTRY_free(entries);
338
0
                ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
339
0
                goto err;
340
0
            }
341
5.44M
            set = entry->set;
342
5.44M
        }
343
15.8M
        tmpentry = X509_NAME_ENTRY_new();
344
15.8M
        if (tmpentry == NULL) {
345
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
346
0
            goto err;
347
0
        }
348
15.8M
        tmpentry->object = OBJ_dup(entry->object);
349
15.8M
        if (tmpentry->object == NULL) {
350
0
            ERR_raise(ERR_LIB_X509, ERR_R_OBJ_LIB);
351
0
            goto err;
352
0
        }
353
15.8M
        if (!asn1_string_canon(tmpentry->value, entry->value))
354
24.0k
            goto err;
355
15.8M
        if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
356
0
            ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
357
0
            goto err;
358
0
        }
359
15.8M
        tmpentry = NULL;
360
15.8M
    }
361
362
    /* Finally generate encoding */
363
349k
    len = i2d_name_canon(intname, NULL);
364
349k
    if (len < 0)
365
0
        goto err;
366
349k
    a->canon_enclen = len;
367
368
349k
    p = OPENSSL_malloc(a->canon_enclen);
369
349k
    if (p == NULL)
370
0
        goto err;
371
372
349k
    a->canon_enc = p;
373
374
349k
    i2d_name_canon(intname, &p);
375
376
349k
    ret = 1;
377
378
373k
 err:
379
373k
    X509_NAME_ENTRY_free(tmpentry);
380
373k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
381
373k
                                         local_sk_X509_NAME_ENTRY_pop_free);
382
373k
    return ret;
383
349k
}
384
385
/* Bitmap of all the types of string that will be canonicalized. */
386
387
#define ASN1_MASK_CANON \
388
15.8M
        (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
389
15.8M
        | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
390
15.8M
        | B_ASN1_VISIBLESTRING)
391
392
static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in)
393
15.8M
{
394
15.8M
    unsigned char *to, *from;
395
15.8M
    int len, i;
396
397
    /* If type not in bitmask just copy string across */
398
15.8M
    if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
399
7.16M
        if (!ASN1_STRING_copy(out, in))
400
0
            return 0;
401
7.16M
        return 1;
402
7.16M
    }
403
404
8.71M
    out->type = V_ASN1_UTF8STRING;
405
8.71M
    out->length = ASN1_STRING_to_UTF8(&out->data, in);
406
8.71M
    if (out->length == -1)
407
24.0k
        return 0;
408
409
8.69M
    to = out->data;
410
8.69M
    from = to;
411
412
8.69M
    len = out->length;
413
414
    /*
415
     * Convert string in place to canonical form. Ultimately we may need to
416
     * handle a wider range of characters but for now ignore anything with
417
     * MSB set and rely on the ossl_isspace() to fail on bad characters without
418
     * needing isascii or range checks as well.
419
     */
420
421
    /* Ignore leading spaces */
422
8.71M
    while (len > 0 && ossl_isspace(*from)) {
423
17.1k
        from++;
424
17.1k
        len--;
425
17.1k
    }
426
427
8.69M
    to = from + len;
428
429
    /* Ignore trailing spaces */
430
8.70M
    while (len > 0 && ossl_isspace(to[-1])) {
431
10.3k
        to--;
432
10.3k
        len--;
433
10.3k
    }
434
435
8.69M
    to = out->data;
436
437
8.69M
    i = 0;
438
561M
    while (i < len) {
439
        /* If not ASCII set just copy across */
440
552M
        if (!ossl_isascii(*from)) {
441
524M
            *to++ = *from++;
442
524M
            i++;
443
524M
        }
444
        /* Collapse multiple spaces */
445
28.1M
        else if (ossl_isspace(*from)) {
446
            /* Copy one space across */
447
636k
            *to++ = ' ';
448
            /*
449
             * Ignore subsequent spaces. Note: don't need to check len here
450
             * because we know the last character is a non-space so we can't
451
             * overflow.
452
             */
453
1.93M
            do {
454
1.93M
                from++;
455
1.93M
                i++;
456
1.93M
            }
457
1.93M
            while (ossl_isspace(*from));
458
27.5M
        } else {
459
27.5M
            *to++ = ossl_tolower(*from);
460
27.5M
            from++;
461
27.5M
            i++;
462
27.5M
        }
463
552M
    }
464
465
8.69M
    out->length = to - out->data;
466
467
8.69M
    return 1;
468
469
8.71M
}
470
471
static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
472
                          unsigned char **in)
473
699k
{
474
699k
    int i, len, ltmp;
475
699k
    const ASN1_VALUE *v;
476
699k
    STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
477
478
699k
    len = 0;
479
11.5M
    for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
480
10.8M
        v = sk_ASN1_VALUE_value(intname, i);
481
10.8M
        ltmp = ASN1_item_ex_i2d(&v, in,
482
10.8M
                                ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
483
10.8M
        if (ltmp < 0 || len > INT_MAX - ltmp)
484
0
            return -1;
485
10.8M
        len += ltmp;
486
10.8M
    }
487
699k
    return len;
488
699k
}
489
490
int X509_NAME_set(X509_NAME **xn, const X509_NAME *name)
491
41.9k
{
492
41.9k
    X509_NAME *name_copy;
493
494
41.9k
    if (*xn == name)
495
0
        return *xn != NULL;
496
41.9k
    if ((name_copy = X509_NAME_dup(name)) == NULL)
497
0
        return 0;
498
41.9k
    X509_NAME_free(*xn);
499
41.9k
    *xn = name_copy;
500
41.9k
    return 1;
501
41.9k
}
502
503
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
504
116k
{
505
116k
    char *s, *c, *b;
506
116k
    int i;
507
508
116k
    b = X509_NAME_oneline(name, NULL, 0);
509
116k
    if (b == NULL)
510
329
        return 0;
511
116k
    if (*b == '\0') {
512
81.0k
        OPENSSL_free(b);
513
81.0k
        return 1;
514
81.0k
    }
515
35.4k
    s = b + 1;                  /* skip the first slash */
516
517
35.4k
    c = s;
518
481M
    for (;;) {
519
481M
        if (((*s == '/') &&
520
481M
             (ossl_isupper(s[1]) && ((s[2] == '=') ||
521
156k
                                (ossl_isupper(s[2]) && (s[3] == '='))
522
481M
              ))) || (*s == '\0'))
523
75.8k
        {
524
75.8k
            i = s - c;
525
75.8k
            if (BIO_write(bp, c, i) != i)
526
0
                goto err;
527
75.8k
            c = s + 1;          /* skip following slash */
528
75.8k
            if (*s != '\0') {
529
40.4k
                if (BIO_write(bp, ", ", 2) != 2)
530
0
                    goto err;
531
40.4k
            }
532
75.8k
        }
533
481M
        if (*s == '\0')
534
35.4k
            break;
535
481M
        s++;
536
481M
    }
537
538
35.4k
    OPENSSL_free(b);
539
35.4k
    return 1;
540
0
 err:
541
0
    ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
542
0
    OPENSSL_free(b);
543
0
    return 0;
544
35.4k
}
545
546
int X509_NAME_get0_der(const X509_NAME *nm, const unsigned char **pder,
547
                       size_t *pderlen)
548
0
{
549
    /* Make sure encoding is valid */
550
0
    if (i2d_X509_NAME(nm, NULL) <= 0)
551
0
        return 0;
552
0
    if (pder != NULL)
553
0
        *pder = (unsigned char *)nm->bytes->data;
554
0
    if (pderlen != NULL)
555
0
        *pderlen = nm->bytes->length;
556
0
    return 1;
557
0
}