tools/url-pattern-translator.pl: Skip CLIENT-TAG patterns
[privoxy.git] / ssl.c
1 /*********************************************************************
2  *
3  * File        :  $Source: /cvsroot/ijbswa/current/ssl.c,v $
4  *
5  * Purpose     :  File with TLS/SSL extension. Contains methods for
6  *                creating, using and closing TLS/SSL connections.
7  *
8  * Copyright   :  Written by and Copyright (c) 2017 Vaclav Svec. FIT CVUT.
9  *                Copyright (C) 2018-2020 by Fabian Keil <fk@fabiankeil.de>
10  *
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.
16  *
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.
22  *
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.
28  *
29  *********************************************************************/
30
31 #include <ctype.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #if !defined(MBEDTLS_CONFIG_FILE)
36 #  include "mbedtls/config.h"
37 #else
38 #  include MBEDTLS_CONFIG_FILE
39 #endif
40
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"
47
48 #include "config.h"
49 #include "project.h"
50 #include "miscutil.h"
51 #include "errlog.h"
52 #include "jcc.h"
53 #include "ssl.h"
54 #include "encode.h"
55
56
57 /*
58  * Macros for searching begin and end of certificates.
59  * Necessary to convert structure mbedtls_x509_crt to crt file.
60  */
61 #define PEM_BEGIN_CRT     "-----BEGIN CERTIFICATE-----\n"
62 #define PEM_END_CRT       "-----END CERTIFICATE-----\n"
63
64 /*
65  * Macros for ssl.c
66  */
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                 ""
83
84 /*
85  * Properties of cert for generating
86  */
87 typedef struct {
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                   */
101 } cert_options;
102
103 /*
104  * Properties of key for generating
105  */
106 typedef struct {
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 */
110 } key_options;
111
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);
122
123 /*********************************************************************
124  *
125  * Function    :  client_use_ssl
126  *
127  * Description :  Tests if client in current client state structure
128  *                should use SSL connection or standard connection.
129  *
130  * Parameters  :
131  *          1  :  csp = Current client state (buffers, headers, etc...)
132  *
133  * Returns     :  If client should use TLS/SSL connection, 1 is returned.
134  *                Otherwise 0 is returned.
135  *
136  *********************************************************************/
137 extern int client_use_ssl(const struct client_state *csp)
138 {
139    return csp->http->client_ssl;
140 }
141
142
143 /*********************************************************************
144  *
145  * Function    :  server_use_ssl
146  *
147  * Description :  Tests if server in current client state structure
148  *                should use SSL connection or standard connection.
149  *
150  * Parameters  :
151  *          1  :  csp = Current client state (buffers, headers, etc...)
152  *
153  * Returns     :  If server should use TLS/SSL connection, 1 is returned.
154  *                Otherwise 0 is returned.
155  *
156  *********************************************************************/
157 extern int server_use_ssl(const struct client_state *csp)
158 {
159    return csp->http->server_ssl;
160 }
161
162
163 /*********************************************************************
164  *
165  * Function    :  is_ssl_pending
166  *
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.
171  *
172  * Parameters  :
173  *          1  :  ssl = SSL context to test
174  *
175  * Returns     :   0 => No data are pending
176  *                >0 => Pending data length
177  *
178  *********************************************************************/
179 extern size_t is_ssl_pending(mbedtls_ssl_context *ssl)
180 {
181    if (ssl == NULL)
182    {
183       return 0;
184    }
185
186    return mbedtls_ssl_get_bytes_avail(ssl);
187 }
188
189
190 /*********************************************************************
191  *
192  * Function    :  ssl_send_data
193  *
194  * Description :  Sends the content of buf (for n bytes) to given SSL
195  *                connection context.
196  *
197  * Parameters  :
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
201  *
202  * Returns     :  Length of sent data or negative value on error.
203  *
204  *********************************************************************/
205 extern int ssl_send_data(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
206 {
207    int ret = 0;
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 */
211
212    if (len == 0)
213    {
214       return 0;
215    }
216
217    /* Getting maximal length of data sent in one fragment */
218    max_fragment_size = mbedtls_ssl_get_max_frag_len(ssl);
219
220    /*
221     * Whole buffer must be sent in many fragments, because each fragment
222     * has its maximal length.
223     */
224    while (pos < len)
225    {
226       /* Compute length of data, that can be send in next fragment */
227       if ((pos + (int)max_fragment_size) > len)
228       {
229          send_len = (int)len - pos;
230       }
231       else
232       {
233          send_len = (int)max_fragment_size;
234       }
235
236       log_error(LOG_LEVEL_WRITING, "TLS: %N", send_len, buf+pos);
237
238       /*
239        * Sending one part of the buffer
240        */
241       while ((ret = mbedtls_ssl_write(ssl,
242          (const unsigned char *)(buf + pos),
243          (size_t)send_len)) < 0)
244       {
245          if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
246              ret != MBEDTLS_ERR_SSL_WANT_WRITE)
247          {
248             char err_buf[ERROR_BUF_SIZE];
249
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);
253             return -1;
254          }
255       }
256       /* Adding count of sent bytes to position in buffer */
257       pos = pos + send_len;
258    }
259
260    return (int)len;
261 }
262
263
264 /*********************************************************************
265  *
266  * Function    :  ssl_send_data_delayed
267  *
268  * Description :  Sends the contents of buf (for n bytes) to given SSL
269  *                connection, optionally delaying the operation.
270  *
271  * Parameters  :
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.
276  *
277  * Returns     :  0 on success (entire buffer sent).
278  *                nonzero on error.
279  *
280  *********************************************************************/
281 extern int ssl_send_data_delayed(mbedtls_ssl_context *ssl,
282                                  const unsigned char *buf, size_t len,
283                                  unsigned int delay)
284 {
285    size_t i = 0;
286
287    if (delay == 0)
288    {
289       if (ssl_send_data(ssl, buf, len) < 0)
290       {
291          return -1;
292       }
293       else
294       {
295          return 0;
296       }
297    }
298
299    while (i < len)
300    {
301       size_t write_length;
302       enum { MAX_WRITE_LENGTH = 10 };
303
304       if ((i + MAX_WRITE_LENGTH) > len)
305       {
306          write_length = len - i;
307       }
308       else
309       {
310          write_length = MAX_WRITE_LENGTH;
311       }
312
313       privoxy_millisleep(delay);
314
315       if (ssl_send_data(ssl, buf + i, write_length) < 0)
316       {
317          return -1;
318       }
319       i += write_length;
320    }
321
322    return 0;
323
324 }
325
326
327 /*********************************************************************
328  *
329  * Function    :  ssl_recv_data
330  *
331  * Description :  Receives data from given SSL context and puts
332  *                it into buffer.
333  *
334  * Parameters  :
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
338  *
339  * Returns     :  Number of bytes read, 0 for EOF, or -1
340  *                on error.
341  *
342  *********************************************************************/
343 extern int ssl_recv_data(mbedtls_ssl_context *ssl, unsigned char *buf, size_t max_length)
344 {
345    int ret = 0;
346    memset(buf, 0, max_length);
347
348    /*
349     * Receiving data from SSL context into buffer
350     */
351    do
352    {
353       ret = mbedtls_ssl_read(ssl, buf, max_length);
354    } while (ret == MBEDTLS_ERR_SSL_WANT_READ
355       || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
356
357    if (ret < 0)
358    {
359       char err_buf[ERROR_BUF_SIZE];
360
361       if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
362       {
363          log_error(LOG_LEVEL_CONNECT,
364             "The peer notified us that the connection is going to be closed");
365          return 0;
366       }
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);
370
371       return -1;
372    }
373
374    log_error(LOG_LEVEL_RECEIVED, "TLS: %N", ret, buf);
375
376    return ret;
377 }
378
379
380 /*********************************************************************
381  *
382  * Function    :  ssl_flush_socket
383  *
384  * Description :  Send any pending "buffered" content with given
385  *                SSL connection. Alternative to function flush_socket.
386  *
387  * Parameters  :
388  *          1  :  ssl = SSL context to send buffer to
389  *          2  :  iob = The I/O buffer to flush, usually csp->iob.
390  *
391  * Returns     :  On success, the number of bytes send are returned (zero
392  *                indicates nothing was sent).  On error, -1 is returned.
393  *
394  *********************************************************************/
395 extern long ssl_flush_socket(mbedtls_ssl_context *ssl, struct iob *iob)
396 {
397    /* Computing length of buffer part to send */
398    long len = iob->eod - iob->cur;
399
400    if (len <= 0)
401    {
402       return(0);
403    }
404
405    /* Sending data to given SSl context */
406    if (ssl_send_data(ssl, (const unsigned char *)iob->cur, (size_t)len) < 0)
407    {
408       return -1;
409    }
410    iob->eod = iob->cur = iob->buf;
411    return(len);
412 }
413
414
415 /*********************************************************************
416  *
417  * Function    :  ssl_debug_callback
418  *
419  * Description :  Debug callback function for mbedtls library.
420  *                Prints info into log file.
421  *
422  * Parameters  :
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
428  *
429  * Returns     :  N/A
430  *
431  *********************************************************************/
432 static void ssl_debug_callback(void *ctx, int level, const char *file, int line, const char *str)
433 {
434    /*
435    ((void)level);
436    fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str);
437    fflush((FILE *)ctx);
438    log_error(LOG_LEVEL_INFO, "SSL debug message: %s:%04d: %s", file, line, str);
439    */
440 }
441
442
443 /*********************************************************************
444  *
445  * Function    :  create_client_ssl_connection
446  *
447  * Description :  Creates TLS/SSL secured connection with client
448  *
449  * Parameters  :
450  *          1  :  csp = Current client state (buffers, headers, etc...)
451  *
452  * Returns     :  0 on success, negative value if connection wasn't created
453  *                successfully.
454  *
455  *********************************************************************/
456 extern int create_client_ssl_connection(struct client_state *csp)
457 {
458    /* Paths to certificates file and key file */
459    char *key_file  = NULL;
460    char *ca_file   = NULL;
461    char *cert_file = NULL;
462    int ret = 0;
463    char err_buf[ERROR_BUF_SIZE];
464
465    /*
466     * Initializing mbedtls structures for TLS/SSL connection
467     */
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));
475 #endif
476
477    /*
478     * Preparing hash of host for creating certificates
479     */
480    ret = host_to_hash(csp);
481    if (ret != 0)
482    {
483       log_error(LOG_LEVEL_ERROR, "Generating hash of host failed: %d", ret);
484       ret = -1;
485       goto exit;
486    }
487
488    /*
489     * Preparing paths to certificates files and key file
490     */
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);
496
497    if (cert_file == NULL || key_file == NULL)
498    {
499       ret = -1;
500       goto exit;
501    }
502
503    /*
504     * Generating certificate for requested host. Mutex to prevent
505     * certificate and key inconsistence must be locked.
506     */
507    privoxy_mutex_lock(&certificate_mutex);
508
509    ret = generate_webpage_certificate(csp);
510    if (ret < 0)
511    {
512       log_error(LOG_LEVEL_ERROR,
513          "Generate_webpage_certificate failed: %d", ret);
514       privoxy_mutex_unlock(&certificate_mutex);
515       ret = -1;
516       goto exit;
517    }
518    privoxy_mutex_unlock(&certificate_mutex);
519
520    /*
521     * Seed the RNG
522     */
523    ret = seed_rng(csp);
524    if (ret != 0)
525    {
526       ret = -1;
527       goto exit;
528    }
529
530    /*
531     * Loading CA file, webpage certificate and key files
532     */
533    ret = mbedtls_x509_crt_parse_file(&(csp->mbedtls_client_attr.server_cert),
534       cert_file);
535    if (ret != 0)
536    {
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);
540       ret = -1;
541       goto exit;
542    }
543
544    ret = mbedtls_x509_crt_parse_file(&(csp->mbedtls_client_attr.server_cert),
545       ca_file);
546    if (ret != 0)
547    {
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);
551       ret = -1;
552       goto exit;
553    }
554
555    ret = mbedtls_pk_parse_keyfile(&(csp->mbedtls_client_attr.prim_key),
556       key_file, NULL);
557    if (ret != 0)
558    {
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",
562          key_file, err_buf);
563       ret = -1;
564       goto exit;
565    }
566
567    /*
568     * Setting SSL parameters
569     */
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);
573    if (ret != 0)
574    {
575       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
576       log_error(LOG_LEVEL_ERROR,
577          "mbedtls_ssl_config_defaults failed: %s", err_buf);
578       ret = -1;
579       goto exit;
580    }
581
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);
586
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);
591 #endif
592
593    /*
594     * Setting certificates
595     */
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));
599    if (ret != 0)
600    {
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);
604       ret = -1;
605       goto exit;
606    }
607
608    ret = mbedtls_ssl_setup(&(csp->mbedtls_client_attr.ssl),
609       &(csp->mbedtls_client_attr.conf));
610    if (ret != 0)
611    {
612       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
613       log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_setup failed: %s", err_buf);
614       ret = -1;
615       goto exit;
616    }
617
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));
622
623    /*
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.
627     */
628    csp->mbedtls_client_attr.socket_fd.fd = csp->cfd;
629
630    /*
631     *  Handshake with client
632     */
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)
637    {
638       if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
639           ret != MBEDTLS_ERR_SSL_WANT_WRITE)
640       {
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);
644          ret = -1;
645          goto exit;
646       }
647    }
648
649    log_error(LOG_LEVEL_CONNECT, "Client successfully connected over TLS/SSL");
650    csp->ssl_with_client_is_opened = 1;
651
652 exit:
653    /*
654     * Freeing allocated paths to files
655     */
656    freez(cert_file);
657    freez(key_file);
658
659    /* Freeing structures if connection wasn't created successfully */
660    if (ret < 0)
661    {
662       free_client_ssl_structures(csp);
663    }
664    return ret;
665 }
666
667
668 /*********************************************************************
669  *
670  * Function    :  close_client_ssl_connection
671  *
672  * Description :  Closes TLS/SSL connection with client. This function
673  *                checks if this connection is already created.
674  *
675  * Parameters  :
676  *          1  :  csp = Current client state (buffers, headers, etc...)
677  *
678  * Returns     :  N/A
679  *
680  *********************************************************************/
681 extern void close_client_ssl_connection(struct client_state *csp)
682 {
683    int ret = 0;
684
685    if (csp->ssl_with_client_is_opened == 0)
686    {
687       return;
688    }
689
690    /*
691     * Notifying the peer that the connection is being closed.
692     */
693    do {
694       ret = mbedtls_ssl_close_notify(&(csp->mbedtls_client_attr.ssl));
695    } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
696
697    free_client_ssl_structures(csp);
698    csp->ssl_with_client_is_opened = 0;
699 }
700
701
702 /*********************************************************************
703  *
704  * Function    :  free_client_ssl_structures
705  *
706  * Description :  Frees structures used for SSL communication with
707  *                client.
708  *
709  * Parameters  :
710  *          1  :  csp = Current client state (buffers, headers, etc...)
711  *
712  * Returns     :  N/A
713  *
714  *********************************************************************/
715 static void free_client_ssl_structures(struct client_state *csp)
716 {
717    /*
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.
722    */
723    csp->mbedtls_client_attr.socket_fd.fd = -1;
724
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));
732 #endif
733 }
734
735
736 /*********************************************************************
737  *
738  * Function    :  create_server_ssl_connection
739  *
740  * Description :  Creates TLS/SSL secured connection with server.
741  *
742  * Parameters  :
743  *          1  :  csp = Current client state (buffers, headers, etc...)
744  *
745  * Returns     :  0 on success, negative value if connection wasn't created
746  *                successfully.
747  *
748  *********************************************************************/
749 extern int create_server_ssl_connection(struct client_state *csp)
750 {
751    int ret = 0;
752    char err_buf[ERROR_BUF_SIZE];
753    char *trusted_cas_file = NULL;
754    int auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
755
756    csp->server_cert_verification_result = SSL_CERT_NOT_VERIFIED;
757    csp->server_certs_chain.next = NULL;
758
759    /* Setting path to file with trusted CAs */
760    trusted_cas_file = csp->config->trusted_cas_file;
761
762    /*
763     * Initializing mbedtls structures for TLS/SSL connection
764     */
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));
769
770    /*
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.
774    */
775    csp->mbedtls_server_attr.socket_fd.fd = csp->server_connection.sfd;
776
777    /*
778     * Seed the RNG
779     */
780    ret = seed_rng(csp);
781    if (ret != 0)
782    {
783       ret = -1;
784       goto exit;
785    }
786
787    /*
788     * Loading file with trusted CAs
789     */
790    ret = mbedtls_x509_crt_parse_file(&(csp->mbedtls_server_attr.ca_cert),
791       trusted_cas_file);
792    if (ret < 0)
793    {
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);
797       ret = -1;
798       goto exit;
799    }
800
801    /*
802     * Set TLS/SSL options
803     */
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);
808    if (ret != 0)
809    {
810       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
811       log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_config_defaults failed: %s",
812          err_buf);
813       ret = -1;
814       goto exit;
815    }
816
817    /*
818     * Setting how strict should certificate verification be and other
819     * parameters for certificate verification
820     */
821    if (csp->dont_verify_certificate)
822    {
823       auth_mode = MBEDTLS_SSL_VERIFY_NONE;
824    }
825
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);
829
830    /* Setting callback function for certificates verification */
831    mbedtls_ssl_conf_verify(&(csp->mbedtls_server_attr.conf),
832       ssl_verify_callback, (void *)csp);
833
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);
838
839    ret = mbedtls_ssl_setup(&(csp->mbedtls_server_attr.ssl),
840       &(csp->mbedtls_server_attr.conf));
841    if (ret != 0)
842    {
843       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
844       log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_setup failed: %s", err_buf);
845       ret = -1;
846       goto exit;
847    }
848
849    /*
850     * Set the hostname to check against the received server certificate
851     */
852    ret = mbedtls_ssl_set_hostname(&(csp->mbedtls_server_attr.ssl),
853       csp->http->host);
854    if (ret != 0)
855    {
856       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
857       log_error(LOG_LEVEL_ERROR, "mbedtls_ssl_set_hostname failed: %s",
858          err_buf);
859       ret = -1;
860       goto exit;
861    }
862
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);
866
867    /*
868     * Handshake with server
869     */
870    log_error(LOG_LEVEL_CONNECT,
871       "Performing the TLS/SSL handshake with the server");
872
873    while ((ret = mbedtls_ssl_handshake(&(csp->mbedtls_server_attr.ssl))) != 0)
874    {
875       if (ret != MBEDTLS_ERR_SSL_WANT_READ
876        && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
877       {
878          mbedtls_strerror(ret, err_buf, sizeof(err_buf));
879
880          if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
881          {
882             char reason[INVALID_CERT_INFO_BUF_SIZE];
883
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);
888
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);
893             ret = -1;
894          }
895          else
896          {
897             log_error(LOG_LEVEL_ERROR,
898                "mbedtls_ssl_handshake with server failed: %s", err_buf);
899             free_certificate_chain(csp);
900             ret = -1;
901          }
902          goto exit;
903       }
904    }
905
906    log_error(LOG_LEVEL_CONNECT, "Server successfully connected over TLS/SSL");
907
908    /*
909     * Server certificate chain is valid, so we can clean
910     * chain, because we will not send it to client.
911     */
912    free_certificate_chain(csp);
913
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));
917
918 exit:
919    /* Freeing structures if connection wasn't created successfully */
920    if (ret < 0)
921    {
922       free_server_ssl_structures(csp);
923    }
924
925    return ret;
926 }
927
928
929 /*********************************************************************
930  *
931  * Function    :  close_server_ssl_connection
932  *
933  * Description :  Closes TLS/SSL connection with server. This function
934  *                checks if this connection is already opened.
935  *
936  * Parameters  :
937  *          1  :  csp = Current client state (buffers, headers, etc...)
938  *
939  * Returns     :  N/A
940  *
941  *********************************************************************/
942 static void close_server_ssl_connection(struct client_state *csp)
943 {
944    int ret = 0;
945
946    if (csp->ssl_with_server_is_opened == 0)
947    {
948       return;
949    }
950
951    /*
952    * Notifying the peer that the connection is being closed.
953    */
954    do {
955       ret = mbedtls_ssl_close_notify(&(csp->mbedtls_server_attr.ssl));
956    } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
957
958    free_server_ssl_structures(csp);
959    csp->ssl_with_server_is_opened = 0;
960 }
961
962
963 /*********************************************************************
964  *
965  * Function    :  free_server_ssl_structures
966  *
967  * Description :  Frees structures used for SSL communication with server
968  *
969  * Parameters  :
970  *          1  :  csp = Current client state (buffers, headers, etc...)
971  *
972  * Returns     :  N/A
973  *
974  *********************************************************************/
975 static void free_server_ssl_structures(struct client_state *csp)
976 {
977    /*
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.
982    */
983    csp->mbedtls_server_attr.socket_fd.fd = -1;
984
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));
988 }
989
990
991 /*********************************************************************
992  *
993  * Function    :  close_client_and_server_ssl_connections
994  *
995  * Description :  Checks if client or server should use secured
996  *                connection over SSL and if so, closes all of them.
997  *
998  * Parameters  :
999  *          1  :  csp = Current client state (buffers, headers, etc...)
1000  *
1001  * Returns     :  N/A
1002  *
1003  *********************************************************************/
1004 extern void close_client_and_server_ssl_connections(struct client_state *csp)
1005 {
1006    if (client_use_ssl(csp) == 1)
1007    {
1008       close_client_ssl_connection(csp);
1009    }
1010    if (server_use_ssl(csp) == 1)
1011    {
1012       close_server_ssl_connection(csp);
1013    }
1014 }
1015
1016 /*====================== Certificates ======================*/
1017
1018 /*********************************************************************
1019  *
1020  * Function    :  write_certificate
1021  *
1022  * Description :  Writes certificate into file.
1023  *
1024  * Parameters  :
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
1029  *
1030  * Returns     :  Length of written certificate on success or negative value
1031  *                on error
1032  *
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)
1036 {
1037    FILE *f = NULL;
1038    size_t len = 0;
1039    unsigned char cert_buf[CERTIFICATE_BUF_SIZE + 1]; /* Buffer for certificate in PEM format + terminating NULL */
1040    int ret = 0;
1041    char err_buf[ERROR_BUF_SIZE];
1042
1043    memset(cert_buf, 0, sizeof(cert_buf));
1044
1045    /*
1046     * Writing certificate into PEM string. If buffer is too small, function
1047     * returns specific error and no buffer overflow can happen.
1048     */
1049    if ((ret = mbedtls_x509write_crt_pem(crt, cert_buf,
1050       sizeof(cert_buf) - 1, f_rng, p_rng)) != 0)
1051    {
1052       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1053       log_error(LOG_LEVEL_ERROR,
1054          "Writing certificate into buffer failed: %s", err_buf);
1055       return -1;
1056    }
1057
1058    len = strlen((char *)cert_buf);
1059
1060    /*
1061     * Saving certificate into file
1062     */
1063    if ((f = fopen(output_file, "w")) == NULL)
1064    {
1065       log_error(LOG_LEVEL_ERROR, "Opening file %s to save certificate failed",
1066          output_file);
1067       return -1;
1068    }
1069
1070    if (fwrite(cert_buf, 1, len, f) != len)
1071    {
1072       log_error(LOG_LEVEL_ERROR,
1073          "Writing certificate into file %s failed", output_file);
1074       fclose(f);
1075       return -1;
1076    }
1077
1078    fclose(f);
1079
1080    return (int)len;
1081 }
1082
1083
1084 /*********************************************************************
1085  *
1086  * Function    :  write_private_key
1087  *
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
1091  *                caller.
1092  *
1093  * Parameters  :
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
1097  *
1098  * Returns     :  Length of written private key on success or negative value
1099  *                on error
1100  *
1101  *********************************************************************/
1102 static int write_private_key(mbedtls_pk_context *key, unsigned char **ret_buf,
1103    const char *key_file_path)
1104 {
1105    size_t len = 0;                /* Length of created key    */
1106    FILE *f = NULL;                /* File to save certificate */
1107    int ret = 0;
1108    char err_buf[ERROR_BUF_SIZE];
1109
1110    /* Initializing buffer for key file content */
1111    *ret_buf = zalloc_or_die(PRIVATE_KEY_BUF_SIZE + 1);
1112
1113    /*
1114     * Writing private key into PEM string
1115     */
1116    if ((ret = mbedtls_pk_write_key_pem(key, *ret_buf, PRIVATE_KEY_BUF_SIZE)) != 0)
1117    {
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);
1121       ret = -1;
1122       goto exit;
1123    }
1124    len = strlen((char *)*ret_buf);
1125
1126    /*
1127     * Saving key into file
1128     */
1129    if ((f = fopen(key_file_path, "wb")) == NULL)
1130    {
1131       log_error(LOG_LEVEL_ERROR,
1132          "Opening file %s to save private key failed: %E",
1133          key_file_path);
1134       ret = -1;
1135       goto exit;
1136    }
1137
1138    if (fwrite(*ret_buf, 1, len, f) != len)
1139    {
1140       fclose(f);
1141       log_error(LOG_LEVEL_ERROR,
1142          "Writing private key into file %s failed",
1143          key_file_path);
1144       ret = -1;
1145       goto exit;
1146    }
1147
1148    fclose(f);
1149
1150 exit:
1151    if (ret < 0)
1152    {
1153       freez(*ret_buf);
1154       *ret_buf = NULL;
1155       return ret;
1156    }
1157    return (int)len;
1158 }
1159
1160
1161 /*********************************************************************
1162  *
1163  * Function    :  generate_key
1164  *
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.
1171  *
1172  * Parameters  :
1173  *          1  :  csp = Current client state (buffers, headers, etc...)
1174  *          2  :  key_buf = buffer to save new generated key
1175  *
1176  * Returns     :  -1 => Error while generating private key
1177  *                 0 => Key already exists
1178  *                >0 => Length of generated private key
1179  *
1180  *********************************************************************/
1181 static int generate_key(struct client_state *csp, unsigned char **key_buf)
1182 {
1183    mbedtls_pk_context key;
1184    key_options key_opt;
1185    int ret = 0;
1186    char err_buf[ERROR_BUF_SIZE];
1187
1188    key_opt.key_file_path = NULL;
1189
1190    /*
1191     * Initializing structures for key generating
1192     */
1193    mbedtls_pk_init(&key);
1194
1195    /*
1196     * Preparing path for key file and other properties for generating key
1197     */
1198    key_opt.type        = MBEDTLS_PK_RSA;
1199    key_opt.rsa_keysize = RSA_KEYSIZE;
1200
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)
1204    {
1205       ret = -1;
1206       goto exit;
1207    }
1208
1209    /*
1210     * Test if key already exists. If so, we don't have to create it again.
1211     */
1212    if (file_exists(key_opt.key_file_path) == 1)
1213    {
1214       ret = 0;
1215       goto exit;
1216    }
1217
1218    /*
1219     * Seed the RNG
1220     */
1221    ret = seed_rng(csp);
1222    if (ret != 0)
1223    {
1224       ret = -1;
1225       goto exit;
1226    }
1227
1228    /*
1229     * Setting attributes of private key and generating it
1230     */
1231    if ((ret = mbedtls_pk_setup(&key,
1232       mbedtls_pk_info_from_type(key_opt.type))) != 0)
1233    {
1234       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1235       log_error(LOG_LEVEL_ERROR, "mbedtls_pk_setup failed: %s", err_buf);
1236       ret = -1;
1237       goto exit;
1238    }
1239
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);
1242    if (ret != 0)
1243    {
1244       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1245       log_error(LOG_LEVEL_ERROR, "Key generating failed: %s", err_buf);
1246       ret = -1;
1247       goto exit;
1248    }
1249
1250    /*
1251     * Exporting private key into file
1252     */
1253    if ((ret = write_private_key(&key, key_buf, key_opt.key_file_path)) < 0)
1254    {
1255       log_error(LOG_LEVEL_ERROR,
1256          "Writing private key into file %s failed", key_opt.key_file_path);
1257       ret = -1;
1258       goto exit;
1259    }
1260
1261 exit:
1262    /*
1263     * Freeing used variables
1264     */
1265    freez(key_opt.key_file_path);
1266
1267    mbedtls_pk_free(&key);
1268
1269    return ret;
1270 }
1271
1272
1273 /*********************************************************************
1274  *
1275  * Function    :  ssl_certificate_is_invalid
1276  *
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.
1280  *
1281  * Parameters  :
1282  *          1  :  cert_file = The certificate to check
1283  *
1284  * Returns     :   0 => The certificate is valid.
1285  *                 1 => The certificate is invalid
1286  *
1287  *********************************************************************/
1288 static int ssl_certificate_is_invalid(const char *cert_file)
1289 {
1290    mbedtls_x509_crt cert;
1291    int ret;
1292
1293    mbedtls_x509_crt_init(&cert);
1294
1295    ret = mbedtls_x509_crt_parse_file(&cert, cert_file);
1296    if (ret != 0)
1297    {
1298       char err_buf[ERROR_BUF_SIZE];
1299
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);
1305
1306       return 1;
1307    }
1308    if (mbedtls_x509_time_is_past(&cert.valid_to))
1309    {
1310       mbedtls_x509_crt_free(&cert);
1311
1312       return 1;
1313    }
1314
1315    mbedtls_x509_crt_free(&cert);
1316
1317    return 0;
1318
1319 }
1320
1321
1322 /*********************************************************************
1323  *
1324  * Function    :  generate_certificate_valid_date
1325  *
1326  * Description :  Turns a time_t into the format expected by mbedTLS.
1327  *
1328  * Parameters  :
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
1332  *
1333  * Returns     :   0 => The conversion worked
1334  *                 1 => The conversion failed
1335  *
1336  *********************************************************************/
1337 static int generate_certificate_valid_date(time_t time_spec, char *buffer,
1338                                            size_t buffer_size)
1339 {
1340    struct tm valid_date;
1341    struct tm *timeptr;
1342    size_t ret;
1343
1344    timeptr = privoxy_gmtime_r(&time_spec, &valid_date);
1345    if (NULL == timeptr)
1346    {
1347       return 1;
1348    }
1349
1350    ret = strftime(buffer, buffer_size, "%Y%m%d%H%M%S", timeptr);
1351    if (ret != 14)
1352    {
1353       return 1;
1354    }
1355
1356    return 0;
1357
1358 }
1359
1360
1361 /*********************************************************************
1362  *
1363  * Function    :  get_certificate_valid_from_date
1364  *
1365  * Description :  Generates a "valid from" date in the format
1366  *                expected by mbedTLS.
1367  *
1368  * Parameters  :
1369  *          1  :  buffer = The buffer to write the date to
1370  *          2  :  buffer_size = The size of the buffer
1371  *
1372  * Returns     :   0 => The generation worked
1373  *                 1 => The generation failed
1374  *
1375  *********************************************************************/
1376 static int get_certificate_valid_from_date(char *buffer, size_t buffer_size)
1377 {
1378    time_t time_spec;
1379
1380    time_spec = time(NULL);
1381    /* 1 month in the past */
1382    time_spec -= 30 * 24 * 60 * 60;
1383
1384    return generate_certificate_valid_date(time_spec, buffer, buffer_size);
1385
1386 }
1387
1388
1389 /*********************************************************************
1390  *
1391  * Function    :  get_certificate_valid_to_date
1392  *
1393  * Description :  Generates a "valid to" date in the format
1394  *                expected by mbedTLS.
1395  *
1396  * Parameters  :
1397  *          1  :  buffer = The buffer to write the date to
1398  *          2  :  buffer_size = The size of the buffer
1399  *
1400  * Returns     :   0 => The generation worked
1401  *                 1 => The generation failed
1402  *
1403  *********************************************************************/
1404 static int get_certificate_valid_to_date(char *buffer, size_t buffer_size)
1405 {
1406    time_t time_spec;
1407
1408    time_spec = time(NULL);
1409    /* Three months in the future */
1410    time_spec += 90 * 24 * 60 * 60;
1411
1412    return generate_certificate_valid_date(time_spec, buffer, buffer_size);
1413
1414 }
1415
1416
1417 /*********************************************************************
1418  *
1419  * Function    :  set_subject_alternative_name
1420  *
1421  * Description :  Sets the Subject Alternative Name extension to a cert
1422  *
1423  * Parameters  :
1424  *          1  :  cert = The certificate to modify
1425  *          2  :  hostname = The hostname to add
1426  *
1427  * Returns     :  <0 => Error while creating certificate.
1428  *                 0 => It worked
1429  *
1430  *********************************************************************/
1431 static int set_subject_alternative_name(mbedtls_x509write_cert *cert, const char *hostname)
1432 {
1433    char err_buf[ERROR_BUF_SIZE];
1434    int ret;
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];
1439    unsigned char *c;
1440    int len;
1441
1442    subject_alternative_name_len = strlen(hostname) + 1;
1443    subject_alternative_name = zalloc_or_die(subject_alternative_name_len);
1444
1445    strlcpy(subject_alternative_name, hostname, subject_alternative_name_len);
1446
1447    memset(san_buf, 0, sizeof(san_buf));
1448
1449    c = san_buf + sizeof(san_buf);
1450    len = 0;
1451
1452    ret = mbedtls_asn1_write_raw_buffer(&c, san_buf,
1453       (const unsigned char *)subject_alternative_name,
1454       strlen(subject_alternative_name));
1455    if (ret < 0)
1456    {
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);
1460       goto exit;
1461    }
1462    len += ret;
1463
1464    ret = mbedtls_asn1_write_len(&c, san_buf, strlen(subject_alternative_name));
1465    if (ret < 0)
1466    {
1467       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1468       log_error(LOG_LEVEL_ERROR,
1469          "mbedtls_asn1_write_len() failed: %s", err_buf);
1470       goto exit;
1471    }
1472    len += ret;
1473
1474    ret = mbedtls_asn1_write_tag(&c, san_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2);
1475    if (ret < 0)
1476    {
1477       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1478       log_error(LOG_LEVEL_ERROR,
1479          "mbedtls_asn1_write_tag() failed: %s", err_buf);
1480       goto exit;
1481    }
1482    len += ret;
1483
1484    ret = mbedtls_asn1_write_len(&c, san_buf, (size_t)len);
1485    if (ret < 0)
1486    {
1487       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1488       log_error(LOG_LEVEL_ERROR,
1489          "mbedtls_asn1_write_len() failed: %s", err_buf);
1490       goto exit;
1491    }
1492    len += ret;
1493
1494    ret = mbedtls_asn1_write_tag(&c, san_buf,
1495       MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
1496    if (ret < 0)
1497    {
1498       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1499       log_error(LOG_LEVEL_ERROR,
1500          "mbedtls_asn1_write_tag() failed: %s", err_buf);
1501       goto exit;
1502    }
1503    len += ret;
1504
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);
1509    if (ret < 0)
1510    {
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);
1514    }
1515
1516 exit:
1517    freez(subject_alternative_name);
1518
1519    return ret;
1520
1521 }
1522
1523
1524 /*********************************************************************
1525  *
1526  * Function    :  host_is_ip_address
1527  *
1528  * Description :  Checks whether or not a host is specified by
1529  *                IP address. Does not actually validate the
1530  *                address.
1531  *
1532  * Parameters  :
1533  *          1  :  host = The host name to check
1534  *
1535  * Returns     :   1 => Yes
1536  *                 0 => No
1537  *
1538  *********************************************************************/
1539 static int host_is_ip_address(const char *host)
1540 {
1541    const char *p;
1542
1543    if (NULL != strstr(host, ":"))
1544    {
1545       /* Assume an IPv6 address. */
1546       return 1;
1547    }
1548
1549    for (p = host; *p; p++)
1550    {
1551       if ((*p != '.') && !privoxy_isdigit(*p))
1552       {
1553          /* Not a dot or digit so it can't be an IPv4 address. */
1554          return 0;
1555       }
1556    }
1557
1558    /*
1559     * Host only consists of dots and digits so
1560     * assume that is an IPv4 address.
1561     */
1562    return 1;
1563
1564 }
1565
1566
1567 /*********************************************************************
1568  *
1569  * Function    :  generate_webpage_certificate
1570  *
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.
1576  *
1577  * Parameters  :
1578  *          1  :  csp = Current client state (buffers, headers, etc...)
1579  *
1580  * Returns     :  -1 => Error while creating certificate.
1581  *                 0 => Certificate already exists.
1582  *                >0 => Length of created certificate.
1583  *
1584  *********************************************************************/
1585 static int generate_webpage_certificate(struct client_state *csp)
1586 {
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;
1592    mbedtls_mpi serial;
1593
1594    unsigned char *key_buf = NULL;    /* Buffer for created key */
1595
1596    int ret = 0;
1597    char err_buf[ERROR_BUF_SIZE];
1598    cert_options cert_opt;
1599    char cert_valid_from[15];
1600    char cert_valid_to[15];
1601
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;
1606
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)
1610    {
1611       return -1;
1612    }
1613
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)
1617    {
1618       freez(cert_opt.output_file);
1619       return -1;
1620    }
1621
1622    if (file_exists(cert_opt.output_file) == 1)
1623    {
1624       /* The file exists, but is it valid? */
1625       if (ssl_certificate_is_invalid(cert_opt.output_file))
1626       {
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))
1631          {
1632             log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1633                cert_opt.output_file);
1634
1635             freez(cert_opt.output_file);
1636             freez(cert_opt.subject_key);
1637
1638             return -1;
1639          }
1640          if (unlink(cert_opt.subject_key))
1641          {
1642             log_error(LOG_LEVEL_ERROR, "Failed to unlink %s: %E",
1643                cert_opt.subject_key);
1644
1645             freez(cert_opt.output_file);
1646             freez(cert_opt.subject_key);
1647
1648             return -1;
1649          }
1650       }
1651       else
1652       {
1653          freez(cert_opt.output_file);
1654          freez(cert_opt.subject_key);
1655
1656          return 0;
1657       }
1658    }
1659
1660    /*
1661     * Create key for requested host
1662     */
1663    int subject_key_len = generate_key(csp, &key_buf);
1664    if (subject_key_len < 0)
1665    {
1666       freez(cert_opt.output_file);
1667       freez(cert_opt.subject_key);
1668       log_error(LOG_LEVEL_ERROR, "Key generating failed");
1669       return -1;
1670    }
1671
1672    /*
1673     * Initializing structures for certificate generating
1674     */
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);
1681
1682    /*
1683     * Presetting parameters for certificate. We must compute total length
1684     * of parameters.
1685     */
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);
1692
1693    /*
1694     * Converting unsigned long serial number to char * serial number.
1695     * We must compute length of serial number in string + terminating null.
1696     */
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)
1702    {
1703       serial_num_size = 1;
1704    }
1705
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)
1710    {
1711       log_error(LOG_LEVEL_ERROR,
1712          "Converting certificate serial number into string failed");
1713       ret = -1;
1714       goto exit;
1715    }
1716
1717    /*
1718     * Preparing parameters for certificate
1719     */
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);
1727
1728    cert_opt.issuer_crt = csp->config->ca_cert_file;
1729    cert_opt.issuer_key = csp->config->ca_key_file;
1730
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)))
1733    {
1734       log_error(LOG_LEVEL_ERROR, "Generating one of the validity dates failed");
1735       ret = -1;
1736       goto exit;
1737    }
1738
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;
1745    cert_opt.is_ca         = 0;
1746    cert_opt.max_pathlen   = -1;
1747
1748    /*
1749     * Test if the private key was already created.
1750     * XXX: Can this still happen?
1751     */
1752    if (subject_key_len == 0)
1753    {
1754       log_error(LOG_LEVEL_ERROR, "Subject key was already created");
1755       ret = 0;
1756       goto exit;
1757    }
1758
1759    /*
1760     * Seed the PRNG
1761     */
1762    ret = seed_rng(csp);
1763    if (ret != 0)
1764    {
1765       ret = -1;
1766       goto exit;
1767    }
1768
1769    /*
1770     * Parse serial to MPI
1771     */
1772    ret = mbedtls_mpi_read_string(&serial, 10, cert_opt.serial);
1773    if (ret != 0)
1774    {
1775       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1776       log_error(LOG_LEVEL_ERROR,
1777          "mbedtls_mpi_read_string failed: %s", err_buf);
1778       ret = -1;
1779       goto exit;
1780    }
1781
1782    /*
1783     * Loading certificates
1784     */
1785    ret = mbedtls_x509_crt_parse_file(&issuer_cert, cert_opt.issuer_crt);
1786    if (ret != 0)
1787    {
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);
1791       ret = -1;
1792       goto exit;
1793    }
1794
1795    ret = mbedtls_x509_dn_gets(cert_opt.issuer_name,
1796       sizeof(cert_opt.issuer_name), &issuer_cert.subject);
1797    if (ret < 0)
1798    {
1799       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
1800       log_error(LOG_LEVEL_ERROR, "mbedtls_x509_dn_gets failed: %s", err_buf);
1801       ret = -1;
1802       goto exit;
1803    }
1804
1805    /*
1806     * Loading keys from file or from buffer
1807     */
1808    if (key_buf != NULL && subject_key_len > 0)
1809    {
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));
1814    }
1815    else
1816    {
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);
1820    }
1821
1822    if (ret != 0)
1823    {
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);
1827       ret = -1;
1828       goto exit;
1829    }
1830
1831    ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, cert_opt.issuer_key,
1832       cert_opt.issuer_pwd);
1833    if (ret != 0)
1834    {
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);
1838       ret = -1;
1839       goto exit;
1840    }
1841
1842    /*
1843     * Check if key and issuer certificate match
1844     */
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)
1850    {
1851       log_error(LOG_LEVEL_ERROR,
1852          "Issuer key doesn't match issuer certificate");
1853       ret = -1;
1854       goto exit;
1855    }
1856
1857    mbedtls_x509write_crt_set_subject_key(&cert, subject_key);
1858    mbedtls_x509write_crt_set_issuer_key(&cert, issuer_key);
1859
1860    /*
1861     * Setting parameters of signed certificate
1862     */
1863    ret = mbedtls_x509write_crt_set_subject_name(&cert, cert_opt.subject_name);
1864    if (ret != 0)
1865    {
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);
1869       ret = -1;
1870       goto exit;
1871    }
1872
1873    ret = mbedtls_x509write_crt_set_issuer_name(&cert, cert_opt.issuer_name);
1874    if (ret != 0)
1875    {
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);
1879       ret = -1;
1880       goto exit;
1881    }
1882
1883    ret = mbedtls_x509write_crt_set_serial(&cert, &serial);
1884    if (ret != 0)
1885    {
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);
1889       ret = -1;
1890       goto exit;
1891    }
1892
1893    ret = mbedtls_x509write_crt_set_validity(&cert, cert_opt.not_before,
1894       cert_opt.not_after);
1895    if (ret != 0)
1896    {
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);
1900       ret = -1;
1901       goto exit;
1902    }
1903
1904    /*
1905     * Setting the basicConstraints extension for certificate
1906     */
1907    ret = mbedtls_x509write_crt_set_basic_constraints(&cert, cert_opt.is_ca,
1908       cert_opt.max_pathlen);
1909    if (ret != 0)
1910    {
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);
1914       ret = -1;
1915       goto exit;
1916    }
1917
1918 #if defined(MBEDTLS_SHA1_C)
1919    /* Setting the subjectKeyIdentifier extension for certificate */
1920    ret = mbedtls_x509write_crt_set_subject_key_identifier(&cert);
1921    if (ret != 0)
1922    {
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);
1926       ret = -1;
1927       goto exit;
1928    }
1929
1930    /* Setting the authorityKeyIdentifier extension for certificate */
1931    ret = mbedtls_x509write_crt_set_authority_key_identifier(&cert);
1932    if (ret != 0)
1933    {
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);
1937       ret = -1;
1938       goto exit;
1939    }
1940 #endif /* MBEDTLS_SHA1_C */
1941
1942    if (!host_is_ip_address(csp->http->host) &&
1943       set_subject_alternative_name(&cert, csp->http->host))
1944    {
1945       /* Errors are already logged by set_subject_alternative_name() */
1946       ret = -1;
1947       goto exit;
1948    }
1949
1950    /*
1951     * Writing certificate into file
1952     */
1953    ret = write_certificate(&cert, cert_opt.output_file,
1954       mbedtls_ctr_drbg_random, &ctr_drbg);
1955    if (ret < 0)
1956    {
1957       log_error(LOG_LEVEL_ERROR, "Writing certificate into file failed");
1958       goto exit;
1959    }
1960
1961 exit:
1962    /*
1963     * Freeing used structures
1964     */
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);
1970
1971    freez(cert_opt.subject_key);
1972    freez(cert_opt.output_file);
1973    freez(key_buf);
1974
1975    return ret;
1976 }
1977
1978
1979 /*********************************************************************
1980  *
1981  * Function    :  make_certs_path
1982  *
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.
1988  *
1989  * Parameters  :
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.
1994  *
1995  * Returns     :  path => Path was built up successfully
1996  *                NULL => Path can't be built up
1997  *
1998  *********************************************************************/
1999 static char *make_certs_path(const char *conf_dir, const char *file_name,
2000    const char *suffix)
2001 {
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')
2005    {
2006       log_error(LOG_LEVEL_ERROR,
2007          "make_certs_path failed: bad input parameters");
2008       return NULL;
2009    }
2010
2011    char *path = NULL;
2012    size_t path_size = strlen(conf_dir)
2013       + strlen(file_name) + strlen(suffix) + 2;
2014
2015    /* Setting delimiter and editing path length */
2016 #if defined(_WIN32) || defined(__OS2__)
2017    char delim[] = "\\";
2018    path_size += 1;
2019 #else /* ifndef _WIN32 || __OS2__ */
2020    char delim[] = "/";
2021 #endif /* ifndef _WIN32 || __OS2__ */
2022
2023    /*
2024     * Building up path from many parts
2025     */
2026 #if defined(unix)
2027    if (*conf_dir != '/' && basedir && *basedir)
2028    {
2029       /*
2030        * Replacing conf_dir with basedir. This new variable contains
2031        * absolute path to cwd.
2032        */
2033       path_size += strlen(basedir) + 2;
2034       path = zalloc_or_die(path_size);
2035
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);
2042    }
2043    else
2044 #endif /* defined unix */
2045    {
2046       path = zalloc_or_die(path_size);
2047
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);
2052    }
2053
2054    return path;
2055 }
2056
2057
2058 /*********************************************************************
2059  *
2060  * Function    :  get_certificate_serial
2061  *
2062  * Description :  Computes serial number for new certificate from host
2063  *                name hash. This hash must be already saved in csp
2064  *                structure.
2065  *
2066  * Parameters  :
2067  *          1  :  csp = Current client state (buffers, headers, etc...)
2068  *
2069  * Returns     :  Serial number for new certificate
2070  *
2071  *********************************************************************/
2072 static unsigned long get_certificate_serial(struct client_state *csp)
2073 {
2074    unsigned long exp    = 1;
2075    unsigned long serial = 0;
2076
2077    int i = CERT_SERIAL_NUM_LENGTH;
2078
2079    for (; i >= 0; i--)
2080    {
2081       serial += exp * (unsigned)csp->http->hash_of_host[i];
2082       exp *= 256;
2083    }
2084    return serial;
2085 }
2086
2087
2088 /*********************************************************************
2089  *
2090  * Function    :  ssl_send_certificate_error
2091  *
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.
2095  *
2096  * Parameters  :
2097  *          1  :  csp = Current client state (buffers, headers, etc...)
2098  *
2099  * Returns     :  N/A
2100  *
2101  *********************************************************************/
2102 extern void ssl_send_certificate_error(struct client_state *csp)
2103 {
2104    size_t message_len = 0;
2105    int ret = 0;
2106    struct certs_chain *cert = NULL;
2107
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"
2113       "<!DOCTYPE html>\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>"
2118       "<p>Reason: ";
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));
2122
2123    /* Get verification message from verification return code */
2124    mbedtls_x509_crt_verify_info(reason, sizeof(reason), " ",
2125       csp->server_cert_verification_result);
2126
2127    /*
2128     * Computing total length of message with all certificates inside
2129     */
2130    message_len = strlen(message_begin) + strlen(message_end)
2131                  + strlen(reason) + strlen("</p>") + 1;
2132
2133    cert = &(csp->server_certs_chain);
2134    while (cert->next != NULL)
2135    {
2136       size_t base64_len = 4 * ((strlen(cert->file_buf) + 2) / 3) + 1;
2137
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>");
2141       cert = cert->next;
2142    }
2143
2144    /*
2145     * Joining all blocks in one long message
2146     */
2147    char message[message_len];
2148    memset(message, 0, message_len);
2149
2150    strlcpy(message, message_begin, message_len);
2151    strlcat(message, reason       , message_len);
2152    strlcat(message, "</p>"       , message_len);
2153
2154    cert = &(csp->server_certs_chain);
2155    while (cert->next != NULL)
2156    {
2157       size_t olen = 0;
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);
2161
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));
2166       if (ret != 0)
2167       {
2168          log_error(LOG_LEVEL_ERROR,
2169             "Encoding to base64 failed, buffer is to small");
2170       }
2171
2172       strlcat(message, "<pre>",        message_len);
2173       strlcat(message, cert->info_buf, message_len);
2174       strlcat(message, "</pre>\n",     message_len);
2175
2176       if (ret == 0)
2177       {
2178          strlcat(message, "<a href=\"data:application/x-x509-ca-cert;base64,",
2179             message_len);
2180          strlcat(message, base64_buf, message_len);
2181          strlcat(message, "\">Download certificate</a>", message_len);
2182       }
2183
2184       cert = cert->next;
2185    }
2186    strlcat(message, message_end, message_len);
2187
2188    /*
2189     * Sending final message to client
2190     */
2191    ssl_send_data(&(csp->mbedtls_client_attr.ssl),
2192       (const unsigned char *)message, strlen(message));
2193
2194    free_certificate_chain(csp);
2195 }
2196
2197
2198 /*********************************************************************
2199  *
2200  * Function    :  ssl_verify_callback
2201  *
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.
2207  *
2208  * Parameters  :
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
2213  *
2214  * Returns     :  0 on success and negative value on error
2215  *
2216  *********************************************************************/
2217 static int ssl_verify_callback(void *csp_void, mbedtls_x509_crt *crt,
2218    int depth, uint32_t *flags)
2219 {
2220    struct client_state *csp  = (struct client_state *)csp_void;
2221    struct certs_chain  *last = &(csp->server_certs_chain);
2222    size_t olen = 0;
2223    int ret = 0;
2224
2225    /*
2226     * Searching for last item in certificates linked list
2227     */
2228    while (last->next != NULL)
2229    {
2230       last = last->next;
2231    }
2232
2233    /*
2234     * Preparing next item in linked list for next certificate
2235     */
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));
2240
2241    /*
2242     * Saving certificate file into buffer
2243     */
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)
2247    {
2248       char err_buf[ERROR_BUF_SIZE];
2249
2250       mbedtls_strerror(ret, err_buf, sizeof(err_buf));
2251       log_error(LOG_LEVEL_ERROR, "mbedtls_pem_write_buffer() failed: %s",
2252          err_buf);
2253
2254       return(ret);
2255    }
2256
2257    /*
2258     * Saving certificate information into buffer
2259     */
2260    {
2261       char buf[CERT_INFO_BUF_SIZE];
2262       char *encoded_text;
2263
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);
2268    }
2269
2270    return 0;
2271 }
2272
2273
2274 /*********************************************************************
2275  *
2276  * Function    :  free_certificate_chain
2277  *
2278  * Description :  Frees certificates linked list. This linked list is
2279  *                used to save information about certificates in
2280  *                trusted chain.
2281  *
2282  * Parameters  :
2283  *          1  :  csp = Current client state (buffers, headers, etc...)
2284  *
2285  * Returns     :  N/A
2286  *
2287  *********************************************************************/
2288 static void free_certificate_chain(struct client_state *csp)
2289 {
2290    struct certs_chain *cert = csp->server_certs_chain.next;
2291
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;
2298
2299    /* Freeing memory in whole linked list */
2300    while (cert != NULL)
2301    {
2302       struct certs_chain *cert_for_free = cert;
2303       cert = cert->next;
2304       freez(cert_for_free);
2305    }
2306 }
2307
2308
2309 /*********************************************************************
2310  *
2311  * Function    :  file_exists
2312  *
2313  * Description :  Tests if file exists and is readable.
2314  *
2315  * Parameters  :
2316  *          1  :  path = Path to tested file.
2317  *
2318  * Returns     :  1 => File exists and is readable.
2319  *                0 => File doesn't exist or is not readable.
2320  *
2321  *********************************************************************/
2322 static int file_exists(const char *path)
2323 {
2324    FILE *f;
2325    if ((f = fopen(path, "r")) != NULL)
2326    {
2327       fclose(f);
2328       return 1;
2329    }
2330
2331    return 0;
2332 }
2333
2334
2335 /*********************************************************************
2336  *
2337  * Function    :  host_to_hash
2338  *
2339  * Description :  Creates MD5 hash from host name. Host name is loaded
2340  *                from structure csp and saved again into it.
2341  *
2342  * Parameters  :
2343  *          1  :  csp = Current client state (buffers, headers, etc...)
2344  *
2345  * Returns     :  1 => Error while creating hash
2346  *                0 => Hash created successfully
2347  *
2348  *********************************************************************/
2349 static int host_to_hash(struct client_state *csp)
2350 {
2351    int ret = 0;
2352
2353 #if !defined(MBEDTLS_MD5_C)
2354 #error mbedTLS needs to be compiled with md5 support
2355 #else
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);
2359
2360    /* Converting hash into string with hex */
2361    size_t i = 0;
2362    for (; i < 16; i++)
2363    {
2364       if ((ret = sprintf((char *)csp->http->hash_of_host_hex + 2 * i, "%02x",
2365          csp->http->hash_of_host[i])) < 0)
2366       {
2367          log_error(LOG_LEVEL_ERROR, "Sprintf return value: %d", ret);
2368          return -1;
2369       }
2370    }
2371
2372    return 0;
2373 #endif /* MBEDTLS_MD5_C */
2374 }
2375
2376
2377 /*********************************************************************
2378  *
2379  * Function    :  tunnel_established_successfully
2380  *
2381  * Description :  Check if parent proxy server response contains
2382  *                information about successfully created connection with
2383  *                destination server. (HTTP/... 2xx ...)
2384  *
2385  * Parameters  :
2386  *          1  :  server_response = Buffer with parent proxy server response
2387  *          2  :  response_len = Length of server_response
2388  *
2389  * Returns     :  1 => Connection created successfully
2390  *                0 => Connection wasn't created successfully
2391  *
2392  *********************************************************************/
2393 extern int tunnel_established_successfully(const char *server_response,
2394    unsigned int response_len)
2395 {
2396    unsigned int pos = 0;
2397
2398    if (server_response == NULL)
2399    {
2400       return 0;
2401    }
2402
2403    /* Tests if "HTTP/" string is at the begin of received response */
2404    if (strncmp(server_response, "HTTP/", 5) != 0)
2405    {
2406       return 0;
2407    }
2408
2409    for (pos = 0; pos < response_len; pos++)
2410    {
2411       if (server_response[pos] == ' ')
2412       {
2413          break;
2414       }
2415    }
2416
2417    /*
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'
2421     *             pos = |
2422     */
2423    if (pos >= (response_len - 3))
2424    {
2425       return 0;
2426    }
2427
2428    /* Test HTTP status code */
2429    if (server_response[pos + 1] != '2')
2430    {
2431       return 0;
2432    }
2433
2434    return 1;
2435 }
2436
2437
2438 /*********************************************************************
2439  *
2440  * Function    :  seed_rng
2441  *
2442  * Description :  Seeding the RNG for all SSL uses
2443  *
2444  * Parameters  :
2445  *          1  :  csp = Current client state (buffers, headers, etc...)
2446  *
2447  * Returns     : -1 => RNG wasn't seed successfully
2448  *                0 => RNG is seeded successfully
2449  *
2450  *********************************************************************/
2451 static int seed_rng(struct client_state *csp)
2452 {
2453    int ret = 0;
2454    char err_buf[ERROR_BUF_SIZE];
2455
2456    if (rng_seeded == 0)
2457    {
2458       privoxy_mutex_lock(&rng_mutex);
2459       if (rng_seeded == 0)
2460       {
2461          mbedtls_ctr_drbg_init(&ctr_drbg);
2462          mbedtls_entropy_init(&entropy);
2463          ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
2464             &entropy, NULL, 0);
2465          if (ret != 0)
2466          {
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);
2471             return -1;
2472          }
2473          rng_seeded = 1;
2474       }
2475       privoxy_mutex_unlock(&rng_mutex);
2476    }
2477    return 0;
2478 }