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