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 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
168 ret = ssl_set_pkey(ssl->cert, pkey);
174 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
178 * Special case for DH: check two DH certificate types for a match. This
179 * means for DH certificates we must set the certificate first.
181 if (pkey->type == EVP_PKEY_DH) {
184 x = c->pkeys[SSL_PKEY_DH_RSA].x509;
185 if (x && X509_check_private_key(x, pkey))
187 x = c->pkeys[SSL_PKEY_DH_DSA].x509;
188 if (i == -1 && x && X509_check_private_key(x, pkey))
192 i = ssl_cert_type(NULL, pkey);
194 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
198 if (c->pkeys[i].x509 != NULL) {
200 pktmp = X509_get_pubkey(c->pkeys[i].x509);
202 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
203 EVP_PKEY_free(pktmp);
207 * The return code from EVP_PKEY_copy_parameters is deliberately
208 * ignored. Some EVP_PKEY types cannot do this.
210 EVP_PKEY_copy_parameters(pktmp, pkey);
211 EVP_PKEY_free(pktmp);
214 #ifndef OPENSSL_NO_RSA
216 * Don't check the public/private key, this is mostly for smart
219 if ((pkey->type == EVP_PKEY_RSA) &&
220 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
223 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
224 X509_free(c->pkeys[i].x509);
225 c->pkeys[i].x509 = NULL;
230 if (c->pkeys[i].privatekey != NULL)
231 EVP_PKEY_free(c->pkeys[i].privatekey);
232 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
233 c->pkeys[i].privatekey = pkey;
234 c->key = &(c->pkeys[i]);
240 #ifndef OPENSSL_NO_RSA
241 # ifndef OPENSSL_NO_STDIO
242 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
248 in = BIO_new(BIO_s_file_internal());
250 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
254 if (BIO_read_filename(in, file) <= 0) {
255 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
258 if (type == SSL_FILETYPE_ASN1) {
260 rsa = d2i_RSAPrivateKey_bio(in, NULL);
261 } else if (type == SSL_FILETYPE_PEM) {
263 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
264 ssl->ctx->default_passwd_callback,
266 ctx->default_passwd_callback_userdata);
268 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
272 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
275 ret = SSL_use_RSAPrivateKey(ssl, rsa);
284 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
287 const unsigned char *p;
291 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
292 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
296 ret = SSL_use_RSAPrivateKey(ssl, rsa);
300 #endif /* !OPENSSL_NO_RSA */
302 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
307 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
310 if (!ssl_cert_inst(&ssl->cert)) {
311 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
314 ret = ssl_set_pkey(ssl->cert, pkey);
318 #ifndef OPENSSL_NO_STDIO
319 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
323 EVP_PKEY *pkey = NULL;
325 in = BIO_new(BIO_s_file_internal());
327 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
331 if (BIO_read_filename(in, file) <= 0) {
332 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
335 if (type == SSL_FILETYPE_PEM) {
337 pkey = PEM_read_bio_PrivateKey(in, NULL,
338 ssl->ctx->default_passwd_callback,
340 ctx->default_passwd_callback_userdata);
341 } else if (type == SSL_FILETYPE_ASN1) {
343 pkey = d2i_PrivateKey_bio(in, NULL);
345 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
349 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
352 ret = SSL_use_PrivateKey(ssl, pkey);
361 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
365 const unsigned char *p;
369 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
370 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
374 ret = SSL_use_PrivateKey(ssl, pkey);
379 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
382 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
385 if (!ssl_cert_inst(&ctx->cert)) {
386 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
389 return (ssl_set_cert(ctx->cert, x));
392 static int ssl_set_cert(CERT *c, X509 *x)
397 pkey = X509_get_pubkey(x);
399 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
403 i = ssl_cert_type(x, pkey);
405 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
410 if (c->pkeys[i].privatekey != NULL) {
412 * The return code from EVP_PKEY_copy_parameters is deliberately
413 * ignored. Some EVP_PKEY types cannot do this.
415 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
418 #ifndef OPENSSL_NO_RSA
420 * Don't check the public/private key, this is mostly for smart
423 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
424 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
425 RSA_METHOD_FLAG_NO_CHECK)) ;
427 #endif /* OPENSSL_NO_RSA */
428 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
430 * don't fail for a cert/key mismatch, just free current private
431 * key (when switching to a different cert & key, first this
432 * function should be used, then ssl_set_pkey
434 EVP_PKEY_free(c->pkeys[i].privatekey);
435 c->pkeys[i].privatekey = NULL;
436 /* clear error queue */
443 if (c->pkeys[i].x509 != NULL)
444 X509_free(c->pkeys[i].x509);
445 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
446 c->pkeys[i].x509 = x;
447 c->key = &(c->pkeys[i]);
453 #ifndef OPENSSL_NO_STDIO
454 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
461 in = BIO_new(BIO_s_file_internal());
463 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
467 if (BIO_read_filename(in, file) <= 0) {
468 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
471 if (type == SSL_FILETYPE_ASN1) {
473 x = d2i_X509_bio(in, NULL);
474 } else if (type == SSL_FILETYPE_PEM) {
476 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
477 ctx->default_passwd_callback_userdata);
479 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
484 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
488 ret = SSL_CTX_use_certificate(ctx, x);
498 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
499 const unsigned char *d)
504 x = d2i_X509(NULL, &d, (long)len);
506 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
510 ret = SSL_CTX_use_certificate(ctx, x);
515 #ifndef OPENSSL_NO_RSA
516 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
522 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
525 if (!ssl_cert_inst(&ctx->cert)) {
526 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
529 if ((pkey = EVP_PKEY_new()) == NULL) {
530 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
535 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
540 ret = ssl_set_pkey(ctx->cert, pkey);
545 # ifndef OPENSSL_NO_STDIO
546 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
552 in = BIO_new(BIO_s_file_internal());
554 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
558 if (BIO_read_filename(in, file) <= 0) {
559 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
562 if (type == SSL_FILETYPE_ASN1) {
564 rsa = d2i_RSAPrivateKey_bio(in, NULL);
565 } else if (type == SSL_FILETYPE_PEM) {
567 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
568 ctx->default_passwd_callback,
569 ctx->default_passwd_callback_userdata);
571 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
575 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
578 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
587 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
591 const unsigned char *p;
595 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
596 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
600 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
604 #endif /* !OPENSSL_NO_RSA */
606 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
609 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
612 if (!ssl_cert_inst(&ctx->cert)) {
613 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
616 return (ssl_set_pkey(ctx->cert, pkey));
619 #ifndef OPENSSL_NO_STDIO
620 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
624 EVP_PKEY *pkey = NULL;
626 in = BIO_new(BIO_s_file_internal());
628 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
632 if (BIO_read_filename(in, file) <= 0) {
633 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
636 if (type == SSL_FILETYPE_PEM) {
638 pkey = PEM_read_bio_PrivateKey(in, NULL,
639 ctx->default_passwd_callback,
640 ctx->default_passwd_callback_userdata);
641 } else if (type == SSL_FILETYPE_ASN1) {
643 pkey = d2i_PrivateKey_bio(in, NULL);
645 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
649 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
652 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
661 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
662 const unsigned char *d, long len)
665 const unsigned char *p;
669 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
670 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
674 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
679 #ifndef OPENSSL_NO_STDIO
681 * Read a file that contains our certificate in "PEM" format, possibly
682 * followed by a sequence of CA certificates that should be sent to the peer
683 * in the Certificate message.
685 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
691 ERR_clear_error(); /* clear error stack for
692 * SSL_CTX_use_certificate() */
694 in = BIO_new(BIO_s_file_internal());
696 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
700 if (BIO_read_filename(in, file) <= 0) {
701 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
705 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
706 ctx->default_passwd_callback_userdata);
708 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
712 ret = SSL_CTX_use_certificate(ctx, x);
714 if (ERR_peek_error() != 0)
715 ret = 0; /* Key/certificate mismatch doesn't imply
719 * If we could set up our certificate, now proceed to the CA
726 SSL_CTX_clear_chain_certs(ctx);
728 while ((ca = PEM_read_bio_X509(in, NULL,
729 ctx->default_passwd_callback,
730 ctx->default_passwd_callback_userdata))
732 r = SSL_CTX_add0_chain_cert(ctx, ca);
739 * Note that we must not free r if it was successfully added to
740 * the chain (while we must free the main certificate, since its
741 * reference count is increased by SSL_CTX_use_certificate).
744 /* When the while loop ends, it's usually just EOF. */
745 err = ERR_peek_last_error();
746 if (ERR_GET_LIB(err) == ERR_LIB_PEM
747 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
750 ret = 0; /* some real error */
762 #ifndef OPENSSL_NO_TLSEXT
763 static int serverinfo_find_extension(const unsigned char *serverinfo,
764 size_t serverinfo_length,
765 unsigned int extension_type,
766 const unsigned char **extension_data,
767 size_t *extension_length)
769 *extension_data = NULL;
770 *extension_length = 0;
771 if (serverinfo == NULL || serverinfo_length == 0)
774 unsigned int type = 0;
777 /* end of serverinfo */
778 if (serverinfo_length == 0)
779 return 0; /* Extension not found */
781 /* read 2-byte type field */
782 if (serverinfo_length < 2)
783 return -1; /* Error */
784 type = (serverinfo[0] << 8) + serverinfo[1];
786 serverinfo_length -= 2;
788 /* read 2-byte len field */
789 if (serverinfo_length < 2)
790 return -1; /* Error */
791 len = (serverinfo[0] << 8) + serverinfo[1];
793 serverinfo_length -= 2;
795 if (len > serverinfo_length)
796 return -1; /* Error */
798 if (type == extension_type) {
799 *extension_data = serverinfo;
800 *extension_length = len;
801 return 1; /* Success */
805 serverinfo_length -= len;
807 return 0; /* Error */
810 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
811 const unsigned char *in,
812 size_t inlen, int *al, void *arg)
816 *al = SSL_AD_DECODE_ERROR;
823 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
824 const unsigned char **out, size_t *outlen,
827 const unsigned char *serverinfo = NULL;
828 size_t serverinfo_length = 0;
830 /* Is there serverinfo data for the chosen server cert? */
831 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
832 &serverinfo_length)) != 0) {
833 /* Find the relevant extension from the serverinfo */
834 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
835 ext_type, out, outlen);
837 *al = SSL_AD_DECODE_ERROR;
838 return -1; /* Error */
841 return 0; /* No extension found, don't send extension */
842 return 1; /* Send extension */
844 return 0; /* No serverinfo data found, don't send
849 * With a NULL context, this function just checks that the serverinfo data
850 * parses correctly. With a non-NULL context, it registers callbacks for
851 * the included extensions.
853 static int serverinfo_process_buffer(const unsigned char *serverinfo,
854 size_t serverinfo_length, SSL_CTX *ctx)
856 if (serverinfo == NULL || serverinfo_length == 0)
859 unsigned int ext_type = 0;
862 /* end of serverinfo */
863 if (serverinfo_length == 0)
866 /* read 2-byte type field */
867 if (serverinfo_length < 2)
869 /* FIXME: check for types we understand explicitly? */
871 /* Register callbacks for extensions */
872 ext_type = (serverinfo[0] << 8) + serverinfo[1];
874 int have_ext_cbs = 0;
876 custom_ext_methods *exts = &ctx->cert->srv_ext;
877 custom_ext_method *meth = exts->meths;
879 for (i = 0; i < exts->meths_count; i++, meth++) {
880 if (ext_type == meth->ext_type) {
886 if (!have_ext_cbs && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
887 serverinfo_srv_add_cb,
889 serverinfo_srv_parse_cb,
895 serverinfo_length -= 2;
897 /* read 2-byte len field */
898 if (serverinfo_length < 2)
900 len = (serverinfo[0] << 8) + serverinfo[1];
902 serverinfo_length -= 2;
904 if (len > serverinfo_length)
908 serverinfo_length -= len;
912 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
913 size_t serverinfo_length)
915 unsigned char *new_serverinfo;
917 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
918 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
921 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
922 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
925 if (!ssl_cert_inst(&ctx->cert)) {
926 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
929 if (ctx->cert->key == NULL) {
930 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
933 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
935 if (new_serverinfo == NULL) {
936 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
939 ctx->cert->key->serverinfo = new_serverinfo;
940 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
941 ctx->cert->key->serverinfo_length = serverinfo_length;
944 * Now that the serverinfo is validated and stored, go ahead and
945 * register callbacks.
947 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
948 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
954 # ifndef OPENSSL_NO_STDIO
955 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
957 unsigned char *serverinfo = NULL;
958 size_t serverinfo_length = 0;
959 unsigned char *extension = 0;
960 long extension_length = 0;
963 char namePrefix[] = "SERVERINFO FOR ";
966 size_t num_extensions = 0;
967 unsigned char *new_serverinfo;
969 if (ctx == NULL || file == NULL) {
970 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
971 ERR_R_PASSED_NULL_PARAMETER);
975 bin = BIO_new(BIO_s_file_internal());
977 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
980 if (BIO_read_filename(bin, file) <= 0) {
981 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
985 for (num_extensions = 0;; num_extensions++) {
986 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
989 * There must be at least one extension in this file
991 if (num_extensions == 0) {
992 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
993 SSL_R_NO_PEM_EXTENSIONS);
995 } else /* End of file, we're done */
998 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
999 if (strlen(name) < strlen(namePrefix)) {
1000 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
1001 SSL_R_PEM_NAME_TOO_SHORT);
1004 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
1005 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
1006 SSL_R_PEM_NAME_BAD_PREFIX);
1010 * Check that the decoded PEM data is plausible (valid length field)
1012 if (extension_length < 4
1013 || (extension[2] << 8) + extension[3] != extension_length - 4) {
1014 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1017 /* Append the decoded extension to the serverinfo buffer */
1019 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
1020 if (new_serverinfo == NULL) {
1021 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1024 serverinfo = new_serverinfo;
1025 memcpy(serverinfo + serverinfo_length, extension, extension_length);
1026 serverinfo_length += extension_length;
1030 OPENSSL_free(header);
1032 OPENSSL_free(extension);
1036 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1038 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1040 OPENSSL_free(header);
1041 OPENSSL_free(extension);
1042 OPENSSL_free(serverinfo);
1047 # endif /* OPENSSL_NO_STDIO */
1048 #endif /* OPENSSL_NO_TLSEXT */