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 if (!ssl_cert_inst(&ssl
->cert
))
78 SSLerr(SSL_F_SSL_USE_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
81 return(ssl_set_cert(ssl
->cert
,x
));
84 #ifndef OPENSSL_NO_STDIO
85 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
92 in
=BIO_new(BIO_s_file_internal());
95 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,ERR_R_BUF_LIB
);
99 if (BIO_read_filename(in
,file
) <= 0)
101 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,ERR_R_SYS_LIB
);
104 if (type
== SSL_FILETYPE_ASN1
)
107 x
=d2i_X509_bio(in
,NULL
);
109 else if (type
== SSL_FILETYPE_PEM
)
112 x
=PEM_read_bio_X509(in
,NULL
,ssl
->ctx
->default_passwd_callback
,ssl
->ctx
->default_passwd_callback_userdata
);
116 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,SSL_R_BAD_SSL_FILETYPE
);
122 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,j
);
126 ret
=SSL_use_certificate(ssl
,x
);
128 if (x
!= NULL
) X509_free(x
);
129 if (in
!= NULL
) BIO_free(in
);
134 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
139 x
=d2i_X509(NULL
,&d
,(long)len
);
142 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
,ERR_R_ASN1_LIB
);
146 ret
=SSL_use_certificate(ssl
,x
);
151 #ifndef OPENSSL_NO_RSA
152 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
159 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
162 if (!ssl_cert_inst(&ssl
->cert
))
164 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
167 if ((pkey
=EVP_PKEY_new()) == NULL
)
169 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
,ERR_R_EVP_LIB
);
174 EVP_PKEY_assign_RSA(pkey
,rsa
);
176 ret
=ssl_set_pkey(ssl
->cert
,pkey
);
182 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
185 /* Special case for DH: check two DH certificate types for a match.
186 * This means for DH certificates we must set the certificate first.
188 if (pkey
->type
== EVP_PKEY_DH
)
192 x
= c
->pkeys
[SSL_PKEY_DH_RSA
].x509
;
193 if (x
&& X509_check_private_key(x
, pkey
))
195 x
= c
->pkeys
[SSL_PKEY_DH_DSA
].x509
;
196 if (i
== -1 && x
&& X509_check_private_key(x
, pkey
))
201 i
=ssl_cert_type(NULL
,pkey
);
204 SSLerr(SSL_F_SSL_SET_PKEY
,SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
208 if (c
->pkeys
[i
].x509
!= NULL
)
211 pktmp
= X509_get_pubkey(c
->pkeys
[i
].x509
);
212 EVP_PKEY_copy_parameters(pktmp
,pkey
);
213 EVP_PKEY_free(pktmp
);
216 #ifndef OPENSSL_NO_RSA
217 /* Don't check the public/private key, this is mostly
218 * for smart cards. */
219 if ((pkey
->type
== EVP_PKEY_RSA
) &&
220 (RSA_flags(pkey
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
))
224 if (!X509_check_private_key(c
->pkeys
[i
].x509
,pkey
))
226 X509_free(c
->pkeys
[i
].x509
);
227 c
->pkeys
[i
].x509
= NULL
;
232 if (c
->pkeys
[i
].privatekey
!= NULL
)
233 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
234 CRYPTO_add(&pkey
->references
,1,CRYPTO_LOCK_EVP_PKEY
);
235 c
->pkeys
[i
].privatekey
=pkey
;
236 c
->key
= &(c
->pkeys
[i
]);
242 #ifndef OPENSSL_NO_RSA
243 #ifndef OPENSSL_NO_STDIO
244 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
250 in
=BIO_new(BIO_s_file_internal());
253 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
257 if (BIO_read_filename(in
,file
) <= 0)
259 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
262 if (type
== SSL_FILETYPE_ASN1
)
265 rsa
=d2i_RSAPrivateKey_bio(in
,NULL
);
267 else if (type
== SSL_FILETYPE_PEM
)
270 rsa
=PEM_read_bio_RSAPrivateKey(in
,NULL
,
271 ssl
->ctx
->default_passwd_callback
,ssl
->ctx
->default_passwd_callback_userdata
);
275 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
280 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,j
);
283 ret
=SSL_use_RSAPrivateKey(ssl
,rsa
);
286 if (in
!= NULL
) BIO_free(in
);
291 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, unsigned char *d
, long len
)
294 const unsigned char *p
;
298 if ((rsa
=d2i_RSAPrivateKey(NULL
,&p
,(long)len
)) == NULL
)
300 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
304 ret
=SSL_use_RSAPrivateKey(ssl
,rsa
);
308 #endif /* !OPENSSL_NO_RSA */
310 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
316 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
319 if (!ssl_cert_inst(&ssl
->cert
))
321 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
324 ret
=ssl_set_pkey(ssl
->cert
,pkey
);
328 #ifndef OPENSSL_NO_STDIO
329 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
335 in
=BIO_new(BIO_s_file_internal());
338 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
342 if (BIO_read_filename(in
,file
) <= 0)
344 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
347 if (type
== SSL_FILETYPE_PEM
)
350 pkey
=PEM_read_bio_PrivateKey(in
,NULL
,
351 ssl
->ctx
->default_passwd_callback
,ssl
->ctx
->default_passwd_callback_userdata
);
353 else if (type
== SSL_FILETYPE_ASN1
)
356 pkey
= d2i_PrivateKey_bio(in
,NULL
);
360 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
365 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,j
);
368 ret
=SSL_use_PrivateKey(ssl
,pkey
);
371 if (in
!= NULL
) BIO_free(in
);
376 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
, long len
)
379 const unsigned char *p
;
383 if ((pkey
=d2i_PrivateKey(type
,NULL
,&p
,(long)len
)) == NULL
)
385 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
389 ret
=SSL_use_PrivateKey(ssl
,pkey
);
394 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
398 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
,ERR_R_PASSED_NULL_PARAMETER
);
401 if (!ssl_cert_inst(&ctx
->cert
))
403 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
406 return(ssl_set_cert(ctx
->cert
, x
));
409 static int ssl_set_cert(CERT
*c
, X509
*x
)
414 pkey
=X509_get_pubkey(x
);
417 SSLerr(SSL_F_SSL_SET_CERT
,SSL_R_X509_LIB
);
421 i
=ssl_cert_type(x
,pkey
);
424 SSLerr(SSL_F_SSL_SET_CERT
,SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
429 if (c
->pkeys
[i
].privatekey
!= NULL
)
431 EVP_PKEY_copy_parameters(pkey
,c
->pkeys
[i
].privatekey
);
434 #ifndef OPENSSL_NO_RSA
435 /* Don't check the public/private key, this is mostly
436 * for smart cards. */
437 if ((c
->pkeys
[i
].privatekey
->type
== EVP_PKEY_RSA
) &&
438 (RSA_flags(c
->pkeys
[i
].privatekey
->pkey
.rsa
) &
439 RSA_METHOD_FLAG_NO_CHECK
))
442 #endif /* OPENSSL_NO_RSA */
443 if (!X509_check_private_key(x
,c
->pkeys
[i
].privatekey
))
445 /* don't fail for a cert/key mismatch, just free
446 * current private key (when switching to a different
447 * cert & key, first this function should be used,
448 * then ssl_set_pkey */
449 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
450 c
->pkeys
[i
].privatekey
=NULL
;
451 /* clear error queue */
458 if (c
->pkeys
[i
].x509
!= NULL
)
459 X509_free(c
->pkeys
[i
].x509
);
460 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
462 #ifndef OPENSSL_NO_TLSEXT
463 /* Free the old serverinfo data, if it exists. */
464 if (c
->pkeys
[i
].serverinfo
!= NULL
)
466 OPENSSL_free(c
->pkeys
[i
].serverinfo
);
467 c
->pkeys
[i
].serverinfo
= NULL
;
468 c
->pkeys
[i
].serverinfo_length
= 0;
471 c
->key
= &(c
->pkeys
[i
]);
477 #ifndef OPENSSL_NO_STDIO
478 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
485 in
=BIO_new(BIO_s_file_internal());
488 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,ERR_R_BUF_LIB
);
492 if (BIO_read_filename(in
,file
) <= 0)
494 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,ERR_R_SYS_LIB
);
497 if (type
== SSL_FILETYPE_ASN1
)
500 x
=d2i_X509_bio(in
,NULL
);
502 else if (type
== SSL_FILETYPE_PEM
)
505 x
=PEM_read_bio_X509(in
,NULL
,ctx
->default_passwd_callback
,ctx
->default_passwd_callback_userdata
);
509 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,SSL_R_BAD_SSL_FILETYPE
);
515 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,j
);
519 ret
=SSL_CTX_use_certificate(ctx
,x
);
521 if (x
!= NULL
) X509_free(x
);
522 if (in
!= NULL
) BIO_free(in
);
527 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
, const unsigned char *d
)
532 x
=d2i_X509(NULL
,&d
,(long)len
);
535 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
,ERR_R_ASN1_LIB
);
539 ret
=SSL_CTX_use_certificate(ctx
,x
);
544 #ifndef OPENSSL_NO_RSA
545 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
552 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
555 if (!ssl_cert_inst(&ctx
->cert
))
557 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
560 if ((pkey
=EVP_PKEY_new()) == NULL
)
562 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
,ERR_R_EVP_LIB
);
567 EVP_PKEY_assign_RSA(pkey
,rsa
);
569 ret
=ssl_set_pkey(ctx
->cert
, pkey
);
574 #ifndef OPENSSL_NO_STDIO
575 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
581 in
=BIO_new(BIO_s_file_internal());
584 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
588 if (BIO_read_filename(in
,file
) <= 0)
590 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
593 if (type
== SSL_FILETYPE_ASN1
)
596 rsa
=d2i_RSAPrivateKey_bio(in
,NULL
);
598 else if (type
== SSL_FILETYPE_PEM
)
601 rsa
=PEM_read_bio_RSAPrivateKey(in
,NULL
,
602 ctx
->default_passwd_callback
,ctx
->default_passwd_callback_userdata
);
606 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
611 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,j
);
614 ret
=SSL_CTX_use_RSAPrivateKey(ctx
,rsa
);
617 if (in
!= NULL
) BIO_free(in
);
622 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
, long len
)
625 const unsigned char *p
;
629 if ((rsa
=d2i_RSAPrivateKey(NULL
,&p
,(long)len
)) == NULL
)
631 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
635 ret
=SSL_CTX_use_RSAPrivateKey(ctx
,rsa
);
639 #endif /* !OPENSSL_NO_RSA */
641 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
645 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
648 if (!ssl_cert_inst(&ctx
->cert
))
650 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
653 return(ssl_set_pkey(ctx
->cert
,pkey
));
656 #ifndef OPENSSL_NO_STDIO
657 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
663 in
=BIO_new(BIO_s_file_internal());
666 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
670 if (BIO_read_filename(in
,file
) <= 0)
672 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
675 if (type
== SSL_FILETYPE_PEM
)
678 pkey
=PEM_read_bio_PrivateKey(in
,NULL
,
679 ctx
->default_passwd_callback
,ctx
->default_passwd_callback_userdata
);
681 else if (type
== SSL_FILETYPE_ASN1
)
684 pkey
= d2i_PrivateKey_bio(in
,NULL
);
688 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
693 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,j
);
696 ret
=SSL_CTX_use_PrivateKey(ctx
,pkey
);
699 if (in
!= NULL
) BIO_free(in
);
704 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
, const unsigned char *d
,
708 const unsigned char *p
;
712 if ((pkey
=d2i_PrivateKey(type
,NULL
,&p
,(long)len
)) == NULL
)
714 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
718 ret
=SSL_CTX_use_PrivateKey(ctx
,pkey
);
724 #ifndef OPENSSL_NO_STDIO
725 /* Read a file that contains our certificate in "PEM" format,
726 * possibly followed by a sequence of CA certificates that should be
727 * sent to the peer in the Certificate message.
729 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
735 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
737 in
= BIO_new(BIO_s_file_internal());
740 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
,ERR_R_BUF_LIB
);
744 if (BIO_read_filename(in
,file
) <= 0)
746 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
,ERR_R_SYS_LIB
);
750 x
=PEM_read_bio_X509_AUX(in
,NULL
,ctx
->default_passwd_callback
,
751 ctx
->default_passwd_callback_userdata
);
754 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
,ERR_R_PEM_LIB
);
758 ret
= SSL_CTX_use_certificate(ctx
, x
);
760 if (ERR_peek_error() != 0)
761 ret
= 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
764 /* If we could set up our certificate, now proceed to
765 * the CA certificates.
771 SSL_CTX_clear_chain_certs(ctx
);
773 while ((ca
= PEM_read_bio_X509(in
, NULL
,
774 ctx
->default_passwd_callback
,
775 ctx
->default_passwd_callback_userdata
))
778 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
785 /* Note that we must not free r if it was successfully
786 * added to the chain (while we must free the main
787 * certificate, since its reference count is increased
788 * by SSL_CTX_use_certificate). */
790 /* When the while loop ends, it's usually just EOF. */
791 err
= ERR_peek_last_error();
792 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
&& ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
795 ret
= 0; /* some real error */
799 if (x
!= NULL
) X509_free(x
);
800 if (in
!= NULL
) BIO_free(in
);
805 #ifndef OPENSSL_NO_TLSEXT
806 static int serverinfo_find_extension(const unsigned char *serverinfo
,
807 size_t serverinfo_length
,
808 unsigned short extension_type
,
809 const unsigned char **extension_data
,
810 unsigned short *extension_length
)
812 *extension_data
= NULL
;
813 *extension_length
= 0;
814 if (serverinfo
== NULL
|| serverinfo_length
== 0)
818 unsigned short type
= 0; /* uint16 */
819 unsigned short len
= 0; /* uint16 */
821 /* end of serverinfo */
822 if (serverinfo_length
== 0)
823 return -1; /* Extension not found */
825 /* read 2-byte type field */
826 if (serverinfo_length
< 2)
827 return 0; /* Error */
828 type
= (serverinfo
[0] << 8) + serverinfo
[1];
830 serverinfo_length
-= 2;
832 /* read 2-byte len field */
833 if (serverinfo_length
< 2)
834 return 0; /* Error */
835 len
= (serverinfo
[0] << 8) + serverinfo
[1];
837 serverinfo_length
-= 2;
839 if (len
> serverinfo_length
)
840 return 0; /* Error */
842 if (type
== extension_type
)
844 *extension_data
= serverinfo
;
845 *extension_length
= len
;
846 return 1; /* Success */
850 serverinfo_length
-= len
;
852 return 0; /* Error */
855 static int serverinfo_srv_first_cb(SSL
*s
, unsigned short ext_type
,
856 const unsigned char *in
,
857 unsigned short inlen
, int *al
,
862 *al
= SSL_AD_DECODE_ERROR
;
868 static int serverinfo_srv_second_cb(SSL
*s
, unsigned short ext_type
,
869 const unsigned char **out
, unsigned short *outlen
,
872 const unsigned char *serverinfo
= NULL
;
873 size_t serverinfo_length
= 0;
875 /* Is there serverinfo data for the chosen server cert? */
876 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
877 &serverinfo_length
)) != 0)
879 /* Find the relevant extension from the serverinfo */
880 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
881 ext_type
, out
, outlen
);
883 return 0; /* Error */
885 return -1; /* No extension found, don't send extension */
886 return 1; /* Send extension */
888 return -1; /* No serverinfo data found, don't send extension */
891 /* With a NULL context, this function just checks that the serverinfo data
892 parses correctly. With a non-NULL context, it registers callbacks for
893 the included extensions. */
894 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
895 size_t serverinfo_length
, SSL_CTX
*ctx
)
897 if (serverinfo
== NULL
|| serverinfo_length
== 0)
901 unsigned short ext_type
= 0; /* uint16 */
902 unsigned short len
= 0; /* uint16 */
904 /* end of serverinfo */
905 if (serverinfo_length
== 0)
908 /* read 2-byte type field */
909 if (serverinfo_length
< 2)
911 /* FIXME: check for types we understand explicitly? */
913 /* Register callbacks for extensions */
914 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
915 if (ctx
&& !SSL_CTX_set_custom_srv_ext(ctx
, ext_type
,
916 serverinfo_srv_first_cb
,
917 serverinfo_srv_second_cb
, NULL
))
921 serverinfo_length
-= 2;
923 /* read 2-byte len field */
924 if (serverinfo_length
< 2)
926 len
= (serverinfo
[0] << 8) + serverinfo
[1];
928 serverinfo_length
-= 2;
930 if (len
> serverinfo_length
)
934 serverinfo_length
-= len
;
938 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
939 size_t serverinfo_length
)
941 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0)
943 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_PASSED_NULL_PARAMETER
);
946 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
))
948 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,SSL_R_INVALID_SERVERINFO_DATA
);
951 if (!ssl_cert_inst(&ctx
->cert
))
953 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_MALLOC_FAILURE
);
956 if (ctx
->cert
->key
== NULL
)
958 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_INTERNAL_ERROR
);
961 ctx
->cert
->key
->serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
963 if (ctx
->cert
->key
->serverinfo
== NULL
)
965 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_MALLOC_FAILURE
);
968 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
969 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
971 /* Now that the serverinfo is validated and stored, go ahead and
972 * register callbacks. */
973 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
))
975 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,SSL_R_INVALID_SERVERINFO_DATA
);
981 #ifndef OPENSSL_NO_STDIO
982 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
984 unsigned char *serverinfo
= NULL
;
985 size_t serverinfo_length
= 0;
986 unsigned char* extension
= 0;
987 long extension_length
= 0;
990 char namePrefix
[] = "SERVERINFO FOR ";
993 size_t num_extensions
= 0;
995 if (ctx
== NULL
|| file
== NULL
)
997 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,ERR_R_PASSED_NULL_PARAMETER
);
1001 bin
= BIO_new(BIO_s_file_internal());
1004 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
1007 if (BIO_read_filename(bin
, file
) <= 0)
1009 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
1013 for (num_extensions
=0;; num_extensions
++)
1015 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
) == 0)
1017 /* There must be at least one extension in this file */
1018 if (num_extensions
== 0)
1020 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_NO_PEM_EXTENSIONS
);
1023 else /* End of file, we're done */
1026 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
1027 if (strlen(name
) < strlen(namePrefix
))
1029 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_PEM_NAME_TOO_SHORT
);
1032 if (strncmp(name
, namePrefix
, strlen(namePrefix
)) != 0)
1034 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_PEM_NAME_BAD_PREFIX
);
1037 /* Check that the decoded PEM data is plausible (valid length field) */
1038 if (extension_length
< 4 || (extension
[2] << 8) + extension
[3] != extension_length
- 4)
1040 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_BAD_DATA
);
1043 /* Append the decoded extension to the serverinfo buffer */
1044 serverinfo
= OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
1045 if (serverinfo
== NULL
)
1047 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
1050 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
1051 serverinfo_length
+= extension_length
;
1053 OPENSSL_free(name
); name
= NULL
;
1054 OPENSSL_free(header
); header
= NULL
;
1055 OPENSSL_free(extension
); extension
= NULL
;
1058 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
1060 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1062 OPENSSL_free(header
);
1063 OPENSSL_free(extension
);
1064 OPENSSL_free(serverinfo
);
1069 #endif /* OPENSSL_NO_STDIO */
1070 #endif /* OPENSSL_NO_TLSEXT */