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);
128 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
133 x = d2i_X509(NULL, &d, (long)len);
135 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
139 ret = SSL_use_certificate(ssl, x);
144 #ifndef OPENSSL_NO_RSA
145 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
151 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
154 if ((pkey = EVP_PKEY_new()) == NULL) {
155 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
160 EVP_PKEY_assign_RSA(pkey, rsa);
162 ret = ssl_set_pkey(ssl->cert, pkey);
168 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
172 * Special case for DH: check two DH certificate types for a match. This
173 * means for DH certificates we must set the certificate first.
175 if (pkey->type == EVP_PKEY_DH) {
178 x = c->pkeys[SSL_PKEY_DH_RSA].x509;
179 if (x && X509_check_private_key(x, pkey))
181 x = c->pkeys[SSL_PKEY_DH_DSA].x509;
182 if (i == -1 && x && X509_check_private_key(x, pkey))
186 i = ssl_cert_type(NULL, pkey);
188 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
192 if (c->pkeys[i].x509 != NULL) {
194 pktmp = X509_get_pubkey(c->pkeys[i].x509);
195 EVP_PKEY_copy_parameters(pktmp, pkey);
196 EVP_PKEY_free(pktmp);
199 #ifndef OPENSSL_NO_RSA
201 * Don't check the public/private key, this is mostly for smart
204 if ((pkey->type == EVP_PKEY_RSA) &&
205 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
208 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
209 X509_free(c->pkeys[i].x509);
210 c->pkeys[i].x509 = NULL;
215 EVP_PKEY_free(c->pkeys[i].privatekey);
216 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
217 c->pkeys[i].privatekey = pkey;
218 c->key = &(c->pkeys[i]);
223 #ifndef OPENSSL_NO_RSA
224 # ifndef OPENSSL_NO_STDIO
225 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
231 in = BIO_new(BIO_s_file_internal());
233 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
237 if (BIO_read_filename(in, file) <= 0) {
238 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
241 if (type == SSL_FILETYPE_ASN1) {
243 rsa = d2i_RSAPrivateKey_bio(in, NULL);
244 } else if (type == SSL_FILETYPE_PEM) {
246 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
247 ssl->ctx->default_passwd_callback,
249 ctx->default_passwd_callback_userdata);
251 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
255 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
258 ret = SSL_use_RSAPrivateKey(ssl, rsa);
266 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
269 const unsigned char *p;
273 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
274 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
278 ret = SSL_use_RSAPrivateKey(ssl, rsa);
282 #endif /* !OPENSSL_NO_RSA */
284 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
289 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
292 ret = ssl_set_pkey(ssl->cert, pkey);
296 #ifndef OPENSSL_NO_STDIO
297 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
301 EVP_PKEY *pkey = NULL;
303 in = BIO_new(BIO_s_file_internal());
305 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
309 if (BIO_read_filename(in, file) <= 0) {
310 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
313 if (type == SSL_FILETYPE_PEM) {
315 pkey = PEM_read_bio_PrivateKey(in, NULL,
316 ssl->ctx->default_passwd_callback,
318 ctx->default_passwd_callback_userdata);
319 } else if (type == SSL_FILETYPE_ASN1) {
321 pkey = d2i_PrivateKey_bio(in, NULL);
323 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
327 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
330 ret = SSL_use_PrivateKey(ssl, pkey);
338 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
342 const unsigned char *p;
346 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
347 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
351 ret = SSL_use_PrivateKey(ssl, pkey);
356 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
360 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
363 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
365 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
368 return (ssl_set_cert(ctx->cert, x));
371 static int ssl_set_cert(CERT *c, X509 *x)
376 pkey = X509_get_pubkey(x);
378 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
382 i = ssl_cert_type(x, pkey);
384 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
389 if (c->pkeys[i].privatekey != NULL) {
390 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
393 #ifndef OPENSSL_NO_RSA
395 * Don't check the public/private key, this is mostly for smart
398 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
399 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
400 RSA_METHOD_FLAG_NO_CHECK)) ;
402 #endif /* OPENSSL_NO_RSA */
403 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
405 * don't fail for a cert/key mismatch, just free current private
406 * key (when switching to a different cert & key, first this
407 * function should be used, then ssl_set_pkey
409 EVP_PKEY_free(c->pkeys[i].privatekey);
410 c->pkeys[i].privatekey = NULL;
411 /* clear error queue */
418 X509_free(c->pkeys[i].x509);
419 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
420 c->pkeys[i].x509 = x;
421 c->key = &(c->pkeys[i]);
427 #ifndef OPENSSL_NO_STDIO
428 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
435 in = BIO_new(BIO_s_file_internal());
437 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
441 if (BIO_read_filename(in, file) <= 0) {
442 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
445 if (type == SSL_FILETYPE_ASN1) {
447 x = d2i_X509_bio(in, NULL);
448 } else if (type == SSL_FILETYPE_PEM) {
450 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
451 ctx->default_passwd_callback_userdata);
453 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
458 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
462 ret = SSL_CTX_use_certificate(ctx, x);
470 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
471 const unsigned char *d)
476 x = d2i_X509(NULL, &d, (long)len);
478 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
482 ret = SSL_CTX_use_certificate(ctx, x);
487 #ifndef OPENSSL_NO_RSA
488 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
494 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
497 if ((pkey = EVP_PKEY_new()) == NULL) {
498 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
503 EVP_PKEY_assign_RSA(pkey, rsa);
505 ret = ssl_set_pkey(ctx->cert, pkey);
510 # ifndef OPENSSL_NO_STDIO
511 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
517 in = BIO_new(BIO_s_file_internal());
519 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
523 if (BIO_read_filename(in, file) <= 0) {
524 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
527 if (type == SSL_FILETYPE_ASN1) {
529 rsa = d2i_RSAPrivateKey_bio(in, NULL);
530 } else if (type == SSL_FILETYPE_PEM) {
532 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
533 ctx->default_passwd_callback,
534 ctx->default_passwd_callback_userdata);
536 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
540 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
543 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
551 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
555 const unsigned char *p;
559 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
560 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
564 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
568 #endif /* !OPENSSL_NO_RSA */
570 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
573 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
576 return (ssl_set_pkey(ctx->cert, pkey));
579 #ifndef OPENSSL_NO_STDIO
580 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
584 EVP_PKEY *pkey = NULL;
586 in = BIO_new(BIO_s_file_internal());
588 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
592 if (BIO_read_filename(in, file) <= 0) {
593 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
596 if (type == SSL_FILETYPE_PEM) {
598 pkey = PEM_read_bio_PrivateKey(in, NULL,
599 ctx->default_passwd_callback,
600 ctx->default_passwd_callback_userdata);
601 } else if (type == SSL_FILETYPE_ASN1) {
603 pkey = d2i_PrivateKey_bio(in, NULL);
605 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
609 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
612 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
620 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
621 const unsigned char *d, long len)
624 const unsigned char *p;
628 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
629 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
633 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
638 #ifndef OPENSSL_NO_STDIO
640 * Read a file that contains our certificate in "PEM" format, possibly
641 * followed by a sequence of CA certificates that should be sent to the peer
642 * in the Certificate message.
644 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
650 ERR_clear_error(); /* clear error stack for
651 * SSL_CTX_use_certificate() */
653 in = BIO_new(BIO_s_file_internal());
655 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
659 if (BIO_read_filename(in, file) <= 0) {
660 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
664 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
665 ctx->default_passwd_callback_userdata);
667 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
671 ret = SSL_CTX_use_certificate(ctx, x);
673 if (ERR_peek_error() != 0)
674 ret = 0; /* Key/certificate mismatch doesn't imply
678 * If we could set up our certificate, now proceed to the CA
685 if (!SSL_CTX_clear_chain_certs(ctx)) {
690 while ((ca = PEM_read_bio_X509(in, NULL,
691 ctx->default_passwd_callback,
692 ctx->default_passwd_callback_userdata))
694 r = SSL_CTX_add0_chain_cert(ctx, ca);
701 * Note that we must not free r if it was successfully added to
702 * the chain (while we must free the main certificate, since its
703 * reference count is increased by SSL_CTX_use_certificate).
706 /* When the while loop ends, it's usually just EOF. */
707 err = ERR_peek_last_error();
708 if (ERR_GET_LIB(err) == ERR_LIB_PEM
709 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
712 ret = 0; /* some real error */
722 #ifndef OPENSSL_NO_TLSEXT
723 static int serverinfo_find_extension(const unsigned char *serverinfo,
724 size_t serverinfo_length,
725 unsigned int extension_type,
726 const unsigned char **extension_data,
727 size_t *extension_length)
729 *extension_data = NULL;
730 *extension_length = 0;
731 if (serverinfo == NULL || serverinfo_length == 0)
734 unsigned int type = 0;
737 /* end of serverinfo */
738 if (serverinfo_length == 0)
739 return -1; /* Extension not found */
741 /* read 2-byte type field */
742 if (serverinfo_length < 2)
743 return 0; /* Error */
744 type = (serverinfo[0] << 8) + serverinfo[1];
746 serverinfo_length -= 2;
748 /* read 2-byte len field */
749 if (serverinfo_length < 2)
750 return 0; /* Error */
751 len = (serverinfo[0] << 8) + serverinfo[1];
753 serverinfo_length -= 2;
755 if (len > serverinfo_length)
756 return 0; /* Error */
758 if (type == extension_type) {
759 *extension_data = serverinfo;
760 *extension_length = len;
761 return 1; /* Success */
765 serverinfo_length -= len;
770 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
771 const unsigned char *in,
772 size_t inlen, int *al, void *arg)
776 *al = SSL_AD_DECODE_ERROR;
783 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
784 const unsigned char **out, size_t *outlen,
787 const unsigned char *serverinfo = NULL;
788 size_t serverinfo_length = 0;
790 /* Is there serverinfo data for the chosen server cert? */
791 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
792 &serverinfo_length)) != 0) {
793 /* Find the relevant extension from the serverinfo */
794 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
795 ext_type, out, outlen);
797 return 0; /* Error */
799 return -1; /* No extension found, don't send extension */
800 return 1; /* Send extension */
802 return -1; /* No serverinfo data found, don't send
807 * With a NULL context, this function just checks that the serverinfo data
808 * parses correctly. With a non-NULL context, it registers callbacks for
809 * the included extensions.
811 static int serverinfo_process_buffer(const unsigned char *serverinfo,
812 size_t serverinfo_length, SSL_CTX *ctx)
814 if (serverinfo == NULL || serverinfo_length == 0)
817 unsigned int ext_type = 0;
820 /* end of serverinfo */
821 if (serverinfo_length == 0)
824 /* read 2-byte type field */
825 if (serverinfo_length < 2)
827 /* FIXME: check for types we understand explicitly? */
829 /* Register callbacks for extensions */
830 ext_type = (serverinfo[0] << 8) + serverinfo[1];
831 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
832 serverinfo_srv_add_cb,
834 serverinfo_srv_parse_cb,
839 serverinfo_length -= 2;
841 /* read 2-byte len field */
842 if (serverinfo_length < 2)
844 len = (serverinfo[0] << 8) + serverinfo[1];
846 serverinfo_length -= 2;
848 if (len > serverinfo_length)
852 serverinfo_length -= len;
856 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
857 size_t serverinfo_length)
859 unsigned char *new_serverinfo;
861 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
862 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
865 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
866 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
869 if (ctx->cert->key == NULL) {
870 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
873 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
875 if (new_serverinfo == NULL) {
876 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
879 ctx->cert->key->serverinfo = new_serverinfo;
880 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
881 ctx->cert->key->serverinfo_length = serverinfo_length;
884 * Now that the serverinfo is validated and stored, go ahead and
885 * register callbacks.
887 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
888 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
894 # ifndef OPENSSL_NO_STDIO
895 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
897 unsigned char *serverinfo = NULL;
898 size_t serverinfo_length = 0;
899 unsigned char *extension = 0;
900 long extension_length = 0;
903 char namePrefix[] = "SERVERINFO FOR ";
906 size_t num_extensions = 0;
908 if (ctx == NULL || file == NULL) {
909 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
910 ERR_R_PASSED_NULL_PARAMETER);
914 bin = BIO_new(BIO_s_file_internal());
916 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
919 if (BIO_read_filename(bin, file) <= 0) {
920 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
924 for (num_extensions = 0;; num_extensions++) {
925 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
928 * There must be at least one extension in this file
930 if (num_extensions == 0) {
931 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
932 SSL_R_NO_PEM_EXTENSIONS);
934 } else /* End of file, we're done */
937 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
938 if (strlen(name) < strlen(namePrefix)) {
939 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
940 SSL_R_PEM_NAME_TOO_SHORT);
943 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
944 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
945 SSL_R_PEM_NAME_BAD_PREFIX);
949 * Check that the decoded PEM data is plausible (valid length field)
951 if (extension_length < 4
952 || (extension[2] << 8) + extension[3] != extension_length - 4) {
953 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
956 /* Append the decoded extension to the serverinfo buffer */
958 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
959 if (serverinfo == NULL) {
960 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
963 memcpy(serverinfo + serverinfo_length, extension, extension_length);
964 serverinfo_length += extension_length;
968 OPENSSL_free(header);
970 OPENSSL_free(extension);
974 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
976 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
978 OPENSSL_free(header);
979 OPENSSL_free(extension);
980 OPENSSL_free(serverinfo);
984 # endif /* OPENSSL_NO_STDIO */
985 #endif /* OPENSSL_NO_TLSEXT */