1 /*********************************************************************
5 * Purpose : File with TLS/SSL extension. Contains methods for
6 * creating, using and closing TLS/SSL connections.
8 * Copyright : Written by and Copyright (c) 2020 Maxim Antonov <mantonov@gmail.com>
10 * This program is free software; you can redistribute it
11 * and/or modify it under the terms of the GNU General
12 * Public License as published by the Free Software
13 * Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will
17 * be useful, but WITHOUT ANY WARRANTY; without even the
18 * implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU General Public
20 * License for more details.
22 * The GNU General Public License should be included with
23 * this file. If not, you can view it at
24 * http://www.gnu.org/copyleft/gpl.html
25 * or write to the Free Software Foundation, Inc., 59
26 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 *********************************************************************/
33 #include <openssl/bn.h>
34 #include <openssl/opensslv.h>
35 #include <openssl/pem.h>
36 #include <openssl/md5.h>
37 #include <openssl/x509v3.h>
46 #include "ssl_common.h"
49 * Macros for openssl.c
51 #define CERTIFICATE_BASIC_CONSTRAINTS "CA:FALSE"
52 #define CERTIFICATE_SUBJECT_KEY "hash"
53 #define CERTIFICATE_AUTHORITY_KEY "keyid:always"
54 #define CERTIFICATE_ALT_NAME_PREFIX "DNS:"
55 #define CERTIFICATE_VERSION 2
56 #define VALID_DATETIME_FMT "%Y%m%d%H%M%SZ"
57 #define VALID_DATETIME_BUFLEN 16
59 static int generate_webpage_certificate(struct client_state *csp);
60 static void free_client_ssl_structures(struct client_state *csp);
61 static void free_server_ssl_structures(struct client_state *csp);
62 static int ssl_store_cert(struct client_state *csp, X509* crt);
63 static void log_ssl_errors(int debuglevel, const char* fmt, ...) __attribute__((format(printf, 2, 3)));
65 static int ssl_inited = 0;
67 /*********************************************************************
69 * Function : openssl_init
71 * Description : Initializes OpenSSL library once
77 *********************************************************************/
78 static void openssl_init(void)
82 privoxy_mutex_lock(&ssl_init_mutex);
85 #if OPENSSL_VERSION_NUMBER < 0x10100000L
88 OPENSSL_init_ssl(0, NULL);
90 SSL_load_error_strings();
91 OpenSSL_add_ssl_algorithms();
94 privoxy_mutex_unlock(&ssl_init_mutex);
99 /*********************************************************************
101 * Function : is_ssl_pending
103 * Description : Tests if there are some waiting data on ssl connection.
104 * Only considers data that has actually been received
105 * locally and ignores data that is still on the fly
106 * or has not yet been sent by the remote end.
109 * 1 : ssl_attr = SSL context to test
111 * Returns : 0 => No data are pending
112 * >0 => Pending data length
114 *********************************************************************/
115 extern size_t is_ssl_pending(struct ssl_attr *ssl_attr)
117 BIO *bio = ssl_attr->openssl_attr.bio;
123 return (size_t)BIO_pending(bio);
127 /*********************************************************************
129 * Function : ssl_send_data
131 * Description : Sends the content of buf (for n bytes) to given SSL
132 * connection context.
135 * 1 : ssl_attr = SSL context to send data to
136 * 2 : buf = Pointer to data to be sent
137 * 3 : len = Length of data to be sent to the SSL context
139 * Returns : Length of sent data or negative value on error.
141 *********************************************************************/
142 extern int ssl_send_data(struct ssl_attr *ssl_attr, const unsigned char *buf, size_t len)
144 BIO *bio = ssl_attr->openssl_attr.bio;
146 int pos = 0; /* Position of unsent part in buffer */
155 int send_len = (int)len - pos;
157 log_error(LOG_LEVEL_WRITING, "TLS: %N", send_len, buf+pos);
160 * Sending one part of the buffer
162 while ((ret = BIO_write(bio,
163 (const unsigned char *)(buf + pos),
166 if (!BIO_should_retry(bio))
168 log_ssl_errors(LOG_LEVEL_ERROR,
169 "Sending data over TLS/SSL failed");
173 /* Adding count of sent bytes to position in buffer */
181 /*********************************************************************
183 * Function : ssl_recv_data
185 * Description : Receives data from given SSL context and puts
189 * 1 : ssl_attr = SSL context to receive data from
190 * 2 : buf = Pointer to buffer where data will be written
191 * 3 : max_length = Maximum number of bytes to read
193 * Returns : Number of bytes read, 0 for EOF, or -1
196 *********************************************************************/
197 extern int ssl_recv_data(struct ssl_attr *ssl_attr, unsigned char *buf, size_t max_length)
199 BIO *bio = ssl_attr->openssl_attr.bio;
201 memset(buf, 0, max_length);
204 * Receiving data from SSL context into buffer
208 ret = BIO_read(bio, buf, (int)max_length);
209 } while (ret <= 0 && BIO_should_retry(bio));
213 log_ssl_errors(LOG_LEVEL_ERROR,
214 "Receiving data over TLS/SSL failed");
219 log_error(LOG_LEVEL_RECEIVED, "TLS: %N", ret, buf);
225 /*********************************************************************
227 * Function : ssl_store_cert
229 * Description : This is a callback function for certificate verification.
230 * It's called once for each certificate in the server's
231 * certificate trusted chain and prepares information about
232 * the certificate. The information can be used to inform
233 * the user about invalid certificates.
236 * 1 : csp = Current client state (buffers, headers, etc...)
237 * 2 : crt = certificate from trusted chain
239 * Returns : 0 on success and negative value on error
241 *********************************************************************/
242 static int ssl_store_cert(struct client_state *csp, X509* crt)
245 struct certs_chain *last = &(csp->server_certs_chain);
247 BIO *bio = BIO_new(BIO_s_mem());
248 EVP_PKEY *pkey = NULL;
249 char *bio_mem_data = 0;
252 const ASN1_INTEGER *bs;
253 const X509_ALGOR *tsig_alg;
258 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
263 * Searching for last item in certificates linked list
265 while (last->next != NULL)
271 * Preparing next item in linked list for next certificate
273 last->next = malloc_or_die(sizeof(struct certs_chain));
274 last->next->next = NULL;
275 memset(last->next->info_buf, 0, sizeof(last->next->info_buf));
276 memset(last->next->file_buf, 0, sizeof(last->next->file_buf));
279 * Saving certificate file into buffer
281 if (!PEM_write_bio_X509(bio, crt))
283 log_ssl_errors(LOG_LEVEL_ERROR, "PEM_write_X509() failed");
288 len = BIO_get_mem_data(bio, &bio_mem_data);
290 if (len > (sizeof(last->file_buf) - 1))
292 log_error(LOG_LEVEL_ERROR,
293 "X509 PEM cert len %d is larger then buffer len %s",
294 len, sizeof(last->file_buf) - 1);
295 len = sizeof(last->file_buf) - 1;
298 strncpy(last->file_buf, bio_mem_data, (size_t)len);
300 bio = BIO_new(BIO_s_mem());
303 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
309 * Saving certificate information into buffer
311 l = X509_get_version(crt);
312 if (l >= 0 && l <= 2)
314 if (BIO_printf(bio, "cert. version : %ld\n", l + 1) <= 0)
316 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
323 if (BIO_printf(bio, "cert. version : Unknown (%ld)\n", l) <= 0)
325 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
331 if (BIO_puts(bio, "serial number : ") <= 0)
333 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_write() for serial failed");
337 bs = X509_get0_serialNumber(crt);
338 if (bs->length <= (int)sizeof(long))
341 l = ASN1_INTEGER_get(bs);
352 if (bs->type == V_ASN1_NEG_INTEGER)
354 ul = 0 - (unsigned long)l;
359 ul = (unsigned long)l;
362 if (BIO_printf(bio, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
364 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
371 if (bs->type == V_ASN1_NEG_INTEGER)
373 if (BIO_puts(bio, " (Negative)") < 0)
375 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
380 for (int i = 0; i < bs->length; i++)
382 if (BIO_printf(bio, "%02x%c", bs->data[i],
383 ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
385 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
392 if (BIO_puts(bio, "issuer name : ") <= 0)
394 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issuer failed");
398 if (X509_NAME_print_ex(bio, X509_get_issuer_name(crt), 0, 0) < 0)
400 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for issuer failed");
405 if (BIO_puts(bio, "\nsubject name : ") <= 0)
407 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for sublect failed");
411 if (X509_NAME_print_ex(bio, X509_get_subject_name(crt), 0, 0) < 0) {
412 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for subject failed");
417 if (BIO_puts(bio, "\nissued on : ") <= 0)
419 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issued on failed");
423 if (!ASN1_TIME_print(bio, X509_get0_notBefore(crt)))
425 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for issued on failed");
430 if (BIO_puts(bio, "\nexpires on : ") <= 0)
432 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for expires on failed");
436 if (!ASN1_TIME_print(bio, X509_get0_notAfter(crt)))
438 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for expires on failed");
443 if (BIO_puts(bio, "\nsigned using : ") <= 0)
445 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for signed using failed");
449 tsig_alg = X509_get0_tbs_sigalg(crt);
450 if (!i2a_ASN1_OBJECT(bio, tsig_alg->algorithm))
452 log_ssl_errors(LOG_LEVEL_ERROR, "i2a_ASN1_OBJECT() for signed using on failed");
456 pkey = X509_get_pubkey(crt);
459 log_ssl_errors(LOG_LEVEL_ERROR, "X509_get_pubkey() failed");
464 switch (EVP_PKEY_base_id(pkey))
467 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "RSA key size", EVP_PKEY_bits(pkey));
470 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "DSA key size", EVP_PKEY_bits(pkey));
473 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "non-RSA/DSA key size", EVP_PKEY_bits(pkey));
478 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key size failed");
483 loc = X509_get_ext_by_NID(crt, NID_basic_constraints, -1);
486 X509_EXTENSION *ex = X509_get_ext(crt, loc);
487 if (BIO_puts(bio, "\nbasic constraints : ") <= 0)
489 log_ssl_errors(LOG_LEVEL_ERROR,
490 "BIO_printf() for basic constraints failed");
494 if (!X509V3_EXT_print(bio, ex, 0, 0))
496 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253))
498 log_ssl_errors(LOG_LEVEL_ERROR,
499 "ASN1_STRING_print_ex() for basic constraints failed");
506 loc = X509_get_ext_by_NID(crt, NID_subject_alt_name, -1);
509 X509_EXTENSION *ex = X509_get_ext(crt, loc);
510 if (BIO_puts(bio, "\nsubject alt name : ") <= 0)
512 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for alt name failed");
516 if (!X509V3_EXT_print(bio, ex, 0, 0))
518 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
519 ASN1_STRFLGS_RFC2253))
521 log_ssl_errors(LOG_LEVEL_ERROR,
522 "ASN1_STRING_print_ex() for alt name failed");
529 loc = X509_get_ext_by_NID(crt, NID_netscape_cert_type, -1);
532 X509_EXTENSION *ex = X509_get_ext(crt, loc);
533 if (BIO_puts(bio, "\ncert. type : ") <= 0)
535 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for cert type failed");
539 if (!X509V3_EXT_print(bio, ex, 0, 0))
541 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
542 ASN1_STRFLGS_RFC2253))
544 log_ssl_errors(LOG_LEVEL_ERROR,
545 "ASN1_STRING_print_ex() for cert type failed");
552 loc = X509_get_ext_by_NID(crt, NID_key_usage, -1);
555 X509_EXTENSION *ex = X509_get_ext(crt, loc);
556 if (BIO_puts(bio, "\nkey usage : ") <= 0)
558 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key usage failed");
562 if (!X509V3_EXT_print(bio, ex, 0, 0))
564 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
565 ASN1_STRFLGS_RFC2253))
567 log_ssl_errors(LOG_LEVEL_ERROR,
568 "ASN1_STRING_print_ex() for key usage failed");
575 loc = X509_get_ext_by_NID(crt, NID_ext_key_usage, -1);
577 X509_EXTENSION *ex = X509_get_ext(crt, loc);
578 if (BIO_puts(bio, "\next key usage : ") <= 0)
580 log_ssl_errors(LOG_LEVEL_ERROR,
581 "BIO_printf() for ext key usage failed");
585 if (!X509V3_EXT_print(bio, ex, 0, 0))
587 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
588 ASN1_STRFLGS_RFC2253))
590 log_ssl_errors(LOG_LEVEL_ERROR,
591 "ASN1_STRING_print_ex() for ext key usage failed");
598 loc = X509_get_ext_by_NID(crt, NID_certificate_policies, -1);
601 X509_EXTENSION *ex = X509_get_ext(crt, loc);
602 if (BIO_puts(bio, "\ncertificate policies : ") <= 0)
604 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for certificate policies failed");
608 if (!X509V3_EXT_print(bio, ex, 0, 0))
610 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
611 ASN1_STRFLGS_RFC2253))
613 log_ssl_errors(LOG_LEVEL_ERROR,
614 "ASN1_STRING_print_ex() for certificate policies failed");
621 /* make valgrind happy */
622 static const char zero = 0;
623 BIO_write(bio, &zero, 1);
625 len = BIO_get_mem_data(bio, &bio_mem_data);
626 encoded_text = html_encode(bio_mem_data);
627 strlcpy(last->info_buf, encoded_text, sizeof(last->info_buf));
644 /*********************************************************************
646 * Function : host_to_hash
648 * Description : Creates MD5 hash from host name. Host name is loaded
649 * from structure csp and saved again into it.
652 * 1 : csp = Current client state (buffers, headers, etc...)
654 * Returns : 1 => Error while creating hash
655 * 0 => Hash created successfully
657 *********************************************************************/
658 static int host_to_hash(struct client_state *csp)
662 memset(csp->http->hash_of_host, 0, sizeof(csp->http->hash_of_host));
663 MD5((unsigned char *)csp->http->host, strlen(csp->http->host),
664 csp->http->hash_of_host);
666 /* Converting hash into string with hex */
670 if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
671 csp->http->hash_of_host[i])) < 0)
673 log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
682 /*********************************************************************
684 * Function : create_client_ssl_connection
686 * Description : Creates TLS/SSL secured connection with client
689 * 1 : csp = Current client state (buffers, headers, etc...)
691 * Returns : 0 on success, negative value if connection wasn't created
694 *********************************************************************/
695 extern int create_client_ssl_connection(struct client_state *csp)
697 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
698 /* Paths to certificates file and key file */
699 char *key_file = NULL;
700 char *ca_file = NULL;
701 char *cert_file = NULL;
706 * Initializing OpenSSL structures for TLS/SSL connection
711 * Preparing hash of host for creating certificates
713 ret = host_to_hash(csp);
716 log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
722 * Preparing paths to certificates files and key file
724 ca_file = csp->config->ca_cert_file;
725 cert_file = make_certs_path(csp->config->certificate_directory,
726 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
727 key_file = make_certs_path(csp->config->certificate_directory,
728 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
730 if (cert_file == NULL || key_file == NULL)
737 * Generating certificate for requested host. Mutex to prevent
738 * certificate and key inconsistence must be locked.
740 privoxy_mutex_lock(&certificate_mutex);
742 ret = generate_webpage_certificate(csp);
745 log_error(LOG_LEVEL_ERROR,
746 "Generate_webpage_certificate failed: %d", ret);
747 privoxy_mutex_unlock(&certificate_mutex);
751 privoxy_mutex_unlock(&certificate_mutex);
753 if (!(ssl_attr->openssl_attr.ctx = SSL_CTX_new(SSLv23_server_method())))
755 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create SSL context");
760 /* Set the key and cert */
761 if (SSL_CTX_use_certificate_file(ssl_attr->openssl_attr.ctx,
762 cert_file, SSL_FILETYPE_PEM) != 1)
764 log_ssl_errors(LOG_LEVEL_ERROR,
765 "Loading webpage certificate %s failed", cert_file);
770 if (SSL_CTX_use_PrivateKey_file(ssl_attr->openssl_attr.ctx,
771 key_file, SSL_FILETYPE_PEM) != 1)
773 log_ssl_errors(LOG_LEVEL_ERROR,
774 "Loading webpage certificate private key %s failed", key_file);
779 SSL_CTX_set_options(ssl_attr->openssl_attr.ctx, SSL_OP_NO_SSLv3);
781 if (!(ssl_attr->openssl_attr.bio = BIO_new_ssl(ssl_attr->openssl_attr.ctx, 0)))
783 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
788 if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1)
790 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
795 if (!SSL_set_fd(ssl, csp->cfd))
797 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
803 * Handshake with client
805 log_error(LOG_LEVEL_CONNECT,
806 "Performing the TLS/SSL handshake with client. Hash of host: %s",
807 csp->http->hash_of_host_hex);
808 if (BIO_do_handshake(ssl_attr->openssl_attr.bio) != 1)
810 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_do_handshake failed");
815 log_error(LOG_LEVEL_CONNECT, "Client successfully connected over TLS/SSL");
816 csp->ssl_with_client_is_opened = 1;
821 * Freeing allocated paths to files
826 /* Freeing structures if connection wasn't created successfully */
829 free_client_ssl_structures(csp);
835 /*********************************************************************
837 * Function : close_client_ssl_connection
839 * Description : Closes TLS/SSL connection with client. This function
840 * checks if this connection is already created.
843 * 1 : csp = Current client state (buffers, headers, etc...)
847 *********************************************************************/
848 extern void close_client_ssl_connection(struct client_state *csp)
850 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
852 if (csp->ssl_with_client_is_opened == 0)
858 * Notifying the peer that the connection is being closed.
860 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
861 free_client_ssl_structures(csp);
862 csp->ssl_with_client_is_opened = 0;
866 /*********************************************************************
868 * Function : free_client_ssl_structures
870 * Description : Frees structures used for SSL communication with
874 * 1 : csp = Current client state (buffers, headers, etc...)
878 *********************************************************************/
879 static void free_client_ssl_structures(struct client_state *csp)
881 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
883 if (ssl_attr->openssl_attr.bio)
885 BIO_free_all(ssl_attr->openssl_attr.bio);
887 if (ssl_attr->openssl_attr.ctx)
889 SSL_CTX_free(ssl_attr->openssl_attr.ctx);
894 /*********************************************************************
896 * Function : close_server_ssl_connection
898 * Description : Closes TLS/SSL connection with server. This function
899 * checks if this connection is already opened.
902 * 1 : csp = Current client state (buffers, headers, etc...)
906 *********************************************************************/
907 extern void close_server_ssl_connection(struct client_state *csp)
909 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
911 if (csp->ssl_with_server_is_opened == 0)
917 * Notifying the peer that the connection is being closed.
919 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
920 free_server_ssl_structures(csp);
921 csp->ssl_with_server_is_opened = 0;
925 /*********************************************************************
927 * Function : create_server_ssl_connection
929 * Description : Creates TLS/SSL secured connection with server.
932 * 1 : csp = Current client state (buffers, headers, etc...)
934 * Returns : 0 on success, negative value if connection wasn't created
937 *********************************************************************/
938 extern int create_server_ssl_connection(struct client_state *csp)
940 openssl_connection_attr *ssl_attrs = &csp->ssl_server_attr.openssl_attr;
942 char *trusted_cas_file = NULL;
943 STACK_OF(X509) *chain;
946 csp->server_cert_verification_result = SSL_CERT_NOT_VERIFIED;
947 csp->server_certs_chain.next = NULL;
949 /* Setting path to file with trusted CAs */
950 trusted_cas_file = csp->config->trusted_cas_file;
952 ssl_attrs->ctx = SSL_CTX_new(SSLv23_method());
955 log_ssl_errors(LOG_LEVEL_ERROR, "SSL context creation failed");
961 * Loading file with trusted CAs
963 if (!SSL_CTX_load_verify_locations(ssl_attrs->ctx, trusted_cas_file, NULL))
965 log_ssl_errors(LOG_LEVEL_ERROR, "Loading trusted CAs file %s failed",
971 SSL_CTX_set_verify(ssl_attrs->ctx, SSL_VERIFY_NONE, NULL);
972 SSL_CTX_set_options(ssl_attrs->ctx, SSL_OP_NO_SSLv3);
974 if (!(ssl_attrs->bio = BIO_new_ssl(ssl_attrs->ctx, 1)))
976 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
981 if (BIO_get_ssl(ssl_attrs->bio, &ssl) != 1)
983 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
988 if (!SSL_set_fd(ssl, csp->server_connection.sfd))
990 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
996 * Set the hostname to check against the received server certificate
998 if (!SSL_set1_host(ssl, csp->http->host))
1000 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set1_host failed");
1006 if (!SSL_set_tlsext_host_name(ssl, csp->http->host))
1008 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_tlsext_host_name failed");
1014 * Handshake with server
1016 log_error(LOG_LEVEL_CONNECT,
1017 "Performing the TLS/SSL handshake with the server");
1019 if (BIO_do_handshake(ssl_attrs->bio) != 1)
1021 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_do_handshake failed");
1026 chain = SSL_get_peer_cert_chain(ssl);
1029 for (int i = 0; i < sk_X509_num(chain); i++)
1031 if (ssl_store_cert(csp, sk_X509_value(chain, i)) != 0)
1033 log_error(LOG_LEVEL_ERROR, "ssl_store_cert failed");
1040 if (!csp->dont_verify_certificate)
1042 long verify_result = SSL_get_verify_result(ssl);
1043 if (verify_result == X509_V_OK)
1046 csp->server_cert_verification_result = SSL_CERT_VALID;
1050 csp->server_cert_verification_result = verify_result;
1051 log_error(LOG_LEVEL_ERROR, "SSL_get_verify_result failed: %s",
1052 X509_verify_cert_error_string(verify_result));
1058 log_error(LOG_LEVEL_CONNECT, "Server successfully connected over TLS/SSL");
1061 * Server certificate chain is valid, so we can clean
1062 * chain, because we will not send it to client.
1064 free_certificate_chain(csp);
1066 csp->ssl_with_server_is_opened = 1;
1068 /* Freeing structures if connection wasn't created successfully */
1071 free_server_ssl_structures(csp);
1078 /*********************************************************************
1080 * Function : free_server_ssl_structures
1082 * Description : Frees structures used for SSL communication with server
1085 * 1 : csp = Current client state (buffers, headers, etc...)
1089 *********************************************************************/
1090 static void free_server_ssl_structures(struct client_state *csp)
1092 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
1094 if (ssl_attr->openssl_attr.bio)
1096 BIO_free_all(ssl_attr->openssl_attr.bio);
1098 if (ssl_attr->openssl_attr.ctx)
1100 SSL_CTX_free(ssl_attr->openssl_attr.ctx);
1105 /*********************************************************************
1107 * Function : log_ssl_errors
1109 * Description : Log SSL errors
1112 * 1 : debuglevel = Debug level
1113 * 2 : desc = Error description
1117 *********************************************************************/
1118 static void log_ssl_errors(int debuglevel, const char* fmt, ...)
1120 unsigned long err_code;
1121 char prefix[ERROR_BUF_SIZE];
1123 va_start(args, fmt);
1124 vsnprintf(prefix, sizeof(prefix), fmt, args);
1127 while ((err_code = ERR_get_error()))
1129 char err_buf[ERROR_BUF_SIZE];
1131 ERR_error_string_n(err_code, err_buf, sizeof(err_buf));
1132 log_error(debuglevel, "%s: %s", prefix, err_buf);
1136 * In case if called by mistake and there were
1137 * no SSL errors let's report it to the log.
1141 log_error(debuglevel, "%s: no ssl errors detected", prefix);
1146 /*********************************************************************
1148 * Function : ssl_base64_encode
1150 * Description : Encode a buffer into base64 format.
1153 * 1 : dst = Destination buffer
1154 * 2 : dlen = Destination buffer length
1155 * 3 : olen = Number of bytes written
1156 * 4 : src = Source buffer
1157 * 5 : slen = Amount of data to be encoded
1159 * Returns : 0 on success, error code othervise
1161 *********************************************************************/
1162 extern int ssl_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
1163 const unsigned char *src, size_t slen)
1165 *olen = 4 * ((slen/3) + ((slen%3) ? 1 : 0)) + 1;
1170 *olen = (size_t)EVP_EncodeBlock(dst, src, (int)slen) + 1;
1175 /*********************************************************************
1177 * Function : close_file_stream
1179 * Description : Close file stream, report error on close error
1182 * 1 : f = file stream to close
1183 * 2 : path = path for error report
1187 *********************************************************************/
1188 static void close_file_stream(FILE *f, const char *path)
1192 log_error(LOG_LEVEL_ERROR,
1193 "Error closing file %s: %s", path, strerror(errno));
1198 /*********************************************************************
1200 * Function : write_certificate
1202 * Description : Writes certificate into file.
1205 * 1 : crt = certificate to write into file
1206 * 2 : output_file = path to save certificate file
1209 * Returns : 1 on success success or negative value
1211 *********************************************************************/
1212 static int write_certificate(X509 *crt, const char *output_file)
1218 * Saving certificate into file
1220 if ((f = fopen(output_file, "w")) == NULL)
1222 log_error(LOG_LEVEL_ERROR, "Opening file %s to save certificate failed",
1227 ret = PEM_write_X509(f, crt);
1230 log_ssl_errors(LOG_LEVEL_ERROR,
1231 "Writing certificate into file %s failed", output_file);
1235 close_file_stream(f, output_file);
1240 /*********************************************************************
1242 * Function : write_private_key
1244 * Description : Writes private key into file and copies saved
1245 * content into given pointer to string. If function
1246 * returns 0 for success, this copy must be freed by
1250 * 1 : key = key to write into file
1251 * 2 : ret_buf = pointer to string with created key file content
1252 * 3 : key_file_path = path where to save key file
1254 * Returns : Length of written private key on success or negative value
1257 *********************************************************************/
1258 static int write_private_key(EVP_PKEY *key, char **ret_buf,
1259 const char *key_file_path)
1261 size_t len = 0; /* Length of created key */
1262 FILE *f = NULL; /* File to save certificate */
1264 BIO *bio_mem = BIO_new(BIO_s_mem());
1265 char *bio_mem_data = 0;
1267 if (bio_mem == NULL)
1269 log_ssl_errors(LOG_LEVEL_ERROR, "write_private_key memory allocation failure");
1274 * Writing private key into PEM string
1276 if (!PEM_write_bio_PrivateKey(bio_mem, key, NULL, NULL, 0, NULL, NULL))
1278 log_ssl_errors(LOG_LEVEL_ERROR,
1279 "Writing private key into PEM string failed");
1284 len = (size_t)BIO_get_mem_data(bio_mem, &bio_mem_data);
1286 /* Initializing buffer for key file content */
1287 *ret_buf = zalloc_or_die(len + 1);
1288 (*ret_buf)[len] = 0;
1290 strncpy(*ret_buf, bio_mem_data, len);
1293 * Saving key into file
1295 if ((f = fopen(key_file_path, "wb")) == NULL)
1297 log_error(LOG_LEVEL_ERROR,
1298 "Opening file %s to save private key failed: %E",
1304 if (fwrite(*ret_buf, 1, len, f) != len)
1306 log_error(LOG_LEVEL_ERROR,
1307 "Writing private key into file %s failed",
1309 close_file_stream(f, key_file_path);
1314 close_file_stream(f, key_file_path);
1328 /*********************************************************************
1330 * Function : generate_key
1332 * Description : Tests if private key for host saved in csp already
1333 * exists. If this file doesn't exists, a new key is
1334 * generated and saved in a file. The generated key is also
1335 * copied into given parameter key_buf, which must be then
1336 * freed by caller. If file with key exists, key_buf
1337 * contain NULL and no private key is generated.
1340 * 1 : csp = Current client state (buffers, headers, etc...)
1341 * 2 : key_buf = buffer to save new generated key
1343 * Returns : -1 => Error while generating private key
1344 * 0 => Key already exists
1345 * >0 => Length of generated private key
1347 *********************************************************************/
1348 static int generate_key(struct client_state *csp, char **key_buf)
1351 char* key_file_path = NULL;
1352 BIGNUM *exp = BN_new();
1353 RSA *rsa = RSA_new();
1354 EVP_PKEY *key = EVP_PKEY_new();
1356 if (exp == NULL || rsa == NULL || key == NULL)
1358 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1363 BN_set_word(exp, RSA_KEY_PUBLIC_EXPONENT);
1365 key_file_path = make_certs_path(csp->config->certificate_directory,
1366 (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1367 if (key_file_path == NULL)
1374 * Test if key already exists. If so, we don't have to create it again.
1376 if (file_exists(key_file_path) == 1)
1382 ret = RSA_generate_key_ex(rsa, RSA_KEYSIZE, exp, NULL);
1385 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key generation failure");
1390 if (!EVP_PKEY_set1_RSA(key, rsa))
1392 log_ssl_errors(LOG_LEVEL_ERROR,
1393 "Error assigning RSA key pair to PKEY structure");
1399 * Exporting private key into file
1401 if ((ret = write_private_key(key, key_buf, key_file_path)) < 0)
1403 log_error(LOG_LEVEL_ERROR,
1404 "Writing private key into file %s failed", key_file_path);
1411 * Freeing used variables
1425 freez(key_file_path);
1431 /*********************************************************************
1433 * Function : ssl_certificate_load
1435 * Description : Loads certificate from file.
1438 * 1 : cert_path = The certificate path to load
1440 * Returns : NULL => error loading certificate,
1441 * pointer to certificate instance otherwise
1443 *********************************************************************/
1444 static X509* ssl_certificate_load(const char *cert_path)
1447 FILE *cert_f = NULL;
1449 if (!(cert_f = fopen(cert_path, "r")))
1451 log_error(LOG_LEVEL_ERROR,
1452 "Error opening certificate file %s: %s", cert_path, strerror(errno));
1456 if (!(cert = PEM_read_X509(cert_f, NULL, NULL, NULL)))
1458 log_ssl_errors(LOG_LEVEL_ERROR,
1459 "Error reading certificate file %s", cert_path);
1462 close_file_stream(cert_f, cert_path);
1467 /*********************************************************************
1469 * Function : ssl_certificate_is_invalid
1471 * Description : Checks whether or not a certificate is valid.
1472 * Currently only checks that the certificate can be
1473 * parsed and that the "valid to" date is in the future.
1476 * 1 : cert_file = The certificate to check
1478 * Returns : 0 => The certificate is valid.
1479 * 1 => The certificate is invalid
1481 *********************************************************************/
1482 static int ssl_certificate_is_invalid(const char *cert_file)
1488 if (!(cert = ssl_certificate_load(cert_file)))
1490 log_ssl_errors(LOG_LEVEL_ERROR,
1491 "Error reading certificate file %s", cert_file);
1495 ret = X509_cmp_current_time(X509_get_notAfter(cert));
1498 log_ssl_errors(LOG_LEVEL_ERROR,
1499 "Error checking certificate %s validity", cert_file);
1504 return ret == -1 ? 1 : 0;
1508 /*********************************************************************
1510 * Function : set_x509_ext
1512 * Description : Sets the X509V3 extension data
1515 * 1 : cert = The certificate to modify
1516 * 2 : issuer = Issuer certificate
1517 * 3 : nid = OpenSSL NID
1518 * 4 : value = extension value
1520 * Returns : 0 => Error while setting extensuon data
1523 *********************************************************************/
1524 static int set_x509_ext(X509 *cert, X509 *issuer, int nid, const char *value)
1526 X509_EXTENSION *ext = NULL;
1530 X509V3_set_ctx(&ctx, issuer, cert, NULL, NULL, 0);
1531 ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
1534 log_ssl_errors(LOG_LEVEL_ERROR, "X509V3_EXT_conf_nid failure");
1538 if (!X509_add_ext(cert, ext, -1))
1540 log_ssl_errors(LOG_LEVEL_ERROR, "X509_add_ext failure");
1548 X509_EXTENSION_free(ext);
1554 /*********************************************************************
1556 * Function : set_subject_alternative_name
1558 * Description : Sets the Subject Alternative Name extension to a cert
1561 * 1 : cert = The certificate to modify
1562 * 2 : issuer = Issuer certificate
1563 * 3 : hostname = The hostname to add
1565 * Returns : 0 => Error while creating certificate.
1568 *********************************************************************/
1569 static int set_subject_alternative_name(X509 *cert, X509 *issuer, const char *hostname)
1571 size_t altname_len = strlen(hostname) + sizeof(CERTIFICATE_ALT_NAME_PREFIX);
1572 char alt_name_buf[altname_len];
1574 snprintf(alt_name_buf, sizeof(alt_name_buf),
1575 CERTIFICATE_ALT_NAME_PREFIX"%s", hostname);
1576 return set_x509_ext(cert, issuer, NID_subject_alt_name, alt_name_buf);
1580 /*********************************************************************
1582 * Function : generate_webpage_certificate
1584 * Description : Creates certificate file in presetted directory.
1585 * If certificate already exists, no other certificate
1586 * will be created. Subject of certificate is named
1587 * by csp->http->host from parameter. This function also
1588 * triggers generating of private key for new certificate.
1591 * 1 : csp = Current client state (buffers, headers, etc...)
1593 * Returns : -1 => Error while creating certificate.
1594 * 0 => Certificate already exists.
1595 * 1 => Certificate created
1597 *********************************************************************/
1598 static int generate_webpage_certificate(struct client_state *csp)
1600 char *key_buf = NULL; /* Buffer for created key */
1601 X509 *issuer_cert = NULL;
1604 EVP_PKEY *loaded_subject_key = NULL;
1605 EVP_PKEY *loaded_issuer_key = NULL;
1606 X509_NAME *issuer_name;
1607 X509_NAME *subject_name = NULL;
1608 ASN1_TIME *asn_time = NULL;
1609 ASN1_INTEGER *serial = NULL;
1610 BIGNUM *serial_num = NULL;
1613 cert_options cert_opt;
1614 char cert_valid_from[VALID_DATETIME_BUFLEN];
1615 char cert_valid_to[VALID_DATETIME_BUFLEN];
1617 /* Paths to keys and certificates needed to create certificate */
1618 cert_opt.issuer_key = NULL;
1619 cert_opt.subject_key = NULL;
1620 cert_opt.issuer_crt = NULL;
1622 cert_opt.output_file = make_certs_path(csp->config->certificate_directory,
1623 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
1624 if (cert_opt.output_file == NULL)
1629 cert_opt.subject_key = make_certs_path(csp->config->certificate_directory,
1630 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1631 if (cert_opt.subject_key == NULL)
1633 freez(cert_opt.output_file);
1637 if (file_exists(cert_opt.output_file) == 1)
1639 /* The file exists, but is it valid? */
1640 if (ssl_certificate_is_invalid(cert_opt.output_file))
1642 log_error(LOG_LEVEL_CONNECT,
1643 "Certificate %s is no longer valid. Removing it.",
1644 cert_opt.output_file);
1645 if (unlink(cert_opt.output_file))
1647 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1648 cert_opt.output_file);
1650 freez(cert_opt.output_file);
1651 freez(cert_opt.subject_key);
1655 if (unlink(cert_opt.subject_key))
1657 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1658 cert_opt.subject_key);
1660 freez(cert_opt.output_file);
1661 freez(cert_opt.subject_key);
1668 freez(cert_opt.output_file);
1669 freez(cert_opt.subject_key);
1676 * Create key for requested host
1678 int subject_key_len = generate_key(csp, &key_buf);
1679 if (subject_key_len < 0)
1681 freez(cert_opt.output_file);
1682 freez(cert_opt.subject_key);
1683 log_error(LOG_LEVEL_ERROR, "Key generating failed");
1688 * Converting unsigned long serial number to char * serial number.
1689 * We must compute length of serial number in string + terminating null.
1691 unsigned long certificate_serial = get_certificate_serial(csp);
1692 unsigned long certificate_serial_time = (unsigned long)time(NULL);
1693 int serial_num_size = snprintf(NULL, 0, "%lu%lu",
1694 certificate_serial_time, certificate_serial) + 1;
1695 if (serial_num_size <= 0)
1697 serial_num_size = 1;
1700 char serial_num_text[serial_num_size]; /* Buffer for serial number */
1701 ret = snprintf(serial_num_text, (size_t)serial_num_size, "%lu%lu",
1702 certificate_serial_time, certificate_serial);
1703 if (ret < 0 || ret >= serial_num_size)
1705 log_error(LOG_LEVEL_ERROR,
1706 "Converting certificate serial number into string failed");
1712 * Preparing parameters for certificate
1714 subject_name = X509_NAME_new();
1717 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1722 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COMMON_NAME_FCODE,
1723 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1725 log_ssl_errors(LOG_LEVEL_ERROR,
1726 "X509 subject name (code: %s, val: %s) error",
1727 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1731 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORGANIZATION_FCODE,
1732 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1734 log_ssl_errors(LOG_LEVEL_ERROR,
1735 "X509 subject name (code: %s, val: %s) error",
1736 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1740 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORG_UNIT_FCODE,
1741 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1743 log_ssl_errors(LOG_LEVEL_ERROR,
1744 "X509 subject name (code: %s, val: %s) error",
1745 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1749 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COUNTRY_FCODE,
1750 MBSTRING_ASC, (void *)CERT_PARAM_COUNTRY_CODE, -1, -1, 0))
1752 log_ssl_errors(LOG_LEVEL_ERROR,
1753 "X509 subject name (code: %s, val: %s) error",
1754 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1759 cert_opt.issuer_crt = csp->config->ca_cert_file;
1760 cert_opt.issuer_key = csp->config->ca_key_file;
1762 if (get_certificate_valid_from_date(cert_valid_from,
1763 sizeof(cert_valid_from), VALID_DATETIME_FMT)
1764 || get_certificate_valid_to_date(cert_valid_to,
1765 sizeof(cert_valid_to), VALID_DATETIME_FMT))
1767 log_error(LOG_LEVEL_ERROR, "Generating one of the validity dates failed");
1772 cert_opt.subject_pwd = CERT_SUBJECT_PASSWORD;
1773 cert_opt.issuer_pwd = csp->config->ca_password;
1774 cert_opt.not_before = cert_valid_from;
1775 cert_opt.not_after = cert_valid_to;
1776 cert_opt.serial = serial_num_text;
1777 cert_opt.max_pathlen = -1;
1780 * Test if the private key was already created.
1781 * XXX: Can this still happen?
1783 if (subject_key_len == 0)
1785 log_error(LOG_LEVEL_ERROR, "Subject key was already created");
1791 * Parse serial to MPI
1793 serial_num = BN_new();
1796 log_error(LOG_LEVEL_ERROR, "generate_webpage_certificate: memory error");
1800 if (!BN_dec2bn(&serial_num, cert_opt.serial))
1802 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to parse serial %s", cert_opt.serial);
1807 if (!(serial = BN_to_ASN1_INTEGER(serial_num, NULL)))
1809 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to generate serial ASN1 representation");
1815 * Loading certificates
1817 if (!(issuer_cert = ssl_certificate_load(cert_opt.issuer_crt)))
1819 log_error(LOG_LEVEL_ERROR, "Loading issuer certificate %s failed",
1820 cert_opt.issuer_crt);
1825 issuer_name = X509_get_issuer_name(issuer_cert);
1828 * Loading keys from file or from buffer
1830 if (key_buf != NULL && subject_key_len > 0)
1832 pk_bio = BIO_new_mem_buf(key_buf, subject_key_len);
1834 else if (!(pk_bio = BIO_new_file(cert_opt.subject_key, "r")))
1836 log_ssl_errors(LOG_LEVEL_ERROR,
1837 "Failure opening subject key %s BIO", cert_opt.subject_key);
1842 loaded_subject_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL,
1843 (void *)cert_opt.subject_pwd);
1844 if (!loaded_subject_key)
1846 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing subject key %s failed",
1847 cert_opt.subject_key);
1852 if (!BIO_free(pk_bio))
1854 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing subject key BIO");
1857 if (!(pk_bio = BIO_new_file(cert_opt.issuer_key, "r")))
1859 log_ssl_errors(LOG_LEVEL_ERROR, "Failure opening issuer key %s BIO",
1860 cert_opt.issuer_key);
1865 loaded_issuer_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL,
1866 (void *)cert_opt.issuer_pwd);
1867 if (!loaded_issuer_key)
1869 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing issuer key %s failed",
1870 cert_opt.subject_key);
1878 log_ssl_errors(LOG_LEVEL_ERROR, "Certificate allocation error");
1883 if (!X509_set_version(cert, CERTIFICATE_VERSION))
1885 log_ssl_errors(LOG_LEVEL_ERROR, "X509_set_version failed");
1891 * Setting parameters of signed certificate
1893 if (!X509_set_pubkey(cert, loaded_subject_key))
1895 log_ssl_errors(LOG_LEVEL_ERROR,
1896 "Setting issuer name in signed certificate failed");
1901 if (!X509_set_subject_name(cert, subject_name))
1903 log_ssl_errors(LOG_LEVEL_ERROR,
1904 "Setting issuer name in signed certificate failed");
1909 if (!X509_set_issuer_name(cert, issuer_name))
1911 log_ssl_errors(LOG_LEVEL_ERROR,
1912 "Setting issuer name in signed certificate failed");
1917 if (!X509_set_serialNumber(cert, serial))
1919 log_ssl_errors(LOG_LEVEL_ERROR,
1920 "Setting serial number in signed certificate failed");
1925 asn_time = ASN1_TIME_new();
1928 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time memory allocation failure");
1933 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_after))
1935 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time [%s] encode error", cert_opt.not_after);
1940 if (!X509_set1_notAfter(cert, asn_time))
1942 log_ssl_errors(LOG_LEVEL_ERROR,
1943 "Setting valid not after in signed certificate failed");
1948 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_before))
1950 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time encode error");
1955 if (!X509_set1_notBefore(cert, asn_time))
1957 log_ssl_errors(LOG_LEVEL_ERROR,
1958 "Setting valid not befre in signed certificate failed");
1963 if (!set_x509_ext(cert, issuer_cert, NID_basic_constraints, CERTIFICATE_BASIC_CONSTRAINTS))
1965 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the basicConstraints extension "
1966 "in signed certificate failed");
1971 if (!set_x509_ext(cert, issuer_cert, NID_subject_key_identifier, CERTIFICATE_SUBJECT_KEY))
1973 log_ssl_errors(LOG_LEVEL_ERROR,
1974 "Setting the Subject Key Identifie extension failed");
1979 if (!set_x509_ext(cert, issuer_cert, NID_authority_key_identifier, CERTIFICATE_AUTHORITY_KEY))
1981 log_ssl_errors(LOG_LEVEL_ERROR,
1982 "Setting the Authority Key Identifier extension failed");
1987 if (!host_is_ip_address(csp->http->host) &&
1988 !set_subject_alternative_name(cert, issuer_cert, csp->http->host))
1990 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the Subject Alt Nameextension failed");
1995 if (!X509_sign(cert, loaded_issuer_key, EVP_sha256()))
1997 log_ssl_errors(LOG_LEVEL_ERROR, "Signing certificate failed");
2003 * Writing certificate into file
2005 if (write_certificate(cert, cert_opt.output_file) < 0)
2007 log_error(LOG_LEVEL_ERROR, "Writing certificate into file failed");
2016 * Freeing used structures
2020 X509_free(issuer_cert);
2026 if (pk_bio && !BIO_free(pk_bio))
2028 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing pk BIO");
2030 if (loaded_subject_key)
2032 EVP_PKEY_free(loaded_subject_key);
2034 if (loaded_issuer_key)
2036 EVP_PKEY_free(loaded_issuer_key);
2040 X509_NAME_free(subject_name);
2044 ASN1_TIME_free(asn_time);
2048 BN_free(serial_num);
2052 ASN1_INTEGER_free(serial);
2054 freez(cert_opt.subject_key);
2055 freez(cert_opt.output_file);
2062 /*********************************************************************
2064 * Function : ssl_crt_verify_info
2066 * Description : Returns an informational string about the verification
2067 * status of a certificate.
2070 * 1 : buf = Buffer to write to
2071 * 2 : size = Maximum size of buffer
2072 * 3 : csp = client state
2076 *********************************************************************/
2077 extern void ssl_crt_verify_info(char *buf, size_t size, struct client_state *csp)
2079 strncpy(buf, X509_verify_cert_error_string(csp->server_cert_verification_result), size);
2084 /*********************************************************************
2086 * Function : ssl_release
2088 * Description : Release all SSL resources
2094 *********************************************************************/
2095 extern void ssl_release(void)
2097 if (ssl_inited == 1)
2099 SSL_COMP_free_compression_methods();
2101 CONF_modules_free();
2102 CONF_modules_unload(1);
2104 COMP_zlib_cleanup();
2109 CRYPTO_cleanup_all_ex_data();