Coverage Report

Created: 2018-08-29 13:53

/src/openssl/ssl/ssl_rsa.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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
#include <stdio.h>
11
#include "ssl_locl.h"
12
#include "packet_locl.h"
13
#include <openssl/bio.h>
14
#include <openssl/objects.h>
15
#include <openssl/evp.h>
16
#include <openssl/x509.h>
17
#include <openssl/pem.h>
18
19
static int ssl_set_cert(CERT *c, X509 *x509);
20
static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
21
22
0
#define  SYNTHV1CONTEXT     (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
23
0
                             | SSL_EXT_CLIENT_HELLO \
24
0
                             | SSL_EXT_TLS1_2_SERVER_HELLO \
25
0
                             | SSL_EXT_IGNORE_ON_RESUMPTION)
26
27
int SSL_use_certificate(SSL *ssl, X509 *x)
28
0
{
29
0
    int rv;
30
0
    if (x == NULL) {
31
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
32
0
        return 0;
33
0
    }
34
0
    rv = ssl_security_cert(ssl, NULL, x, 0, 1);
35
0
    if (rv != 1) {
36
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
37
0
        return 0;
38
0
    }
39
0
40
0
    return ssl_set_cert(ssl->cert, x);
41
0
}
42
43
int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
44
0
{
45
0
    int j;
46
0
    BIO *in;
47
0
    int ret = 0;
48
0
    X509 *x = NULL;
49
0
50
0
    in = BIO_new(BIO_s_file());
51
0
    if (in == NULL) {
52
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
53
0
        goto end;
54
0
    }
55
0
56
0
    if (BIO_read_filename(in, file) <= 0) {
57
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
58
0
        goto end;
59
0
    }
60
0
    if (type == SSL_FILETYPE_ASN1) {
61
0
        j = ERR_R_ASN1_LIB;
62
0
        x = d2i_X509_bio(in, NULL);
63
0
    } else if (type == SSL_FILETYPE_PEM) {
64
0
        j = ERR_R_PEM_LIB;
65
0
        x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
66
0
                              ssl->default_passwd_callback_userdata);
67
0
    } else {
68
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
69
0
        goto end;
70
0
    }
71
0
72
0
    if (x == NULL) {
73
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
74
0
        goto end;
75
0
    }
76
0
77
0
    ret = SSL_use_certificate(ssl, x);
78
0
 end:
79
0
    X509_free(x);
80
0
    BIO_free(in);
81
0
    return ret;
82
0
}
83
84
int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
85
0
{
86
0
    X509 *x;
87
0
    int ret;
88
0
89
0
    x = d2i_X509(NULL, &d, (long)len);
90
0
    if (x == NULL) {
91
0
        SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
92
0
        return 0;
93
0
    }
94
0
95
0
    ret = SSL_use_certificate(ssl, x);
96
0
    X509_free(x);
97
0
    return ret;
98
0
}
99
100
#ifndef OPENSSL_NO_RSA
101
int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
102
0
{
103
0
    EVP_PKEY *pkey;
104
0
    int ret;
105
0
106
0
    if (rsa == NULL) {
107
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
108
0
        return 0;
109
0
    }
110
0
    if ((pkey = EVP_PKEY_new()) == NULL) {
111
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
112
0
        return 0;
113
0
    }
114
0
115
0
    RSA_up_ref(rsa);
116
0
    if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
117
0
        RSA_free(rsa);
118
0
        EVP_PKEY_free(pkey);
119
0
        return 0;
120
0
    }
121
0
122
0
    ret = ssl_set_pkey(ssl->cert, pkey);
123
0
    EVP_PKEY_free(pkey);
124
0
    return ret;
125
0
}
126
#endif
127
128
static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
129
0
{
130
0
    size_t i;
131
0
132
0
    if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
133
0
        SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
134
0
        return 0;
135
0
    }
136
0
137
0
    if (c->pkeys[i].x509 != NULL) {
138
0
        EVP_PKEY *pktmp;
139
0
        pktmp = X509_get0_pubkey(c->pkeys[i].x509);
140
0
        if (pktmp == NULL) {
141
0
            SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
142
0
            return 0;
143
0
        }
144
0
        /*
145
0
         * The return code from EVP_PKEY_copy_parameters is deliberately
146
0
         * ignored. Some EVP_PKEY types cannot do this.
147
0
         */
148
0
        EVP_PKEY_copy_parameters(pktmp, pkey);
149
0
        ERR_clear_error();
150
0
151
0
#ifndef OPENSSL_NO_RSA
152
0
        /*
153
0
         * Don't check the public/private key, this is mostly for smart
154
0
         * cards.
155
0
         */
156
0
        if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
157
0
            && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
158
0
        else
159
0
#endif
160
0
        if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
161
0
            X509_free(c->pkeys[i].x509);
162
0
            c->pkeys[i].x509 = NULL;
163
0
            return 0;
164
0
        }
165
0
    }
166
0
167
0
    EVP_PKEY_free(c->pkeys[i].privatekey);
168
0
    EVP_PKEY_up_ref(pkey);
169
0
    c->pkeys[i].privatekey = pkey;
170
0
    c->key = &c->pkeys[i];
171
0
    return 1;
172
0
}
173
174
#ifndef OPENSSL_NO_RSA
175
int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
176
0
{
177
0
    int j, ret = 0;
178
0
    BIO *in;
179
0
    RSA *rsa = NULL;
180
0
181
0
    in = BIO_new(BIO_s_file());
182
0
    if (in == NULL) {
183
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
184
0
        goto end;
185
0
    }
186
0
187
0
    if (BIO_read_filename(in, file) <= 0) {
188
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
189
0
        goto end;
190
0
    }
191
0
    if (type == SSL_FILETYPE_ASN1) {
192
0
        j = ERR_R_ASN1_LIB;
193
0
        rsa = d2i_RSAPrivateKey_bio(in, NULL);
194
0
    } else if (type == SSL_FILETYPE_PEM) {
195
0
        j = ERR_R_PEM_LIB;
196
0
        rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
197
0
                                         ssl->default_passwd_callback,
198
0
                                         ssl->default_passwd_callback_userdata);
199
0
    } else {
200
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
201
0
        goto end;
202
0
    }
203
0
    if (rsa == NULL) {
204
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
205
0
        goto end;
206
0
    }
207
0
    ret = SSL_use_RSAPrivateKey(ssl, rsa);
208
0
    RSA_free(rsa);
209
0
 end:
210
0
    BIO_free(in);
211
0
    return ret;
212
0
}
213
214
int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
215
0
{
216
0
    int ret;
217
0
    const unsigned char *p;
218
0
    RSA *rsa;
219
0
220
0
    p = d;
221
0
    if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
222
0
        SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
223
0
        return 0;
224
0
    }
225
0
226
0
    ret = SSL_use_RSAPrivateKey(ssl, rsa);
227
0
    RSA_free(rsa);
228
0
    return ret;
229
0
}
230
#endif                          /* !OPENSSL_NO_RSA */
231
232
int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
233
0
{
234
0
    int ret;
235
0
236
0
    if (pkey == NULL) {
237
0
        SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
238
0
        return 0;
239
0
    }
240
0
    ret = ssl_set_pkey(ssl->cert, pkey);
241
0
    return ret;
242
0
}
243
244
int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
245
0
{
246
0
    int j, ret = 0;
247
0
    BIO *in;
248
0
    EVP_PKEY *pkey = NULL;
249
0
250
0
    in = BIO_new(BIO_s_file());
251
0
    if (in == NULL) {
252
0
        SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
253
0
        goto end;
254
0
    }
255
0
256
0
    if (BIO_read_filename(in, file) <= 0) {
257
0
        SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
258
0
        goto end;
259
0
    }
260
0
    if (type == SSL_FILETYPE_PEM) {
261
0
        j = ERR_R_PEM_LIB;
262
0
        pkey = PEM_read_bio_PrivateKey(in, NULL,
263
0
                                       ssl->default_passwd_callback,
264
0
                                       ssl->default_passwd_callback_userdata);
265
0
    } else if (type == SSL_FILETYPE_ASN1) {
266
0
        j = ERR_R_ASN1_LIB;
267
0
        pkey = d2i_PrivateKey_bio(in, NULL);
268
0
    } else {
269
0
        SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
270
0
        goto end;
271
0
    }
272
0
    if (pkey == NULL) {
273
0
        SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
274
0
        goto end;
275
0
    }
276
0
    ret = SSL_use_PrivateKey(ssl, pkey);
277
0
    EVP_PKEY_free(pkey);
278
0
 end:
279
0
    BIO_free(in);
280
0
    return ret;
281
0
}
282
283
int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
284
                            long len)
285
0
{
286
0
    int ret;
287
0
    const unsigned char *p;
288
0
    EVP_PKEY *pkey;
289
0
290
0
    p = d;
291
0
    if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
292
0
        SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
293
0
        return 0;
294
0
    }
295
0
296
0
    ret = SSL_use_PrivateKey(ssl, pkey);
297
0
    EVP_PKEY_free(pkey);
298
0
    return ret;
299
0
}
300
301
int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
302
0
{
303
0
    int rv;
304
0
    if (x == NULL) {
305
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
306
0
        return 0;
307
0
    }
308
0
    rv = ssl_security_cert(NULL, ctx, x, 0, 1);
309
0
    if (rv != 1) {
310
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
311
0
        return 0;
312
0
    }
313
0
    return ssl_set_cert(ctx->cert, x);
314
0
}
315
316
static int ssl_set_cert(CERT *c, X509 *x)
317
0
{
318
0
    EVP_PKEY *pkey;
319
0
    size_t i;
320
0
321
0
    pkey = X509_get0_pubkey(x);
322
0
    if (pkey == NULL) {
323
0
        SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
324
0
        return 0;
325
0
    }
326
0
327
0
    if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
328
0
        SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
329
0
        return 0;
330
0
    }
331
0
#ifndef OPENSSL_NO_EC
332
0
    if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
333
0
        SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
334
0
        return 0;
335
0
    }
336
0
#endif
337
0
    if (c->pkeys[i].privatekey != NULL) {
338
0
        /*
339
0
         * The return code from EVP_PKEY_copy_parameters is deliberately
340
0
         * ignored. Some EVP_PKEY types cannot do this.
341
0
         */
342
0
        EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
343
0
        ERR_clear_error();
344
0
345
0
#ifndef OPENSSL_NO_RSA
346
0
        /*
347
0
         * Don't check the public/private key, this is mostly for smart
348
0
         * cards.
349
0
         */
350
0
        if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
351
0
            && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
352
0
            RSA_METHOD_FLAG_NO_CHECK) ;
353
0
        else
354
0
#endif                          /* OPENSSL_NO_RSA */
355
0
        if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
356
0
            /*
357
0
             * don't fail for a cert/key mismatch, just free current private
358
0
             * key (when switching to a different cert & key, first this
359
0
             * function should be used, then ssl_set_pkey
360
0
             */
361
0
            EVP_PKEY_free(c->pkeys[i].privatekey);
362
0
            c->pkeys[i].privatekey = NULL;
363
0
            /* clear error queue */
364
0
            ERR_clear_error();
365
0
        }
366
0
    }
367
0
368
0
    X509_free(c->pkeys[i].x509);
369
0
    X509_up_ref(x);
370
0
    c->pkeys[i].x509 = x;
371
0
    c->key = &(c->pkeys[i]);
372
0
373
0
    return 1;
374
0
}
375
376
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
377
0
{
378
0
    int j;
379
0
    BIO *in;
380
0
    int ret = 0;
381
0
    X509 *x = NULL;
382
0
383
0
    in = BIO_new(BIO_s_file());
384
0
    if (in == NULL) {
385
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
386
0
        goto end;
387
0
    }
388
0
389
0
    if (BIO_read_filename(in, file) <= 0) {
390
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
391
0
        goto end;
392
0
    }
393
0
    if (type == SSL_FILETYPE_ASN1) {
394
0
        j = ERR_R_ASN1_LIB;
395
0
        x = d2i_X509_bio(in, NULL);
396
0
    } else if (type == SSL_FILETYPE_PEM) {
397
0
        j = ERR_R_PEM_LIB;
398
0
        x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
399
0
                              ctx->default_passwd_callback_userdata);
400
0
    } else {
401
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
402
0
        goto end;
403
0
    }
404
0
405
0
    if (x == NULL) {
406
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
407
0
        goto end;
408
0
    }
409
0
410
0
    ret = SSL_CTX_use_certificate(ctx, x);
411
0
 end:
412
0
    X509_free(x);
413
0
    BIO_free(in);
414
0
    return ret;
415
0
}
416
417
int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
418
0
{
419
0
    X509 *x;
420
0
    int ret;
421
0
422
0
    x = d2i_X509(NULL, &d, (long)len);
423
0
    if (x == NULL) {
424
0
        SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
425
0
        return 0;
426
0
    }
427
0
428
0
    ret = SSL_CTX_use_certificate(ctx, x);
429
0
    X509_free(x);
430
0
    return ret;
431
0
}
432
433
#ifndef OPENSSL_NO_RSA
434
int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
435
0
{
436
0
    int ret;
437
0
    EVP_PKEY *pkey;
438
0
439
0
    if (rsa == NULL) {
440
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
441
0
        return 0;
442
0
    }
443
0
    if ((pkey = EVP_PKEY_new()) == NULL) {
444
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
445
0
        return 0;
446
0
    }
447
0
448
0
    RSA_up_ref(rsa);
449
0
    if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
450
0
        RSA_free(rsa);
451
0
        EVP_PKEY_free(pkey);
452
0
        return 0;
453
0
    }
454
0
455
0
    ret = ssl_set_pkey(ctx->cert, pkey);
456
0
    EVP_PKEY_free(pkey);
457
0
    return ret;
458
0
}
459
460
int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
461
0
{
462
0
    int j, ret = 0;
463
0
    BIO *in;
464
0
    RSA *rsa = NULL;
465
0
466
0
    in = BIO_new(BIO_s_file());
467
0
    if (in == NULL) {
468
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
469
0
        goto end;
470
0
    }
471
0
472
0
    if (BIO_read_filename(in, file) <= 0) {
473
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
474
0
        goto end;
475
0
    }
476
0
    if (type == SSL_FILETYPE_ASN1) {
477
0
        j = ERR_R_ASN1_LIB;
478
0
        rsa = d2i_RSAPrivateKey_bio(in, NULL);
479
0
    } else if (type == SSL_FILETYPE_PEM) {
480
0
        j = ERR_R_PEM_LIB;
481
0
        rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
482
0
                                         ctx->default_passwd_callback,
483
0
                                         ctx->default_passwd_callback_userdata);
484
0
    } else {
485
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
486
0
        goto end;
487
0
    }
488
0
    if (rsa == NULL) {
489
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
490
0
        goto end;
491
0
    }
492
0
    ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
493
0
    RSA_free(rsa);
494
0
 end:
495
0
    BIO_free(in);
496
0
    return ret;
497
0
}
498
499
int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
500
                                   long len)
501
0
{
502
0
    int ret;
503
0
    const unsigned char *p;
504
0
    RSA *rsa;
505
0
506
0
    p = d;
507
0
    if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
508
0
        SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
509
0
        return 0;
510
0
    }
511
0
512
0
    ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
513
0
    RSA_free(rsa);
514
0
    return ret;
515
0
}
516
#endif                          /* !OPENSSL_NO_RSA */
517
518
int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
519
0
{
520
0
    if (pkey == NULL) {
521
0
        SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
522
0
        return 0;
523
0
    }
524
0
    return ssl_set_pkey(ctx->cert, pkey);
525
0
}
526
527
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
528
0
{
529
0
    int j, ret = 0;
530
0
    BIO *in;
531
0
    EVP_PKEY *pkey = NULL;
532
0
533
0
    in = BIO_new(BIO_s_file());
534
0
    if (in == NULL) {
535
0
        SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
536
0
        goto end;
537
0
    }
538
0
539
0
    if (BIO_read_filename(in, file) <= 0) {
540
0
        SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
541
0
        goto end;
542
0
    }
543
0
    if (type == SSL_FILETYPE_PEM) {
544
0
        j = ERR_R_PEM_LIB;
545
0
        pkey = PEM_read_bio_PrivateKey(in, NULL,
546
0
                                       ctx->default_passwd_callback,
547
0
                                       ctx->default_passwd_callback_userdata);
548
0
    } else if (type == SSL_FILETYPE_ASN1) {
549
0
        j = ERR_R_ASN1_LIB;
550
0
        pkey = d2i_PrivateKey_bio(in, NULL);
551
0
    } else {
552
0
        SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
553
0
        goto end;
554
0
    }
555
0
    if (pkey == NULL) {
556
0
        SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
557
0
        goto end;
558
0
    }
559
0
    ret = SSL_CTX_use_PrivateKey(ctx, pkey);
560
0
    EVP_PKEY_free(pkey);
561
0
 end:
562
0
    BIO_free(in);
563
0
    return ret;
564
0
}
565
566
int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
567
                                const unsigned char *d, long len)
568
0
{
569
0
    int ret;
570
0
    const unsigned char *p;
571
0
    EVP_PKEY *pkey;
572
0
573
0
    p = d;
574
0
    if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
575
0
        SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
576
0
        return 0;
577
0
    }
578
0
579
0
    ret = SSL_CTX_use_PrivateKey(ctx, pkey);
580
0
    EVP_PKEY_free(pkey);
581
0
    return ret;
582
0
}
583
584
/*
585
 * Read a file that contains our certificate in "PEM" format, possibly
586
 * followed by a sequence of CA certificates that should be sent to the peer
587
 * in the Certificate message.
588
 */
589
static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
590
0
{
591
0
    BIO *in;
592
0
    int ret = 0;
593
0
    X509 *x = NULL;
594
0
    pem_password_cb *passwd_callback;
595
0
    void *passwd_callback_userdata;
596
0
597
0
    ERR_clear_error();          /* clear error stack for
598
0
                                 * SSL_CTX_use_certificate() */
599
0
600
0
    if (ctx != NULL) {
601
0
        passwd_callback = ctx->default_passwd_callback;
602
0
        passwd_callback_userdata = ctx->default_passwd_callback_userdata;
603
0
    } else {
604
0
        passwd_callback = ssl->default_passwd_callback;
605
0
        passwd_callback_userdata = ssl->default_passwd_callback_userdata;
606
0
    }
607
0
608
0
    in = BIO_new(BIO_s_file());
609
0
    if (in == NULL) {
610
0
        SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
611
0
        goto end;
612
0
    }
613
0
614
0
    if (BIO_read_filename(in, file) <= 0) {
615
0
        SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
616
0
        goto end;
617
0
    }
618
0
619
0
    x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
620
0
                              passwd_callback_userdata);
621
0
    if (x == NULL) {
622
0
        SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
623
0
        goto end;
624
0
    }
625
0
626
0
    if (ctx)
627
0
        ret = SSL_CTX_use_certificate(ctx, x);
628
0
    else
629
0
        ret = SSL_use_certificate(ssl, x);
630
0
631
0
    if (ERR_peek_error() != 0)
632
0
        ret = 0;                /* Key/certificate mismatch doesn't imply
633
0
                                 * ret==0 ... */
634
0
    if (ret) {
635
0
        /*
636
0
         * If we could set up our certificate, now proceed to the CA
637
0
         * certificates.
638
0
         */
639
0
        X509 *ca;
640
0
        int r;
641
0
        unsigned long err;
642
0
643
0
        if (ctx)
644
0
            r = SSL_CTX_clear_chain_certs(ctx);
645
0
        else
646
0
            r = SSL_clear_chain_certs(ssl);
647
0
648
0
        if (r == 0) {
649
0
            ret = 0;
650
0
            goto end;
651
0
        }
652
0
653
0
        while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
654
0
                                       passwd_callback_userdata))
655
0
               != NULL) {
656
0
            if (ctx)
657
0
                r = SSL_CTX_add0_chain_cert(ctx, ca);
658
0
            else
659
0
                r = SSL_add0_chain_cert(ssl, ca);
660
0
            /*
661
0
             * Note that we must not free ca if it was successfully added to
662
0
             * the chain (while we must free the main certificate, since its
663
0
             * reference count is increased by SSL_CTX_use_certificate).
664
0
             */
665
0
            if (!r) {
666
0
                X509_free(ca);
667
0
                ret = 0;
668
0
                goto end;
669
0
            }
670
0
        }
671
0
        /* When the while loop ends, it's usually just EOF. */
672
0
        err = ERR_peek_last_error();
673
0
        if (ERR_GET_LIB(err) == ERR_LIB_PEM
674
0
            && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
675
0
            ERR_clear_error();
676
0
        else
677
0
            ret = 0;            /* some real error */
678
0
    }
679
0
680
0
 end:
681
0
    X509_free(x);
682
0
    BIO_free(in);
683
0
    return ret;
684
0
}
685
686
int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
687
0
{
688
0
    return use_certificate_chain_file(ctx, NULL, file);
689
0
}
690
691
int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
692
0
{
693
0
    return use_certificate_chain_file(NULL, ssl, file);
694
0
}
695
696
static int serverinfo_find_extension(const unsigned char *serverinfo,
697
                                     size_t serverinfo_length,
698
                                     unsigned int extension_type,
699
                                     const unsigned char **extension_data,
700
                                     size_t *extension_length)
701
0
{
702
0
    PACKET pkt, data;
703
0
704
0
    *extension_data = NULL;
705
0
    *extension_length = 0;
706
0
    if (serverinfo == NULL || serverinfo_length == 0)
707
0
        return -1;
708
0
709
0
    if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
710
0
        return -1;
711
0
712
0
    for (;;) {
713
0
        unsigned int type = 0;
714
0
        unsigned long context = 0;
715
0
716
0
        /* end of serverinfo */
717
0
        if (PACKET_remaining(&pkt) == 0)
718
0
            return 0;           /* Extension not found */
719
0
720
0
        if (!PACKET_get_net_4(&pkt, &context)
721
0
                || !PACKET_get_net_2(&pkt, &type)
722
0
                || !PACKET_get_length_prefixed_2(&pkt, &data))
723
0
            return -1;
724
0
725
0
        if (type == extension_type) {
726
0
            *extension_data = PACKET_data(&data);
727
0
            *extension_length = PACKET_remaining(&data);;
728
0
            return 1;           /* Success */
729
0
        }
730
0
    }
731
0
    /* Unreachable */
732
0
}
733
734
static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
735
                                     unsigned int context,
736
                                     const unsigned char *in,
737
                                     size_t inlen, X509 *x, size_t chainidx,
738
                                     int *al, void *arg)
739
0
{
740
0
741
0
    if (inlen != 0) {
742
0
        *al = SSL_AD_DECODE_ERROR;
743
0
        return 0;
744
0
    }
745
0
746
0
    return 1;
747
0
}
748
749
static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
750
                                   const unsigned char *in,
751
                                   size_t inlen, int *al, void *arg)
752
0
{
753
0
    return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
754
0
                                     arg);
755
0
}
756
757
static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
758
                                   unsigned int context,
759
                                   const unsigned char **out,
760
                                   size_t *outlen, X509 *x, size_t chainidx,
761
                                   int *al, void *arg)
762
0
{
763
0
    const unsigned char *serverinfo = NULL;
764
0
    size_t serverinfo_length = 0;
765
0
766
0
    /* We only support extensions for the first Certificate */
767
0
    if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
768
0
        return 0;
769
0
770
0
    /* Is there serverinfo data for the chosen server cert? */
771
0
    if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
772
0
                                        &serverinfo_length)) != 0) {
773
0
        /* Find the relevant extension from the serverinfo */
774
0
        int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
775
0
                                               ext_type, out, outlen);
776
0
        if (retval == -1) {
777
0
            *al = SSL_AD_INTERNAL_ERROR;
778
0
            return -1;          /* Error */
779
0
        }
780
0
        if (retval == 0)
781
0
            return 0;           /* No extension found, don't send extension */
782
0
        return 1;               /* Send extension */
783
0
    }
784
0
    return 0;                   /* No serverinfo data found, don't send
785
0
                                 * extension */
786
0
}
787
788
static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
789
                                 const unsigned char **out, size_t *outlen,
790
                                 int *al, void *arg)
791
0
{
792
0
    return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
793
0
                                   arg);
794
0
}
795
796
/*
797
 * With a NULL context, this function just checks that the serverinfo data
798
 * parses correctly.  With a non-NULL context, it registers callbacks for
799
 * the included extensions.
800
 */
801
static int serverinfo_process_buffer(unsigned int version,
802
                                     const unsigned char *serverinfo,
803
                                     size_t serverinfo_length, SSL_CTX *ctx)
804
0
{
805
0
    PACKET pkt;
806
0
807
0
    if (serverinfo == NULL || serverinfo_length == 0)
808
0
        return 0;
809
0
810
0
    if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
811
0
        return 0;
812
0
813
0
    if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
814
0
        return 0;
815
0
816
0
    while (PACKET_remaining(&pkt)) {
817
0
        unsigned long context = 0;
818
0
        unsigned int ext_type = 0;
819
0
        PACKET data;
820
0
821
0
        if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
822
0
                || !PACKET_get_net_2(&pkt, &ext_type)
823
0
                || !PACKET_get_length_prefixed_2(&pkt, &data))
824
0
            return 0;
825
0
826
0
        if (ctx == NULL)
827
0
            continue;
828
0
829
0
        /*
830
0
         * The old style custom extensions API could be set separately for
831
0
         * server/client, i.e. you could set one custom extension for a client,
832
0
         * and *for the same extension in the same SSL_CTX* you could set a
833
0
         * custom extension for the server as well. It seems quite weird to be
834
0
         * setting a custom extension for both client and server in a single
835
0
         * SSL_CTX - but theoretically possible. This isn't possible in the
836
0
         * new API. Therefore, if we have V1 serverinfo we use the old API. We
837
0
         * also use the old API even if we have V2 serverinfo but the context
838
0
         * looks like an old style <= TLSv1.2 extension.
839
0
         */
840
0
        if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
841
0
            if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
842
0
                                               serverinfo_srv_add_cb,
843
0
                                               NULL, NULL,
844
0
                                               serverinfo_srv_parse_cb,
845
0
                                               NULL))
846
0
                return 0;
847
0
        } else {
848
0
            if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
849
0
                                        serverinfoex_srv_add_cb,
850
0
                                        NULL, NULL,
851
0
                                        serverinfoex_srv_parse_cb,
852
0
                                        NULL))
853
0
                return 0;
854
0
        }
855
0
    }
856
0
857
0
    return 1;
858
0
}
859
860
int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
861
                              const unsigned char *serverinfo,
862
                              size_t serverinfo_length)
863
0
{
864
0
    unsigned char *new_serverinfo;
865
0
866
0
    if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
867
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
868
0
        return 0;
869
0
    }
870
0
    if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
871
0
                                   NULL)) {
872
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
873
0
        return 0;
874
0
    }
875
0
    if (ctx->cert->key == NULL) {
876
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
877
0
        return 0;
878
0
    }
879
0
    new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
880
0
                                     serverinfo_length);
881
0
    if (new_serverinfo == NULL) {
882
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
883
0
        return 0;
884
0
    }
885
0
    ctx->cert->key->serverinfo = new_serverinfo;
886
0
    memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
887
0
    ctx->cert->key->serverinfo_length = serverinfo_length;
888
0
889
0
    /*
890
0
     * Now that the serverinfo is validated and stored, go ahead and
891
0
     * register callbacks.
892
0
     */
893
0
    if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
894
0
                                   ctx)) {
895
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
896
0
        return 0;
897
0
    }
898
0
    return 1;
899
0
}
900
901
int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
902
                           size_t serverinfo_length)
903
0
{
904
0
    return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
905
0
                                     serverinfo_length);
906
0
}
907
908
int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
909
0
{
910
0
    unsigned char *serverinfo = NULL;
911
0
    unsigned char *tmp;
912
0
    size_t serverinfo_length = 0;
913
0
    unsigned char *extension = 0;
914
0
    long extension_length = 0;
915
0
    char *name = NULL;
916
0
    char *header = NULL;
917
0
    char namePrefix1[] = "SERVERINFO FOR ";
918
0
    char namePrefix2[] = "SERVERINFOV2 FOR ";
919
0
    int ret = 0;
920
0
    BIO *bin = NULL;
921
0
    size_t num_extensions = 0, contextoff = 0;
922
0
923
0
    if (ctx == NULL || file == NULL) {
924
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
925
0
        goto end;
926
0
    }
927
0
928
0
    bin = BIO_new(BIO_s_file());
929
0
    if (bin == NULL) {
930
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
931
0
        goto end;
932
0
    }
933
0
    if (BIO_read_filename(bin, file) <= 0) {
934
0
        SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
935
0
        goto end;
936
0
    }
937
0
938
0
    for (num_extensions = 0;; num_extensions++) {
939
0
        unsigned int version;
940
0
941
0
        if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
942
0
            == 0) {
943
0
            /*
944
0
             * There must be at least one extension in this file
945
0
             */
946
0
            if (num_extensions == 0) {
947
0
                SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
948
0
                       SSL_R_NO_PEM_EXTENSIONS);
949
0
                goto end;
950
0
            } else              /* End of file, we're done */
951
0
                break;
952
0
        }
953
0
        /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
954
0
        if (strlen(name) < strlen(namePrefix1)) {
955
0
            SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
956
0
            goto end;
957
0
        }
958
0
        if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
959
0
            version = SSL_SERVERINFOV1;
960
0
        } else {
961
0
            if (strlen(name) < strlen(namePrefix2)) {
962
0
                SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
963
0
                       SSL_R_PEM_NAME_TOO_SHORT);
964
0
                goto end;
965
0
            }
966
0
            if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
967
0
                SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
968
0
                       SSL_R_PEM_NAME_BAD_PREFIX);
969
0
                goto end;
970
0
            }
971
0
            version = SSL_SERVERINFOV2;
972
0
        }
973
0
        /*
974
0
         * Check that the decoded PEM data is plausible (valid length field)
975
0
         */
976
0
        if (version == SSL_SERVERINFOV1) {
977
0
            /* 4 byte header: 2 bytes type, 2 bytes len */
978
0
            if (extension_length < 4
979
0
                    || (extension[2] << 8) + extension[3]
980
0
                       != extension_length - 4) {
981
0
                SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
982
0
                goto end;
983
0
            }
984
0
            /*
985
0
             * File does not have a context value so we must take account of
986
0
             * this later.
987
0
             */
988
0
            contextoff = 4;
989
0
        } else {
990
0
            /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
991
0
            if (extension_length < 8
992
0
                    || (extension[6] << 8) + extension[7]
993
0
                       != extension_length - 8) {
994
0
                SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
995
0
                goto end;
996
0
            }
997
0
        }
998
0
        /* Append the decoded extension to the serverinfo buffer */
999
0
        tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1000
0
                                          + contextoff);
1001
0
        if (tmp == NULL) {
1002
0
            SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1003
0
            goto end;
1004
0
        }
1005
0
        serverinfo = tmp;
1006
0
        if (contextoff > 0) {
1007
0
            unsigned char *sinfo = serverinfo + serverinfo_length;
1008
0
1009
0
            /* We know this only uses the last 2 bytes */
1010
0
            sinfo[0] = 0;
1011
0
            sinfo[1] = 0;
1012
0
            sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1013
0
            sinfo[3] = SYNTHV1CONTEXT & 0xff;
1014
0
        }
1015
0
        memcpy(serverinfo + serverinfo_length + contextoff,
1016
0
               extension, extension_length);
1017
0
        serverinfo_length += extension_length + contextoff;
1018
0
1019
0
        OPENSSL_free(name);
1020
0
        name = NULL;
1021
0
        OPENSSL_free(header);
1022
0
        header = NULL;
1023
0
        OPENSSL_free(extension);
1024
0
        extension = NULL;
1025
0
    }
1026
0
1027
0
    ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1028
0
                                    serverinfo_length);
1029
0
 end:
1030
0
    /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1031
0
    OPENSSL_free(name);
1032
0
    OPENSSL_free(header);
1033
0
    OPENSSL_free(extension);
1034
0
    OPENSSL_free(serverinfo);
1035
0
    BIO_free(bin);
1036
0
    return ret;
1037
0
}
1038
1039
static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1040
                                STACK_OF(X509) *chain, int override)
1041
0
{
1042
0
    int ret = 0;
1043
0
    size_t i;
1044
0
    int j;
1045
0
    int rv;
1046
0
    CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
1047
0
    STACK_OF(X509) *dup_chain = NULL;
1048
0
    EVP_PKEY *pubkey = NULL;
1049
0
1050
0
    /* Do all security checks before anything else */
1051
0
    rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
1052
0
    if (rv != 1) {
1053
0
        SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1054
0
        goto out;
1055
0
    }
1056
0
    for (j = 0; j < sk_X509_num(chain); j++) {
1057
0
        rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
1058
0
        if (rv != 1) {
1059
0
            SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1060
0
            goto out;
1061
0
        }
1062
0
    }
1063
0
1064
0
    pubkey = X509_get_pubkey(x509); /* bumps reference */
1065
0
    if (pubkey == NULL)
1066
0
        goto out;
1067
0
    if (privatekey == NULL) {
1068
0
        privatekey = pubkey;
1069
0
    } else {
1070
0
        /* For RSA, which has no parameters, missing returns 0 */
1071
0
        if (EVP_PKEY_missing_parameters(privatekey)) {
1072
0
            if (EVP_PKEY_missing_parameters(pubkey)) {
1073
0
                /* nobody has parameters? - error */
1074
0
                SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
1075
0
                goto out;
1076
0
            } else {
1077
0
                /* copy to privatekey from pubkey */
1078
0
                EVP_PKEY_copy_parameters(privatekey, pubkey);
1079
0
            }
1080
0
        } else if (EVP_PKEY_missing_parameters(pubkey)) {
1081
0
            /* copy to pubkey from privatekey */
1082
0
            EVP_PKEY_copy_parameters(pubkey, privatekey);
1083
0
        } /* else both have parameters */
1084
0
1085
0
        /* Copied from ssl_set_cert/pkey */
1086
0
#ifndef OPENSSL_NO_RSA
1087
0
        if ((EVP_PKEY_id(privatekey) == EVP_PKEY_RSA) &&
1088
0
            ((RSA_flags(EVP_PKEY_get0_RSA(privatekey)) & RSA_METHOD_FLAG_NO_CHECK)))
1089
0
            /* no-op */ ;
1090
0
        else
1091
0
#endif
1092
0
        /* check that key <-> cert match */
1093
0
        if (EVP_PKEY_cmp(pubkey, privatekey) != 1) {
1094
0
            SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
1095
0
            goto out;
1096
0
        }
1097
0
    }
1098
0
    if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
1099
0
        SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1100
0
        goto out;
1101
0
    }
1102
0
1103
0
    if (!override && (c->pkeys[i].x509 != NULL
1104
0
                      || c->pkeys[i].privatekey != NULL
1105
0
                      || c->pkeys[i].chain != NULL)) {
1106
0
        /* No override, and something already there */
1107
0
        SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
1108
0
        goto out;
1109
0
    }
1110
0
1111
0
    if (chain != NULL) {
1112
0
        dup_chain = X509_chain_up_ref(chain);
1113
0
        if  (dup_chain == NULL) {
1114
0
            SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
1115
0
            goto out;
1116
0
        }
1117
0
    }
1118
0
1119
0
    sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1120
0
    c->pkeys[i].chain = dup_chain;
1121
0
1122
0
    X509_free(c->pkeys[i].x509);
1123
0
    X509_up_ref(x509);
1124
0
    c->pkeys[i].x509 = x509;
1125
0
1126
0
    EVP_PKEY_free(c->pkeys[i].privatekey);
1127
0
    EVP_PKEY_up_ref(privatekey);
1128
0
    c->pkeys[i].privatekey = privatekey;
1129
0
1130
0
    c->key = &(c->pkeys[i]);
1131
0
1132
0
    ret = 1;
1133
0
 out:
1134
0
    EVP_PKEY_free(pubkey);
1135
0
    return ret;
1136
0
}
1137
1138
int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1139
                         STACK_OF(X509) *chain, int override)
1140
0
{
1141
0
    return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1142
0
}
1143
1144
int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1145
                             STACK_OF(X509) *chain, int override)
1146
0
{
1147
0
    return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
1148
0
}