Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/x509/v3_san.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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 "internal/cryptlib.h"
12
#include "crypto/x509.h"
13
#include <openssl/conf.h>
14
#include <openssl/x509v3.h>
15
#include <openssl/bio.h>
16
#include "ext_dat.h"
17
18
static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
19
    X509V3_CTX *ctx,
20
    STACK_OF(CONF_VALUE) *nval);
21
static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
22
    X509V3_CTX *ctx,
23
    STACK_OF(CONF_VALUE) *nval);
24
static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
25
static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
26
static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
27
static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
28
29
const X509V3_EXT_METHOD ossl_v3_alt[3] = {
30
    { NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
31
        0, 0, 0, 0,
32
        0, 0,
33
        (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
34
        (X509V3_EXT_V2I)v2i_subject_alt,
35
        NULL, NULL, NULL },
36
37
    { NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
38
        0, 0, 0, 0,
39
        0, 0,
40
        (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
41
        (X509V3_EXT_V2I)v2i_issuer_alt,
42
        NULL, NULL, NULL },
43
44
    { NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES),
45
        0, 0, 0, 0,
46
        0, 0,
47
        (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
48
        NULL, NULL, NULL, NULL },
49
};
50
51
STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
52
    GENERAL_NAMES *gens,
53
    STACK_OF(CONF_VALUE) *ret)
54
61.8k
{
55
61.8k
    int i;
56
61.8k
    GENERAL_NAME *gen;
57
61.8k
    STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret;
58
59
193k
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
60
151k
        gen = sk_GENERAL_NAME_value(gens, i);
61
        /*
62
         * i2v_GENERAL_NAME allocates ret if it is NULL. If something goes
63
         * wrong we need to free the stack - but only if it was empty when we
64
         * originally entered this function.
65
         */
66
151k
        tmpret = i2v_GENERAL_NAME(method, gen, ret);
67
151k
        if (tmpret == NULL) {
68
19.9k
            if (origret == NULL)
69
18.8k
                sk_CONF_VALUE_pop_free(ret, X509V3_conf_free);
70
19.9k
            return NULL;
71
19.9k
        }
72
131k
        ret = tmpret;
73
131k
    }
74
41.8k
    if (ret == NULL)
75
7.14k
        return sk_CONF_VALUE_new_null();
76
34.7k
    return ret;
77
41.8k
}
78
79
STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
80
    GENERAL_NAME *gen,
81
    STACK_OF(CONF_VALUE) *ret)
82
154k
{
83
154k
    char othername[300];
84
154k
    char oline[256], *tmp;
85
86
154k
    switch (gen->type) {
87
30.2k
    case GEN_OTHERNAME:
88
30.2k
        switch (OBJ_obj2nid(gen->d.otherName->type_id)) {
89
794
        case NID_id_on_SmtpUTF8Mailbox:
90
794
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
91
561
                || !x509v3_add_len_value_uchar("othername: SmtpUTF8Mailbox",
92
561
                    gen->d.otherName->value->value.utf8string->data,
93
561
                    gen->d.otherName->value->value.utf8string->length,
94
561
                    &ret))
95
644
                return NULL;
96
150
            break;
97
935
        case NID_XmppAddr:
98
935
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
99
428
                || !x509v3_add_len_value_uchar("othername: XmppAddr",
100
428
                    gen->d.otherName->value->value.utf8string->data,
101
428
                    gen->d.otherName->value->value.utf8string->length,
102
428
                    &ret))
103
782
                return NULL;
104
153
            break;
105
496
        case NID_SRVName:
106
496
            if (gen->d.otherName->value->type != V_ASN1_IA5STRING
107
240
                || !x509v3_add_len_value_uchar("othername: SRVName",
108
240
                    gen->d.otherName->value->value.ia5string->data,
109
240
                    gen->d.otherName->value->value.ia5string->length,
110
240
                    &ret))
111
442
                return NULL;
112
54
            break;
113
480
        case NID_ms_upn:
114
480
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
115
317
                || !x509v3_add_len_value_uchar("othername: UPN",
116
317
                    gen->d.otherName->value->value.utf8string->data,
117
317
                    gen->d.otherName->value->value.utf8string->length,
118
317
                    &ret))
119
370
                return NULL;
120
110
            break;
121
6.11k
        case NID_NAIRealm:
122
6.11k
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
123
4.07k
                || !x509v3_add_len_value_uchar("othername: NAIRealm",
124
4.07k
                    gen->d.otherName->value->value.utf8string->data,
125
4.07k
                    gen->d.otherName->value->value.utf8string->length,
126
4.07k
                    &ret))
127
2.39k
                return NULL;
128
3.72k
            break;
129
21.4k
        default:
130
21.4k
            if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0)
131
21.4k
                BIO_snprintf(othername, sizeof(othername), "othername: %s",
132
21.4k
                    oline);
133
0
            else
134
0
                OPENSSL_strlcpy(othername, "othername", sizeof(othername));
135
136
            /* check if the value is something printable */
137
21.4k
            if (gen->d.otherName->value->type == V_ASN1_IA5STRING) {
138
4.18k
                if (x509v3_add_len_value_uchar(othername,
139
4.18k
                        gen->d.otherName->value->value.ia5string->data,
140
4.18k
                        gen->d.otherName->value->value.ia5string->length,
141
4.18k
                        &ret))
142
3.21k
                    return ret;
143
4.18k
            }
144
18.2k
            if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) {
145
6.63k
                if (x509v3_add_len_value_uchar(othername,
146
6.63k
                        gen->d.otherName->value->value.utf8string->data,
147
6.63k
                        gen->d.otherName->value->value.utf8string->length,
148
6.63k
                        &ret))
149
3.06k
                    return ret;
150
6.63k
            }
151
15.1k
            if (!X509V3_add_value(othername, "<unsupported>", &ret))
152
0
                return NULL;
153
15.1k
            break;
154
30.2k
        }
155
19.3k
        break;
156
157
19.3k
    case GEN_X400:
158
6.61k
        if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
159
0
            return NULL;
160
6.61k
        break;
161
162
6.61k
    case GEN_EDIPARTY:
163
1.01k
        if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
164
0
            return NULL;
165
1.01k
        break;
166
167
18.8k
    case GEN_EMAIL:
168
18.8k
        if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data,
169
18.8k
                gen->d.ia5->length, &ret))
170
8.21k
            return NULL;
171
10.6k
        break;
172
173
30.2k
    case GEN_DNS:
174
30.2k
        if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data,
175
30.2k
                gen->d.ia5->length, &ret))
176
3.01k
            return NULL;
177
27.2k
        break;
178
179
36.4k
    case GEN_URI:
180
36.4k
        if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data,
181
36.4k
                gen->d.ia5->length, &ret))
182
4.91k
            return NULL;
183
31.5k
        break;
184
185
31.5k
    case GEN_DIRNAME:
186
6.41k
        if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL
187
6.41k
            || !X509V3_add_value("DirName", oline, &ret))
188
0
            return NULL;
189
6.41k
        break;
190
191
21.1k
    case GEN_IPADD:
192
21.1k
        tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
193
21.1k
        if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret))
194
0
            ret = NULL;
195
21.1k
        OPENSSL_free(tmp);
196
21.1k
        break;
197
198
3.72k
    case GEN_RID:
199
3.72k
        i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
200
3.72k
        if (!X509V3_add_value("Registered ID", oline, &ret))
201
0
            return NULL;
202
3.72k
        break;
203
154k
    }
204
127k
    return ret;
205
154k
}
206
207
int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
208
78.4k
{
209
78.4k
    char *tmp;
210
78.4k
    int nid;
211
212
78.4k
    switch (gen->type) {
213
1.01k
    case GEN_OTHERNAME:
214
1.01k
        nid = OBJ_obj2nid(gen->d.otherName->type_id);
215
        /* Validate the types are as we expect before we use them */
216
1.01k
        if ((nid == NID_SRVName
217
27
                && gen->d.otherName->value->type != V_ASN1_IA5STRING)
218
999
            || (nid != NID_SRVName
219
986
                && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) {
220
653
            BIO_printf(out, "othername:<unsupported>");
221
653
            break;
222
653
        }
223
224
360
        switch (nid) {
225
12
        case NID_id_on_SmtpUTF8Mailbox:
226
12
            BIO_printf(out, "othername:SmtpUTF8Mailbox:%.*s",
227
12
                gen->d.otherName->value->value.utf8string->length,
228
12
                gen->d.otherName->value->value.utf8string->data);
229
12
            break;
230
13
        case NID_XmppAddr:
231
13
            BIO_printf(out, "othername:XmppAddr:%.*s",
232
13
                gen->d.otherName->value->value.utf8string->length,
233
13
                gen->d.otherName->value->value.utf8string->data);
234
13
            break;
235
13
        case NID_SRVName:
236
13
            BIO_printf(out, "othername:SRVName:%.*s",
237
13
                gen->d.otherName->value->value.ia5string->length,
238
13
                gen->d.otherName->value->value.ia5string->data);
239
13
            break;
240
46
        case NID_ms_upn:
241
46
            BIO_printf(out, "othername:UPN:%.*s",
242
46
                gen->d.otherName->value->value.utf8string->length,
243
46
                gen->d.otherName->value->value.utf8string->data);
244
46
            break;
245
12
        case NID_NAIRealm:
246
12
            BIO_printf(out, "othername:NAIRealm:%.*s",
247
12
                gen->d.otherName->value->value.utf8string->length,
248
12
                gen->d.otherName->value->value.utf8string->data);
249
12
            break;
250
264
        default:
251
264
            BIO_printf(out, "othername:<unsupported>");
252
264
            break;
253
360
        }
254
360
        break;
255
256
5.21k
    case GEN_X400:
257
5.21k
        BIO_printf(out, "X400Name:<unsupported>");
258
5.21k
        break;
259
260
75
    case GEN_EDIPARTY:
261
        /* Maybe fix this: it is supported now */
262
75
        BIO_printf(out, "EdiPartyName:<unsupported>");
263
75
        break;
264
265
11.6k
    case GEN_EMAIL:
266
11.6k
        BIO_printf(out, "email:");
267
11.6k
        ASN1_STRING_print(out, gen->d.ia5);
268
11.6k
        break;
269
270
4.78k
    case GEN_DNS:
271
4.78k
        BIO_printf(out, "DNS:");
272
4.78k
        ASN1_STRING_print(out, gen->d.ia5);
273
4.78k
        break;
274
275
7.95k
    case GEN_URI:
276
7.95k
        BIO_printf(out, "URI:");
277
7.95k
        ASN1_STRING_print(out, gen->d.ia5);
278
7.95k
        break;
279
280
19.3k
    case GEN_DIRNAME:
281
19.3k
        BIO_printf(out, "DirName:");
282
19.3k
        X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
283
19.3k
        break;
284
285
22.6k
    case GEN_IPADD:
286
22.6k
        tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
287
22.6k
        if (tmp == NULL)
288
0
            return 0;
289
22.6k
        BIO_printf(out, "IP Address:%s", tmp);
290
22.6k
        OPENSSL_free(tmp);
291
22.6k
        break;
292
293
5.75k
    case GEN_RID:
294
5.75k
        BIO_printf(out, "Registered ID:");
295
5.75k
        i2a_ASN1_OBJECT(out, gen->d.rid);
296
5.75k
        break;
297
78.4k
    }
298
78.4k
    return 1;
299
78.4k
}
300
301
static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
302
    X509V3_CTX *ctx,
303
    STACK_OF(CONF_VALUE) *nval)
304
851
{
305
851
    const int num = sk_CONF_VALUE_num(nval);
306
851
    GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num);
307
851
    int i;
308
309
851
    if (gens == NULL) {
310
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
311
0
        return NULL;
312
0
    }
313
1.97k
    for (i = 0; i < num; i++) {
314
1.45k
        CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
315
316
1.45k
        if (!ossl_v3_name_cmp(cnf->name, "issuer")
317
479
            && cnf->value && strcmp(cnf->value, "copy") == 0) {
318
444
            if (!copy_issuer(ctx, gens))
319
159
                goto err;
320
1.00k
        } else {
321
1.00k
            GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf);
322
323
1.00k
            if (gen == NULL)
324
169
                goto err;
325
838
            sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
326
838
        }
327
1.45k
    }
328
523
    return gens;
329
328
err:
330
328
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
331
328
    return NULL;
332
851
}
333
334
/* Append subject altname of issuer to issuer alt name of subject */
335
336
static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
337
444
{
338
444
    GENERAL_NAMES *ialt = NULL;
339
444
    GENERAL_NAME *gen;
340
444
    const X509_EXTENSION *ext;
341
444
    int i, num;
342
343
444
    if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
344
0
        return 1;
345
444
    if (!ctx || !ctx->issuer_cert) {
346
158
        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_DETAILS);
347
158
        goto err;
348
158
    }
349
286
    i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
350
286
    if (i < 0)
351
162
        return 1;
352
124
    if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL
353
124
        || (ialt = X509V3_EXT_d2i(ext)) == NULL) {
354
1
        ERR_raise(ERR_LIB_X509V3, X509V3_R_ISSUER_DECODE_ERROR);
355
1
        goto err;
356
1
    }
357
358
123
    num = sk_GENERAL_NAME_num(ialt);
359
123
    if (!sk_GENERAL_NAME_reserve(gens, num)) {
360
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
361
0
        goto err;
362
0
    }
363
364
32.9k
    for (i = 0; i < num; i++) {
365
32.8k
        gen = sk_GENERAL_NAME_value(ialt, i);
366
32.8k
        sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
367
32.8k
    }
368
123
    sk_GENERAL_NAME_free(ialt);
369
370
123
    return 1;
371
372
159
err:
373
159
    sk_GENERAL_NAME_free(ialt);
374
159
    return 0;
375
123
}
376
377
static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
378
    X509V3_CTX *ctx,
379
    STACK_OF(CONF_VALUE) *nval)
380
857
{
381
857
    GENERAL_NAMES *gens;
382
857
    CONF_VALUE *cnf;
383
857
    const int num = sk_CONF_VALUE_num(nval);
384
857
    int i;
385
386
857
    gens = sk_GENERAL_NAME_new_reserve(NULL, num);
387
857
    if (gens == NULL) {
388
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
389
0
        return NULL;
390
0
    }
391
392
94.3k
    for (i = 0; i < num; i++) {
393
93.7k
        cnf = sk_CONF_VALUE_value(nval, i);
394
93.7k
        if (ossl_v3_name_cmp(cnf->name, "email") == 0
395
2.60k
            && cnf->value && strcmp(cnf->value, "copy") == 0) {
396
7
            if (!copy_email(ctx, gens, 0))
397
3
                goto err;
398
93.7k
        } else if (ossl_v3_name_cmp(cnf->name, "email") == 0
399
2.59k
            && cnf->value && strcmp(cnf->value, "move") == 0) {
400
9
            if (!copy_email(ctx, gens, 1))
401
4
                goto err;
402
93.7k
        } else {
403
93.7k
            GENERAL_NAME *gen;
404
93.7k
            if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
405
267
                goto err;
406
93.5k
            sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
407
93.5k
        }
408
93.7k
    }
409
583
    return gens;
410
274
err:
411
274
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
412
274
    return NULL;
413
857
}
414
415
/*
416
 * Copy any email addresses in a certificate or request to GENERAL_NAMES
417
 */
418
419
static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
420
16
{
421
16
    const X509_NAME *nm;
422
16
    ASN1_IA5STRING *email = NULL;
423
16
    X509_NAME_ENTRY *ne;
424
16
    GENERAL_NAME *gen = NULL;
425
16
    int i = -1;
426
427
16
    if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
428
0
        return 1;
429
16
    if (ctx == NULL
430
16
        || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
431
7
        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
432
7
        return 0;
433
7
    }
434
    /* Find the subject name */
435
9
    nm = ctx->subject_cert != NULL ? X509_get_subject_name(ctx->subject_cert)
436
9
                                   : X509_REQ_get_subject_name(ctx->subject_req);
437
438
    /* Now add any email address(es) to STACK */
439
9
    while ((i = X509_NAME_get_index_by_NID(nm,
440
9
                NID_pkcs9_emailAddress, i))
441
9
        >= 0) {
442
0
        if (move_p) {
443
            /* We should really not support deleting things in a const object
444
             * to rip the pointer out of it. If we truly want a new object
445
             * without this in it, we should just construct one without it.
446
             */
447
0
            return 0;
448
0
        }
449
        /* XXX Casts away const */
450
0
        ne = (X509_NAME_ENTRY *)X509_NAME_get_entry(nm, i);
451
0
        email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
452
0
        if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
453
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
454
0
            goto err;
455
0
        }
456
0
        gen->d.ia5 = email;
457
0
        email = NULL;
458
0
        gen->type = GEN_EMAIL;
459
0
        if (!sk_GENERAL_NAME_push(gens, gen)) {
460
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
461
0
            goto err;
462
0
        }
463
0
        gen = NULL;
464
0
    }
465
466
9
    return 1;
467
468
0
err:
469
0
    GENERAL_NAME_free(gen);
470
0
    ASN1_IA5STRING_free(email);
471
0
    return 0;
472
9
}
473
474
GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
475
    X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
476
60
{
477
60
    GENERAL_NAME *gen;
478
60
    GENERAL_NAMES *gens;
479
60
    CONF_VALUE *cnf;
480
60
    const int num = sk_CONF_VALUE_num(nval);
481
60
    int i;
482
483
60
    gens = sk_GENERAL_NAME_new_reserve(NULL, num);
484
60
    if (gens == NULL) {
485
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
486
0
        return NULL;
487
0
    }
488
489
317
    for (i = 0; i < num; i++) {
490
279
        cnf = sk_CONF_VALUE_value(nval, i);
491
279
        if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
492
22
            goto err;
493
257
        sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
494
257
    }
495
38
    return gens;
496
22
err:
497
22
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
498
22
    return NULL;
499
60
}
500
501
GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
502
    X509V3_CTX *ctx, CONF_VALUE *cnf)
503
129k
{
504
129k
    return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
505
129k
}
506
507
GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
508
    const X509V3_EXT_METHOD *method,
509
    X509V3_CTX *ctx, int gen_type, const char *value,
510
    int is_nc)
511
132k
{
512
132k
    char is_string = 0;
513
132k
    GENERAL_NAME *gen = NULL;
514
515
132k
    if (!value) {
516
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
517
0
        return NULL;
518
0
    }
519
520
132k
    if (out)
521
3.00k
        gen = out;
522
129k
    else {
523
129k
        gen = GENERAL_NAME_new();
524
129k
        if (gen == NULL) {
525
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
526
0
            return NULL;
527
0
        }
528
129k
    }
529
530
132k
    switch (gen_type) {
531
89.5k
    case GEN_URI:
532
94.7k
    case GEN_EMAIL:
533
95.5k
    case GEN_DNS:
534
95.5k
        is_string = 1;
535
95.5k
        break;
536
537
34.6k
    case GEN_RID: {
538
34.6k
        ASN1_OBJECT *obj;
539
34.6k
        if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
540
93
            ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT,
541
93
                "value=%s", value);
542
93
            goto err;
543
93
        }
544
34.6k
        gen->d.rid = obj;
545
34.6k
    } break;
546
547
1.07k
    case GEN_IPADD:
548
1.07k
        if (is_nc)
549
711
            gen->d.ip = a2i_IPADDRESS_NC(value);
550
365
        else
551
365
            gen->d.ip = a2i_IPADDRESS(value);
552
1.07k
        if (gen->d.ip == NULL) {
553
245
            ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS,
554
245
                "value=%s", value);
555
245
            goto err;
556
245
        }
557
831
        break;
558
559
831
    case GEN_DIRNAME:
560
758
        if (!do_dirname(gen, value, ctx)) {
561
82
            ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR);
562
82
            goto err;
563
82
        }
564
676
        break;
565
566
676
    case GEN_OTHERNAME:
567
232
        if (!do_othername(gen, value, ctx)) {
568
81
            ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR);
569
81
            goto err;
570
81
        }
571
151
        break;
572
151
    default:
573
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE);
574
0
        goto err;
575
132k
    }
576
577
131k
    if (is_string) {
578
95.5k
        if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL || !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value, (int)strlen(value))) {
579
0
            ASN1_IA5STRING_free(gen->d.ia5);
580
0
            gen->d.ia5 = NULL;
581
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
582
0
            goto err;
583
0
        }
584
95.5k
    }
585
586
131k
    gen->type = gen_type;
587
588
131k
    return gen;
589
590
501
err:
591
501
    if (!out)
592
315
        GENERAL_NAME_free(gen);
593
501
    return NULL;
594
131k
}
595
596
GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
597
    const X509V3_EXT_METHOD *method,
598
    X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
599
132k
{
600
132k
    int type;
601
602
132k
    char *name, *value;
603
604
132k
    name = cnf->name;
605
132k
    value = cnf->value;
606
607
132k
    if (!value) {
608
137
        ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
609
137
        return NULL;
610
137
    }
611
612
132k
    if (!ossl_v3_name_cmp(name, "email"))
613
5.23k
        type = GEN_EMAIL;
614
127k
    else if (!ossl_v3_name_cmp(name, "URI"))
615
89.5k
        type = GEN_URI;
616
37.7k
    else if (!ossl_v3_name_cmp(name, "DNS"))
617
802
        type = GEN_DNS;
618
36.9k
    else if (!ossl_v3_name_cmp(name, "RID"))
619
34.6k
        type = GEN_RID;
620
2.28k
    else if (!ossl_v3_name_cmp(name, "IP"))
621
1.07k
        type = GEN_IPADD;
622
1.20k
    else if (!ossl_v3_name_cmp(name, "dirName"))
623
758
        type = GEN_DIRNAME;
624
450
    else if (!ossl_v3_name_cmp(name, "otherName"))
625
232
        type = GEN_OTHERNAME;
626
218
    else {
627
218
        ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION,
628
218
            "name=%s", name);
629
218
        return NULL;
630
218
    }
631
632
132k
    return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
633
132k
}
634
635
static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
636
232
{
637
232
    char *objtmp = NULL;
638
232
    const char *p;
639
232
    size_t objlen;
640
641
232
    if ((p = strchr(value, ';')) == NULL)
642
3
        return 0;
643
229
    if ((gen->d.otherName = OTHERNAME_new()) == NULL)
644
0
        return 0;
645
    /*
646
     * Free this up because we will overwrite it. no need to free type_id
647
     * because it is static
648
     */
649
229
    ASN1_TYPE_free(gen->d.otherName->value);
650
229
    if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
651
24
        goto err;
652
205
    objlen = p - value;
653
205
    objtmp = OPENSSL_strndup(value, objlen);
654
205
    if (objtmp == NULL)
655
0
        goto err;
656
205
    gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
657
205
    OPENSSL_free(objtmp);
658
205
    if (!gen->d.otherName->type_id)
659
54
        goto err;
660
151
    return 1;
661
662
78
err:
663
78
    OTHERNAME_free(gen->d.otherName);
664
78
    gen->d.otherName = NULL;
665
78
    return 0;
666
205
}
667
668
static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
669
758
{
670
758
    int ret = 0;
671
758
    STACK_OF(CONF_VALUE) *sk = NULL;
672
758
    X509_NAME *nm;
673
674
758
    if ((nm = X509_NAME_new()) == NULL)
675
0
        goto err;
676
758
    sk = X509V3_get_section(ctx, value);
677
758
    if (!sk) {
678
18
        ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND,
679
18
            "section=%s", value);
680
18
        goto err;
681
18
    }
682
    /* FIXME: should allow other character types... */
683
740
    ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
684
740
    if (!ret)
685
64
        goto err;
686
676
    gen->d.dirn = nm;
687
688
758
err:
689
758
    if (ret == 0)
690
82
        X509_NAME_free(nm);
691
758
    X509V3_section_free(ctx, sk);
692
758
    return ret;
693
676
}