2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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
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
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.]
61 #include <openssl/bio.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
67 static int ssl_set_cert(CERT
*c
, X509
*x509
);
68 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
);
69 int SSL_use_certificate(SSL
*ssl
, X509
*x
)
73 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
76 rv
= ssl_security_cert(ssl
, NULL
, x
, 0, 1);
78 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, rv
);
82 return (ssl_set_cert(ssl
->cert
, x
));
85 #ifndef OPENSSL_NO_STDIO
86 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
93 in
= BIO_new(BIO_s_file());
95 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
99 if (BIO_read_filename(in
, file
) <= 0) {
100 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
103 if (type
== SSL_FILETYPE_ASN1
) {
105 x
= d2i_X509_bio(in
, NULL
);
106 } else if (type
== SSL_FILETYPE_PEM
) {
108 x
= PEM_read_bio_X509(in
, NULL
, ssl
->ctx
->default_passwd_callback
,
109 ssl
->ctx
->default_passwd_callback_userdata
);
111 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
116 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, j
);
120 ret
= SSL_use_certificate(ssl
, x
);
128 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
133 x
= d2i_X509(NULL
, &d
, (long)len
);
135 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
139 ret
= SSL_use_certificate(ssl
, x
);
144 #ifndef OPENSSL_NO_RSA
145 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
151 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
154 if ((pkey
= EVP_PKEY_new()) == NULL
) {
155 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
160 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
165 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
171 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
174 i
= ssl_cert_type(NULL
, pkey
);
176 SSLerr(SSL_F_SSL_SET_PKEY
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
180 if (c
->pkeys
[i
].x509
!= NULL
) {
182 pktmp
= X509_get0_pubkey(c
->pkeys
[i
].x509
);
184 SSLerr(SSL_F_SSL_SET_PKEY
, ERR_R_MALLOC_FAILURE
);
188 * The return code from EVP_PKEY_copy_parameters is deliberately
189 * ignored. Some EVP_PKEY types cannot do this.
191 EVP_PKEY_copy_parameters(pktmp
, pkey
);
194 #ifndef OPENSSL_NO_RSA
196 * Don't check the public/private key, this is mostly for smart
199 if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
200 && RSA_flags(EVP_PKEY_get0_RSA(pkey
)) & RSA_METHOD_FLAG_NO_CHECK
);
203 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
204 X509_free(c
->pkeys
[i
].x509
);
205 c
->pkeys
[i
].x509
= NULL
;
210 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
211 EVP_PKEY_up_ref(pkey
);
212 c
->pkeys
[i
].privatekey
= pkey
;
213 c
->key
= &(c
->pkeys
[i
]);
217 #ifndef OPENSSL_NO_RSA
218 # ifndef OPENSSL_NO_STDIO
219 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
225 in
= BIO_new(BIO_s_file());
227 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
231 if (BIO_read_filename(in
, file
) <= 0) {
232 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
235 if (type
== SSL_FILETYPE_ASN1
) {
237 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
238 } else if (type
== SSL_FILETYPE_PEM
) {
240 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
241 ssl
->ctx
->default_passwd_callback
,
243 ctx
->default_passwd_callback_userdata
);
245 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
249 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, j
);
252 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
260 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, const unsigned char *d
, long len
)
263 const unsigned char *p
;
267 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
268 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
272 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
276 #endif /* !OPENSSL_NO_RSA */
278 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
283 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
286 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
290 #ifndef OPENSSL_NO_STDIO
291 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
295 EVP_PKEY
*pkey
= NULL
;
297 in
= BIO_new(BIO_s_file());
299 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
303 if (BIO_read_filename(in
, file
) <= 0) {
304 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
307 if (type
== SSL_FILETYPE_PEM
) {
309 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
310 ssl
->ctx
->default_passwd_callback
,
312 ctx
->default_passwd_callback_userdata
);
313 } else if (type
== SSL_FILETYPE_ASN1
) {
315 pkey
= d2i_PrivateKey_bio(in
, NULL
);
317 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
321 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, j
);
324 ret
= SSL_use_PrivateKey(ssl
, pkey
);
332 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
,
336 const unsigned char *p
;
340 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
341 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
345 ret
= SSL_use_PrivateKey(ssl
, pkey
);
350 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
354 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
357 rv
= ssl_security_cert(NULL
, ctx
, x
, 0, 1);
359 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, rv
);
362 return (ssl_set_cert(ctx
->cert
, x
));
365 static int ssl_set_cert(CERT
*c
, X509
*x
)
370 pkey
= X509_get0_pubkey(x
);
372 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_X509_LIB
);
376 i
= ssl_cert_type(x
, pkey
);
378 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
382 if (c
->pkeys
[i
].privatekey
!= NULL
) {
384 * The return code from EVP_PKEY_copy_parameters is deliberately
385 * ignored. Some EVP_PKEY types cannot do this.
387 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
390 #ifndef OPENSSL_NO_RSA
392 * Don't check the public/private key, this is mostly for smart
395 if (EVP_PKEY_id(c
->pkeys
[i
].privatekey
) == EVP_PKEY_RSA
396 && RSA_flags(EVP_PKEY_get0_RSA(c
->pkeys
[i
].privatekey
)) &
397 RSA_METHOD_FLAG_NO_CHECK
) ;
399 #endif /* OPENSSL_NO_RSA */
400 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
402 * don't fail for a cert/key mismatch, just free current private
403 * key (when switching to a different cert & key, first this
404 * function should be used, then ssl_set_pkey
406 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
407 c
->pkeys
[i
].privatekey
= NULL
;
408 /* clear error queue */
413 X509_free(c
->pkeys
[i
].x509
);
415 c
->pkeys
[i
].x509
= x
;
416 c
->key
= &(c
->pkeys
[i
]);
421 #ifndef OPENSSL_NO_STDIO
422 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
429 in
= BIO_new(BIO_s_file());
431 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
435 if (BIO_read_filename(in
, file
) <= 0) {
436 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
439 if (type
== SSL_FILETYPE_ASN1
) {
441 x
= d2i_X509_bio(in
, NULL
);
442 } else if (type
== SSL_FILETYPE_PEM
) {
444 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
445 ctx
->default_passwd_callback_userdata
);
447 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
452 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, j
);
456 ret
= SSL_CTX_use_certificate(ctx
, x
);
464 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
,
465 const unsigned char *d
)
470 x
= d2i_X509(NULL
, &d
, (long)len
);
472 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
476 ret
= SSL_CTX_use_certificate(ctx
, x
);
481 #ifndef OPENSSL_NO_RSA
482 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
488 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
491 if ((pkey
= EVP_PKEY_new()) == NULL
) {
492 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
497 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
502 ret
= ssl_set_pkey(ctx
->cert
, pkey
);
507 # ifndef OPENSSL_NO_STDIO
508 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
514 in
= BIO_new(BIO_s_file());
516 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
520 if (BIO_read_filename(in
, file
) <= 0) {
521 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
524 if (type
== SSL_FILETYPE_ASN1
) {
526 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
527 } else if (type
== SSL_FILETYPE_PEM
) {
529 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
530 ctx
->default_passwd_callback
,
531 ctx
->default_passwd_callback_userdata
);
533 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
537 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, j
);
540 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
548 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
,
552 const unsigned char *p
;
556 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
557 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
561 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
565 #endif /* !OPENSSL_NO_RSA */
567 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
570 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
573 return (ssl_set_pkey(ctx
->cert
, pkey
));
576 #ifndef OPENSSL_NO_STDIO
577 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
581 EVP_PKEY
*pkey
= NULL
;
583 in
= BIO_new(BIO_s_file());
585 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
589 if (BIO_read_filename(in
, file
) <= 0) {
590 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
593 if (type
== SSL_FILETYPE_PEM
) {
595 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
596 ctx
->default_passwd_callback
,
597 ctx
->default_passwd_callback_userdata
);
598 } else if (type
== SSL_FILETYPE_ASN1
) {
600 pkey
= d2i_PrivateKey_bio(in
, NULL
);
602 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
606 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, j
);
609 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
617 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
,
618 const unsigned char *d
, long len
)
621 const unsigned char *p
;
625 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
626 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
630 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
635 #ifndef OPENSSL_NO_STDIO
637 * Read a file that contains our certificate in "PEM" format, possibly
638 * followed by a sequence of CA certificates that should be sent to the peer
639 * in the Certificate message.
641 static int use_certificate_chain_file(SSL_CTX
*ctx
, SSL
*ssl
, const char *file
)
646 pem_password_cb
*passwd_callback
;
647 void *passwd_callback_userdata
;
649 ERR_clear_error(); /* clear error stack for
650 * SSL_CTX_use_certificate() */
653 passwd_callback
= ctx
->default_passwd_callback
;
654 passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
656 passwd_callback
= ssl
->default_passwd_callback
;
657 passwd_callback_userdata
= ssl
->default_passwd_callback_userdata
;
660 in
= BIO_new(BIO_s_file());
662 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
666 if (BIO_read_filename(in
, file
) <= 0) {
667 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_SYS_LIB
);
671 x
= PEM_read_bio_X509_AUX(in
, NULL
, passwd_callback
,
672 passwd_callback_userdata
);
674 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_PEM_LIB
);
679 ret
= SSL_CTX_use_certificate(ctx
, x
);
681 ret
= SSL_use_certificate(ssl
, x
);
683 if (ERR_peek_error() != 0)
684 ret
= 0; /* Key/certificate mismatch doesn't imply
688 * If we could set up our certificate, now proceed to the CA
696 r
= SSL_CTX_clear_chain_certs(ctx
);
698 r
= SSL_clear_chain_certs(ssl
);
705 while ((ca
= PEM_read_bio_X509(in
, NULL
, passwd_callback
,
706 passwd_callback_userdata
))
709 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
711 r
= SSL_add0_chain_cert(ssl
, ca
);
713 * Note that we must not free ca if it was successfully added to
714 * the chain (while we must free the main certificate, since its
715 * reference count is increased by SSL_CTX_use_certificate).
723 /* When the while loop ends, it's usually just EOF. */
724 err
= ERR_peek_last_error();
725 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
726 && ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
729 ret
= 0; /* some real error */
738 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
740 return use_certificate_chain_file(ctx
, NULL
, file
);
743 int SSL_use_certificate_chain_file(SSL
*ssl
, const char *file
)
745 return use_certificate_chain_file(NULL
, ssl
, file
);
749 static int serverinfo_find_extension(const unsigned char *serverinfo
,
750 size_t serverinfo_length
,
751 unsigned int extension_type
,
752 const unsigned char **extension_data
,
753 size_t *extension_length
)
755 *extension_data
= NULL
;
756 *extension_length
= 0;
757 if (serverinfo
== NULL
|| serverinfo_length
== 0)
760 unsigned int type
= 0;
763 /* end of serverinfo */
764 if (serverinfo_length
== 0)
765 return 0; /* Extension not found */
767 /* read 2-byte type field */
768 if (serverinfo_length
< 2)
769 return -1; /* Error */
770 type
= (serverinfo
[0] << 8) + serverinfo
[1];
772 serverinfo_length
-= 2;
774 /* read 2-byte len field */
775 if (serverinfo_length
< 2)
776 return -1; /* Error */
777 len
= (serverinfo
[0] << 8) + serverinfo
[1];
779 serverinfo_length
-= 2;
781 if (len
> serverinfo_length
)
782 return -1; /* Error */
784 if (type
== extension_type
) {
785 *extension_data
= serverinfo
;
786 *extension_length
= len
;
787 return 1; /* Success */
791 serverinfo_length
-= len
;
796 static int serverinfo_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
797 const unsigned char *in
,
798 size_t inlen
, int *al
, void *arg
)
802 *al
= SSL_AD_DECODE_ERROR
;
809 static int serverinfo_srv_add_cb(SSL
*s
, unsigned int ext_type
,
810 const unsigned char **out
, size_t *outlen
,
813 const unsigned char *serverinfo
= NULL
;
814 size_t serverinfo_length
= 0;
816 /* Is there serverinfo data for the chosen server cert? */
817 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
818 &serverinfo_length
)) != 0) {
819 /* Find the relevant extension from the serverinfo */
820 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
821 ext_type
, out
, outlen
);
823 *al
= SSL_AD_DECODE_ERROR
;
824 return -1; /* Error */
827 return 0; /* No extension found, don't send extension */
828 return 1; /* Send extension */
830 return -1; /* No serverinfo data found, don't send
835 * With a NULL context, this function just checks that the serverinfo data
836 * parses correctly. With a non-NULL context, it registers callbacks for
837 * the included extensions.
839 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
840 size_t serverinfo_length
, SSL_CTX
*ctx
)
842 if (serverinfo
== NULL
|| serverinfo_length
== 0)
845 unsigned int ext_type
= 0;
848 /* end of serverinfo */
849 if (serverinfo_length
== 0)
852 /* read 2-byte type field */
853 if (serverinfo_length
< 2)
855 /* FIXME: check for types we understand explicitly? */
857 /* Register callbacks for extensions */
858 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
859 if (ctx
&& !SSL_CTX_add_server_custom_ext(ctx
, ext_type
,
860 serverinfo_srv_add_cb
,
862 serverinfo_srv_parse_cb
,
867 serverinfo_length
-= 2;
869 /* read 2-byte len field */
870 if (serverinfo_length
< 2)
872 len
= (serverinfo
[0] << 8) + serverinfo
[1];
874 serverinfo_length
-= 2;
876 if (len
> serverinfo_length
)
880 serverinfo_length
-= len
;
884 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
885 size_t serverinfo_length
)
887 unsigned char *new_serverinfo
;
889 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0) {
890 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_PASSED_NULL_PARAMETER
);
893 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
)) {
894 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
897 if (ctx
->cert
->key
== NULL
) {
898 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_INTERNAL_ERROR
);
901 new_serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
903 if (new_serverinfo
== NULL
) {
904 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
907 ctx
->cert
->key
->serverinfo
= new_serverinfo
;
908 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
909 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
912 * Now that the serverinfo is validated and stored, go ahead and
913 * register callbacks.
915 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
)) {
916 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
922 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
924 unsigned char *serverinfo
= NULL
;
925 size_t serverinfo_length
= 0;
926 unsigned char *extension
= 0;
927 long extension_length
= 0;
930 char namePrefix
[] = "SERVERINFO FOR ";
933 size_t num_extensions
= 0;
935 if (ctx
== NULL
|| file
== NULL
) {
936 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
937 ERR_R_PASSED_NULL_PARAMETER
);
941 bin
= BIO_new(BIO_s_file());
943 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
946 if (BIO_read_filename(bin
, file
) <= 0) {
947 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
951 for (num_extensions
= 0;; num_extensions
++) {
952 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
)
955 * There must be at least one extension in this file
957 if (num_extensions
== 0) {
958 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
959 SSL_R_NO_PEM_EXTENSIONS
);
961 } else /* End of file, we're done */
964 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
965 if (strlen(name
) < strlen(namePrefix
)) {
966 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
967 SSL_R_PEM_NAME_TOO_SHORT
);
970 if (strncmp(name
, namePrefix
, strlen(namePrefix
)) != 0) {
971 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
972 SSL_R_PEM_NAME_BAD_PREFIX
);
976 * Check that the decoded PEM data is plausible (valid length field)
978 if (extension_length
< 4
979 || (extension
[2] << 8) + extension
[3] != extension_length
- 4) {
980 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_BAD_DATA
);
983 /* Append the decoded extension to the serverinfo buffer */
985 OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
986 if (serverinfo
== NULL
) {
987 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
990 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
991 serverinfo_length
+= extension_length
;
995 OPENSSL_free(header
);
997 OPENSSL_free(extension
);
1001 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
1003 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1005 OPENSSL_free(header
);
1006 OPENSSL_free(extension
);
1007 OPENSSL_free(serverinfo
);