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 if (!ssl_cert_inst(&ssl
->cert
)) {
83 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
86 return (ssl_set_cert(ssl
->cert
, x
));
89 #ifndef OPENSSL_NO_STDIO
90 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
97 in
= BIO_new(BIO_s_file_internal());
99 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
103 if (BIO_read_filename(in
, file
) <= 0) {
104 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
107 if (type
== SSL_FILETYPE_ASN1
) {
109 x
= d2i_X509_bio(in
, NULL
);
110 } else if (type
== SSL_FILETYPE_PEM
) {
112 x
= PEM_read_bio_X509(in
, NULL
, ssl
->ctx
->default_passwd_callback
,
113 ssl
->ctx
->default_passwd_callback_userdata
);
115 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
120 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, j
);
124 ret
= SSL_use_certificate(ssl
, x
);
134 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
139 x
= d2i_X509(NULL
, &d
, (long)len
);
141 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
145 ret
= SSL_use_certificate(ssl
, x
);
150 #ifndef OPENSSL_NO_RSA
151 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
157 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
160 if (!ssl_cert_inst(&ssl
->cert
)) {
161 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
164 if ((pkey
= EVP_PKEY_new()) == NULL
) {
165 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
170 EVP_PKEY_assign_RSA(pkey
, rsa
);
172 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
178 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
182 * Special case for DH: check two DH certificate types for a match. This
183 * means for DH certificates we must set the certificate first.
185 if (pkey
->type
== EVP_PKEY_DH
) {
188 x
= c
->pkeys
[SSL_PKEY_DH_RSA
].x509
;
189 if (x
&& X509_check_private_key(x
, pkey
))
191 x
= c
->pkeys
[SSL_PKEY_DH_DSA
].x509
;
192 if (i
== -1 && x
&& X509_check_private_key(x
, pkey
))
196 i
= ssl_cert_type(NULL
, pkey
);
198 SSLerr(SSL_F_SSL_SET_PKEY
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
202 if (c
->pkeys
[i
].x509
!= NULL
) {
204 pktmp
= X509_get_pubkey(c
->pkeys
[i
].x509
);
205 EVP_PKEY_copy_parameters(pktmp
, pkey
);
206 EVP_PKEY_free(pktmp
);
209 #ifndef OPENSSL_NO_RSA
211 * Don't check the public/private key, this is mostly for smart
214 if ((pkey
->type
== EVP_PKEY_RSA
) &&
215 (RSA_flags(pkey
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
)) ;
218 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
219 X509_free(c
->pkeys
[i
].x509
);
220 c
->pkeys
[i
].x509
= NULL
;
225 if (c
->pkeys
[i
].privatekey
!= NULL
)
226 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
227 CRYPTO_add(&pkey
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
228 c
->pkeys
[i
].privatekey
= pkey
;
229 c
->key
= &(c
->pkeys
[i
]);
235 #ifndef OPENSSL_NO_RSA
236 # ifndef OPENSSL_NO_STDIO
237 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
243 in
= BIO_new(BIO_s_file_internal());
245 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
249 if (BIO_read_filename(in
, file
) <= 0) {
250 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
253 if (type
== SSL_FILETYPE_ASN1
) {
255 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
256 } else if (type
== SSL_FILETYPE_PEM
) {
258 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
259 ssl
->ctx
->default_passwd_callback
,
261 ctx
->default_passwd_callback_userdata
);
263 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
267 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, j
);
270 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
279 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, const unsigned char *d
, long len
)
282 const unsigned char *p
;
286 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
287 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
291 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
295 #endif /* !OPENSSL_NO_RSA */
297 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
302 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
305 if (!ssl_cert_inst(&ssl
->cert
)) {
306 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
309 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
313 #ifndef OPENSSL_NO_STDIO
314 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
318 EVP_PKEY
*pkey
= NULL
;
320 in
= BIO_new(BIO_s_file_internal());
322 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
326 if (BIO_read_filename(in
, file
) <= 0) {
327 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
330 if (type
== SSL_FILETYPE_PEM
) {
332 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
333 ssl
->ctx
->default_passwd_callback
,
335 ctx
->default_passwd_callback_userdata
);
336 } else if (type
== SSL_FILETYPE_ASN1
) {
338 pkey
= d2i_PrivateKey_bio(in
, NULL
);
340 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
344 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, j
);
347 ret
= SSL_use_PrivateKey(ssl
, pkey
);
356 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
,
360 const unsigned char *p
;
364 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
365 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
369 ret
= SSL_use_PrivateKey(ssl
, pkey
);
374 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
378 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
381 rv
= ssl_security_cert(NULL
, ctx
, x
, 0, 1);
383 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, rv
);
386 if (!ssl_cert_inst(&ctx
->cert
)) {
387 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
390 return (ssl_set_cert(ctx
->cert
, x
));
393 static int ssl_set_cert(CERT
*c
, X509
*x
)
398 pkey
= X509_get_pubkey(x
);
400 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_X509_LIB
);
404 i
= ssl_cert_type(x
, pkey
);
406 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
411 if (c
->pkeys
[i
].privatekey
!= NULL
) {
412 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
415 #ifndef OPENSSL_NO_RSA
417 * Don't check the public/private key, this is mostly for smart
420 if ((c
->pkeys
[i
].privatekey
->type
== EVP_PKEY_RSA
) &&
421 (RSA_flags(c
->pkeys
[i
].privatekey
->pkey
.rsa
) &
422 RSA_METHOD_FLAG_NO_CHECK
)) ;
424 #endif /* OPENSSL_NO_RSA */
425 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
427 * don't fail for a cert/key mismatch, just free current private
428 * key (when switching to a different cert & key, first this
429 * function should be used, then ssl_set_pkey
431 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
432 c
->pkeys
[i
].privatekey
= NULL
;
433 /* clear error queue */
440 if (c
->pkeys
[i
].x509
!= NULL
)
441 X509_free(c
->pkeys
[i
].x509
);
442 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
443 c
->pkeys
[i
].x509
= x
;
444 c
->key
= &(c
->pkeys
[i
]);
450 #ifndef OPENSSL_NO_STDIO
451 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
458 in
= BIO_new(BIO_s_file_internal());
460 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
464 if (BIO_read_filename(in
, file
) <= 0) {
465 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
468 if (type
== SSL_FILETYPE_ASN1
) {
470 x
= d2i_X509_bio(in
, NULL
);
471 } else if (type
== SSL_FILETYPE_PEM
) {
473 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
474 ctx
->default_passwd_callback_userdata
);
476 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
481 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, j
);
485 ret
= SSL_CTX_use_certificate(ctx
, x
);
495 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
,
496 const unsigned char *d
)
501 x
= d2i_X509(NULL
, &d
, (long)len
);
503 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
507 ret
= SSL_CTX_use_certificate(ctx
, x
);
512 #ifndef OPENSSL_NO_RSA
513 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
519 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
522 if (!ssl_cert_inst(&ctx
->cert
)) {
523 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
526 if ((pkey
= EVP_PKEY_new()) == NULL
) {
527 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
532 EVP_PKEY_assign_RSA(pkey
, rsa
);
534 ret
= ssl_set_pkey(ctx
->cert
, pkey
);
539 # ifndef OPENSSL_NO_STDIO
540 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
546 in
= BIO_new(BIO_s_file_internal());
548 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
552 if (BIO_read_filename(in
, file
) <= 0) {
553 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
556 if (type
== SSL_FILETYPE_ASN1
) {
558 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
559 } else if (type
== SSL_FILETYPE_PEM
) {
561 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
562 ctx
->default_passwd_callback
,
563 ctx
->default_passwd_callback_userdata
);
565 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
569 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, j
);
572 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
581 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
,
585 const unsigned char *p
;
589 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
590 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
594 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
598 #endif /* !OPENSSL_NO_RSA */
600 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
603 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
606 if (!ssl_cert_inst(&ctx
->cert
)) {
607 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
610 return (ssl_set_pkey(ctx
->cert
, pkey
));
613 #ifndef OPENSSL_NO_STDIO
614 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
618 EVP_PKEY
*pkey
= NULL
;
620 in
= BIO_new(BIO_s_file_internal());
622 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
626 if (BIO_read_filename(in
, file
) <= 0) {
627 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
630 if (type
== SSL_FILETYPE_PEM
) {
632 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
633 ctx
->default_passwd_callback
,
634 ctx
->default_passwd_callback_userdata
);
635 } else if (type
== SSL_FILETYPE_ASN1
) {
637 pkey
= d2i_PrivateKey_bio(in
, NULL
);
639 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
643 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, j
);
646 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
655 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
,
656 const unsigned char *d
, long len
)
659 const unsigned char *p
;
663 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
664 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
668 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
673 #ifndef OPENSSL_NO_STDIO
675 * Read a file that contains our certificate in "PEM" format, possibly
676 * followed by a sequence of CA certificates that should be sent to the peer
677 * in the Certificate message.
679 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
685 ERR_clear_error(); /* clear error stack for
686 * SSL_CTX_use_certificate() */
688 in
= BIO_new(BIO_s_file_internal());
690 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
694 if (BIO_read_filename(in
, file
) <= 0) {
695 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_SYS_LIB
);
699 x
= PEM_read_bio_X509_AUX(in
, NULL
, ctx
->default_passwd_callback
,
700 ctx
->default_passwd_callback_userdata
);
702 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_PEM_LIB
);
706 ret
= SSL_CTX_use_certificate(ctx
, x
);
708 if (ERR_peek_error() != 0)
709 ret
= 0; /* Key/certificate mismatch doesn't imply
713 * If we could set up our certificate, now proceed to the CA
720 SSL_CTX_clear_chain_certs(ctx
);
722 while ((ca
= PEM_read_bio_X509(in
, NULL
,
723 ctx
->default_passwd_callback
,
724 ctx
->default_passwd_callback_userdata
))
726 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
733 * Note that we must not free r if it was successfully added to
734 * the chain (while we must free the main certificate, since its
735 * reference count is increased by SSL_CTX_use_certificate).
738 /* When the while loop ends, it's usually just EOF. */
739 err
= ERR_peek_last_error();
740 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
741 && ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
744 ret
= 0; /* some real error */
756 #ifndef OPENSSL_NO_TLSEXT
757 static int serverinfo_find_extension(const unsigned char *serverinfo
,
758 size_t serverinfo_length
,
759 unsigned int extension_type
,
760 const unsigned char **extension_data
,
761 size_t *extension_length
)
763 *extension_data
= NULL
;
764 *extension_length
= 0;
765 if (serverinfo
== NULL
|| serverinfo_length
== 0)
768 unsigned int type
= 0;
771 /* end of serverinfo */
772 if (serverinfo_length
== 0)
773 return -1; /* Extension not found */
775 /* read 2-byte type field */
776 if (serverinfo_length
< 2)
777 return 0; /* Error */
778 type
= (serverinfo
[0] << 8) + serverinfo
[1];
780 serverinfo_length
-= 2;
782 /* read 2-byte len field */
783 if (serverinfo_length
< 2)
784 return 0; /* Error */
785 len
= (serverinfo
[0] << 8) + serverinfo
[1];
787 serverinfo_length
-= 2;
789 if (len
> serverinfo_length
)
790 return 0; /* Error */
792 if (type
== extension_type
) {
793 *extension_data
= serverinfo
;
794 *extension_length
= len
;
795 return 1; /* Success */
799 serverinfo_length
-= len
;
801 return 0; /* Error */
804 static int serverinfo_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
805 const unsigned char *in
,
806 size_t inlen
, int *al
, void *arg
)
810 *al
= SSL_AD_DECODE_ERROR
;
817 static int serverinfo_srv_add_cb(SSL
*s
, unsigned int ext_type
,
818 const unsigned char **out
, size_t *outlen
,
821 const unsigned char *serverinfo
= NULL
;
822 size_t serverinfo_length
= 0;
824 /* Is there serverinfo data for the chosen server cert? */
825 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
826 &serverinfo_length
)) != 0) {
827 /* Find the relevant extension from the serverinfo */
828 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
829 ext_type
, out
, outlen
);
831 return 0; /* Error */
833 return -1; /* No extension found, don't send extension */
834 return 1; /* Send extension */
836 return -1; /* No serverinfo data found, don't send
841 * With a NULL context, this function just checks that the serverinfo data
842 * parses correctly. With a non-NULL context, it registers callbacks for
843 * the included extensions.
845 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
846 size_t serverinfo_length
, SSL_CTX
*ctx
)
848 if (serverinfo
== NULL
|| serverinfo_length
== 0)
851 unsigned int ext_type
= 0;
854 /* end of serverinfo */
855 if (serverinfo_length
== 0)
858 /* read 2-byte type field */
859 if (serverinfo_length
< 2)
861 /* FIXME: check for types we understand explicitly? */
863 /* Register callbacks for extensions */
864 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
865 if (ctx
&& !SSL_CTX_add_server_custom_ext(ctx
, ext_type
,
866 serverinfo_srv_add_cb
,
868 serverinfo_srv_parse_cb
,
873 serverinfo_length
-= 2;
875 /* read 2-byte len field */
876 if (serverinfo_length
< 2)
878 len
= (serverinfo
[0] << 8) + serverinfo
[1];
880 serverinfo_length
-= 2;
882 if (len
> serverinfo_length
)
886 serverinfo_length
-= len
;
890 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
891 size_t serverinfo_length
)
893 unsigned char *new_serverinfo
;
895 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0) {
896 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_PASSED_NULL_PARAMETER
);
899 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
)) {
900 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
903 if (!ssl_cert_inst(&ctx
->cert
)) {
904 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
907 if (ctx
->cert
->key
== NULL
) {
908 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_INTERNAL_ERROR
);
911 new_serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
913 if (new_serverinfo
== NULL
) {
914 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
917 ctx
->cert
->key
->serverinfo
= new_serverinfo
;
918 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
919 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
922 * Now that the serverinfo is validated and stored, go ahead and
923 * register callbacks.
925 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
)) {
926 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
932 # ifndef OPENSSL_NO_STDIO
933 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
935 unsigned char *serverinfo
= NULL
;
936 size_t serverinfo_length
= 0;
937 unsigned char *extension
= 0;
938 long extension_length
= 0;
941 char namePrefix
[] = "SERVERINFO FOR ";
944 size_t num_extensions
= 0;
946 if (ctx
== NULL
|| file
== NULL
) {
947 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
948 ERR_R_PASSED_NULL_PARAMETER
);
952 bin
= BIO_new(BIO_s_file_internal());
954 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
957 if (BIO_read_filename(bin
, file
) <= 0) {
958 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
962 for (num_extensions
= 0;; num_extensions
++) {
963 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
)
966 * There must be at least one extension in this file
968 if (num_extensions
== 0) {
969 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
970 SSL_R_NO_PEM_EXTENSIONS
);
972 } else /* End of file, we're done */
975 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
976 if (strlen(name
) < strlen(namePrefix
)) {
977 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
978 SSL_R_PEM_NAME_TOO_SHORT
);
981 if (strncmp(name
, namePrefix
, strlen(namePrefix
)) != 0) {
982 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
983 SSL_R_PEM_NAME_BAD_PREFIX
);
987 * Check that the decoded PEM data is plausible (valid length field)
989 if (extension_length
< 4
990 || (extension
[2] << 8) + extension
[3] != extension_length
- 4) {
991 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_BAD_DATA
);
994 /* Append the decoded extension to the serverinfo buffer */
996 OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
997 if (serverinfo
== NULL
) {
998 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
1001 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
1002 serverinfo_length
+= extension_length
;
1006 OPENSSL_free(header
);
1008 OPENSSL_free(extension
);
1012 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
1014 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1016 OPENSSL_free(header
);
1017 OPENSSL_free(extension
);
1018 OPENSSL_free(serverinfo
);
1023 # endif /* OPENSSL_NO_STDIO */
1024 #endif /* OPENSSL_NO_TLSEXT */