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
)
72 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
75 if (!ssl_cert_inst(&ssl
->cert
)) {
76 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
79 return (ssl_set_cert(ssl
->cert
, x
));
82 #ifndef OPENSSL_NO_STDIO
83 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
90 in
= BIO_new(BIO_s_file_internal());
92 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
96 if (BIO_read_filename(in
, file
) <= 0) {
97 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
100 if (type
== SSL_FILETYPE_ASN1
) {
102 x
= d2i_X509_bio(in
, NULL
);
103 } else if (type
== SSL_FILETYPE_PEM
) {
105 x
= PEM_read_bio_X509(in
, NULL
, ssl
->ctx
->default_passwd_callback
,
106 ssl
->ctx
->default_passwd_callback_userdata
);
108 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
113 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, j
);
117 ret
= SSL_use_certificate(ssl
, x
);
127 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
132 x
= d2i_X509(NULL
, &d
, (long)len
);
134 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
138 ret
= SSL_use_certificate(ssl
, x
);
143 #ifndef OPENSSL_NO_RSA
144 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
150 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
153 if (!ssl_cert_inst(&ssl
->cert
)) {
154 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
157 if ((pkey
= EVP_PKEY_new()) == NULL
) {
158 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
163 EVP_PKEY_assign_RSA(pkey
, rsa
);
165 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
171 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
175 * Special case for DH: check two DH certificate types for a match. This
176 * means for DH certificates we must set the certificate first.
178 if (pkey
->type
== EVP_PKEY_DH
) {
181 x
= c
->pkeys
[SSL_PKEY_DH_RSA
].x509
;
182 if (x
&& X509_check_private_key(x
, pkey
))
184 x
= c
->pkeys
[SSL_PKEY_DH_DSA
].x509
;
185 if (i
== -1 && x
&& X509_check_private_key(x
, pkey
))
189 i
= ssl_cert_type(NULL
, pkey
);
191 SSLerr(SSL_F_SSL_SET_PKEY
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
195 if (c
->pkeys
[i
].x509
!= NULL
) {
197 pktmp
= X509_get_pubkey(c
->pkeys
[i
].x509
);
198 EVP_PKEY_copy_parameters(pktmp
, pkey
);
199 EVP_PKEY_free(pktmp
);
202 #ifndef OPENSSL_NO_RSA
204 * Don't check the public/private key, this is mostly for smart
207 if ((pkey
->type
== EVP_PKEY_RSA
) &&
208 (RSA_flags(pkey
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
)) ;
211 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
212 X509_free(c
->pkeys
[i
].x509
);
213 c
->pkeys
[i
].x509
= NULL
;
218 if (c
->pkeys
[i
].privatekey
!= NULL
)
219 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
220 CRYPTO_add(&pkey
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
221 c
->pkeys
[i
].privatekey
= pkey
;
222 c
->key
= &(c
->pkeys
[i
]);
228 #ifndef OPENSSL_NO_RSA
229 # ifndef OPENSSL_NO_STDIO
230 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
236 in
= BIO_new(BIO_s_file_internal());
238 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
242 if (BIO_read_filename(in
, file
) <= 0) {
243 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
246 if (type
== SSL_FILETYPE_ASN1
) {
248 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
249 } else if (type
== SSL_FILETYPE_PEM
) {
251 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
252 ssl
->ctx
->default_passwd_callback
,
254 ctx
->default_passwd_callback_userdata
);
256 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
260 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, j
);
263 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
272 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, unsigned char *d
, long len
)
275 const unsigned char *p
;
279 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
280 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
284 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
288 #endif /* !OPENSSL_NO_RSA */
290 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
295 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
298 if (!ssl_cert_inst(&ssl
->cert
)) {
299 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
302 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
306 #ifndef OPENSSL_NO_STDIO
307 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
311 EVP_PKEY
*pkey
= NULL
;
313 in
= BIO_new(BIO_s_file_internal());
315 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
319 if (BIO_read_filename(in
, file
) <= 0) {
320 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
323 if (type
== SSL_FILETYPE_PEM
) {
325 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
326 ssl
->ctx
->default_passwd_callback
,
328 ctx
->default_passwd_callback_userdata
);
329 } else if (type
== SSL_FILETYPE_ASN1
) {
331 pkey
= d2i_PrivateKey_bio(in
, NULL
);
333 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
337 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, j
);
340 ret
= SSL_use_PrivateKey(ssl
, pkey
);
349 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
,
353 const unsigned char *p
;
357 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
358 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
362 ret
= SSL_use_PrivateKey(ssl
, pkey
);
367 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
370 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
373 if (!ssl_cert_inst(&ctx
->cert
)) {
374 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
377 return (ssl_set_cert(ctx
->cert
, x
));
380 static int ssl_set_cert(CERT
*c
, X509
*x
)
385 pkey
= X509_get_pubkey(x
);
387 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_X509_LIB
);
391 i
= ssl_cert_type(x
, pkey
);
393 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
398 if (c
->pkeys
[i
].privatekey
!= NULL
) {
399 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
402 #ifndef OPENSSL_NO_RSA
404 * Don't check the public/private key, this is mostly for smart
407 if ((c
->pkeys
[i
].privatekey
->type
== EVP_PKEY_RSA
) &&
408 (RSA_flags(c
->pkeys
[i
].privatekey
->pkey
.rsa
) &
409 RSA_METHOD_FLAG_NO_CHECK
)) ;
411 #endif /* OPENSSL_NO_RSA */
412 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
414 * don't fail for a cert/key mismatch, just free current private
415 * key (when switching to a different cert & key, first this
416 * function should be used, then ssl_set_pkey
418 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
419 c
->pkeys
[i
].privatekey
= NULL
;
420 /* clear error queue */
427 if (c
->pkeys
[i
].x509
!= NULL
)
428 X509_free(c
->pkeys
[i
].x509
);
429 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
430 c
->pkeys
[i
].x509
= x
;
431 c
->key
= &(c
->pkeys
[i
]);
437 #ifndef OPENSSL_NO_STDIO
438 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
445 in
= BIO_new(BIO_s_file_internal());
447 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
451 if (BIO_read_filename(in
, file
) <= 0) {
452 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
455 if (type
== SSL_FILETYPE_ASN1
) {
457 x
= d2i_X509_bio(in
, NULL
);
458 } else if (type
== SSL_FILETYPE_PEM
) {
460 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
461 ctx
->default_passwd_callback_userdata
);
463 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
468 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, j
);
472 ret
= SSL_CTX_use_certificate(ctx
, x
);
482 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
,
483 const unsigned char *d
)
488 x
= d2i_X509(NULL
, &d
, (long)len
);
490 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
494 ret
= SSL_CTX_use_certificate(ctx
, x
);
499 #ifndef OPENSSL_NO_RSA
500 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
506 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
509 if (!ssl_cert_inst(&ctx
->cert
)) {
510 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
513 if ((pkey
= EVP_PKEY_new()) == NULL
) {
514 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
519 EVP_PKEY_assign_RSA(pkey
, rsa
);
521 ret
= ssl_set_pkey(ctx
->cert
, pkey
);
526 # ifndef OPENSSL_NO_STDIO
527 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
533 in
= BIO_new(BIO_s_file_internal());
535 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
539 if (BIO_read_filename(in
, file
) <= 0) {
540 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
543 if (type
== SSL_FILETYPE_ASN1
) {
545 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
546 } else if (type
== SSL_FILETYPE_PEM
) {
548 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
549 ctx
->default_passwd_callback
,
550 ctx
->default_passwd_callback_userdata
);
552 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
556 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, j
);
559 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
568 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
,
572 const unsigned char *p
;
576 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
577 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
581 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
585 #endif /* !OPENSSL_NO_RSA */
587 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
590 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
593 if (!ssl_cert_inst(&ctx
->cert
)) {
594 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
597 return (ssl_set_pkey(ctx
->cert
, pkey
));
600 #ifndef OPENSSL_NO_STDIO
601 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
605 EVP_PKEY
*pkey
= NULL
;
607 in
= BIO_new(BIO_s_file_internal());
609 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
613 if (BIO_read_filename(in
, file
) <= 0) {
614 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
617 if (type
== SSL_FILETYPE_PEM
) {
619 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
620 ctx
->default_passwd_callback
,
621 ctx
->default_passwd_callback_userdata
);
622 } else if (type
== SSL_FILETYPE_ASN1
) {
624 pkey
= d2i_PrivateKey_bio(in
, NULL
);
626 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
630 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, j
);
633 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
642 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
,
643 const unsigned char *d
, long len
)
646 const unsigned char *p
;
650 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
651 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
655 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
660 #ifndef OPENSSL_NO_STDIO
662 * Read a file that contains our certificate in "PEM" format, possibly
663 * followed by a sequence of CA certificates that should be sent to the peer
664 * in the Certificate message.
666 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
672 ERR_clear_error(); /* clear error stack for
673 * SSL_CTX_use_certificate() */
675 in
= BIO_new(BIO_s_file_internal());
677 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
681 if (BIO_read_filename(in
, file
) <= 0) {
682 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_SYS_LIB
);
686 x
= PEM_read_bio_X509_AUX(in
, NULL
, ctx
->default_passwd_callback
,
687 ctx
->default_passwd_callback_userdata
);
689 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_PEM_LIB
);
693 ret
= SSL_CTX_use_certificate(ctx
, x
);
695 if (ERR_peek_error() != 0)
696 ret
= 0; /* Key/certificate mismatch doesn't imply
700 * If we could set up our certificate, now proceed to the CA
707 SSL_CTX_clear_chain_certs(ctx
);
709 while ((ca
= PEM_read_bio_X509(in
, NULL
,
710 ctx
->default_passwd_callback
,
711 ctx
->default_passwd_callback_userdata
))
713 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
720 * Note that we must not free r if it was successfully added to
721 * the chain (while we must free the main certificate, since its
722 * reference count is increased by SSL_CTX_use_certificate).
725 /* When the while loop ends, it's usually just EOF. */
726 err
= ERR_peek_last_error();
727 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
728 && ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
731 ret
= 0; /* some real error */
743 #ifndef OPENSSL_NO_TLSEXT
744 static int serverinfo_find_extension(const unsigned char *serverinfo
,
745 size_t serverinfo_length
,
746 unsigned int extension_type
,
747 const unsigned char **extension_data
,
748 size_t *extension_length
)
750 *extension_data
= NULL
;
751 *extension_length
= 0;
752 if (serverinfo
== NULL
|| serverinfo_length
== 0)
755 unsigned int type
= 0;
758 /* end of serverinfo */
759 if (serverinfo_length
== 0)
760 return -1; /* Extension not found */
762 /* read 2-byte type field */
763 if (serverinfo_length
< 2)
764 return 0; /* Error */
765 type
= (serverinfo
[0] << 8) + serverinfo
[1];
767 serverinfo_length
-= 2;
769 /* read 2-byte len field */
770 if (serverinfo_length
< 2)
771 return 0; /* Error */
772 len
= (serverinfo
[0] << 8) + serverinfo
[1];
774 serverinfo_length
-= 2;
776 if (len
> serverinfo_length
)
777 return 0; /* Error */
779 if (type
== extension_type
) {
780 *extension_data
= serverinfo
;
781 *extension_length
= len
;
782 return 1; /* Success */
786 serverinfo_length
-= len
;
788 return 0; /* Error */
791 static int serverinfo_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
792 const unsigned char *in
,
793 size_t inlen
, int *al
, void *arg
)
797 *al
= SSL_AD_DECODE_ERROR
;
804 static int serverinfo_srv_add_cb(SSL
*s
, unsigned int ext_type
,
805 const unsigned char **out
, size_t *outlen
,
808 const unsigned char *serverinfo
= NULL
;
809 size_t serverinfo_length
= 0;
811 /* Is there serverinfo data for the chosen server cert? */
812 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
813 &serverinfo_length
)) != 0) {
814 /* Find the relevant extension from the serverinfo */
815 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
816 ext_type
, out
, outlen
);
818 return 0; /* Error */
820 return -1; /* No extension found, don't send extension */
821 return 1; /* Send extension */
823 return -1; /* No serverinfo data found, don't send
828 * With a NULL context, this function just checks that the serverinfo data
829 * parses correctly. With a non-NULL context, it registers callbacks for
830 * the included extensions.
832 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
833 size_t serverinfo_length
, SSL_CTX
*ctx
)
835 if (serverinfo
== NULL
|| serverinfo_length
== 0)
838 unsigned int ext_type
= 0;
841 /* end of serverinfo */
842 if (serverinfo_length
== 0)
845 /* read 2-byte type field */
846 if (serverinfo_length
< 2)
848 /* FIXME: check for types we understand explicitly? */
850 /* Register callbacks for extensions */
851 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
852 if (ctx
&& !SSL_CTX_add_server_custom_ext(ctx
, ext_type
,
853 serverinfo_srv_add_cb
,
855 serverinfo_srv_parse_cb
,
860 serverinfo_length
-= 2;
862 /* read 2-byte len field */
863 if (serverinfo_length
< 2)
865 len
= (serverinfo
[0] << 8) + serverinfo
[1];
867 serverinfo_length
-= 2;
869 if (len
> serverinfo_length
)
873 serverinfo_length
-= len
;
877 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
878 size_t serverinfo_length
)
880 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0) {
881 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_PASSED_NULL_PARAMETER
);
884 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
)) {
885 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
888 if (!ssl_cert_inst(&ctx
->cert
)) {
889 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
892 if (ctx
->cert
->key
== NULL
) {
893 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_INTERNAL_ERROR
);
896 ctx
->cert
->key
->serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
898 if (ctx
->cert
->key
->serverinfo
== NULL
) {
899 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
902 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
903 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
906 * Now that the serverinfo is validated and stored, go ahead and
907 * register callbacks.
909 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
)) {
910 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
916 # ifndef OPENSSL_NO_STDIO
917 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
919 unsigned char *serverinfo
= NULL
;
920 size_t serverinfo_length
= 0;
921 unsigned char *extension
= 0;
922 long extension_length
= 0;
925 char namePrefix
[] = "SERVERINFO FOR ";
928 size_t num_extensions
= 0;
930 if (ctx
== NULL
|| file
== NULL
) {
931 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
932 ERR_R_PASSED_NULL_PARAMETER
);
936 bin
= BIO_new(BIO_s_file_internal());
938 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
941 if (BIO_read_filename(bin
, file
) <= 0) {
942 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
946 for (num_extensions
= 0;; num_extensions
++) {
947 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
)
950 * There must be at least one extension in this file
952 if (num_extensions
== 0) {
953 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
954 SSL_R_NO_PEM_EXTENSIONS
);
956 } else /* End of file, we're done */
959 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
960 if (strlen(name
) < strlen(namePrefix
)) {
961 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
962 SSL_R_PEM_NAME_TOO_SHORT
);
965 if (strncmp(name
, namePrefix
, strlen(namePrefix
)) != 0) {
966 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
967 SSL_R_PEM_NAME_BAD_PREFIX
);
971 * Check that the decoded PEM data is plausible (valid length field)
973 if (extension_length
< 4
974 || (extension
[2] << 8) + extension
[3] != extension_length
- 4) {
975 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_BAD_DATA
);
978 /* Append the decoded extension to the serverinfo buffer */
980 OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
981 if (serverinfo
== NULL
) {
982 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
985 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
986 serverinfo_length
+= extension_length
;
990 OPENSSL_free(header
);
992 OPENSSL_free(extension
);
996 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
998 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1000 OPENSSL_free(header
);
1001 OPENSSL_free(extension
);
1002 OPENSSL_free(serverinfo
);
1007 # endif /* OPENSSL_NO_STDIO */
1008 #endif /* OPENSSL_NO_TLSEXT */