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());
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 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
165 ret = ssl_set_pkey(ssl->cert, pkey);
171 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
174 i = ssl_cert_type(NULL, pkey);
176 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
180 if (c->pkeys[i].x509 != NULL) {
182 pktmp = X509_get_pubkey(c->pkeys[i].x509);
184 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
185 EVP_PKEY_free(pktmp);
189 * The return code from EVP_PKEY_copy_parameters is deliberately
190 * ignored. Some EVP_PKEY types cannot do this.
192 EVP_PKEY_copy_parameters(pktmp, pkey);
193 EVP_PKEY_free(pktmp);
196 #ifndef OPENSSL_NO_RSA
198 * Don't check the public/private key, this is mostly for smart
201 if ((pkey->type == EVP_PKEY_RSA) &&
202 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
205 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
206 X509_free(c->pkeys[i].x509);
207 c->pkeys[i].x509 = NULL;
212 EVP_PKEY_free(c->pkeys[i].privatekey);
213 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
214 c->pkeys[i].privatekey = pkey;
215 c->key = &(c->pkeys[i]);
219 #ifndef OPENSSL_NO_RSA
220 # ifndef OPENSSL_NO_STDIO
221 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
227 in = BIO_new(BIO_s_file());
229 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
233 if (BIO_read_filename(in, file) <= 0) {
234 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
237 if (type == SSL_FILETYPE_ASN1) {
239 rsa = d2i_RSAPrivateKey_bio(in, NULL);
240 } else if (type == SSL_FILETYPE_PEM) {
242 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
243 ssl->ctx->default_passwd_callback,
245 ctx->default_passwd_callback_userdata);
247 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
251 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
254 ret = SSL_use_RSAPrivateKey(ssl, rsa);
262 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
265 const unsigned char *p;
269 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
270 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
274 ret = SSL_use_RSAPrivateKey(ssl, rsa);
278 #endif /* !OPENSSL_NO_RSA */
280 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
285 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
288 ret = ssl_set_pkey(ssl->cert, pkey);
292 #ifndef OPENSSL_NO_STDIO
293 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
297 EVP_PKEY *pkey = NULL;
299 in = BIO_new(BIO_s_file());
301 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
305 if (BIO_read_filename(in, file) <= 0) {
306 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
309 if (type == SSL_FILETYPE_PEM) {
311 pkey = PEM_read_bio_PrivateKey(in, NULL,
312 ssl->ctx->default_passwd_callback,
314 ctx->default_passwd_callback_userdata);
315 } else if (type == SSL_FILETYPE_ASN1) {
317 pkey = d2i_PrivateKey_bio(in, NULL);
319 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
323 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
326 ret = SSL_use_PrivateKey(ssl, pkey);
334 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
338 const unsigned char *p;
342 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
343 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
347 ret = SSL_use_PrivateKey(ssl, pkey);
352 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
356 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
359 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
361 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
364 return (ssl_set_cert(ctx->cert, x));
367 static int ssl_set_cert(CERT *c, X509 *x)
372 pkey = X509_get_pubkey(x);
374 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
378 i = ssl_cert_type(x, pkey);
380 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
385 if (c->pkeys[i].privatekey != NULL) {
387 * The return code from EVP_PKEY_copy_parameters is deliberately
388 * ignored. Some EVP_PKEY types cannot do this.
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);
420 c->pkeys[i].x509 = x;
421 c->key = &(c->pkeys[i]);
426 #ifndef OPENSSL_NO_STDIO
427 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
434 in = BIO_new(BIO_s_file());
436 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
440 if (BIO_read_filename(in, file) <= 0) {
441 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
444 if (type == SSL_FILETYPE_ASN1) {
446 x = d2i_X509_bio(in, NULL);
447 } else if (type == SSL_FILETYPE_PEM) {
449 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
450 ctx->default_passwd_callback_userdata);
452 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
457 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
461 ret = SSL_CTX_use_certificate(ctx, x);
469 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
470 const unsigned char *d)
475 x = d2i_X509(NULL, &d, (long)len);
477 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
481 ret = SSL_CTX_use_certificate(ctx, x);
486 #ifndef OPENSSL_NO_RSA
487 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
493 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
496 if ((pkey = EVP_PKEY_new()) == NULL) {
497 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
502 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
507 ret = ssl_set_pkey(ctx->cert, pkey);
512 # ifndef OPENSSL_NO_STDIO
513 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
519 in = BIO_new(BIO_s_file());
521 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
525 if (BIO_read_filename(in, file) <= 0) {
526 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
529 if (type == SSL_FILETYPE_ASN1) {
531 rsa = d2i_RSAPrivateKey_bio(in, NULL);
532 } else if (type == SSL_FILETYPE_PEM) {
534 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
535 ctx->default_passwd_callback,
536 ctx->default_passwd_callback_userdata);
538 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
542 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
545 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
553 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
557 const unsigned char *p;
561 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
562 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
566 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
570 #endif /* !OPENSSL_NO_RSA */
572 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
575 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
578 return (ssl_set_pkey(ctx->cert, pkey));
581 #ifndef OPENSSL_NO_STDIO
582 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
586 EVP_PKEY *pkey = NULL;
588 in = BIO_new(BIO_s_file());
590 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
594 if (BIO_read_filename(in, file) <= 0) {
595 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
598 if (type == SSL_FILETYPE_PEM) {
600 pkey = PEM_read_bio_PrivateKey(in, NULL,
601 ctx->default_passwd_callback,
602 ctx->default_passwd_callback_userdata);
603 } else if (type == SSL_FILETYPE_ASN1) {
605 pkey = d2i_PrivateKey_bio(in, NULL);
607 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
611 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
614 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
622 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
623 const unsigned char *d, long len)
626 const unsigned char *p;
630 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
631 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
635 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
640 #ifndef OPENSSL_NO_STDIO
642 * Read a file that contains our certificate in "PEM" format, possibly
643 * followed by a sequence of CA certificates that should be sent to the peer
644 * in the Certificate message.
646 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
651 pem_password_cb *passwd_callback;
652 void *passwd_callback_userdata;
654 ERR_clear_error(); /* clear error stack for
655 * SSL_CTX_use_certificate() */
658 passwd_callback = ctx->default_passwd_callback;
659 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
661 passwd_callback = ssl->default_passwd_callback;
662 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
665 in = BIO_new(BIO_s_file());
667 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
671 if (BIO_read_filename(in, file) <= 0) {
672 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
676 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
677 passwd_callback_userdata);
679 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
684 ret = SSL_CTX_use_certificate(ctx, x);
686 ret = SSL_use_certificate(ssl, x);
688 if (ERR_peek_error() != 0)
689 ret = 0; /* Key/certificate mismatch doesn't imply
693 * If we could set up our certificate, now proceed to the CA
701 r = SSL_CTX_clear_chain_certs(ctx);
703 r = SSL_clear_chain_certs(ssl);
710 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
711 passwd_callback_userdata))
714 r = SSL_CTX_add0_chain_cert(ctx, ca);
716 r = SSL_add0_chain_cert(ssl, ca);
718 * Note that we must not free ca if it was successfully added to
719 * the chain (while we must free the main certificate, since its
720 * reference count is increased by SSL_CTX_use_certificate).
728 /* When the while loop ends, it's usually just EOF. */
729 err = ERR_peek_last_error();
730 if (ERR_GET_LIB(err) == ERR_LIB_PEM
731 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
734 ret = 0; /* some real error */
743 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
745 return use_certificate_chain_file(ctx, NULL, file);
748 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
750 return use_certificate_chain_file(NULL, ssl, file);
754 static int serverinfo_find_extension(const unsigned char *serverinfo,
755 size_t serverinfo_length,
756 unsigned int extension_type,
757 const unsigned char **extension_data,
758 size_t *extension_length)
760 *extension_data = NULL;
761 *extension_length = 0;
762 if (serverinfo == NULL || serverinfo_length == 0)
765 unsigned int type = 0;
768 /* end of serverinfo */
769 if (serverinfo_length == 0)
770 return 0; /* Extension not found */
772 /* read 2-byte type field */
773 if (serverinfo_length < 2)
774 return -1; /* Error */
775 type = (serverinfo[0] << 8) + serverinfo[1];
777 serverinfo_length -= 2;
779 /* read 2-byte len field */
780 if (serverinfo_length < 2)
781 return -1; /* Error */
782 len = (serverinfo[0] << 8) + serverinfo[1];
784 serverinfo_length -= 2;
786 if (len > serverinfo_length)
787 return -1; /* Error */
789 if (type == extension_type) {
790 *extension_data = serverinfo;
791 *extension_length = len;
792 return 1; /* Success */
796 serverinfo_length -= len;
801 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
802 const unsigned char *in,
803 size_t inlen, int *al, void *arg)
807 *al = SSL_AD_DECODE_ERROR;
814 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
815 const unsigned char **out, size_t *outlen,
818 const unsigned char *serverinfo = NULL;
819 size_t serverinfo_length = 0;
821 /* Is there serverinfo data for the chosen server cert? */
822 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
823 &serverinfo_length)) != 0) {
824 /* Find the relevant extension from the serverinfo */
825 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
826 ext_type, out, outlen);
828 *al = SSL_AD_DECODE_ERROR;
829 return -1; /* Error */
832 return 0; /* No extension found, don't send extension */
833 return 1; /* Send extension */
835 return -1; /* No serverinfo data found, don't send
840 * With a NULL context, this function just checks that the serverinfo data
841 * parses correctly. With a non-NULL context, it registers callbacks for
842 * the included extensions.
844 static int serverinfo_process_buffer(const unsigned char *serverinfo,
845 size_t serverinfo_length, SSL_CTX *ctx)
847 if (serverinfo == NULL || serverinfo_length == 0)
850 unsigned int ext_type = 0;
853 /* end of serverinfo */
854 if (serverinfo_length == 0)
857 /* read 2-byte type field */
858 if (serverinfo_length < 2)
860 /* FIXME: check for types we understand explicitly? */
862 /* Register callbacks for extensions */
863 ext_type = (serverinfo[0] << 8) + serverinfo[1];
864 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
865 serverinfo_srv_add_cb,
867 serverinfo_srv_parse_cb,
872 serverinfo_length -= 2;
874 /* read 2-byte len field */
875 if (serverinfo_length < 2)
877 len = (serverinfo[0] << 8) + serverinfo[1];
879 serverinfo_length -= 2;
881 if (len > serverinfo_length)
885 serverinfo_length -= len;
889 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
890 size_t serverinfo_length)
892 unsigned char *new_serverinfo;
894 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
895 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
898 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
899 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
902 if (ctx->cert->key == NULL) {
903 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
906 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
908 if (new_serverinfo == NULL) {
909 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
912 ctx->cert->key->serverinfo = new_serverinfo;
913 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
914 ctx->cert->key->serverinfo_length = serverinfo_length;
917 * Now that the serverinfo is validated and stored, go ahead and
918 * register callbacks.
920 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
921 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
927 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
929 unsigned char *serverinfo = NULL;
930 size_t serverinfo_length = 0;
931 unsigned char *extension = 0;
932 long extension_length = 0;
935 char namePrefix[] = "SERVERINFO FOR ";
938 size_t num_extensions = 0;
940 if (ctx == NULL || file == NULL) {
941 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
942 ERR_R_PASSED_NULL_PARAMETER);
946 bin = BIO_new(BIO_s_file());
948 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
951 if (BIO_read_filename(bin, file) <= 0) {
952 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
956 for (num_extensions = 0;; num_extensions++) {
957 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
960 * There must be at least one extension in this file
962 if (num_extensions == 0) {
963 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
964 SSL_R_NO_PEM_EXTENSIONS);
966 } else /* End of file, we're done */
969 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
970 if (strlen(name) < strlen(namePrefix)) {
971 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
972 SSL_R_PEM_NAME_TOO_SHORT);
975 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
976 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
977 SSL_R_PEM_NAME_BAD_PREFIX);
981 * Check that the decoded PEM data is plausible (valid length field)
983 if (extension_length < 4
984 || (extension[2] << 8) + extension[3] != extension_length - 4) {
985 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
988 /* Append the decoded extension to the serverinfo buffer */
990 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
991 if (serverinfo == NULL) {
992 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
995 memcpy(serverinfo + serverinfo_length, extension, extension_length);
996 serverinfo_length += extension_length;
1000 OPENSSL_free(header);
1002 OPENSSL_free(extension);
1006 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1008 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1010 OPENSSL_free(header);
1011 OPENSSL_free(extension);
1012 OPENSSL_free(serverinfo);