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 return (ssl_set_cert(ssl->cert, x));
85 #ifndef OPENSSL_NO_STDIO
86 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
93 in = BIO_new(BIO_s_file_internal());
95 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
99 if (BIO_read_filename(in, file) <= 0) {
100 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
103 if (type == SSL_FILETYPE_ASN1) {
105 x = d2i_X509_bio(in, NULL);
106 } else if (type == SSL_FILETYPE_PEM) {
108 x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
109 ssl->ctx->default_passwd_callback_userdata);
111 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
116 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
120 ret = SSL_use_certificate(ssl, x);
129 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
134 x = d2i_X509(NULL, &d, (long)len);
136 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
140 ret = SSL_use_certificate(ssl, x);
145 #ifndef OPENSSL_NO_RSA
146 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
152 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
155 if ((pkey = EVP_PKEY_new()) == NULL) {
156 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
161 EVP_PKEY_assign_RSA(pkey, rsa);
163 ret = ssl_set_pkey(ssl->cert, pkey);
169 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
173 * Special case for DH: check two DH certificate types for a match. This
174 * means for DH certificates we must set the certificate first.
176 if (pkey->type == EVP_PKEY_DH) {
179 x = c->pkeys[SSL_PKEY_DH_RSA].x509;
180 if (x && X509_check_private_key(x, pkey))
182 x = c->pkeys[SSL_PKEY_DH_DSA].x509;
183 if (i == -1 && x && X509_check_private_key(x, pkey))
187 i = ssl_cert_type(NULL, pkey);
189 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
193 if (c->pkeys[i].x509 != NULL) {
195 pktmp = X509_get_pubkey(c->pkeys[i].x509);
196 EVP_PKEY_copy_parameters(pktmp, pkey);
197 EVP_PKEY_free(pktmp);
200 #ifndef OPENSSL_NO_RSA
202 * Don't check the public/private key, this is mostly for smart
205 if ((pkey->type == EVP_PKEY_RSA) &&
206 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
209 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
210 X509_free(c->pkeys[i].x509);
211 c->pkeys[i].x509 = NULL;
216 if (c->pkeys[i].privatekey != NULL)
217 EVP_PKEY_free(c->pkeys[i].privatekey);
218 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
219 c->pkeys[i].privatekey = pkey;
220 c->key = &(c->pkeys[i]);
226 #ifndef OPENSSL_NO_RSA
227 # ifndef OPENSSL_NO_STDIO
228 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
234 in = BIO_new(BIO_s_file_internal());
236 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
240 if (BIO_read_filename(in, file) <= 0) {
241 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
244 if (type == SSL_FILETYPE_ASN1) {
246 rsa = d2i_RSAPrivateKey_bio(in, NULL);
247 } else if (type == SSL_FILETYPE_PEM) {
249 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
250 ssl->ctx->default_passwd_callback,
252 ctx->default_passwd_callback_userdata);
254 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
258 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
261 ret = SSL_use_RSAPrivateKey(ssl, rsa);
269 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
272 const unsigned char *p;
276 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
277 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
281 ret = SSL_use_RSAPrivateKey(ssl, rsa);
285 #endif /* !OPENSSL_NO_RSA */
287 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
292 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
295 ret = ssl_set_pkey(ssl->cert, pkey);
299 #ifndef OPENSSL_NO_STDIO
300 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
304 EVP_PKEY *pkey = NULL;
306 in = BIO_new(BIO_s_file_internal());
308 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
312 if (BIO_read_filename(in, file) <= 0) {
313 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
316 if (type == SSL_FILETYPE_PEM) {
318 pkey = PEM_read_bio_PrivateKey(in, NULL,
319 ssl->ctx->default_passwd_callback,
321 ctx->default_passwd_callback_userdata);
322 } else if (type == SSL_FILETYPE_ASN1) {
324 pkey = d2i_PrivateKey_bio(in, NULL);
326 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
330 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
333 ret = SSL_use_PrivateKey(ssl, pkey);
341 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
345 const unsigned char *p;
349 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
350 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
354 ret = SSL_use_PrivateKey(ssl, pkey);
359 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
363 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
366 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
368 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
371 return (ssl_set_cert(ctx->cert, x));
374 static int ssl_set_cert(CERT *c, X509 *x)
379 pkey = X509_get_pubkey(x);
381 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
385 i = ssl_cert_type(x, pkey);
387 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
392 if (c->pkeys[i].privatekey != NULL) {
393 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
396 #ifndef OPENSSL_NO_RSA
398 * Don't check the public/private key, this is mostly for smart
401 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
402 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
403 RSA_METHOD_FLAG_NO_CHECK)) ;
405 #endif /* OPENSSL_NO_RSA */
406 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
408 * don't fail for a cert/key mismatch, just free current private
409 * key (when switching to a different cert & key, first this
410 * function should be used, then ssl_set_pkey
412 EVP_PKEY_free(c->pkeys[i].privatekey);
413 c->pkeys[i].privatekey = NULL;
414 /* clear error queue */
421 if (c->pkeys[i].x509 != NULL)
422 X509_free(c->pkeys[i].x509);
423 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
424 c->pkeys[i].x509 = x;
425 c->key = &(c->pkeys[i]);
431 #ifndef OPENSSL_NO_STDIO
432 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
439 in = BIO_new(BIO_s_file_internal());
441 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
445 if (BIO_read_filename(in, file) <= 0) {
446 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
449 if (type == SSL_FILETYPE_ASN1) {
451 x = d2i_X509_bio(in, NULL);
452 } else if (type == SSL_FILETYPE_PEM) {
454 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
455 ctx->default_passwd_callback_userdata);
457 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
462 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
466 ret = SSL_CTX_use_certificate(ctx, x);
475 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
476 const unsigned char *d)
481 x = d2i_X509(NULL, &d, (long)len);
483 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
487 ret = SSL_CTX_use_certificate(ctx, x);
492 #ifndef OPENSSL_NO_RSA
493 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
499 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
502 if ((pkey = EVP_PKEY_new()) == NULL) {
503 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
508 EVP_PKEY_assign_RSA(pkey, rsa);
510 ret = ssl_set_pkey(ctx->cert, pkey);
515 # ifndef OPENSSL_NO_STDIO
516 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
522 in = BIO_new(BIO_s_file_internal());
524 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
528 if (BIO_read_filename(in, file) <= 0) {
529 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
532 if (type == SSL_FILETYPE_ASN1) {
534 rsa = d2i_RSAPrivateKey_bio(in, NULL);
535 } else if (type == SSL_FILETYPE_PEM) {
537 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
538 ctx->default_passwd_callback,
539 ctx->default_passwd_callback_userdata);
541 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
545 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
548 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
556 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
560 const unsigned char *p;
564 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
565 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
569 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
573 #endif /* !OPENSSL_NO_RSA */
575 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
578 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
581 return (ssl_set_pkey(ctx->cert, pkey));
584 #ifndef OPENSSL_NO_STDIO
585 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
589 EVP_PKEY *pkey = NULL;
591 in = BIO_new(BIO_s_file_internal());
593 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
597 if (BIO_read_filename(in, file) <= 0) {
598 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
601 if (type == SSL_FILETYPE_PEM) {
603 pkey = PEM_read_bio_PrivateKey(in, NULL,
604 ctx->default_passwd_callback,
605 ctx->default_passwd_callback_userdata);
606 } else if (type == SSL_FILETYPE_ASN1) {
608 pkey = d2i_PrivateKey_bio(in, NULL);
610 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
614 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
617 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
625 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
626 const unsigned char *d, long len)
629 const unsigned char *p;
633 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
634 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
638 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
643 #ifndef OPENSSL_NO_STDIO
645 * Read a file that contains our certificate in "PEM" format, possibly
646 * followed by a sequence of CA certificates that should be sent to the peer
647 * in the Certificate message.
649 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
655 ERR_clear_error(); /* clear error stack for
656 * SSL_CTX_use_certificate() */
658 in = BIO_new(BIO_s_file_internal());
660 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
664 if (BIO_read_filename(in, file) <= 0) {
665 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
669 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
670 ctx->default_passwd_callback_userdata);
672 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
676 ret = SSL_CTX_use_certificate(ctx, x);
678 if (ERR_peek_error() != 0)
679 ret = 0; /* Key/certificate mismatch doesn't imply
683 * If we could set up our certificate, now proceed to the CA
690 if(!SSL_CTX_clear_chain_certs(ctx)) {
695 while ((ca = PEM_read_bio_X509(in, NULL,
696 ctx->default_passwd_callback,
697 ctx->default_passwd_callback_userdata))
699 r = SSL_CTX_add0_chain_cert(ctx, ca);
706 * Note that we must not free r if it was successfully added to
707 * the chain (while we must free the main certificate, since its
708 * reference count is increased by SSL_CTX_use_certificate).
711 /* When the while loop ends, it's usually just EOF. */
712 err = ERR_peek_last_error();
713 if (ERR_GET_LIB(err) == ERR_LIB_PEM
714 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
717 ret = 0; /* some real error */
728 #ifndef OPENSSL_NO_TLSEXT
729 static int serverinfo_find_extension(const unsigned char *serverinfo,
730 size_t serverinfo_length,
731 unsigned int extension_type,
732 const unsigned char **extension_data,
733 size_t *extension_length)
735 *extension_data = NULL;
736 *extension_length = 0;
737 if (serverinfo == NULL || serverinfo_length == 0)
740 unsigned int type = 0;
743 /* end of serverinfo */
744 if (serverinfo_length == 0)
745 return -1; /* Extension not found */
747 /* read 2-byte type field */
748 if (serverinfo_length < 2)
749 return 0; /* Error */
750 type = (serverinfo[0] << 8) + serverinfo[1];
752 serverinfo_length -= 2;
754 /* read 2-byte len field */
755 if (serverinfo_length < 2)
756 return 0; /* Error */
757 len = (serverinfo[0] << 8) + serverinfo[1];
759 serverinfo_length -= 2;
761 if (len > serverinfo_length)
762 return 0; /* Error */
764 if (type == extension_type) {
765 *extension_data = serverinfo;
766 *extension_length = len;
767 return 1; /* Success */
771 serverinfo_length -= len;
776 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
777 const unsigned char *in,
778 size_t inlen, int *al, void *arg)
782 *al = SSL_AD_DECODE_ERROR;
789 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
790 const unsigned char **out, size_t *outlen,
793 const unsigned char *serverinfo = NULL;
794 size_t serverinfo_length = 0;
796 /* Is there serverinfo data for the chosen server cert? */
797 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
798 &serverinfo_length)) != 0) {
799 /* Find the relevant extension from the serverinfo */
800 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
801 ext_type, out, outlen);
803 return 0; /* Error */
805 return -1; /* No extension found, don't send extension */
806 return 1; /* Send extension */
808 return -1; /* No serverinfo data found, don't send
813 * With a NULL context, this function just checks that the serverinfo data
814 * parses correctly. With a non-NULL context, it registers callbacks for
815 * the included extensions.
817 static int serverinfo_process_buffer(const unsigned char *serverinfo,
818 size_t serverinfo_length, SSL_CTX *ctx)
820 if (serverinfo == NULL || serverinfo_length == 0)
823 unsigned int ext_type = 0;
826 /* end of serverinfo */
827 if (serverinfo_length == 0)
830 /* read 2-byte type field */
831 if (serverinfo_length < 2)
833 /* FIXME: check for types we understand explicitly? */
835 /* Register callbacks for extensions */
836 ext_type = (serverinfo[0] << 8) + serverinfo[1];
837 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
838 serverinfo_srv_add_cb,
840 serverinfo_srv_parse_cb,
845 serverinfo_length -= 2;
847 /* read 2-byte len field */
848 if (serverinfo_length < 2)
850 len = (serverinfo[0] << 8) + serverinfo[1];
852 serverinfo_length -= 2;
854 if (len > serverinfo_length)
858 serverinfo_length -= len;
862 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
863 size_t serverinfo_length)
865 unsigned char *new_serverinfo;
867 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
868 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
871 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
872 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
875 if (ctx->cert->key == NULL) {
876 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
879 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
881 if (new_serverinfo == NULL) {
882 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
885 ctx->cert->key->serverinfo = new_serverinfo;
886 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
887 ctx->cert->key->serverinfo_length = serverinfo_length;
890 * Now that the serverinfo is validated and stored, go ahead and
891 * register callbacks.
893 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
894 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
900 # ifndef OPENSSL_NO_STDIO
901 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
903 unsigned char *serverinfo = NULL;
904 size_t serverinfo_length = 0;
905 unsigned char *extension = 0;
906 long extension_length = 0;
909 char namePrefix[] = "SERVERINFO FOR ";
912 size_t num_extensions = 0;
914 if (ctx == NULL || file == NULL) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
916 ERR_R_PASSED_NULL_PARAMETER);
920 bin = BIO_new(BIO_s_file_internal());
922 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
925 if (BIO_read_filename(bin, file) <= 0) {
926 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
930 for (num_extensions = 0;; num_extensions++) {
931 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
934 * There must be at least one extension in this file
936 if (num_extensions == 0) {
937 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
938 SSL_R_NO_PEM_EXTENSIONS);
940 } else /* End of file, we're done */
943 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
944 if (strlen(name) < strlen(namePrefix)) {
945 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
946 SSL_R_PEM_NAME_TOO_SHORT);
949 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
950 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
951 SSL_R_PEM_NAME_BAD_PREFIX);
955 * Check that the decoded PEM data is plausible (valid length field)
957 if (extension_length < 4
958 || (extension[2] << 8) + extension[3] != extension_length - 4) {
959 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
962 /* Append the decoded extension to the serverinfo buffer */
964 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
965 if (serverinfo == NULL) {
966 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
969 memcpy(serverinfo + serverinfo_length, extension, extension_length);
970 serverinfo_length += extension_length;
974 OPENSSL_free(header);
976 OPENSSL_free(extension);
980 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
982 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
984 OPENSSL_free(header);
985 OPENSSL_free(extension);
986 OPENSSL_free(serverinfo);
990 # endif /* OPENSSL_NO_STDIO */
991 #endif /* OPENSSL_NO_TLSEXT */