Coverage Report

Created: 2018-08-29 13:53

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