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 = 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 = 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 = 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", len, sizeof(last->file_buf) - 1);
294 len = sizeof(last->file_buf) - 1;
297 strncpy(last->file_buf, bio_mem_data, (size_t)len);
299 bio = BIO_new(BIO_s_mem());
302 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
308 * Saving certificate information into buffer
310 l = X509_get_version(crt);
311 if (l >= 0 && l <= 2) {
312 if (BIO_printf(bio, "cert. version : %ld\n", l + 1) <= 0) {
313 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
318 if (BIO_printf(bio, "cert. version : Unknown (%ld)\n", l) <= 0) {
319 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
325 if (BIO_puts(bio, "serial number : ") <= 0) {
326 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_write() for serial failed");
330 bs = X509_get0_serialNumber(crt);
331 if (bs->length <= (int)sizeof(long)) {
333 l = ASN1_INTEGER_get(bs);
341 if (bs->type == V_ASN1_NEG_INTEGER) {
342 ul = 0 - (unsigned long)l;
345 ul = (unsigned long)l;
348 if (BIO_printf(bio, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0) {
349 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
354 if (bs->type == V_ASN1_NEG_INTEGER) {
355 if (BIO_puts(bio, " (Negative)") < 0) {
356 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
361 for (int i = 0; i < bs->length; i++) {
362 if (BIO_printf(bio, "%02x%c", bs->data[i],
363 ((i + 1 == bs->length) ? '\n' : ':')) <= 0) {
364 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
371 if (BIO_puts(bio, "issuer name : ") <= 0) {
372 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issuer failed");
376 if (X509_NAME_print_ex(bio, X509_get_issuer_name(crt), 0, 0) < 0) {
377 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for issuer failed");
382 if (BIO_puts(bio, "\nsubject name : ") <= 0) {
383 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for sublect failed");
387 if (X509_NAME_print_ex(bio, X509_get_subject_name(crt), 0, 0) < 0) {
388 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for subject failed");
393 if (BIO_puts(bio, "\nissued on : ") <= 0) {
394 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issued on failed");
398 if (!ASN1_TIME_print(bio, X509_get0_notBefore(crt))) {
399 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for issued on failed");
404 if (BIO_puts(bio, "\nexpires on : ") <= 0) {
405 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for expires on failed");
409 if (!ASN1_TIME_print(bio, X509_get0_notAfter(crt))) {
410 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for expires on failed");
415 if (BIO_puts(bio, "\nsigned using : ") <= 0) {
416 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for signed using failed");
420 tsig_alg = X509_get0_tbs_sigalg(crt);
421 if (!i2a_ASN1_OBJECT(bio, tsig_alg->algorithm)) {
422 log_ssl_errors(LOG_LEVEL_ERROR, "i2a_ASN1_OBJECT() for signed using on failed");
426 pkey = X509_get_pubkey(crt);
428 log_ssl_errors(LOG_LEVEL_ERROR, "X509_get_pubkey() failed");
433 switch (EVP_PKEY_base_id(pkey)) {
435 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "RSA key size", EVP_PKEY_bits(pkey));
438 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "DSA key size", EVP_PKEY_bits(pkey));
441 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "non-RSA/DSA key size", EVP_PKEY_bits(pkey));
445 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key size failed");
450 loc = X509_get_ext_by_NID(crt, NID_basic_constraints, -1);
452 X509_EXTENSION *ex = X509_get_ext(crt, loc);
453 if (BIO_puts(bio, "\nbasic constraints : ") <= 0) {
454 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for basic constraints failed");
458 if (!X509V3_EXT_print(bio, ex, 0, 0)) {
459 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253)) {
460 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_STRING_print_ex() for basic constraints failed");
467 loc = X509_get_ext_by_NID(crt, NID_subject_alt_name, -1);
469 X509_EXTENSION *ex = X509_get_ext(crt, loc);
470 if (BIO_puts(bio, "\nsubject alt name : ") <= 0) {
471 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for alt name failed");
475 if (!X509V3_EXT_print(bio, ex, 0, 0)) {
476 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253)) {
477 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_STRING_print_ex() for alt name failed");
484 loc = X509_get_ext_by_NID(crt, NID_netscape_cert_type, -1);
486 X509_EXTENSION *ex = X509_get_ext(crt, loc);
487 if (BIO_puts(bio, "\ncert. type : ") <= 0) {
488 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for cert type failed");
492 if (!X509V3_EXT_print(bio, ex, 0, 0)) {
493 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253)) {
494 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_STRING_print_ex() for cert type failed");
501 loc = X509_get_ext_by_NID(crt, NID_key_usage, -1);
503 X509_EXTENSION *ex = X509_get_ext(crt, loc);
504 if (BIO_puts(bio, "\nkey usage : ") <= 0) {
505 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key usage failed");
509 if (!X509V3_EXT_print(bio, ex, 0, 0)) {
510 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253)) {
511 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_STRING_print_ex() for key usage failed");
518 loc = X509_get_ext_by_NID(crt, NID_ext_key_usage, -1);
520 X509_EXTENSION *ex = X509_get_ext(crt, loc);
521 if (BIO_puts(bio, "\next key usage : ") <= 0) {
522 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for ext key usage failed");
526 if (!X509V3_EXT_print(bio, ex, 0, 0)) {
527 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253)) {
528 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_STRING_print_ex() for ext key usage failed");
535 loc = X509_get_ext_by_NID(crt, NID_certificate_policies, -1);
537 X509_EXTENSION *ex = X509_get_ext(crt, loc);
538 if (BIO_puts(bio, "\ncertificate policies : ") <= 0) {
539 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for certificate policies failed");
543 if (!X509V3_EXT_print(bio, ex, 0, 0)) {
544 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253)) {
545 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_STRING_print_ex() for certificate policies failed");
552 /* make valgrind happy */
553 static const char zero = 0;
554 BIO_write(bio, &zero, 1);
556 len = BIO_get_mem_data(bio, &bio_mem_data);
557 encoded_text = html_encode(bio_mem_data);
558 strlcpy(last->info_buf, encoded_text, sizeof(last->info_buf));
563 if (bio) BIO_free(bio);
564 if (pkey) EVP_PKEY_free(pkey);
568 /*********************************************************************
570 * Function : host_to_hash
572 * Description : Creates MD5 hash from host name. Host name is loaded
573 * from structure csp and saved again into it.
576 * 1 : csp = Current client state (buffers, headers, etc...)
578 * Returns : 1 => Error while creating hash
579 * 0 => Hash created successfully
581 *********************************************************************/
582 static int host_to_hash(struct client_state *csp)
586 memset(csp->http->hash_of_host, 0, sizeof(csp->http->hash_of_host));
587 MD5((unsigned char *)csp->http->host, strlen(csp->http->host),
588 csp->http->hash_of_host);
590 /* Converting hash into string with hex */
594 if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
595 csp->http->hash_of_host[i])) < 0)
597 log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
605 /*********************************************************************
607 * Function : create_client_ssl_connection
609 * Description : Creates TLS/SSL secured connection with client
612 * 1 : csp = Current client state (buffers, headers, etc...)
614 * Returns : 0 on success, negative value if connection wasn't created
617 *********************************************************************/
618 extern int create_client_ssl_connection(struct client_state *csp)
620 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
621 /* Paths to certificates file and key file */
622 char *key_file = NULL;
623 char *ca_file = NULL;
624 char *cert_file = NULL;
629 * Initializing mbedtls structures for TLS/SSL connection
634 * Preparing hash of host for creating certificates
636 ret = host_to_hash(csp);
639 log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
645 * Preparing paths to certificates files and key file
647 ca_file = csp->config->ca_cert_file;
648 cert_file = make_certs_path(csp->config->certificate_directory,
649 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
650 key_file = make_certs_path(csp->config->certificate_directory,
651 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
653 if (cert_file == NULL || key_file == NULL)
660 * Generating certificate for requested host. Mutex to prevent
661 * certificate and key inconsistence must be locked.
663 privoxy_mutex_lock(&certificate_mutex);
665 ret = generate_webpage_certificate(csp);
668 log_error(LOG_LEVEL_ERROR,
669 "Generate_webpage_certificate failed: %d", ret);
670 privoxy_mutex_unlock(&certificate_mutex);
674 privoxy_mutex_unlock(&certificate_mutex);
676 if (!(ssl_attr->openssl_attr.ctx = SSL_CTX_new(SSLv23_server_method()))) {
677 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create SSL context");
682 /* Set the key and cert */
683 if (SSL_CTX_use_certificate_file(ssl_attr->openssl_attr.ctx,
684 cert_file, SSL_FILETYPE_PEM) != 1) {
685 log_ssl_errors(LOG_LEVEL_ERROR, "Loading webpage certificate %s failed", cert_file);
690 if (SSL_CTX_use_PrivateKey_file(ssl_attr->openssl_attr.ctx, key_file, SSL_FILETYPE_PEM) != 1) {
691 log_ssl_errors(LOG_LEVEL_ERROR, "Loading webpage certificate private key %s failed", key_file);
696 SSL_CTX_set_options(ssl_attr->openssl_attr.ctx, SSL_OP_NO_SSLv3);
698 if (!(ssl_attr->openssl_attr.bio = BIO_new_ssl(ssl_attr->openssl_attr.ctx, 0))) {
699 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
704 if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1)
706 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
711 if(!SSL_set_fd(ssl, csp->cfd))
713 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
719 * Handshake with client
721 log_error(LOG_LEVEL_CONNECT,
722 "Performing the TLS/SSL handshake with client. Hash of host: %s",
723 csp->http->hash_of_host_hex);
724 if (BIO_do_handshake(ssl_attr->openssl_attr.bio) != 1) {
725 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_do_handshake failed");
730 log_error(LOG_LEVEL_CONNECT, "Client successfully connected over TLS/SSL");
731 csp->ssl_with_client_is_opened = 1;
736 * Freeing allocated paths to files
741 /* Freeing structures if connection wasn't created successfully */
744 free_client_ssl_structures(csp);
750 /*********************************************************************
752 * Function : close_client_ssl_connection
754 * Description : Closes TLS/SSL connection with client. This function
755 * checks if this connection is already created.
758 * 1 : csp = Current client state (buffers, headers, etc...)
762 *********************************************************************/
763 extern void close_client_ssl_connection(struct client_state *csp)
765 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
767 if (csp->ssl_with_client_is_opened == 0)
773 * Notifying the peer that the connection is being closed.
775 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
776 free_client_ssl_structures(csp);
777 csp->ssl_with_client_is_opened = 0;
781 /*********************************************************************
783 * Function : free_client_ssl_structures
785 * Description : Frees structures used for SSL communication with
789 * 1 : csp = Current client state (buffers, headers, etc...)
793 *********************************************************************/
794 static void free_client_ssl_structures(struct client_state *csp)
796 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
798 if (ssl_attr->openssl_attr.bio) BIO_free_all(ssl_attr->openssl_attr.bio);
799 if (ssl_attr->openssl_attr.ctx) SSL_CTX_free(ssl_attr->openssl_attr.ctx);
803 /*********************************************************************
805 * Function : close_server_ssl_connection
807 * Description : Closes TLS/SSL connection with server. This function
808 * checks if this connection is already opened.
811 * 1 : csp = Current client state (buffers, headers, etc...)
815 *********************************************************************/
816 extern void close_server_ssl_connection(struct client_state *csp)
818 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
820 if (csp->ssl_with_server_is_opened == 0)
826 * Notifying the peer that the connection is being closed.
828 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
829 free_server_ssl_structures(csp);
830 csp->ssl_with_server_is_opened = 0;
834 /*********************************************************************
836 * Function : create_server_ssl_connection
838 * Description : Creates TLS/SSL secured connection with server.
841 * 1 : csp = Current client state (buffers, headers, etc...)
843 * Returns : 0 on success, negative value if connection wasn't created
846 *********************************************************************/
847 extern int create_server_ssl_connection(struct client_state *csp)
849 openssl_connection_attr *ssl_attrs = &csp->ssl_server_attr.openssl_attr;
851 char *trusted_cas_file = NULL;
852 STACK_OF(X509) *chain;
855 csp->server_cert_verification_result = SSL_CERT_NOT_VERIFIED;
856 csp->server_certs_chain.next = NULL;
858 /* Setting path to file with trusted CAs */
859 trusted_cas_file = csp->config->trusted_cas_file;
862 ssl_attrs->ctx = SSL_CTX_new(SSLv23_method());
865 log_ssl_errors(LOG_LEVEL_ERROR, "SSL context creation failed");
871 * Loading file with trusted CAs
873 if(!SSL_CTX_load_verify_locations(ssl_attrs->ctx, trusted_cas_file, NULL))
875 log_ssl_errors(LOG_LEVEL_ERROR, "Loading trusted CAs file %s failed",
881 SSL_CTX_set_verify(ssl_attrs->ctx, SSL_VERIFY_NONE, NULL);
882 SSL_CTX_set_options(ssl_attrs->ctx, SSL_OP_NO_SSLv3);
884 if (!(ssl_attrs->bio = BIO_new_ssl(ssl_attrs->ctx, 1))) {
885 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
890 if (BIO_get_ssl(ssl_attrs->bio, &ssl) != 1)
892 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
897 if (!SSL_set_fd(ssl, csp->server_connection.sfd))
899 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
905 * Set the hostname to check against the received server certificate
907 if (!SSL_set1_host(ssl, csp->http->host))
909 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set1_host failed");
915 if (!SSL_set_tlsext_host_name(ssl, csp->http->host))
917 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_tlsext_host_name failed");
923 * Handshake with server
925 log_error(LOG_LEVEL_CONNECT,
926 "Performing the TLS/SSL handshake with the server");
928 if (BIO_do_handshake(ssl_attrs->bio) != 1)
930 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_do_handshake failed");
935 chain = SSL_get_peer_cert_chain(ssl);
937 for (int i = 0; i < sk_X509_num(chain); i++) {
938 if (ssl_store_cert(csp, sk_X509_value(chain, i)) != 0) {
939 log_error(LOG_LEVEL_ERROR, "ssl_store_cert failed");
946 if (!csp->dont_verify_certificate)
948 long verify_result = SSL_get_verify_result(ssl);
949 if (verify_result == X509_V_OK)
952 csp->server_cert_verification_result = SSL_CERT_VALID;
954 csp->server_cert_verification_result = verify_result;
955 log_error(LOG_LEVEL_ERROR, "SSL_get_verify_result failed: %s",
956 X509_verify_cert_error_string(verify_result));
962 log_error(LOG_LEVEL_CONNECT, "Server successfully connected over TLS/SSL");
965 * Server certificate chain is valid, so we can clean
966 * chain, because we will not send it to client.
968 free_certificate_chain(csp);
970 csp->ssl_with_server_is_opened = 1;
972 /* Freeing structures if connection wasn't created successfully */
975 free_server_ssl_structures(csp);
982 /*********************************************************************
984 * Function : free_server_ssl_structures
986 * Description : Frees structures used for SSL communication with server
989 * 1 : csp = Current client state (buffers, headers, etc...)
993 *********************************************************************/
994 static void free_server_ssl_structures(struct client_state *csp)
996 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
998 if (ssl_attr->openssl_attr.bio) BIO_free_all(ssl_attr->openssl_attr.bio);
999 if (ssl_attr->openssl_attr.ctx) SSL_CTX_free(ssl_attr->openssl_attr.ctx);
1003 /*********************************************************************
1005 * Function : log_ssl_errors
1007 * Description : Log SSL errors
1010 * 1 : debuglevel = Debug level
1011 * 2 : desc = Error description
1015 *********************************************************************/
1016 static void log_ssl_errors(int debuglevel, const char* fmt, ...)
1018 unsigned long err_code;
1019 char prefix[ERROR_BUF_SIZE];
1021 va_start(args, fmt);
1022 vsnprintf(prefix, sizeof(prefix), fmt, args);
1025 while ((err_code = ERR_get_error())) {
1026 char err_buf[ERROR_BUF_SIZE];
1028 ERR_error_string_n(err_code, err_buf, sizeof(err_buf));
1029 log_error(debuglevel, "%s: %s", prefix, err_buf);
1032 /* in case if called by mistake and there were no SSL errors let's report it to the log */
1034 log_error(debuglevel, "%s: no ssl errors detected", prefix);
1038 /*********************************************************************
1040 * Function : ssl_base64_encode
1042 * Description : Encode a buffer into base64 format.
1045 * 1 : dst = Destination buffer
1046 * 2 : dlen = Destination buffer length
1047 * 3 : olen = Number of bytes written
1048 * 4 : src = Source buffer
1049 * 5 : slen = Amount of data to be encoded
1051 * Returns : 0 on success, error code othervise
1053 *********************************************************************/
1054 extern int ssl_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
1055 const unsigned char *src, size_t slen )
1057 *olen = 4 * ((slen/3) + ((slen%3) ? 1 : 0)) + 1;
1060 *olen = (size_t)EVP_EncodeBlock(dst, src, (int)slen) + 1;
1065 /*********************************************************************
1067 * Function : close_file_stream
1069 * Description : Close file stream, report error on close error
1072 * 1 : f = file stream to close
1073 * 2 : path = path for error report
1077 *********************************************************************/
1078 static void close_file_stream(FILE *f, const char* path) {
1079 if (fclose(f) != 0) {
1080 log_error(LOG_LEVEL_ERROR, "Error closing file %s: %s", path, strerror(errno));
1085 /*********************************************************************
1087 * Function : write_certificate
1089 * Description : Writes certificate into file.
1092 * 1 : crt = certificate to write into file
1093 * 2 : output_file = path to save certificate file
1096 * Returns : 1 on success success or negative value
1098 *********************************************************************/
1099 static int write_certificate(X509 *crt, const char *output_file)
1105 * Saving certificate into file
1107 if ((f = fopen(output_file, "w")) == NULL)
1109 log_error(LOG_LEVEL_ERROR, "Opening file %s to save certificate failed",
1114 ret = PEM_write_X509(f, crt);
1117 log_ssl_errors(LOG_LEVEL_ERROR,
1118 "Writing certificate into file %s failed", output_file);
1122 close_file_stream(f, output_file);
1127 /*********************************************************************
1129 * Function : write_private_key
1131 * Description : Writes private key into file and copies saved
1132 * content into given pointer to string. If function
1133 * returns 0 for success, this copy must be freed by
1137 * 1 : key = key to write into file
1138 * 2 : ret_buf = pointer to string with created key file content
1139 * 3 : key_file_path = path where to save key file
1141 * Returns : Length of written private key on success or negative value
1144 *********************************************************************/
1145 static int write_private_key(EVP_PKEY *key, char **ret_buf,
1146 const char *key_file_path)
1148 size_t len = 0; /* Length of created key */
1149 FILE *f = NULL; /* File to save certificate */
1151 BIO *bio_mem = BIO_new(BIO_s_mem());
1152 char *bio_mem_data = 0;
1154 if (bio_mem == NULL) {
1155 log_ssl_errors(LOG_LEVEL_ERROR, "write_private_key memory allocation failure");
1160 * Writing private key into PEM string
1162 if (!PEM_write_bio_PrivateKey(bio_mem, key, NULL, NULL, 0, NULL, NULL)) {
1163 log_ssl_errors(LOG_LEVEL_ERROR, "Writing private key into PEM string failed");
1168 len = (size_t)BIO_get_mem_data(bio_mem, &bio_mem_data);
1170 /* Initializing buffer for key file content */
1171 *ret_buf = zalloc_or_die(len + 1);
1172 (*ret_buf)[len] = 0;
1174 strncpy(*ret_buf, bio_mem_data, len);
1177 * Saving key into file
1179 if ((f = fopen(key_file_path, "wb")) == NULL)
1181 log_error(LOG_LEVEL_ERROR,
1182 "Opening file %s to save private key failed: %E",
1188 if (fwrite(*ret_buf, 1, len, f) != len)
1190 log_error(LOG_LEVEL_ERROR,
1191 "Writing private key into file %s failed",
1193 close_file_stream(f, key_file_path);
1198 close_file_stream(f, key_file_path);
1212 /*********************************************************************
1214 * Function : generate_key
1216 * Description : Tests if private key for host saved in csp already
1217 * exists. If this file doesn't exists, a new key is
1218 * generated and saved in a file. The generated key is also
1219 * copied into given parameter key_buf, which must be then
1220 * freed by caller. If file with key exists, key_buf
1221 * contain NULL and no private key is generated.
1224 * 1 : csp = Current client state (buffers, headers, etc...)
1225 * 2 : key_buf = buffer to save new generated key
1227 * Returns : -1 => Error while generating private key
1228 * 0 => Key already exists
1229 * >0 => Length of generated private key
1231 *********************************************************************/
1232 static int generate_key(struct client_state *csp, char **key_buf)
1235 char* key_file_path = NULL;
1236 BIGNUM *exp = BN_new();
1237 RSA *rsa = RSA_new();
1238 EVP_PKEY *key = EVP_PKEY_new();
1240 if (exp == NULL || rsa == NULL || key == NULL) {
1241 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1246 BN_set_word(exp, RSA_KEY_PUBLIC_EXPONENT);
1248 key_file_path = make_certs_path(csp->config->certificate_directory,
1249 (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1250 if (key_file_path == NULL)
1257 * Test if key already exists. If so, we don't have to create it again.
1259 if (file_exists(key_file_path) == 1)
1265 ret = RSA_generate_key_ex(rsa, RSA_KEYSIZE, exp, NULL);
1267 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key generation failure");
1272 if (!EVP_PKEY_set1_RSA(key, rsa)) {
1273 log_ssl_errors(LOG_LEVEL_ERROR, "Error assigning RSA key pair to PKEY structure");
1279 * Exporting private key into file
1281 if ((ret = write_private_key(key, key_buf, key_file_path)) < 0)
1283 log_error(LOG_LEVEL_ERROR,
1284 "Writing private key into file %s failed", key_file_path);
1291 * Freeing used variables
1293 if (exp) BN_free(exp);
1294 if (rsa) RSA_free(rsa);
1295 if (key) EVP_PKEY_free(key);
1296 freez(key_file_path);
1301 /*********************************************************************
1303 * Function : ssl_certificate_load
1305 * Description : Loads certificate from file.
1308 * 1 : cert_path = The certificate path to load
1310 * Returns : NULL => error loading certificate,
1311 * pointer to certificate instance otherwise
1313 *********************************************************************/
1314 static X509* ssl_certificate_load(const char *cert_path)
1317 FILE *cert_f = NULL;
1319 if (!(cert_f = fopen(cert_path, "r"))) {
1320 log_error(LOG_LEVEL_ERROR, "Error opening certificate file %s: %s", cert_path, strerror(errno));
1324 if (!(cert = PEM_read_X509(cert_f, NULL, NULL, NULL))) {
1325 log_ssl_errors(LOG_LEVEL_ERROR, "Error reading certificate file %s", cert_path);
1328 close_file_stream(cert_f, cert_path);
1333 /*********************************************************************
1335 * Function : ssl_certificate_is_invalid
1337 * Description : Checks whether or not a certificate is valid.
1338 * Currently only checks that the certificate can be
1339 * parsed and that the "valid to" date is in the future.
1342 * 1 : cert_file = The certificate to check
1344 * Returns : 0 => The certificate is valid.
1345 * 1 => The certificate is invalid
1347 *********************************************************************/
1348 static int ssl_certificate_is_invalid(const char *cert_file)
1354 if (!(cert = ssl_certificate_load(cert_file))) {
1355 log_ssl_errors(LOG_LEVEL_ERROR, "Error reading certificate file %s", cert_file);
1359 ret = X509_cmp_current_time(X509_get_notAfter(cert));
1361 log_ssl_errors(LOG_LEVEL_ERROR, "Error checking certificate %s validity", cert_file);
1366 return ret == -1 ? 1 : 0;
1369 /*********************************************************************
1371 * Function : set_x509_ext
1373 * Description : Sets the X509V3 extension data
1376 * 1 : cert = The certificate to modify
1377 * 2 : issuer = Issuer certificate
1378 * 3 : nid = OpenSSL NID
1379 * 2 : data = extension value
1381 * Returns : 0 => Error while setting extensuon data
1384 *********************************************************************/
1385 static int set_x509_ext(X509 *cert, X509 *issuer, int nid, const char *value)
1387 X509_EXTENSION * ext = NULL;
1391 X509V3_set_ctx(&ctx, issuer, cert, NULL, NULL, 0);
1392 ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
1395 log_ssl_errors(LOG_LEVEL_ERROR, "X509V3_EXT_conf_nid failure");
1399 if (!X509_add_ext(cert, ext, -1))
1401 log_ssl_errors(LOG_LEVEL_ERROR, "X509_add_ext failure");
1407 if (ext) X509_EXTENSION_free(ext);
1412 /*********************************************************************
1414 * Function : set_subject_alternative_name
1416 * Description : Sets the Subject Alternative Name extension to a cert
1419 * 1 : cert = The certificate to modify
1420 * 2 : issuer = Issuer certificate
1421 * 2 : hostname = The hostname to add
1423 * Returns : 0 => Error while creating certificate.
1426 *********************************************************************/
1427 static int set_subject_alternative_name(X509 *cert, X509 *issuer, const char *hostname)
1429 size_t altname_len = strlen(hostname) + sizeof(CERTIFICATE_ALT_NAME_PREFIX);
1430 char alt_name_buf[altname_len];
1432 snprintf(alt_name_buf, sizeof(alt_name_buf), CERTIFICATE_ALT_NAME_PREFIX"%s", hostname);
1433 return set_x509_ext(cert, issuer, NID_subject_alt_name, alt_name_buf);
1437 /*********************************************************************
1439 * Function : generate_webpage_certificate
1441 * Description : Creates certificate file in presetted directory.
1442 * If certificate already exists, no other certificate
1443 * will be created. Subject of certificate is named
1444 * by csp->http->host from parameter. This function also
1445 * triggers generating of private key for new certificate.
1448 * 1 : csp = Current client state (buffers, headers, etc...)
1450 * Returns : -1 => Error while creating certificate.
1451 * 0 => Certificate already exists.
1452 * 1 => Certificate created
1454 *********************************************************************/
1455 static int generate_webpage_certificate(struct client_state *csp)
1457 char *key_buf = NULL; /* Buffer for created key */
1458 X509* issuer_cert = NULL;
1461 EVP_PKEY *loaded_subject_key = NULL;
1462 EVP_PKEY *loaded_issuer_key = NULL;
1463 X509_NAME* issuer_name;
1464 X509_NAME* subject_name = NULL;
1465 ASN1_TIME* asn_time = NULL;
1466 ASN1_INTEGER *serial = NULL;
1467 BIGNUM *serial_num = NULL;
1470 cert_options cert_opt;
1471 char cert_valid_from[VALID_DATETIME_BUFLEN];
1472 char cert_valid_to[VALID_DATETIME_BUFLEN];
1474 /* Paths to keys and certificates needed to create certificate */
1475 cert_opt.issuer_key = NULL;
1476 cert_opt.subject_key = NULL;
1477 cert_opt.issuer_crt = NULL;
1479 cert_opt.output_file = make_certs_path(csp->config->certificate_directory,
1480 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
1481 if (cert_opt.output_file == NULL)
1486 cert_opt.subject_key = make_certs_path(csp->config->certificate_directory,
1487 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1488 if (cert_opt.subject_key == NULL)
1490 freez(cert_opt.output_file);
1494 if (file_exists(cert_opt.output_file) == 1)
1496 /* The file exists, but is it valid? */
1497 if (ssl_certificate_is_invalid(cert_opt.output_file))
1499 log_error(LOG_LEVEL_CONNECT,
1500 "Certificate %s is no longer valid. Removing it.",
1501 cert_opt.output_file);
1502 if (unlink(cert_opt.output_file))
1504 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1505 cert_opt.output_file);
1507 freez(cert_opt.output_file);
1508 freez(cert_opt.subject_key);
1512 if (unlink(cert_opt.subject_key))
1514 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1515 cert_opt.subject_key);
1517 freez(cert_opt.output_file);
1518 freez(cert_opt.subject_key);
1525 freez(cert_opt.output_file);
1526 freez(cert_opt.subject_key);
1533 * Create key for requested host
1535 int subject_key_len = generate_key(csp, &key_buf);
1536 if (subject_key_len < 0)
1538 freez(cert_opt.output_file);
1539 freez(cert_opt.subject_key);
1540 log_error(LOG_LEVEL_ERROR, "Key generating failed");
1545 * Converting unsigned long serial number to char * serial number.
1546 * We must compute length of serial number in string + terminating null.
1548 unsigned long certificate_serial = get_certificate_serial(csp);
1549 unsigned long certificate_serial_time = (unsigned long)time(NULL);
1550 int serial_num_size = snprintf(NULL, 0, "%lu%lu",
1551 certificate_serial_time, certificate_serial) + 1;
1552 if (serial_num_size <= 0)
1554 serial_num_size = 1;
1557 char serial_num_text[serial_num_size]; /* Buffer for serial number */
1558 ret = snprintf(serial_num_text, (size_t)serial_num_size, "%lu%lu",
1559 certificate_serial_time, certificate_serial);
1560 if (ret < 0 || ret >= serial_num_size)
1562 log_error(LOG_LEVEL_ERROR,
1563 "Converting certificate serial number into string failed");
1569 * Preparing parameters for certificate
1571 subject_name = X509_NAME_new();
1574 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1579 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COMMON_NAME_FCODE, MBSTRING_ASC,
1580 (void*)csp->http->host, -1, -1, 0))
1582 log_ssl_errors(LOG_LEVEL_ERROR, "X509 subject name (code: %s, val: %s) error",
1583 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1587 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORGANIZATION_FCODE, MBSTRING_ASC,
1588 (void*)csp->http->host, -1, -1, 0))
1590 log_ssl_errors(LOG_LEVEL_ERROR, "X509 subject name (code: %s, val: %s) error",
1591 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1595 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORG_UNIT_FCODE, MBSTRING_ASC,
1596 (void*)csp->http->host, -1, -1, 0))
1598 log_ssl_errors(LOG_LEVEL_ERROR, "X509 subject name (code: %s, val: %s) error",
1599 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1603 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COUNTRY_FCODE, MBSTRING_ASC,
1604 (void*)CERT_PARAM_COUNTRY_CODE, -1, -1, 0))
1606 log_ssl_errors(LOG_LEVEL_ERROR, "X509 subject name (code: %s, val: %s) error",
1607 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1612 cert_opt.issuer_crt = csp->config->ca_cert_file;
1613 cert_opt.issuer_key = csp->config->ca_key_file;
1615 if (get_certificate_valid_from_date(cert_valid_from, sizeof(cert_valid_from), VALID_DATETIME_FMT)
1616 || get_certificate_valid_to_date(cert_valid_to, sizeof(cert_valid_to), VALID_DATETIME_FMT))
1618 log_error(LOG_LEVEL_ERROR, "Generating one of the validity dates failed");
1623 cert_opt.subject_pwd = CERT_SUBJECT_PASSWORD;
1624 cert_opt.issuer_pwd = csp->config->ca_password;
1625 cert_opt.not_before = cert_valid_from;
1626 cert_opt.not_after = cert_valid_to;
1627 cert_opt.serial = serial_num_text;
1628 cert_opt.max_pathlen = -1;
1631 * Test if the private key was already created.
1632 * XXX: Can this still happen?
1634 if (subject_key_len == 0)
1636 log_error(LOG_LEVEL_ERROR, "Subject key was already created");
1642 * Parse serial to MPI
1644 serial_num = BN_new();
1647 log_error(LOG_LEVEL_ERROR, "generate_webpage_certificate: memory error");
1651 if (!BN_dec2bn(&serial_num, cert_opt.serial))
1653 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to parse serial %s", cert_opt.serial);
1658 if (!(serial = BN_to_ASN1_INTEGER(serial_num, NULL)))
1660 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to generate serial ASN1 representation");
1666 * Loading certificates
1668 if (!(issuer_cert = ssl_certificate_load(cert_opt.issuer_crt)))
1670 log_error(LOG_LEVEL_ERROR, "Loading issuer certificate %s failed", cert_opt.issuer_crt);
1675 issuer_name = X509_get_issuer_name(issuer_cert);
1678 * Loading keys from file or from buffer
1680 if (key_buf != NULL && subject_key_len > 0)
1682 pk_bio = BIO_new_mem_buf(key_buf, subject_key_len);
1684 else if (!(pk_bio = BIO_new_file(cert_opt.subject_key, "r")))
1686 log_ssl_errors(LOG_LEVEL_ERROR, "Failure opening subject key %s BIO", cert_opt.subject_key);
1691 loaded_subject_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL, (void*)cert_opt.subject_pwd);
1692 if (!loaded_subject_key)
1694 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing subject key %s failed", cert_opt.subject_key);
1699 if (!BIO_free(pk_bio)) {
1700 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing subject key BIO");
1703 if (!(pk_bio = BIO_new_file(cert_opt.issuer_key, "r")))
1705 log_ssl_errors(LOG_LEVEL_ERROR, "Failure opening issuer key %s BIO", cert_opt.issuer_key);
1710 loaded_issuer_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL, (void*)cert_opt.issuer_pwd);
1711 if (!loaded_issuer_key)
1713 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing issuer key %s failed", cert_opt.subject_key);
1721 log_ssl_errors(LOG_LEVEL_ERROR, "Certificate allocation error");
1726 if (!X509_set_version(cert, CERTIFICATE_VERSION))
1728 log_ssl_errors(LOG_LEVEL_ERROR, "X509_set_version failed");
1734 * Setting parameters of signed certificate
1736 if (!X509_set_pubkey(cert, loaded_subject_key)) {
1737 log_ssl_errors(LOG_LEVEL_ERROR, "Setting issuer name in signed certificate failed");
1742 if (!X509_set_subject_name(cert, subject_name))
1744 log_ssl_errors(LOG_LEVEL_ERROR, "Setting issuer name in signed certificate failed");
1749 if (!X509_set_issuer_name(cert, issuer_name))
1751 log_ssl_errors(LOG_LEVEL_ERROR, "Setting issuer name in signed certificate failed");
1756 if (!X509_set_serialNumber(cert, serial))
1758 log_ssl_errors(LOG_LEVEL_ERROR, "Setting serial number in signed certificate failed");
1763 asn_time = ASN1_TIME_new();
1765 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time memory allocation failure");
1770 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_after)) {
1771 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time [%s] encode error", cert_opt.not_after);
1776 if (!X509_set1_notAfter(cert, asn_time))
1778 log_ssl_errors(LOG_LEVEL_ERROR, "Setting valid not after in signed certificate failed");
1783 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_before)) {
1784 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time encode error");
1789 if (!X509_set1_notBefore(cert, asn_time))
1791 log_ssl_errors(LOG_LEVEL_ERROR, "Setting valid not befre in signed certificate failed");
1796 if(!set_x509_ext(cert, issuer_cert, NID_basic_constraints, CERTIFICATE_BASIC_CONSTRAINTS))
1798 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the basicConstraints extension "
1799 "in signed certificate failed");
1804 if (!set_x509_ext(cert, issuer_cert, NID_subject_key_identifier, CERTIFICATE_SUBJECT_KEY))
1806 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the Subject Key Identifie extension failed");
1811 if (!set_x509_ext(cert, issuer_cert, NID_authority_key_identifier, CERTIFICATE_AUTHORITY_KEY))
1813 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the Authority Key Identifier extension failed");
1818 if (!host_is_ip_address(csp->http->host) &&
1819 !set_subject_alternative_name(cert, issuer_cert, csp->http->host))
1821 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the Subject Alt Nameextension failed");
1826 if (!X509_sign(cert, loaded_issuer_key, EVP_sha256()))
1828 log_ssl_errors(LOG_LEVEL_ERROR, "Signing certificate failed");
1834 * Writing certificate into file
1836 if (write_certificate(cert, cert_opt.output_file) < 0)
1838 log_error(LOG_LEVEL_ERROR, "Writing certificate into file failed");
1847 * Freeing used structures
1849 if (issuer_cert) X509_free(issuer_cert);
1850 if (cert) X509_free(cert);
1851 if (pk_bio && !BIO_free(pk_bio)) {
1852 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing pk BIO");
1854 if (loaded_subject_key) EVP_PKEY_free(loaded_subject_key);
1855 if (loaded_issuer_key) EVP_PKEY_free(loaded_issuer_key);
1856 if (subject_name) X509_NAME_free(subject_name);
1857 if (asn_time) ASN1_TIME_free(asn_time);
1858 if (serial_num) BN_free(serial_num);
1859 if (serial) ASN1_INTEGER_free(serial);
1860 freez(cert_opt.subject_key);
1861 freez(cert_opt.output_file);
1868 /*********************************************************************
1870 * Function : ssl_crt_verify_info
1872 * Description : Returns an informational string about the verification
1873 * status of a certificate.
1876 * 1 : buf = Buffer to write to
1877 * 2 : size = Maximum size of buffer
1878 * 3 : csp = client state
1882 *********************************************************************/
1883 extern void ssl_crt_verify_info(char *buf, size_t size, struct client_state *csp)
1885 strncpy(buf, X509_verify_cert_error_string(csp->server_cert_verification_result), size);
1890 /*********************************************************************
1892 * Function : ssl_release
1894 * Description : Release all SSL resources
1900 *********************************************************************/
1901 extern void ssl_release(void)
1903 if (ssl_inited == 1)
1905 SSL_COMP_free_compression_methods();
1907 CONF_modules_free();
1908 CONF_modules_unload(1);
1910 COMP_zlib_cleanup();
1915 CRYPTO_cleanup_all_ex_data();