1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
60 #include <openssl/bio.h>
61 #include <openssl/objects.h>
62 #include <openssl/evp.h>
63 #include <openssl/x509.h>
64 #include <openssl/pem.h>
66 static int ssl_set_cert(CERT
*c
, X509
*x509
);
67 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
);
68 int SSL_use_certificate(SSL
*ssl
, X509
*x
)
72 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
75 rv
= ssl_security_cert(ssl
, NULL
, x
, 0, 1);
77 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, rv
);
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());
94 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
98 if (BIO_read_filename(in
, file
) <= 0) {
99 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
102 if (type
== SSL_FILETYPE_ASN1
) {
104 x
= d2i_X509_bio(in
, NULL
);
105 } else if (type
== SSL_FILETYPE_PEM
) {
107 x
= PEM_read_bio_X509(in
, NULL
, ssl
->ctx
->default_passwd_callback
,
108 ssl
->ctx
->default_passwd_callback_userdata
);
110 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
115 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, j
);
119 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 ((pkey
= EVP_PKEY_new()) == NULL
) {
154 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
159 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
164 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
170 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
173 i
= ssl_cert_type(NULL
, pkey
);
175 SSLerr(SSL_F_SSL_SET_PKEY
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
179 if (c
->pkeys
[i
].x509
!= NULL
) {
181 pktmp
= X509_get0_pubkey(c
->pkeys
[i
].x509
);
183 SSLerr(SSL_F_SSL_SET_PKEY
, ERR_R_MALLOC_FAILURE
);
187 * The return code from EVP_PKEY_copy_parameters is deliberately
188 * ignored. Some EVP_PKEY types cannot do this.
190 EVP_PKEY_copy_parameters(pktmp
, pkey
);
193 #ifndef OPENSSL_NO_RSA
195 * Don't check the public/private key, this is mostly for smart
198 if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
199 && RSA_flags(EVP_PKEY_get0_RSA(pkey
)) & RSA_METHOD_FLAG_NO_CHECK
);
202 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
203 X509_free(c
->pkeys
[i
].x509
);
204 c
->pkeys
[i
].x509
= NULL
;
209 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
210 EVP_PKEY_up_ref(pkey
);
211 c
->pkeys
[i
].privatekey
= pkey
;
212 c
->key
= &(c
->pkeys
[i
]);
216 #ifndef OPENSSL_NO_RSA
217 # ifndef OPENSSL_NO_STDIO
218 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
224 in
= BIO_new(BIO_s_file());
226 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
230 if (BIO_read_filename(in
, file
) <= 0) {
231 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
234 if (type
== SSL_FILETYPE_ASN1
) {
236 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
237 } else if (type
== SSL_FILETYPE_PEM
) {
239 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
240 ssl
->ctx
->default_passwd_callback
,
242 ctx
->default_passwd_callback_userdata
);
244 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
248 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, j
);
251 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
259 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, const unsigned char *d
, long len
)
262 const unsigned char *p
;
266 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
267 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
271 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
275 #endif /* !OPENSSL_NO_RSA */
277 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
282 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
285 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
289 #ifndef OPENSSL_NO_STDIO
290 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
294 EVP_PKEY
*pkey
= NULL
;
296 in
= BIO_new(BIO_s_file());
298 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
302 if (BIO_read_filename(in
, file
) <= 0) {
303 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
306 if (type
== SSL_FILETYPE_PEM
) {
308 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
309 ssl
->ctx
->default_passwd_callback
,
311 ctx
->default_passwd_callback_userdata
);
312 } else if (type
== SSL_FILETYPE_ASN1
) {
314 pkey
= d2i_PrivateKey_bio(in
, NULL
);
316 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
320 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, j
);
323 ret
= SSL_use_PrivateKey(ssl
, pkey
);
331 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
,
335 const unsigned char *p
;
339 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
340 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
344 ret
= SSL_use_PrivateKey(ssl
, pkey
);
349 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
353 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
356 rv
= ssl_security_cert(NULL
, ctx
, x
, 0, 1);
358 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, rv
);
361 return (ssl_set_cert(ctx
->cert
, x
));
364 static int ssl_set_cert(CERT
*c
, X509
*x
)
369 pkey
= X509_get0_pubkey(x
);
371 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_X509_LIB
);
375 i
= ssl_cert_type(x
, pkey
);
377 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
380 #ifndef OPENSSL_NO_EC
381 if (i
== SSL_PKEY_ECC
&& !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey
))) {
382 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
386 if (c
->pkeys
[i
].privatekey
!= NULL
) {
388 * The return code from EVP_PKEY_copy_parameters is deliberately
389 * ignored. Some EVP_PKEY types cannot do this.
391 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
394 #ifndef OPENSSL_NO_RSA
396 * Don't check the public/private key, this is mostly for smart
399 if (EVP_PKEY_id(c
->pkeys
[i
].privatekey
) == EVP_PKEY_RSA
400 && RSA_flags(EVP_PKEY_get0_RSA(c
->pkeys
[i
].privatekey
)) &
401 RSA_METHOD_FLAG_NO_CHECK
) ;
403 #endif /* OPENSSL_NO_RSA */
404 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
406 * don't fail for a cert/key mismatch, just free current private
407 * key (when switching to a different cert & key, first this
408 * function should be used, then ssl_set_pkey
410 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
411 c
->pkeys
[i
].privatekey
= NULL
;
412 /* clear error queue */
417 X509_free(c
->pkeys
[i
].x509
);
419 c
->pkeys
[i
].x509
= x
;
420 c
->key
= &(c
->pkeys
[i
]);
425 #ifndef OPENSSL_NO_STDIO
426 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
433 in
= BIO_new(BIO_s_file());
435 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
439 if (BIO_read_filename(in
, file
) <= 0) {
440 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
443 if (type
== SSL_FILETYPE_ASN1
) {
445 x
= d2i_X509_bio(in
, NULL
);
446 } else if (type
== SSL_FILETYPE_PEM
) {
448 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
449 ctx
->default_passwd_callback_userdata
);
451 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
456 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, j
);
460 ret
= SSL_CTX_use_certificate(ctx
, x
);
468 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
,
469 const unsigned char *d
)
474 x
= d2i_X509(NULL
, &d
, (long)len
);
476 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
480 ret
= SSL_CTX_use_certificate(ctx
, x
);
485 #ifndef OPENSSL_NO_RSA
486 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
492 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
495 if ((pkey
= EVP_PKEY_new()) == NULL
) {
496 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
501 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
506 ret
= ssl_set_pkey(ctx
->cert
, pkey
);
511 # ifndef OPENSSL_NO_STDIO
512 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
518 in
= BIO_new(BIO_s_file());
520 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
524 if (BIO_read_filename(in
, file
) <= 0) {
525 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
528 if (type
== SSL_FILETYPE_ASN1
) {
530 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
531 } else if (type
== SSL_FILETYPE_PEM
) {
533 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
534 ctx
->default_passwd_callback
,
535 ctx
->default_passwd_callback_userdata
);
537 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
541 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, j
);
544 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
552 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
,
556 const unsigned char *p
;
560 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
561 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
565 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
569 #endif /* !OPENSSL_NO_RSA */
571 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
574 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
577 return (ssl_set_pkey(ctx
->cert
, pkey
));
580 #ifndef OPENSSL_NO_STDIO
581 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
585 EVP_PKEY
*pkey
= NULL
;
587 in
= BIO_new(BIO_s_file());
589 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
593 if (BIO_read_filename(in
, file
) <= 0) {
594 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
597 if (type
== SSL_FILETYPE_PEM
) {
599 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
600 ctx
->default_passwd_callback
,
601 ctx
->default_passwd_callback_userdata
);
602 } else if (type
== SSL_FILETYPE_ASN1
) {
604 pkey
= d2i_PrivateKey_bio(in
, NULL
);
606 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
610 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, j
);
613 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
621 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
,
622 const unsigned char *d
, long len
)
625 const unsigned char *p
;
629 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
, (long)len
)) == NULL
) {
630 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
634 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
639 #ifndef OPENSSL_NO_STDIO
641 * Read a file that contains our certificate in "PEM" format, possibly
642 * followed by a sequence of CA certificates that should be sent to the peer
643 * in the Certificate message.
645 static int use_certificate_chain_file(SSL_CTX
*ctx
, SSL
*ssl
, const char *file
)
650 pem_password_cb
*passwd_callback
;
651 void *passwd_callback_userdata
;
653 ERR_clear_error(); /* clear error stack for
654 * SSL_CTX_use_certificate() */
657 passwd_callback
= ctx
->default_passwd_callback
;
658 passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
660 passwd_callback
= ssl
->default_passwd_callback
;
661 passwd_callback_userdata
= ssl
->default_passwd_callback_userdata
;
664 in
= BIO_new(BIO_s_file());
666 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
670 if (BIO_read_filename(in
, file
) <= 0) {
671 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_SYS_LIB
);
675 x
= PEM_read_bio_X509_AUX(in
, NULL
, passwd_callback
,
676 passwd_callback_userdata
);
678 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_PEM_LIB
);
683 ret
= SSL_CTX_use_certificate(ctx
, x
);
685 ret
= SSL_use_certificate(ssl
, x
);
687 if (ERR_peek_error() != 0)
688 ret
= 0; /* Key/certificate mismatch doesn't imply
692 * If we could set up our certificate, now proceed to the CA
700 r
= SSL_CTX_clear_chain_certs(ctx
);
702 r
= SSL_clear_chain_certs(ssl
);
709 while ((ca
= PEM_read_bio_X509(in
, NULL
, passwd_callback
,
710 passwd_callback_userdata
))
713 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
715 r
= SSL_add0_chain_cert(ssl
, ca
);
717 * Note that we must not free ca if it was successfully added to
718 * the chain (while we must free the main certificate, since its
719 * reference count is increased by SSL_CTX_use_certificate).
727 /* When the while loop ends, it's usually just EOF. */
728 err
= ERR_peek_last_error();
729 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
730 && ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
733 ret
= 0; /* some real error */
742 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
744 return use_certificate_chain_file(ctx
, NULL
, file
);
747 int SSL_use_certificate_chain_file(SSL
*ssl
, const char *file
)
749 return use_certificate_chain_file(NULL
, ssl
, file
);
753 static int serverinfo_find_extension(const unsigned char *serverinfo
,
754 size_t serverinfo_length
,
755 unsigned int extension_type
,
756 const unsigned char **extension_data
,
757 size_t *extension_length
)
759 *extension_data
= NULL
;
760 *extension_length
= 0;
761 if (serverinfo
== NULL
|| serverinfo_length
== 0)
764 unsigned int type
= 0;
767 /* end of serverinfo */
768 if (serverinfo_length
== 0)
769 return 0; /* Extension not found */
771 /* read 2-byte type field */
772 if (serverinfo_length
< 2)
773 return -1; /* Error */
774 type
= (serverinfo
[0] << 8) + serverinfo
[1];
776 serverinfo_length
-= 2;
778 /* read 2-byte len field */
779 if (serverinfo_length
< 2)
780 return -1; /* Error */
781 len
= (serverinfo
[0] << 8) + serverinfo
[1];
783 serverinfo_length
-= 2;
785 if (len
> serverinfo_length
)
786 return -1; /* Error */
788 if (type
== extension_type
) {
789 *extension_data
= serverinfo
;
790 *extension_length
= len
;
791 return 1; /* Success */
795 serverinfo_length
-= len
;
800 static int serverinfo_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
801 const unsigned char *in
,
802 size_t inlen
, int *al
, void *arg
)
806 *al
= SSL_AD_DECODE_ERROR
;
813 static int serverinfo_srv_add_cb(SSL
*s
, unsigned int ext_type
,
814 const unsigned char **out
, size_t *outlen
,
817 const unsigned char *serverinfo
= NULL
;
818 size_t serverinfo_length
= 0;
820 /* Is there serverinfo data for the chosen server cert? */
821 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
822 &serverinfo_length
)) != 0) {
823 /* Find the relevant extension from the serverinfo */
824 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
825 ext_type
, out
, outlen
);
827 *al
= SSL_AD_DECODE_ERROR
;
828 return -1; /* Error */
831 return 0; /* No extension found, don't send extension */
832 return 1; /* Send extension */
834 return 0; /* No serverinfo data found, don't send
839 * With a NULL context, this function just checks that the serverinfo data
840 * parses correctly. With a non-NULL context, it registers callbacks for
841 * the included extensions.
843 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
844 size_t serverinfo_length
, SSL_CTX
*ctx
)
846 if (serverinfo
== NULL
|| serverinfo_length
== 0)
849 unsigned int ext_type
= 0;
852 /* end of serverinfo */
853 if (serverinfo_length
== 0)
856 /* read 2-byte type field */
857 if (serverinfo_length
< 2)
859 /* FIXME: check for types we understand explicitly? */
861 /* Register callbacks for extensions */
862 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
864 int have_ext_cbs
= 0;
866 custom_ext_methods
*exts
= &ctx
->cert
->srv_ext
;
867 custom_ext_method
*meth
= exts
->meths
;
869 for (i
= 0; i
< exts
->meths_count
; i
++, meth
++) {
870 if (ext_type
== meth
->ext_type
) {
876 if (!have_ext_cbs
&& !SSL_CTX_add_server_custom_ext(ctx
, ext_type
,
877 serverinfo_srv_add_cb
,
879 serverinfo_srv_parse_cb
,
885 serverinfo_length
-= 2;
887 /* read 2-byte len field */
888 if (serverinfo_length
< 2)
890 len
= (serverinfo
[0] << 8) + serverinfo
[1];
892 serverinfo_length
-= 2;
894 if (len
> serverinfo_length
)
898 serverinfo_length
-= len
;
902 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
903 size_t serverinfo_length
)
905 unsigned char *new_serverinfo
;
907 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0) {
908 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_PASSED_NULL_PARAMETER
);
911 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
)) {
912 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
915 if (ctx
->cert
->key
== NULL
) {
916 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_INTERNAL_ERROR
);
919 new_serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
921 if (new_serverinfo
== NULL
) {
922 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, ERR_R_MALLOC_FAILURE
);
925 ctx
->cert
->key
->serverinfo
= new_serverinfo
;
926 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
927 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
930 * Now that the serverinfo is validated and stored, go ahead and
931 * register callbacks.
933 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
)) {
934 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
, SSL_R_INVALID_SERVERINFO_DATA
);
940 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
942 unsigned char *serverinfo
= NULL
;
943 size_t serverinfo_length
= 0;
944 unsigned char *extension
= 0;
945 long extension_length
= 0;
948 char namePrefix
[] = "SERVERINFO FOR ";
951 size_t num_extensions
= 0;
953 if (ctx
== NULL
|| file
== NULL
) {
954 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
955 ERR_R_PASSED_NULL_PARAMETER
);
959 bin
= BIO_new(BIO_s_file());
961 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
964 if (BIO_read_filename(bin
, file
) <= 0) {
965 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
969 for (num_extensions
= 0;; num_extensions
++) {
970 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
)
973 * There must be at least one extension in this file
975 if (num_extensions
== 0) {
976 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
977 SSL_R_NO_PEM_EXTENSIONS
);
979 } else /* End of file, we're done */
982 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
983 if (strlen(name
) < strlen(namePrefix
)) {
984 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
985 SSL_R_PEM_NAME_TOO_SHORT
);
988 if (strncmp(name
, namePrefix
, strlen(namePrefix
)) != 0) {
989 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,
990 SSL_R_PEM_NAME_BAD_PREFIX
);
994 * Check that the decoded PEM data is plausible (valid length field)
996 if (extension_length
< 4
997 || (extension
[2] << 8) + extension
[3] != extension_length
- 4) {
998 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, SSL_R_BAD_DATA
);
1001 /* Append the decoded extension to the serverinfo buffer */
1003 OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
1004 if (serverinfo
== NULL
) {
1005 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
1008 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
1009 serverinfo_length
+= extension_length
;
1013 OPENSSL_free(header
);
1015 OPENSSL_free(extension
);
1019 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
1021 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1023 OPENSSL_free(header
);
1024 OPENSSL_free(extension
);
1025 OPENSSL_free(serverinfo
);