2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/bio.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
18 static int ssl_set_cert(CERT
*c
, X509
*x509
);
19 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
);
20 int SSL_use_certificate(SSL
*ssl
, X509
*x
)
24 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
27 rv
= ssl_security_cert(ssl
, NULL
, x
, 0, 1);
29 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, rv
);
33 return (ssl_set_cert(ssl
->cert
, x
));
36 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
43 in
= BIO_new(BIO_s_file());
45 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
49 if (BIO_read_filename(in
, file
) <= 0) {
50 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
53 if (type
== SSL_FILETYPE_ASN1
) {
55 x
= d2i_X509_bio(in
, NULL
);
56 } else if (type
== SSL_FILETYPE_PEM
) {
58 x
= PEM_read_bio_X509(in
, NULL
, ssl
->ctx
->default_passwd_callback
,
59 ssl
->ctx
->default_passwd_callback_userdata
);
61 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
66 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, j
);
70 ret
= SSL_use_certificate(ssl
, x
);
77 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
82 x
= d2i_X509(NULL
, &d
, (long)len
);
84 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
88 ret
= SSL_use_certificate(ssl
, x
);
93 #ifndef OPENSSL_NO_RSA
94 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
100 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
103 if ((pkey
= EVP_PKEY_new()) == NULL
) {
104 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
109 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
115 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
121 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
124 i
= ssl_cert_type(NULL
, pkey
);
126 SSLerr(SSL_F_SSL_SET_PKEY
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
130 if (c
->pkeys
[i
].x509
!= NULL
) {
132 pktmp
= X509_get0_pubkey(c
->pkeys
[i
].x509
);
134 SSLerr(SSL_F_SSL_SET_PKEY
, ERR_R_MALLOC_FAILURE
);
138 * The return code from EVP_PKEY_copy_parameters is deliberately
139 * ignored. Some EVP_PKEY types cannot do this.
141 EVP_PKEY_copy_parameters(pktmp
, pkey
);
144 #ifndef OPENSSL_NO_RSA
146 * Don't check the public/private key, this is mostly for smart
149 if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
150 && RSA_flags(EVP_PKEY_get0_RSA(pkey
)) & RSA_METHOD_FLAG_NO_CHECK
) ;
153 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
154 X509_free(c
->pkeys
[i
].x509
);
155 c
->pkeys
[i
].x509
= NULL
;
160 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
161 EVP_PKEY_up_ref(pkey
);
162 c
->pkeys
[i
].privatekey
= pkey
;
163 c
->key
= &(c
->pkeys
[i
]);
167 #ifndef OPENSSL_NO_RSA
168 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
174 in
= BIO_new(BIO_s_file());
176 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
180 if (BIO_read_filename(in
, file
) <= 0) {
181 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
184 if (type
== SSL_FILETYPE_ASN1
) {
186 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
187 } else if (type
== SSL_FILETYPE_PEM
) {
189 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
190 ssl
->ctx
->default_passwd_callback
,
192 ctx
->default_passwd_callback_userdata
);
194 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
198 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, j
);
201 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
208 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, const unsigned char *d
, long len
)
211 const unsigned char *p
;
215 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
216 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
220 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
224 #endif /* !OPENSSL_NO_RSA */
226 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
231 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
234 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
238 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
242 EVP_PKEY
*pkey
= NULL
;
244 in
= BIO_new(BIO_s_file());
246 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
250 if (BIO_read_filename(in
, file
) <= 0) {
251 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
254 if (type
== SSL_FILETYPE_PEM
) {
256 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
257 ssl
->ctx
->default_passwd_callback
,
259 ctx
->default_passwd_callback_userdata
);
260 } else if (type
== SSL_FILETYPE_ASN1
) {
262 pkey
= d2i_PrivateKey_bio(in
, NULL
);
264 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
268 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, j
);
271 ret
= SSL_use_PrivateKey(ssl
, pkey
);
278 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
,
282 const unsigned char *p
;
286 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
287 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
291 ret
= SSL_use_PrivateKey(ssl
, pkey
);
296 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
300 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
303 rv
= ssl_security_cert(NULL
, ctx
, x
, 0, 1);
305 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, rv
);
308 return (ssl_set_cert(ctx
->cert
, x
));
311 static int ssl_set_cert(CERT
*c
, X509
*x
)
316 pkey
= X509_get0_pubkey(x
);
318 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_X509_LIB
);
322 i
= ssl_cert_type(x
, pkey
);
324 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
327 #ifndef OPENSSL_NO_EC
328 if (i
== SSL_PKEY_ECC
&& !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey
))) {
329 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
333 if (c
->pkeys
[i
].privatekey
!= NULL
) {
335 * The return code from EVP_PKEY_copy_parameters is deliberately
336 * ignored. Some EVP_PKEY types cannot do this.
338 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
341 #ifndef OPENSSL_NO_RSA
343 * Don't check the public/private key, this is mostly for smart
346 if (EVP_PKEY_id(c
->pkeys
[i
].privatekey
) == EVP_PKEY_RSA
347 && RSA_flags(EVP_PKEY_get0_RSA(c
->pkeys
[i
].privatekey
)) &
348 RSA_METHOD_FLAG_NO_CHECK
) ;
350 #endif /* OPENSSL_NO_RSA */
351 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
353 * don't fail for a cert/key mismatch, just free current private
354 * key (when switching to a different cert & key, first this
355 * function should be used, then ssl_set_pkey
357 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
358 c
->pkeys
[i
].privatekey
= NULL
;
359 /* clear error queue */
364 X509_free(c
->pkeys
[i
].x509
);
366 c
->pkeys
[i
].x509
= x
;
367 c
->key
= &(c
->pkeys
[i
]);
372 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
379 in
= BIO_new(BIO_s_file());
381 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
385 if (BIO_read_filename(in
, file
) <= 0) {
386 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
389 if (type
== SSL_FILETYPE_ASN1
) {
391 x
= d2i_X509_bio(in
, NULL
);
392 } else if (type
== SSL_FILETYPE_PEM
) {
394 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
395 ctx
->default_passwd_callback_userdata
);
397 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
402 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, j
);
406 ret
= SSL_CTX_use_certificate(ctx
, x
);
413 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
, const unsigned char *d
)
418 x
= d2i_X509(NULL
, &d
, (long)len
);
420 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
424 ret
= SSL_CTX_use_certificate(ctx
, x
);
429 #ifndef OPENSSL_NO_RSA
430 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
436 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
439 if ((pkey
= EVP_PKEY_new()) == NULL
) {
440 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
445 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
451 ret
= ssl_set_pkey(ctx
->cert
, pkey
);
456 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
462 in
= BIO_new(BIO_s_file());
464 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
468 if (BIO_read_filename(in
, file
) <= 0) {
469 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
472 if (type
== SSL_FILETYPE_ASN1
) {
474 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
475 } else if (type
== SSL_FILETYPE_PEM
) {
477 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
478 ctx
->default_passwd_callback
,
479 ctx
->default_passwd_callback_userdata
);
481 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
485 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, j
);
488 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
495 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
,
499 const unsigned char *p
;
503 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
504 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
508 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
512 #endif /* !OPENSSL_NO_RSA */
514 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
517 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
520 return (ssl_set_pkey(ctx
->cert
, pkey
));
523 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
527 EVP_PKEY
*pkey
= NULL
;
529 in
= BIO_new(BIO_s_file());
531 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
535 if (BIO_read_filename(in
, file
) <= 0) {
536 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
539 if (type
== SSL_FILETYPE_PEM
) {
541 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
542 ctx
->default_passwd_callback
,
543 ctx
->default_passwd_callback_userdata
);
544 } else if (type
== SSL_FILETYPE_ASN1
) {
546 pkey
= d2i_PrivateKey_bio(in
, NULL
);
548 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
552 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, j
);
555 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
562 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
,
563 const unsigned char *d
, long len
)
566 const unsigned char *p
;
570 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
571 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
575 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
581 * Read a file that contains our certificate in "PEM" format, possibly
582 * followed by a sequence of CA certificates that should be sent to the peer
583 * in the Certificate message.
585 static int use_certificate_chain_file(SSL_CTX
*ctx
, SSL
*ssl
, const char *file
)
590 pem_password_cb
*passwd_callback
;
591 void *passwd_callback_userdata
;
593 ERR_clear_error(); /* clear error stack for
594 * SSL_CTX_use_certificate() */
597 passwd_callback
= ctx
->default_passwd_callback
;
598 passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
600 passwd_callback
= ssl
->default_passwd_callback
;
601 passwd_callback_userdata
= ssl
->default_passwd_callback_userdata
;
604 in
= BIO_new(BIO_s_file());
606 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
610 if (BIO_read_filename(in
, file
) <= 0) {
611 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_SYS_LIB
);
615 x
= PEM_read_bio_X509_AUX(in
, NULL
, passwd_callback
,
616 passwd_callback_userdata
);
618 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_PEM_LIB
);
623 ret
= SSL_CTX_use_certificate(ctx
, x
);
625 ret
= SSL_use_certificate(ssl
, x
);
627 if (ERR_peek_error() != 0)
628 ret
= 0; /* Key/certificate mismatch doesn't imply
632 * If we could set up our certificate, now proceed to the CA
640 r
= SSL_CTX_clear_chain_certs(ctx
);
642 r
= SSL_clear_chain_certs(ssl
);
649 while ((ca
= PEM_read_bio_X509(in
, NULL
, passwd_callback
,
650 passwd_callback_userdata
))
653 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
655 r
= SSL_add0_chain_cert(ssl
, ca
);
657 * Note that we must not free ca if it was successfully added to
658 * the chain (while we must free the main certificate, since its
659 * reference count is increased by SSL_CTX_use_certificate).
667 /* When the while loop ends, it's usually just EOF. */
668 err
= ERR_peek_last_error();
669 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
670 && ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
673 ret
= 0; /* some real error */
682 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
684 return use_certificate_chain_file(ctx
, NULL
, file
);
687 int SSL_use_certificate_chain_file(SSL
*ssl
, const char *file
)
689 return use_certificate_chain_file(NULL
, ssl
, file
);
692 static int serverinfo_find_extension(const unsigned char *serverinfo
,
693 size_t serverinfo_length
,
694 unsigned int extension_type
,
695 const unsigned char **extension_data
,
696 size_t *extension_length
)
698 *extension_data
= NULL
;
699 *extension_length
= 0;
700 if (serverinfo
== NULL
|| serverinfo_length
== 0)
703 unsigned int type
= 0;
706 /* end of serverinfo */
707 if (serverinfo_length
== 0)
708 return 0; /* Extension not found */
710 /* read 2-byte type field */
711 if (serverinfo_length
< 2)
712 return -1; /* Error */
713 type
= (serverinfo
[0] << 8) + serverinfo
[1];
715 serverinfo_length
-= 2;
717 /* read 2-byte len field */
718 if (serverinfo_length
< 2)
719 return -1; /* Error */
720 len
= (serverinfo
[0] << 8) + serverinfo
[1];
722 serverinfo_length
-= 2;
724 if (len
> serverinfo_length
)
725 return -1; /* Error */
727 if (type
== extension_type
) {
728 *extension_data
= serverinfo
;
729 *extension_length
= len
;
730 return 1; /* Success */
734 serverinfo_length
-= len
;
739 static int serverinfo_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
740 const unsigned char *in
,
741 size_t inlen
, int *al
, void *arg
)
745 *al
= SSL_AD_DECODE_ERROR
;
752 static int serverinfo_srv_add_cb(SSL
*s
, unsigned int ext_type
,
753 const unsigned char **out
, size_t *outlen
,
756 const unsigned char *serverinfo
= NULL
;
757 size_t serverinfo_length
= 0;
759 /* Is there serverinfo data for the chosen server cert? */
760 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
761 &serverinfo_length
)) != 0) {
762 /* Find the relevant extension from the serverinfo */
763 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
764 ext_type
, out
, outlen
);
766 *al
= SSL_AD_DECODE_ERROR
;
767 return -1; /* Error */
770 return 0; /* No extension found, don't send extension */
771 return 1; /* Send extension */
773 return 0; /* No serverinfo data found, don't send
778 * With a NULL context, this function just checks that the serverinfo data
779 * parses correctly. With a non-NULL context, it registers callbacks for
780 * the included extensions.
782 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
783 size_t serverinfo_length
, SSL_CTX
*ctx
)
785 if (serverinfo
== NULL
|| serverinfo_length
== 0)
788 unsigned int ext_type
= 0;
791 /* end of serverinfo */
792 if (serverinfo_length
== 0)
795 /* read 2-byte type field */
796 if (serverinfo_length
< 2)
798 /* FIXME: check for types we understand explicitly? */
800 /* Register callbacks for extensions */
801 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
803 int have_ext_cbs
= 0;
805 custom_ext_methods
*exts
= &ctx
->cert
->srv_ext
;
806 custom_ext_method
*meth
= exts
->meths
;
808 for (i
= 0; i
< exts
->meths_count
; i
++, meth
++) {
809 if (ext_type
== meth
->ext_type
) {
815 if (!have_ext_cbs
&& !SSL_CTX_add_server_custom_ext(ctx
, ext_type
,
816 serverinfo_srv_add_cb
,
818 serverinfo_srv_parse_cb
,
824 serverinfo_length
-= 2;
826 /* read 2-byte len field */
827 if (serverinfo_length
< 2)
829 len
= (serverinfo
[0] << 8) + serverinfo
[1];
831 serverinfo_length
-= 2;
833 if (len
> serverinfo_length
)
837 serverinfo_length
-= len
;
841 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
842 size_t serverinfo_length
)
844 unsigned char *new_serverinfo
;
846 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0) {
847 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_PASSED_NULL_PARAMETER
);
850 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
)) {
851 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
854 if (ctx
->cert
->key
== NULL
) {
855 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_INTERNAL_ERROR
);
858 new_serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
860 if (new_serverinfo
== NULL
) {
861 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
864 ctx
->cert
->key
->serverinfo
= new_serverinfo
;
865 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
866 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
869 * Now that the serverinfo is validated and stored, go ahead and
870 * register callbacks.
872 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
)) {
873 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
879 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
881 unsigned char *serverinfo
= NULL
;
883 size_t serverinfo_length
= 0;
884 unsigned char *extension
= 0;
885 long extension_length
= 0;
888 char namePrefix
[] = "SERVERINFO FOR ";
891 size_t num_extensions
= 0;
893 if (ctx
== NULL
|| file
== NULL
) {
894 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_PASSED_NULL_PARAMETER
);
898 bin
= BIO_new(BIO_s_file());
900 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
903 if (BIO_read_filename(bin
, file
) <= 0) {
904 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
908 for (num_extensions
= 0;; num_extensions
++) {
909 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
)
912 * There must be at least one extension in this file
914 if (num_extensions
== 0) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
916 SSL_R_NO_PEM_EXTENSIONS
);
918 } else /* End of file, we're done */
921 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
922 if (strlen(name
) < strlen(namePrefix
)) {
923 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_PEM_NAME_TOO_SHORT
);
926 if (strncmp(name
, namePrefix
, strlen(namePrefix
)) != 0) {
927 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
928 SSL_R_PEM_NAME_BAD_PREFIX
);
932 * Check that the decoded PEM data is plausible (valid length field)
934 if (extension_length
< 4
935 || (extension
[2] << 8) + extension
[3] != extension_length
- 4) {
936 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_BAD_DATA
);
939 /* Append the decoded extension to the serverinfo buffer */
940 tmp
= OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
942 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
946 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
947 serverinfo_length
+= extension_length
;
951 OPENSSL_free(header
);
953 OPENSSL_free(extension
);
957 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
959 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
961 OPENSSL_free(header
);
962 OPENSSL_free(extension
);
963 OPENSSL_free(serverinfo
);