2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/bio.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
18 static int ssl_set_cert(CERT *c, X509 *x509);
19 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
20 int SSL_use_certificate(SSL *ssl, X509 *x)
24 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
27 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
29 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
33 return (ssl_set_cert(ssl->cert, x));
36 #ifndef OPENSSL_NO_STDIO
37 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
44 in = BIO_new(BIO_s_file());
46 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
50 if (BIO_read_filename(in, file) <= 0) {
51 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
54 if (type == SSL_FILETYPE_ASN1) {
56 x = d2i_X509_bio(in, NULL);
57 } else if (type == SSL_FILETYPE_PEM) {
59 x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
60 ssl->ctx->default_passwd_callback_userdata);
62 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
67 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
71 ret = SSL_use_certificate(ssl, x);
79 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
84 x = d2i_X509(NULL, &d, (long)len);
86 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
90 ret = SSL_use_certificate(ssl, x);
95 #ifndef OPENSSL_NO_RSA
96 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
102 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
105 if ((pkey = EVP_PKEY_new()) == NULL) {
106 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
111 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
117 ret = ssl_set_pkey(ssl->cert, pkey);
123 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
126 i = ssl_cert_type(NULL, pkey);
128 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
132 if (c->pkeys[i].x509 != NULL) {
134 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
136 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
140 * The return code from EVP_PKEY_copy_parameters is deliberately
141 * ignored. Some EVP_PKEY types cannot do this.
143 EVP_PKEY_copy_parameters(pktmp, pkey);
146 #ifndef OPENSSL_NO_RSA
148 * Don't check the public/private key, this is mostly for smart
151 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
152 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK);
155 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
156 X509_free(c->pkeys[i].x509);
157 c->pkeys[i].x509 = NULL;
162 EVP_PKEY_free(c->pkeys[i].privatekey);
163 EVP_PKEY_up_ref(pkey);
164 c->pkeys[i].privatekey = pkey;
165 c->key = &(c->pkeys[i]);
169 #ifndef OPENSSL_NO_RSA
170 # ifndef OPENSSL_NO_STDIO
171 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
177 in = BIO_new(BIO_s_file());
179 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
183 if (BIO_read_filename(in, file) <= 0) {
184 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
187 if (type == SSL_FILETYPE_ASN1) {
189 rsa = d2i_RSAPrivateKey_bio(in, NULL);
190 } else if (type == SSL_FILETYPE_PEM) {
192 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
193 ssl->ctx->default_passwd_callback,
195 ctx->default_passwd_callback_userdata);
197 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
201 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
204 ret = SSL_use_RSAPrivateKey(ssl, rsa);
212 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
215 const unsigned char *p;
219 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
220 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
224 ret = SSL_use_RSAPrivateKey(ssl, rsa);
228 #endif /* !OPENSSL_NO_RSA */
230 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
235 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
238 ret = ssl_set_pkey(ssl->cert, pkey);
242 #ifndef OPENSSL_NO_STDIO
243 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
247 EVP_PKEY *pkey = NULL;
249 in = BIO_new(BIO_s_file());
251 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
255 if (BIO_read_filename(in, file) <= 0) {
256 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
259 if (type == SSL_FILETYPE_PEM) {
261 pkey = PEM_read_bio_PrivateKey(in, NULL,
262 ssl->ctx->default_passwd_callback,
264 ctx->default_passwd_callback_userdata);
265 } else if (type == SSL_FILETYPE_ASN1) {
267 pkey = d2i_PrivateKey_bio(in, NULL);
269 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
273 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
276 ret = SSL_use_PrivateKey(ssl, pkey);
284 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
288 const unsigned char *p;
292 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
293 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
297 ret = SSL_use_PrivateKey(ssl, pkey);
302 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
306 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
309 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
311 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
314 return (ssl_set_cert(ctx->cert, x));
317 static int ssl_set_cert(CERT *c, X509 *x)
322 pkey = X509_get0_pubkey(x);
324 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
328 i = ssl_cert_type(x, pkey);
330 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
333 #ifndef OPENSSL_NO_EC
334 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
335 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
339 if (c->pkeys[i].privatekey != NULL) {
341 * The return code from EVP_PKEY_copy_parameters is deliberately
342 * ignored. Some EVP_PKEY types cannot do this.
344 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
347 #ifndef OPENSSL_NO_RSA
349 * Don't check the public/private key, this is mostly for smart
352 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
353 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
354 RSA_METHOD_FLAG_NO_CHECK) ;
356 #endif /* OPENSSL_NO_RSA */
357 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
359 * don't fail for a cert/key mismatch, just free current private
360 * key (when switching to a different cert & key, first this
361 * function should be used, then ssl_set_pkey
363 EVP_PKEY_free(c->pkeys[i].privatekey);
364 c->pkeys[i].privatekey = NULL;
365 /* clear error queue */
370 X509_free(c->pkeys[i].x509);
372 c->pkeys[i].x509 = x;
373 c->key = &(c->pkeys[i]);
378 #ifndef OPENSSL_NO_STDIO
379 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
386 in = BIO_new(BIO_s_file());
388 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
392 if (BIO_read_filename(in, file) <= 0) {
393 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
396 if (type == SSL_FILETYPE_ASN1) {
398 x = d2i_X509_bio(in, NULL);
399 } else if (type == SSL_FILETYPE_PEM) {
401 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
402 ctx->default_passwd_callback_userdata);
404 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
409 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
413 ret = SSL_CTX_use_certificate(ctx, x);
421 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
422 const unsigned char *d)
427 x = d2i_X509(NULL, &d, (long)len);
429 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
433 ret = SSL_CTX_use_certificate(ctx, x);
438 #ifndef OPENSSL_NO_RSA
439 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
445 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
448 if ((pkey = EVP_PKEY_new()) == NULL) {
449 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
454 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
460 ret = ssl_set_pkey(ctx->cert, pkey);
465 # ifndef OPENSSL_NO_STDIO
466 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
472 in = BIO_new(BIO_s_file());
474 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
478 if (BIO_read_filename(in, file) <= 0) {
479 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
482 if (type == SSL_FILETYPE_ASN1) {
484 rsa = d2i_RSAPrivateKey_bio(in, NULL);
485 } else if (type == SSL_FILETYPE_PEM) {
487 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
488 ctx->default_passwd_callback,
489 ctx->default_passwd_callback_userdata);
491 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
495 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
498 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
506 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
510 const unsigned char *p;
514 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
515 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
519 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
523 #endif /* !OPENSSL_NO_RSA */
525 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
528 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
531 return (ssl_set_pkey(ctx->cert, pkey));
534 #ifndef OPENSSL_NO_STDIO
535 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
539 EVP_PKEY *pkey = NULL;
541 in = BIO_new(BIO_s_file());
543 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
547 if (BIO_read_filename(in, file) <= 0) {
548 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
551 if (type == SSL_FILETYPE_PEM) {
553 pkey = PEM_read_bio_PrivateKey(in, NULL,
554 ctx->default_passwd_callback,
555 ctx->default_passwd_callback_userdata);
556 } else if (type == SSL_FILETYPE_ASN1) {
558 pkey = d2i_PrivateKey_bio(in, NULL);
560 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
564 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
567 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
575 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
576 const unsigned char *d, long len)
579 const unsigned char *p;
583 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
584 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
588 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
593 #ifndef OPENSSL_NO_STDIO
595 * Read a file that contains our certificate in "PEM" format, possibly
596 * followed by a sequence of CA certificates that should be sent to the peer
597 * in the Certificate message.
599 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
604 pem_password_cb *passwd_callback;
605 void *passwd_callback_userdata;
607 ERR_clear_error(); /* clear error stack for
608 * SSL_CTX_use_certificate() */
611 passwd_callback = ctx->default_passwd_callback;
612 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
614 passwd_callback = ssl->default_passwd_callback;
615 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
618 in = BIO_new(BIO_s_file());
620 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
624 if (BIO_read_filename(in, file) <= 0) {
625 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
629 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
630 passwd_callback_userdata);
632 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
637 ret = SSL_CTX_use_certificate(ctx, x);
639 ret = SSL_use_certificate(ssl, x);
641 if (ERR_peek_error() != 0)
642 ret = 0; /* Key/certificate mismatch doesn't imply
646 * If we could set up our certificate, now proceed to the CA
654 r = SSL_CTX_clear_chain_certs(ctx);
656 r = SSL_clear_chain_certs(ssl);
663 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
664 passwd_callback_userdata))
667 r = SSL_CTX_add0_chain_cert(ctx, ca);
669 r = SSL_add0_chain_cert(ssl, ca);
671 * Note that we must not free ca if it was successfully added to
672 * the chain (while we must free the main certificate, since its
673 * reference count is increased by SSL_CTX_use_certificate).
681 /* When the while loop ends, it's usually just EOF. */
682 err = ERR_peek_last_error();
683 if (ERR_GET_LIB(err) == ERR_LIB_PEM
684 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
687 ret = 0; /* some real error */
696 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
698 return use_certificate_chain_file(ctx, NULL, file);
701 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
703 return use_certificate_chain_file(NULL, ssl, file);
707 static int serverinfo_find_extension(const unsigned char *serverinfo,
708 size_t serverinfo_length,
709 unsigned int extension_type,
710 const unsigned char **extension_data,
711 size_t *extension_length)
713 *extension_data = NULL;
714 *extension_length = 0;
715 if (serverinfo == NULL || serverinfo_length == 0)
718 unsigned int type = 0;
721 /* end of serverinfo */
722 if (serverinfo_length == 0)
723 return 0; /* Extension not found */
725 /* read 2-byte type field */
726 if (serverinfo_length < 2)
727 return -1; /* Error */
728 type = (serverinfo[0] << 8) + serverinfo[1];
730 serverinfo_length -= 2;
732 /* read 2-byte len field */
733 if (serverinfo_length < 2)
734 return -1; /* Error */
735 len = (serverinfo[0] << 8) + serverinfo[1];
737 serverinfo_length -= 2;
739 if (len > serverinfo_length)
740 return -1; /* Error */
742 if (type == extension_type) {
743 *extension_data = serverinfo;
744 *extension_length = len;
745 return 1; /* Success */
749 serverinfo_length -= len;
754 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
755 const unsigned char *in,
756 size_t inlen, int *al, void *arg)
760 *al = SSL_AD_DECODE_ERROR;
767 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
768 const unsigned char **out, size_t *outlen,
771 const unsigned char *serverinfo = NULL;
772 size_t serverinfo_length = 0;
774 /* Is there serverinfo data for the chosen server cert? */
775 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
776 &serverinfo_length)) != 0) {
777 /* Find the relevant extension from the serverinfo */
778 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
779 ext_type, out, outlen);
781 *al = SSL_AD_DECODE_ERROR;
782 return -1; /* Error */
785 return 0; /* No extension found, don't send extension */
786 return 1; /* Send extension */
788 return 0; /* No serverinfo data found, don't send
793 * With a NULL context, this function just checks that the serverinfo data
794 * parses correctly. With a non-NULL context, it registers callbacks for
795 * the included extensions.
797 static int serverinfo_process_buffer(const unsigned char *serverinfo,
798 size_t serverinfo_length, SSL_CTX *ctx)
800 if (serverinfo == NULL || serverinfo_length == 0)
803 unsigned int ext_type = 0;
806 /* end of serverinfo */
807 if (serverinfo_length == 0)
810 /* read 2-byte type field */
811 if (serverinfo_length < 2)
813 /* FIXME: check for types we understand explicitly? */
815 /* Register callbacks for extensions */
816 ext_type = (serverinfo[0] << 8) + serverinfo[1];
818 int have_ext_cbs = 0;
820 custom_ext_methods *exts = &ctx->cert->srv_ext;
821 custom_ext_method *meth = exts->meths;
823 for (i = 0; i < exts->meths_count; i++, meth++) {
824 if (ext_type == meth->ext_type) {
830 if (!have_ext_cbs && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
831 serverinfo_srv_add_cb,
833 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 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
896 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());
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);
960 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
964 memcpy(serverinfo + serverinfo_length, extension, extension_length);
965 serverinfo_length += extension_length;
969 OPENSSL_free(header);
971 OPENSSL_free(extension);
975 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
977 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
979 OPENSSL_free(header);
980 OPENSSL_free(extension);
981 OPENSSL_free(serverinfo);