1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/ssl.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) 2017 Vaclav Svec. FIT CVUT.
9 * Copyright (C) 2018-2020 by Fabian Keil <fk@fabiankeil.de>
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will
18 * be useful, but WITHOUT ANY WARRANTY; without even the
19 * implied warranty of MERCHANTABILITY or FITNESS FOR A
20 * PARTICULAR PURPOSE. See the GNU General Public
21 * License for more details.
23 * The GNU General Public License should be included with
24 * this file. If not, you can view it at
25 * http://www.gnu.org/copyleft/gpl.html
26 * or write to the Free Software Foundation, Inc., 59
27 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 *********************************************************************/
35 #if !defined(MBEDTLS_CONFIG_FILE)
36 # include "mbedtls/config.h"
38 # include MBEDTLS_CONFIG_FILE
41 #include "mbedtls/md5.h"
42 #include "mbedtls/pem.h"
43 #include "mbedtls/base64.h"
44 #include "mbedtls/error.h"
45 #include "mbedtls/oid.h"
46 #include "mbedtls/asn1write.h"
58 * Macros for searching begin and end of certificates.
59 * Necessary to convert structure mbedtls_x509_crt to crt file.
61 #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
62 #define PEM_END_CRT "-----END CERTIFICATE-----\n"
67 #define ERROR_BUF_SIZE 1024 /* Size of buffer for error messages */
68 #define CERTIFICATE_BUF_SIZE 16384 /* Size of buffer to save certificate. Value 4096 is mbedtls library buffer size for certificate in DER form */
69 #define PRIVATE_KEY_BUF_SIZE 16000 /* Size of buffer to save private key. Value 16000 is taken from mbed TLS library examples. */
70 #define RSA_KEY_PUBLIC_EXPONENT 65537 /* Public exponent for RSA private key generating */
71 #define RSA_KEYSIZE 2048 /* Size of generated RSA keys */
72 #define CERT_SIGNATURE_ALGORITHM MBEDTLS_MD_SHA256 /* The MD algorithm to use for the signature */
73 #define CERT_SERIAL_NUM_LENGTH 4 /* Bytes of hash to be used for creating serial number of certificate. Min=2 and max=16 */
74 #define INVALID_CERT_INFO_BUF_SIZE 2048 /* Size of buffer for message with information about reason of certificate invalidity. Data after the end of buffer will not be saved */
75 #define CERT_PARAM_COMMON_NAME "CN="
76 #define CERT_PARAM_ORGANIZATION ",O="
77 #define CERT_PARAM_ORG_UNIT ",OU="
78 #define CERT_PARAM_COUNTRY ",C=CZ"
79 #define KEY_FILE_TYPE ".pem"
80 #define CERT_FILE_TYPE ".crt"
81 #define CERT_SUBJECT_PASSWORD ""
82 #define CERT_INFO_PREFIX ""
85 * Properties of cert for generating
88 char *issuer_crt; /* filename of the issuer certificate */
89 char *subject_key; /* filename of the subject key file */
90 char *issuer_key; /* filename of the issuer key file */
91 const char *subject_pwd; /* password for the subject key file */
92 const char *issuer_pwd; /* password for the issuer key file */
93 char *output_file; /* where to store the constructed key file */
94 const char *subject_name; /* subject name for certificate */
95 char issuer_name[ISSUER_NAME_BUF_SIZE]; /* issuer name for certificate */
96 const char *not_before; /* validity period not before */
97 const char *not_after; /* validity period not after */
98 const char *serial; /* serial number string */
99 int is_ca; /* is a CA certificate */
100 int max_pathlen; /* maximum CA path length */
104 * Properties of key for generating
107 mbedtls_pk_type_t type; /* type of key to generate */
108 int rsa_keysize; /* length of key in bits */
109 char *key_file_path; /* filename of the key file */
112 static int generate_webpage_certificate(struct client_state *csp);
113 static char *make_certs_path(const char *conf_dir, const char *file_name, const char *suffix);
114 static int file_exists(const char *path);
115 static int host_to_hash(struct client_state *csp);
116 static int ssl_verify_callback(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags);
117 static void free_certificate_chain(struct client_state *csp);
118 static unsigned long get_certificate_serial(struct client_state *csp);
119 static void free_client_ssl_structures(struct client_state *csp);
120 static void free_server_ssl_structures(struct client_state *csp);
121 static int seed_rng(struct client_state *csp);
123 /*********************************************************************
125 * Function : client_use_ssl
127 * Description : Tests if client in current client state structure
128 * should use SSL connection or standard connection.
131 * 1 : csp = Current client state (buffers, headers, etc...)
133 * Returns : If client should use TLS/SSL connection, 1 is returned.
134 * Otherwise 0 is returned.
136 *********************************************************************/
137 extern int client_use_ssl(const struct client_state *csp)
139 return csp->http->client_ssl;
143 /*********************************************************************
145 * Function : server_use_ssl
147 * Description : Tests if server in current client state structure
148 * should use SSL connection or standard connection.
151 * 1 : csp = Current client state (buffers, headers, etc...)
153 * Returns : If server should use TLS/SSL connection, 1 is returned.
154 * Otherwise 0 is returned.
156 *********************************************************************/
157 extern int server_use_ssl(const struct client_state *csp)
159 return csp->http->server_ssl;
163 /*********************************************************************
165 * Function : is_ssl_pending
167 * Description : Tests if there are some waiting data on ssl connection.
168 * Only considers data that has actually been received
169 * locally and ignores data that is still on the fly
170 * or has not yet been sent by the remote end.
173 * 1 : ssl = SSL context to test
175 * Returns : 0 => No data are pending
176 * >0 => Pending data length
178 *********************************************************************/
179 extern size_t is_ssl_pending(mbedtls_ssl_context *ssl)
186 return mbedtls_ssl_get_bytes_avail(ssl);
190 /*********************************************************************
192 * Function : ssl_send_data
194 * Description : Sends the content of buf (for n bytes) to given SSL
195 * connection context.
198 * 1 : ssl = SSL context to send data to
199 * 2 : buf = Pointer to data to be sent
200 * 3 : len = Length of data to be sent to the SSL context
202 * Returns : Length of sent data or negative value on error.
204 *********************************************************************/
205 extern int ssl_send_data(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
208 size_t max_fragment_size = 0; /* Maximal length of data in one SSL fragment*/
209 int send_len = 0; /* length of one data part to send */
210 int pos = 0; /* Position of unsent part in buffer */
217 /* Getting maximal length of data sent in one fragment */
218 max_fragment_size = mbedtls_ssl_get_max_frag_len(ssl);
221 * Whole buffer must be sent in many fragments, because each fragment
222 * has its maximal length.
226 /* Compute length of data, that can be send in next fragment */
227 if ((pos + (int)max_fragment_size) > len)
229 send_len = (int)len - pos;
233 send_len = (int)max_fragment_size;
236 log_error(LOG_LEVEL_WRITING, "TLS: %N", send_len, buf+pos);
239 * Sending one part of the buffer
241 while ((ret = mbedtls_ssl_write(ssl,
242 (const unsigned char *)(buf + pos),
243 (size_t)send_len)) < 0)
245 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
246 ret != MBEDTLS_ERR_SSL_WANT_WRITE)
248 char err_buf[ERROR_BUF_SIZE];
250 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
251 log_error(LOG_LEVEL_ERROR,
252 "Sending data over TLS/SSL failed: %s", err_buf);
256 /* Adding count of sent bytes to position in buffer */
257 pos = pos + send_len;
264 /*********************************************************************
266 * Function : ssl_send_data_delayed
268 * Description : Sends the contents of buf (for n bytes) to given SSL
269 * connection, optionally delaying the operation.
272 * 1 : ssl = SSL context to send data to
273 * 2 : buf = Pointer to data to be sent
274 * 3 : len = Length of data to be sent to the SSL context
275 * 4 : delay = Delay in milliseconds.
277 * Returns : 0 on success (entire buffer sent).
280 *********************************************************************/
281 extern int ssl_send_data_delayed(mbedtls_ssl_context *ssl,
282 const unsigned char *buf, size_t len,
289 if (ssl_send_data(ssl, buf, len) < 0)
302 enum { MAX_WRITE_LENGTH = 10 };
304 if ((i + MAX_WRITE_LENGTH) > len)
306 write_length = len - i;
310 write_length = MAX_WRITE_LENGTH;
313 privoxy_millisleep(delay);
315 if (ssl_send_data(ssl, buf + i, write_length) < 0)
327 /*********************************************************************
329 * Function : ssl_recv_data
331 * Description : Receives data from given SSL context and puts
335 * 1 : ssl = SSL context to receive data from
336 * 2 : buf = Pointer to buffer where data will be written
337 * 3 : max_length = Maximum number of bytes to read
339 * Returns : Number of bytes read, 0 for EOF, or -1
342 *********************************************************************/
343 extern int ssl_recv_data(mbedtls_ssl_context *ssl, unsigned char *buf, size_t max_length)
346 memset(buf, 0, max_length);
349 * Receiving data from SSL context into buffer
353 ret = mbedtls_ssl_read(ssl, buf, max_length);
354 } while (ret == MBEDTLS_ERR_SSL_WANT_READ
355 || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
359 char err_buf[ERROR_BUF_SIZE];
361 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
363 log_error(LOG_LEVEL_CONNECT,
364 "The peer notified us that the connection is going to be closed");
367 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
368 log_error(LOG_LEVEL_ERROR,
369 "Receiving data over TLS/SSL failed: %s", err_buf);
374 log_error(LOG_LEVEL_RECEIVED, "TLS: %N", ret, buf);
380 /*********************************************************************
382 * Function : ssl_flush_socket
384 * Description : Send any pending "buffered" content with given
385 * SSL connection. Alternative to function flush_socket.
388 * 1 : ssl = SSL context to send buffer to
389 * 2 : iob = The I/O buffer to flush, usually csp->iob.
391 * Returns : On success, the number of bytes send are returned (zero
392 * indicates nothing was sent). On error, -1 is returned.
394 *********************************************************************/
395 extern long ssl_flush_socket(mbedtls_ssl_context *ssl, struct iob *iob)
397 /* Computing length of buffer part to send */
398 long len = iob->eod - iob->cur;
405 /* Sending data to given SSl context */
406 if (ssl_send_data(ssl, (const unsigned char *)iob->cur, (size_t)len) < 0)
410 iob->eod = iob->cur = iob->buf;
415 /*********************************************************************
417 * Function : ssl_debug_callback
419 * Description : Debug callback function for mbedtls library.
420 * Prints info into log file.
423 * 1 : ctx = File to save log in
424 * 2 : level = Debug level
425 * 3 : file = File calling debug message
426 * 4 : line = Line calling debug message
427 * 5 : str = Debug message
431 *********************************************************************/
432 static void ssl_debug_callback(void *ctx, int level, const char *file, int line, const char *str)
436 fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str);
438 log_error(LOG_LEVEL_INFO, "SSL debug message: %s:%04d: %s", file, line, str);
443 /*********************************************************************
445 * Function : create_client_ssl_connection
447 * Description : Creates TLS/SSL secured connection with client
450 * 1 : csp = Current client state (buffers, headers, etc...)
452 * Returns : 0 on success, negative value if connection wasn't created
455 *********************************************************************/
456 extern int create_client_ssl_connection(struct client_state *csp)
458 /* Paths to certificates file and key file */
459 char *key_file = NULL;
460 char *ca_file = NULL;
461 char *cert_file = NULL;
463 char err_buf[ERROR_BUF_SIZE];
466 * Initializing mbedtls structures for TLS/SSL connection
468 mbedtls_net_init(&(csp->mbedtls_client_attr.socket_fd));
469 mbedtls_ssl_init(&(csp->mbedtls_client_attr.ssl));
470 mbedtls_ssl_config_init(&(csp->mbedtls_client_attr.conf));
471 mbedtls_x509_crt_init(&(csp->mbedtls_client_attr.server_cert));
472 mbedtls_pk_init(&(csp->mbedtls_client_attr.prim_key));
473 #if defined(MBEDTLS_SSL_CACHE_C)
474 mbedtls_ssl_cache_init(&(csp->mbedtls_client_attr.cache));
478 * Preparing hash of host for creating certificates
480 ret = host_to_hash(csp);
483 log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
489 * Preparing paths to certificates files and key file
491 ca_file = csp->config->ca_cert_file;
492 cert_file = make_certs_path(csp->config->certificate_directory,
493 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
494 key_file = make_certs_path(csp->config->certificate_directory,
495 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
497 if (cert_file == NULL || key_file == NULL)
504 * Generating certificate for requested host. Mutex to prevent
505 * certificate and key inconsistence must be locked.
507 privoxy_mutex_lock(&certificate_mutex);
509 ret = generate_webpage_certificate(csp);
512 log_error(LOG_LEVEL_ERROR,
513 "Generate_webpage_certificate failed: %d", ret);
514 privoxy_mutex_unlock(&certificate_mutex);
518 privoxy_mutex_unlock(&certificate_mutex);
531 * Loading CA file, webpage certificate and key files
533 ret = mbedtls_x509_crt_parse_file(&(csp->mbedtls_client_attr.server_cert),
537 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
538 log_error(LOG_LEVEL_ERROR,
539 "Loading webpage certificate %s failed: %s", cert_file, err_buf);
544 ret = mbedtls_x509_crt_parse_file(&(csp->mbedtls_client_attr.server_cert),
548 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
549 log_error(LOG_LEVEL_ERROR,
550 "Loading CA certificate %s failed: %s", ca_file, err_buf);
555 ret = mbedtls_pk_parse_keyfile(&(csp->mbedtls_client_attr.prim_key),
559 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
560 log_error(LOG_LEVEL_ERROR,
561 "Loading and parsing webpage certificate private key %s failed: %s",
568 * Setting SSL parameters
570 ret = mbedtls_ssl_config_defaults(&(csp->mbedtls_client_attr.conf),
571 MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM,
572 MBEDTLS_SSL_PRESET_DEFAULT);
575 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
576 log_error(LOG_LEVEL_ERROR,
577 "mbedtls_ssl_config_defaults failed: %s", err_buf);
582 mbedtls_ssl_conf_rng(&(csp->mbedtls_client_attr.conf),
583 mbedtls_ctr_drbg_random, &ctr_drbg);
584 mbedtls_ssl_conf_dbg(&(csp->mbedtls_client_attr.conf),
585 ssl_debug_callback, stdout);
587 #if defined(MBEDTLS_SSL_CACHE_C)
588 mbedtls_ssl_conf_session_cache(&(csp->mbedtls_client_attr.conf),
589 &(csp->mbedtls_client_attr.cache), mbedtls_ssl_cache_get,
590 mbedtls_ssl_cache_set);
594 * Setting certificates
596 ret = mbedtls_ssl_conf_own_cert(&(csp->mbedtls_client_attr.conf),
597 &(csp->mbedtls_client_attr.server_cert),
598 &(csp->mbedtls_client_attr.prim_key));
601 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
602 log_error(LOG_LEVEL_ERROR,
603 "mbedtls_ssl_conf_own_cert failed: %s", err_buf);
608 ret = mbedtls_ssl_setup(&(csp->mbedtls_client_attr.ssl),
609 &(csp->mbedtls_client_attr.conf));
612 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
613 log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_setup failed: %s", err_buf);
618 mbedtls_ssl_set_bio(&(csp->mbedtls_client_attr.ssl),
619 &(csp->mbedtls_client_attr.socket_fd), mbedtls_net_send,
620 mbedtls_net_recv, NULL);
621 mbedtls_ssl_session_reset(&(csp->mbedtls_client_attr.ssl));
624 * Setting socket fd in mbedtls_net_context structure. This structure
625 * can't be set by mbedtls functions, because we already have created
626 * a TCP connection when this function is called.
628 csp->mbedtls_client_attr.socket_fd.fd = csp->cfd;
631 * Handshake with client
633 log_error(LOG_LEVEL_CONNECT,
634 "Performing the TLS/SSL handshake with client. Hash of host: %s",
635 csp->http->hash_of_host_hex);
636 while ((ret = mbedtls_ssl_handshake(&(csp->mbedtls_client_attr.ssl))) != 0)
638 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
639 ret != MBEDTLS_ERR_SSL_WANT_WRITE)
641 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
642 log_error(LOG_LEVEL_ERROR,
643 "medtls_ssl_handshake with client failed: %s", err_buf);
649 log_error(LOG_LEVEL_CONNECT, "Client successfully connected over TLS/SSL");
650 csp->ssl_with_client_is_opened = 1;
654 * Freeing allocated paths to files
659 /* Freeing structures if connection wasn't created successfully */
662 free_client_ssl_structures(csp);
668 /*********************************************************************
670 * Function : close_client_ssl_connection
672 * Description : Closes TLS/SSL connection with client. This function
673 * checks if this connection is already created.
676 * 1 : csp = Current client state (buffers, headers, etc...)
680 *********************************************************************/
681 extern void close_client_ssl_connection(struct client_state *csp)
685 if (csp->ssl_with_client_is_opened == 0)
691 * Notifying the peer that the connection is being closed.
694 ret = mbedtls_ssl_close_notify(&(csp->mbedtls_client_attr.ssl));
695 } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
697 free_client_ssl_structures(csp);
698 csp->ssl_with_client_is_opened = 0;
702 /*********************************************************************
704 * Function : free_client_ssl_structures
706 * Description : Frees structures used for SSL communication with
710 * 1 : csp = Current client state (buffers, headers, etc...)
714 *********************************************************************/
715 static void free_client_ssl_structures(struct client_state *csp)
718 * We can't use function mbedtls_net_free, because this function
719 * inter alia close TCP connection on set fd. Instead of this
720 * function, we change fd to -1, which is the same what does
721 * rest of mbedtls_net_free function.
723 csp->mbedtls_client_attr.socket_fd.fd = -1;
725 /* Freeing mbedtls structures */
726 mbedtls_x509_crt_free(&(csp->mbedtls_client_attr.server_cert));
727 mbedtls_pk_free(&(csp->mbedtls_client_attr.prim_key));
728 mbedtls_ssl_free(&(csp->mbedtls_client_attr.ssl));
729 mbedtls_ssl_config_free(&(csp->mbedtls_client_attr.conf));
730 #if defined(MBEDTLS_SSL_CACHE_C)
731 mbedtls_ssl_cache_free(&(csp->mbedtls_client_attr.cache));
736 /*********************************************************************
738 * Function : create_server_ssl_connection
740 * Description : Creates TLS/SSL secured connection with server.
743 * 1 : csp = Current client state (buffers, headers, etc...)
745 * Returns : 0 on success, negative value if connection wasn't created
748 *********************************************************************/
749 extern int create_server_ssl_connection(struct client_state *csp)
752 char err_buf[ERROR_BUF_SIZE];
753 char *trusted_cas_file = NULL;
754 int auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
756 csp->server_cert_verification_result = SSL_CERT_NOT_VERIFIED;
757 csp->server_certs_chain.next = NULL;
759 /* Setting path to file with trusted CAs */
760 trusted_cas_file = csp->config->trusted_cas_file;
763 * Initializing mbedtls structures for TLS/SSL connection
765 mbedtls_net_init(&(csp->mbedtls_server_attr.socket_fd));
766 mbedtls_ssl_init(&(csp->mbedtls_server_attr.ssl));
767 mbedtls_ssl_config_init(&(csp->mbedtls_server_attr.conf));
768 mbedtls_x509_crt_init(&(csp->mbedtls_server_attr.ca_cert));
771 * Setting socket fd in mbedtls_net_context structure. This structure
772 * can't be set by mbedtls functions, because we already have created
773 * TCP connection when calling this function.
775 csp->mbedtls_server_attr.socket_fd.fd = csp->server_connection.sfd;
788 * Loading file with trusted CAs
790 ret = mbedtls_x509_crt_parse_file(&(csp->mbedtls_server_attr.ca_cert),
794 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
795 log_error(LOG_LEVEL_ERROR, "Loading trusted CAs file %s failed: %s",
796 trusted_cas_file, err_buf);
802 * Set TLS/SSL options
804 ret = mbedtls_ssl_config_defaults(&(csp->mbedtls_server_attr.conf),
805 MBEDTLS_SSL_IS_CLIENT,
806 MBEDTLS_SSL_TRANSPORT_STREAM,
807 MBEDTLS_SSL_PRESET_DEFAULT);
810 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
811 log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_config_defaults failed: %s",
818 * Setting how strict should certificate verification be and other
819 * parameters for certificate verification
821 if (csp->dont_verify_certificate)
823 auth_mode = MBEDTLS_SSL_VERIFY_NONE;
826 mbedtls_ssl_conf_authmode(&(csp->mbedtls_server_attr.conf), auth_mode);
827 mbedtls_ssl_conf_ca_chain(&(csp->mbedtls_server_attr.conf),
828 &(csp->mbedtls_server_attr.ca_cert), NULL);
830 /* Setting callback function for certificates verification */
831 mbedtls_ssl_conf_verify(&(csp->mbedtls_server_attr.conf),
832 ssl_verify_callback, (void *)csp);
834 mbedtls_ssl_conf_rng(&(csp->mbedtls_server_attr.conf),
835 mbedtls_ctr_drbg_random, &ctr_drbg);
836 mbedtls_ssl_conf_dbg(&(csp->mbedtls_server_attr.conf),
837 ssl_debug_callback, stdout);
839 ret = mbedtls_ssl_setup(&(csp->mbedtls_server_attr.ssl),
840 &(csp->mbedtls_server_attr.conf));
843 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
844 log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_setup failed: %s", err_buf);
850 * Set the hostname to check against the received server certificate
852 ret = mbedtls_ssl_set_hostname(&(csp->mbedtls_server_attr.ssl),
856 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
857 log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_set_hostname failed: %s",
863 mbedtls_ssl_set_bio(&(csp->mbedtls_server_attr.ssl),
864 &(csp->mbedtls_server_attr.socket_fd), mbedtls_net_send,
865 mbedtls_net_recv, NULL);
868 * Handshake with server
870 log_error(LOG_LEVEL_CONNECT,
871 "Performing the TLS/SSL handshake with the server");
873 while ((ret = mbedtls_ssl_handshake(&(csp->mbedtls_server_attr.ssl))) != 0)
875 if (ret != MBEDTLS_ERR_SSL_WANT_READ
876 && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
878 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
880 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
882 char reason[INVALID_CERT_INFO_BUF_SIZE];
884 csp->server_cert_verification_result =
885 mbedtls_ssl_get_verify_result(&(csp->mbedtls_server_attr.ssl));
886 mbedtls_x509_crt_verify_info(reason, sizeof(reason), "",
887 csp->server_cert_verification_result);
889 /* Log the reason without the trailing new line */
890 log_error(LOG_LEVEL_ERROR,
891 "X509 certificate verification for %s failed: %N",
892 csp->http->hostport, strlen(reason)-1, reason);
897 log_error(LOG_LEVEL_ERROR,
898 "mbedtls_ssl_handshake with server failed: %s", err_buf);
899 free_certificate_chain(csp);
906 log_error(LOG_LEVEL_CONNECT, "Server successfully connected over TLS/SSL");
909 * Server certificate chain is valid, so we can clean
910 * chain, because we will not send it to client.
912 free_certificate_chain(csp);
914 csp->ssl_with_server_is_opened = 1;
915 csp->server_cert_verification_result =
916 mbedtls_ssl_get_verify_result(&(csp->mbedtls_server_attr.ssl));
919 /* Freeing structures if connection wasn't created successfully */
922 free_server_ssl_structures(csp);
929 /*********************************************************************
931 * Function : close_server_ssl_connection
933 * Description : Closes TLS/SSL connection with server. This function
934 * checks if this connection is already opened.
937 * 1 : csp = Current client state (buffers, headers, etc...)
941 *********************************************************************/
942 static void close_server_ssl_connection(struct client_state *csp)
946 if (csp->ssl_with_server_is_opened == 0)
952 * Notifying the peer that the connection is being closed.
955 ret = mbedtls_ssl_close_notify(&(csp->mbedtls_server_attr.ssl));
956 } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
958 free_server_ssl_structures(csp);
959 csp->ssl_with_server_is_opened = 0;
963 /*********************************************************************
965 * Function : free_server_ssl_structures
967 * Description : Frees structures used for SSL communication with server
970 * 1 : csp = Current client state (buffers, headers, etc...)
974 *********************************************************************/
975 static void free_server_ssl_structures(struct client_state *csp)
978 * We can't use function mbedtls_net_free, because this function
979 * inter alia close TCP connection on set fd. Instead of this
980 * function, we change fd to -1, which is the same what does
981 * rest of mbedtls_net_free function.
983 csp->mbedtls_server_attr.socket_fd.fd = -1;
985 mbedtls_x509_crt_free(&(csp->mbedtls_server_attr.ca_cert));
986 mbedtls_ssl_free(&(csp->mbedtls_server_attr.ssl));
987 mbedtls_ssl_config_free(&(csp->mbedtls_server_attr.conf));
991 /*********************************************************************
993 * Function : close_client_and_server_ssl_connections
995 * Description : Checks if client or server should use secured
996 * connection over SSL and if so, closes all of them.
999 * 1 : csp = Current client state (buffers, headers, etc...)
1003 *********************************************************************/
1004 extern void close_client_and_server_ssl_connections(struct client_state *csp)
1006 if (client_use_ssl(csp) == 1)
1008 close_client_ssl_connection(csp);
1010 if (server_use_ssl(csp) == 1)
1012 close_server_ssl_connection(csp);
1016 /*====================== Certificates ======================*/
1018 /*********************************************************************
1020 * Function : write_certificate
1022 * Description : Writes certificate into file.
1025 * 1 : crt = certificate to write into file
1026 * 2 : output_file = path to save certificate file
1027 * 3 : f_rng = mbedtls_ctr_drbg_random
1028 * 4 : p_rng = mbedtls_ctr_drbg_context
1030 * Returns : Length of written certificate on success or negative value
1033 *********************************************************************/
1034 static int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
1035 int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1039 unsigned char cert_buf[CERTIFICATE_BUF_SIZE + 1]; /* Buffer for certificate in PEM format + terminating NULL */
1041 char err_buf[ERROR_BUF_SIZE];
1043 memset(cert_buf, 0, sizeof(cert_buf));
1046 * Writing certificate into PEM string. If buffer is too small, function
1047 * returns specific error and no buffer overflow can happen.
1049 if ((ret = mbedtls_x509write_crt_pem(crt, cert_buf,
1050 sizeof(cert_buf) - 1, f_rng, p_rng)) != 0)
1052 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1053 log_error(LOG_LEVEL_ERROR,
1054 "Writing certificate into buffer failed: %s", err_buf);
1058 len = strlen((char *)cert_buf);
1061 * Saving certificate into file
1063 if ((f = fopen(output_file, "w")) == NULL)
1065 log_error(LOG_LEVEL_ERROR, "Opening file %s to save certificate failed",
1070 if (fwrite(cert_buf, 1, len, f) != len)
1072 log_error(LOG_LEVEL_ERROR,
1073 "Writing certificate into file %s failed", output_file);
1084 /*********************************************************************
1086 * Function : write_private_key
1088 * Description : Writes private key into file and copies saved
1089 * content into given pointer to string. If function
1090 * returns 0 for success, this copy must be freed by
1094 * 1 : key = key to write into file
1095 * 2 : ret_buf = pointer to string with created key file content
1096 * 3 : key_file_path = path where to save key file
1098 * Returns : Length of written private key on success or negative value
1101 *********************************************************************/
1102 static int write_private_key(mbedtls_pk_context *key, unsigned char **ret_buf,
1103 const char *key_file_path)
1105 size_t len = 0; /* Length of created key */
1106 FILE *f = NULL; /* File to save certificate */
1108 char err_buf[ERROR_BUF_SIZE];
1110 /* Initializing buffer for key file content */
1111 *ret_buf = zalloc_or_die(PRIVATE_KEY_BUF_SIZE + 1);
1114 * Writing private key into PEM string
1116 if ((ret = mbedtls_pk_write_key_pem(key, *ret_buf, PRIVATE_KEY_BUF_SIZE)) != 0)
1118 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1119 log_error(LOG_LEVEL_ERROR,
1120 "Writing private key into PEM string failed: %s", err_buf);
1124 len = strlen((char *)*ret_buf);
1127 * Saving key into file
1129 if ((f = fopen(key_file_path, "wb")) == NULL)
1131 log_error(LOG_LEVEL_ERROR,
1132 "Opening file %s to save private key failed: %E",
1138 if (fwrite(*ret_buf, 1, len, f) != len)
1141 log_error(LOG_LEVEL_ERROR,
1142 "Writing private key into file %s failed",
1161 /*********************************************************************
1163 * Function : generate_key
1165 * Description : Tests if private key for host saved in csp already
1166 * exists. If this file doesn't exists, a new key is
1167 * generated and saved in a file. The generated key is also
1168 * copied into given parameter key_buf, which must be then
1169 * freed by caller. If file with key exists, key_buf
1170 * contain NULL and no private key is generated.
1173 * 1 : csp = Current client state (buffers, headers, etc...)
1174 * 2 : key_buf = buffer to save new generated key
1176 * Returns : -1 => Error while generating private key
1177 * 0 => Key already exists
1178 * >0 => Length of generated private key
1180 *********************************************************************/
1181 static int generate_key(struct client_state *csp, unsigned char **key_buf)
1183 mbedtls_pk_context key;
1184 key_options key_opt;
1186 char err_buf[ERROR_BUF_SIZE];
1188 key_opt.key_file_path = NULL;
1191 * Initializing structures for key generating
1193 mbedtls_pk_init(&key);
1196 * Preparing path for key file and other properties for generating key
1198 key_opt.type = MBEDTLS_PK_RSA;
1199 key_opt.rsa_keysize = RSA_KEYSIZE;
1201 key_opt.key_file_path = make_certs_path(csp->config->certificate_directory,
1202 (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1203 if (key_opt.key_file_path == NULL)
1210 * Test if key already exists. If so, we don't have to create it again.
1212 if (file_exists(key_opt.key_file_path) == 1)
1221 ret = seed_rng(csp);
1229 * Setting attributes of private key and generating it
1231 if ((ret = mbedtls_pk_setup(&key,
1232 mbedtls_pk_info_from_type(key_opt.type))) != 0)
1234 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1235 log_error(LOG_LEVEL_ERROR, "mbedtls_pk_setup failed: %s", err_buf);
1240 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random,
1241 &ctr_drbg, (unsigned)key_opt.rsa_keysize, RSA_KEY_PUBLIC_EXPONENT);
1244 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1245 log_error(LOG_LEVEL_ERROR, "Key generating failed: %s", err_buf);
1251 * Exporting private key into file
1253 if ((ret = write_private_key(&key, key_buf, key_opt.key_file_path)) < 0)
1255 log_error(LOG_LEVEL_ERROR,
1256 "Writing private key into file %s failed", key_opt.key_file_path);
1263 * Freeing used variables
1265 freez(key_opt.key_file_path);
1267 mbedtls_pk_free(&key);
1273 /*********************************************************************
1275 * Function : ssl_certificate_is_invalid
1277 * Description : Checks whether or not a certificate is valid.
1278 * Currently only checks that the certificate can be
1279 * parsed and that the "valid to" date is in the future.
1282 * 1 : cert_file = The certificate to check
1284 * Returns : 0 => The certificate is valid.
1285 * 1 => The certificate is invalid
1287 *********************************************************************/
1288 static int ssl_certificate_is_invalid(const char *cert_file)
1290 mbedtls_x509_crt cert;
1293 mbedtls_x509_crt_init(&cert);
1295 ret = mbedtls_x509_crt_parse_file(&cert, cert_file);
1298 char err_buf[ERROR_BUF_SIZE];
1300 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1301 log_error(LOG_LEVEL_ERROR,
1302 "Loading certificate %s to check validity failed: %s",
1303 cert_file, err_buf);
1304 mbedtls_x509_crt_free(&cert);
1308 if (mbedtls_x509_time_is_past(&cert.valid_to))
1310 mbedtls_x509_crt_free(&cert);
1315 mbedtls_x509_crt_free(&cert);
1322 /*********************************************************************
1324 * Function : generate_certificate_valid_date
1326 * Description : Turns a time_t into the format expected by mbedTLS.
1329 * 1 : time_spec = The timestamp to convert
1330 * 2 : buffer = The buffer to write the date to
1331 * 3 : buffer_size = The size of the buffer
1333 * Returns : 0 => The conversion worked
1334 * 1 => The conversion failed
1336 *********************************************************************/
1337 static int generate_certificate_valid_date(time_t time_spec, char *buffer,
1340 struct tm valid_date;
1344 timeptr = privoxy_gmtime_r(&time_spec, &valid_date);
1345 if (NULL == timeptr)
1350 ret = strftime(buffer, buffer_size, "%Y%m%d%H%M%S", timeptr);
1361 /*********************************************************************
1363 * Function : get_certificate_valid_from_date
1365 * Description : Generates a "valid from" date in the format
1366 * expected by mbedTLS.
1369 * 1 : buffer = The buffer to write the date to
1370 * 2 : buffer_size = The size of the buffer
1372 * Returns : 0 => The generation worked
1373 * 1 => The generation failed
1375 *********************************************************************/
1376 static int get_certificate_valid_from_date(char *buffer, size_t buffer_size)
1380 time_spec = time(NULL);
1381 /* 1 month in the past */
1382 time_spec -= 30 * 24 * 60 * 60;
1384 return generate_certificate_valid_date(time_spec, buffer, buffer_size);
1389 /*********************************************************************
1391 * Function : get_certificate_valid_to_date
1393 * Description : Generates a "valid to" date in the format
1394 * expected by mbedTLS.
1397 * 1 : buffer = The buffer to write the date to
1398 * 2 : buffer_size = The size of the buffer
1400 * Returns : 0 => The generation worked
1401 * 1 => The generation failed
1403 *********************************************************************/
1404 static int get_certificate_valid_to_date(char *buffer, size_t buffer_size)
1408 time_spec = time(NULL);
1409 /* Three months in the future */
1410 time_spec += 90 * 24 * 60 * 60;
1412 return generate_certificate_valid_date(time_spec, buffer, buffer_size);
1417 /*********************************************************************
1419 * Function : set_subject_alternative_name
1421 * Description : Sets the Subject Alternative Name extension to a cert
1424 * 1 : cert = The certificate to modify
1425 * 2 : hostname = The hostname to add
1427 * Returns : <0 => Error while creating certificate.
1430 *********************************************************************/
1431 static int set_subject_alternative_name(mbedtls_x509write_cert *cert, const char *hostname)
1433 char err_buf[ERROR_BUF_SIZE];
1435 char *subject_alternative_name;
1436 size_t subject_alternative_name_len;
1437 #define MBEDTLS_SUBJECT_ALTERNATIVE_NAME_MAX_LEN 255
1438 unsigned char san_buf[MBEDTLS_SUBJECT_ALTERNATIVE_NAME_MAX_LEN + 1];
1442 subject_alternative_name_len = strlen(hostname) + 1;
1443 subject_alternative_name = zalloc_or_die(subject_alternative_name_len);
1445 strlcpy(subject_alternative_name, hostname, subject_alternative_name_len);
1447 memset(san_buf, 0, sizeof(san_buf));
1449 c = san_buf + sizeof(san_buf);
1452 ret = mbedtls_asn1_write_raw_buffer(&c, san_buf,
1453 (const unsigned char *)subject_alternative_name,
1454 strlen(subject_alternative_name));
1457 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1458 log_error(LOG_LEVEL_ERROR,
1459 "mbedtls_asn1_write_raw_buffer() failed: %s", err_buf);
1464 ret = mbedtls_asn1_write_len(&c, san_buf, strlen(subject_alternative_name));
1467 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1468 log_error(LOG_LEVEL_ERROR,
1469 "mbedtls_asn1_write_len() failed: %s", err_buf);
1474 ret = mbedtls_asn1_write_tag(&c, san_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2);
1477 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1478 log_error(LOG_LEVEL_ERROR,
1479 "mbedtls_asn1_write_tag() failed: %s", err_buf);
1484 ret = mbedtls_asn1_write_len(&c, san_buf, (size_t)len);
1487 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1488 log_error(LOG_LEVEL_ERROR,
1489 "mbedtls_asn1_write_len() failed: %s", err_buf);
1494 ret = mbedtls_asn1_write_tag(&c, san_buf,
1495 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1498 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1499 log_error(LOG_LEVEL_ERROR,
1500 "mbedtls_asn1_write_tag() failed: %s", err_buf);
1505 ret = mbedtls_x509write_crt_set_extension(cert,
1506 MBEDTLS_OID_SUBJECT_ALT_NAME,
1507 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
1508 0, san_buf + sizeof(san_buf) - len, (size_t)len);
1511 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1512 log_error(LOG_LEVEL_ERROR,
1513 "mbedtls_x509write_crt_set_extension() failed: %s", err_buf);
1517 freez(subject_alternative_name);
1524 /*********************************************************************
1526 * Function : host_is_ip_address
1528 * Description : Checks whether or not a host is specified by
1529 * IP address. Does not actually validate the
1533 * 1 : host = The host name to check
1535 * Returns : 1 => Yes
1538 *********************************************************************/
1539 static int host_is_ip_address(const char *host)
1543 if (NULL != strstr(host, ":"))
1545 /* Assume an IPv6 address. */
1549 for (p = host; *p; p++)
1551 if ((*p != '.') && !privoxy_isdigit(*p))
1553 /* Not a dot or digit so it can't be an IPv4 address. */
1559 * Host only consists of dots and digits so
1560 * assume that is an IPv4 address.
1567 /*********************************************************************
1569 * Function : generate_webpage_certificate
1571 * Description : Creates certificate file in presetted directory.
1572 * If certificate already exists, no other certificate
1573 * will be created. Subject of certificate is named
1574 * by csp->http->host from parameter. This function also
1575 * triggers generating of private key for new certificate.
1578 * 1 : csp = Current client state (buffers, headers, etc...)
1580 * Returns : -1 => Error while creating certificate.
1581 * 0 => Certificate already exists.
1582 * >0 => Length of created certificate.
1584 *********************************************************************/
1585 static int generate_webpage_certificate(struct client_state *csp)
1587 mbedtls_x509_crt issuer_cert;
1588 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
1589 mbedtls_pk_context *issuer_key = &loaded_issuer_key;
1590 mbedtls_pk_context *subject_key = &loaded_subject_key;
1591 mbedtls_x509write_cert cert;
1594 unsigned char *key_buf = NULL; /* Buffer for created key */
1597 char err_buf[ERROR_BUF_SIZE];
1598 cert_options cert_opt;
1599 char cert_valid_from[15];
1600 char cert_valid_to[15];
1602 /* Paths to keys and certificates needed to create certificate */
1603 cert_opt.issuer_key = NULL;
1604 cert_opt.subject_key = NULL;
1605 cert_opt.issuer_crt = NULL;
1607 cert_opt.output_file = make_certs_path(csp->config->certificate_directory,
1608 (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE);
1609 if (cert_opt.output_file == NULL)
1614 cert_opt.subject_key = make_certs_path(csp->config->certificate_directory,
1615 (const char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE);
1616 if (cert_opt.subject_key == NULL)
1618 freez(cert_opt.output_file);
1622 if (file_exists(cert_opt.output_file) == 1)
1624 /* The file exists, but is it valid? */
1625 if (ssl_certificate_is_invalid(cert_opt.output_file))
1627 log_error(LOG_LEVEL_CONNECT,
1628 "Certificate %s is no longer valid. Removing it.",
1629 cert_opt.output_file);
1630 if (unlink(cert_opt.output_file))
1632 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1633 cert_opt.output_file);
1635 freez(cert_opt.output_file);
1636 freez(cert_opt.subject_key);
1640 if (unlink(cert_opt.subject_key))
1642 log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1643 cert_opt.subject_key);
1645 freez(cert_opt.output_file);
1646 freez(cert_opt.subject_key);
1653 freez(cert_opt.output_file);
1654 freez(cert_opt.subject_key);
1661 * Create key for requested host
1663 int subject_key_len = generate_key(csp, &key_buf);
1664 if (subject_key_len < 0)
1666 freez(cert_opt.output_file);
1667 freez(cert_opt.subject_key);
1668 log_error(LOG_LEVEL_ERROR, "Key generating failed");
1673 * Initializing structures for certificate generating
1675 mbedtls_x509write_crt_init(&cert);
1676 mbedtls_x509write_crt_set_md_alg(&cert, CERT_SIGNATURE_ALGORITHM);
1677 mbedtls_pk_init(&loaded_issuer_key);
1678 mbedtls_pk_init(&loaded_subject_key);
1679 mbedtls_mpi_init(&serial);
1680 mbedtls_x509_crt_init(&issuer_cert);
1683 * Presetting parameters for certificate. We must compute total length
1686 size_t cert_params_len = strlen(CERT_PARAM_COMMON_NAME) +
1687 strlen(CERT_PARAM_ORGANIZATION) + strlen(CERT_PARAM_COUNTRY) +
1688 strlen(CERT_PARAM_ORG_UNIT) +
1689 3 * strlen(csp->http->host) + 1;
1690 char cert_params[cert_params_len];
1691 memset(cert_params, 0, cert_params_len);
1694 * Converting unsigned long serial number to char * serial number.
1695 * We must compute length of serial number in string + terminating null.
1697 unsigned long certificate_serial = get_certificate_serial(csp);
1698 unsigned long certificate_serial_time = (unsigned long)time(NULL);
1699 int serial_num_size = snprintf(NULL, 0, "%lu%lu",
1700 certificate_serial_time, certificate_serial) + 1;
1701 if (serial_num_size <= 0)
1703 serial_num_size = 1;
1706 char serial_num_text[serial_num_size]; /* Buffer for serial number */
1707 ret = snprintf(serial_num_text, (size_t)serial_num_size, "%lu%lu",
1708 certificate_serial_time, certificate_serial);
1709 if (ret < 0 || ret >= serial_num_size)
1711 log_error(LOG_LEVEL_ERROR,
1712 "Converting certificate serial number into string failed");
1718 * Preparing parameters for certificate
1720 strlcpy(cert_params, CERT_PARAM_COMMON_NAME, cert_params_len);
1721 strlcat(cert_params, csp->http->host, cert_params_len);
1722 strlcat(cert_params, CERT_PARAM_ORGANIZATION, cert_params_len);
1723 strlcat(cert_params, csp->http->host, cert_params_len);
1724 strlcat(cert_params, CERT_PARAM_ORG_UNIT, cert_params_len);
1725 strlcat(cert_params, csp->http->host, cert_params_len);
1726 strlcat(cert_params, CERT_PARAM_COUNTRY, cert_params_len);
1728 cert_opt.issuer_crt = csp->config->ca_cert_file;
1729 cert_opt.issuer_key = csp->config->ca_key_file;
1731 if (get_certificate_valid_from_date(cert_valid_from, sizeof(cert_valid_from))
1732 || get_certificate_valid_to_date(cert_valid_to, sizeof(cert_valid_to)))
1734 log_error(LOG_LEVEL_ERROR, "Generating one of the validity dates failed");
1739 cert_opt.subject_pwd = CERT_SUBJECT_PASSWORD;
1740 cert_opt.issuer_pwd = csp->config->ca_password;
1741 cert_opt.subject_name = cert_params;
1742 cert_opt.not_before = cert_valid_from;
1743 cert_opt.not_after = cert_valid_to;
1744 cert_opt.serial = serial_num_text;
1746 cert_opt.max_pathlen = -1;
1749 * Test if the private key was already created.
1750 * XXX: Can this still happen?
1752 if (subject_key_len == 0)
1754 log_error(LOG_LEVEL_ERROR, "Subject key was already created");
1762 ret = seed_rng(csp);
1770 * Parse serial to MPI
1772 ret = mbedtls_mpi_read_string(&serial, 10, cert_opt.serial);
1775 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1776 log_error(LOG_LEVEL_ERROR,
1777 "mbedtls_mpi_read_string failed: %s", err_buf);
1783 * Loading certificates
1785 ret = mbedtls_x509_crt_parse_file(&issuer_cert, cert_opt.issuer_crt);
1788 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1789 log_error(LOG_LEVEL_ERROR, "Loading issuer certificate %s failed: %s",
1790 cert_opt.issuer_crt, err_buf);
1795 ret = mbedtls_x509_dn_gets(cert_opt.issuer_name,
1796 sizeof(cert_opt.issuer_name), &issuer_cert.subject);
1799 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1800 log_error(LOG_LEVEL_ERROR, "mbedtls_x509_dn_gets failed: %s", err_buf);
1806 * Loading keys from file or from buffer
1808 if (key_buf != NULL && subject_key_len > 0)
1810 /* Key was created in this function and is stored in buffer */
1811 ret = mbedtls_pk_parse_key(&loaded_subject_key, key_buf,
1812 (size_t)(subject_key_len + 1), (unsigned const char *)
1813 cert_opt.subject_pwd, strlen(cert_opt.subject_pwd));
1817 /* Key wasn't created in this function, because it already existed */
1818 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key,
1819 cert_opt.subject_key, cert_opt.subject_pwd);
1824 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1825 log_error(LOG_LEVEL_ERROR, "Parsing subject key %s failed: %s",
1826 cert_opt.subject_key, err_buf);
1831 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, cert_opt.issuer_key,
1832 cert_opt.issuer_pwd);
1835 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1836 log_error(LOG_LEVEL_ERROR,
1837 "Parsing issuer key %s failed: %s", cert_opt.issuer_key, err_buf);
1843 * Check if key and issuer certificate match
1845 if (!mbedtls_pk_can_do(&issuer_cert.pk, MBEDTLS_PK_RSA) ||
1846 mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa(issuer_cert.pk)->N,
1847 &mbedtls_pk_rsa(*issuer_key)->N) != 0 ||
1848 mbedtls_mpi_cmp_mpi(&mbedtls_pk_rsa(issuer_cert.pk)->E,
1849 &mbedtls_pk_rsa(*issuer_key)->E) != 0)
1851 log_error(LOG_LEVEL_ERROR,
1852 "Issuer key doesn't match issuer certificate");
1857 mbedtls_x509write_crt_set_subject_key(&cert, subject_key);
1858 mbedtls_x509write_crt_set_issuer_key(&cert, issuer_key);
1861 * Setting parameters of signed certificate
1863 ret = mbedtls_x509write_crt_set_subject_name(&cert, cert_opt.subject_name);
1866 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1867 log_error(LOG_LEVEL_ERROR,
1868 "Setting subject name in signed certificate failed: %s", err_buf);
1873 ret = mbedtls_x509write_crt_set_issuer_name(&cert, cert_opt.issuer_name);
1876 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1877 log_error(LOG_LEVEL_ERROR,
1878 "Setting issuer name in signed certificate failed: %s", err_buf);
1883 ret = mbedtls_x509write_crt_set_serial(&cert, &serial);
1886 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1887 log_error(LOG_LEVEL_ERROR,
1888 "Setting serial number in signed certificate failed: %s", err_buf);
1893 ret = mbedtls_x509write_crt_set_validity(&cert, cert_opt.not_before,
1894 cert_opt.not_after);
1897 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1898 log_error(LOG_LEVEL_ERROR,
1899 "Setting validity in signed certificate failed: %s", err_buf);
1905 * Setting the basicConstraints extension for certificate
1907 ret = mbedtls_x509write_crt_set_basic_constraints(&cert, cert_opt.is_ca,
1908 cert_opt.max_pathlen);
1911 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1912 log_error(LOG_LEVEL_ERROR, "Setting the basicConstraints extension "
1913 "in signed certificate failed: %s", err_buf);
1918 #if defined(MBEDTLS_SHA1_C)
1919 /* Setting the subjectKeyIdentifier extension for certificate */
1920 ret = mbedtls_x509write_crt_set_subject_key_identifier(&cert);
1923 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1924 log_error(LOG_LEVEL_ERROR, "mbedtls_x509write_crt_set_subject_key_"
1925 "identifier failed: %s", err_buf);
1930 /* Setting the authorityKeyIdentifier extension for certificate */
1931 ret = mbedtls_x509write_crt_set_authority_key_identifier(&cert);
1934 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1935 log_error(LOG_LEVEL_ERROR, "mbedtls_x509write_crt_set_authority_key_"
1936 "identifier failed: %s", err_buf);
1940 #endif /* MBEDTLS_SHA1_C */
1942 if (!host_is_ip_address(csp->http->host) &&
1943 set_subject_alternative_name(&cert, csp->http->host))
1945 /* Errors are already logged by set_subject_alternative_name() */
1951 * Writing certificate into file
1953 ret = write_certificate(&cert, cert_opt.output_file,
1954 mbedtls_ctr_drbg_random, &ctr_drbg);
1957 log_error(LOG_LEVEL_ERROR, "Writing certificate into file failed");
1963 * Freeing used structures
1965 mbedtls_x509write_crt_free(&cert);
1966 mbedtls_pk_free(&loaded_subject_key);
1967 mbedtls_pk_free(&loaded_issuer_key);
1968 mbedtls_mpi_free(&serial);
1969 mbedtls_x509_crt_free(&issuer_cert);
1971 freez(cert_opt.subject_key);
1972 freez(cert_opt.output_file);
1979 /*********************************************************************
1981 * Function : make_certs_path
1983 * Description : Creates path to file from three pieces. This function
1984 * takes parameters and puts them in one new mallocated
1985 * char * in correct order. Returned variable must be freed
1986 * by caller. This function is mainly used for creating
1987 * paths of certificates and keys files.
1990 * 1 : conf_dir = Name/path of directory where is the file.
1991 * '.' can be used for current directory.
1992 * 2 : file_name = Name of file in conf_dir without suffix.
1993 * 3 : suffix = Suffix of given file_name.
1995 * Returns : path => Path was built up successfully
1996 * NULL => Path can't be built up
1998 *********************************************************************/
1999 static char *make_certs_path(const char *conf_dir, const char *file_name,
2002 /* Test if all given parameters are valid */
2003 if (conf_dir == NULL || *conf_dir == '\0' || file_name == NULL ||
2004 *file_name == '\0' || suffix == NULL || *suffix == '\0')
2006 log_error(LOG_LEVEL_ERROR,
2007 "make_certs_path failed: bad input parameters");
2012 size_t path_size = strlen(conf_dir)
2013 + strlen(file_name) + strlen(suffix) + 2;
2015 /* Setting delimiter and editing path length */
2016 #if defined(_WIN32) || defined(__OS2__)
2017 char delim[] = "\\";
2019 #else /* ifndef _WIN32 || __OS2__ */
2021 #endif /* ifndef _WIN32 || __OS2__ */
2024 * Building up path from many parts
2027 if (*conf_dir != '/' && basedir && *basedir)
2030 * Replacing conf_dir with basedir. This new variable contains
2031 * absolute path to cwd.
2033 path_size += strlen(basedir) + 2;
2034 path = zalloc_or_die(path_size);
2036 strlcpy(path, basedir, path_size);
2037 strlcat(path, delim, path_size);
2038 strlcat(path, conf_dir, path_size);
2039 strlcat(path, delim, path_size);
2040 strlcat(path, file_name, path_size);
2041 strlcat(path, suffix, path_size);
2044 #endif /* defined unix */
2046 path = zalloc_or_die(path_size);
2048 strlcpy(path, conf_dir, path_size);
2049 strlcat(path, delim, path_size);
2050 strlcat(path, file_name, path_size);
2051 strlcat(path, suffix, path_size);
2058 /*********************************************************************
2060 * Function : get_certificate_serial
2062 * Description : Computes serial number for new certificate from host
2063 * name hash. This hash must be already saved in csp
2067 * 1 : csp = Current client state (buffers, headers, etc...)
2069 * Returns : Serial number for new certificate
2071 *********************************************************************/
2072 static unsigned long get_certificate_serial(struct client_state *csp)
2074 unsigned long exp = 1;
2075 unsigned long serial = 0;
2077 int i = CERT_SERIAL_NUM_LENGTH;
2081 serial += exp * (unsigned)csp->http->hash_of_host[i];
2088 /*********************************************************************
2090 * Function : ssl_send_certificate_error
2092 * Description : Sends info about invalid server certificate to client.
2093 * Sent message is including all trusted chain certificates,
2094 * that can be downloaded in web browser.
2097 * 1 : csp = Current client state (buffers, headers, etc...)
2101 *********************************************************************/
2102 extern void ssl_send_certificate_error(struct client_state *csp)
2104 size_t message_len = 0;
2106 struct certs_chain *cert = NULL;
2108 /* Header of message with certificate information */
2109 const char message_begin[] =
2110 "HTTP/1.1 200 OK\r\n"
2111 "Content-Type: text/html\r\n"
2112 "Connection: close\r\n\r\n"
2114 "<html><head><title>Server certificate verification failed</title></head>\n"
2115 "<body><h1>Server certificate verification failed</h1>\n"
2116 "<p><a href=\"https://" CGI_SITE_2_HOST "/\">Privoxy</a> was unable "
2117 "to securely connnect to the destination server.</p>"
2119 const char message_end[] = "</body></html>\r\n\r\n";
2120 char reason[INVALID_CERT_INFO_BUF_SIZE];
2121 memset(reason, 0, sizeof(reason));
2123 /* Get verification message from verification return code */
2124 mbedtls_x509_crt_verify_info(reason, sizeof(reason), " ",
2125 csp->server_cert_verification_result);
2128 * Computing total length of message with all certificates inside
2130 message_len = strlen(message_begin) + strlen(message_end)
2131 + strlen(reason) + strlen("</p>") + 1;
2133 cert = &(csp->server_certs_chain);
2134 while (cert->next != NULL)
2136 size_t base64_len = 4 * ((strlen(cert->file_buf) + 2) / 3) + 1;
2138 message_len += strlen(cert->info_buf) + strlen("<pre></pre>\n")
2139 + base64_len + strlen("<a href=\"data:application"
2140 "/x-x509-ca-cert;base64,\">Download certificate</a>");
2145 * Joining all blocks in one long message
2147 char message[message_len];
2148 memset(message, 0, message_len);
2150 strlcpy(message, message_begin, message_len);
2151 strlcat(message, reason , message_len);
2152 strlcat(message, "</p>" , message_len);
2154 cert = &(csp->server_certs_chain);
2155 while (cert->next != NULL)
2158 size_t base64_len = 4 * ((strlen(cert->file_buf) + 2) / 3) + 1; /* +1 for terminating null*/
2159 char base64_buf[base64_len];
2160 memset(base64_buf, 0, base64_len);
2162 /* Encoding certificate into base64 code */
2163 ret = mbedtls_base64_encode((unsigned char*)base64_buf,
2164 base64_len, &olen, (const unsigned char*)cert->file_buf,
2165 strlen(cert->file_buf));
2168 log_error(LOG_LEVEL_ERROR,
2169 "Encoding to base64 failed, buffer is to small");
2172 strlcat(message, "<pre>", message_len);
2173 strlcat(message, cert->info_buf, message_len);
2174 strlcat(message, "</pre>\n", message_len);
2178 strlcat(message, "<a href=\"data:application/x-x509-ca-cert;base64,",
2180 strlcat(message, base64_buf, message_len);
2181 strlcat(message, "\">Download certificate</a>", message_len);
2186 strlcat(message, message_end, message_len);
2189 * Sending final message to client
2191 ssl_send_data(&(csp->mbedtls_client_attr.ssl),
2192 (const unsigned char *)message, strlen(message));
2194 free_certificate_chain(csp);
2198 /*********************************************************************
2200 * Function : ssl_verify_callback
2202 * Description : This is a callback function for certificate verification.
2203 * It's called once for each certificate in the server's
2204 * certificate trusted chain and prepares information about
2205 * the certificate. The information can be used to inform
2206 * the user about invalid certificates.
2209 * 1 : csp_void = Current client state (buffers, headers, etc...)
2210 * 2 : crt = certificate from trusted chain
2211 * 3 : depth = depth in trusted chain
2212 * 4 : flags = certificate flags
2214 * Returns : 0 on success and negative value on error
2216 *********************************************************************/
2217 static int ssl_verify_callback(void *csp_void, mbedtls_x509_crt *crt,
2218 int depth, uint32_t *flags)
2220 struct client_state *csp = (struct client_state *)csp_void;
2221 struct certs_chain *last = &(csp->server_certs_chain);
2226 * Searching for last item in certificates linked list
2228 while (last->next != NULL)
2234 * Preparing next item in linked list for next certificate
2236 last->next = malloc_or_die(sizeof(struct certs_chain));
2237 last->next->next = NULL;
2238 memset(last->next->info_buf, 0, sizeof(last->next->info_buf));
2239 memset(last->next->file_buf, 0, sizeof(last->next->file_buf));
2242 * Saving certificate file into buffer
2244 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
2245 crt->raw.p, crt->raw.len, (unsigned char *)last->file_buf,
2246 sizeof(last->file_buf)-1, &olen)) != 0)
2248 char err_buf[ERROR_BUF_SIZE];
2250 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
2251 log_error(LOG_LEVEL_ERROR, "mbedtls_pem_write_buffer() failed: %s",
2258 * Saving certificate information into buffer
2261 char buf[CERT_INFO_BUF_SIZE];
2264 mbedtls_x509_crt_info(buf, sizeof(buf), CERT_INFO_PREFIX, crt);
2265 encoded_text = html_encode(buf);
2266 strlcpy(last->info_buf, encoded_text, sizeof(last->info_buf));
2267 freez(encoded_text);
2274 /*********************************************************************
2276 * Function : free_certificate_chain
2278 * Description : Frees certificates linked list. This linked list is
2279 * used to save information about certificates in
2283 * 1 : csp = Current client state (buffers, headers, etc...)
2287 *********************************************************************/
2288 static void free_certificate_chain(struct client_state *csp)
2290 struct certs_chain *cert = csp->server_certs_chain.next;
2292 /* Cleaning buffers */
2293 memset(csp->server_certs_chain.info_buf, 0,
2294 sizeof(csp->server_certs_chain.info_buf));
2295 memset(csp->server_certs_chain.file_buf, 0,
2296 sizeof(csp->server_certs_chain.file_buf));
2297 csp->server_certs_chain.next = NULL;
2299 /* Freeing memory in whole linked list */
2300 while (cert != NULL)
2302 struct certs_chain *cert_for_free = cert;
2304 freez(cert_for_free);
2309 /*********************************************************************
2311 * Function : file_exists
2313 * Description : Tests if file exists and is readable.
2316 * 1 : path = Path to tested file.
2318 * Returns : 1 => File exists and is readable.
2319 * 0 => File doesn't exist or is not readable.
2321 *********************************************************************/
2322 static int file_exists(const char *path)
2325 if ((f = fopen(path, "r")) != NULL)
2335 /*********************************************************************
2337 * Function : host_to_hash
2339 * Description : Creates MD5 hash from host name. Host name is loaded
2340 * from structure csp and saved again into it.
2343 * 1 : csp = Current client state (buffers, headers, etc...)
2345 * Returns : 1 => Error while creating hash
2346 * 0 => Hash created successfully
2348 *********************************************************************/
2349 static int host_to_hash(struct client_state *csp)
2353 #if !defined(MBEDTLS_MD5_C)
2354 #error mbedTLS needs to be compiled with md5 support
2356 memset(csp->http->hash_of_host, 0, sizeof(csp->http->hash_of_host));
2357 mbedtls_md5((unsigned char *)csp->http->host, strlen(csp->http->host),
2358 csp->http->hash_of_host);
2360 /* Converting hash into string with hex */
2364 if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
2365 csp->http->hash_of_host[i])) < 0)
2367 log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
2373 #endif /* MBEDTLS_MD5_C */
2377 /*********************************************************************
2379 * Function : tunnel_established_successfully
2381 * Description : Check if parent proxy server response contains
2382 * information about successfully created connection with
2383 * destination server. (HTTP/... 2xx ...)
2386 * 1 : server_response = Buffer with parent proxy server response
2387 * 2 : response_len = Length of server_response
2389 * Returns : 1 => Connection created successfully
2390 * 0 => Connection wasn't created successfully
2392 *********************************************************************/
2393 extern int tunnel_established_successfully(const char *server_response,
2394 unsigned int response_len)
2396 unsigned int pos = 0;
2398 if (server_response == NULL)
2403 /* Tests if "HTTP/" string is at the begin of received response */
2404 if (strncmp(server_response, "HTTP/", 5) != 0)
2409 for (pos = 0; pos < response_len; pos++)
2411 if (server_response[pos] == ' ')
2418 * response_len -3 because of buffer end, response structure and 200 code.
2419 * There must be at least 3 chars after space.
2420 * End of buffer: ... 2xx'\0'
2423 if (pos >= (response_len - 3))
2428 /* Test HTTP status code */
2429 if (server_response[pos + 1] != '2')
2438 /*********************************************************************
2440 * Function : seed_rng
2442 * Description : Seeding the RNG for all SSL uses
2445 * 1 : csp = Current client state (buffers, headers, etc...)
2447 * Returns : -1 => RNG wasn't seed successfully
2448 * 0 => RNG is seeded successfully
2450 *********************************************************************/
2451 static int seed_rng(struct client_state *csp)
2454 char err_buf[ERROR_BUF_SIZE];
2456 if (rng_seeded == 0)
2458 privoxy_mutex_lock(&rng_mutex);
2459 if (rng_seeded == 0)
2461 mbedtls_ctr_drbg_init(&ctr_drbg);
2462 mbedtls_entropy_init(&entropy);
2463 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
2467 mbedtls_strerror(ret, err_buf, sizeof(err_buf));
2468 log_error(LOG_LEVEL_ERROR,
2469 "mbedtls_ctr_drbg_seed failed: %s", err_buf);
2470 privoxy_mutex_unlock(&rng_mutex);
2475 privoxy_mutex_unlock(&rng_mutex);