Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/x509/v3_utl.c
Line
Count
Source
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
/* X509 v3 extension utilities */
11
12
#include "internal/e_os.h"
13
#include "internal/cryptlib.h"
14
#include <stdio.h>
15
#include <string.h>
16
#include "crypto/ctype.h"
17
#include <openssl/conf.h>
18
#include <openssl/crypto.h>
19
#include <openssl/x509v3.h>
20
#include "crypto/x509.h"
21
#include <openssl/bn.h>
22
#include "ext_dat.h"
23
#include "x509_local.h"
24
25
static char *strip_spaces(char *name);
26
static int sk_strcmp(const char *const *a, const char *const *b);
27
static STACK_OF(OPENSSL_STRING) *get_email(const X509_NAME *name,
28
    GENERAL_NAMES *gens);
29
static void str_free(OPENSSL_STRING str);
30
static int append_ia5(STACK_OF(OPENSSL_STRING) **sk,
31
    const ASN1_IA5STRING *email);
32
33
static int ipv4_from_asc(unsigned char *v4, const char *in);
34
static int ipv6_from_asc(unsigned char *v6, const char *in);
35
static int ipv6_cb(const char *elem, int len, void *usr);
36
static int ipv6_hex(unsigned char *out, const char *in, int inlen);
37
38
/* Add a CONF_VALUE name value pair to stack */
39
40
static int x509v3_add_len_value(const char *name, const char *value,
41
    size_t vallen, STACK_OF(CONF_VALUE) **extlist)
42
378k
{
43
378k
    CONF_VALUE *vtmp = NULL;
44
378k
    char *tname = NULL, *tvalue = NULL;
45
378k
    int sk_allocated = (*extlist == NULL);
46
47
378k
    if (name != NULL && (tname = OPENSSL_strdup(name)) == NULL)
48
0
        goto err;
49
378k
    if (value != NULL) {
50
        /* We don't allow embedded NUL characters */
51
335k
        if (memchr(value, 0, vallen) != NULL)
52
30.6k
            goto err;
53
305k
        tvalue = OPENSSL_strndup(value, vallen);
54
305k
        if (tvalue == NULL)
55
0
            goto err;
56
305k
    }
57
348k
    if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)
58
0
        goto err;
59
348k
    if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL) {
60
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
61
0
        goto err;
62
0
    }
63
348k
    vtmp->section = NULL;
64
348k
    vtmp->name = tname;
65
348k
    vtmp->value = tvalue;
66
348k
    if (!sk_CONF_VALUE_push(*extlist, vtmp))
67
0
        goto err;
68
348k
    return 1;
69
30.6k
err:
70
30.6k
    if (sk_allocated) {
71
8.93k
        sk_CONF_VALUE_free(*extlist);
72
8.93k
        *extlist = NULL;
73
8.93k
    }
74
30.6k
    OPENSSL_free(vtmp);
75
30.6k
    OPENSSL_free(tname);
76
30.6k
    OPENSSL_free(tvalue);
77
30.6k
    return 0;
78
348k
}
79
80
int X509V3_add_value(const char *name, const char *value,
81
    STACK_OF(CONF_VALUE) **extlist)
82
238k
{
83
238k
    return x509v3_add_len_value(name, value,
84
238k
        value != NULL ? strlen((const char *)value) : 0,
85
238k
        extlist);
86
238k
}
87
88
int X509V3_add_value_uchar(const char *name, const unsigned char *value,
89
    STACK_OF(CONF_VALUE) **extlist)
90
0
{
91
0
    return x509v3_add_len_value(name, (const char *)value,
92
0
        value != NULL ? strlen((const char *)value) : 0,
93
0
        extlist);
94
0
}
95
96
int x509v3_add_len_value_uchar(const char *name, const unsigned char *value,
97
    size_t vallen, STACK_OF(CONF_VALUE) **extlist)
98
140k
{
99
140k
    return x509v3_add_len_value(name, (const char *)value, vallen, extlist);
100
140k
}
101
102
/* Free function for STACK_OF(CONF_VALUE) */
103
104
void X509V3_conf_free(CONF_VALUE *conf)
105
348k
{
106
348k
    if (!conf)
107
0
        return;
108
348k
    OPENSSL_free(conf->name);
109
348k
    OPENSSL_free(conf->value);
110
348k
    OPENSSL_free(conf->section);
111
348k
    OPENSSL_free(conf);
112
348k
}
113
114
int X509V3_add_value_bool(const char *name, int asn1_bool,
115
    STACK_OF(CONF_VALUE) **extlist)
116
19.4k
{
117
19.4k
    if (asn1_bool)
118
3.81k
        return X509V3_add_value(name, "TRUE", extlist);
119
15.6k
    return X509V3_add_value(name, "FALSE", extlist);
120
19.4k
}
121
122
int X509V3_add_value_bool_nf(const char *name, int asn1_bool,
123
    STACK_OF(CONF_VALUE) **extlist)
124
0
{
125
0
    if (asn1_bool)
126
0
        return X509V3_add_value(name, "TRUE", extlist);
127
0
    return 1;
128
0
}
129
130
static char *bignum_to_string(const BIGNUM *bn)
131
3.05M
{
132
3.05M
    char *tmp, *ret;
133
3.05M
    size_t len;
134
135
    /*
136
     * Display large numbers in hex and small numbers in decimal. Converting to
137
     * decimal takes quadratic time and is no more useful than hex for large
138
     * numbers.
139
     */
140
3.05M
    if (BN_num_bits(bn) < 128)
141
3.02M
        return BN_bn2dec(bn);
142
143
33.1k
    tmp = BN_bn2hex(bn);
144
33.1k
    if (tmp == NULL)
145
0
        return NULL;
146
147
33.1k
    len = strlen(tmp) + 3;
148
33.1k
    ret = OPENSSL_malloc(len);
149
33.1k
    if (ret == NULL) {
150
0
        OPENSSL_free(tmp);
151
0
        return NULL;
152
0
    }
153
154
    /* Prepend "0x", but place it after the "-" if negative. */
155
33.1k
    if (tmp[0] == '-') {
156
9.10k
        OPENSSL_strlcpy(ret, "-0x", len);
157
9.10k
        OPENSSL_strlcat(ret, tmp + 1, len);
158
24.0k
    } else {
159
24.0k
        OPENSSL_strlcpy(ret, "0x", len);
160
24.0k
        OPENSSL_strlcat(ret, tmp, len);
161
24.0k
    }
162
33.1k
    OPENSSL_free(tmp);
163
33.1k
    return ret;
164
33.1k
}
165
166
char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)
167
16.3k
{
168
16.3k
    BIGNUM *bntmp = NULL;
169
16.3k
    char *strtmp = NULL;
170
171
16.3k
    if (!a)
172
0
        return NULL;
173
16.3k
    if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL)
174
16.3k
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
175
16.3k
    else if ((strtmp = bignum_to_string(bntmp)) == NULL)
176
16.3k
        ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
177
16.3k
    BN_free(bntmp);
178
16.3k
    return strtmp;
179
16.3k
}
180
181
char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a)
182
2.60M
{
183
2.60M
    BIGNUM *bntmp = NULL;
184
2.60M
    char *strtmp = NULL;
185
186
2.60M
    if (!a)
187
0
        return NULL;
188
2.60M
    if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL)
189
2.60M
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
190
2.60M
    else if ((strtmp = bignum_to_string(bntmp)) == NULL)
191
2.60M
        ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
192
2.60M
    BN_free(bntmp);
193
2.60M
    return strtmp;
194
2.60M
}
195
196
ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value)
197
0
{
198
0
    BIGNUM *bn = NULL;
199
0
    ASN1_INTEGER *aint;
200
0
    int isneg, ishex;
201
0
    int ret;
202
203
0
    if (value == NULL) {
204
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE);
205
0
        return NULL;
206
0
    }
207
0
    bn = BN_new();
208
0
    if (bn == NULL) {
209
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_BN_LIB);
210
0
        return NULL;
211
0
    }
212
0
    if (value[0] == '-') {
213
0
        value++;
214
0
        isneg = 1;
215
0
    } else {
216
0
        isneg = 0;
217
0
    }
218
219
0
    if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
220
0
        value += 2;
221
0
        ishex = 1;
222
0
    } else {
223
0
        ishex = 0;
224
0
    }
225
226
0
    if (ishex)
227
0
        ret = BN_hex2bn(&bn, value);
228
0
    else
229
0
        ret = BN_dec2bn(&bn, value);
230
231
0
    if (!ret || value[ret]) {
232
0
        BN_free(bn);
233
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_BN_DEC2BN_ERROR);
234
0
        return NULL;
235
0
    }
236
237
0
    if (isneg && BN_is_zero(bn))
238
0
        isneg = 0;
239
240
0
    aint = BN_to_ASN1_INTEGER(bn, NULL);
241
0
    BN_free(bn);
242
0
    if (!aint) {
243
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
244
0
        return NULL;
245
0
    }
246
0
    if (isneg)
247
0
        aint->type |= V_ASN1_NEG;
248
0
    return aint;
249
0
}
250
251
int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint,
252
    STACK_OF(CONF_VALUE) **extlist)
253
72.8k
{
254
72.8k
    char *strtmp;
255
72.8k
    int ret;
256
257
72.8k
    if (!aint)
258
29.7k
        return 1;
259
43.1k
    if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL)
260
0
        return 0;
261
43.1k
    ret = X509V3_add_value(name, strtmp, extlist);
262
43.1k
    OPENSSL_free(strtmp);
263
43.1k
    return ret;
264
43.1k
}
265
266
int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool)
267
0
{
268
0
    const char *btmp;
269
270
0
    if ((btmp = value->value) == NULL)
271
0
        goto err;
272
0
    if (strcmp(btmp, "TRUE") == 0
273
0
        || strcmp(btmp, "true") == 0
274
0
        || strcmp(btmp, "Y") == 0
275
0
        || strcmp(btmp, "y") == 0
276
0
        || strcmp(btmp, "YES") == 0
277
0
        || strcmp(btmp, "yes") == 0) {
278
0
        *asn1_bool = 0xff;
279
0
        return 1;
280
0
    }
281
0
    if (strcmp(btmp, "FALSE") == 0
282
0
        || strcmp(btmp, "false") == 0
283
0
        || strcmp(btmp, "N") == 0
284
0
        || strcmp(btmp, "n") == 0
285
0
        || strcmp(btmp, "NO") == 0
286
0
        || strcmp(btmp, "no") == 0) {
287
0
        *asn1_bool = 0;
288
0
        return 1;
289
0
    }
290
0
err:
291
0
    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_BOOLEAN_STRING);
292
0
    X509V3_conf_add_error_name_value(value);
293
0
    return 0;
294
0
}
295
296
int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint)
297
0
{
298
0
    ASN1_INTEGER *itmp;
299
300
0
    if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) {
301
0
        X509V3_conf_add_error_name_value(value);
302
0
        return 0;
303
0
    }
304
0
    *aint = itmp;
305
0
    return 1;
306
0
}
307
308
0
#define HDR_NAME 1
309
0
#define HDR_VALUE 2
310
311
/*
312
 * #define DEBUG
313
 */
314
315
STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
316
0
{
317
0
    char *p, *q, c;
318
0
    char *ntmp, *vtmp;
319
0
    STACK_OF(CONF_VALUE) *values = NULL;
320
0
    char *linebuf;
321
0
    int state;
322
323
    /* We are going to modify the line so copy it first */
324
0
    linebuf = OPENSSL_strdup(line);
325
0
    if (linebuf == NULL)
326
0
        goto err;
327
0
    state = HDR_NAME;
328
0
    ntmp = NULL;
329
    /* Go through all characters */
330
0
    for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
331
0
        p++) {
332
333
0
        switch (state) {
334
0
        case HDR_NAME:
335
0
            if (c == ':') {
336
0
                state = HDR_VALUE;
337
0
                *p = 0;
338
0
                ntmp = strip_spaces(q);
339
0
                if (!ntmp) {
340
0
                    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME);
341
0
                    goto err;
342
0
                }
343
0
                q = p + 1;
344
0
            } else if (c == ',') {
345
0
                *p = 0;
346
0
                ntmp = strip_spaces(q);
347
0
                q = p + 1;
348
0
                if (!ntmp) {
349
0
                    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME);
350
0
                    goto err;
351
0
                }
352
0
                if (!X509V3_add_value(ntmp, NULL, &values)) {
353
0
                    goto err;
354
0
                }
355
0
            }
356
0
            break;
357
358
0
        case HDR_VALUE:
359
0
            if (c == ',') {
360
0
                state = HDR_NAME;
361
0
                *p = 0;
362
0
                vtmp = strip_spaces(q);
363
0
                if (!vtmp) {
364
0
                    ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE);
365
0
                    goto err;
366
0
                }
367
0
                if (!X509V3_add_value(ntmp, vtmp, &values)) {
368
0
                    goto err;
369
0
                }
370
0
                ntmp = NULL;
371
0
                q = p + 1;
372
0
            }
373
0
        }
374
0
    }
375
376
0
    if (state == HDR_VALUE) {
377
0
        vtmp = strip_spaces(q);
378
0
        if (!vtmp) {
379
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE);
380
0
            goto err;
381
0
        }
382
0
        if (!X509V3_add_value(ntmp, vtmp, &values)) {
383
0
            goto err;
384
0
        }
385
0
    } else {
386
0
        ntmp = strip_spaces(q);
387
0
        if (!ntmp) {
388
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME);
389
0
            goto err;
390
0
        }
391
0
        if (!X509V3_add_value(ntmp, NULL, &values)) {
392
0
            goto err;
393
0
        }
394
0
    }
395
0
    OPENSSL_free(linebuf);
396
0
    return values;
397
398
0
err:
399
0
    OPENSSL_free(linebuf);
400
0
    sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
401
0
    return NULL;
402
0
}
403
404
/* Delete leading and trailing spaces from a string */
405
static char *strip_spaces(char *name)
406
0
{
407
0
    char *p, *q;
408
409
    /* Skip over leading spaces */
410
0
    p = name;
411
0
    while (*p && ossl_isspace(*p))
412
0
        p++;
413
0
    if (*p == '\0')
414
0
        return NULL;
415
0
    q = p + strlen(p) - 1;
416
0
    while ((q != p) && ossl_isspace(*q))
417
0
        q--;
418
0
    if (p != q)
419
0
        q[1] = 0;
420
0
    if (*p == '\0')
421
0
        return NULL;
422
0
    return p;
423
0
}
424
425
/*
426
 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
427
 */
428
429
int ossl_v3_name_cmp(const char *name, const char *cmp)
430
0
{
431
0
    int len, ret;
432
0
    char c;
433
434
0
    len = strlen(cmp);
435
0
    if ((ret = strncmp(name, cmp, len)))
436
0
        return ret;
437
0
    c = name[len];
438
0
    if (!c || (c == '.'))
439
0
        return 0;
440
0
    return 1;
441
0
}
442
443
static int sk_strcmp(const char *const *a, const char *const *b)
444
0
{
445
0
    return strcmp(*a, *b);
446
0
}
447
448
STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
449
0
{
450
0
    GENERAL_NAMES *gens;
451
0
    STACK_OF(OPENSSL_STRING) *ret;
452
453
0
    gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
454
0
    ret = get_email(X509_get_subject_name(x), gens);
455
0
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
456
0
    return ret;
457
0
}
458
459
STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
460
0
{
461
0
    AUTHORITY_INFO_ACCESS *info;
462
0
    STACK_OF(OPENSSL_STRING) *ret = NULL;
463
0
    int i;
464
465
0
    info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
466
0
    if (!info)
467
0
        return NULL;
468
0
    for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
469
0
        ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
470
0
        if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
471
0
            if (ad->location->type == GEN_URI) {
472
0
                if (!append_ia5(&ret, ad->location->d.uniformResourceIdentifier))
473
0
                    break;
474
0
            }
475
0
        }
476
0
    }
477
0
    AUTHORITY_INFO_ACCESS_free(info);
478
0
    return ret;
479
0
}
480
481
STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
482
0
{
483
0
    GENERAL_NAMES *gens;
484
0
    STACK_OF(X509_EXTENSION) *exts;
485
0
    STACK_OF(OPENSSL_STRING) *ret;
486
487
0
    exts = X509_REQ_get_extensions(x);
488
0
    gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
489
0
    ret = get_email(X509_REQ_get_subject_name(x), gens);
490
0
    sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
491
0
    sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
492
0
    return ret;
493
0
}
494
495
static STACK_OF(OPENSSL_STRING) *get_email(const X509_NAME *name,
496
    GENERAL_NAMES *gens)
497
0
{
498
0
    STACK_OF(OPENSSL_STRING) *ret = NULL;
499
0
    X509_NAME_ENTRY *ne;
500
0
    const ASN1_IA5STRING *email;
501
0
    GENERAL_NAME *gen;
502
0
    int i = -1;
503
504
    /* Now add any email address(es) to STACK */
505
    /* First supplied X509_NAME */
506
0
    while ((i = X509_NAME_get_index_by_NID(name,
507
0
                NID_pkcs9_emailAddress, i))
508
0
        >= 0) {
509
0
        ne = X509_NAME_get_entry(name, i);
510
0
        email = X509_NAME_ENTRY_get_data(ne);
511
0
        if (!append_ia5(&ret, email))
512
0
            return NULL;
513
0
    }
514
0
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
515
0
        gen = sk_GENERAL_NAME_value(gens, i);
516
0
        if (gen->type != GEN_EMAIL)
517
0
            continue;
518
0
        if (!append_ia5(&ret, gen->d.ia5))
519
0
            return NULL;
520
0
    }
521
0
    return ret;
522
0
}
523
524
static void str_free(OPENSSL_STRING str)
525
0
{
526
0
    OPENSSL_free(str);
527
0
}
528
529
static int append_ia5(STACK_OF(OPENSSL_STRING) **sk,
530
    const ASN1_IA5STRING *email)
531
0
{
532
0
    char *emtmp;
533
534
    /* First some sanity checks */
535
0
    if (email->type != V_ASN1_IA5STRING)
536
0
        return 1;
537
0
    if (email->data == NULL || email->length == 0)
538
0
        return 1;
539
0
    if (memchr(email->data, 0, email->length) != NULL)
540
0
        return 1;
541
0
    if (*sk == NULL)
542
0
        *sk = sk_OPENSSL_STRING_new(sk_strcmp);
543
0
    if (*sk == NULL)
544
0
        return 0;
545
546
0
    emtmp = OPENSSL_strndup((char *)email->data, email->length);
547
0
    if (emtmp == NULL) {
548
0
        X509_email_free(*sk);
549
0
        *sk = NULL;
550
0
        return 0;
551
0
    }
552
553
    /* Don't add duplicates */
554
0
    if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) {
555
0
        OPENSSL_free(emtmp);
556
0
        return 1;
557
0
    }
558
0
    if (!sk_OPENSSL_STRING_push(*sk, emtmp)) {
559
0
        OPENSSL_free(emtmp); /* free on push failure */
560
0
        X509_email_free(*sk);
561
0
        *sk = NULL;
562
0
        return 0;
563
0
    }
564
0
    return 1;
565
0
}
566
567
void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
568
0
{
569
0
    sk_OPENSSL_STRING_pop_free(sk, str_free);
570
0
}
571
572
typedef int (*equal_fn)(const unsigned char *pattern, size_t pattern_len,
573
    const unsigned char *subject, size_t subject_len,
574
    unsigned int flags);
575
576
/* Skip pattern prefix to match "wildcard" subject */
577
static void skip_prefix(const unsigned char **p, size_t *plen,
578
    size_t subject_len,
579
    unsigned int flags)
580
0
{
581
0
    const unsigned char *pattern = *p;
582
0
    size_t pattern_len = *plen;
583
584
    /*
585
     * If subject starts with a leading '.' followed by more octets, and
586
     * pattern is longer, compare just an equal-length suffix with the
587
     * full subject (starting at the '.'), provided the prefix contains
588
     * no NULs.
589
     */
590
0
    if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
591
0
        return;
592
593
0
    while (pattern_len > subject_len && *pattern) {
594
0
        if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) && *pattern == '.')
595
0
            break;
596
0
        ++pattern;
597
0
        --pattern_len;
598
0
    }
599
600
    /* Skip if entire prefix acceptable */
601
0
    if (pattern_len == subject_len) {
602
0
        *p = pattern;
603
0
        *plen = pattern_len;
604
0
    }
605
0
}
606
607
/* Compare while ASCII ignoring case. */
608
static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
609
    const unsigned char *subject, size_t subject_len,
610
    unsigned int flags)
611
0
{
612
0
    skip_prefix(&pattern, &pattern_len, subject_len, flags);
613
0
    if (pattern_len != subject_len)
614
0
        return 0;
615
0
    while (pattern_len != 0) {
616
0
        unsigned char l = *pattern;
617
0
        unsigned char r = *subject;
618
619
        /* The pattern must not contain NUL characters. */
620
0
        if (l == 0)
621
0
            return 0;
622
0
        if (l != r) {
623
0
            if ('A' <= l && l <= 'Z')
624
0
                l = (l - 'A') + 'a';
625
0
            if ('A' <= r && r <= 'Z')
626
0
                r = (r - 'A') + 'a';
627
0
            if (l != r)
628
0
                return 0;
629
0
        }
630
0
        ++pattern;
631
0
        ++subject;
632
0
        --pattern_len;
633
0
    }
634
0
    return 1;
635
0
}
636
637
/* Compare using memcmp. */
638
static int equal_case(const unsigned char *pattern, size_t pattern_len,
639
    const unsigned char *subject, size_t subject_len,
640
    unsigned int flags)
641
0
{
642
0
    skip_prefix(&pattern, &pattern_len, subject_len, flags);
643
0
    if (pattern_len != subject_len)
644
0
        return 0;
645
0
    return !memcmp(pattern, subject, pattern_len);
646
0
}
647
648
/*
649
 * RFC 5280, section 7.5, requires that only the domain is compared in a
650
 * case-insensitive manner.
651
 */
652
static int equal_email(const unsigned char *a, size_t a_len,
653
    const unsigned char *b, size_t b_len,
654
    unsigned int unused_flags)
655
0
{
656
0
    size_t i = a_len;
657
658
0
    if (a_len != b_len)
659
0
        return 0;
660
    /*
661
     * We search backwards for the '@' character, so that we do not have to
662
     * deal with quoted local-parts.  The domain part is compared in a
663
     * case-insensitive manner.
664
     */
665
0
    while (i > 0) {
666
0
        --i;
667
0
        if (a[i] == '@' || b[i] == '@') {
668
0
            if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
669
0
                return 0;
670
0
            break;
671
0
        }
672
0
    }
673
0
    if (i == 0)
674
0
        i = a_len;
675
0
    return equal_case(a, i, b, i, 0);
676
0
}
677
678
/*
679
 * Compare the prefix and suffix with the subject, and check that the
680
 * characters in-between are valid.
681
 */
682
static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
683
    const unsigned char *suffix, size_t suffix_len,
684
    const unsigned char *subject, size_t subject_len,
685
    unsigned int flags)
686
0
{
687
0
    const unsigned char *wildcard_start;
688
0
    const unsigned char *wildcard_end;
689
0
    const unsigned char *p;
690
0
    int allow_multi = 0;
691
0
    int allow_idna = 0;
692
693
0
    if (subject_len < prefix_len + suffix_len)
694
0
        return 0;
695
0
    if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
696
0
        return 0;
697
0
    wildcard_start = subject + prefix_len;
698
0
    wildcard_end = subject + (subject_len - suffix_len);
699
0
    if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
700
0
        return 0;
701
    /*
702
     * If the wildcard makes up the entire first label, it must match at
703
     * least one character.
704
     */
705
0
    if (prefix_len == 0 && *suffix == '.') {
706
0
        if (wildcard_start == wildcard_end)
707
0
            return 0;
708
0
        allow_idna = 1;
709
0
        if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
710
0
            allow_multi = 1;
711
0
    }
712
    /* IDNA labels cannot match partial wildcards */
713
0
    if (!allow_idna && subject_len >= 4 && HAS_CASE_PREFIX((const char *)subject, "xn--"))
714
0
        return 0;
715
    /* The wildcard may match a literal '*' */
716
0
    if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
717
0
        return 1;
718
    /*
719
     * Check that the part matched by the wildcard contains only
720
     * permitted characters and only matches a single label unless
721
     * allow_multi is set.
722
     */
723
0
    for (p = wildcard_start; p != wildcard_end; ++p)
724
0
        if (!(('0' <= *p && *p <= '9') || ('A' <= *p && *p <= 'Z') || ('a' <= *p && *p <= 'z') || *p == '-' || (allow_multi && *p == '.')))
725
0
            return 0;
726
0
    return 1;
727
0
}
728
729
0
#define LABEL_START (1 << 0)
730
#define LABEL_END (1 << 1)
731
0
#define LABEL_HYPHEN (1 << 2)
732
0
#define LABEL_IDNA (1 << 3)
733
734
static const unsigned char *valid_star(const unsigned char *p, size_t len,
735
    unsigned int flags)
736
0
{
737
0
    const unsigned char *star = 0;
738
0
    size_t i;
739
0
    int state = LABEL_START;
740
0
    int dots = 0;
741
742
0
    for (i = 0; i < len; ++i) {
743
        /*
744
         * Locate first and only legal wildcard, either at the start
745
         * or end of a non-IDNA first and not final label.
746
         */
747
0
        if (p[i] == '*') {
748
0
            int atstart = (state & LABEL_START);
749
0
            int atend = (i == len - 1 || p[i + 1] == '.');
750
            /*-
751
             * At most one wildcard per pattern.
752
             * No wildcards in IDNA labels.
753
             * No wildcards after the first label.
754
             */
755
0
            if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
756
0
                return NULL;
757
            /* Only full-label '*.example.com' wildcards? */
758
0
            if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
759
0
                && (!atstart || !atend))
760
0
                return NULL;
761
            /* No 'foo*bar' wildcards */
762
0
            if (!atstart && !atend)
763
0
                return NULL;
764
0
            star = &p[i];
765
0
            state &= ~LABEL_START;
766
0
        } else if (('a' <= p[i] && p[i] <= 'z')
767
0
            || ('A' <= p[i] && p[i] <= 'Z')
768
0
            || ('0' <= p[i] && p[i] <= '9')) {
769
0
            if ((state & LABEL_START) != 0
770
0
                && len - i >= 4 && HAS_CASE_PREFIX((const char *)&p[i], "xn--"))
771
0
                state |= LABEL_IDNA;
772
0
            state &= ~(LABEL_HYPHEN | LABEL_START);
773
0
        } else if (p[i] == '.') {
774
0
            if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
775
0
                return NULL;
776
0
            state = LABEL_START;
777
0
            ++dots;
778
0
        } else if (p[i] == '-') {
779
            /* no domain/subdomain starts with '-' */
780
0
            if ((state & LABEL_START) != 0)
781
0
                return NULL;
782
0
            state |= LABEL_HYPHEN;
783
0
        } else {
784
0
            return NULL;
785
0
        }
786
0
    }
787
788
    /*
789
     * The final label must not end in a hyphen or ".", and
790
     * there must be at least two dots after the star.
791
     */
792
0
    if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
793
0
        return NULL;
794
0
    return star;
795
0
}
796
797
/* Compare using wildcards. */
798
static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
799
    const unsigned char *subject, size_t subject_len,
800
    unsigned int flags)
801
0
{
802
0
    const unsigned char *star = NULL;
803
804
    /*
805
     * Subject names starting with '.' can only match a wildcard pattern
806
     * via a subject sub-domain pattern suffix match.
807
     */
808
0
    if (!(subject_len > 1 && subject[0] == '.'))
809
0
        star = valid_star(pattern, pattern_len, flags);
810
0
    if (star == NULL)
811
0
        return equal_nocase(pattern, pattern_len,
812
0
            subject, subject_len, flags);
813
0
    return wildcard_match(pattern, star - pattern,
814
0
        star + 1, (pattern + pattern_len) - star - 1,
815
0
        subject, subject_len, flags);
816
0
}
817
818
/*
819
 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
820
 * cmp_type > 0 only compare if string matches the type, otherwise convert it
821
 * to UTF8.
822
 */
823
824
static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal,
825
    unsigned int flags, const char *b, size_t blen,
826
    char **peername)
827
0
{
828
0
    int rv = 0;
829
830
0
    if (!a->data || !a->length)
831
0
        return 0;
832
0
    if (cmp_type > 0) {
833
0
        if (cmp_type != a->type)
834
0
            return 0;
835
0
        if (cmp_type == V_ASN1_IA5STRING)
836
0
            rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
837
0
        else if (a->length == (int)blen && !memcmp(a->data, b, blen))
838
0
            rv = 1;
839
0
        if (rv > 0 && peername != NULL) {
840
0
            *peername = OPENSSL_strndup((char *)a->data, a->length);
841
0
            if (*peername == NULL)
842
0
                return -1;
843
0
        }
844
0
    } else {
845
0
        int astrlen;
846
0
        unsigned char *astr;
847
0
        astrlen = ASN1_STRING_to_UTF8(&astr, a);
848
0
        if (astrlen < 0) {
849
            /*
850
             * -1 could be an internal malloc failure or a decoding error from
851
             * malformed input; we can't distinguish.
852
             */
853
0
            return -1;
854
0
        }
855
0
        rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
856
0
        if (rv > 0 && peername != NULL) {
857
0
            *peername = OPENSSL_strndup((char *)astr, astrlen);
858
0
            if (*peername == NULL) {
859
0
                OPENSSL_free(astr);
860
0
                return -1;
861
0
            }
862
0
        }
863
0
        OPENSSL_free(astr);
864
0
    }
865
0
    return rv;
866
0
}
867
868
static int do_x509_check(X509 *x, const char *chk, size_t chklen,
869
    unsigned int flags, int check_type, char **peername)
870
0
{
871
0
    GENERAL_NAMES *gens = NULL;
872
0
    const X509_NAME *name = NULL;
873
0
    int i;
874
0
    int cnid = NID_undef;
875
0
    int alt_type;
876
0
    int san_present = 0;
877
0
    int rv = 0;
878
0
    equal_fn equal;
879
880
    /* See below, this flag is internal-only */
881
0
    flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
882
0
    if (check_type == GEN_EMAIL) {
883
0
        cnid = NID_pkcs9_emailAddress;
884
0
        alt_type = V_ASN1_IA5STRING;
885
0
        equal = equal_email;
886
0
    } else if (check_type == GEN_DNS) {
887
0
        cnid = NID_commonName;
888
        /* Implicit client-side DNS sub-domain pattern */
889
0
        if (chklen > 1 && chk[0] == '.')
890
0
            flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
891
0
        alt_type = V_ASN1_IA5STRING;
892
0
        if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
893
0
            equal = equal_nocase;
894
0
        else
895
0
            equal = equal_wildcard;
896
0
    } else {
897
0
        alt_type = V_ASN1_OCTET_STRING;
898
0
        equal = equal_case;
899
0
    }
900
901
0
    if (chklen == 0)
902
0
        chklen = strlen(chk);
903
904
0
    gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
905
0
    if (gens) {
906
0
        for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
907
0
            GENERAL_NAME *gen;
908
0
            ASN1_STRING *cstr;
909
910
0
            gen = sk_GENERAL_NAME_value(gens, i);
911
0
            switch (gen->type) {
912
0
            default:
913
0
                continue;
914
0
            case GEN_OTHERNAME:
915
0
                switch (OBJ_obj2nid(gen->d.otherName->type_id)) {
916
0
                default:
917
0
                    continue;
918
0
                case NID_id_on_SmtpUTF8Mailbox:
919
                    /*-
920
                     * https://datatracker.ietf.org/doc/html/rfc8398#section-3
921
                     *
922
                     *   Due to name constraint compatibility reasons described
923
                     *   in Section 6, SmtpUTF8Mailbox subjectAltName MUST NOT
924
                     *   be used unless the local-part of the email address
925
                     *   contains non-ASCII characters. When the local-part is
926
                     *   ASCII, rfc822Name subjectAltName MUST be used instead
927
                     *   of SmtpUTF8Mailbox. This is compatible with legacy
928
                     *   software that supports only rfc822Name (and not
929
                     *   SmtpUTF8Mailbox). [...]
930
                     *
931
                     *   SmtpUTF8Mailbox is encoded as UTF8String.
932
                     *
933
                     * If it is not a UTF8String then that is unexpected, and
934
                     * we ignore the invalid SAN (neither set san_present nor
935
                     * consider it a candidate for equality).  This does mean
936
                     * that the subject CN may be considered, as would be the
937
                     * case when the malformed SmtpUtf8Mailbox SAN is instead
938
                     * simply absent.
939
                     *
940
                     * When CN-ID matching is not desirable, applications can
941
                     * choose to turn it off, doing so is at this time a best
942
                     * practice.
943
                     */
944
0
                    if (check_type != GEN_EMAIL
945
0
                        || gen->d.otherName->value->type != V_ASN1_UTF8STRING)
946
0
                        continue;
947
0
                    alt_type = 0;
948
0
                    cstr = gen->d.otherName->value->value.utf8string;
949
0
                    break;
950
0
                }
951
0
                break;
952
0
            case GEN_EMAIL:
953
0
                if (check_type != GEN_EMAIL)
954
0
                    continue;
955
0
                cstr = gen->d.rfc822Name;
956
0
                break;
957
0
            case GEN_DNS:
958
0
                if (check_type != GEN_DNS)
959
0
                    continue;
960
0
                cstr = gen->d.dNSName;
961
0
                break;
962
0
            case GEN_IPADD:
963
0
                if (check_type != GEN_IPADD)
964
0
                    continue;
965
0
                cstr = gen->d.iPAddress;
966
0
                break;
967
0
            }
968
0
            san_present = 1;
969
            /* Positive on success, negative on error! */
970
0
            if ((rv = do_check_string(cstr, alt_type, equal, flags,
971
0
                     chk, chklen, peername))
972
0
                != 0)
973
0
                break;
974
0
        }
975
0
        GENERAL_NAMES_free(gens);
976
0
        if (rv != 0)
977
0
            return rv;
978
0
        if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT))
979
0
            return 0;
980
0
    }
981
982
    /* We're done if CN-ID is not pertinent */
983
0
    if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT))
984
0
        return 0;
985
986
0
    i = -1;
987
0
    name = X509_get_subject_name(x);
988
0
    while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
989
0
        const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i);
990
0
        const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne);
991
992
        /* Positive on success, negative on error! */
993
0
        if ((rv = do_check_string(str, -1, equal, flags,
994
0
                 chk, chklen, peername))
995
0
            != 0)
996
0
            return rv;
997
0
    }
998
0
    return 0;
999
0
}
1000
1001
int X509_check_host(X509 *x, const char *chk, size_t chklen,
1002
    unsigned int flags, char **peername)
1003
0
{
1004
0
    if (chk == NULL)
1005
0
        return -2;
1006
    /*
1007
     * Embedded NULs are disallowed, except as the last character of a
1008
     * string of length 2 or more (tolerate caller including terminating
1009
     * NUL in string length).
1010
     */
1011
0
    if (chklen == 0)
1012
0
        chklen = strlen(chk);
1013
0
    else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1014
0
        return -2;
1015
0
    if (chklen > 1 && chk[chklen - 1] == '\0')
1016
0
        --chklen;
1017
0
    return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
1018
0
}
1019
1020
int X509_check_email(X509 *x, const char *chk, size_t chklen,
1021
    unsigned int flags)
1022
0
{
1023
0
    if (chk == NULL)
1024
0
        return -2;
1025
    /*
1026
     * Embedded NULs are disallowed, except as the last character of a
1027
     * string of length 2 or more (tolerate caller including terminating
1028
     * NUL in string length).
1029
     */
1030
0
    if (chklen == 0)
1031
0
        chklen = strlen((char *)chk);
1032
0
    else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1033
0
        return -2;
1034
0
    if (chklen > 1 && chk[chklen - 1] == '\0')
1035
0
        --chklen;
1036
0
    return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
1037
0
}
1038
1039
int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
1040
    unsigned int flags)
1041
0
{
1042
0
    if (chk == NULL)
1043
0
        return -2;
1044
0
    return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
1045
0
}
1046
1047
int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
1048
0
{
1049
0
    unsigned char ipout[16];
1050
0
    size_t iplen;
1051
1052
0
    if (ipasc == NULL)
1053
0
        return -2;
1054
0
    iplen = (size_t)ossl_a2i_ipadd(ipout, ipasc);
1055
0
    if (iplen == 0)
1056
0
        return -2;
1057
0
    return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
1058
0
}
1059
1060
char *ossl_ipaddr_to_asc(unsigned char *p, int len)
1061
83.0k
{
1062
    /*
1063
     * 40 is enough space for the longest IPv6 address + nul terminator byte
1064
     * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX\0
1065
     */
1066
83.0k
    char buf[40], *out;
1067
83.0k
    int i = 0, remain = 0, bytes = 0;
1068
1069
83.0k
    switch (len) {
1070
26.1k
    case 4: /* IPv4 */
1071
26.1k
        BIO_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
1072
26.1k
        break;
1073
10.8k
    case 16: /* IPv6 */
1074
10.8k
        for (out = buf, i = 8, remain = sizeof(buf);
1075
97.9k
            i-- > 0 && bytes >= 0;
1076
87.0k
            remain -= bytes, out += bytes) {
1077
87.0k
            const char *template = (i > 0 ? "%X:" : "%X");
1078
1079
87.0k
            bytes = BIO_snprintf(out, remain, template, p[0] << 8 | p[1]);
1080
87.0k
            p += 2;
1081
87.0k
        }
1082
10.8k
        break;
1083
46.0k
    default:
1084
46.0k
        BIO_snprintf(buf, sizeof(buf), "<invalid length=%d>", len);
1085
46.0k
        break;
1086
83.0k
    }
1087
83.0k
    return OPENSSL_strdup(buf);
1088
83.0k
}
1089
1090
/*
1091
 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1092
 * with RFC3280.
1093
 */
1094
1095
ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
1096
0
{
1097
0
    unsigned char ipout[16];
1098
0
    ASN1_OCTET_STRING *ret;
1099
0
    int iplen;
1100
1101
    /* If string contains a ':' assume IPv6 */
1102
1103
0
    iplen = ossl_a2i_ipadd(ipout, ipasc);
1104
1105
0
    if (!iplen)
1106
0
        return NULL;
1107
1108
0
    ret = ASN1_OCTET_STRING_new();
1109
0
    if (ret == NULL)
1110
0
        return NULL;
1111
0
    if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
1112
0
        ASN1_OCTET_STRING_free(ret);
1113
0
        return NULL;
1114
0
    }
1115
0
    return ret;
1116
0
}
1117
1118
ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
1119
0
{
1120
0
    ASN1_OCTET_STRING *ret = NULL;
1121
0
    unsigned char ipout[32];
1122
0
    char *iptmp = NULL, *p;
1123
0
    int iplen1, iplen2;
1124
1125
0
    p = strchr(ipasc, '/');
1126
0
    if (p == NULL)
1127
0
        return NULL;
1128
0
    iptmp = OPENSSL_strdup(ipasc);
1129
0
    if (iptmp == NULL)
1130
0
        return NULL;
1131
0
    p = iptmp + (p - ipasc);
1132
0
    *p++ = 0;
1133
1134
0
    iplen1 = ossl_a2i_ipadd(ipout, iptmp);
1135
1136
0
    if (!iplen1)
1137
0
        goto err;
1138
1139
0
    iplen2 = ossl_a2i_ipadd(ipout + iplen1, p);
1140
1141
0
    OPENSSL_free(iptmp);
1142
0
    iptmp = NULL;
1143
1144
0
    if (!iplen2 || (iplen1 != iplen2))
1145
0
        goto err;
1146
1147
0
    ret = ASN1_OCTET_STRING_new();
1148
0
    if (ret == NULL)
1149
0
        goto err;
1150
0
    if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1151
0
        goto err;
1152
1153
0
    return ret;
1154
1155
0
err:
1156
0
    OPENSSL_free(iptmp);
1157
0
    ASN1_OCTET_STRING_free(ret);
1158
0
    return NULL;
1159
0
}
1160
1161
int ossl_a2i_ipadd(unsigned char *ipout, const char *ipasc)
1162
0
{
1163
    /* If string contains a ':' assume IPv6 */
1164
1165
0
    if (strchr(ipasc, ':')) {
1166
0
        if (!ipv6_from_asc(ipout, ipasc))
1167
0
            return 0;
1168
0
        return 16;
1169
0
    } else {
1170
0
        if (!ipv4_from_asc(ipout, ipasc))
1171
0
            return 0;
1172
0
        return 4;
1173
0
    }
1174
0
}
1175
1176
/*
1177
 * get_ipv4_component consumes one IPv4 component, terminated by either '.' or
1178
 * the end of the string, from *str. On success, it returns one, sets *out
1179
 * to the component, and advances *str to the first unconsumed character. On
1180
 * invalid input, it returns zero.
1181
 */
1182
static int get_ipv4_component(uint8_t *out_byte, const char **str)
1183
0
{
1184
    /* Store a slightly larger intermediary so the overflow check is easier. */
1185
0
    uint32_t out = 0;
1186
1187
0
    for (;;) {
1188
0
        if (!ossl_isdigit(**str)) {
1189
0
            return 0;
1190
0
        }
1191
0
        out = (out * 10) + (**str - '0');
1192
0
        if (out > 255) {
1193
            /* Components must be 8-bit. */
1194
0
            return 0;
1195
0
        }
1196
0
        (*str)++;
1197
0
        if ((**str) == '.' || (**str) == '\0') {
1198
0
            *out_byte = (uint8_t)out;
1199
0
            return 1;
1200
0
        }
1201
0
        if (out == 0) {
1202
            /* Reject extra leading zeros. Parsers sometimes treat them as
1203
             * octal, so accepting them would misinterpret input.
1204
             */
1205
0
            return 0;
1206
0
        }
1207
0
    }
1208
0
}
1209
1210
/*
1211
 * get_ipv4_dot consumes a '.' from *str and advances it. It returns one on
1212
 * success and zero if *str does not point to a '.'.
1213
 */
1214
static int get_ipv4_dot(const char **str)
1215
0
{
1216
0
    if (**str != '.') {
1217
0
        return 0;
1218
0
    }
1219
0
    (*str)++;
1220
0
    return 1;
1221
0
}
1222
1223
static int ipv4_from_asc(unsigned char *v4, const char *in)
1224
0
{
1225
0
    if (!get_ipv4_component(&v4[0], &in) || !get_ipv4_dot(&in)
1226
0
        || !get_ipv4_component(&v4[1], &in) || !get_ipv4_dot(&in)
1227
0
        || !get_ipv4_component(&v4[2], &in) || !get_ipv4_dot(&in)
1228
0
        || !get_ipv4_component(&v4[3], &in) || *in != '\0') {
1229
0
        return 0;
1230
0
    }
1231
0
    return 1;
1232
0
}
1233
1234
typedef struct {
1235
    /* Temporary store for IPV6 output */
1236
    unsigned char tmp[16];
1237
    /* Total number of bytes in tmp */
1238
    int total;
1239
    /* The position of a zero (corresponding to '::') */
1240
    int zero_pos;
1241
    /* Number of zeroes */
1242
    int zero_cnt;
1243
} IPV6_STAT;
1244
1245
static int ipv6_from_asc(unsigned char *v6, const char *in)
1246
0
{
1247
0
    IPV6_STAT v6stat;
1248
1249
0
    v6stat.total = 0;
1250
0
    v6stat.zero_pos = -1;
1251
0
    v6stat.zero_cnt = 0;
1252
    /*
1253
     * Treat the IPv6 representation as a list of values separated by ':'.
1254
     * The presence of a '::' will parse as one, two or three zero length
1255
     * elements.
1256
     */
1257
0
    if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1258
0
        return 0;
1259
1260
    /* Now for some sanity checks */
1261
1262
0
    if (v6stat.zero_pos == -1) {
1263
        /* If no '::' must have exactly 16 bytes */
1264
0
        if (v6stat.total != 16)
1265
0
            return 0;
1266
0
    } else {
1267
        /* If '::' must have less than 16 bytes */
1268
0
        if (v6stat.total == 16)
1269
0
            return 0;
1270
        /* More than three zeroes is an error */
1271
0
        if (v6stat.zero_cnt > 3) {
1272
0
            return 0;
1273
            /* Can only have three zeroes if nothing else present */
1274
0
        } else if (v6stat.zero_cnt == 3) {
1275
0
            if (v6stat.total > 0)
1276
0
                return 0;
1277
0
        } else if (v6stat.zero_cnt == 2) {
1278
            /* Can only have two zeroes if at start or end */
1279
0
            if ((v6stat.zero_pos != 0)
1280
0
                && (v6stat.zero_pos != v6stat.total))
1281
0
                return 0;
1282
0
        } else {
1283
            /* Can only have one zero if *not* start or end */
1284
0
            if ((v6stat.zero_pos == 0)
1285
0
                || (v6stat.zero_pos == v6stat.total))
1286
0
                return 0;
1287
0
        }
1288
0
    }
1289
1290
    /* Format result */
1291
1292
0
    if (v6stat.zero_pos >= 0) {
1293
        /* Copy initial part */
1294
0
        memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1295
        /* Zero middle */
1296
0
        memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1297
        /* Copy final part */
1298
0
        if (v6stat.total != v6stat.zero_pos)
1299
0
            memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1300
0
                v6stat.tmp + v6stat.zero_pos,
1301
0
                v6stat.total - v6stat.zero_pos);
1302
0
    } else {
1303
0
        memcpy(v6, v6stat.tmp, 16);
1304
0
    }
1305
1306
0
    return 1;
1307
0
}
1308
1309
static int ipv6_cb(const char *elem, int len, void *usr)
1310
0
{
1311
0
    IPV6_STAT *s = usr;
1312
1313
    /* Error if 16 bytes written */
1314
0
    if (s->total == 16)
1315
0
        return 0;
1316
0
    if (len == 0) {
1317
        /* Zero length element, corresponds to '::' */
1318
0
        if (s->zero_pos == -1)
1319
0
            s->zero_pos = s->total;
1320
        /* If we've already got a :: its an error */
1321
0
        else if (s->zero_pos != s->total)
1322
0
            return 0;
1323
0
        s->zero_cnt++;
1324
0
    } else {
1325
        /* If more than 4 characters could be final a.b.c.d form */
1326
0
        if (len > 4) {
1327
            /* Need at least 4 bytes left */
1328
0
            if (s->total > 12)
1329
0
                return 0;
1330
            /* Must be end of string */
1331
0
            if (elem[len])
1332
0
                return 0;
1333
0
            if (!ipv4_from_asc(s->tmp + s->total, elem))
1334
0
                return 0;
1335
0
            s->total += 4;
1336
0
        } else {
1337
0
            if (!ipv6_hex(s->tmp + s->total, elem, len))
1338
0
                return 0;
1339
0
            s->total += 2;
1340
0
        }
1341
0
    }
1342
0
    return 1;
1343
0
}
1344
1345
/*
1346
 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1347
 */
1348
1349
static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1350
0
{
1351
0
    unsigned char c;
1352
0
    unsigned int num = 0;
1353
0
    int x;
1354
1355
0
    if (inlen > 4)
1356
0
        return 0;
1357
0
    while (inlen--) {
1358
0
        c = *in++;
1359
0
        num <<= 4;
1360
0
        x = OPENSSL_hexchar2int(c);
1361
0
        if (x < 0)
1362
0
            return 0;
1363
0
        num |= (char)x;
1364
0
    }
1365
0
    out[0] = num >> 8;
1366
0
    out[1] = num & 0xff;
1367
0
    return 1;
1368
0
}
1369
1370
int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1371
    unsigned long chtype)
1372
0
{
1373
0
    CONF_VALUE *v;
1374
0
    int i, mval, spec_char, plus_char;
1375
0
    char *p, *type;
1376
1377
0
    if (!nm)
1378
0
        return 0;
1379
1380
0
    for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1381
0
        v = sk_CONF_VALUE_value(dn_sk, i);
1382
0
        type = v->name;
1383
        /*
1384
         * Skip past any leading X. X: X, etc to allow for multiple instances
1385
         */
1386
0
        for (p = type; *p; p++) {
1387
0
#ifndef CHARSET_EBCDIC
1388
0
            spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));
1389
#else
1390
            spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])
1391
                || (*p == os_toascii['.']));
1392
#endif
1393
0
            if (spec_char) {
1394
0
                p++;
1395
0
                if (*p)
1396
0
                    type = p;
1397
0
                break;
1398
0
            }
1399
0
        }
1400
0
#ifndef CHARSET_EBCDIC
1401
0
        plus_char = (*type == '+');
1402
#else
1403
        plus_char = (*type == os_toascii['+']);
1404
#endif
1405
0
        if (plus_char) {
1406
0
            mval = -1;
1407
0
            type++;
1408
0
        } else {
1409
0
            mval = 0;
1410
0
        }
1411
0
        if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1412
0
                (unsigned char *)v->value, -1, -1,
1413
0
                mval))
1414
0
            return 0;
1415
0
    }
1416
0
    return 1;
1417
0
}
1418
1419
int OSSL_GENERAL_NAMES_print(BIO *out, GENERAL_NAMES *gens, int indent)
1420
20.7k
{
1421
20.7k
    int i;
1422
1423
42.4k
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1424
21.6k
        if (i > 0)
1425
12.6k
            BIO_puts(out, "\n");
1426
21.6k
        BIO_printf(out, "%*s", indent + 2, "");
1427
21.6k
        GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
1428
21.6k
    }
1429
20.7k
    return 1;
1430
20.7k
}
1431
1432
int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len)
1433
30.3k
{
1434
30.3k
    int result;
1435
30.3k
    char *hexbuf;
1436
1437
30.3k
    if (len == 0)
1438
8.51k
        return 1;
1439
1440
21.7k
    hexbuf = OPENSSL_buf2hexstr(buf, len);
1441
21.7k
    if (hexbuf == NULL)
1442
0
        return 0;
1443
21.7k
    result = BIO_puts(out, hexbuf) > 0;
1444
1445
21.7k
    OPENSSL_free(hexbuf);
1446
21.7k
    return result;
1447
21.7k
}