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)
74 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
77 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
80 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
84 if (!ssl_cert_inst(&ssl->cert))
86 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
89 return(ssl_set_cert(ssl->cert,x));
92 #ifndef OPENSSL_NO_STDIO
93 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
100 in=BIO_new(BIO_s_file_internal());
103 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
107 if (BIO_read_filename(in,file) <= 0)
109 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
112 if (type == SSL_FILETYPE_ASN1)
115 x=d2i_X509_bio(in,NULL);
117 else if (type == SSL_FILETYPE_PEM)
120 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
124 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
130 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
134 ret=SSL_use_certificate(ssl,x);
136 if (x != NULL) X509_free(x);
137 if (in != NULL) BIO_free(in);
142 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
147 x=d2i_X509(NULL,&d,(long)len);
150 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
154 ret=SSL_use_certificate(ssl,x);
159 #ifndef OPENSSL_NO_RSA
160 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
167 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
170 if (!ssl_cert_inst(&ssl->cert))
172 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
175 if ((pkey=EVP_PKEY_new()) == NULL)
177 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
182 EVP_PKEY_assign_RSA(pkey,rsa);
184 ret=ssl_set_pkey(ssl->cert,pkey);
190 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
193 /* Special case for DH: check two DH certificate types for a match.
194 * This means for DH certificates we must set the certificate first.
196 if (pkey->type == EVP_PKEY_DH)
200 x = c->pkeys[SSL_PKEY_DH_RSA].x509;
201 if (x && X509_check_private_key(x, pkey))
203 x = c->pkeys[SSL_PKEY_DH_DSA].x509;
204 if (i == -1 && x && X509_check_private_key(x, pkey))
209 i=ssl_cert_type(NULL,pkey);
212 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
216 if (c->pkeys[i].x509 != NULL)
219 pktmp = X509_get_pubkey(c->pkeys[i].x509);
220 EVP_PKEY_copy_parameters(pktmp,pkey);
221 EVP_PKEY_free(pktmp);
224 #ifndef OPENSSL_NO_RSA
225 /* Don't check the public/private key, this is mostly
226 * for smart cards. */
227 if ((pkey->type == EVP_PKEY_RSA) &&
228 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
232 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
234 X509_free(c->pkeys[i].x509);
235 c->pkeys[i].x509 = NULL;
240 if (c->pkeys[i].privatekey != NULL)
241 EVP_PKEY_free(c->pkeys[i].privatekey);
242 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
243 c->pkeys[i].privatekey=pkey;
244 c->key= &(c->pkeys[i]);
250 #ifndef OPENSSL_NO_RSA
251 #ifndef OPENSSL_NO_STDIO
252 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
258 in=BIO_new(BIO_s_file_internal());
261 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
265 if (BIO_read_filename(in,file) <= 0)
267 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
270 if (type == SSL_FILETYPE_ASN1)
273 rsa=d2i_RSAPrivateKey_bio(in,NULL);
275 else if (type == SSL_FILETYPE_PEM)
278 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
279 ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
283 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
288 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
291 ret=SSL_use_RSAPrivateKey(ssl,rsa);
294 if (in != NULL) BIO_free(in);
299 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
302 const unsigned char *p;
306 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
308 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
312 ret=SSL_use_RSAPrivateKey(ssl,rsa);
316 #endif /* !OPENSSL_NO_RSA */
318 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
324 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
327 if (!ssl_cert_inst(&ssl->cert))
329 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
332 ret=ssl_set_pkey(ssl->cert,pkey);
336 #ifndef OPENSSL_NO_STDIO
337 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
343 in=BIO_new(BIO_s_file_internal());
346 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
350 if (BIO_read_filename(in,file) <= 0)
352 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
355 if (type == SSL_FILETYPE_PEM)
358 pkey=PEM_read_bio_PrivateKey(in,NULL,
359 ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
361 else if (type == SSL_FILETYPE_ASN1)
364 pkey = d2i_PrivateKey_bio(in,NULL);
368 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
373 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
376 ret=SSL_use_PrivateKey(ssl,pkey);
379 if (in != NULL) BIO_free(in);
384 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
387 const unsigned char *p;
391 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
393 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
397 ret=SSL_use_PrivateKey(ssl,pkey);
402 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
407 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
410 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
413 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
416 if (!ssl_cert_inst(&ctx->cert))
418 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
421 return(ssl_set_cert(ctx->cert, x));
424 static int ssl_set_cert(CERT *c, X509 *x)
429 pkey=X509_get_pubkey(x);
432 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
436 i=ssl_cert_type(x,pkey);
439 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
444 if (c->pkeys[i].privatekey != NULL)
446 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
449 #ifndef OPENSSL_NO_RSA
450 /* Don't check the public/private key, this is mostly
451 * for smart cards. */
452 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
453 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
454 RSA_METHOD_FLAG_NO_CHECK))
457 #endif /* OPENSSL_NO_RSA */
458 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
460 /* don't fail for a cert/key mismatch, just free
461 * current private key (when switching to a different
462 * cert & key, first this function should be used,
463 * then ssl_set_pkey */
464 EVP_PKEY_free(c->pkeys[i].privatekey);
465 c->pkeys[i].privatekey=NULL;
466 /* clear error queue */
473 if (c->pkeys[i].x509 != NULL)
474 X509_free(c->pkeys[i].x509);
475 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
477 c->key= &(c->pkeys[i]);
483 #ifndef OPENSSL_NO_STDIO
484 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
491 in=BIO_new(BIO_s_file_internal());
494 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
498 if (BIO_read_filename(in,file) <= 0)
500 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
503 if (type == SSL_FILETYPE_ASN1)
506 x=d2i_X509_bio(in,NULL);
508 else if (type == SSL_FILETYPE_PEM)
511 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
515 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
521 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
525 ret=SSL_CTX_use_certificate(ctx,x);
527 if (x != NULL) X509_free(x);
528 if (in != NULL) BIO_free(in);
533 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
538 x=d2i_X509(NULL,&d,(long)len);
541 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
545 ret=SSL_CTX_use_certificate(ctx,x);
550 #ifndef OPENSSL_NO_RSA
551 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
558 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
561 if (!ssl_cert_inst(&ctx->cert))
563 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
566 if ((pkey=EVP_PKEY_new()) == NULL)
568 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
573 EVP_PKEY_assign_RSA(pkey,rsa);
575 ret=ssl_set_pkey(ctx->cert, pkey);
580 #ifndef OPENSSL_NO_STDIO
581 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
587 in=BIO_new(BIO_s_file_internal());
590 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
594 if (BIO_read_filename(in,file) <= 0)
596 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
599 if (type == SSL_FILETYPE_ASN1)
602 rsa=d2i_RSAPrivateKey_bio(in,NULL);
604 else if (type == SSL_FILETYPE_PEM)
607 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
608 ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
612 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
617 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
620 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
623 if (in != NULL) BIO_free(in);
628 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
631 const unsigned char *p;
635 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
637 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
641 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
645 #endif /* !OPENSSL_NO_RSA */
647 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
651 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
654 if (!ssl_cert_inst(&ctx->cert))
656 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
659 return(ssl_set_pkey(ctx->cert,pkey));
662 #ifndef OPENSSL_NO_STDIO
663 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
669 in=BIO_new(BIO_s_file_internal());
672 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
676 if (BIO_read_filename(in,file) <= 0)
678 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
681 if (type == SSL_FILETYPE_PEM)
684 pkey=PEM_read_bio_PrivateKey(in,NULL,
685 ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
687 else if (type == SSL_FILETYPE_ASN1)
690 pkey = d2i_PrivateKey_bio(in,NULL);
694 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
699 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
702 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
705 if (in != NULL) BIO_free(in);
710 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
714 const unsigned char *p;
718 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
720 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
724 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
730 #ifndef OPENSSL_NO_STDIO
731 /* Read a file that contains our certificate in "PEM" format,
732 * possibly followed by a sequence of CA certificates that should be
733 * sent to the peer in the Certificate message.
735 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
741 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
743 in = BIO_new(BIO_s_file_internal());
746 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
750 if (BIO_read_filename(in,file) <= 0)
752 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
756 x=PEM_read_bio_X509_AUX(in,NULL,ctx->default_passwd_callback,
757 ctx->default_passwd_callback_userdata);
760 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
764 ret = SSL_CTX_use_certificate(ctx, x);
766 if (ERR_peek_error() != 0)
767 ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
770 /* If we could set up our certificate, now proceed to
771 * the CA certificates.
777 SSL_CTX_clear_chain_certs(ctx);
779 while ((ca = PEM_read_bio_X509(in, NULL,
780 ctx->default_passwd_callback,
781 ctx->default_passwd_callback_userdata))
784 r = SSL_CTX_add0_chain_cert(ctx, ca);
791 /* Note that we must not free r if it was successfully
792 * added to the chain (while we must free the main
793 * certificate, since its reference count is increased
794 * by SSL_CTX_use_certificate). */
796 /* When the while loop ends, it's usually just EOF. */
797 err = ERR_peek_last_error();
798 if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
801 ret = 0; /* some real error */
805 if (x != NULL) X509_free(x);
806 if (in != NULL) BIO_free(in);
811 #ifndef OPENSSL_NO_TLSEXT
812 static int serverinfo_find_extension(const unsigned char *serverinfo,
813 size_t serverinfo_length,
814 unsigned int extension_type,
815 const unsigned char **extension_data,
816 size_t *extension_length)
818 *extension_data = NULL;
819 *extension_length = 0;
820 if (serverinfo == NULL || serverinfo_length == 0)
824 unsigned int type = 0;
827 /* end of serverinfo */
828 if (serverinfo_length == 0)
829 return -1; /* Extension not found */
831 /* read 2-byte type field */
832 if (serverinfo_length < 2)
833 return 0; /* Error */
834 type = (serverinfo[0] << 8) + serverinfo[1];
836 serverinfo_length -= 2;
838 /* read 2-byte len field */
839 if (serverinfo_length < 2)
840 return 0; /* Error */
841 len = (serverinfo[0] << 8) + serverinfo[1];
843 serverinfo_length -= 2;
845 if (len > serverinfo_length)
846 return 0; /* Error */
848 if (type == extension_type)
850 *extension_data = serverinfo;
851 *extension_length = len;
852 return 1; /* Success */
856 serverinfo_length -= len;
858 return 0; /* Error */
861 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
862 const unsigned char *in,
863 size_t inlen, int *al,
869 *al = SSL_AD_DECODE_ERROR;
876 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
877 const unsigned char **out, size_t *outlen,
880 const unsigned char *serverinfo = NULL;
881 size_t serverinfo_length = 0;
883 /* Is there serverinfo data for the chosen server cert? */
884 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
885 &serverinfo_length)) != 0)
887 /* Find the relevant extension from the serverinfo */
888 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
889 ext_type, out, outlen);
891 return 0; /* Error */
893 return -1; /* No extension found, don't send extension */
894 return 1; /* Send extension */
896 return -1; /* No serverinfo data found, don't send extension */
899 /* With a NULL context, this function just checks that the serverinfo data
900 parses correctly. With a non-NULL context, it registers callbacks for
901 the included extensions. */
902 static int serverinfo_process_buffer(const unsigned char *serverinfo,
903 size_t serverinfo_length, SSL_CTX *ctx)
905 if (serverinfo == NULL || serverinfo_length == 0)
909 unsigned int ext_type = 0;
912 /* end of serverinfo */
913 if (serverinfo_length == 0)
916 /* read 2-byte type field */
917 if (serverinfo_length < 2)
919 /* FIXME: check for types we understand explicitly? */
921 /* Register callbacks for extensions */
922 ext_type = (serverinfo[0] << 8) + serverinfo[1];
923 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
924 serverinfo_srv_add_cb,
926 serverinfo_srv_parse_cb,
931 serverinfo_length -= 2;
933 /* read 2-byte len field */
934 if (serverinfo_length < 2)
936 len = (serverinfo[0] << 8) + serverinfo[1];
938 serverinfo_length -= 2;
940 if (len > serverinfo_length)
944 serverinfo_length -= len;
948 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
949 size_t serverinfo_length)
951 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0)
953 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO,ERR_R_PASSED_NULL_PARAMETER);
956 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL))
958 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO,SSL_R_INVALID_SERVERINFO_DATA);
961 if (!ssl_cert_inst(&ctx->cert))
963 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO,ERR_R_MALLOC_FAILURE);
966 if (ctx->cert->key == NULL)
968 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO,ERR_R_INTERNAL_ERROR);
971 ctx->cert->key->serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
973 if (ctx->cert->key->serverinfo == NULL)
975 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO,ERR_R_MALLOC_FAILURE);
978 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
979 ctx->cert->key->serverinfo_length = serverinfo_length;
981 /* Now that the serverinfo is validated and stored, go ahead and
982 * register callbacks. */
983 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx))
985 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO,SSL_R_INVALID_SERVERINFO_DATA);
991 #ifndef OPENSSL_NO_STDIO
992 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
994 unsigned char *serverinfo = NULL;
995 size_t serverinfo_length = 0;
996 unsigned char* extension = 0;
997 long extension_length = 0;
1000 char namePrefix[] = "SERVERINFO FOR ";
1003 size_t num_extensions = 0;
1005 if (ctx == NULL || file == NULL)
1007 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,ERR_R_PASSED_NULL_PARAMETER);
1011 bin = BIO_new(BIO_s_file_internal());
1014 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
1017 if (BIO_read_filename(bin, file) <= 0)
1019 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
1023 for (num_extensions=0;; num_extensions++)
1025 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length) == 0)
1027 /* There must be at least one extension in this file */
1028 if (num_extensions == 0)
1030 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_NO_PEM_EXTENSIONS);
1033 else /* End of file, we're done */
1036 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
1037 if (strlen(name) < strlen(namePrefix))
1039 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
1042 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0)
1044 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_BAD_PREFIX);
1047 /* Check that the decoded PEM data is plausible (valid length field) */
1048 if (extension_length < 4 || (extension[2] << 8) + extension[3] != extension_length - 4)
1050 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1053 /* Append the decoded extension to the serverinfo buffer */
1054 serverinfo = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
1055 if (serverinfo == NULL)
1057 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1060 memcpy(serverinfo + serverinfo_length, extension, extension_length);
1061 serverinfo_length += extension_length;
1063 OPENSSL_free(name); name = NULL;
1064 OPENSSL_free(header); header = NULL;
1065 OPENSSL_free(extension); extension = NULL;
1068 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1070 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1072 OPENSSL_free(header);
1073 OPENSSL_free(extension);
1074 OPENSSL_free(serverinfo);
1079 #endif /* OPENSSL_NO_STDIO */
1080 #endif /* OPENSSL_NO_TLSEXT */