1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/openssl.c,v $
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 function is called once for each certificate in the
240 * server's certificate trusted chain and prepares
241 * information about the certificate. The information can
242 * be used to inform the user about invalid certificates.
245 * 1 : csp = Current client state (buffers, headers, etc...)
246 * 2 : crt = certificate from trusted chain
248 * Returns : 0 on success and negative value on error
250 *********************************************************************/
251 static int ssl_store_cert(struct client_state *csp, X509* crt)
254 struct certs_chain *last = &(csp->server_certs_chain);
256 BIO *bio = BIO_new(BIO_s_mem());
257 EVP_PKEY *pkey = NULL;
258 char *bio_mem_data = 0;
261 const ASN1_INTEGER *bs;
262 #if OPENSSL_VERSION_NUMBER > 0x10100000L
263 const X509_ALGOR *tsig_alg;
269 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
274 * Searching for last item in certificates linked list
276 while (last->next != NULL)
282 * Preparing next item in linked list for next certificate
284 last->next = malloc_or_die(sizeof(struct certs_chain));
285 last->next->next = NULL;
286 memset(last->next->info_buf, 0, sizeof(last->next->info_buf));
287 memset(last->next->file_buf, 0, sizeof(last->next->file_buf));
290 * Saving certificate file into buffer
292 if (!PEM_write_bio_X509(bio, crt))
294 log_ssl_errors(LOG_LEVEL_ERROR, "PEM_write_X509() failed");
299 len = BIO_get_mem_data(bio, &bio_mem_data);
301 if (len > (sizeof(last->file_buf) - 1))
303 log_error(LOG_LEVEL_ERROR,
304 "X509 PEM cert len %d is larger then buffer len %s",
305 len, sizeof(last->file_buf) - 1);
306 len = sizeof(last->file_buf) - 1;
309 strncpy(last->file_buf, bio_mem_data, (size_t)len);
311 bio = BIO_new(BIO_s_mem());
314 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_new_mem_buf() failed");
320 * Saving certificate information into buffer
322 l = X509_get_version(crt);
323 if (l >= 0 && l <= 2)
325 if (BIO_printf(bio, "cert. version : %ld\n", l + 1) <= 0)
327 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
334 if (BIO_printf(bio, "cert. version : Unknown (%ld)\n", l) <= 0)
336 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for version failed");
342 if (BIO_puts(bio, "serial number : ") <= 0)
344 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
348 bs = X509_get0_serialNumber(crt);
349 if (bs->length <= (int)sizeof(long))
352 l = ASN1_INTEGER_get(bs);
363 if (bs->type == V_ASN1_NEG_INTEGER)
365 ul = 0 - (unsigned long)l;
370 ul = (unsigned long)l;
373 if (BIO_printf(bio, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
375 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
382 if (bs->type == V_ASN1_NEG_INTEGER)
384 if (BIO_puts(bio, " (Negative)") < 0)
386 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for serial failed");
391 for (int i = 0; i < bs->length; i++)
393 if (BIO_printf(bio, "%02x%c", bs->data[i],
394 ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
396 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for serial failed");
403 if (BIO_puts(bio, "issuer name : ") <= 0)
405 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issuer failed");
409 if (X509_NAME_print_ex(bio, X509_get_issuer_name(crt), 0, 0) < 0)
411 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for issuer failed");
416 if (BIO_puts(bio, "\nsubject name : ") <= 0)
418 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for subject failed");
422 if (X509_NAME_print_ex(bio, X509_get_subject_name(crt), 0, 0) < 0) {
423 log_ssl_errors(LOG_LEVEL_ERROR, "X509_NAME_print_ex() for subject failed");
428 if (BIO_puts(bio, "\nissued on : ") <= 0)
430 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for issued on failed");
434 if (!ASN1_TIME_print(bio, X509_get0_notBefore(crt)))
436 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for issued on failed");
441 if (BIO_puts(bio, "\nexpires on : ") <= 0)
443 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for expires on failed");
447 if (!ASN1_TIME_print(bio, X509_get0_notAfter(crt)))
449 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1_TIME_print() for expires on failed");
454 #if OPENSSL_VERSION_NUMBER > 0x10100000L
455 if (BIO_puts(bio, "\nsigned using : ") <= 0)
457 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_puts() for signed using failed");
461 tsig_alg = X509_get0_tbs_sigalg(crt);
462 if (!i2a_ASN1_OBJECT(bio, tsig_alg->algorithm))
464 log_ssl_errors(LOG_LEVEL_ERROR, "i2a_ASN1_OBJECT() for signed using failed");
469 pkey = X509_get_pubkey(crt);
472 log_ssl_errors(LOG_LEVEL_ERROR, "X509_get_pubkey() failed");
477 switch (EVP_PKEY_base_id(pkey))
480 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "RSA key size", EVP_PKEY_bits(pkey));
483 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "DSA key size", EVP_PKEY_bits(pkey));
486 ret = BIO_printf(bio, "\n%-" BC "s: %d bits", "non-RSA/DSA key size", EVP_PKEY_bits(pkey));
491 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key size failed");
496 loc = X509_get_ext_by_NID(crt, NID_basic_constraints, -1);
499 X509_EXTENSION *ex = X509_get_ext(crt, loc);
500 if (BIO_puts(bio, "\nbasic constraints : ") <= 0)
502 log_ssl_errors(LOG_LEVEL_ERROR,
503 "BIO_printf() for basic constraints failed");
507 if (!X509V3_EXT_print(bio, ex, 0, 0))
509 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex), ASN1_STRFLGS_RFC2253))
511 log_ssl_errors(LOG_LEVEL_ERROR,
512 "ASN1_STRING_print_ex() for basic constraints failed");
519 loc = X509_get_ext_by_NID(crt, NID_subject_alt_name, -1);
522 X509_EXTENSION *ex = X509_get_ext(crt, loc);
523 if (BIO_puts(bio, "\nsubject alt name : ") <= 0)
525 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for alt name failed");
529 if (!X509V3_EXT_print(bio, ex, 0, 0))
531 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
532 ASN1_STRFLGS_RFC2253))
534 log_ssl_errors(LOG_LEVEL_ERROR,
535 "ASN1_STRING_print_ex() for alt name failed");
542 loc = X509_get_ext_by_NID(crt, NID_netscape_cert_type, -1);
545 X509_EXTENSION *ex = X509_get_ext(crt, loc);
546 if (BIO_puts(bio, "\ncert. type : ") <= 0)
548 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for cert type failed");
552 if (!X509V3_EXT_print(bio, ex, 0, 0))
554 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
555 ASN1_STRFLGS_RFC2253))
557 log_ssl_errors(LOG_LEVEL_ERROR,
558 "ASN1_STRING_print_ex() for cert type failed");
565 loc = X509_get_ext_by_NID(crt, NID_key_usage, -1);
568 X509_EXTENSION *ex = X509_get_ext(crt, loc);
569 if (BIO_puts(bio, "\nkey usage : ") <= 0)
571 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for key usage failed");
575 if (!X509V3_EXT_print(bio, ex, 0, 0))
577 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
578 ASN1_STRFLGS_RFC2253))
580 log_ssl_errors(LOG_LEVEL_ERROR,
581 "ASN1_STRING_print_ex() for key usage failed");
588 loc = X509_get_ext_by_NID(crt, NID_ext_key_usage, -1);
590 X509_EXTENSION *ex = X509_get_ext(crt, loc);
591 if (BIO_puts(bio, "\next key usage : ") <= 0)
593 log_ssl_errors(LOG_LEVEL_ERROR,
594 "BIO_printf() for ext key usage failed");
598 if (!X509V3_EXT_print(bio, ex, 0, 0))
600 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
601 ASN1_STRFLGS_RFC2253))
603 log_ssl_errors(LOG_LEVEL_ERROR,
604 "ASN1_STRING_print_ex() for ext key usage failed");
611 loc = X509_get_ext_by_NID(crt, NID_certificate_policies, -1);
614 X509_EXTENSION *ex = X509_get_ext(crt, loc);
615 if (BIO_puts(bio, "\ncertificate policies : ") <= 0)
617 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_printf() for certificate policies failed");
621 if (!X509V3_EXT_print(bio, ex, 0, 0))
623 if (!ASN1_STRING_print_ex(bio, X509_EXTENSION_get_data(ex),
624 ASN1_STRFLGS_RFC2253))
626 log_ssl_errors(LOG_LEVEL_ERROR,
627 "ASN1_STRING_print_ex() for certificate policies failed");
634 /* make valgrind happy */
635 static const char zero = 0;
636 BIO_write(bio, &zero, 1);
638 len = BIO_get_mem_data(bio, &bio_mem_data);
639 encoded_text = html_encode(bio_mem_data);
640 if (encoded_text == NULL)
642 log_error(LOG_LEVEL_ERROR,
643 "Failed to HTML-encode the certificate information");
648 strlcpy(last->info_buf, encoded_text, sizeof(last->info_buf));
665 /*********************************************************************
667 * Function : host_to_hash
669 * Description : Creates MD5 hash from host name. Host name is loaded
670 * from structure csp and saved again into it.
673 * 1 : csp = Current client state (buffers, headers, etc...)
675 * Returns : 1 => Error while creating hash
676 * 0 => Hash created successfully
678 *********************************************************************/
679 static int host_to_hash(struct client_state *csp)
683 memset(csp->http->hash_of_host, 0, sizeof(csp->http->hash_of_host));
684 MD5((unsigned char *)csp->http->host, strlen(csp->http->host),
685 csp->http->hash_of_host);
687 /* Converting hash into string with hex */
691 if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
692 csp->http->hash_of_host[i])) < 0)
694 log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
703 /*********************************************************************
705 * Function : create_client_ssl_connection
707 * Description : Creates TLS/SSL secured connection with client
710 * 1 : csp = Current client state (buffers, headers, etc...)
712 * Returns : 0 on success, negative value if connection wasn't created
715 *********************************************************************/
716 extern int create_client_ssl_connection(struct client_state *csp)
718 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
719 /* Paths to certificates file and key file */
720 char *key_file = NULL;
721 char *ca_file = NULL;
722 char *cert_file = NULL;
727 * Initializing OpenSSL structures for TLS/SSL connection
732 * Preparing hash of host for creating certificates
734 ret = host_to_hash(csp);
737 log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
743 * Preparing paths to certificates files and key file
745 ca_file = csp->config->ca_cert_file;
746 cert_file = make_certs_path(csp->config->certificate_directory,
747 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
748 key_file = make_certs_path(csp->config->certificate_directory,
749 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
751 if (cert_file == NULL || key_file == NULL)
758 * Generating certificate for requested host. Mutex to prevent
759 * certificate and key inconsistence must be locked.
761 privoxy_mutex_lock(&certificate_mutex);
763 ret = generate_webpage_certificate(csp);
766 log_error(LOG_LEVEL_ERROR,
767 "Generate_webpage_certificate failed: %d", ret);
768 privoxy_mutex_unlock(&certificate_mutex);
772 privoxy_mutex_unlock(&certificate_mutex);
774 if (!(ssl_attr->openssl_attr.ctx = SSL_CTX_new(SSLv23_server_method())))
776 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create SSL context");
781 /* Set the key and cert */
782 if (SSL_CTX_use_certificate_file(ssl_attr->openssl_attr.ctx,
783 cert_file, SSL_FILETYPE_PEM) != 1)
785 log_ssl_errors(LOG_LEVEL_ERROR,
786 "Loading webpage certificate %s failed", cert_file);
791 if (SSL_CTX_use_PrivateKey_file(ssl_attr->openssl_attr.ctx,
792 key_file, SSL_FILETYPE_PEM) != 1)
794 log_ssl_errors(LOG_LEVEL_ERROR,
795 "Loading webpage certificate private key %s failed", key_file);
800 SSL_CTX_set_options(ssl_attr->openssl_attr.ctx, SSL_OP_NO_SSLv3);
802 if (!(ssl_attr->openssl_attr.bio = BIO_new_ssl(ssl_attr->openssl_attr.ctx, 0)))
804 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
809 if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1)
811 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
816 if (!SSL_set_fd(ssl, csp->cfd))
818 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
824 * Handshake with client
826 log_error(LOG_LEVEL_CONNECT,
827 "Performing the TLS/SSL handshake with client. Hash of host: %s",
828 csp->http->hash_of_host_hex);
829 if (BIO_do_handshake(ssl_attr->openssl_attr.bio) != 1)
831 log_ssl_errors(LOG_LEVEL_ERROR,
832 "The TLS/SSL handshake with the client failed");
837 log_error(LOG_LEVEL_CONNECT, "Client successfully connected over TLS/SSL");
838 csp->ssl_with_client_is_opened = 1;
843 * Freeing allocated paths to files
848 /* Freeing structures if connection wasn't created successfully */
851 free_client_ssl_structures(csp);
857 /*********************************************************************
859 * Function : close_client_ssl_connection
861 * Description : Closes TLS/SSL connection with client. This function
862 * checks if this connection is already created.
865 * 1 : csp = Current client state (buffers, headers, etc...)
869 *********************************************************************/
870 extern void close_client_ssl_connection(struct client_state *csp)
872 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
874 if (csp->ssl_with_client_is_opened == 0)
880 * Notifying the peer that the connection is being closed.
882 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
883 free_client_ssl_structures(csp);
884 csp->ssl_with_client_is_opened = 0;
888 /*********************************************************************
890 * Function : free_client_ssl_structures
892 * Description : Frees structures used for SSL communication with
896 * 1 : csp = Current client state (buffers, headers, etc...)
900 *********************************************************************/
901 static void free_client_ssl_structures(struct client_state *csp)
903 struct ssl_attr *ssl_attr = &csp->ssl_client_attr;
905 if (ssl_attr->openssl_attr.bio)
907 BIO_free_all(ssl_attr->openssl_attr.bio);
909 if (ssl_attr->openssl_attr.ctx)
911 SSL_CTX_free(ssl_attr->openssl_attr.ctx);
916 /*********************************************************************
918 * Function : close_server_ssl_connection
920 * Description : Closes TLS/SSL connection with server. This function
921 * checks if this connection is already opened.
924 * 1 : csp = Current client state (buffers, headers, etc...)
928 *********************************************************************/
929 extern void close_server_ssl_connection(struct client_state *csp)
931 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
933 if (csp->ssl_with_server_is_opened == 0)
939 * Notifying the peer that the connection is being closed.
941 BIO_ssl_shutdown(ssl_attr->openssl_attr.bio);
942 free_server_ssl_structures(csp);
943 csp->ssl_with_server_is_opened = 0;
947 /*********************************************************************
949 * Function : create_server_ssl_connection
951 * Description : Creates TLS/SSL secured connection with server.
954 * 1 : csp = Current client state (buffers, headers, etc...)
956 * Returns : 0 on success, negative value if connection wasn't created
959 *********************************************************************/
960 extern int create_server_ssl_connection(struct client_state *csp)
962 openssl_connection_attr *ssl_attrs = &csp->ssl_server_attr.openssl_attr;
964 char *trusted_cas_file = NULL;
965 STACK_OF(X509) *chain;
968 csp->server_cert_verification_result = SSL_CERT_NOT_VERIFIED;
969 csp->server_certs_chain.next = NULL;
971 /* Setting path to file with trusted CAs */
972 trusted_cas_file = csp->config->trusted_cas_file;
974 ssl_attrs->ctx = SSL_CTX_new(SSLv23_method());
977 log_ssl_errors(LOG_LEVEL_ERROR, "SSL context creation failed");
983 * Loading file with trusted CAs
985 if (!SSL_CTX_load_verify_locations(ssl_attrs->ctx, trusted_cas_file, NULL))
987 log_ssl_errors(LOG_LEVEL_ERROR, "Loading trusted CAs file %s failed",
993 SSL_CTX_set_verify(ssl_attrs->ctx, SSL_VERIFY_NONE, NULL);
994 SSL_CTX_set_options(ssl_attrs->ctx, SSL_OP_NO_SSLv3);
996 if (!(ssl_attrs->bio = BIO_new_ssl(ssl_attrs->ctx, 1)))
998 log_ssl_errors(LOG_LEVEL_ERROR, "Unable to create BIO structure");
1003 if (BIO_get_ssl(ssl_attrs->bio, &ssl) != 1)
1005 log_ssl_errors(LOG_LEVEL_ERROR, "BIO_get_ssl failed");
1010 if (!SSL_set_fd(ssl, csp->server_connection.sfd))
1012 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_fd failed");
1018 * Set the hostname to check against the received server certificate
1020 #if OPENSSL_VERSION_NUMBER > 0x10100000L
1021 if (!SSL_set1_host(ssl, csp->http->host))
1023 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set1_host failed");
1028 if (host_is_ip_address(csp->http->host))
1030 if (X509_VERIFY_PARAM_set1_ip_asc(ssl->param, csp->http->host) != 1)
1032 log_ssl_errors(LOG_LEVEL_ERROR,
1033 "X509_VERIFY_PARAM_set1_ip_asc() failed");
1040 if (X509_VERIFY_PARAM_set1_host(ssl->param, csp->http->host, 0) != 1)
1042 log_ssl_errors(LOG_LEVEL_ERROR,
1043 "X509_VERIFY_PARAM_set1_host() failed");
1050 if (!SSL_set_tlsext_host_name(ssl, csp->http->host))
1052 log_ssl_errors(LOG_LEVEL_ERROR, "SSL_set_tlsext_host_name failed");
1058 * Handshake with server
1060 log_error(LOG_LEVEL_CONNECT,
1061 "Performing the TLS/SSL handshake with the server");
1063 if (BIO_do_handshake(ssl_attrs->bio) != 1)
1065 log_ssl_errors(LOG_LEVEL_ERROR,
1066 "The TLS/SSL handshake with the server failed");
1071 chain = SSL_get_peer_cert_chain(ssl);
1074 for (int i = 0; i < sk_X509_num(chain); i++)
1076 if (ssl_store_cert(csp, sk_X509_value(chain, i)) != 0)
1078 log_error(LOG_LEVEL_ERROR, "ssl_store_cert failed");
1085 if (!csp->dont_verify_certificate)
1087 long verify_result = SSL_get_verify_result(ssl);
1088 if (verify_result == X509_V_OK)
1091 csp->server_cert_verification_result = SSL_CERT_VALID;
1095 csp->server_cert_verification_result = verify_result;
1096 log_error(LOG_LEVEL_ERROR, "SSL_get_verify_result failed: %s",
1097 X509_verify_cert_error_string(verify_result));
1103 log_error(LOG_LEVEL_CONNECT, "Server successfully connected over TLS/SSL");
1106 * Server certificate chain is valid, so we can clean
1107 * chain, because we will not send it to client.
1109 free_certificate_chain(csp);
1111 csp->ssl_with_server_is_opened = 1;
1113 /* Freeing structures if connection wasn't created successfully */
1116 free_server_ssl_structures(csp);
1123 /*********************************************************************
1125 * Function : free_server_ssl_structures
1127 * Description : Frees structures used for SSL communication with server
1130 * 1 : csp = Current client state (buffers, headers, etc...)
1134 *********************************************************************/
1135 static void free_server_ssl_structures(struct client_state *csp)
1137 struct ssl_attr *ssl_attr = &csp->ssl_server_attr;
1139 if (ssl_attr->openssl_attr.bio)
1141 BIO_free_all(ssl_attr->openssl_attr.bio);
1143 if (ssl_attr->openssl_attr.ctx)
1145 SSL_CTX_free(ssl_attr->openssl_attr.ctx);
1150 /*********************************************************************
1152 * Function : log_ssl_errors
1154 * Description : Log SSL errors
1157 * 1 : debuglevel = Debug level
1158 * 2 : desc = Error description
1162 *********************************************************************/
1163 static void log_ssl_errors(int debuglevel, const char* fmt, ...)
1165 unsigned long err_code;
1166 char prefix[ERROR_BUF_SIZE];
1168 va_start(args, fmt);
1169 vsnprintf(prefix, sizeof(prefix), fmt, args);
1172 while ((err_code = ERR_get_error()))
1174 char err_buf[ERROR_BUF_SIZE];
1176 ERR_error_string_n(err_code, err_buf, sizeof(err_buf));
1177 log_error(debuglevel, "%s: %s", prefix, err_buf);
1181 * In case if called by mistake and there were
1182 * no TLS/SSL errors let's report it to the log.
1186 log_error(debuglevel, "%s: no TLS/SSL errors detected", prefix);
1191 /*********************************************************************
1193 * Function : ssl_base64_encode
1195 * Description : Encode a buffer into base64 format.
1198 * 1 : dst = Destination buffer
1199 * 2 : dlen = Destination buffer length
1200 * 3 : olen = Number of bytes written
1201 * 4 : src = Source buffer
1202 * 5 : slen = Amount of data to be encoded
1204 * Returns : 0 on success, error code othervise
1206 *********************************************************************/
1207 extern int ssl_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
1208 const unsigned char *src, size_t slen)
1210 *olen = 4 * ((slen/3) + ((slen%3) ? 1 : 0)) + 1;
1215 *olen = (size_t)EVP_EncodeBlock(dst, src, (int)slen) + 1;
1220 /*********************************************************************
1222 * Function : close_file_stream
1224 * Description : Close file stream, report error on close error
1227 * 1 : f = file stream to close
1228 * 2 : path = path for error report
1232 *********************************************************************/
1233 static void close_file_stream(FILE *f, const char *path)
1237 log_error(LOG_LEVEL_ERROR,
1238 "Error closing file %s: %s", path, strerror(errno));
1243 /*********************************************************************
1245 * Function : write_certificate
1247 * Description : Writes certificate into file.
1250 * 1 : crt = certificate to write into file
1251 * 2 : output_file = path to save certificate file
1254 * Returns : 1 on success success or negative value
1256 *********************************************************************/
1257 static int write_certificate(X509 *crt, const char *output_file)
1263 * Saving certificate into file
1265 if ((f = fopen(output_file, "w")) == NULL)
1267 log_error(LOG_LEVEL_ERROR, "Opening file %s to save certificate failed",
1272 ret = PEM_write_X509(f, crt);
1275 log_ssl_errors(LOG_LEVEL_ERROR,
1276 "Writing certificate into file %s failed", output_file);
1280 close_file_stream(f, output_file);
1285 /*********************************************************************
1287 * Function : write_private_key
1289 * Description : Writes private key into file and copies saved
1290 * content into given pointer to string. If function
1291 * returns 0 for success, this copy must be freed by
1295 * 1 : key = key to write into file
1296 * 2 : ret_buf = pointer to string with created key file content
1297 * 3 : key_file_path = path where to save key file
1299 * Returns : Length of written private key on success or negative value
1302 *********************************************************************/
1303 static int write_private_key(EVP_PKEY *key, char **ret_buf,
1304 const char *key_file_path)
1306 size_t len = 0; /* Length of created key */
1307 FILE *f = NULL; /* File to save certificate */
1309 BIO *bio_mem = BIO_new(BIO_s_mem());
1310 char *bio_mem_data = 0;
1312 if (bio_mem == NULL)
1314 log_ssl_errors(LOG_LEVEL_ERROR, "write_private_key memory allocation failure");
1319 * Writing private key into PEM string
1321 if (!PEM_write_bio_PrivateKey(bio_mem, key, NULL, NULL, 0, NULL, NULL))
1323 log_ssl_errors(LOG_LEVEL_ERROR,
1324 "Writing private key into PEM string failed");
1329 len = (size_t)BIO_get_mem_data(bio_mem, &bio_mem_data);
1331 /* Initializing buffer for key file content */
1332 *ret_buf = zalloc_or_die(len + 1);
1333 (*ret_buf)[len] = 0;
1335 strncpy(*ret_buf, bio_mem_data, len);
1338 * Saving key into file
1340 if ((f = fopen(key_file_path, "wb")) == NULL)
1342 log_error(LOG_LEVEL_ERROR,
1343 "Opening file %s to save private key failed: %E",
1349 if (fwrite(*ret_buf, 1, len, f) != len)
1351 log_error(LOG_LEVEL_ERROR,
1352 "Writing private key into file %s failed",
1354 close_file_stream(f, key_file_path);
1359 close_file_stream(f, key_file_path);
1373 /*********************************************************************
1375 * Function : generate_key
1377 * Description : Tests if private key for host saved in csp already
1378 * exists. If this file doesn't exists, a new key is
1379 * generated and saved in a file. The generated key is also
1380 * copied into given parameter key_buf, which must be then
1381 * freed by caller. If file with key exists, key_buf
1382 * contain NULL and no private key is generated.
1385 * 1 : csp = Current client state (buffers, headers, etc...)
1386 * 2 : key_buf = buffer to save new generated key
1388 * Returns : -1 => Error while generating private key
1389 * 0 => Key already exists
1390 * >0 => Length of generated private key
1392 *********************************************************************/
1393 static int generate_key(struct client_state *csp, char **key_buf)
1396 char* key_file_path = NULL;
1397 BIGNUM *exp = BN_new();
1398 RSA *rsa = RSA_new();
1399 EVP_PKEY *key = EVP_PKEY_new();
1401 if (exp == NULL || rsa == NULL || key == NULL)
1403 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1408 if (BN_set_word(exp, RSA_KEY_PUBLIC_EXPONENT) != 1)
1410 log_ssl_errors(LOG_LEVEL_ERROR, "Setting RSA key exponent failed");
1415 key_file_path = make_certs_path(csp->config->certificate_directory,
1416 (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1417 if (key_file_path == NULL)
1424 * Test if key already exists. If so, we don't have to create it again.
1426 if (file_exists(key_file_path) == 1)
1432 ret = RSA_generate_key_ex(rsa, RSA_KEYSIZE, exp, NULL);
1435 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key generation failure");
1440 if (!EVP_PKEY_set1_RSA(key, rsa))
1442 log_ssl_errors(LOG_LEVEL_ERROR,
1443 "Error assigning RSA key pair to PKEY structure");
1449 * Exporting private key into file
1451 if ((ret = write_private_key(key, key_buf, key_file_path)) < 0)
1453 log_error(LOG_LEVEL_ERROR,
1454 "Writing private key into file %s failed", key_file_path);
1461 * Freeing used variables
1475 freez(key_file_path);
1481 /*********************************************************************
1483 * Function : ssl_certificate_load
1485 * Description : Loads certificate from file.
1488 * 1 : cert_path = The certificate path to load
1490 * Returns : NULL => error loading certificate,
1491 * pointer to certificate instance otherwise
1493 *********************************************************************/
1494 static X509* ssl_certificate_load(const char *cert_path)
1497 FILE *cert_f = NULL;
1499 if (!(cert_f = fopen(cert_path, "r")))
1501 log_error(LOG_LEVEL_ERROR,
1502 "Error opening certificate file %s: %s", cert_path, strerror(errno));
1506 if (!(cert = PEM_read_X509(cert_f, NULL, NULL, NULL)))
1508 log_ssl_errors(LOG_LEVEL_ERROR,
1509 "Error reading certificate file %s", cert_path);
1512 close_file_stream(cert_f, cert_path);
1517 /*********************************************************************
1519 * Function : ssl_certificate_is_invalid
1521 * Description : Checks whether or not a certificate is valid.
1522 * Currently only checks that the certificate can be
1523 * parsed and that the "valid to" date is in the future.
1526 * 1 : cert_file = The certificate to check
1528 * Returns : 0 => The certificate is valid.
1529 * 1 => The certificate is invalid
1531 *********************************************************************/
1532 static int ssl_certificate_is_invalid(const char *cert_file)
1538 if (!(cert = ssl_certificate_load(cert_file)))
1540 log_ssl_errors(LOG_LEVEL_ERROR,
1541 "Error reading certificate file %s", cert_file);
1545 ret = X509_cmp_current_time(X509_get_notAfter(cert));
1548 log_ssl_errors(LOG_LEVEL_ERROR,
1549 "Error checking certificate %s validity", cert_file);
1554 return ret == -1 ? 1 : 0;
1558 /*********************************************************************
1560 * Function : set_x509_ext
1562 * Description : Sets the X509V3 extension data
1565 * 1 : cert = The certificate to modify
1566 * 2 : issuer = Issuer certificate
1567 * 3 : nid = OpenSSL NID
1568 * 4 : value = extension value
1570 * Returns : 0 => Error while setting extensuon data
1573 *********************************************************************/
1574 static int set_x509_ext(X509 *cert, X509 *issuer, int nid, char *value)
1576 X509_EXTENSION *ext = NULL;
1580 X509V3_set_ctx(&ctx, issuer, cert, NULL, NULL, 0);
1581 ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
1584 log_ssl_errors(LOG_LEVEL_ERROR, "X509V3_EXT_conf_nid failure");
1588 if (!X509_add_ext(cert, ext, -1))
1590 log_ssl_errors(LOG_LEVEL_ERROR, "X509_add_ext failure");
1598 X509_EXTENSION_free(ext);
1604 /*********************************************************************
1606 * Function : set_subject_alternative_name
1608 * Description : Sets the Subject Alternative Name extension to a cert
1611 * 1 : cert = The certificate to modify
1612 * 2 : issuer = Issuer certificate
1613 * 3 : hostname = The hostname to add
1615 * Returns : 0 => Error while creating certificate.
1618 *********************************************************************/
1619 static int set_subject_alternative_name(X509 *cert, X509 *issuer, const char *hostname)
1621 size_t altname_len = strlen(hostname) + sizeof(CERTIFICATE_ALT_NAME_PREFIX);
1622 char alt_name_buf[altname_len];
1624 snprintf(alt_name_buf, sizeof(alt_name_buf),
1625 CERTIFICATE_ALT_NAME_PREFIX"%s", hostname);
1626 return set_x509_ext(cert, issuer, NID_subject_alt_name, alt_name_buf);
1630 /*********************************************************************
1632 * Function : generate_webpage_certificate
1634 * Description : Creates certificate file in presetted directory.
1635 * If certificate already exists, no other certificate
1636 * will be created. Subject of certificate is named
1637 * by csp->http->host from parameter. This function also
1638 * triggers generating of private key for new certificate.
1641 * 1 : csp = Current client state (buffers, headers, etc...)
1643 * Returns : -1 => Error while creating certificate.
1644 * 0 => Certificate already exists.
1645 * 1 => Certificate created
1647 *********************************************************************/
1648 static int generate_webpage_certificate(struct client_state *csp)
1650 char *key_buf = NULL; /* Buffer for created key */
1651 X509 *issuer_cert = NULL;
1654 EVP_PKEY *loaded_subject_key = NULL;
1655 EVP_PKEY *loaded_issuer_key = NULL;
1656 X509_NAME *issuer_name;
1657 X509_NAME *subject_name = NULL;
1658 ASN1_TIME *asn_time = NULL;
1659 ASN1_INTEGER *serial = NULL;
1660 BIGNUM *serial_num = NULL;
1663 cert_options cert_opt;
1664 char cert_valid_from[VALID_DATETIME_BUFLEN];
1665 char cert_valid_to[VALID_DATETIME_BUFLEN];
1667 /* Paths to keys and certificates needed to create certificate */
1668 cert_opt.issuer_key = NULL;
1669 cert_opt.subject_key = NULL;
1670 cert_opt.issuer_crt = NULL;
1672 cert_opt.output_file = make_certs_path(csp->config->certificate_directory,
1673 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
1674 if (cert_opt.output_file == NULL)
1679 cert_opt.subject_key = make_certs_path(csp->config->certificate_directory,
1680 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1681 if (cert_opt.subject_key == NULL)
1683 freez(cert_opt.output_file);
1687 if (file_exists(cert_opt.output_file) == 1)
1689 /* The file exists, but is it valid? */
1690 if (ssl_certificate_is_invalid(cert_opt.output_file))
1692 log_error(LOG_LEVEL_CONNECT,
1693 "Certificate %s is no longer valid. Removing it.",
1694 cert_opt.output_file);
1695 if (unlink(cert_opt.output_file))
1697 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1698 cert_opt.output_file);
1700 freez(cert_opt.output_file);
1701 freez(cert_opt.subject_key);
1705 if (unlink(cert_opt.subject_key))
1707 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1708 cert_opt.subject_key);
1710 freez(cert_opt.output_file);
1711 freez(cert_opt.subject_key);
1718 freez(cert_opt.output_file);
1719 freez(cert_opt.subject_key);
1726 * Create key for requested host
1728 int subject_key_len = generate_key(csp, &key_buf);
1729 if (subject_key_len < 0)
1731 freez(cert_opt.output_file);
1732 freez(cert_opt.subject_key);
1733 log_error(LOG_LEVEL_ERROR, "Key generating failed");
1738 * Converting unsigned long serial number to char * serial number.
1739 * We must compute length of serial number in string + terminating null.
1741 unsigned long certificate_serial = get_certificate_serial(csp);
1742 unsigned long certificate_serial_time = (unsigned long)time(NULL);
1743 int serial_num_size = snprintf(NULL, 0, "%lu%lu",
1744 certificate_serial_time, certificate_serial) + 1;
1745 if (serial_num_size <= 0)
1747 serial_num_size = 1;
1750 char serial_num_text[serial_num_size]; /* Buffer for serial number */
1751 ret = snprintf(serial_num_text, (size_t)serial_num_size, "%lu%lu",
1752 certificate_serial_time, certificate_serial);
1753 if (ret < 0 || ret >= serial_num_size)
1755 log_error(LOG_LEVEL_ERROR,
1756 "Converting certificate serial number into string failed");
1762 * Preparing parameters for certificate
1764 subject_name = X509_NAME_new();
1767 log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure");
1772 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COMMON_NAME_FCODE,
1773 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1775 log_ssl_errors(LOG_LEVEL_ERROR,
1776 "X509 subject name (code: %s, val: %s) error",
1777 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1781 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORGANIZATION_FCODE,
1782 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1784 log_ssl_errors(LOG_LEVEL_ERROR,
1785 "X509 subject name (code: %s, val: %s) error",
1786 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1790 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_ORG_UNIT_FCODE,
1791 MBSTRING_ASC, (void *)csp->http->host, -1, -1, 0))
1793 log_ssl_errors(LOG_LEVEL_ERROR,
1794 "X509 subject name (code: %s, val: %s) error",
1795 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1799 if (!X509_NAME_add_entry_by_txt(subject_name, CERT_PARAM_COUNTRY_FCODE,
1800 MBSTRING_ASC, (void *)CERT_PARAM_COUNTRY_CODE, -1, -1, 0))
1802 log_ssl_errors(LOG_LEVEL_ERROR,
1803 "X509 subject name (code: %s, val: %s) error",
1804 CERT_PARAM_COMMON_NAME_FCODE, csp->http->host);
1809 cert_opt.issuer_crt = csp->config->ca_cert_file;
1810 cert_opt.issuer_key = csp->config->ca_key_file;
1812 if (get_certificate_valid_from_date(cert_valid_from,
1813 sizeof(cert_valid_from), VALID_DATETIME_FMT)
1814 || get_certificate_valid_to_date(cert_valid_to,
1815 sizeof(cert_valid_to), VALID_DATETIME_FMT))
1817 log_error(LOG_LEVEL_ERROR, "Generating one of the validity dates failed");
1822 cert_opt.subject_pwd = CERT_SUBJECT_PASSWORD;
1823 cert_opt.issuer_pwd = csp->config->ca_password;
1824 cert_opt.not_before = cert_valid_from;
1825 cert_opt.not_after = cert_valid_to;
1826 cert_opt.serial = serial_num_text;
1827 cert_opt.max_pathlen = -1;
1830 * Test if the private key was already created.
1831 * XXX: Can this still happen?
1833 if (subject_key_len == 0)
1835 log_error(LOG_LEVEL_ERROR, "Subject key was already created");
1841 * Parse serial to MPI
1843 serial_num = BN_new();
1846 log_error(LOG_LEVEL_ERROR, "generate_webpage_certificate: memory error");
1850 if (!BN_dec2bn(&serial_num, cert_opt.serial))
1852 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to parse serial %s", cert_opt.serial);
1857 if (!(serial = BN_to_ASN1_INTEGER(serial_num, NULL)))
1859 log_ssl_errors(LOG_LEVEL_ERROR, "Failed to generate serial ASN1 representation");
1865 * Loading certificates
1867 if (!(issuer_cert = ssl_certificate_load(cert_opt.issuer_crt)))
1869 log_error(LOG_LEVEL_ERROR, "Loading issuer certificate %s failed",
1870 cert_opt.issuer_crt);
1875 issuer_name = X509_get_issuer_name(issuer_cert);
1878 * Loading keys from file or from buffer
1880 if (key_buf != NULL && subject_key_len > 0)
1882 pk_bio = BIO_new_mem_buf(key_buf, subject_key_len);
1884 else if (!(pk_bio = BIO_new_file(cert_opt.subject_key, "r")))
1886 log_ssl_errors(LOG_LEVEL_ERROR,
1887 "Failure opening subject key %s BIO", cert_opt.subject_key);
1892 loaded_subject_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL,
1893 (void *)cert_opt.subject_pwd);
1894 if (!loaded_subject_key)
1896 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing subject key %s failed",
1897 cert_opt.subject_key);
1902 if (!BIO_free(pk_bio))
1904 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing subject key BIO");
1907 if (!(pk_bio = BIO_new_file(cert_opt.issuer_key, "r")))
1909 log_ssl_errors(LOG_LEVEL_ERROR, "Failure opening issuer key %s BIO",
1910 cert_opt.issuer_key);
1915 loaded_issuer_key = PEM_read_bio_PrivateKey(pk_bio, NULL, NULL,
1916 (void *)cert_opt.issuer_pwd);
1917 if (!loaded_issuer_key)
1919 log_ssl_errors(LOG_LEVEL_ERROR, "Parsing issuer key %s failed",
1920 cert_opt.subject_key);
1928 log_ssl_errors(LOG_LEVEL_ERROR, "Certificate allocation error");
1933 if (!X509_set_version(cert, CERTIFICATE_VERSION))
1935 log_ssl_errors(LOG_LEVEL_ERROR, "X509_set_version failed");
1941 * Setting parameters of signed certificate
1943 if (!X509_set_pubkey(cert, loaded_subject_key))
1945 log_ssl_errors(LOG_LEVEL_ERROR,
1946 "Setting issuer name in signed certificate failed");
1951 if (!X509_set_subject_name(cert, subject_name))
1953 log_ssl_errors(LOG_LEVEL_ERROR,
1954 "Setting issuer name in signed certificate failed");
1959 if (!X509_set_issuer_name(cert, issuer_name))
1961 log_ssl_errors(LOG_LEVEL_ERROR,
1962 "Setting issuer name in signed certificate failed");
1967 if (!X509_set_serialNumber(cert, serial))
1969 log_ssl_errors(LOG_LEVEL_ERROR,
1970 "Setting serial number in signed certificate failed");
1975 asn_time = ASN1_TIME_new();
1978 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time memory allocation failure");
1983 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_after))
1985 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time [%s] encode error", cert_opt.not_after);
1990 if (!X509_set1_notAfter(cert, asn_time))
1992 log_ssl_errors(LOG_LEVEL_ERROR,
1993 "Setting valid not after in signed certificate failed");
1998 if (!ASN1_TIME_set_string(asn_time, cert_opt.not_before))
2000 log_ssl_errors(LOG_LEVEL_ERROR, "ASN1 time encode error");
2005 if (!X509_set1_notBefore(cert, asn_time))
2007 log_ssl_errors(LOG_LEVEL_ERROR,
2008 "Setting valid not befre in signed certificate failed");
2013 if (!set_x509_ext(cert, issuer_cert, NID_basic_constraints, CERTIFICATE_BASIC_CONSTRAINTS))
2015 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the basicConstraints extension "
2016 "in signed certificate failed");
2021 if (!set_x509_ext(cert, issuer_cert, NID_subject_key_identifier, CERTIFICATE_SUBJECT_KEY))
2023 log_ssl_errors(LOG_LEVEL_ERROR,
2024 "Setting the Subject Key Identifie extension failed");
2029 if (!set_x509_ext(cert, issuer_cert, NID_authority_key_identifier, CERTIFICATE_AUTHORITY_KEY))
2031 log_ssl_errors(LOG_LEVEL_ERROR,
2032 "Setting the Authority Key Identifier extension failed");
2037 if (!host_is_ip_address(csp->http->host) &&
2038 !set_subject_alternative_name(cert, issuer_cert, csp->http->host))
2040 log_ssl_errors(LOG_LEVEL_ERROR, "Setting the Subject Alt Nameextension failed");
2045 if (!X509_sign(cert, loaded_issuer_key, EVP_sha256()))
2047 log_ssl_errors(LOG_LEVEL_ERROR, "Signing certificate failed");
2053 * Writing certificate into file
2055 if (write_certificate(cert, cert_opt.output_file) < 0)
2057 log_error(LOG_LEVEL_ERROR, "Writing certificate into file failed");
2066 * Freeing used structures
2070 X509_free(issuer_cert);
2076 if (pk_bio && !BIO_free(pk_bio))
2078 log_ssl_errors(LOG_LEVEL_ERROR, "Error closing pk BIO");
2080 if (loaded_subject_key)
2082 EVP_PKEY_free(loaded_subject_key);
2084 if (loaded_issuer_key)
2086 EVP_PKEY_free(loaded_issuer_key);
2090 X509_NAME_free(subject_name);
2094 ASN1_TIME_free(asn_time);
2098 BN_free(serial_num);
2102 ASN1_INTEGER_free(serial);
2104 freez(cert_opt.subject_key);
2105 freez(cert_opt.output_file);
2112 /*********************************************************************
2114 * Function : ssl_crt_verify_info
2116 * Description : Returns an informational string about the verification
2117 * status of a certificate.
2120 * 1 : buf = Buffer to write to
2121 * 2 : size = Maximum size of buffer
2122 * 3 : csp = client state
2126 *********************************************************************/
2127 extern void ssl_crt_verify_info(char *buf, size_t size, struct client_state *csp)
2129 strncpy(buf, X509_verify_cert_error_string(csp->server_cert_verification_result), size);
2134 /*********************************************************************
2136 * Function : ssl_release
2138 * Description : Release all SSL resources
2144 *********************************************************************/
2145 extern void ssl_release(void)
2147 if (ssl_inited == 1)
2149 SSL_COMP_free_compression_methods();
2151 CONF_modules_free();
2152 CONF_modules_unload(1);
2154 COMP_zlib_cleanup();
2159 CRYPTO_cleanup_all_ex_data();