Coverage Report

Created: 2022-11-30 06:20

/src/openssl/ssl/ssl_cert.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * ! \file ssl/ssl_cert.c
3
 */
4
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5
 * All rights reserved.
6
 *
7
 * This package is an SSL implementation written
8
 * by Eric Young (eay@cryptsoft.com).
9
 * The implementation was written so as to conform with Netscapes SSL.
10
 *
11
 * This library is free for commercial and non-commercial use as long as
12
 * the following conditions are aheared to.  The following conditions
13
 * apply to all code found in this distribution, be it the RC4, RSA,
14
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15
 * included with this distribution is covered by the same copyright terms
16
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17
 *
18
 * Copyright remains Eric Young's, and as such any Copyright notices in
19
 * the code are not to be removed.
20
 * If this package is used in a product, Eric Young should be given attribution
21
 * as the author of the parts of the library used.
22
 * This can be in the form of a textual message at program startup or
23
 * in documentation (online or textual) provided with the package.
24
 *
25
 * Redistribution and use in source and binary forms, with or without
26
 * modification, are permitted provided that the following conditions
27
 * are met:
28
 * 1. Redistributions of source code must retain the copyright
29
 *    notice, this list of conditions and the following disclaimer.
30
 * 2. Redistributions in binary form must reproduce the above copyright
31
 *    notice, this list of conditions and the following disclaimer in the
32
 *    documentation and/or other materials provided with the distribution.
33
 * 3. All advertising materials mentioning features or use of this software
34
 *    must display the following acknowledgement:
35
 *    "This product includes cryptographic software written by
36
 *     Eric Young (eay@cryptsoft.com)"
37
 *    The word 'cryptographic' can be left out if the rouines from the library
38
 *    being used are not cryptographic related :-).
39
 * 4. If you include any Windows specific code (or a derivative thereof) from
40
 *    the apps directory (application code) you must include an acknowledgement:
41
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42
 *
43
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53
 * SUCH DAMAGE.
54
 *
55
 * The licence and distribution terms for any publically available version or
56
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
57
 * copied and put under another distribution licence
58
 * [including the GNU Public Licence.]
59
 */
60
/* ====================================================================
61
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
62
 *
63
 * Redistribution and use in source and binary forms, with or without
64
 * modification, are permitted provided that the following conditions
65
 * are met:
66
 *
67
 * 1. Redistributions of source code must retain the above copyright
68
 *    notice, this list of conditions and the following disclaimer.
69
 *
70
 * 2. Redistributions in binary form must reproduce the above copyright
71
 *    notice, this list of conditions and the following disclaimer in
72
 *    the documentation and/or other materials provided with the
73
 *    distribution.
74
 *
75
 * 3. All advertising materials mentioning features or use of this
76
 *    software must display the following acknowledgment:
77
 *    "This product includes software developed by the OpenSSL Project
78
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79
 *
80
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81
 *    endorse or promote products derived from this software without
82
 *    prior written permission. For written permission, please contact
83
 *    openssl-core@openssl.org.
84
 *
85
 * 5. Products derived from this software may not be called "OpenSSL"
86
 *    nor may "OpenSSL" appear in their names without prior written
87
 *    permission of the OpenSSL Project.
88
 *
89
 * 6. Redistributions of any form whatsoever must retain the following
90
 *    acknowledgment:
91
 *    "This product includes software developed by the OpenSSL Project
92
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93
 *
94
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105
 * OF THE POSSIBILITY OF SUCH DAMAGE.
106
 * ====================================================================
107
 *
108
 * This product includes cryptographic software written by Eric Young
109
 * (eay@cryptsoft.com).  This product includes software written by Tim
110
 * Hudson (tjh@cryptsoft.com).
111
 *
112
 */
113
/* ====================================================================
114
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115
 * ECC cipher suite support in OpenSSL originally developed by
116
 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117
 */
118
119
#include <stdio.h>
120
121
#include "e_os.h"
122
#ifndef NO_SYS_TYPES_H
123
# include <sys/types.h>
124
#endif
125
126
#include "o_dir.h"
127
#include <openssl/objects.h>
128
#include <openssl/bio.h>
129
#include <openssl/pem.h>
130
#include <openssl/x509v3.h>
131
#ifndef OPENSSL_NO_DH
132
# include <openssl/dh.h>
133
#endif
134
#include <openssl/bn.h>
135
#include "ssl_locl.h"
136
137
int SSL_get_ex_data_X509_STORE_CTX_idx(void)
138
1.24k
{
139
1.24k
    static volatile int ssl_x509_store_ctx_idx = -1;
140
1.24k
    int got_write_lock = 0;
141
142
1.24k
    if (((size_t)&ssl_x509_store_ctx_idx &
143
1.24k
         (sizeof(ssl_x509_store_ctx_idx) - 1))
144
1.24k
        == 0) {                 /* check alignment, practically always true */
145
1.24k
        int ret;
146
147
1.24k
        if ((ret = ssl_x509_store_ctx_idx) < 0) {
148
8
            CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
149
8
            if ((ret = ssl_x509_store_ctx_idx) < 0) {
150
8
                ret = ssl_x509_store_ctx_idx =
151
8
                    X509_STORE_CTX_get_ex_new_index(0,
152
8
                                                    "SSL for verify callback",
153
8
                                                    NULL, NULL, NULL);
154
8
            }
155
8
            CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
156
8
        }
157
158
1.24k
        return ret;
159
1.24k
    } else {                    /* commonly eliminated */
160
161
0
        CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
162
163
0
        if (ssl_x509_store_ctx_idx < 0) {
164
0
            CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
165
0
            CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
166
0
            got_write_lock = 1;
167
168
0
            if (ssl_x509_store_ctx_idx < 0) {
169
0
                ssl_x509_store_ctx_idx =
170
0
                    X509_STORE_CTX_get_ex_new_index(0,
171
0
                                                    "SSL for verify callback",
172
0
                                                    NULL, NULL, NULL);
173
0
            }
174
0
        }
175
176
0
        if (got_write_lock)
177
0
            CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
178
0
        else
179
0
            CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
180
181
0
        return ssl_x509_store_ctx_idx;
182
0
    }
183
1.24k
}
184
185
void ssl_cert_set_default_md(CERT *cert)
186
1.24k
{
187
    /* Set digest values to defaults */
188
1.24k
#ifndef OPENSSL_NO_DSA
189
1.24k
    cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
190
1.24k
#endif
191
1.24k
#ifndef OPENSSL_NO_RSA
192
1.24k
    cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
193
1.24k
    cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
194
1.24k
#endif
195
1.24k
#ifndef OPENSSL_NO_ECDSA
196
1.24k
    cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
197
1.24k
#endif
198
1.24k
}
199
200
CERT *ssl_cert_new(void)
201
1.24k
{
202
1.24k
    CERT *ret;
203
204
1.24k
    ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
205
1.24k
    if (ret == NULL) {
206
0
        SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
207
0
        return (NULL);
208
0
    }
209
1.24k
    memset(ret, 0, sizeof(CERT));
210
211
1.24k
    ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]);
212
1.24k
    ret->references = 1;
213
1.24k
    ssl_cert_set_default_md(ret);
214
1.24k
    return (ret);
215
1.24k
}
216
217
CERT *ssl_cert_dup(CERT *cert)
218
0
{
219
0
    CERT *ret;
220
0
    int i;
221
222
0
    ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
223
0
    if (ret == NULL) {
224
0
        SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
225
0
        return (NULL);
226
0
    }
227
228
0
    memset(ret, 0, sizeof(CERT));
229
230
0
    ret->references = 1;
231
0
    ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
232
    /*
233
     * or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
234
     * more readable
235
     */
236
237
0
    ret->valid = cert->valid;
238
0
    ret->mask_k = cert->mask_k;
239
0
    ret->mask_a = cert->mask_a;
240
0
    ret->export_mask_k = cert->export_mask_k;
241
0
    ret->export_mask_a = cert->export_mask_a;
242
243
0
#ifndef OPENSSL_NO_RSA
244
0
    if (cert->rsa_tmp != NULL) {
245
0
        RSA_up_ref(cert->rsa_tmp);
246
0
        ret->rsa_tmp = cert->rsa_tmp;
247
0
    }
248
0
    ret->rsa_tmp_cb = cert->rsa_tmp_cb;
249
0
#endif
250
251
0
#ifndef OPENSSL_NO_DH
252
0
    if (cert->dh_tmp != NULL) {
253
0
        ret->dh_tmp = DHparams_dup(cert->dh_tmp);
254
0
        if (ret->dh_tmp == NULL) {
255
0
            SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
256
0
            goto err;
257
0
        }
258
0
        if (cert->dh_tmp->priv_key) {
259
0
            BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
260
0
            if (!b) {
261
0
                SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
262
0
                goto err;
263
0
            }
264
0
            ret->dh_tmp->priv_key = b;
265
0
        }
266
0
        if (cert->dh_tmp->pub_key) {
267
0
            BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
268
0
            if (!b) {
269
0
                SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
270
0
                goto err;
271
0
            }
272
0
            ret->dh_tmp->pub_key = b;
273
0
        }
274
0
    }
275
0
    ret->dh_tmp_cb = cert->dh_tmp_cb;
276
0
#endif
277
278
0
#ifndef OPENSSL_NO_ECDH
279
0
    if (cert->ecdh_tmp) {
280
0
        ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
281
0
        if (ret->ecdh_tmp == NULL) {
282
0
            SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB);
283
0
            goto err;
284
0
        }
285
0
    }
286
0
    ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
287
0
    ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
288
0
#endif
289
290
0
    for (i = 0; i < SSL_PKEY_NUM; i++) {
291
0
        CERT_PKEY *cpk = cert->pkeys + i;
292
0
        CERT_PKEY *rpk = ret->pkeys + i;
293
0
        if (cpk->x509 != NULL) {
294
0
            rpk->x509 = cpk->x509;
295
0
            CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509);
296
0
        }
297
298
0
        if (cpk->privatekey != NULL) {
299
0
            rpk->privatekey = cpk->privatekey;
300
0
            CRYPTO_add(&cpk->privatekey->references, 1, CRYPTO_LOCK_EVP_PKEY);
301
0
        }
302
303
0
        if (cpk->chain) {
304
0
            rpk->chain = X509_chain_up_ref(cpk->chain);
305
0
            if (!rpk->chain) {
306
0
                SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
307
0
                goto err;
308
0
            }
309
0
        }
310
0
        rpk->valid_flags = 0;
311
0
#ifndef OPENSSL_NO_TLSEXT
312
0
        if (cert->pkeys[i].serverinfo != NULL) {
313
            /* Just copy everything. */
314
0
            ret->pkeys[i].serverinfo =
315
0
                OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
316
0
            if (ret->pkeys[i].serverinfo == NULL) {
317
0
                SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
318
0
                goto err;
319
0
            }
320
0
            ret->pkeys[i].serverinfo_length =
321
0
                cert->pkeys[i].serverinfo_length;
322
0
            memcpy(ret->pkeys[i].serverinfo,
323
0
                   cert->pkeys[i].serverinfo,
324
0
                   cert->pkeys[i].serverinfo_length);
325
0
        }
326
0
#endif
327
0
    }
328
329
    /*
330
     * Set digests to defaults. NB: we don't copy existing values as they
331
     * will be set during handshake.
332
     */
333
0
    ssl_cert_set_default_md(ret);
334
    /* Peer sigalgs set to NULL as we get these from handshake too */
335
0
    ret->peer_sigalgs = NULL;
336
0
    ret->peer_sigalgslen = 0;
337
    /* Configured sigalgs however we copy across */
338
339
0
    if (cert->conf_sigalgs) {
340
0
        ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
341
0
        if (!ret->conf_sigalgs)
342
0
            goto err;
343
0
        memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen);
344
0
        ret->conf_sigalgslen = cert->conf_sigalgslen;
345
0
    } else
346
0
        ret->conf_sigalgs = NULL;
347
348
0
    if (cert->client_sigalgs) {
349
0
        ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
350
0
        if (!ret->client_sigalgs)
351
0
            goto err;
352
0
        memcpy(ret->client_sigalgs, cert->client_sigalgs,
353
0
               cert->client_sigalgslen);
354
0
        ret->client_sigalgslen = cert->client_sigalgslen;
355
0
    } else
356
0
        ret->client_sigalgs = NULL;
357
    /* Shared sigalgs also NULL */
358
0
    ret->shared_sigalgs = NULL;
359
    /* Copy any custom client certificate types */
360
0
    if (cert->ctypes) {
361
0
        ret->ctypes = OPENSSL_malloc(cert->ctype_num);
362
0
        if (!ret->ctypes)
363
0
            goto err;
364
0
        memcpy(ret->ctypes, cert->ctypes, cert->ctype_num);
365
0
        ret->ctype_num = cert->ctype_num;
366
0
    }
367
368
0
    ret->cert_flags = cert->cert_flags;
369
370
0
    ret->cert_cb = cert->cert_cb;
371
0
    ret->cert_cb_arg = cert->cert_cb_arg;
372
373
0
    if (cert->verify_store) {
374
0
        CRYPTO_add(&cert->verify_store->references, 1,
375
0
                   CRYPTO_LOCK_X509_STORE);
376
0
        ret->verify_store = cert->verify_store;
377
0
    }
378
379
0
    if (cert->chain_store) {
380
0
        CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
381
0
        ret->chain_store = cert->chain_store;
382
0
    }
383
384
0
    ret->ciphers_raw = NULL;
385
386
0
#ifndef OPENSSL_NO_TLSEXT
387
0
    if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext))
388
0
        goto err;
389
0
    if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext))
390
0
        goto err;
391
0
#endif
392
393
0
    return (ret);
394
395
0
 err:
396
0
#ifndef OPENSSL_NO_RSA
397
0
    if (ret->rsa_tmp != NULL)
398
0
        RSA_free(ret->rsa_tmp);
399
0
#endif
400
0
#ifndef OPENSSL_NO_DH
401
0
    if (ret->dh_tmp != NULL)
402
0
        DH_free(ret->dh_tmp);
403
0
#endif
404
0
#ifndef OPENSSL_NO_ECDH
405
0
    if (ret->ecdh_tmp != NULL)
406
0
        EC_KEY_free(ret->ecdh_tmp);
407
0
#endif
408
409
0
#ifndef OPENSSL_NO_TLSEXT
410
0
    custom_exts_free(&ret->cli_ext);
411
0
    custom_exts_free(&ret->srv_ext);
412
0
#endif
413
414
0
    ssl_cert_clear_certs(ret);
415
0
    OPENSSL_free(ret);
416
417
0
    return NULL;
418
0
}
419
420
/* Free up and clear all certificates and chains */
421
422
void ssl_cert_clear_certs(CERT *c)
423
1.24k
{
424
1.24k
    int i;
425
1.24k
    if (c == NULL)
426
0
        return;
427
11.1k
    for (i = 0; i < SSL_PKEY_NUM; i++) {
428
9.94k
        CERT_PKEY *cpk = c->pkeys + i;
429
9.94k
        if (cpk->x509) {
430
0
            X509_free(cpk->x509);
431
0
            cpk->x509 = NULL;
432
0
        }
433
9.94k
        if (cpk->privatekey) {
434
0
            EVP_PKEY_free(cpk->privatekey);
435
0
            cpk->privatekey = NULL;
436
0
        }
437
9.94k
        if (cpk->chain) {
438
0
            sk_X509_pop_free(cpk->chain, X509_free);
439
0
            cpk->chain = NULL;
440
0
        }
441
9.94k
#ifndef OPENSSL_NO_TLSEXT
442
9.94k
        if (cpk->serverinfo) {
443
0
            OPENSSL_free(cpk->serverinfo);
444
0
            cpk->serverinfo = NULL;
445
0
            cpk->serverinfo_length = 0;
446
0
        }
447
9.94k
#endif
448
        /* Clear all flags apart from explicit sign */
449
9.94k
        cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
450
9.94k
    }
451
1.24k
}
452
453
void ssl_cert_free(CERT *c)
454
1.24k
{
455
1.24k
    int i;
456
457
1.24k
    if (c == NULL)
458
0
        return;
459
460
1.24k
    i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT);
461
#ifdef REF_PRINT
462
    REF_PRINT("CERT", c);
463
#endif
464
1.24k
    if (i > 0)
465
0
        return;
466
#ifdef REF_CHECK
467
    if (i < 0) {
468
        fprintf(stderr, "ssl_cert_free, bad reference count\n");
469
        abort();                /* ok */
470
    }
471
#endif
472
473
1.24k
#ifndef OPENSSL_NO_RSA
474
1.24k
    if (c->rsa_tmp)
475
0
        RSA_free(c->rsa_tmp);
476
1.24k
#endif
477
1.24k
#ifndef OPENSSL_NO_DH
478
1.24k
    if (c->dh_tmp)
479
0
        DH_free(c->dh_tmp);
480
1.24k
#endif
481
1.24k
#ifndef OPENSSL_NO_ECDH
482
1.24k
    if (c->ecdh_tmp)
483
0
        EC_KEY_free(c->ecdh_tmp);
484
1.24k
#endif
485
486
1.24k
    ssl_cert_clear_certs(c);
487
1.24k
    if (c->peer_sigalgs)
488
0
        OPENSSL_free(c->peer_sigalgs);
489
1.24k
    if (c->conf_sigalgs)
490
0
        OPENSSL_free(c->conf_sigalgs);
491
1.24k
    if (c->client_sigalgs)
492
0
        OPENSSL_free(c->client_sigalgs);
493
1.24k
    if (c->shared_sigalgs)
494
0
        OPENSSL_free(c->shared_sigalgs);
495
1.24k
    if (c->ctypes)
496
0
        OPENSSL_free(c->ctypes);
497
1.24k
    if (c->verify_store)
498
0
        X509_STORE_free(c->verify_store);
499
1.24k
    if (c->chain_store)
500
0
        X509_STORE_free(c->chain_store);
501
1.24k
    if (c->ciphers_raw)
502
0
        OPENSSL_free(c->ciphers_raw);
503
1.24k
#ifndef OPENSSL_NO_TLSEXT
504
1.24k
    custom_exts_free(&c->cli_ext);
505
1.24k
    custom_exts_free(&c->srv_ext);
506
1.24k
    if (c->alpn_proposed)
507
0
        OPENSSL_free(c->alpn_proposed);
508
1.24k
#endif
509
1.24k
    OPENSSL_free(c);
510
1.24k
}
511
512
int ssl_cert_inst(CERT **o)
513
0
{
514
    /*
515
     * Create a CERT if there isn't already one (which cannot really happen,
516
     * as it is initially created in SSL_CTX_new; but the earlier code
517
     * usually allows for that one being non-existant, so we follow that
518
     * behaviour, as it might turn out that there actually is a reason for it
519
     * -- but I'm not sure that *all* of the existing code could cope with
520
     * s->cert being NULL, otherwise we could do without the initialization
521
     * in SSL_CTX_new).
522
     */
523
524
0
    if (o == NULL) {
525
0
        SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
526
0
        return (0);
527
0
    }
528
0
    if (*o == NULL) {
529
0
        if ((*o = ssl_cert_new()) == NULL) {
530
0
            SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
531
0
            return (0);
532
0
        }
533
0
    }
534
0
    return (1);
535
0
}
536
537
int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
538
0
{
539
0
    CERT_PKEY *cpk = c->key;
540
0
    if (!cpk)
541
0
        return 0;
542
0
    if (cpk->chain)
543
0
        sk_X509_pop_free(cpk->chain, X509_free);
544
0
    cpk->chain = chain;
545
0
    return 1;
546
0
}
547
548
int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
549
0
{
550
0
    STACK_OF(X509) *dchain;
551
0
    if (!chain)
552
0
        return ssl_cert_set0_chain(c, NULL);
553
0
    dchain = X509_chain_up_ref(chain);
554
0
    if (!dchain)
555
0
        return 0;
556
0
    if (!ssl_cert_set0_chain(c, dchain)) {
557
0
        sk_X509_pop_free(dchain, X509_free);
558
0
        return 0;
559
0
    }
560
0
    return 1;
561
0
}
562
563
int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
564
0
{
565
0
    CERT_PKEY *cpk = c->key;
566
0
    if (!cpk)
567
0
        return 0;
568
0
    if (!cpk->chain)
569
0
        cpk->chain = sk_X509_new_null();
570
0
    if (!cpk->chain || !sk_X509_push(cpk->chain, x))
571
0
        return 0;
572
0
    return 1;
573
0
}
574
575
int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
576
0
{
577
0
    if (!ssl_cert_add0_chain_cert(c, x))
578
0
        return 0;
579
0
    CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
580
0
    return 1;
581
0
}
582
583
int ssl_cert_select_current(CERT *c, X509 *x)
584
0
{
585
0
    int i;
586
0
    if (x == NULL)
587
0
        return 0;
588
0
    for (i = 0; i < SSL_PKEY_NUM; i++) {
589
0
        CERT_PKEY *cpk = c->pkeys + i;
590
0
        if (cpk->x509 == x && cpk->privatekey) {
591
0
            c->key = cpk;
592
0
            return 1;
593
0
        }
594
0
    }
595
596
0
    for (i = 0; i < SSL_PKEY_NUM; i++) {
597
0
        CERT_PKEY *cpk = c->pkeys + i;
598
0
        if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
599
0
            c->key = cpk;
600
0
            return 1;
601
0
        }
602
0
    }
603
0
    return 0;
604
0
}
605
606
int ssl_cert_set_current(CERT *c, long op)
607
0
{
608
0
    int i, idx;
609
0
    if (!c)
610
0
        return 0;
611
0
    if (op == SSL_CERT_SET_FIRST)
612
0
        idx = 0;
613
0
    else if (op == SSL_CERT_SET_NEXT) {
614
0
        idx = (int)(c->key - c->pkeys + 1);
615
0
        if (idx >= SSL_PKEY_NUM)
616
0
            return 0;
617
0
    } else
618
0
        return 0;
619
0
    for (i = idx; i < SSL_PKEY_NUM; i++) {
620
0
        CERT_PKEY *cpk = c->pkeys + i;
621
0
        if (cpk->x509 && cpk->privatekey) {
622
0
            c->key = cpk;
623
0
            return 1;
624
0
        }
625
0
    }
626
0
    return 0;
627
0
}
628
629
void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
630
0
{
631
0
    c->cert_cb = cb;
632
0
    c->cert_cb_arg = arg;
633
0
}
634
635
SESS_CERT *ssl_sess_cert_new(void)
636
0
{
637
0
    SESS_CERT *ret;
638
639
0
    ret = OPENSSL_malloc(sizeof *ret);
640
0
    if (ret == NULL) {
641
0
        SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
642
0
        return NULL;
643
0
    }
644
645
0
    memset(ret, 0, sizeof *ret);
646
0
    ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
647
0
    ret->references = 1;
648
649
0
    return ret;
650
0
}
651
652
void ssl_sess_cert_free(SESS_CERT *sc)
653
0
{
654
0
    int i;
655
656
0
    if (sc == NULL)
657
0
        return;
658
659
0
    i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
660
#ifdef REF_PRINT
661
    REF_PRINT("SESS_CERT", sc);
662
#endif
663
0
    if (i > 0)
664
0
        return;
665
#ifdef REF_CHECK
666
    if (i < 0) {
667
        fprintf(stderr, "ssl_sess_cert_free, bad reference count\n");
668
        abort();                /* ok */
669
    }
670
#endif
671
672
    /* i == 0 */
673
0
    if (sc->cert_chain != NULL)
674
0
        sk_X509_pop_free(sc->cert_chain, X509_free);
675
0
    for (i = 0; i < SSL_PKEY_NUM; i++) {
676
0
        if (sc->peer_pkeys[i].x509 != NULL)
677
0
            X509_free(sc->peer_pkeys[i].x509);
678
#if 0                           /* We don't have the peer's private key.
679
                                 * These lines are just * here as a reminder
680
                                 * that we're still using a
681
                                 * not-quite-appropriate * data structure. */
682
        if (sc->peer_pkeys[i].privatekey != NULL)
683
            EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
684
#endif
685
0
    }
686
687
0
#ifndef OPENSSL_NO_RSA
688
0
    if (sc->peer_rsa_tmp != NULL)
689
0
        RSA_free(sc->peer_rsa_tmp);
690
0
#endif
691
0
#ifndef OPENSSL_NO_DH
692
0
    if (sc->peer_dh_tmp != NULL)
693
0
        DH_free(sc->peer_dh_tmp);
694
0
#endif
695
0
#ifndef OPENSSL_NO_ECDH
696
0
    if (sc->peer_ecdh_tmp != NULL)
697
0
        EC_KEY_free(sc->peer_ecdh_tmp);
698
0
#endif
699
700
0
    OPENSSL_free(sc);
701
0
}
702
703
int ssl_set_peer_cert_type(SESS_CERT *sc, int type)
704
0
{
705
0
    sc->peer_cert_type = type;
706
0
    return (1);
707
0
}
708
709
int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
710
0
{
711
0
    X509 *x;
712
0
    int i;
713
0
    X509_STORE *verify_store;
714
0
    X509_STORE_CTX ctx;
715
716
0
    if (s->cert->verify_store)
717
0
        verify_store = s->cert->verify_store;
718
0
    else
719
0
        verify_store = s->ctx->cert_store;
720
721
0
    if ((sk == NULL) || (sk_X509_num(sk) == 0))
722
0
        return (0);
723
724
0
    x = sk_X509_value(sk, 0);
725
0
    if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
726
0
        SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
727
0
        return (0);
728
0
    }
729
    /* Set suite B flags if needed */
730
0
    X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s));
731
#if 0
732
    if (SSL_get_verify_depth(s) >= 0)
733
        X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
734
#endif
735
0
    X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
736
737
    /*
738
     * We need to inherit the verify parameters. These can be determined by
739
     * the context: if its a server it will verify SSL client certificates or
740
     * vice versa.
741
     */
742
743
0
    X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
744
    /*
745
     * Anything non-default in "param" should overwrite anything in the ctx.
746
     */
747
0
    X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
748
749
0
    if (s->verify_callback)
750
0
        X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
751
752
0
    if (s->ctx->app_verify_callback != NULL)
753
0
#if 1                           /* new with OpenSSL 0.9.7 */
754
0
        i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
755
#else
756
        i = s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
757
#endif
758
0
    else {
759
0
#ifndef OPENSSL_NO_X509_VERIFY
760
0
        i = X509_verify_cert(&ctx);
761
#else
762
        i = 0;
763
        ctx.error = X509_V_ERR_APPLICATION_VERIFICATION;
764
        SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_NO_VERIFY_CALLBACK);
765
#endif
766
0
    }
767
768
0
    s->verify_result = ctx.error;
769
0
    X509_STORE_CTX_cleanup(&ctx);
770
771
0
    return (i);
772
0
}
773
774
static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
775
                               STACK_OF(X509_NAME) *name_list)
776
0
{
777
0
    if (*ca_list != NULL)
778
0
        sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
779
780
0
    *ca_list = name_list;
781
0
}
782
783
STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
784
0
{
785
0
    int i;
786
0
    STACK_OF(X509_NAME) *ret;
787
0
    X509_NAME *name;
788
789
0
    ret = sk_X509_NAME_new_null();
790
0
    for (i = 0; i < sk_X509_NAME_num(sk); i++) {
791
0
        name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
792
0
        if ((name == NULL) || !sk_X509_NAME_push(ret, name)) {
793
0
            sk_X509_NAME_pop_free(ret, X509_NAME_free);
794
0
            return (NULL);
795
0
        }
796
0
    }
797
0
    return (ret);
798
0
}
799
800
void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
801
0
{
802
0
    set_client_CA_list(&(s->client_CA), name_list);
803
0
}
804
805
void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
806
0
{
807
0
    set_client_CA_list(&(ctx->client_CA), name_list);
808
0
}
809
810
STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
811
0
{
812
0
    return (ctx->client_CA);
813
0
}
814
815
STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
816
0
{
817
0
    if (s->type == SSL_ST_CONNECT) { /* we are in the client */
818
0
        if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL))
819
0
            return (s->s3->tmp.ca_names);
820
0
        else
821
0
            return (NULL);
822
0
    } else {
823
0
        if (s->client_CA != NULL)
824
0
            return (s->client_CA);
825
0
        else
826
0
            return (s->ctx->client_CA);
827
0
    }
828
0
}
829
830
static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x)
831
0
{
832
0
    X509_NAME *name;
833
834
0
    if (x == NULL)
835
0
        return (0);
836
0
    if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL))
837
0
        return (0);
838
839
0
    if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
840
0
        return (0);
841
842
0
    if (!sk_X509_NAME_push(*sk, name)) {
843
0
        X509_NAME_free(name);
844
0
        return (0);
845
0
    }
846
0
    return (1);
847
0
}
848
849
int SSL_add_client_CA(SSL *ssl, X509 *x)
850
0
{
851
0
    return (add_client_CA(&(ssl->client_CA), x));
852
0
}
853
854
int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
855
0
{
856
0
    return (add_client_CA(&(ctx->client_CA), x));
857
0
}
858
859
static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
860
0
{
861
0
    return (X509_NAME_cmp(*a, *b));
862
0
}
863
864
#ifndef OPENSSL_NO_STDIO
865
/**
866
 * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
867
 * it doesn't really have anything to do with clients (except that a common use
868
 * for a stack of CAs is to send it to the client). Actually, it doesn't have
869
 * much to do with CAs, either, since it will load any old cert.
870
 * \param file the file containing one or more certs.
871
 * \return a ::STACK containing the certs.
872
 */
873
STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
874
0
{
875
0
    BIO *in;
876
0
    X509 *x = NULL;
877
0
    X509_NAME *xn = NULL;
878
0
    STACK_OF(X509_NAME) *ret = NULL, *sk;
879
880
0
    sk = sk_X509_NAME_new(xname_cmp);
881
882
0
    in = BIO_new(BIO_s_file_internal());
883
884
0
    if ((sk == NULL) || (in == NULL)) {
885
0
        SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
886
0
        goto err;
887
0
    }
888
889
0
    if (!BIO_read_filename(in, file))
890
0
        goto err;
891
892
0
    for (;;) {
893
0
        if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
894
0
            break;
895
0
        if (ret == NULL) {
896
0
            ret = sk_X509_NAME_new_null();
897
0
            if (ret == NULL) {
898
0
                SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
899
0
                goto err;
900
0
            }
901
0
        }
902
0
        if ((xn = X509_get_subject_name(x)) == NULL)
903
0
            goto err;
904
        /* check for duplicates */
905
0
        xn = X509_NAME_dup(xn);
906
0
        if (xn == NULL)
907
0
            goto err;
908
0
        if (sk_X509_NAME_find(sk, xn) >= 0)
909
0
            X509_NAME_free(xn);
910
0
        else {
911
0
            sk_X509_NAME_push(sk, xn);
912
0
            sk_X509_NAME_push(ret, xn);
913
0
        }
914
0
    }
915
916
0
    if (0) {
917
0
 err:
918
0
        if (ret != NULL)
919
0
            sk_X509_NAME_pop_free(ret, X509_NAME_free);
920
0
        ret = NULL;
921
0
    }
922
0
    if (sk != NULL)
923
0
        sk_X509_NAME_free(sk);
924
0
    if (in != NULL)
925
0
        BIO_free(in);
926
0
    if (x != NULL)
927
0
        X509_free(x);
928
0
    if (ret != NULL)
929
0
        ERR_clear_error();
930
0
    return (ret);
931
0
}
932
#endif
933
934
/**
935
 * Add a file of certs to a stack.
936
 * \param stack the stack to add to.
937
 * \param file the file to add from. All certs in this file that are not
938
 * already in the stack will be added.
939
 * \return 1 for success, 0 for failure. Note that in the case of failure some
940
 * certs may have been added to \c stack.
941
 */
942
943
int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
944
                                        const char *file)
945
0
{
946
0
    BIO *in;
947
0
    X509 *x = NULL;
948
0
    X509_NAME *xn = NULL;
949
0
    int ret = 1;
950
0
    int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
951
952
0
    oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
953
954
0
    in = BIO_new(BIO_s_file_internal());
955
956
0
    if (in == NULL) {
957
0
        SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,
958
0
               ERR_R_MALLOC_FAILURE);
959
0
        goto err;
960
0
    }
961
962
0
    if (!BIO_read_filename(in, file))
963
0
        goto err;
964
965
0
    for (;;) {
966
0
        if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
967
0
            break;
968
0
        if ((xn = X509_get_subject_name(x)) == NULL)
969
0
            goto err;
970
0
        xn = X509_NAME_dup(xn);
971
0
        if (xn == NULL)
972
0
            goto err;
973
0
        if (sk_X509_NAME_find(stack, xn) >= 0)
974
0
            X509_NAME_free(xn);
975
0
        else
976
0
            sk_X509_NAME_push(stack, xn);
977
0
    }
978
979
0
    ERR_clear_error();
980
981
0
    if (0) {
982
0
 err:
983
0
        ret = 0;
984
0
    }
985
0
    if (in != NULL)
986
0
        BIO_free(in);
987
0
    if (x != NULL)
988
0
        X509_free(x);
989
990
0
    (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
991
992
0
    return ret;
993
0
}
994
995
/**
996
 * Add a directory of certs to a stack.
997
 * \param stack the stack to append to.
998
 * \param dir the directory to append from. All files in this directory will be
999
 * examined as potential certs. Any that are acceptable to
1000
 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
1001
 * included.
1002
 * \return 1 for success, 0 for failure. Note that in the case of failure some
1003
 * certs may have been added to \c stack.
1004
 */
1005
1006
int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
1007
                                       const char *dir)
1008
0
{
1009
0
    OPENSSL_DIR_CTX *d = NULL;
1010
0
    const char *filename;
1011
0
    int ret = 0;
1012
1013
0
    CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
1014
1015
    /* Note that a side effect is that the CAs will be sorted by name */
1016
1017
0
    while ((filename = OPENSSL_DIR_read(&d, dir))) {
1018
0
        char buf[1024];
1019
0
        int r;
1020
1021
0
        if (strlen(dir) + strlen(filename) + 2 > sizeof buf) {
1022
0
            SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
1023
0
                   SSL_R_PATH_TOO_LONG);
1024
0
            goto err;
1025
0
        }
1026
#ifdef OPENSSL_SYS_VMS
1027
        r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename);
1028
#else
1029
0
        r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
1030
0
#endif
1031
0
        if (r <= 0 || r >= (int)sizeof(buf))
1032
0
            goto err;
1033
0
        if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
1034
0
            goto err;
1035
0
    }
1036
1037
0
    if (errno) {
1038
0
        SYSerr(SYS_F_OPENDIR, get_last_sys_error());
1039
0
        ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
1040
0
        SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
1041
0
        goto err;
1042
0
    }
1043
1044
0
    ret = 1;
1045
1046
0
 err:
1047
0
    if (d)
1048
0
        OPENSSL_DIR_end(&d);
1049
0
    CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
1050
0
    return ret;
1051
0
}
1052
1053
/* Add a certificate to a BUF_MEM structure */
1054
1055
static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
1056
0
{
1057
0
    int n;
1058
0
    unsigned char *p;
1059
1060
0
    n = i2d_X509(x, NULL);
1061
0
    if (n < 0 || !BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
1062
0
        SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
1063
0
        return 0;
1064
0
    }
1065
0
    p = (unsigned char *)&(buf->data[*l]);
1066
0
    l2n3(n, p);
1067
0
    n = i2d_X509(x, &p);
1068
0
    if (n < 0) {
1069
        /* Shouldn't happen */
1070
0
        SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
1071
0
        return 0;
1072
0
    }
1073
0
    *l += n + 3;
1074
1075
0
    return 1;
1076
0
}
1077
1078
/* Add certificate chain to internal SSL BUF_MEM strcuture */
1079
int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
1080
0
{
1081
0
    BUF_MEM *buf = s->init_buf;
1082
0
    int no_chain;
1083
0
    int i;
1084
1085
0
    X509 *x;
1086
0
    STACK_OF(X509) *extra_certs;
1087
0
    X509_STORE *chain_store;
1088
1089
0
    if (cpk)
1090
0
        x = cpk->x509;
1091
0
    else
1092
0
        x = NULL;
1093
1094
0
    if (s->cert->chain_store)
1095
0
        chain_store = s->cert->chain_store;
1096
0
    else
1097
0
        chain_store = s->ctx->cert_store;
1098
1099
    /*
1100
     * If we have a certificate specific chain use it, else use parent ctx.
1101
     */
1102
0
    if (cpk && cpk->chain)
1103
0
        extra_certs = cpk->chain;
1104
0
    else
1105
0
        extra_certs = s->ctx->extra_certs;
1106
1107
0
    if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
1108
0
        no_chain = 1;
1109
0
    else
1110
0
        no_chain = 0;
1111
1112
    /* TLSv1 sends a chain with nothing in it, instead of an alert */
1113
0
    if (!BUF_MEM_grow_clean(buf, 10)) {
1114
0
        SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);
1115
0
        return 0;
1116
0
    }
1117
0
    if (x != NULL) {
1118
0
        if (no_chain) {
1119
0
            if (!ssl_add_cert_to_buf(buf, l, x))
1120
0
                return 0;
1121
0
        } else {
1122
0
            X509_STORE_CTX xs_ctx;
1123
1124
0
            if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
1125
0
                SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
1126
0
                return (0);
1127
0
            }
1128
0
            X509_verify_cert(&xs_ctx);
1129
            /* Don't leave errors in the queue */
1130
0
            ERR_clear_error();
1131
0
            for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
1132
0
                x = sk_X509_value(xs_ctx.chain, i);
1133
1134
0
                if (!ssl_add_cert_to_buf(buf, l, x)) {
1135
0
                    X509_STORE_CTX_cleanup(&xs_ctx);
1136
0
                    return 0;
1137
0
                }
1138
0
            }
1139
0
            X509_STORE_CTX_cleanup(&xs_ctx);
1140
0
        }
1141
0
    }
1142
0
    for (i = 0; i < sk_X509_num(extra_certs); i++) {
1143
0
        x = sk_X509_value(extra_certs, i);
1144
0
        if (!ssl_add_cert_to_buf(buf, l, x))
1145
0
            return 0;
1146
0
    }
1147
1148
0
    return 1;
1149
0
}
1150
1151
/* Build a certificate chain for current certificate */
1152
int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
1153
0
{
1154
0
    CERT_PKEY *cpk = c->key;
1155
0
    X509_STORE_CTX xs_ctx;
1156
0
    STACK_OF(X509) *chain = NULL, *untrusted = NULL;
1157
0
    X509 *x;
1158
0
    int i, rv = 0;
1159
0
    unsigned long error;
1160
1161
0
    if (!cpk->x509) {
1162
0
        SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
1163
0
        goto err;
1164
0
    }
1165
    /* Rearranging and check the chain: add everything to a store */
1166
0
    if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
1167
0
        chain_store = X509_STORE_new();
1168
0
        if (!chain_store)
1169
0
            goto err;
1170
0
        for (i = 0; i < sk_X509_num(cpk->chain); i++) {
1171
0
            x = sk_X509_value(cpk->chain, i);
1172
0
            if (!X509_STORE_add_cert(chain_store, x)) {
1173
0
                error = ERR_peek_last_error();
1174
0
                if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1175
0
                    ERR_GET_REASON(error) !=
1176
0
                    X509_R_CERT_ALREADY_IN_HASH_TABLE)
1177
0
                    goto err;
1178
0
                ERR_clear_error();
1179
0
            }
1180
0
        }
1181
        /* Add EE cert too: it might be self signed */
1182
0
        if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
1183
0
            error = ERR_peek_last_error();
1184
0
            if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1185
0
                ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1186
0
                goto err;
1187
0
            ERR_clear_error();
1188
0
        }
1189
0
    } else {
1190
0
        if (c->chain_store)
1191
0
            chain_store = c->chain_store;
1192
1193
0
        if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
1194
0
            untrusted = cpk->chain;
1195
0
    }
1196
1197
0
    if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
1198
0
        SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
1199
0
        goto err;
1200
0
    }
1201
    /* Set suite B flags if needed */
1202
0
    X509_STORE_CTX_set_flags(&xs_ctx,
1203
0
                             c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
1204
1205
0
    i = X509_verify_cert(&xs_ctx);
1206
0
    if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
1207
0
        if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
1208
0
            ERR_clear_error();
1209
0
        i = 1;
1210
0
        rv = 2;
1211
0
    }
1212
0
    if (i > 0)
1213
0
        chain = X509_STORE_CTX_get1_chain(&xs_ctx);
1214
0
    if (i <= 0) {
1215
0
        SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
1216
0
        i = X509_STORE_CTX_get_error(&xs_ctx);
1217
0
        ERR_add_error_data(2, "Verify error:",
1218
0
                           X509_verify_cert_error_string(i));
1219
1220
0
        X509_STORE_CTX_cleanup(&xs_ctx);
1221
0
        goto err;
1222
0
    }
1223
0
    X509_STORE_CTX_cleanup(&xs_ctx);
1224
0
    if (cpk->chain)
1225
0
        sk_X509_pop_free(cpk->chain, X509_free);
1226
    /* Remove EE certificate from chain */
1227
0
    x = sk_X509_shift(chain);
1228
0
    X509_free(x);
1229
0
    if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
1230
0
        if (sk_X509_num(chain) > 0) {
1231
            /* See if last cert is self signed */
1232
0
            x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1233
0
            X509_check_purpose(x, -1, 0);
1234
0
            if (x->ex_flags & EXFLAG_SS) {
1235
0
                x = sk_X509_pop(chain);
1236
0
                X509_free(x);
1237
0
            }
1238
0
        }
1239
0
    }
1240
0
    cpk->chain = chain;
1241
0
    if (rv == 0)
1242
0
        rv = 1;
1243
0
 err:
1244
0
    if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1245
0
        X509_STORE_free(chain_store);
1246
1247
0
    return rv;
1248
0
}
1249
1250
int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1251
0
{
1252
0
    X509_STORE **pstore;
1253
0
    if (chain)
1254
0
        pstore = &c->chain_store;
1255
0
    else
1256
0
        pstore = &c->verify_store;
1257
0
    if (*pstore)
1258
0
        X509_STORE_free(*pstore);
1259
0
    *pstore = store;
1260
0
    if (ref && store)
1261
0
        CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
1262
0
    return 1;
1263
0
}