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 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
43 in = BIO_new(BIO_s_file());
45 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
49 if (BIO_read_filename(in, file) <= 0) {
50 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
53 if (type == SSL_FILETYPE_ASN1) {
55 x = d2i_X509_bio(in, NULL);
56 } else if (type == SSL_FILETYPE_PEM) {
58 x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
59 ssl->ctx->default_passwd_callback_userdata);
61 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
66 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
70 ret = SSL_use_certificate(ssl, x);
77 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
82 x = d2i_X509(NULL, &d, (long)len);
84 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
88 ret = SSL_use_certificate(ssl, x);
93 #ifndef OPENSSL_NO_RSA
94 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
100 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
103 if ((pkey = EVP_PKEY_new()) == NULL) {
104 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
109 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
115 ret = ssl_set_pkey(ssl->cert, pkey);
121 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
124 i = ssl_cert_type(NULL, pkey);
126 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
130 if (c->pkeys[i].x509 != NULL) {
132 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
134 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
138 * The return code from EVP_PKEY_copy_parameters is deliberately
139 * ignored. Some EVP_PKEY types cannot do this.
141 EVP_PKEY_copy_parameters(pktmp, pkey);
144 #ifndef OPENSSL_NO_RSA
146 * Don't check the public/private key, this is mostly for smart
149 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
150 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK);
153 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
154 X509_free(c->pkeys[i].x509);
155 c->pkeys[i].x509 = NULL;
160 EVP_PKEY_free(c->pkeys[i].privatekey);
161 EVP_PKEY_up_ref(pkey);
162 c->pkeys[i].privatekey = pkey;
163 c->key = &(c->pkeys[i]);
167 #ifndef OPENSSL_NO_RSA
168 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
174 in = BIO_new(BIO_s_file());
176 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
180 if (BIO_read_filename(in, file) <= 0) {
181 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
184 if (type == SSL_FILETYPE_ASN1) {
186 rsa = d2i_RSAPrivateKey_bio(in, NULL);
187 } else if (type == SSL_FILETYPE_PEM) {
189 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
190 ssl->ctx->default_passwd_callback,
192 ctx->default_passwd_callback_userdata);
194 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
198 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
201 ret = SSL_use_RSAPrivateKey(ssl, rsa);
208 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
211 const unsigned char *p;
215 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
216 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
220 ret = SSL_use_RSAPrivateKey(ssl, rsa);
224 #endif /* !OPENSSL_NO_RSA */
226 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
231 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
234 ret = ssl_set_pkey(ssl->cert, pkey);
238 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
242 EVP_PKEY *pkey = NULL;
244 in = BIO_new(BIO_s_file());
246 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
250 if (BIO_read_filename(in, file) <= 0) {
251 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
254 if (type == SSL_FILETYPE_PEM) {
256 pkey = PEM_read_bio_PrivateKey(in, NULL,
257 ssl->ctx->default_passwd_callback,
259 ctx->default_passwd_callback_userdata);
260 } else if (type == SSL_FILETYPE_ASN1) {
262 pkey = d2i_PrivateKey_bio(in, NULL);
264 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
268 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
271 ret = SSL_use_PrivateKey(ssl, pkey);
278 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
282 const unsigned char *p;
286 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
287 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
291 ret = SSL_use_PrivateKey(ssl, pkey);
296 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
300 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
303 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
305 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
308 return (ssl_set_cert(ctx->cert, x));
311 static int ssl_set_cert(CERT *c, X509 *x)
316 pkey = X509_get0_pubkey(x);
318 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
322 i = ssl_cert_type(x, pkey);
324 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
327 #ifndef OPENSSL_NO_EC
328 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
329 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
333 if (c->pkeys[i].privatekey != NULL) {
335 * The return code from EVP_PKEY_copy_parameters is deliberately
336 * ignored. Some EVP_PKEY types cannot do this.
338 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
341 #ifndef OPENSSL_NO_RSA
343 * Don't check the public/private key, this is mostly for smart
346 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
347 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
348 RSA_METHOD_FLAG_NO_CHECK) ;
350 #endif /* OPENSSL_NO_RSA */
351 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
353 * don't fail for a cert/key mismatch, just free current private
354 * key (when switching to a different cert & key, first this
355 * function should be used, then ssl_set_pkey
357 EVP_PKEY_free(c->pkeys[i].privatekey);
358 c->pkeys[i].privatekey = NULL;
359 /* clear error queue */
364 X509_free(c->pkeys[i].x509);
366 c->pkeys[i].x509 = x;
367 c->key = &(c->pkeys[i]);
372 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
379 in = BIO_new(BIO_s_file());
381 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
385 if (BIO_read_filename(in, file) <= 0) {
386 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
389 if (type == SSL_FILETYPE_ASN1) {
391 x = d2i_X509_bio(in, NULL);
392 } else if (type == SSL_FILETYPE_PEM) {
394 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
395 ctx->default_passwd_callback_userdata);
397 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
402 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
406 ret = SSL_CTX_use_certificate(ctx, x);
413 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
414 const unsigned char *d)
419 x = d2i_X509(NULL, &d, (long)len);
421 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
425 ret = SSL_CTX_use_certificate(ctx, x);
430 #ifndef OPENSSL_NO_RSA
431 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
437 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
440 if ((pkey = EVP_PKEY_new()) == NULL) {
441 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
446 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
452 ret = ssl_set_pkey(ctx->cert, pkey);
457 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
463 in = BIO_new(BIO_s_file());
465 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
469 if (BIO_read_filename(in, file) <= 0) {
470 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
473 if (type == SSL_FILETYPE_ASN1) {
475 rsa = d2i_RSAPrivateKey_bio(in, NULL);
476 } else if (type == SSL_FILETYPE_PEM) {
478 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
479 ctx->default_passwd_callback,
480 ctx->default_passwd_callback_userdata);
482 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
486 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
489 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
496 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
500 const unsigned char *p;
504 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
505 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
509 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
513 #endif /* !OPENSSL_NO_RSA */
515 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
518 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
521 return (ssl_set_pkey(ctx->cert, pkey));
524 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
528 EVP_PKEY *pkey = NULL;
530 in = BIO_new(BIO_s_file());
532 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
536 if (BIO_read_filename(in, file) <= 0) {
537 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
540 if (type == SSL_FILETYPE_PEM) {
542 pkey = PEM_read_bio_PrivateKey(in, NULL,
543 ctx->default_passwd_callback,
544 ctx->default_passwd_callback_userdata);
545 } else if (type == SSL_FILETYPE_ASN1) {
547 pkey = d2i_PrivateKey_bio(in, NULL);
549 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
553 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
556 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
563 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
564 const unsigned char *d, long len)
567 const unsigned char *p;
571 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
572 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
576 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
582 * Read a file that contains our certificate in "PEM" format, possibly
583 * followed by a sequence of CA certificates that should be sent to the peer
584 * in the Certificate message.
586 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
591 pem_password_cb *passwd_callback;
592 void *passwd_callback_userdata;
594 ERR_clear_error(); /* clear error stack for
595 * SSL_CTX_use_certificate() */
598 passwd_callback = ctx->default_passwd_callback;
599 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
601 passwd_callback = ssl->default_passwd_callback;
602 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
605 in = BIO_new(BIO_s_file());
607 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
611 if (BIO_read_filename(in, file) <= 0) {
612 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
616 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
617 passwd_callback_userdata);
619 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
624 ret = SSL_CTX_use_certificate(ctx, x);
626 ret = SSL_use_certificate(ssl, x);
628 if (ERR_peek_error() != 0)
629 ret = 0; /* Key/certificate mismatch doesn't imply
633 * If we could set up our certificate, now proceed to the CA
641 r = SSL_CTX_clear_chain_certs(ctx);
643 r = SSL_clear_chain_certs(ssl);
650 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
651 passwd_callback_userdata))
654 r = SSL_CTX_add0_chain_cert(ctx, ca);
656 r = SSL_add0_chain_cert(ssl, ca);
658 * Note that we must not free ca if it was successfully added to
659 * the chain (while we must free the main certificate, since its
660 * reference count is increased by SSL_CTX_use_certificate).
668 /* When the while loop ends, it's usually just EOF. */
669 err = ERR_peek_last_error();
670 if (ERR_GET_LIB(err) == ERR_LIB_PEM
671 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
674 ret = 0; /* some real error */
683 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
685 return use_certificate_chain_file(ctx, NULL, file);
688 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
690 return use_certificate_chain_file(NULL, ssl, file);
693 static int serverinfo_find_extension(const unsigned char *serverinfo,
694 size_t serverinfo_length,
695 unsigned int extension_type,
696 const unsigned char **extension_data,
697 size_t *extension_length)
699 *extension_data = NULL;
700 *extension_length = 0;
701 if (serverinfo == NULL || serverinfo_length == 0)
704 unsigned int type = 0;
707 /* end of serverinfo */
708 if (serverinfo_length == 0)
709 return 0; /* Extension not found */
711 /* read 2-byte type field */
712 if (serverinfo_length < 2)
713 return -1; /* Error */
714 type = (serverinfo[0] << 8) + serverinfo[1];
716 serverinfo_length -= 2;
718 /* read 2-byte len field */
719 if (serverinfo_length < 2)
720 return -1; /* Error */
721 len = (serverinfo[0] << 8) + serverinfo[1];
723 serverinfo_length -= 2;
725 if (len > serverinfo_length)
726 return -1; /* Error */
728 if (type == extension_type) {
729 *extension_data = serverinfo;
730 *extension_length = len;
731 return 1; /* Success */
735 serverinfo_length -= len;
740 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
741 const unsigned char *in,
742 size_t inlen, int *al, void *arg)
746 *al = SSL_AD_DECODE_ERROR;
753 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
754 const unsigned char **out, size_t *outlen,
757 const unsigned char *serverinfo = NULL;
758 size_t serverinfo_length = 0;
760 /* Is there serverinfo data for the chosen server cert? */
761 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
762 &serverinfo_length)) != 0) {
763 /* Find the relevant extension from the serverinfo */
764 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
765 ext_type, out, outlen);
767 *al = SSL_AD_DECODE_ERROR;
768 return -1; /* Error */
771 return 0; /* No extension found, don't send extension */
772 return 1; /* Send extension */
774 return 0; /* No serverinfo data found, don't send
779 * With a NULL context, this function just checks that the serverinfo data
780 * parses correctly. With a non-NULL context, it registers callbacks for
781 * the included extensions.
783 static int serverinfo_process_buffer(const unsigned char *serverinfo,
784 size_t serverinfo_length, SSL_CTX *ctx)
786 if (serverinfo == NULL || serverinfo_length == 0)
789 unsigned int ext_type = 0;
792 /* end of serverinfo */
793 if (serverinfo_length == 0)
796 /* read 2-byte type field */
797 if (serverinfo_length < 2)
799 /* FIXME: check for types we understand explicitly? */
801 /* Register callbacks for extensions */
802 ext_type = (serverinfo[0] << 8) + serverinfo[1];
804 int have_ext_cbs = 0;
806 custom_ext_methods *exts = &ctx->cert->srv_ext;
807 custom_ext_method *meth = exts->meths;
809 for (i = 0; i < exts->meths_count; i++, meth++) {
810 if (ext_type == meth->ext_type) {
816 if (!have_ext_cbs && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
817 serverinfo_srv_add_cb,
819 serverinfo_srv_parse_cb,
825 serverinfo_length -= 2;
827 /* read 2-byte len field */
828 if (serverinfo_length < 2)
830 len = (serverinfo[0] << 8) + serverinfo[1];
832 serverinfo_length -= 2;
834 if (len > serverinfo_length)
838 serverinfo_length -= len;
842 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
843 size_t serverinfo_length)
845 unsigned char *new_serverinfo;
847 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
848 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
851 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
852 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
855 if (ctx->cert->key == NULL) {
856 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
859 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
861 if (new_serverinfo == NULL) {
862 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
865 ctx->cert->key->serverinfo = new_serverinfo;
866 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
867 ctx->cert->key->serverinfo_length = serverinfo_length;
870 * Now that the serverinfo is validated and stored, go ahead and
871 * register callbacks.
873 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
874 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
880 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
882 unsigned char *serverinfo = NULL;
884 size_t serverinfo_length = 0;
885 unsigned char *extension = 0;
886 long extension_length = 0;
889 char namePrefix[] = "SERVERINFO FOR ";
892 size_t num_extensions = 0;
894 if (ctx == NULL || file == NULL) {
895 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
896 ERR_R_PASSED_NULL_PARAMETER);
900 bin = BIO_new(BIO_s_file());
902 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
905 if (BIO_read_filename(bin, file) <= 0) {
906 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
910 for (num_extensions = 0;; num_extensions++) {
911 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
914 * There must be at least one extension in this file
916 if (num_extensions == 0) {
917 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
918 SSL_R_NO_PEM_EXTENSIONS);
920 } else /* End of file, we're done */
923 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
924 if (strlen(name) < strlen(namePrefix)) {
925 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
926 SSL_R_PEM_NAME_TOO_SHORT);
929 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
930 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
931 SSL_R_PEM_NAME_BAD_PREFIX);
935 * Check that the decoded PEM data is plausible (valid length field)
937 if (extension_length < 4
938 || (extension[2] << 8) + extension[3] != extension_length - 4) {
939 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
942 /* Append the decoded extension to the serverinfo buffer */
944 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
946 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
950 memcpy(serverinfo + serverinfo_length, extension, extension_length);
951 serverinfo_length += extension_length;
955 OPENSSL_free(header);
957 OPENSSL_free(extension);
961 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
963 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
965 OPENSSL_free(header);
966 OPENSSL_free(extension);
967 OPENSSL_free(serverinfo);