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);
130 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
135 x = d2i_X509(NULL, &d, (long)len);
137 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
141 ret = SSL_use_certificate(ssl, x);
146 #ifndef OPENSSL_NO_RSA
147 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
153 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
156 if ((pkey = EVP_PKEY_new()) == NULL) {
157 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
162 EVP_PKEY_assign_RSA(pkey, rsa);
164 ret = ssl_set_pkey(ssl->cert, pkey);
170 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
174 * Special case for DH: check two DH certificate types for a match. This
175 * means for DH certificates we must set the certificate first.
177 if (pkey->type == EVP_PKEY_DH) {
180 x = c->pkeys[SSL_PKEY_DH_RSA].x509;
181 if (x && X509_check_private_key(x, pkey))
183 x = c->pkeys[SSL_PKEY_DH_DSA].x509;
184 if (i == -1 && x && X509_check_private_key(x, pkey))
188 i = ssl_cert_type(NULL, pkey);
190 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
194 if (c->pkeys[i].x509 != NULL) {
196 pktmp = X509_get_pubkey(c->pkeys[i].x509);
197 EVP_PKEY_copy_parameters(pktmp, pkey);
198 EVP_PKEY_free(pktmp);
201 #ifndef OPENSSL_NO_RSA
203 * Don't check the public/private key, this is mostly for smart
206 if ((pkey->type == EVP_PKEY_RSA) &&
207 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
210 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
211 X509_free(c->pkeys[i].x509);
212 c->pkeys[i].x509 = NULL;
217 if (c->pkeys[i].privatekey != NULL)
218 EVP_PKEY_free(c->pkeys[i].privatekey);
219 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
220 c->pkeys[i].privatekey = pkey;
221 c->key = &(c->pkeys[i]);
227 #ifndef OPENSSL_NO_RSA
228 # ifndef OPENSSL_NO_STDIO
229 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
235 in = BIO_new(BIO_s_file_internal());
237 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
241 if (BIO_read_filename(in, file) <= 0) {
242 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
245 if (type == SSL_FILETYPE_ASN1) {
247 rsa = d2i_RSAPrivateKey_bio(in, NULL);
248 } else if (type == SSL_FILETYPE_PEM) {
250 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
251 ssl->ctx->default_passwd_callback,
253 ctx->default_passwd_callback_userdata);
255 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
259 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
262 ret = SSL_use_RSAPrivateKey(ssl, rsa);
271 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
274 const unsigned char *p;
278 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
279 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
283 ret = SSL_use_RSAPrivateKey(ssl, rsa);
287 #endif /* !OPENSSL_NO_RSA */
289 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
294 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
297 ret = ssl_set_pkey(ssl->cert, pkey);
301 #ifndef OPENSSL_NO_STDIO
302 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
306 EVP_PKEY *pkey = NULL;
308 in = BIO_new(BIO_s_file_internal());
310 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
314 if (BIO_read_filename(in, file) <= 0) {
315 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
318 if (type == SSL_FILETYPE_PEM) {
320 pkey = PEM_read_bio_PrivateKey(in, NULL,
321 ssl->ctx->default_passwd_callback,
323 ctx->default_passwd_callback_userdata);
324 } else if (type == SSL_FILETYPE_ASN1) {
326 pkey = d2i_PrivateKey_bio(in, NULL);
328 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
332 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
335 ret = SSL_use_PrivateKey(ssl, pkey);
344 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
348 const unsigned char *p;
352 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
353 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
357 ret = SSL_use_PrivateKey(ssl, pkey);
362 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
366 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
369 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
371 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
374 return (ssl_set_cert(ctx->cert, x));
377 static int ssl_set_cert(CERT *c, X509 *x)
382 pkey = X509_get_pubkey(x);
384 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
388 i = ssl_cert_type(x, pkey);
390 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
395 if (c->pkeys[i].privatekey != NULL) {
396 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
399 #ifndef OPENSSL_NO_RSA
401 * Don't check the public/private key, this is mostly for smart
404 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
405 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
406 RSA_METHOD_FLAG_NO_CHECK)) ;
408 #endif /* OPENSSL_NO_RSA */
409 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
411 * don't fail for a cert/key mismatch, just free current private
412 * key (when switching to a different cert & key, first this
413 * function should be used, then ssl_set_pkey
415 EVP_PKEY_free(c->pkeys[i].privatekey);
416 c->pkeys[i].privatekey = NULL;
417 /* clear error queue */
424 if (c->pkeys[i].x509 != NULL)
425 X509_free(c->pkeys[i].x509);
426 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
427 c->pkeys[i].x509 = x;
428 c->key = &(c->pkeys[i]);
434 #ifndef OPENSSL_NO_STDIO
435 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
442 in = BIO_new(BIO_s_file_internal());
444 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
448 if (BIO_read_filename(in, file) <= 0) {
449 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
452 if (type == SSL_FILETYPE_ASN1) {
454 x = d2i_X509_bio(in, NULL);
455 } else if (type == SSL_FILETYPE_PEM) {
457 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
458 ctx->default_passwd_callback_userdata);
460 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
465 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
469 ret = SSL_CTX_use_certificate(ctx, x);
479 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
480 const unsigned char *d)
485 x = d2i_X509(NULL, &d, (long)len);
487 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
491 ret = SSL_CTX_use_certificate(ctx, x);
496 #ifndef OPENSSL_NO_RSA
497 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
503 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
506 if ((pkey = EVP_PKEY_new()) == NULL) {
507 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
512 EVP_PKEY_assign_RSA(pkey, rsa);
514 ret = ssl_set_pkey(ctx->cert, pkey);
519 # ifndef OPENSSL_NO_STDIO
520 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
526 in = BIO_new(BIO_s_file_internal());
528 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
532 if (BIO_read_filename(in, file) <= 0) {
533 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
536 if (type == SSL_FILETYPE_ASN1) {
538 rsa = d2i_RSAPrivateKey_bio(in, NULL);
539 } else if (type == SSL_FILETYPE_PEM) {
541 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
542 ctx->default_passwd_callback,
543 ctx->default_passwd_callback_userdata);
545 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
549 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
552 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
561 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
565 const unsigned char *p;
569 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
570 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
574 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
578 #endif /* !OPENSSL_NO_RSA */
580 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
583 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
586 return (ssl_set_pkey(ctx->cert, pkey));
589 #ifndef OPENSSL_NO_STDIO
590 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
594 EVP_PKEY *pkey = NULL;
596 in = BIO_new(BIO_s_file_internal());
598 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
602 if (BIO_read_filename(in, file) <= 0) {
603 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
606 if (type == SSL_FILETYPE_PEM) {
608 pkey = PEM_read_bio_PrivateKey(in, NULL,
609 ctx->default_passwd_callback,
610 ctx->default_passwd_callback_userdata);
611 } else if (type == SSL_FILETYPE_ASN1) {
613 pkey = d2i_PrivateKey_bio(in, NULL);
615 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
619 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
622 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
631 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
632 const unsigned char *d, long len)
635 const unsigned char *p;
639 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
640 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
644 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
649 #ifndef OPENSSL_NO_STDIO
651 * Read a file that contains our certificate in "PEM" format, possibly
652 * followed by a sequence of CA certificates that should be sent to the peer
653 * in the Certificate message.
655 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
661 ERR_clear_error(); /* clear error stack for
662 * SSL_CTX_use_certificate() */
664 in = BIO_new(BIO_s_file_internal());
666 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
670 if (BIO_read_filename(in, file) <= 0) {
671 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
675 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
676 ctx->default_passwd_callback_userdata);
678 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
682 ret = SSL_CTX_use_certificate(ctx, x);
684 if (ERR_peek_error() != 0)
685 ret = 0; /* Key/certificate mismatch doesn't imply
689 * If we could set up our certificate, now proceed to the CA
696 if(!SSL_CTX_clear_chain_certs(ctx)) {
701 while ((ca = PEM_read_bio_X509(in, NULL,
702 ctx->default_passwd_callback,
703 ctx->default_passwd_callback_userdata))
705 r = SSL_CTX_add0_chain_cert(ctx, ca);
712 * Note that we must not free r 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).
717 /* When the while loop ends, it's usually just EOF. */
718 err = ERR_peek_last_error();
719 if (ERR_GET_LIB(err) == ERR_LIB_PEM
720 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
723 ret = 0; /* some real error */
735 #ifndef OPENSSL_NO_TLSEXT
736 static int serverinfo_find_extension(const unsigned char *serverinfo,
737 size_t serverinfo_length,
738 unsigned int extension_type,
739 const unsigned char **extension_data,
740 size_t *extension_length)
742 *extension_data = NULL;
743 *extension_length = 0;
744 if (serverinfo == NULL || serverinfo_length == 0)
747 unsigned int type = 0;
750 /* end of serverinfo */
751 if (serverinfo_length == 0)
752 return -1; /* Extension not found */
754 /* read 2-byte type field */
755 if (serverinfo_length < 2)
756 return 0; /* Error */
757 type = (serverinfo[0] << 8) + serverinfo[1];
759 serverinfo_length -= 2;
761 /* read 2-byte len field */
762 if (serverinfo_length < 2)
763 return 0; /* Error */
764 len = (serverinfo[0] << 8) + serverinfo[1];
766 serverinfo_length -= 2;
768 if (len > serverinfo_length)
769 return 0; /* Error */
771 if (type == extension_type) {
772 *extension_data = serverinfo;
773 *extension_length = len;
774 return 1; /* Success */
778 serverinfo_length -= len;
783 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
784 const unsigned char *in,
785 size_t inlen, int *al, void *arg)
789 *al = SSL_AD_DECODE_ERROR;
796 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
797 const unsigned char **out, size_t *outlen,
800 const unsigned char *serverinfo = NULL;
801 size_t serverinfo_length = 0;
803 /* Is there serverinfo data for the chosen server cert? */
804 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
805 &serverinfo_length)) != 0) {
806 /* Find the relevant extension from the serverinfo */
807 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
808 ext_type, out, outlen);
810 return 0; /* Error */
812 return -1; /* No extension found, don't send extension */
813 return 1; /* Send extension */
815 return -1; /* No serverinfo data found, don't send
820 * With a NULL context, this function just checks that the serverinfo data
821 * parses correctly. With a non-NULL context, it registers callbacks for
822 * the included extensions.
824 static int serverinfo_process_buffer(const unsigned char *serverinfo,
825 size_t serverinfo_length, SSL_CTX *ctx)
827 if (serverinfo == NULL || serverinfo_length == 0)
830 unsigned int ext_type = 0;
833 /* end of serverinfo */
834 if (serverinfo_length == 0)
837 /* read 2-byte type field */
838 if (serverinfo_length < 2)
840 /* FIXME: check for types we understand explicitly? */
842 /* Register callbacks for extensions */
843 ext_type = (serverinfo[0] << 8) + serverinfo[1];
844 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
845 serverinfo_srv_add_cb,
847 serverinfo_srv_parse_cb,
852 serverinfo_length -= 2;
854 /* read 2-byte len field */
855 if (serverinfo_length < 2)
857 len = (serverinfo[0] << 8) + serverinfo[1];
859 serverinfo_length -= 2;
861 if (len > serverinfo_length)
865 serverinfo_length -= len;
869 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
870 size_t serverinfo_length)
872 unsigned char *new_serverinfo;
874 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
875 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
878 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
879 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
882 if (ctx->cert->key == NULL) {
883 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
886 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
888 if (new_serverinfo == NULL) {
889 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
892 ctx->cert->key->serverinfo = new_serverinfo;
893 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
894 ctx->cert->key->serverinfo_length = serverinfo_length;
897 * Now that the serverinfo is validated and stored, go ahead and
898 * register callbacks.
900 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
901 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
907 # ifndef OPENSSL_NO_STDIO
908 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
910 unsigned char *serverinfo = NULL;
911 size_t serverinfo_length = 0;
912 unsigned char *extension = 0;
913 long extension_length = 0;
916 char namePrefix[] = "SERVERINFO FOR ";
919 size_t num_extensions = 0;
921 if (ctx == NULL || file == NULL) {
922 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
923 ERR_R_PASSED_NULL_PARAMETER);
927 bin = BIO_new(BIO_s_file_internal());
929 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
932 if (BIO_read_filename(bin, file) <= 0) {
933 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
937 for (num_extensions = 0;; num_extensions++) {
938 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
941 * There must be at least one extension in this file
943 if (num_extensions == 0) {
944 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
945 SSL_R_NO_PEM_EXTENSIONS);
947 } else /* End of file, we're done */
950 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
951 if (strlen(name) < strlen(namePrefix)) {
952 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
953 SSL_R_PEM_NAME_TOO_SHORT);
956 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
957 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
958 SSL_R_PEM_NAME_BAD_PREFIX);
962 * Check that the decoded PEM data is plausible (valid length field)
964 if (extension_length < 4
965 || (extension[2] << 8) + extension[3] != extension_length - 4) {
966 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
969 /* Append the decoded extension to the serverinfo buffer */
971 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
972 if (serverinfo == NULL) {
973 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
976 memcpy(serverinfo + serverinfo_length, extension, extension_length);
977 serverinfo_length += extension_length;
981 OPENSSL_free(header);
983 OPENSSL_free(extension);
987 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
989 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
991 OPENSSL_free(header);
992 OPENSSL_free(extension);
993 OPENSSL_free(serverinfo);
998 # endif /* OPENSSL_NO_STDIO */
999 #endif /* OPENSSL_NO_TLSEXT */