Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/x509/v3_san.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-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 "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
73.3k
{
55
73.3k
    int i;
56
73.3k
    GENERAL_NAME *gen;
57
73.3k
    STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret;
58
59
227k
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
60
175k
        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
175k
        tmpret = i2v_GENERAL_NAME(method, gen, ret);
67
175k
        if (tmpret == NULL) {
68
21.1k
            if (origret == NULL)
69
20.5k
                sk_CONF_VALUE_pop_free(ret, X509V3_conf_free);
70
21.1k
            return NULL;
71
21.1k
        }
72
153k
        ret = tmpret;
73
153k
    }
74
52.2k
    if (ret == NULL)
75
14.0k
        return sk_CONF_VALUE_new_null();
76
38.2k
    return ret;
77
52.2k
}
78
79
STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
80
                                       GENERAL_NAME *gen,
81
                                       STACK_OF(CONF_VALUE) *ret)
82
185k
{
83
185k
    char othername[300];
84
185k
    char oline[256], *tmp;
85
86
185k
    switch (gen->type) {
87
38.7k
    case GEN_OTHERNAME:
88
38.7k
        switch (OBJ_obj2nid(gen->d.otherName->type_id)) {
89
1.35k
        case NID_id_on_SmtpUTF8Mailbox:
90
1.35k
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
91
1.35k
                    || !x509v3_add_len_value_uchar("othername: SmtpUTF8Mailbox",
92
539
                            gen->d.otherName->value->value.utf8string->data,
93
539
                            gen->d.otherName->value->value.utf8string->length,
94
539
                            &ret))
95
1.26k
                return NULL;
96
92
            break;
97
320
        case NID_XmppAddr:
98
320
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
99
320
                    || !x509v3_add_len_value_uchar("othername: XmppAddr",
100
169
                            gen->d.otherName->value->value.utf8string->data,
101
169
                            gen->d.otherName->value->value.utf8string->length,
102
169
                            &ret))
103
204
                return NULL;
104
116
            break;
105
1.03k
        case NID_SRVName:
106
1.03k
            if (gen->d.otherName->value->type != V_ASN1_IA5STRING
107
1.03k
                    || !x509v3_add_len_value_uchar("othername: SRVName",
108
815
                            gen->d.otherName->value->value.ia5string->data,
109
815
                            gen->d.otherName->value->value.ia5string->length,
110
815
                            &ret))
111
978
                return NULL;
112
59
            break;
113
294
        case NID_ms_upn:
114
294
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
115
294
                    || !x509v3_add_len_value_uchar("othername: UPN",
116
157
                            gen->d.otherName->value->value.utf8string->data,
117
157
                            gen->d.otherName->value->value.utf8string->length,
118
157
                            &ret))
119
229
                return NULL;
120
65
            break;
121
6.04k
        case NID_NAIRealm:
122
6.04k
            if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
123
6.04k
                    || !x509v3_add_len_value_uchar("othername: NAIRealm",
124
4.19k
                            gen->d.otherName->value->value.utf8string->data,
125
4.19k
                            gen->d.otherName->value->value.utf8string->length,
126
4.19k
                            &ret))
127
2.23k
                return NULL;
128
3.80k
            break;
129
29.7k
        default:
130
29.7k
            if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0)
131
29.7k
                BIO_snprintf(othername, sizeof(othername), "othername: %s",
132
29.7k
                             oline);
133
0
            else
134
0
                OPENSSL_strlcpy(othername, "othername", sizeof(othername));
135
136
            /* check if the value is something printable */
137
29.7k
            if (gen->d.otherName->value->type == V_ASN1_IA5STRING) {
138
8.00k
                if (x509v3_add_len_value_uchar(othername,
139
8.00k
                             gen->d.otherName->value->value.ia5string->data,
140
8.00k
                             gen->d.otherName->value->value.ia5string->length,
141
8.00k
                             &ret))
142
6.43k
                    return ret;
143
8.00k
            }
144
23.2k
            if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) {
145
8.36k
                if (x509v3_add_len_value_uchar(othername,
146
8.36k
                             gen->d.otherName->value->value.utf8string->data,
147
8.36k
                             gen->d.otherName->value->value.utf8string->length,
148
8.36k
                             &ret))
149
4.95k
                    return ret;
150
8.36k
            }
151
18.3k
            if (!X509V3_add_value(othername, "<unsupported>", &ret))
152
0
                return NULL;
153
18.3k
            break;
154
38.7k
        }
155
22.4k
        break;
156
157
22.4k
    case GEN_X400:
158
8.00k
        if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
159
0
            return NULL;
160
8.00k
        break;
161
162
8.00k
    case GEN_EDIPARTY:
163
1.03k
        if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
164
0
            return NULL;
165
1.03k
        break;
166
167
22.1k
    case GEN_EMAIL:
168
22.1k
        if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data,
169
22.1k
                                        gen->d.ia5->length, &ret))
170
8.68k
            return NULL;
171
13.4k
        break;
172
173
34.7k
    case GEN_DNS:
174
34.7k
        if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data,
175
34.7k
                                        gen->d.ia5->length, &ret))
176
4.52k
            return NULL;
177
30.2k
        break;
178
179
41.3k
    case GEN_URI:
180
41.3k
        if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data,
181
41.3k
                                        gen->d.ia5->length, &ret))
182
4.83k
            return NULL;
183
36.4k
        break;
184
185
36.4k
    case GEN_DIRNAME:
186
6.16k
        if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL
187
6.16k
                || !X509V3_add_value("DirName", oline, &ret))
188
0
            return NULL;
189
6.16k
        break;
190
191
24.9k
    case GEN_IPADD:
192
24.9k
        tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
193
24.9k
        if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret))
194
0
            ret = NULL;
195
24.9k
        OPENSSL_free(tmp);
196
24.9k
        break;
197
198
8.43k
    case GEN_RID:
199
8.43k
        i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
200
8.43k
        if (!X509V3_add_value("Registered ID", oline, &ret))
201
0
            return NULL;
202
8.43k
        break;
203
185k
    }
204
151k
    return ret;
205
185k
}
206
207
int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
208
119k
{
209
119k
    char *tmp;
210
119k
    int nid;
211
212
119k
    switch (gen->type) {
213
1.38k
    case GEN_OTHERNAME:
214
1.38k
        nid = OBJ_obj2nid(gen->d.otherName->type_id);
215
        /* Validate the types are as we expect before we use them */
216
1.38k
        if ((nid == NID_SRVName
217
1.38k
             && gen->d.otherName->value->type != V_ASN1_IA5STRING)
218
1.38k
                || (nid != NID_SRVName
219
1.36k
                    && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) {
220
713
            BIO_printf(out, "othername:<unsupported>");
221
713
            break;
222
713
        }
223
224
671
        switch (nid) {
225
17
        case NID_id_on_SmtpUTF8Mailbox:
226
17
            BIO_printf(out, "othername:SmtpUTF8Mailbox:%.*s",
227
17
                       gen->d.otherName->value->value.utf8string->length,
228
17
                       gen->d.otherName->value->value.utf8string->data);
229
17
            break;
230
16
        case NID_XmppAddr:
231
16
            BIO_printf(out, "othername:XmppAddr:%.*s",
232
16
                       gen->d.otherName->value->value.utf8string->length,
233
16
                       gen->d.otherName->value->value.utf8string->data);
234
16
            break;
235
19
        case NID_SRVName:
236
19
            BIO_printf(out, "othername:SRVName:%.*s",
237
19
                       gen->d.otherName->value->value.ia5string->length,
238
19
                       gen->d.otherName->value->value.ia5string->data);
239
19
            break;
240
69
        case NID_ms_upn:
241
69
            BIO_printf(out, "othername:UPN:%.*s",
242
69
                       gen->d.otherName->value->value.utf8string->length,
243
69
                       gen->d.otherName->value->value.utf8string->data);
244
69
            break;
245
20
        case NID_NAIRealm:
246
20
            BIO_printf(out, "othername:NAIRealm:%.*s",
247
20
                       gen->d.otherName->value->value.utf8string->length,
248
20
                       gen->d.otherName->value->value.utf8string->data);
249
20
            break;
250
530
        default:
251
530
            BIO_printf(out, "othername:<unsupported>");
252
530
            break;
253
671
        }
254
671
        break;
255
256
4.54k
    case GEN_X400:
257
4.54k
        BIO_printf(out, "X400Name:<unsupported>");
258
4.54k
        break;
259
260
486
    case GEN_EDIPARTY:
261
        /* Maybe fix this: it is supported now */
262
486
        BIO_printf(out, "EdiPartyName:<unsupported>");
263
486
        break;
264
265
10.7k
    case GEN_EMAIL:
266
10.7k
        BIO_printf(out, "email:");
267
10.7k
        ASN1_STRING_print(out, gen->d.ia5);
268
10.7k
        break;
269
270
6.62k
    case GEN_DNS:
271
6.62k
        BIO_printf(out, "DNS:");
272
6.62k
        ASN1_STRING_print(out, gen->d.ia5);
273
6.62k
        break;
274
275
7.18k
    case GEN_URI:
276
7.18k
        BIO_printf(out, "URI:");
277
7.18k
        ASN1_STRING_print(out, gen->d.ia5);
278
7.18k
        break;
279
280
38.9k
    case GEN_DIRNAME:
281
38.9k
        BIO_printf(out, "DirName:");
282
38.9k
        X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
283
38.9k
        break;
284
285
41.2k
    case GEN_IPADD:
286
41.2k
        tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
287
41.2k
        if (tmp == NULL)
288
0
            return 0;
289
41.2k
        BIO_printf(out, "IP Address:%s", tmp);
290
41.2k
        OPENSSL_free(tmp);
291
41.2k
        break;
292
293
7.86k
    case GEN_RID:
294
7.86k
        BIO_printf(out, "Registered ID:");
295
7.86k
        i2a_ASN1_OBJECT(out, gen->d.rid);
296
7.86k
        break;
297
119k
    }
298
119k
    return 1;
299
119k
}
300
301
static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
302
                                     X509V3_CTX *ctx,
303
                                     STACK_OF(CONF_VALUE) *nval)
304
0
{
305
0
    const int num = sk_CONF_VALUE_num(nval);
306
0
    GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num);
307
0
    int i;
308
309
0
    if (gens == NULL) {
310
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
311
0
        return NULL;
312
0
    }
313
0
    for (i = 0; i < num; i++) {
314
0
        CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
315
316
0
        if (!ossl_v3_name_cmp(cnf->name, "issuer")
317
0
            && cnf->value && strcmp(cnf->value, "copy") == 0) {
318
0
            if (!copy_issuer(ctx, gens))
319
0
                goto err;
320
0
        } else {
321
0
            GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf);
322
323
0
            if (gen == NULL)
324
0
                goto err;
325
0
            sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
326
0
        }
327
0
    }
328
0
    return gens;
329
0
 err:
330
0
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
331
0
    return NULL;
332
0
}
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
0
{
338
0
    GENERAL_NAMES *ialt = NULL;
339
0
    GENERAL_NAME *gen;
340
0
    X509_EXTENSION *ext;
341
0
    int i, num;
342
343
0
    if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
344
0
        return 1;
345
0
    if (!ctx || !ctx->issuer_cert) {
346
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_DETAILS);
347
0
        goto err;
348
0
    }
349
0
    i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
350
0
    if (i < 0)
351
0
        return 1;
352
0
    if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL
353
0
        || (ialt = X509V3_EXT_d2i(ext)) == NULL) {
354
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_ISSUER_DECODE_ERROR);
355
0
        goto err;
356
0
    }
357
358
0
    num = sk_GENERAL_NAME_num(ialt);
359
0
    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
0
    for (i = 0; i < num; i++) {
365
0
        gen = sk_GENERAL_NAME_value(ialt, i);
366
0
        sk_GENERAL_NAME_push(gens, gen);     /* no failure as it was reserved */
367
0
    }
368
0
    sk_GENERAL_NAME_free(ialt);
369
370
0
    return 1;
371
372
0
 err:
373
0
    sk_GENERAL_NAME_free(ialt);
374
0
    return 0;
375
376
0
}
377
378
static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
379
                                      X509V3_CTX *ctx,
380
                                      STACK_OF(CONF_VALUE) *nval)
381
0
{
382
0
    GENERAL_NAMES *gens;
383
0
    CONF_VALUE *cnf;
384
0
    const int num = sk_CONF_VALUE_num(nval);
385
0
    int i;
386
387
0
    gens = sk_GENERAL_NAME_new_reserve(NULL, num);
388
0
    if (gens == NULL) {
389
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
390
0
        return NULL;
391
0
    }
392
393
0
    for (i = 0; i < num; i++) {
394
0
        cnf = sk_CONF_VALUE_value(nval, i);
395
0
        if (ossl_v3_name_cmp(cnf->name, "email") == 0
396
0
            && cnf->value && strcmp(cnf->value, "copy") == 0) {
397
0
            if (!copy_email(ctx, gens, 0))
398
0
                goto err;
399
0
        } else if (ossl_v3_name_cmp(cnf->name, "email") == 0
400
0
                   && cnf->value && strcmp(cnf->value, "move") == 0) {
401
0
            if (!copy_email(ctx, gens, 1))
402
0
                goto err;
403
0
        } else {
404
0
            GENERAL_NAME *gen;
405
0
            if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
406
0
                goto err;
407
0
            sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
408
0
        }
409
0
    }
410
0
    return gens;
411
0
 err:
412
0
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
413
0
    return NULL;
414
0
}
415
416
/*
417
 * Copy any email addresses in a certificate or request to GENERAL_NAMES
418
 */
419
420
static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
421
0
{
422
0
    X509_NAME *nm;
423
0
    ASN1_IA5STRING *email = NULL;
424
0
    X509_NAME_ENTRY *ne;
425
0
    GENERAL_NAME *gen = NULL;
426
0
    int i = -1;
427
428
0
    if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
429
0
        return 1;
430
0
    if (ctx == NULL
431
0
        || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
432
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
433
0
        return 0;
434
0
    }
435
    /* Find the subject name */
436
0
    nm = ctx->subject_cert != NULL ?
437
0
        X509_get_subject_name(ctx->subject_cert) :
438
0
        X509_REQ_get_subject_name(ctx->subject_req);
439
440
    /* Now add any email address(es) to STACK */
441
0
    while ((i = X509_NAME_get_index_by_NID(nm,
442
0
                                           NID_pkcs9_emailAddress, i)) >= 0) {
443
0
        ne = X509_NAME_get_entry(nm, i);
444
0
        email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
445
0
        if (move_p) {
446
0
            X509_NAME_delete_entry(nm, i);
447
0
            X509_NAME_ENTRY_free(ne);
448
0
            i--;
449
0
        }
450
0
        if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
451
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
452
0
            goto err;
453
0
        }
454
0
        gen->d.ia5 = email;
455
0
        email = NULL;
456
0
        gen->type = GEN_EMAIL;
457
0
        if (!sk_GENERAL_NAME_push(gens, gen)) {
458
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
459
0
            goto err;
460
0
        }
461
0
        gen = NULL;
462
0
    }
463
464
0
    return 1;
465
466
0
 err:
467
0
    GENERAL_NAME_free(gen);
468
0
    ASN1_IA5STRING_free(email);
469
0
    return 0;
470
471
0
}
472
473
GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
474
                                 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
475
0
{
476
0
    GENERAL_NAME *gen;
477
0
    GENERAL_NAMES *gens;
478
0
    CONF_VALUE *cnf;
479
0
    const int num = sk_CONF_VALUE_num(nval);
480
0
    int i;
481
482
0
    gens = sk_GENERAL_NAME_new_reserve(NULL, num);
483
0
    if (gens == NULL) {
484
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
485
0
        return NULL;
486
0
    }
487
488
0
    for (i = 0; i < num; i++) {
489
0
        cnf = sk_CONF_VALUE_value(nval, i);
490
0
        if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
491
0
            goto err;
492
0
        sk_GENERAL_NAME_push(gens, gen);    /* no failure as it was reserved */
493
0
    }
494
0
    return gens;
495
0
 err:
496
0
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
497
0
    return NULL;
498
0
}
499
500
GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
501
                               X509V3_CTX *ctx, CONF_VALUE *cnf)
502
0
{
503
0
    return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
504
0
}
505
506
GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
507
                               const X509V3_EXT_METHOD *method,
508
                               X509V3_CTX *ctx, int gen_type, const char *value,
509
                               int is_nc)
510
0
{
511
0
    char is_string = 0;
512
0
    GENERAL_NAME *gen = NULL;
513
514
0
    if (!value) {
515
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
516
0
        return NULL;
517
0
    }
518
519
0
    if (out)
520
0
        gen = out;
521
0
    else {
522
0
        gen = GENERAL_NAME_new();
523
0
        if (gen == NULL) {
524
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
525
0
            return NULL;
526
0
        }
527
0
    }
528
529
0
    switch (gen_type) {
530
0
    case GEN_URI:
531
0
    case GEN_EMAIL:
532
0
    case GEN_DNS:
533
0
        is_string = 1;
534
0
        break;
535
536
0
    case GEN_RID:
537
0
        {
538
0
            ASN1_OBJECT *obj;
539
0
            if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
540
0
                ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT,
541
0
                               "value=%s", value);
542
0
                goto err;
543
0
            }
544
0
            gen->d.rid = obj;
545
0
        }
546
0
        break;
547
548
0
    case GEN_IPADD:
549
0
        if (is_nc)
550
0
            gen->d.ip = a2i_IPADDRESS_NC(value);
551
0
        else
552
0
            gen->d.ip = a2i_IPADDRESS(value);
553
0
        if (gen->d.ip == NULL) {
554
0
            ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS,
555
0
                           "value=%s", value);
556
0
            goto err;
557
0
        }
558
0
        break;
559
560
0
    case GEN_DIRNAME:
561
0
        if (!do_dirname(gen, value, ctx)) {
562
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR);
563
0
            goto err;
564
0
        }
565
0
        break;
566
567
0
    case GEN_OTHERNAME:
568
0
        if (!do_othername(gen, value, ctx)) {
569
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR);
570
0
            goto err;
571
0
        }
572
0
        break;
573
0
    default:
574
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE);
575
0
        goto err;
576
0
    }
577
578
0
    if (is_string) {
579
0
        if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL ||
580
0
            !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
581
0
                             strlen(value))) {
582
0
            ASN1_IA5STRING_free(gen->d.ia5);
583
0
            gen->d.ia5 = NULL;
584
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
585
0
            goto err;
586
0
        }
587
0
    }
588
589
0
    gen->type = gen_type;
590
591
0
    return gen;
592
593
0
 err:
594
0
    if (!out)
595
0
        GENERAL_NAME_free(gen);
596
0
    return NULL;
597
0
}
598
599
GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
600
                                  const X509V3_EXT_METHOD *method,
601
                                  X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
602
0
{
603
0
    int type;
604
605
0
    char *name, *value;
606
607
0
    name = cnf->name;
608
0
    value = cnf->value;
609
610
0
    if (!value) {
611
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE);
612
0
        return NULL;
613
0
    }
614
615
0
    if (!ossl_v3_name_cmp(name, "email"))
616
0
        type = GEN_EMAIL;
617
0
    else if (!ossl_v3_name_cmp(name, "URI"))
618
0
        type = GEN_URI;
619
0
    else if (!ossl_v3_name_cmp(name, "DNS"))
620
0
        type = GEN_DNS;
621
0
    else if (!ossl_v3_name_cmp(name, "RID"))
622
0
        type = GEN_RID;
623
0
    else if (!ossl_v3_name_cmp(name, "IP"))
624
0
        type = GEN_IPADD;
625
0
    else if (!ossl_v3_name_cmp(name, "dirName"))
626
0
        type = GEN_DIRNAME;
627
0
    else if (!ossl_v3_name_cmp(name, "otherName"))
628
0
        type = GEN_OTHERNAME;
629
0
    else {
630
0
        ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION,
631
0
                       "name=%s", name);
632
0
        return NULL;
633
0
    }
634
635
0
    return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
636
637
0
}
638
639
static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
640
0
{
641
0
    char *objtmp = NULL, *p;
642
0
    int objlen;
643
644
0
    if ((p = strchr(value, ';')) == NULL)
645
0
        return 0;
646
0
    if ((gen->d.otherName = OTHERNAME_new()) == NULL)
647
0
        return 0;
648
    /*
649
     * Free this up because we will overwrite it. no need to free type_id
650
     * because it is static
651
     */
652
0
    ASN1_TYPE_free(gen->d.otherName->value);
653
0
    if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
654
0
        goto err;
655
0
    objlen = p - value;
656
0
    objtmp = OPENSSL_strndup(value, objlen);
657
0
    if (objtmp == NULL)
658
0
        goto err;
659
0
    gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
660
0
    OPENSSL_free(objtmp);
661
0
    if (!gen->d.otherName->type_id)
662
0
        goto err;
663
0
    return 1;
664
665
0
 err:
666
0
    OTHERNAME_free(gen->d.otherName);
667
0
    gen->d.otherName = NULL;
668
0
    return 0;
669
0
}
670
671
static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
672
0
{
673
0
    int ret = 0;
674
0
    STACK_OF(CONF_VALUE) *sk = NULL;
675
0
    X509_NAME *nm;
676
677
0
    if ((nm = X509_NAME_new()) == NULL)
678
0
        goto err;
679
0
    sk = X509V3_get_section(ctx, value);
680
0
    if (!sk) {
681
0
        ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND,
682
0
                       "section=%s", value);
683
0
        goto err;
684
0
    }
685
    /* FIXME: should allow other character types... */
686
0
    ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
687
0
    if (!ret)
688
0
        goto err;
689
0
    gen->d.dirn = nm;
690
691
0
err:
692
0
    if (ret == 0)
693
0
        X509_NAME_free(nm);
694
0
    X509V3_section_free(ctx, sk);
695
0
    return ret;
696
0
}