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]);
222 #ifndef OPENSSL_NO_RSA
223 # ifndef OPENSSL_NO_STDIO
224 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
230 in = BIO_new(BIO_s_file_internal());
232 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
236 if (BIO_read_filename(in, file) <= 0) {
237 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
240 if (type == SSL_FILETYPE_ASN1) {
242 rsa = d2i_RSAPrivateKey_bio(in, NULL);
243 } else if (type == SSL_FILETYPE_PEM) {
245 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
246 ssl->ctx->default_passwd_callback,
248 ctx->default_passwd_callback_userdata);
250 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
254 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
257 ret = SSL_use_RSAPrivateKey(ssl, rsa);
265 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
268 const unsigned char *p;
272 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
273 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
277 ret = SSL_use_RSAPrivateKey(ssl, rsa);
281 #endif /* !OPENSSL_NO_RSA */
283 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
288 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
291 ret = ssl_set_pkey(ssl->cert, pkey);
295 #ifndef OPENSSL_NO_STDIO
296 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
300 EVP_PKEY *pkey = NULL;
302 in = BIO_new(BIO_s_file_internal());
304 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
308 if (BIO_read_filename(in, file) <= 0) {
309 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
312 if (type == SSL_FILETYPE_PEM) {
314 pkey = PEM_read_bio_PrivateKey(in, NULL,
315 ssl->ctx->default_passwd_callback,
317 ctx->default_passwd_callback_userdata);
318 } else if (type == SSL_FILETYPE_ASN1) {
320 pkey = d2i_PrivateKey_bio(in, NULL);
322 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
326 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
329 ret = SSL_use_PrivateKey(ssl, pkey);
337 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
341 const unsigned char *p;
345 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
346 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
350 ret = SSL_use_PrivateKey(ssl, pkey);
355 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
359 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
362 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
364 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
367 return (ssl_set_cert(ctx->cert, x));
370 static int ssl_set_cert(CERT *c, X509 *x)
375 pkey = X509_get_pubkey(x);
377 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
381 i = ssl_cert_type(x, pkey);
383 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
388 if (c->pkeys[i].privatekey != NULL) {
389 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
392 #ifndef OPENSSL_NO_RSA
394 * Don't check the public/private key, this is mostly for smart
397 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
398 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
399 RSA_METHOD_FLAG_NO_CHECK)) ;
401 #endif /* OPENSSL_NO_RSA */
402 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
404 * don't fail for a cert/key mismatch, just free current private
405 * key (when switching to a different cert & key, first this
406 * function should be used, then ssl_set_pkey
408 EVP_PKEY_free(c->pkeys[i].privatekey);
409 c->pkeys[i].privatekey = NULL;
410 /* clear error queue */
417 X509_free(c->pkeys[i].x509);
418 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
419 c->pkeys[i].x509 = x;
420 c->key = &(c->pkeys[i]);
425 #ifndef OPENSSL_NO_STDIO
426 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
433 in = BIO_new(BIO_s_file_internal());
435 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
439 if (BIO_read_filename(in, file) <= 0) {
440 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
443 if (type == SSL_FILETYPE_ASN1) {
445 x = d2i_X509_bio(in, NULL);
446 } else if (type == SSL_FILETYPE_PEM) {
448 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
449 ctx->default_passwd_callback_userdata);
451 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
456 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
460 ret = SSL_CTX_use_certificate(ctx, x);
468 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
469 const unsigned char *d)
474 x = d2i_X509(NULL, &d, (long)len);
476 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
480 ret = SSL_CTX_use_certificate(ctx, x);
485 #ifndef OPENSSL_NO_RSA
486 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
492 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
495 if ((pkey = EVP_PKEY_new()) == NULL) {
496 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
501 EVP_PKEY_assign_RSA(pkey, rsa);
503 ret = ssl_set_pkey(ctx->cert, pkey);
508 # ifndef OPENSSL_NO_STDIO
509 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
515 in = BIO_new(BIO_s_file_internal());
517 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
521 if (BIO_read_filename(in, file) <= 0) {
522 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
525 if (type == SSL_FILETYPE_ASN1) {
527 rsa = d2i_RSAPrivateKey_bio(in, NULL);
528 } else if (type == SSL_FILETYPE_PEM) {
530 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
531 ctx->default_passwd_callback,
532 ctx->default_passwd_callback_userdata);
534 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
538 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
541 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
549 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
553 const unsigned char *p;
557 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
558 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
562 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
566 #endif /* !OPENSSL_NO_RSA */
568 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
571 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
574 return (ssl_set_pkey(ctx->cert, pkey));
577 #ifndef OPENSSL_NO_STDIO
578 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
582 EVP_PKEY *pkey = NULL;
584 in = BIO_new(BIO_s_file_internal());
586 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
590 if (BIO_read_filename(in, file) <= 0) {
591 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
594 if (type == SSL_FILETYPE_PEM) {
596 pkey = PEM_read_bio_PrivateKey(in, NULL,
597 ctx->default_passwd_callback,
598 ctx->default_passwd_callback_userdata);
599 } else if (type == SSL_FILETYPE_ASN1) {
601 pkey = d2i_PrivateKey_bio(in, NULL);
603 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
607 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
610 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
618 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
619 const unsigned char *d, long len)
622 const unsigned char *p;
626 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
627 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
631 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
636 #ifndef OPENSSL_NO_STDIO
638 * Read a file that contains our certificate in "PEM" format, possibly
639 * followed by a sequence of CA certificates that should be sent to the peer
640 * in the Certificate message.
642 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
648 ERR_clear_error(); /* clear error stack for
649 * SSL_CTX_use_certificate() */
651 in = BIO_new(BIO_s_file_internal());
653 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
657 if (BIO_read_filename(in, file) <= 0) {
658 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
662 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
663 ctx->default_passwd_callback_userdata);
665 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
670 ret = SSL_CTX_use_certificate(ctx, x);
672 ret = SSL_use_certificate(ssl, x);
674 if (ERR_peek_error() != 0)
675 ret = 0; /* Key/certificate mismatch doesn't imply
679 * If we could set up our certificate, now proceed to the CA
687 r = SSL_CTX_clear_chain_certs(ctx);
689 r = SSL_clear_chain_certs(ssl);
696 while ((ca = PEM_read_bio_X509(in, NULL,
697 ctx->default_passwd_callback,
698 ctx->default_passwd_callback_userdata))
701 r = SSL_CTX_add0_chain_cert(ctx, ca);
703 r = SSL_add0_chain_cert(ssl, ca);
705 * Note that we must not free ca if it was successfully added to
706 * the chain (while we must free the main certificate, since its
707 * reference count is increased by SSL_CTX_use_certificate).
715 /* When the while loop ends, it's usually just EOF. */
716 err = ERR_peek_last_error();
717 if (ERR_GET_LIB(err) == ERR_LIB_PEM
718 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
721 ret = 0; /* some real error */
730 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
732 return use_certificate_chain_file(ctx, NULL, file);
735 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
737 return use_certificate_chain_file(NULL, ssl, file);
741 static int serverinfo_find_extension(const unsigned char *serverinfo,
742 size_t serverinfo_length,
743 unsigned int extension_type,
744 const unsigned char **extension_data,
745 size_t *extension_length)
747 *extension_data = NULL;
748 *extension_length = 0;
749 if (serverinfo == NULL || serverinfo_length == 0)
752 unsigned int type = 0;
755 /* end of serverinfo */
756 if (serverinfo_length == 0)
757 return -1; /* Extension not found */
759 /* read 2-byte type field */
760 if (serverinfo_length < 2)
761 return 0; /* Error */
762 type = (serverinfo[0] << 8) + serverinfo[1];
764 serverinfo_length -= 2;
766 /* read 2-byte len field */
767 if (serverinfo_length < 2)
768 return 0; /* Error */
769 len = (serverinfo[0] << 8) + serverinfo[1];
771 serverinfo_length -= 2;
773 if (len > serverinfo_length)
774 return 0; /* Error */
776 if (type == extension_type) {
777 *extension_data = serverinfo;
778 *extension_length = len;
779 return 1; /* Success */
783 serverinfo_length -= len;
788 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
789 const unsigned char *in,
790 size_t inlen, int *al, void *arg)
794 *al = SSL_AD_DECODE_ERROR;
801 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
802 const unsigned char **out, size_t *outlen,
805 const unsigned char *serverinfo = NULL;
806 size_t serverinfo_length = 0;
808 /* Is there serverinfo data for the chosen server cert? */
809 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
810 &serverinfo_length)) != 0) {
811 /* Find the relevant extension from the serverinfo */
812 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
813 ext_type, out, outlen);
815 return 0; /* Error */
817 return -1; /* No extension found, don't send extension */
818 return 1; /* Send extension */
820 return -1; /* No serverinfo data found, don't send
825 * With a NULL context, this function just checks that the serverinfo data
826 * parses correctly. With a non-NULL context, it registers callbacks for
827 * the included extensions.
829 static int serverinfo_process_buffer(const unsigned char *serverinfo,
830 size_t serverinfo_length, SSL_CTX *ctx)
832 if (serverinfo == NULL || serverinfo_length == 0)
835 unsigned int ext_type = 0;
838 /* end of serverinfo */
839 if (serverinfo_length == 0)
842 /* read 2-byte type field */
843 if (serverinfo_length < 2)
845 /* FIXME: check for types we understand explicitly? */
847 /* Register callbacks for extensions */
848 ext_type = (serverinfo[0] << 8) + serverinfo[1];
849 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
850 serverinfo_srv_add_cb,
852 serverinfo_srv_parse_cb,
857 serverinfo_length -= 2;
859 /* read 2-byte len field */
860 if (serverinfo_length < 2)
862 len = (serverinfo[0] << 8) + serverinfo[1];
864 serverinfo_length -= 2;
866 if (len > serverinfo_length)
870 serverinfo_length -= len;
874 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
875 size_t serverinfo_length)
877 unsigned char *new_serverinfo;
879 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
880 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
883 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
884 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
887 if (ctx->cert->key == NULL) {
888 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
891 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
893 if (new_serverinfo == NULL) {
894 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
897 ctx->cert->key->serverinfo = new_serverinfo;
898 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
899 ctx->cert->key->serverinfo_length = serverinfo_length;
902 * Now that the serverinfo is validated and stored, go ahead and
903 * register callbacks.
905 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
906 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
912 #ifndef OPENSSL_NO_STDIO
913 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
915 unsigned char *serverinfo = NULL;
916 size_t serverinfo_length = 0;
917 unsigned char *extension = 0;
918 long extension_length = 0;
921 char namePrefix[] = "SERVERINFO FOR ";
924 size_t num_extensions = 0;
926 if (ctx == NULL || file == NULL) {
927 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
928 ERR_R_PASSED_NULL_PARAMETER);
932 bin = BIO_new(BIO_s_file_internal());
934 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
937 if (BIO_read_filename(bin, file) <= 0) {
938 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
942 for (num_extensions = 0;; num_extensions++) {
943 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
946 * There must be at least one extension in this file
948 if (num_extensions == 0) {
949 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
950 SSL_R_NO_PEM_EXTENSIONS);
952 } else /* End of file, we're done */
955 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
956 if (strlen(name) < strlen(namePrefix)) {
957 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
958 SSL_R_PEM_NAME_TOO_SHORT);
961 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
962 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
963 SSL_R_PEM_NAME_BAD_PREFIX);
967 * Check that the decoded PEM data is plausible (valid length field)
969 if (extension_length < 4
970 || (extension[2] << 8) + extension[3] != extension_length - 4) {
971 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
974 /* Append the decoded extension to the serverinfo buffer */
976 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
977 if (serverinfo == NULL) {
978 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
981 memcpy(serverinfo + serverinfo_length, extension, extension_length);
982 serverinfo_length += extension_length;
986 OPENSSL_free(header);
988 OPENSSL_free(extension);
992 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
994 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
996 OPENSSL_free(header);
997 OPENSSL_free(extension);
998 OPENSSL_free(serverinfo);
1002 #endif /* OPENSSL_NO_STDIO */