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);
381 if (c->pkeys[i].privatekey != NULL) {
383 * The return code from EVP_PKEY_copy_parameters is deliberately
384 * ignored. Some EVP_PKEY types cannot do this.
386 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
389 #ifndef OPENSSL_NO_RSA
391 * Don't check the public/private key, this is mostly for smart
394 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
395 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
396 RSA_METHOD_FLAG_NO_CHECK) ;
398 #endif /* OPENSSL_NO_RSA */
399 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
401 * don't fail for a cert/key mismatch, just free current private
402 * key (when switching to a different cert & key, first this
403 * function should be used, then ssl_set_pkey
405 EVP_PKEY_free(c->pkeys[i].privatekey);
406 c->pkeys[i].privatekey = NULL;
407 /* clear error queue */
412 X509_free(c->pkeys[i].x509);
414 c->pkeys[i].x509 = x;
415 c->key = &(c->pkeys[i]);
420 #ifndef OPENSSL_NO_STDIO
421 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
428 in = BIO_new(BIO_s_file());
430 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
434 if (BIO_read_filename(in, file) <= 0) {
435 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
438 if (type == SSL_FILETYPE_ASN1) {
440 x = d2i_X509_bio(in, NULL);
441 } else if (type == SSL_FILETYPE_PEM) {
443 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
444 ctx->default_passwd_callback_userdata);
446 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
451 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
455 ret = SSL_CTX_use_certificate(ctx, x);
463 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
464 const unsigned char *d)
469 x = d2i_X509(NULL, &d, (long)len);
471 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
475 ret = SSL_CTX_use_certificate(ctx, x);
480 #ifndef OPENSSL_NO_RSA
481 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
487 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
490 if ((pkey = EVP_PKEY_new()) == NULL) {
491 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
496 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
501 ret = ssl_set_pkey(ctx->cert, pkey);
506 # ifndef OPENSSL_NO_STDIO
507 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
513 in = BIO_new(BIO_s_file());
515 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
519 if (BIO_read_filename(in, file) <= 0) {
520 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
523 if (type == SSL_FILETYPE_ASN1) {
525 rsa = d2i_RSAPrivateKey_bio(in, NULL);
526 } else if (type == SSL_FILETYPE_PEM) {
528 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
529 ctx->default_passwd_callback,
530 ctx->default_passwd_callback_userdata);
532 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
536 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
539 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
547 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
551 const unsigned char *p;
555 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
556 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
560 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
564 #endif /* !OPENSSL_NO_RSA */
566 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
569 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
572 return (ssl_set_pkey(ctx->cert, pkey));
575 #ifndef OPENSSL_NO_STDIO
576 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
580 EVP_PKEY *pkey = NULL;
582 in = BIO_new(BIO_s_file());
584 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
588 if (BIO_read_filename(in, file) <= 0) {
589 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
592 if (type == SSL_FILETYPE_PEM) {
594 pkey = PEM_read_bio_PrivateKey(in, NULL,
595 ctx->default_passwd_callback,
596 ctx->default_passwd_callback_userdata);
597 } else if (type == SSL_FILETYPE_ASN1) {
599 pkey = d2i_PrivateKey_bio(in, NULL);
601 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
605 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
608 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
616 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
617 const unsigned char *d, long len)
620 const unsigned char *p;
624 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
625 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
629 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
634 #ifndef OPENSSL_NO_STDIO
636 * Read a file that contains our certificate in "PEM" format, possibly
637 * followed by a sequence of CA certificates that should be sent to the peer
638 * in the Certificate message.
640 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
645 pem_password_cb *passwd_callback;
646 void *passwd_callback_userdata;
648 ERR_clear_error(); /* clear error stack for
649 * SSL_CTX_use_certificate() */
652 passwd_callback = ctx->default_passwd_callback;
653 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
655 passwd_callback = ssl->default_passwd_callback;
656 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
659 in = BIO_new(BIO_s_file());
661 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
665 if (BIO_read_filename(in, file) <= 0) {
666 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
670 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
671 passwd_callback_userdata);
673 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
678 ret = SSL_CTX_use_certificate(ctx, x);
680 ret = SSL_use_certificate(ssl, x);
682 if (ERR_peek_error() != 0)
683 ret = 0; /* Key/certificate mismatch doesn't imply
687 * If we could set up our certificate, now proceed to the CA
695 r = SSL_CTX_clear_chain_certs(ctx);
697 r = SSL_clear_chain_certs(ssl);
704 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
705 passwd_callback_userdata))
708 r = SSL_CTX_add0_chain_cert(ctx, ca);
710 r = SSL_add0_chain_cert(ssl, ca);
712 * Note that we must not free ca if it was successfully added to
713 * the chain (while we must free the main certificate, since its
714 * reference count is increased by SSL_CTX_use_certificate).
722 /* When the while loop ends, it's usually just EOF. */
723 err = ERR_peek_last_error();
724 if (ERR_GET_LIB(err) == ERR_LIB_PEM
725 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
728 ret = 0; /* some real error */
737 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
739 return use_certificate_chain_file(ctx, NULL, file);
742 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
744 return use_certificate_chain_file(NULL, ssl, file);
748 static int serverinfo_find_extension(const unsigned char *serverinfo,
749 size_t serverinfo_length,
750 unsigned int extension_type,
751 const unsigned char **extension_data,
752 size_t *extension_length)
754 *extension_data = NULL;
755 *extension_length = 0;
756 if (serverinfo == NULL || serverinfo_length == 0)
759 unsigned int type = 0;
762 /* end of serverinfo */
763 if (serverinfo_length == 0)
764 return 0; /* Extension not found */
766 /* read 2-byte type field */
767 if (serverinfo_length < 2)
768 return -1; /* Error */
769 type = (serverinfo[0] << 8) + serverinfo[1];
771 serverinfo_length -= 2;
773 /* read 2-byte len field */
774 if (serverinfo_length < 2)
775 return -1; /* Error */
776 len = (serverinfo[0] << 8) + serverinfo[1];
778 serverinfo_length -= 2;
780 if (len > serverinfo_length)
781 return -1; /* Error */
783 if (type == extension_type) {
784 *extension_data = serverinfo;
785 *extension_length = len;
786 return 1; /* Success */
790 serverinfo_length -= len;
795 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
796 const unsigned char *in,
797 size_t inlen, int *al, void *arg)
801 *al = SSL_AD_DECODE_ERROR;
808 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
809 const unsigned char **out, size_t *outlen,
812 const unsigned char *serverinfo = NULL;
813 size_t serverinfo_length = 0;
815 /* Is there serverinfo data for the chosen server cert? */
816 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
817 &serverinfo_length)) != 0) {
818 /* Find the relevant extension from the serverinfo */
819 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
820 ext_type, out, outlen);
822 *al = SSL_AD_DECODE_ERROR;
823 return -1; /* Error */
826 return 0; /* No extension found, don't send extension */
827 return 1; /* Send extension */
829 return -1; /* No serverinfo data found, don't send
834 * With a NULL context, this function just checks that the serverinfo data
835 * parses correctly. With a non-NULL context, it registers callbacks for
836 * the included extensions.
838 static int serverinfo_process_buffer(const unsigned char *serverinfo,
839 size_t serverinfo_length, SSL_CTX *ctx)
841 if (serverinfo == NULL || serverinfo_length == 0)
844 unsigned int ext_type = 0;
847 /* end of serverinfo */
848 if (serverinfo_length == 0)
851 /* read 2-byte type field */
852 if (serverinfo_length < 2)
854 /* FIXME: check for types we understand explicitly? */
856 /* Register callbacks for extensions */
857 ext_type = (serverinfo[0] << 8) + serverinfo[1];
858 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
859 serverinfo_srv_add_cb,
861 serverinfo_srv_parse_cb,
866 serverinfo_length -= 2;
868 /* read 2-byte len field */
869 if (serverinfo_length < 2)
871 len = (serverinfo[0] << 8) + serverinfo[1];
873 serverinfo_length -= 2;
875 if (len > serverinfo_length)
879 serverinfo_length -= len;
883 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
884 size_t serverinfo_length)
886 unsigned char *new_serverinfo;
888 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
889 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
892 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
893 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
896 if (ctx->cert->key == NULL) {
897 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
900 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
902 if (new_serverinfo == NULL) {
903 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
906 ctx->cert->key->serverinfo = new_serverinfo;
907 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
908 ctx->cert->key->serverinfo_length = serverinfo_length;
911 * Now that the serverinfo is validated and stored, go ahead and
912 * register callbacks.
914 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
921 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
923 unsigned char *serverinfo = NULL;
924 size_t serverinfo_length = 0;
925 unsigned char *extension = 0;
926 long extension_length = 0;
929 char namePrefix[] = "SERVERINFO FOR ";
932 size_t num_extensions = 0;
934 if (ctx == NULL || file == NULL) {
935 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
936 ERR_R_PASSED_NULL_PARAMETER);
940 bin = BIO_new(BIO_s_file());
942 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
945 if (BIO_read_filename(bin, file) <= 0) {
946 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
950 for (num_extensions = 0;; num_extensions++) {
951 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
954 * There must be at least one extension in this file
956 if (num_extensions == 0) {
957 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
958 SSL_R_NO_PEM_EXTENSIONS);
960 } else /* End of file, we're done */
963 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
964 if (strlen(name) < strlen(namePrefix)) {
965 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
966 SSL_R_PEM_NAME_TOO_SHORT);
969 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
970 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
971 SSL_R_PEM_NAME_BAD_PREFIX);
975 * Check that the decoded PEM data is plausible (valid length field)
977 if (extension_length < 4
978 || (extension[2] << 8) + extension[3] != extension_length - 4) {
979 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
982 /* Append the decoded extension to the serverinfo buffer */
984 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
985 if (serverinfo == NULL) {
986 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
989 memcpy(serverinfo + serverinfo_length, extension, extension_length);
990 serverinfo_length += extension_length;
994 OPENSSL_free(header);
996 OPENSSL_free(extension);
1000 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1002 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1004 OPENSSL_free(header);
1005 OPENSSL_free(extension);
1006 OPENSSL_free(serverinfo);