Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / ssl / ssl_rsa.c
bloba9bb38d27a0286f79b00a59f5bbbcd2352111ed7
1 /* $OpenBSD: ssl_rsa.c,v 1.20 2015/02/06 01:37:11 reyk Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 #include <stdio.h>
61 #include "ssl_locl.h"
63 #include <openssl/bio.h>
64 #include <openssl/evp.h>
65 #include <openssl/objects.h>
66 #include <openssl/pem.h>
67 #include <openssl/x509.h>
69 static int ssl_set_cert(CERT *c, X509 *x509);
70 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
71 static int ssl_ctx_use_certificate_chain_bio(SSL_CTX *, BIO *);
73 int
74 SSL_use_certificate(SSL *ssl, X509 *x)
76 if (x == NULL) {
77 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
78 return (0);
80 if (!ssl_cert_inst(&ssl->cert)) {
81 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
82 return (0);
84 return (ssl_set_cert(ssl->cert, x));
87 int
88 SSL_use_certificate_file(SSL *ssl, const char *file, int type)
90 int j;
91 BIO *in;
92 int ret = 0;
93 X509 *x = NULL;
95 in = BIO_new(BIO_s_file_internal());
96 if (in == NULL) {
97 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
98 goto end;
101 if (BIO_read_filename(in, file) <= 0) {
102 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
103 goto end;
105 if (type == SSL_FILETYPE_ASN1) {
106 j = ERR_R_ASN1_LIB;
107 x = d2i_X509_bio(in, NULL);
108 } else if (type == SSL_FILETYPE_PEM) {
109 j = ERR_R_PEM_LIB;
110 x = PEM_read_bio_X509(in, NULL,
111 ssl->ctx->default_passwd_callback,
112 ssl->ctx->default_passwd_callback_userdata);
113 } else {
114 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
115 goto end;
118 if (x == NULL) {
119 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
120 goto end;
123 ret = SSL_use_certificate(ssl, x);
124 end:
125 X509_free(x);
126 BIO_free(in);
127 return (ret);
131 SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
133 X509 *x;
134 int ret;
136 x = d2i_X509(NULL, &d,(long)len);
137 if (x == NULL) {
138 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
139 return (0);
142 ret = SSL_use_certificate(ssl, x);
143 X509_free(x);
144 return (ret);
148 SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
150 EVP_PKEY *pkey;
151 int ret;
153 if (rsa == NULL) {
154 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
155 return (0);
157 if (!ssl_cert_inst(&ssl->cert)) {
158 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
159 return (0);
161 if ((pkey = EVP_PKEY_new()) == NULL) {
162 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
163 return (0);
166 RSA_up_ref(rsa);
167 EVP_PKEY_assign_RSA(pkey, rsa);
169 ret = ssl_set_pkey(ssl->cert, pkey);
170 EVP_PKEY_free(pkey);
171 return (ret);
174 static int
175 ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
177 int i;
179 i = ssl_cert_type(NULL, pkey);
180 if (i < 0) {
181 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
182 return (0);
185 if (c->pkeys[i].x509 != NULL) {
186 EVP_PKEY *pktmp;
187 pktmp = X509_get_pubkey(c->pkeys[i].x509);
188 EVP_PKEY_copy_parameters(pktmp, pkey);
189 EVP_PKEY_free(pktmp);
190 ERR_clear_error();
193 * Don't check the public/private key, this is mostly
194 * for smart cards.
196 if ((pkey->type == EVP_PKEY_RSA) &&
197 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
199 else
200 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
201 X509_free(c->pkeys[i].x509);
202 c->pkeys[i].x509 = NULL;
203 return 0;
207 EVP_PKEY_free(c->pkeys[i].privatekey);
208 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
209 c->pkeys[i].privatekey = pkey;
210 c->key = &(c->pkeys[i]);
212 c->valid = 0;
213 return (1);
217 SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
219 int j, ret = 0;
220 BIO *in;
221 RSA *rsa = NULL;
223 in = BIO_new(BIO_s_file_internal());
224 if (in == NULL) {
225 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
226 goto end;
229 if (BIO_read_filename(in, file) <= 0) {
230 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
231 goto end;
233 if (type == SSL_FILETYPE_ASN1) {
234 j = ERR_R_ASN1_LIB;
235 rsa = d2i_RSAPrivateKey_bio(in, NULL);
236 } else if (type == SSL_FILETYPE_PEM) {
237 j = ERR_R_PEM_LIB;
238 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
239 ssl->ctx->default_passwd_callback,
240 ssl->ctx->default_passwd_callback_userdata);
241 } else {
242 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
243 goto end;
245 if (rsa == NULL) {
246 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
247 goto end;
249 ret = SSL_use_RSAPrivateKey(ssl, rsa);
250 RSA_free(rsa);
251 end:
252 BIO_free(in);
253 return (ret);
257 SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
259 int ret;
260 const unsigned char *p;
261 RSA *rsa;
263 p = d;
264 if ((rsa = d2i_RSAPrivateKey(NULL, &p,(long)len)) == NULL) {
265 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
266 return (0);
269 ret = SSL_use_RSAPrivateKey(ssl, rsa);
270 RSA_free(rsa);
271 return (ret);
275 SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
277 int ret;
279 if (pkey == NULL) {
280 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
281 return (0);
283 if (!ssl_cert_inst(&ssl->cert)) {
284 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
285 return (0);
287 ret = ssl_set_pkey(ssl->cert, pkey);
288 return (ret);
292 SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
294 int j, ret = 0;
295 BIO *in;
296 EVP_PKEY *pkey = NULL;
298 in = BIO_new(BIO_s_file_internal());
299 if (in == NULL) {
300 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
301 goto end;
304 if (BIO_read_filename(in, file) <= 0) {
305 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
306 goto end;
308 if (type == SSL_FILETYPE_PEM) {
309 j = ERR_R_PEM_LIB;
310 pkey = PEM_read_bio_PrivateKey(in, NULL,
311 ssl->ctx->default_passwd_callback,
312 ssl->ctx->default_passwd_callback_userdata);
313 } else if (type == SSL_FILETYPE_ASN1) {
314 j = ERR_R_ASN1_LIB;
315 pkey = d2i_PrivateKey_bio(in, NULL);
316 } else {
317 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
318 goto end;
320 if (pkey == NULL) {
321 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
322 goto end;
324 ret = SSL_use_PrivateKey(ssl, pkey);
325 EVP_PKEY_free(pkey);
326 end:
327 BIO_free(in);
328 return (ret);
332 SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
334 int ret;
335 const unsigned char *p;
336 EVP_PKEY *pkey;
338 p = d;
339 if ((pkey = d2i_PrivateKey(type, NULL, &p,(long)len)) == NULL) {
340 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
341 return (0);
344 ret = SSL_use_PrivateKey(ssl, pkey);
345 EVP_PKEY_free(pkey);
346 return (ret);
350 SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
352 if (x == NULL) {
353 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
354 return (0);
356 if (!ssl_cert_inst(&ctx->cert)) {
357 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
358 return (0);
360 return (ssl_set_cert(ctx->cert, x));
363 static int
364 ssl_set_cert(CERT *c, X509 *x)
366 EVP_PKEY *pkey;
367 int i;
369 pkey = X509_get_pubkey(x);
370 if (pkey == NULL) {
371 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
372 return (0);
375 i = ssl_cert_type(x, pkey);
376 if (i < 0) {
377 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
378 EVP_PKEY_free(pkey);
379 return (0);
382 if (c->pkeys[i].privatekey != NULL) {
383 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
384 ERR_clear_error();
387 * Don't check the public/private key, this is mostly
388 * for smart cards.
390 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
391 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
392 RSA_METHOD_FLAG_NO_CHECK))
394 else
395 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
397 * don't fail for a cert/key mismatch, just free
398 * current private key (when switching to a different
399 * cert & key, first this function should be used,
400 * then ssl_set_pkey
402 EVP_PKEY_free(c->pkeys[i].privatekey);
403 c->pkeys[i].privatekey = NULL;
404 /* clear error queue */
405 ERR_clear_error();
409 EVP_PKEY_free(pkey);
411 X509_free(c->pkeys[i].x509);
412 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
413 c->pkeys[i].x509 = x;
414 c->key = &(c->pkeys[i]);
416 c->valid = 0;
417 return (1);
421 SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
423 int j;
424 BIO *in;
425 int ret = 0;
426 X509 *x = NULL;
428 in = BIO_new(BIO_s_file_internal());
429 if (in == NULL) {
430 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
431 goto end;
434 if (BIO_read_filename(in, file) <= 0) {
435 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
436 goto end;
438 if (type == SSL_FILETYPE_ASN1) {
439 j = ERR_R_ASN1_LIB;
440 x = d2i_X509_bio(in, NULL);
441 } else if (type == SSL_FILETYPE_PEM) {
442 j = ERR_R_PEM_LIB;
443 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
444 ctx->default_passwd_callback_userdata);
445 } else {
446 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
447 goto end;
450 if (x == NULL) {
451 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
452 goto end;
455 ret = SSL_CTX_use_certificate(ctx, x);
456 end:
457 X509_free(x);
458 BIO_free(in);
459 return (ret);
463 SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
465 X509 *x;
466 int ret;
468 x = d2i_X509(NULL, &d,(long)len);
469 if (x == NULL) {
470 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
471 return (0);
474 ret = SSL_CTX_use_certificate(ctx, x);
475 X509_free(x);
476 return (ret);
480 SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
482 int ret;
483 EVP_PKEY *pkey;
485 if (rsa == NULL) {
486 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
487 return (0);
489 if (!ssl_cert_inst(&ctx->cert)) {
490 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
491 return (0);
493 if ((pkey = EVP_PKEY_new()) == NULL) {
494 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
495 return (0);
498 RSA_up_ref(rsa);
499 EVP_PKEY_assign_RSA(pkey, rsa);
501 ret = ssl_set_pkey(ctx->cert, pkey);
502 EVP_PKEY_free(pkey);
503 return (ret);
507 SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
509 int j, ret = 0;
510 BIO *in;
511 RSA *rsa = NULL;
513 in = BIO_new(BIO_s_file_internal());
514 if (in == NULL) {
515 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
516 goto end;
519 if (BIO_read_filename(in, file) <= 0) {
520 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
521 goto end;
523 if (type == SSL_FILETYPE_ASN1) {
524 j = ERR_R_ASN1_LIB;
525 rsa = d2i_RSAPrivateKey_bio(in, NULL);
526 } else if (type == SSL_FILETYPE_PEM) {
527 j = ERR_R_PEM_LIB;
528 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
529 ctx->default_passwd_callback,
530 ctx->default_passwd_callback_userdata);
531 } else {
532 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
533 goto end;
535 if (rsa == NULL) {
536 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
537 goto end;
539 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
540 RSA_free(rsa);
541 end:
542 BIO_free(in);
543 return (ret);
547 SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
549 int ret;
550 const unsigned char *p;
551 RSA *rsa;
553 p = d;
554 if ((rsa = d2i_RSAPrivateKey(NULL, &p,(long)len)) == NULL) {
555 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
556 return (0);
559 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
560 RSA_free(rsa);
561 return (ret);
565 SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
567 if (pkey == NULL) {
568 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,
569 ERR_R_PASSED_NULL_PARAMETER);
570 return (0);
572 if (!ssl_cert_inst(&ctx->cert)) {
573 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
574 return (0);
576 return (ssl_set_pkey(ctx->cert, pkey));
580 SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
582 int j, ret = 0;
583 BIO *in;
584 EVP_PKEY *pkey = NULL;
586 in = BIO_new(BIO_s_file_internal());
587 if (in == NULL) {
588 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
589 goto end;
592 if (BIO_read_filename(in, file) <= 0) {
593 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
594 goto end;
596 if (type == SSL_FILETYPE_PEM) {
597 j = ERR_R_PEM_LIB;
598 pkey = PEM_read_bio_PrivateKey(in, NULL,
599 ctx->default_passwd_callback,
600 ctx->default_passwd_callback_userdata);
601 } else if (type == SSL_FILETYPE_ASN1) {
602 j = ERR_R_ASN1_LIB;
603 pkey = d2i_PrivateKey_bio(in, NULL);
604 } else {
605 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,
606 SSL_R_BAD_SSL_FILETYPE);
607 goto end;
609 if (pkey == NULL) {
610 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
611 goto end;
613 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
614 EVP_PKEY_free(pkey);
615 end:
616 BIO_free(in);
617 return (ret);
621 SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
622 long len)
624 int ret;
625 const unsigned char *p;
626 EVP_PKEY *pkey;
628 p = d;
629 if ((pkey = d2i_PrivateKey(type, NULL, &p,(long)len)) == NULL) {
630 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
631 return (0);
634 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
635 EVP_PKEY_free(pkey);
636 return (ret);
641 * Read a bio that contains our certificate in "PEM" format,
642 * possibly followed by a sequence of CA certificates that should be
643 * sent to the peer in the Certificate message.
645 static int
646 ssl_ctx_use_certificate_chain_bio(SSL_CTX *ctx, BIO *in)
648 int ret = 0;
649 X509 *x = NULL;
651 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
653 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
654 ctx->default_passwd_callback_userdata);
655 if (x == NULL) {
656 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
657 goto end;
660 ret = SSL_CTX_use_certificate(ctx, x);
662 if (ERR_peek_error() != 0)
663 ret = 0;
664 /* Key/certificate mismatch doesn't imply ret==0 ... */
665 if (ret) {
667 * If we could set up our certificate, now proceed to
668 * the CA certificates.
670 X509 *ca;
671 int r;
672 unsigned long err;
674 if (ctx->extra_certs != NULL) {
675 sk_X509_pop_free(ctx->extra_certs, X509_free);
676 ctx->extra_certs = NULL;
679 while ((ca = PEM_read_bio_X509(in, NULL,
680 ctx->default_passwd_callback,
681 ctx->default_passwd_callback_userdata)) != NULL) {
682 r = SSL_CTX_add_extra_chain_cert(ctx, ca);
683 if (!r) {
684 X509_free(ca);
685 ret = 0;
686 goto end;
689 * Note that we must not free r if it was successfully
690 * added to the chain (while we must free the main
691 * certificate, since its reference count is increased
692 * by SSL_CTX_use_certificate).
696 /* When the while loop ends, it's usually just EOF. */
697 err = ERR_peek_last_error();
698 if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
699 ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
700 ERR_clear_error();
701 else
702 ret = 0; /* some real error */
705 end:
706 X509_free(x);
707 return (ret);
711 SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
713 BIO *in;
714 int ret = 0;
716 in = BIO_new(BIO_s_file_internal());
717 if (in == NULL) {
718 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
719 goto end;
722 if (BIO_read_filename(in, file) <= 0) {
723 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
724 goto end;
727 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
729 end:
730 BIO_free(in);
731 return (ret);
735 SSL_CTX_use_certificate_chain_mem(SSL_CTX *ctx, void *buf, int len)
737 BIO *in;
738 int ret = 0;
740 in = BIO_new_mem_buf(buf, len);
741 if (in == NULL) {
742 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
743 goto end;
746 ret = ssl_ctx_use_certificate_chain_bio(ctx, in);
748 end:
749 BIO_free(in);
750 return (ret);