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>
9 * Copyright (C) 2017 Vaclav Svec. FIT CVUT.
10 * Copyright (C) 2018-2020 by Fabian Keil <fk@fabiankeil.de>
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * This program is distributed in the hope that it will
19 * be useful, but WITHOUT ANY WARRANTY; without even the
20 * implied warranty of MERCHANTABILITY or FITNESS FOR A
21 * PARTICULAR PURPOSE. See the GNU General Public
22 * License for more details.
24 * The GNU General Public License should be included with
25 * this file. If not, you can view it at
26 * http://www.gnu.org/copyleft/gpl.html
27 * or write to the Free Software Foundation, Inc., 59
28 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 *********************************************************************/
35 #include <openssl/bn.h>
36 #include <openssl/opensslv.h>
37 #include <openssl/pem.h>
38 #include <openssl/md5.h>
39 #include <openssl/x509v3.h>
48 #include "ssl_common.h"
51 * Macros for openssl.c
53 #define CERTIFICATE_BASIC_CONSTRAINTS "CA:FALSE"
54 #define CERTIFICATE_SUBJECT_KEY "hash"
55 #define CERTIFICATE_AUTHORITY_KEY "keyid:always"
56 #define CERTIFICATE_ALT_NAME_PREFIX "DNS:"
57 #define CERTIFICATE_VERSION 2
58 #define VALID_DATETIME_FMT "%Y%m%d%H%M%SZ"
59 #define VALID_DATETIME_BUFLEN 16
61 static int generate_webpage_certificate(struct client_state *csp);
62 static void free_client_ssl_structures(struct client_state *csp);
63 static void free_server_ssl_structures(struct client_state *csp);
64 static int ssl_store_cert(struct client_state *csp, X509* crt);
65 static void log_ssl_errors(int debuglevel, const char* fmt, ...) __attribute__((format(printf, 2, 3)));
67 static int ssl_inited = 0;
69 #if OPENSSL_VERSION_NUMBER < 0x10100000L
70 #define X509_set1_notBefore X509_set_notBefore
71 #define X509_set1_notAfter X509_set_notAfter
72 #define X509_get0_serialNumber X509_get_serialNumber
73 #define X509_get0_notBefore X509_get_notBefore
74 #define X509_get0_notAfter X509_get_notAfter
77 /*********************************************************************
79 * Function : openssl_init
81 * Description : Initializes OpenSSL library once
87 *********************************************************************/
88 static void openssl_init(void)
92 privoxy_mutex_lock(&ssl_init_mutex);
95 #if OPENSSL_VERSION_NUMBER < 0x10100000L
98 OPENSSL_init_ssl(0, NULL);
100 SSL_load_error_strings();
101 OpenSSL_add_ssl_algorithms();
104 privoxy_mutex_unlock(&ssl_init_mutex);
109 /*********************************************************************
111 * Function : is_ssl_pending
113 * Description : Tests if there are some waiting data on ssl connection.
114 * Only considers data that has actually been received
115 * locally and ignores data that is still on the fly
116 * or has not yet been sent by the remote end.
119 * 1 : ssl_attr = SSL context to test
121 * Returns : 0 => No data are pending
122 * >0 => Pending data length
124 *********************************************************************/
125 extern size_t is_ssl_pending(struct ssl_attr *ssl_attr)
127 BIO *bio = ssl_attr->openssl_attr.bio;
133 return (size_t)BIO_pending(bio);
137 /*********************************************************************
139 * Function : ssl_send_data
141 * Description : Sends the content of buf (for n bytes) to given SSL
142 * connection context.
145 * 1 : ssl_attr = SSL context to send data to
146 * 2 : buf = Pointer to data to be sent
147 * 3 : len = Length of data to be sent to the SSL context
149 * Returns : Length of sent data or negative value on error.
151 *********************************************************************/
152 extern int ssl_send_data(struct ssl_attr *ssl_attr, const unsigned char *buf, size_t len)
154 BIO *bio = ssl_attr->openssl_attr.bio;
156 int pos = 0; /* Position of unsent part in buffer */
165 int send_len = (int)len - pos;
167 log_error(LOG_LEVEL_WRITING, "TLS: %N", send_len, buf+pos);
170 * Sending one part of the buffer
172 while ((ret = BIO_write(bio,
173 (const unsigned char *)(buf + pos),
176 if (!BIO_should_retry(bio))
178 log_ssl_errors(LOG_LEVEL_ERROR,
179 "Sending data over TLS/SSL failed");
183 /* Adding count of sent bytes to position in buffer */
191 /*********************************************************************
193 * Function : ssl_recv_data
195 * Description : Receives data from given SSL context and puts
199 * 1 : ssl_attr = SSL context to receive data from
200 * 2 : buf = Pointer to buffer where data will be written
201 * 3 : max_length = Maximum number of bytes to read
203 * Returns : Number of bytes read, 0 for EOF, or -1
206 *********************************************************************/
207 extern int ssl_recv_data(struct ssl_attr *ssl_attr, unsigned char *buf, size_t max_length)
209 BIO *bio = ssl_attr->openssl_attr.bio;
211 memset(buf, 0, max_length);
214 * Receiving data from SSL context into buffer
218 ret = BIO_read(bio, buf, (int)max_length);
219 } while (ret <= 0 && BIO_should_retry(bio));
223 log_ssl_errors(LOG_LEVEL_ERROR,
224 "Receiving data over TLS/SSL failed");
229 log_error(LOG_LEVEL_RECEIVED, "TLS: %N", ret, buf);
235 /*********************************************************************
237 * Function : ssl_store_cert
239 * Description : This is a callback function for certificate verification.
240 * It's called once for each certificate in the server's
241 * certificate trusted chain and prepares information about
242 * the certificate. The information can be used to inform
243 * the user about invalid certificates.
246 * 1 : csp = Current client state (buffers, headers, etc...)
247 * 2 : crt = certificate from trusted chain
249 * Returns : 0 on success and negative value on error
251 *********************************************************************/
252 static int ssl_store_cert(struct client_state *csp, X509* crt)
255 struct certs_chain *last = &(csp->server_certs_chain);
257 BIO *bio = BIO_new(BIO_s_mem());
258 EVP_PKEY *pkey = NULL;
259 char *bio_mem_data = 0;
262 const ASN1_INTEGER *bs;
263 #if OPENSSL_VERSION_NUMBER > 0x10100000L
264 const X509_ALGOR *tsig_alg;
270 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
275 * Searching for last item in certificates linked list
277 while (last->next != NULL)
283 * Preparing next item in linked list for next certificate
285 last->next = malloc_or_die(sizeof(struct certs_chain));
286 last->next->next = NULL;
287 memset(last->next->info_buf, 0, sizeof(last->next->info_buf));
288 memset(last->next->file_buf, 0, sizeof(last->next->file_buf));
291 * Saving certificate file into buffer
293 if (!PEM_write_bio_X509(bio, crt))
295 log_ssl_errors(LOG_LEVEL_ERROR, "PEM_write_X509() failed");
300 len = BIO_get_mem_data(bio, &bio_mem_data);
302 if (len > (sizeof(last->file_buf) - 1))
304 log_error(LOG_LEVEL_ERROR,
305 "X509 PEM cert len %d is larger then buffer len %s",
306 len, sizeof(last->file_buf) - 1);
307 len = sizeof(last->file_buf) - 1;
310 strncpy(last->file_buf, bio_mem_data, (size_t)len);
312 bio = BIO_new(BIO_s_mem());
315 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
321 * Saving certificate information into buffer
323 l = X509_get_version(crt);
324 if (l >= 0 && l <= 2)
326 if (BIO_printf(bio, "cert. version : %ld\n", l + 1) <= 0)
328 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
335 if (BIO_printf(bio, "cert. version : Unknown (%ld)\n", l) <= 0)
337 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
343 if (BIO_puts(bio, "serial number : ") <= 0)
345 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_write() for serial failed");
349 bs = X509_get0_serialNumber(crt);
350 if (bs->length <= (int)sizeof(long))
353 l = ASN1_INTEGER_get(bs);
364 if (bs->type == V_ASN1_NEG_INTEGER)
366 ul = 0 - (unsigned long)l;
371 ul = (unsigned long)l;
374 if (BIO_printf(bio, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
376 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
383 if (bs->type == V_ASN1_NEG_INTEGER)
385 if (BIO_puts(bio, " (Negative)") < 0)
387 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
392 for (int i = 0; i < bs->length; i++)
394 if (BIO_printf(bio, "%02x%c", bs->data[i],
395 ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
397 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
404 if (BIO_puts(bio, "issuer name : ") <= 0)
406 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issuer failed");
410 if (X509_NAME_print_ex(bio, X509_get_issuer_name(crt), 0, 0) < 0)
412 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for issuer failed");
417 if (BIO_puts(bio, "\nsubject name : ") <= 0)
419 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for sublect failed");
423 if (X509_NAME_print_ex(bio, X509_get_subject_name(crt), 0, 0) < 0) {
424 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for subject failed");
429 if (BIO_puts(bio, "\nissued on : ") <= 0)
431 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issued on failed");
435 if (!ASN1_TIME_print(bio, X509_get0_notBefore(crt)))
437 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for issued on failed");
442 if (BIO_puts(bio, "\nexpires on : ") <= 0)
444 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for expires on failed");
448 if (!ASN1_TIME_print(bio, X509_get0_notAfter(crt)))
450 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for expires on failed");
455 #if OPENSSL_VERSION_NUMBER > 0x10100000L
456 if (BIO_puts(bio, "\nsigned using : ") <= 0)
458 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for signed using failed");
462 tsig_alg = X509_get0_tbs_sigalg(crt);
463 if (!i2a_ASN1_OBJECT(bio, tsig_alg->algorithm))
465 log_ssl_errors(LOG_LEVEL_ERROR, "i2a_ASN1_OBJECT() for signed using on failed");
470 pkey = X509_get_pubkey(crt);
473 log_ssl_errors(LOG_LEVEL_ERROR, "X509_get_pubkey() failed");
478 switch (EVP_PKEY_base_id(pkey))
481 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "RSA key size", EVP_PKEY_bits(pkey));
484 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "DSA key size", EVP_PKEY_bits(pkey));
487 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "non-RSA/DSA key size", EVP_PKEY_bits(pkey));
492 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key size failed");
497 loc = X509_get_ext_by_NID(crt, NID_basic_constraints, -1);
500 X509_EXTENSION *ex = X509_get_ext(crt, loc);
501 if (BIO_puts(bio, "\nbasic constraints : ") <= 0)
503 log_ssl_errors(LOG_LEVEL_ERROR,
504 "BIO_printf() for basic constraints failed");
508 if (!X509V3_EXT_print(bio, ex, 0, 0))
510 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253))
512 log_ssl_errors(LOG_LEVEL_ERROR,
513 "ASN1_STRING_print_ex() for basic constraints failed");
520 loc = X509_get_ext_by_NID(crt, NID_subject_alt_name, -1);
523 X509_EXTENSION *ex = X509_get_ext(crt, loc);
524 if (BIO_puts(bio, "\nsubject alt name : ") <= 0)
526 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for alt name failed");
530 if (!X509V3_EXT_print(bio, ex, 0, 0))
532 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
533 ASN1_STRFLGS_RFC2253))
535 log_ssl_errors(LOG_LEVEL_ERROR,
536 "ASN1_STRING_print_ex() for alt name failed");
543 loc = X509_get_ext_by_NID(crt, NID_netscape_cert_type, -1);
546 X509_EXTENSION *ex = X509_get_ext(crt, loc);
547 if (BIO_puts(bio, "\ncert. type : ") <= 0)
549 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for cert type failed");
553 if (!X509V3_EXT_print(bio, ex, 0, 0))
555 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
556 ASN1_STRFLGS_RFC2253))
558 log_ssl_errors(LOG_LEVEL_ERROR,
559 "ASN1_STRING_print_ex() for cert type failed");
566 loc = X509_get_ext_by_NID(crt, NID_key_usage, -1);
569 X509_EXTENSION *ex = X509_get_ext(crt, loc);
570 if (BIO_puts(bio, "\nkey usage : ") <= 0)
572 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key usage failed");
576 if (!X509V3_EXT_print(bio, ex, 0, 0))
578 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
579 ASN1_STRFLGS_RFC2253))
581 log_ssl_errors(LOG_LEVEL_ERROR,
582 "ASN1_STRING_print_ex() for key usage failed");
589 loc = X509_get_ext_by_NID(crt, NID_ext_key_usage, -1);
591 X509_EXTENSION *ex = X509_get_ext(crt, loc);
592 if (BIO_puts(bio, "\next key usage : ") <= 0)
594 log_ssl_errors(LOG_LEVEL_ERROR,
595 "BIO_printf() for ext key usage failed");
599 if (!X509V3_EXT_print(bio, ex, 0, 0))
601 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
602 ASN1_STRFLGS_RFC2253))
604 log_ssl_errors(LOG_LEVEL_ERROR,
605 "ASN1_STRING_print_ex() for ext key usage failed");
612 loc = X509_get_ext_by_NID(crt, NID_certificate_policies, -1);
615 X509_EXTENSION *ex = X509_get_ext(crt, loc);
616 if (BIO_puts(bio, "\ncertificate policies : ") <= 0)
618 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for certificate policies failed");
622 if (!X509V3_EXT_print(bio, ex, 0, 0))
624 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
625 ASN1_STRFLGS_RFC2253))
627 log_ssl_errors(LOG_LEVEL_ERROR,
628 "ASN1_STRING_print_ex() for certificate policies failed");
635 /* make valgrind happy */
636 static const char zero = 0;
637 BIO_write(bio, &zero, 1);
639 len = BIO_get_mem_data(bio, &bio_mem_data);
640 encoded_text = html_encode(bio_mem_data);
641 strlcpy(last->info_buf, encoded_text, sizeof(last->info_buf));
658 /*********************************************************************
660 * Function : host_to_hash
662 * Description : Creates MD5 hash from host name. Host name is loaded
663 * from structure csp and saved again into it.
666 * 1 : csp = Current client state (buffers, headers, etc...)
668 * Returns : 1 => Error while creating hash
669 * 0 => Hash created successfully
671 *********************************************************************/
672 static int host_to_hash(struct client_state *csp)
676 memset(csp->http->hash_of_host, 0, sizeof(csp->http->hash_of_host));
677 MD5((unsigned char *)csp->http->host, strlen(csp->http->host),
678 csp->http->hash_of_host);
680 /* Converting hash into string with hex */
684 if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
685 csp->http->hash_of_host[i])) < 0)
687 log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
696 /*********************************************************************
698 * Function : create_client_ssl_connection
700 * Description : Creates TLS/SSL secured connection with client
703 * 1 : csp = Current client state (buffers, headers, etc...)
705 * Returns : 0 on success, negative value if connection wasn't created
708 *********************************************************************/
709 extern int create_client_ssl_connection(struct client_state *csp)
711 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
712 /* Paths to certificates file and key file */
713 char *key_file = NULL;
714 char *ca_file = NULL;
715 char *cert_file = NULL;
720 * Initializing OpenSSL structures for TLS/SSL connection
725 * Preparing hash of host for creating certificates
727 ret = host_to_hash(csp);
730 log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
736 * Preparing paths to certificates files and key file
738 ca_file = csp->config->ca_cert_file;
739 cert_file = make_certs_path(csp->config->certificate_directory,
740 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
741 key_file = make_certs_path(csp->config->certificate_directory,
742 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
744 if (cert_file == NULL || key_file == NULL)
751 * Generating certificate for requested host. Mutex to prevent
752 * certificate and key inconsistence must be locked.
754 privoxy_mutex_lock(&certificate_mutex);
756 ret = generate_webpage_certificate(csp);
759 log_error(LOG_LEVEL_ERROR,
760 "Generate_webpage_certificate failed: %d", ret);
761 privoxy_mutex_unlock(&certificate_mutex);
765 privoxy_mutex_unlock(&certificate_mutex);
767 if (!(ssl_attr->openssl_attr.ctx = SSL_CTX_new(SSLv23_server_method())))
769 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create SSL context");
774 /* Set the key and cert */
775 if (SSL_CTX_use_certificate_file(ssl_attr->openssl_attr.ctx,
776 cert_file, SSL_FILETYPE_PEM) != 1)
778 log_ssl_errors(LOG_LEVEL_ERROR,
779 "Loading webpage certificate %s failed", cert_file);
784 if (SSL_CTX_use_PrivateKey_file(ssl_attr->openssl_attr.ctx,
785 key_file, SSL_FILETYPE_PEM) != 1)
787 log_ssl_errors(LOG_LEVEL_ERROR,
788 "Loading webpage certificate private key %s failed", key_file);
793 SSL_CTX_set_options(ssl_attr->openssl_attr.ctx, SSL_OP_NO_SSLv3);
795 if (!(ssl_attr->openssl_attr.bio = BIO_new_ssl(ssl_attr->openssl_attr.ctx, 0)))
797 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
802 if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1)
804 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
809 if (!SSL_set_fd(ssl, csp->cfd))
811 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
817 * Handshake with client
819 log_error(LOG_LEVEL_CONNECT,
820 "Performing the TLS/SSL handshake with client. Hash of host: %s",
821 csp->http->hash_of_host_hex);
822 if (BIO_do_handshake(ssl_attr->openssl_attr.bio) != 1)
824 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_do_handshake failed");
829 log_error(LOG_LEVEL_CONNECT, "Client successfully connected over TLS/SSL");
830 csp->ssl_with_client_is_opened = 1;
835 * Freeing allocated paths to files
840 /* Freeing structures if connection wasn't created successfully */
843 free_client_ssl_structures(csp);
849 /*********************************************************************
851 * Function : close_client_ssl_connection
853 * Description : Closes TLS/SSL connection with client. This function
854 * checks if this connection is already created.
857 * 1 : csp = Current client state (buffers, headers, etc...)
861 *********************************************************************/
862 extern void close_client_ssl_connection(struct client_state *csp)
864 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
866 if (csp->ssl_with_client_is_opened == 0)
872 * Notifying the peer that the connection is being closed.
874 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
875 free_client_ssl_structures(csp);
876 csp->ssl_with_client_is_opened = 0;
880 /*********************************************************************
882 * Function : free_client_ssl_structures
884 * Description : Frees structures used for SSL communication with
888 * 1 : csp = Current client state (buffers, headers, etc...)
892 *********************************************************************/
893 static void free_client_ssl_structures(struct client_state *csp)
895 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
897 if (ssl_attr->openssl_attr.bio)
899 BIO_free_all(ssl_attr->openssl_attr.bio);
901 if (ssl_attr->openssl_attr.ctx)
903 SSL_CTX_free(ssl_attr->openssl_attr.ctx);
908 /*********************************************************************
910 * Function : close_server_ssl_connection
912 * Description : Closes TLS/SSL connection with server. This function
913 * checks if this connection is already opened.
916 * 1 : csp = Current client state (buffers, headers, etc...)
920 *********************************************************************/
921 extern void close_server_ssl_connection(struct client_state *csp)
923 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
925 if (csp->ssl_with_server_is_opened == 0)
931 * Notifying the peer that the connection is being closed.
933 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
934 free_server_ssl_structures(csp);
935 csp->ssl_with_server_is_opened = 0;
939 /*********************************************************************
941 * Function : create_server_ssl_connection
943 * Description : Creates TLS/SSL secured connection with server.
946 * 1 : csp = Current client state (buffers, headers, etc...)
948 * Returns : 0 on success, negative value if connection wasn't created
951 *********************************************************************/
952 extern int create_server_ssl_connection(struct client_state *csp)
954 openssl_connection_attr *ssl_attrs = &csp->ssl_server_attr.openssl_attr;
956 char *trusted_cas_file = NULL;
957 STACK_OF(X509) *chain;
960 csp->server_cert_verification_result = SSL_CERT_NOT_VERIFIED;
961 csp->server_certs_chain.next = NULL;
963 /* Setting path to file with trusted CAs */
964 trusted_cas_file = csp->config->trusted_cas_file;
966 ssl_attrs->ctx = SSL_CTX_new(SSLv23_method());
969 log_ssl_errors(LOG_LEVEL_ERROR, "SSL context creation failed");
975 * Loading file with trusted CAs
977 if (!SSL_CTX_load_verify_locations(ssl_attrs->ctx, trusted_cas_file, NULL))
979 log_ssl_errors(LOG_LEVEL_ERROR, "Loading trusted CAs file %s failed",
985 SSL_CTX_set_verify(ssl_attrs->ctx, SSL_VERIFY_NONE, NULL);
986 SSL_CTX_set_options(ssl_attrs->ctx, SSL_OP_NO_SSLv3);
988 if (!(ssl_attrs->bio = BIO_new_ssl(ssl_attrs->ctx, 1)))
990 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
995 if (BIO_get_ssl(ssl_attrs->bio, &ssl) != 1)
997 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
1002 if (!SSL_set_fd(ssl, csp->server_connection.sfd))
1004 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
1010 * Set the hostname to check against the received server certificate
1012 if (!SSL_set1_host(ssl, csp->http->host))
1014 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set1_host failed");
1020 if (!SSL_set_tlsext_host_name(ssl, csp->http->host))
1022 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_tlsext_host_name failed");
1028 * Handshake with server
1030 log_error(LOG_LEVEL_CONNECT,
1031 "Performing the TLS/SSL handshake with the server");
1033 if (BIO_do_handshake(ssl_attrs->bio) != 1)
1035 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_do_handshake failed");
1040 chain = SSL_get_peer_cert_chain(ssl);
1043 for (int i = 0; i < sk_X509_num(chain); i++)
1045 if (ssl_store_cert(csp, sk_X509_value(chain, i)) != 0)
1047 log_error(LOG_LEVEL_ERROR, "ssl_store_cert failed");
1054 if (!csp->dont_verify_certificate)
1056 long verify_result = SSL_get_verify_result(ssl);
1057 if (verify_result == X509_V_OK)
1060 csp->server_cert_verification_result = SSL_CERT_VALID;
1064 csp->server_cert_verification_result = verify_result;
1065 log_error(LOG_LEVEL_ERROR, "SSL_get_verify_result failed: %s",
1066 X509_verify_cert_error_string(verify_result));
1072 log_error(LOG_LEVEL_CONNECT, "Server successfully connected over TLS/SSL");
1075 * Server certificate chain is valid, so we can clean
1076 * chain, because we will not send it to client.
1078 free_certificate_chain(csp);
1080 csp->ssl_with_server_is_opened = 1;
1082 /* Freeing structures if connection wasn't created successfully */
1085 free_server_ssl_structures(csp);
1092 /*********************************************************************
1094 * Function : free_server_ssl_structures
1096 * Description : Frees structures used for SSL communication with server
1099 * 1 : csp = Current client state (buffers, headers, etc...)
1103 *********************************************************************/
1104 static void free_server_ssl_structures(struct client_state *csp)
1106 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
1108 if (ssl_attr->openssl_attr.bio)
1110 BIO_free_all(ssl_attr->openssl_attr.bio);
1112 if (ssl_attr->openssl_attr.ctx)
1114 SSL_CTX_free(ssl_attr->openssl_attr.ctx);
1119 /*********************************************************************
1121 * Function : log_ssl_errors
1123 * Description : Log SSL errors
1126 * 1 : debuglevel = Debug level
1127 * 2 : desc = Error description
1131 *********************************************************************/
1132 static void log_ssl_errors(int debuglevel, const char* fmt, ...)
1134 unsigned long err_code;
1135 char prefix[ERROR_BUF_SIZE];
1137 va_start(args, fmt);
1138 vsnprintf(prefix, sizeof(prefix), fmt, args);
1141 while ((err_code = ERR_get_error()))
1143 char err_buf[ERROR_BUF_SIZE];
1145 ERR_error_string_n(err_code, err_buf, sizeof(err_buf));
1146 log_error(debuglevel, "%s: %s", prefix, err_buf);
1150 * In case if called by mistake and there were
1151 * no SSL errors let's report it to the log.
1155 log_error(debuglevel, "%s: no ssl errors detected", prefix);
1160 /*********************************************************************
1162 * Function : ssl_base64_encode
1164 * Description : Encode a buffer into base64 format.
1167 * 1 : dst = Destination buffer
1168 * 2 : dlen = Destination buffer length
1169 * 3 : olen = Number of bytes written
1170 * 4 : src = Source buffer
1171 * 5 : slen = Amount of data to be encoded
1173 * Returns : 0 on success, error code othervise
1175 *********************************************************************/
1176 extern int ssl_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
1177 const unsigned char *src, size_t slen)
1179 *olen = 4 * ((slen/3) + ((slen%3) ? 1 : 0)) + 1;
1184 *olen = (size_t)EVP_EncodeBlock(dst, src, (int)slen) + 1;
1189 /*********************************************************************
1191 * Function : close_file_stream
1193 * Description : Close file stream, report error on close error
1196 * 1 : f = file stream to close
1197 * 2 : path = path for error report
1201 *********************************************************************/
1202 static void close_file_stream(FILE *f, const char *path)
1206 log_error(LOG_LEVEL_ERROR,
1207 "Error closing file %s: %s", path, strerror(errno));
1212 /*********************************************************************
1214 * Function : write_certificate
1216 * Description : Writes certificate into file.
1219 * 1 : crt = certificate to write into file
1220 * 2 : output_file = path to save certificate file
1223 * Returns : 1 on success success or negative value
1225 *********************************************************************/
1226 static int write_certificate(X509 *crt, const char *output_file)
1232 * Saving certificate into file
1234 if ((f = fopen(output_file, "w")) == NULL)
1236 log_error(LOG_LEVEL_ERROR, "Opening file %s to save certificate failed",
1241 ret = PEM_write_X509(f, crt);
1244 log_ssl_errors(LOG_LEVEL_ERROR,
1245 "Writing certificate into file %s failed", output_file);
1249 close_file_stream(f, output_file);
1254 /*********************************************************************
1256 * Function : write_private_key
1258 * Description : Writes private key into file and copies saved
1259 * content into given pointer to string. If function
1260 * returns 0 for success, this copy must be freed by
1264 * 1 : key = key to write into file
1265 * 2 : ret_buf = pointer to string with created key file content
1266 * 3 : key_file_path = path where to save key file
1268 * Returns : Length of written private key on success or negative value
1271 *********************************************************************/
1272 static int write_private_key(EVP_PKEY *key, char **ret_buf,
1273 const char *key_file_path)
1275 size_t len = 0; /* Length of created key */
1276 FILE *f = NULL; /* File to save certificate */
1278 BIO *bio_mem = BIO_new(BIO_s_mem());
1279 char *bio_mem_data = 0;
1281 if (bio_mem == NULL)
1283 log_ssl_errors(LOG_LEVEL_ERROR, "write_private_key memory allocation failure");
1288 * Writing private key into PEM string
1290 if (!PEM_write_bio_PrivateKey(bio_mem, key, NULL, NULL, 0, NULL, NULL))
1292 log_ssl_errors(LOG_LEVEL_ERROR,
1293 "Writing private key into PEM string failed");
1298 len = (size_t)BIO_get_mem_data(bio_mem, &bio_mem_data);
1300 /* Initializing buffer for key file content */
1301 *ret_buf = zalloc_or_die(len + 1);
1302 (*ret_buf)[len] = 0;
1304 strncpy(*ret_buf, bio_mem_data, len);
1307 * Saving key into file
1309 if ((f = fopen(key_file_path, "wb")) == NULL)
1311 log_error(LOG_LEVEL_ERROR,
1312 "Opening file %s to save private key failed: %E",
1318 if (fwrite(*ret_buf, 1, len, f) != len)
1320 log_error(LOG_LEVEL_ERROR,
1321 "Writing private key into file %s failed",
1323 close_file_stream(f, key_file_path);
1328 close_file_stream(f, key_file_path);
1342 /*********************************************************************
1344 * Function : generate_key
1346 * Description : Tests if private key for host saved in csp already
1347 * exists. If this file doesn't exists, a new key is
1348 * generated and saved in a file. The generated key is also
1349 * copied into given parameter key_buf, which must be then
1350 * freed by caller. If file with key exists, key_buf
1351 * contain NULL and no private key is generated.
1354 * 1 : csp = Current client state (buffers, headers, etc...)
1355 * 2 : key_buf = buffer to save new generated key
1357 * Returns : -1 => Error while generating private key
1358 * 0 => Key already exists
1359 * >0 => Length of generated private key
1361 *********************************************************************/
1362 static int generate_key(struct client_state *csp, char **key_buf)
1365 char* key_file_path = NULL;
1366 BIGNUM *exp = BN_new();
1367 RSA *rsa = RSA_new();
1368 EVP_PKEY *key = EVP_PKEY_new();
1370 if (exp == NULL || rsa == NULL || key == NULL)
1372 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1377 BN_set_word(exp, RSA_KEY_PUBLIC_EXPONENT);
1379 key_file_path = make_certs_path(csp->config->certificate_directory,
1380 (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1381 if (key_file_path == NULL)
1388 * Test if key already exists. If so, we don't have to create it again.
1390 if (file_exists(key_file_path) == 1)
1396 ret = RSA_generate_key_ex(rsa, RSA_KEYSIZE, exp, NULL);
1399 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key generation failure");
1404 if (!EVP_PKEY_set1_RSA(key, rsa))
1406 log_ssl_errors(LOG_LEVEL_ERROR,
1407 "Error assigning RSA key pair to PKEY structure");
1413 * Exporting private key into file
1415 if ((ret = write_private_key(key, key_buf, key_file_path)) < 0)
1417 log_error(LOG_LEVEL_ERROR,
1418 "Writing private key into file %s failed", key_file_path);
1425 * Freeing used variables
1439 freez(key_file_path);
1445 /*********************************************************************
1447 * Function : ssl_certificate_load
1449 * Description : Loads certificate from file.
1452 * 1 : cert_path = The certificate path to load
1454 * Returns : NULL => error loading certificate,
1455 * pointer to certificate instance otherwise
1457 *********************************************************************/
1458 static X509* ssl_certificate_load(const char *cert_path)
1461 FILE *cert_f = NULL;
1463 if (!(cert_f = fopen(cert_path, "r")))
1465 log_error(LOG_LEVEL_ERROR,
1466 "Error opening certificate file %s: %s", cert_path, strerror(errno));
1470 if (!(cert = PEM_read_X509(cert_f, NULL, NULL, NULL)))
1472 log_ssl_errors(LOG_LEVEL_ERROR,
1473 "Error reading certificate file %s", cert_path);
1476 close_file_stream(cert_f, cert_path);
1481 /*********************************************************************
1483 * Function : ssl_certificate_is_invalid
1485 * Description : Checks whether or not a certificate is valid.
1486 * Currently only checks that the certificate can be
1487 * parsed and that the "valid to" date is in the future.
1490 * 1 : cert_file = The certificate to check
1492 * Returns : 0 => The certificate is valid.
1493 * 1 => The certificate is invalid
1495 *********************************************************************/
1496 static int ssl_certificate_is_invalid(const char *cert_file)
1502 if (!(cert = ssl_certificate_load(cert_file)))
1504 log_ssl_errors(LOG_LEVEL_ERROR,
1505 "Error reading certificate file %s", cert_file);
1509 ret = X509_cmp_current_time(X509_get_notAfter(cert));
1512 log_ssl_errors(LOG_LEVEL_ERROR,
1513 "Error checking certificate %s validity", cert_file);
1518 return ret == -1 ? 1 : 0;
1522 /*********************************************************************
1524 * Function : set_x509_ext
1526 * Description : Sets the X509V3 extension data
1529 * 1 : cert = The certificate to modify
1530 * 2 : issuer = Issuer certificate
1531 * 3 : nid = OpenSSL NID
1532 * 4 : value = extension value
1534 * Returns : 0 => Error while setting extensuon data
1537 *********************************************************************/
1538 static int set_x509_ext(X509 *cert, X509 *issuer, int nid, char *value)
1540 X509_EXTENSION *ext = NULL;
1544 X509V3_set_ctx(&ctx, issuer, cert, NULL, NULL, 0);
1545 ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
1548 log_ssl_errors(LOG_LEVEL_ERROR, "X509V3_EXT_conf_nid failure");
1552 if (!X509_add_ext(cert, ext, -1))
1554 log_ssl_errors(LOG_LEVEL_ERROR, "X509_add_ext failure");
1562 X509_EXTENSION_free(ext);
1568 /*********************************************************************
1570 * Function : set_subject_alternative_name
1572 * Description : Sets the Subject Alternative Name extension to a cert
1575 * 1 : cert = The certificate to modify
1576 * 2 : issuer = Issuer certificate
1577 * 3 : hostname = The hostname to add
1579 * Returns : 0 => Error while creating certificate.
1582 *********************************************************************/
1583 static int set_subject_alternative_name(X509 *cert, X509 *issuer, const char *hostname)
1585 size_t altname_len = strlen(hostname) + sizeof(CERTIFICATE_ALT_NAME_PREFIX);
1586 char alt_name_buf[altname_len];
1588 snprintf(alt_name_buf, sizeof(alt_name_buf),
1589 CERTIFICATE_ALT_NAME_PREFIX"%s", hostname);
1590 return set_x509_ext(cert, issuer, NID_subject_alt_name, alt_name_buf);
1594 /*********************************************************************
1596 * Function : generate_webpage_certificate
1598 * Description : Creates certificate file in presetted directory.
1599 * If certificate already exists, no other certificate
1600 * will be created. Subject of certificate is named
1601 * by csp->http->host from parameter. This function also
1602 * triggers generating of private key for new certificate.
1605 * 1 : csp = Current client state (buffers, headers, etc...)
1607 * Returns : -1 => Error while creating certificate.
1608 * 0 => Certificate already exists.
1609 * 1 => Certificate created
1611 *********************************************************************/
1612 static int generate_webpage_certificate(struct client_state *csp)
1614 char *key_buf = NULL; /* Buffer for created key */
1615 X509 *issuer_cert = NULL;
1618 EVP_PKEY *loaded_subject_key = NULL;
1619 EVP_PKEY *loaded_issuer_key = NULL;
1620 X509_NAME *issuer_name;
1621 X509_NAME *subject_name = NULL;
1622 ASN1_TIME *asn_time = NULL;
1623 ASN1_INTEGER *serial = NULL;
1624 BIGNUM *serial_num = NULL;
1627 cert_options cert_opt;
1628 char cert_valid_from[VALID_DATETIME_BUFLEN];
1629 char cert_valid_to[VALID_DATETIME_BUFLEN];
1631 /* Paths to keys and certificates needed to create certificate */
1632 cert_opt.issuer_key = NULL;
1633 cert_opt.subject_key = NULL;
1634 cert_opt.issuer_crt = NULL;
1636 cert_opt.output_file = make_certs_path(csp->config->certificate_directory,
1637 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
1638 if (cert_opt.output_file == NULL)
1643 cert_opt.subject_key = make_certs_path(csp->config->certificate_directory,
1644 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1645 if (cert_opt.subject_key == NULL)
1647 freez(cert_opt.output_file);
1651 if (file_exists(cert_opt.output_file) == 1)
1653 /* The file exists, but is it valid? */
1654 if (ssl_certificate_is_invalid(cert_opt.output_file))
1656 log_error(LOG_LEVEL_CONNECT,
1657 "Certificate %s is no longer valid. Removing it.",
1658 cert_opt.output_file);
1659 if (unlink(cert_opt.output_file))
1661 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1662 cert_opt.output_file);
1664 freez(cert_opt.output_file);
1665 freez(cert_opt.subject_key);
1669 if (unlink(cert_opt.subject_key))
1671 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1672 cert_opt.subject_key);
1674 freez(cert_opt.output_file);
1675 freez(cert_opt.subject_key);
1682 freez(cert_opt.output_file);
1683 freez(cert_opt.subject_key);
1690 * Create key for requested host
1692 int subject_key_len = generate_key(csp, &key_buf);
1693 if (subject_key_len < 0)
1695 freez(cert_opt.output_file);
1696 freez(cert_opt.subject_key);
1697 log_error(LOG_LEVEL_ERROR, "Key generating failed");
1702 * Converting unsigned long serial number to char * serial number.
1703 * We must compute length of serial number in string + terminating null.
1705 unsigned long certificate_serial = get_certificate_serial(csp);
1706 unsigned long certificate_serial_time = (unsigned long)time(NULL);
1707 int serial_num_size = snprintf(NULL, 0, "%lu%lu",
1708 certificate_serial_time, certificate_serial) + 1;
1709 if (serial_num_size <= 0)
1711 serial_num_size = 1;
1714 char serial_num_text[serial_num_size]; /* Buffer for serial number */
1715 ret = snprintf(serial_num_text, (size_t)serial_num_size, "%lu%lu",
1716 certificate_serial_time, certificate_serial);
1717 if (ret < 0 || ret >= serial_num_size)
1719 log_error(LOG_LEVEL_ERROR,
1720 "Converting certificate serial number into string failed");
1726 * Preparing parameters for certificate
1728 subject_name = X509_NAME_new();
1731 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1736 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COMMON_NAME_FCODE,
1737 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1739 log_ssl_errors(LOG_LEVEL_ERROR,
1740 "X509 subject name (code: %s, val: %s) error",
1741 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1745 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORGANIZATION_FCODE,
1746 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1748 log_ssl_errors(LOG_LEVEL_ERROR,
1749 "X509 subject name (code: %s, val: %s) error",
1750 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1754 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORG_UNIT_FCODE,
1755 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1757 log_ssl_errors(LOG_LEVEL_ERROR,
1758 "X509 subject name (code: %s, val: %s) error",
1759 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1763 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COUNTRY_FCODE,
1764 MBSTRING_ASC, (void *)CERT_PARAM_COUNTRY_CODE, -1, -1, 0))
1766 log_ssl_errors(LOG_LEVEL_ERROR,
1767 "X509 subject name (code: %s, val: %s) error",
1768 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1773 cert_opt.issuer_crt = csp->config->ca_cert_file;
1774 cert_opt.issuer_key = csp->config->ca_key_file;
1776 if (get_certificate_valid_from_date(cert_valid_from,
1777 sizeof(cert_valid_from), VALID_DATETIME_FMT)
1778 || get_certificate_valid_to_date(cert_valid_to,
1779 sizeof(cert_valid_to), VALID_DATETIME_FMT))
1781 log_error(LOG_LEVEL_ERROR, "Generating one of the validity dates failed");
1786 cert_opt.subject_pwd = CERT_SUBJECT_PASSWORD;
1787 cert_opt.issuer_pwd = csp->config->ca_password;
1788 cert_opt.not_before = cert_valid_from;
1789 cert_opt.not_after = cert_valid_to;
1790 cert_opt.serial = serial_num_text;
1791 cert_opt.max_pathlen = -1;
1794 * Test if the private key was already created.
1795 * XXX: Can this still happen?
1797 if (subject_key_len == 0)
1799 log_error(LOG_LEVEL_ERROR, "Subject key was already created");
1805 * Parse serial to MPI
1807 serial_num = BN_new();
1810 log_error(LOG_LEVEL_ERROR, "generate_webpage_certificate: memory error");
1814 if (!BN_dec2bn(&serial_num, cert_opt.serial))
1816 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to parse serial %s", cert_opt.serial);
1821 if (!(serial = BN_to_ASN1_INTEGER(serial_num, NULL)))
1823 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to generate serial ASN1 representation");
1829 * Loading certificates
1831 if (!(issuer_cert = ssl_certificate_load(cert_opt.issuer_crt)))
1833 log_error(LOG_LEVEL_ERROR, "Loading issuer certificate %s failed",
1834 cert_opt.issuer_crt);
1839 issuer_name = X509_get_issuer_name(issuer_cert);
1842 * Loading keys from file or from buffer
1844 if (key_buf != NULL && subject_key_len > 0)
1846 pk_bio = BIO_new_mem_buf(key_buf, subject_key_len);
1848 else if (!(pk_bio = BIO_new_file(cert_opt.subject_key, "r")))
1850 log_ssl_errors(LOG_LEVEL_ERROR,
1851 "Failure opening subject key %s BIO", cert_opt.subject_key);
1856 loaded_subject_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL,
1857 (void *)cert_opt.subject_pwd);
1858 if (!loaded_subject_key)
1860 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing subject key %s failed",
1861 cert_opt.subject_key);
1866 if (!BIO_free(pk_bio))
1868 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing subject key BIO");
1871 if (!(pk_bio = BIO_new_file(cert_opt.issuer_key, "r")))
1873 log_ssl_errors(LOG_LEVEL_ERROR, "Failure opening issuer key %s BIO",
1874 cert_opt.issuer_key);
1879 loaded_issuer_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL,
1880 (void *)cert_opt.issuer_pwd);
1881 if (!loaded_issuer_key)
1883 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing issuer key %s failed",
1884 cert_opt.subject_key);
1892 log_ssl_errors(LOG_LEVEL_ERROR, "Certificate allocation error");
1897 if (!X509_set_version(cert, CERTIFICATE_VERSION))
1899 log_ssl_errors(LOG_LEVEL_ERROR, "X509_set_version failed");
1905 * Setting parameters of signed certificate
1907 if (!X509_set_pubkey(cert, loaded_subject_key))
1909 log_ssl_errors(LOG_LEVEL_ERROR,
1910 "Setting issuer name in signed certificate failed");
1915 if (!X509_set_subject_name(cert, subject_name))
1917 log_ssl_errors(LOG_LEVEL_ERROR,
1918 "Setting issuer name in signed certificate failed");
1923 if (!X509_set_issuer_name(cert, issuer_name))
1925 log_ssl_errors(LOG_LEVEL_ERROR,
1926 "Setting issuer name in signed certificate failed");
1931 if (!X509_set_serialNumber(cert, serial))
1933 log_ssl_errors(LOG_LEVEL_ERROR,
1934 "Setting serial number in signed certificate failed");
1939 asn_time = ASN1_TIME_new();
1942 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time memory allocation failure");
1947 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_after))
1949 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time [%s] encode error", cert_opt.not_after);
1954 if (!X509_set1_notAfter(cert, asn_time))
1956 log_ssl_errors(LOG_LEVEL_ERROR,
1957 "Setting valid not after in signed certificate failed");
1962 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_before))
1964 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time encode error");
1969 if (!X509_set1_notBefore(cert, asn_time))
1971 log_ssl_errors(LOG_LEVEL_ERROR,
1972 "Setting valid not befre in signed certificate failed");
1977 if (!set_x509_ext(cert, issuer_cert, NID_basic_constraints, CERTIFICATE_BASIC_CONSTRAINTS))
1979 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the basicConstraints extension "
1980 "in signed certificate failed");
1985 if (!set_x509_ext(cert, issuer_cert, NID_subject_key_identifier, CERTIFICATE_SUBJECT_KEY))
1987 log_ssl_errors(LOG_LEVEL_ERROR,
1988 "Setting the Subject Key Identifie extension failed");
1993 if (!set_x509_ext(cert, issuer_cert, NID_authority_key_identifier, CERTIFICATE_AUTHORITY_KEY))
1995 log_ssl_errors(LOG_LEVEL_ERROR,
1996 "Setting the Authority Key Identifier extension failed");
2001 if (!host_is_ip_address(csp->http->host) &&
2002 !set_subject_alternative_name(cert, issuer_cert, csp->http->host))
2004 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the Subject Alt Nameextension failed");
2009 if (!X509_sign(cert, loaded_issuer_key, EVP_sha256()))
2011 log_ssl_errors(LOG_LEVEL_ERROR, "Signing certificate failed");
2017 * Writing certificate into file
2019 if (write_certificate(cert, cert_opt.output_file) < 0)
2021 log_error(LOG_LEVEL_ERROR, "Writing certificate into file failed");
2030 * Freeing used structures
2034 X509_free(issuer_cert);
2040 if (pk_bio && !BIO_free(pk_bio))
2042 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing pk BIO");
2044 if (loaded_subject_key)
2046 EVP_PKEY_free(loaded_subject_key);
2048 if (loaded_issuer_key)
2050 EVP_PKEY_free(loaded_issuer_key);
2054 X509_NAME_free(subject_name);
2058 ASN1_TIME_free(asn_time);
2062 BN_free(serial_num);
2066 ASN1_INTEGER_free(serial);
2068 freez(cert_opt.subject_key);
2069 freez(cert_opt.output_file);
2076 /*********************************************************************
2078 * Function : ssl_crt_verify_info
2080 * Description : Returns an informational string about the verification
2081 * status of a certificate.
2084 * 1 : buf = Buffer to write to
2085 * 2 : size = Maximum size of buffer
2086 * 3 : csp = client state
2090 *********************************************************************/
2091 extern void ssl_crt_verify_info(char *buf, size_t size, struct client_state *csp)
2093 strncpy(buf, X509_verify_cert_error_string(csp->server_cert_verification_result), size);
2098 /*********************************************************************
2100 * Function : ssl_release
2102 * Description : Release all SSL resources
2108 *********************************************************************/
2109 extern void ssl_release(void)
2111 if (ssl_inited == 1)
2113 SSL_COMP_free_compression_methods();
2115 CONF_modules_free();
2116 CONF_modules_unload(1);
2118 COMP_zlib_cleanup();
2123 CRYPTO_cleanup_all_ex_data();