Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/x509/x_name.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
2.95M
#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
148M
} ASN1_SEQUENCE_END(X509_NAME_ENTRY)
50
148M
51
148M
IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
52
148M
IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
53
148M
54
148M
/*
55
148M
 * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
56
148M
 * declare two template wrappers for this
57
148M
 */
58
148M
59
148M
ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
60
236M
static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
61
62
    ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL)
63
    = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
64
3.07M
static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
65
66
    /*
67
     * Normally that's where it would end: we'd have two nested STACK structures
68
     * representing the ASN1. Unfortunately X509_NAME uses a completely different
69
     * form and caches encodings so we have to process the internal form and
70
     * convert to the external form.
71
     */
72
73
    static const ASN1_EXTERN_FUNCS x509_name_ff
74
    = {
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
19.4M
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
906k
{
92
906k
    X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret));
93
94
906k
    if (ret == NULL)
95
0
        goto memerr;
96
906k
    if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
97
0
        goto memerr;
98
906k
    if ((ret->bytes = BUF_MEM_new()) == NULL)
99
0
        goto memerr;
100
906k
    ret->modified = 1;
101
906k
    *val = (ASN1_VALUE *)ret;
102
906k
    return 1;
103
104
0
memerr:
105
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
106
0
    if (ret) {
107
0
        sk_X509_NAME_ENTRY_free(ret->entries);
108
0
        OPENSSL_free(ret);
109
0
    }
110
0
    return 0;
111
906k
}
112
113
static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
114
5.87M
{
115
5.87M
    X509_NAME *a;
116
117
5.87M
    if (pval == NULL || *pval == NULL)
118
0
        return;
119
5.87M
    a = (X509_NAME *)*pval;
120
121
5.87M
    BUF_MEM_free(a->bytes);
122
5.87M
    sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
123
5.87M
    OPENSSL_free(a->canon_enc);
124
5.87M
    OPENSSL_free(a);
125
5.87M
    *pval = NULL;
126
5.87M
}
127
128
static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
129
100M
{
130
100M
    sk_X509_NAME_ENTRY_free(ne);
131
100M
}
132
133
static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
134
24.6M
{
135
24.6M
    sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
136
24.6M
}
137
138
static int x509_name_ex_d2i(ASN1_VALUE **val,
139
    const unsigned char **in, long len,
140
    const ASN1_ITEM *it, int tag, int aclass,
141
    char opt, ASN1_TLC *ctx)
142
2.95M
{
143
2.95M
    const unsigned char *p = *in, *q;
144
2.95M
    union {
145
2.95M
        STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
146
2.95M
        ASN1_VALUE *a;
147
2.95M
    } intname = {
148
2.95M
        NULL
149
2.95M
    };
150
2.95M
    union {
151
2.95M
        X509_NAME *x;
152
2.95M
        ASN1_VALUE *a;
153
2.95M
    } nm = {
154
2.95M
        NULL
155
2.95M
    };
156
2.95M
    int i, j, ret;
157
2.95M
    STACK_OF(X509_NAME_ENTRY) *entries;
158
2.95M
    X509_NAME_ENTRY *entry;
159
160
2.95M
    if (len > X509_NAME_MAX)
161
0
        len = X509_NAME_MAX;
162
2.95M
    q = p;
163
164
    /* Get internal representation of Name */
165
2.95M
    ret = ASN1_item_ex_d2i(&intname.a,
166
2.95M
        &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
167
2.95M
        tag, aclass, opt, ctx);
168
169
2.95M
    if (ret <= 0)
170
326k
        return ret;
171
172
2.62M
    if (*val)
173
2.30M
        x509_name_ex_free(val, NULL);
174
2.62M
    if (!x509_name_ex_new(&nm.a, NULL))
175
0
        goto err;
176
    /* We've decoded it: now cache encoding */
177
2.62M
    if (!BUF_MEM_grow(nm.x->bytes, p - q))
178
0
        goto err;
179
2.62M
    memcpy(nm.x->bytes->data, q, p - q);
180
181
    /* Convert internal representation to X509_NAME structure */
182
117M
    for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
183
114M
        entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
184
132M
        for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
185
18.1M
            entry = sk_X509_NAME_ENTRY_value(entries, j);
186
18.1M
            entry->set = i;
187
18.1M
            if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
188
0
                goto err;
189
18.1M
            (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
190
18.1M
        }
191
114M
    }
192
2.62M
    ret = x509_name_canon(nm.x);
193
2.62M
    if (!ret)
194
27.7k
        goto err;
195
2.59M
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
196
2.59M
        local_sk_X509_NAME_ENTRY_free);
197
2.59M
    nm.x->modified = 0;
198
2.59M
    *val = nm.a;
199
2.59M
    *in = p;
200
2.59M
    return ret;
201
202
27.7k
err:
203
27.7k
    if (nm.x != NULL)
204
27.7k
        X509_NAME_free(nm.x);
205
27.7k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
206
27.7k
        local_sk_X509_NAME_ENTRY_pop_free);
207
27.7k
    ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
208
27.7k
    return 0;
209
2.62M
}
210
211
static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out,
212
    const ASN1_ITEM *it, int tag, int aclass)
213
891k
{
214
891k
    int ret;
215
891k
    X509_NAME *a = (X509_NAME *)*val;
216
217
891k
    if (a->modified) {
218
58.5k
        ret = x509_name_encode(a);
219
58.5k
        if (ret < 0)
220
0
            return ret;
221
58.5k
        ret = x509_name_canon(a);
222
58.5k
        if (!ret)
223
2.95k
            return -1;
224
58.5k
    }
225
888k
    ret = a->bytes->length;
226
888k
    if (out != NULL) {
227
187k
        memcpy(*out, a->bytes->data, ret);
228
187k
        *out += ret;
229
187k
    }
230
888k
    return ret;
231
891k
}
232
233
static int x509_name_encode(X509_NAME *a)
234
10.5k
{
235
10.5k
    union {
236
10.5k
        STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
237
10.5k
        const ASN1_VALUE *a;
238
10.5k
    } intname = {
239
10.5k
        NULL
240
10.5k
    };
241
10.5k
    int len;
242
10.5k
    unsigned char *p;
243
10.5k
    STACK_OF(X509_NAME_ENTRY) *entries = NULL;
244
10.5k
    X509_NAME_ENTRY *entry;
245
10.5k
    int i, set = -1;
246
247
10.5k
    intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
248
10.5k
    if (!intname.s)
249
0
        goto memerr;
250
824k
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
251
814k
        entry = sk_X509_NAME_ENTRY_value(a->entries, i);
252
814k
        if (entry->set != set) {
253
397k
            entries = sk_X509_NAME_ENTRY_new_null();
254
397k
            if (!entries)
255
0
                goto memerr;
256
397k
            if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
257
0
                sk_X509_NAME_ENTRY_free(entries);
258
0
                goto memerr;
259
0
            }
260
397k
            set = entry->set;
261
397k
        }
262
814k
        if (!sk_X509_NAME_ENTRY_push(entries, entry))
263
0
            goto memerr;
264
814k
    }
265
10.5k
    len = ASN1_item_ex_i2d(&intname.a, NULL,
266
10.5k
        ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
267
10.5k
    if (!BUF_MEM_grow(a->bytes, len))
268
0
        goto memerr;
269
10.5k
    p = (unsigned char *)a->bytes->data;
270
10.5k
    ASN1_item_ex_i2d(&intname.a,
271
10.5k
        &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
272
10.5k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
273
10.5k
        local_sk_X509_NAME_ENTRY_free);
274
10.5k
    a->modified = 0;
275
10.5k
    return len;
276
0
memerr:
277
0
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
278
0
        local_sk_X509_NAME_ENTRY_free);
279
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
280
0
    return -1;
281
10.5k
}
282
283
static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
284
    int indent,
285
    const char *fname, const ASN1_PCTX *pctx)
286
62.1k
{
287
62.1k
    if (X509_NAME_print_ex(out, (const X509_NAME *)*pval,
288
62.1k
            indent, pctx->nm_flags)
289
62.1k
        <= 0)
290
213
        return 0;
291
61.9k
    return 2;
292
62.1k
}
293
294
/*
295
 * This function generates the canonical encoding of the Name structure. In
296
 * it all strings are converted to UTF8, leading, trailing and multiple
297
 * spaces collapsed, converted to lower case and the leading SEQUENCE header
298
 * removed. In future we could also normalize the UTF8 too. By doing this
299
 * comparison of Name structures can be rapidly performed by just using
300
 * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
301
 * constraints of type dirName can also be checked with a simple memcmp().
302
 * NOTE: For empty X509_NAME (NULL-DN), canon_enclen == 0 && canon_enc == NULL
303
 */
304
305
static int x509_name_canon(X509_NAME *a)
306
2.68M
{
307
2.68M
    unsigned char *p;
308
2.68M
    STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname;
309
2.68M
    STACK_OF(X509_NAME_ENTRY) *entries = NULL;
310
2.68M
    X509_NAME_ENTRY *entry, *tmpentry = NULL;
311
2.68M
    int i, set = -1, ret = 0, len;
312
313
2.68M
    OPENSSL_free(a->canon_enc);
314
2.68M
    a->canon_enc = NULL;
315
    /* Special case: empty X509_NAME => null encoding */
316
2.68M
    if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
317
1.91M
        a->canon_enclen = 0;
318
1.91M
        return 1;
319
1.91M
    }
320
771k
    intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
321
771k
    if (intname == NULL) {
322
0
        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
323
0
        goto err;
324
0
    }
325
22.6M
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
326
21.8M
        entry = sk_X509_NAME_ENTRY_value(a->entries, i);
327
21.8M
        if (entry->set != set) {
328
9.05M
            entries = sk_X509_NAME_ENTRY_new_null();
329
9.05M
            if (entries == NULL)
330
0
                goto err;
331
9.05M
            if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
332
0
                sk_X509_NAME_ENTRY_free(entries);
333
0
                ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
334
0
                goto err;
335
0
            }
336
9.05M
            set = entry->set;
337
9.05M
        }
338
21.8M
        tmpentry = X509_NAME_ENTRY_new();
339
21.8M
        if (tmpentry == NULL) {
340
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
341
0
            goto err;
342
0
        }
343
21.8M
        tmpentry->object = OBJ_dup(entry->object);
344
21.8M
        if (tmpentry->object == NULL) {
345
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
346
0
            goto err;
347
0
        }
348
21.8M
        if (!asn1_string_canon(tmpentry->value, entry->value))
349
30.7k
            goto err;
350
21.8M
        if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
351
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
352
0
            goto err;
353
0
        }
354
21.8M
        tmpentry = NULL;
355
21.8M
    }
356
357
    /* Finally generate encoding */
358
740k
    len = i2d_name_canon(intname, NULL);
359
740k
    if (len < 0)
360
0
        goto err;
361
740k
    a->canon_enclen = len;
362
363
740k
    p = OPENSSL_malloc(a->canon_enclen);
364
740k
    if (p == NULL) {
365
0
        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
366
0
        goto err;
367
0
    }
368
369
740k
    a->canon_enc = p;
370
371
740k
    i2d_name_canon(intname, &p);
372
373
740k
    ret = 1;
374
375
771k
err:
376
771k
    X509_NAME_ENTRY_free(tmpentry);
377
771k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
378
771k
        local_sk_X509_NAME_ENTRY_pop_free);
379
771k
    return ret;
380
740k
}
381
382
/* Bitmap of all the types of string that will be canonicalized. */
383
384
#define ASN1_MASK_CANON                                                \
385
21.8M
    (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING     \
386
21.8M
        | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
387
21.8M
        | B_ASN1_VISIBLESTRING)
388
389
static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in)
390
21.8M
{
391
21.8M
    unsigned char *to, *from;
392
21.8M
    int len, i;
393
394
    /* If type not in bitmask just copy string across */
395
21.8M
    if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
396
11.6M
        if (!ASN1_STRING_copy(out, in))
397
0
            return 0;
398
11.6M
        return 1;
399
11.6M
    }
400
401
10.1M
    out->type = V_ASN1_UTF8STRING;
402
10.1M
    out->length = ASN1_STRING_to_UTF8(&out->data, in);
403
10.1M
    if (out->length == -1)
404
30.7k
        return 0;
405
406
10.1M
    to = out->data;
407
10.1M
    from = to;
408
409
10.1M
    len = out->length;
410
411
    /*
412
     * Convert string in place to canonical form. Ultimately we may need to
413
     * handle a wider range of characters but for now ignore anything with
414
     * MSB set and rely on the ossl_isspace() to fail on bad characters without
415
     * needing isascii or range checks as well.
416
     */
417
418
    /* Ignore leading spaces */
419
10.2M
    while (len > 0 && ossl_isspace(*from)) {
420
70.9k
        from++;
421
70.9k
        len--;
422
70.9k
    }
423
424
10.1M
    to = from + len;
425
426
    /* Ignore trailing spaces */
427
10.1M
    while (len > 0 && ossl_isspace(to[-1])) {
428
21.2k
        to--;
429
21.2k
        len--;
430
21.2k
    }
431
432
10.1M
    to = out->data;
433
434
10.1M
    i = 0;
435
1.07G
    while (i < len) {
436
        /* If not ASCII set just copy across */
437
1.05G
        if (!ossl_isascii(*from)) {
438
947M
            *to++ = *from++;
439
947M
            i++;
440
947M
        }
441
        /* Collapse multiple spaces */
442
112M
        else if (ossl_isspace(*from)) {
443
            /* Copy one space across */
444
3.02M
            *to++ = ' ';
445
            /*
446
             * Ignore subsequent spaces. Note: don't need to check len here
447
             * because we know the last character is a non-space so we can't
448
             * overflow.
449
             */
450
7.70M
            do {
451
7.70M
                from++;
452
7.70M
                i++;
453
7.70M
            } while (ossl_isspace(*from));
454
109M
        } else {
455
109M
            *to++ = ossl_tolower(*from);
456
109M
            from++;
457
109M
            i++;
458
109M
        }
459
1.05G
    }
460
461
10.1M
    out->length = to - out->data;
462
463
10.1M
    return 1;
464
10.1M
}
465
466
static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
467
    unsigned char **in)
468
1.48M
{
469
1.48M
    int i, len, ltmp;
470
1.48M
    const ASN1_VALUE *v;
471
1.48M
    STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
472
473
1.48M
    len = 0;
474
19.3M
    for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
475
17.8M
        v = sk_ASN1_VALUE_value(intname, i);
476
17.8M
        ltmp = ASN1_item_ex_i2d(&v, in,
477
17.8M
            ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
478
17.8M
        if (ltmp < 0 || len > INT_MAX - ltmp)
479
0
            return -1;
480
17.8M
        len += ltmp;
481
17.8M
    }
482
1.48M
    return len;
483
1.48M
}
484
485
int X509_NAME_set(X509_NAME **xn, const X509_NAME *name)
486
67.4k
{
487
67.4k
    X509_NAME *name_copy;
488
489
67.4k
    if (*xn == name)
490
0
        return *xn != NULL;
491
67.4k
    if ((name_copy = X509_NAME_dup(name)) == NULL)
492
0
        return 0;
493
67.4k
    X509_NAME_free(*xn);
494
67.4k
    *xn = name_copy;
495
67.4k
    return 1;
496
67.4k
}
497
498
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
499
189k
{
500
189k
    char *s, *c, *b;
501
189k
    int i;
502
503
189k
    b = X509_NAME_oneline(name, NULL, 0);
504
189k
    if (b == NULL)
505
641
        return 0;
506
189k
    if (*b == '\0') {
507
124k
        OPENSSL_free(b);
508
124k
        return 1;
509
124k
    }
510
64.6k
    s = b + 1; /* skip the first slash */
511
512
64.6k
    c = s;
513
857M
    for (;;) {
514
857M
        if (((*s == '/') && (ossl_isupper(s[1]) && ((s[2] == '=') || (ossl_isupper(s[2]) && (s[3] == '='))))) || (*s == '\0')) {
515
175k
            i = s - c;
516
175k
            if (BIO_write(bp, c, i) != i)
517
0
                goto err;
518
175k
            c = s + 1; /* skip following slash */
519
175k
            if (*s != '\0') {
520
110k
                if (BIO_write(bp, ", ", 2) != 2)
521
0
                    goto err;
522
110k
            }
523
175k
        }
524
857M
        if (*s == '\0')
525
64.6k
            break;
526
857M
        s++;
527
857M
    }
528
529
64.6k
    OPENSSL_free(b);
530
64.6k
    return 1;
531
0
err:
532
0
    ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
533
0
    OPENSSL_free(b);
534
0
    return 0;
535
64.6k
}
536
537
int X509_NAME_get0_der(const X509_NAME *nm, const unsigned char **pder,
538
    size_t *pderlen)
539
0
{
540
    /* Make sure encoding is valid */
541
0
    if (i2d_X509_NAME(nm, NULL) <= 0)
542
0
        return 0;
543
0
    if (pder != NULL)
544
0
        *pder = (unsigned char *)nm->bytes->data;
545
0
    if (pderlen != NULL)
546
0
        *pderlen = nm->bytes->length;
547
0
    return 1;
548
0
}