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->default_passwd_callback,
59 ssl->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->default_passwd_callback,
191 ssl->default_passwd_callback_userdata);
193 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
197 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
200 ret = SSL_use_RSAPrivateKey(ssl, rsa);
207 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
210 const unsigned char *p;
214 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
215 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
219 ret = SSL_use_RSAPrivateKey(ssl, rsa);
223 #endif /* !OPENSSL_NO_RSA */
225 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
230 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
233 ret = ssl_set_pkey(ssl->cert, pkey);
237 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
241 EVP_PKEY *pkey = NULL;
243 in = BIO_new(BIO_s_file());
245 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
249 if (BIO_read_filename(in, file) <= 0) {
250 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
253 if (type == SSL_FILETYPE_PEM) {
255 pkey = PEM_read_bio_PrivateKey(in, NULL,
256 ssl->default_passwd_callback,
257 ssl->default_passwd_callback_userdata);
258 } else if (type == SSL_FILETYPE_ASN1) {
260 pkey = d2i_PrivateKey_bio(in, NULL);
262 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
266 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
269 ret = SSL_use_PrivateKey(ssl, pkey);
276 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
280 const unsigned char *p;
284 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
285 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
289 ret = SSL_use_PrivateKey(ssl, pkey);
294 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
298 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
301 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
303 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
306 return (ssl_set_cert(ctx->cert, x));
309 static int ssl_set_cert(CERT *c, X509 *x)
314 pkey = X509_get0_pubkey(x);
316 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
320 i = ssl_cert_type(x, pkey);
322 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
325 #ifndef OPENSSL_NO_EC
326 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
327 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
331 if (c->pkeys[i].privatekey != NULL) {
333 * The return code from EVP_PKEY_copy_parameters is deliberately
334 * ignored. Some EVP_PKEY types cannot do this.
336 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
339 #ifndef OPENSSL_NO_RSA
341 * Don't check the public/private key, this is mostly for smart
344 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
345 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
346 RSA_METHOD_FLAG_NO_CHECK) ;
348 #endif /* OPENSSL_NO_RSA */
349 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
351 * don't fail for a cert/key mismatch, just free current private
352 * key (when switching to a different cert & key, first this
353 * function should be used, then ssl_set_pkey
355 EVP_PKEY_free(c->pkeys[i].privatekey);
356 c->pkeys[i].privatekey = NULL;
357 /* clear error queue */
362 X509_free(c->pkeys[i].x509);
364 c->pkeys[i].x509 = x;
365 c->key = &(c->pkeys[i]);
370 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
377 in = BIO_new(BIO_s_file());
379 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
383 if (BIO_read_filename(in, file) <= 0) {
384 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
387 if (type == SSL_FILETYPE_ASN1) {
389 x = d2i_X509_bio(in, NULL);
390 } else if (type == SSL_FILETYPE_PEM) {
392 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
393 ctx->default_passwd_callback_userdata);
395 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
400 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
404 ret = SSL_CTX_use_certificate(ctx, x);
411 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
416 x = d2i_X509(NULL, &d, (long)len);
418 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
422 ret = SSL_CTX_use_certificate(ctx, x);
427 #ifndef OPENSSL_NO_RSA
428 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
434 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
437 if ((pkey = EVP_PKEY_new()) == NULL) {
438 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
443 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
449 ret = ssl_set_pkey(ctx->cert, pkey);
454 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
460 in = BIO_new(BIO_s_file());
462 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
466 if (BIO_read_filename(in, file) <= 0) {
467 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
470 if (type == SSL_FILETYPE_ASN1) {
472 rsa = d2i_RSAPrivateKey_bio(in, NULL);
473 } else if (type == SSL_FILETYPE_PEM) {
475 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
476 ctx->default_passwd_callback,
477 ctx->default_passwd_callback_userdata);
479 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
483 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
486 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
493 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
497 const unsigned char *p;
501 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
502 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
506 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
510 #endif /* !OPENSSL_NO_RSA */
512 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
515 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
518 return (ssl_set_pkey(ctx->cert, pkey));
521 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
525 EVP_PKEY *pkey = NULL;
527 in = BIO_new(BIO_s_file());
529 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
533 if (BIO_read_filename(in, file) <= 0) {
534 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
537 if (type == SSL_FILETYPE_PEM) {
539 pkey = PEM_read_bio_PrivateKey(in, NULL,
540 ctx->default_passwd_callback,
541 ctx->default_passwd_callback_userdata);
542 } else if (type == SSL_FILETYPE_ASN1) {
544 pkey = d2i_PrivateKey_bio(in, NULL);
546 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
550 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
553 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
560 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
561 const unsigned char *d, long len)
564 const unsigned char *p;
568 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
569 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
573 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
579 * Read a file that contains our certificate in "PEM" format, possibly
580 * followed by a sequence of CA certificates that should be sent to the peer
581 * in the Certificate message.
583 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
588 pem_password_cb *passwd_callback;
589 void *passwd_callback_userdata;
591 ERR_clear_error(); /* clear error stack for
592 * SSL_CTX_use_certificate() */
595 passwd_callback = ctx->default_passwd_callback;
596 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
598 passwd_callback = ssl->default_passwd_callback;
599 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
602 in = BIO_new(BIO_s_file());
604 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
608 if (BIO_read_filename(in, file) <= 0) {
609 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
613 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
614 passwd_callback_userdata);
616 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
621 ret = SSL_CTX_use_certificate(ctx, x);
623 ret = SSL_use_certificate(ssl, x);
625 if (ERR_peek_error() != 0)
626 ret = 0; /* Key/certificate mismatch doesn't imply
630 * If we could set up our certificate, now proceed to the CA
638 r = SSL_CTX_clear_chain_certs(ctx);
640 r = SSL_clear_chain_certs(ssl);
647 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
648 passwd_callback_userdata))
651 r = SSL_CTX_add0_chain_cert(ctx, ca);
653 r = SSL_add0_chain_cert(ssl, ca);
655 * Note that we must not free ca if it was successfully added to
656 * the chain (while we must free the main certificate, since its
657 * reference count is increased by SSL_CTX_use_certificate).
665 /* When the while loop ends, it's usually just EOF. */
666 err = ERR_peek_last_error();
667 if (ERR_GET_LIB(err) == ERR_LIB_PEM
668 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
671 ret = 0; /* some real error */
680 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
682 return use_certificate_chain_file(ctx, NULL, file);
685 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
687 return use_certificate_chain_file(NULL, ssl, file);
690 static int serverinfo_find_extension(const unsigned char *serverinfo,
691 size_t serverinfo_length,
692 unsigned int extension_type,
693 const unsigned char **extension_data,
694 size_t *extension_length)
696 *extension_data = NULL;
697 *extension_length = 0;
698 if (serverinfo == NULL || serverinfo_length == 0)
701 unsigned int type = 0;
704 /* end of serverinfo */
705 if (serverinfo_length == 0)
706 return 0; /* Extension not found */
708 /* read 2-byte type field */
709 if (serverinfo_length < 2)
710 return -1; /* Error */
711 type = (serverinfo[0] << 8) + serverinfo[1];
713 serverinfo_length -= 2;
715 /* read 2-byte len field */
716 if (serverinfo_length < 2)
717 return -1; /* Error */
718 len = (serverinfo[0] << 8) + serverinfo[1];
720 serverinfo_length -= 2;
722 if (len > serverinfo_length)
723 return -1; /* Error */
725 if (type == extension_type) {
726 *extension_data = serverinfo;
727 *extension_length = len;
728 return 1; /* Success */
732 serverinfo_length -= len;
737 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
738 const unsigned char *in,
739 size_t inlen, int *al, void *arg)
743 *al = SSL_AD_DECODE_ERROR;
750 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
751 const unsigned char **out, size_t *outlen,
754 const unsigned char *serverinfo = NULL;
755 size_t serverinfo_length = 0;
757 /* Is there serverinfo data for the chosen server cert? */
758 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
759 &serverinfo_length)) != 0) {
760 /* Find the relevant extension from the serverinfo */
761 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
762 ext_type, out, outlen);
764 *al = SSL_AD_DECODE_ERROR;
765 return -1; /* Error */
768 return 0; /* No extension found, don't send extension */
769 return 1; /* Send extension */
771 return 0; /* No serverinfo data found, don't send
776 * With a NULL context, this function just checks that the serverinfo data
777 * parses correctly. With a non-NULL context, it registers callbacks for
778 * the included extensions.
780 static int serverinfo_process_buffer(const unsigned char *serverinfo,
781 size_t serverinfo_length, SSL_CTX *ctx)
783 if (serverinfo == NULL || serverinfo_length == 0)
786 unsigned int ext_type = 0;
789 /* end of serverinfo */
790 if (serverinfo_length == 0)
793 /* read 2-byte type field */
794 if (serverinfo_length < 2)
796 /* FIXME: check for types we understand explicitly? */
798 /* Register callbacks for extensions */
799 ext_type = (serverinfo[0] << 8) + serverinfo[1];
801 && custom_ext_find(&ctx->cert->custext, ENDPOINT_SERVER,
804 && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
805 serverinfo_srv_add_cb,
807 serverinfo_srv_parse_cb,
812 serverinfo_length -= 2;
814 /* read 2-byte len field */
815 if (serverinfo_length < 2)
817 len = (serverinfo[0] << 8) + serverinfo[1];
819 serverinfo_length -= 2;
821 if (len > serverinfo_length)
825 serverinfo_length -= len;
829 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
830 size_t serverinfo_length)
832 unsigned char *new_serverinfo;
834 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
835 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
838 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
839 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
842 if (ctx->cert->key == NULL) {
843 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
846 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
848 if (new_serverinfo == NULL) {
849 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
852 ctx->cert->key->serverinfo = new_serverinfo;
853 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
854 ctx->cert->key->serverinfo_length = serverinfo_length;
857 * Now that the serverinfo is validated and stored, go ahead and
858 * register callbacks.
860 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
861 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
867 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
869 unsigned char *serverinfo = NULL;
871 size_t serverinfo_length = 0;
872 unsigned char *extension = 0;
873 long extension_length = 0;
876 char namePrefix[] = "SERVERINFO FOR ";
879 size_t num_extensions = 0;
881 if (ctx == NULL || file == NULL) {
882 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
886 bin = BIO_new(BIO_s_file());
888 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
891 if (BIO_read_filename(bin, file) <= 0) {
892 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
896 for (num_extensions = 0;; num_extensions++) {
897 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
900 * There must be at least one extension in this file
902 if (num_extensions == 0) {
903 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
904 SSL_R_NO_PEM_EXTENSIONS);
906 } else /* End of file, we're done */
909 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
910 if (strlen(name) < strlen(namePrefix)) {
911 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
914 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
916 SSL_R_PEM_NAME_BAD_PREFIX);
920 * Check that the decoded PEM data is plausible (valid length field)
922 if (extension_length < 4
923 || (extension[2] << 8) + extension[3] != extension_length - 4) {
924 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
927 /* Append the decoded extension to the serverinfo buffer */
928 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
930 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
934 memcpy(serverinfo + serverinfo_length, extension, extension_length);
935 serverinfo_length += extension_length;
939 OPENSSL_free(header);
941 OPENSSL_free(extension);
945 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
947 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
949 OPENSSL_free(header);
950 OPENSSL_free(extension);
951 OPENSSL_free(serverinfo);