source: mod_gnutls/include/mod_gnutls.h.in @ c005645

debian/masterdebian/stretch-backportsupstream
Last change on this file since c005645 was c005645, checked in by Thomas Klute <thomas2.klute@…>, 3 years ago

Mutex for DBM cache access

I noticed that with a DBM cache enabled and session tickets disabled
even a handful of parallel connections trashed the cache database, and
consequently the error log. According to comments and documentation on
mod_socache_dbm the APR DBM module is not thread or multi-process
safe, so a global mutex is necessary. With the mutex the cache
corruption disappears in my benchmarks.

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/**
2 *  Copyright 2004-2005 Paul Querna
3 *  Copyright 2014 Nikos Mavrogiannopoulos
4 *  Copyright 2015-2016 Thomas Klute
5 *
6 *  Licensed under the Apache License, Version 2.0 (the "License");
7 *  you may not use this file except in compliance with the License.
8 *  You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 *  Unless required by applicable law or agreed to in writing, software
13 *  distributed under the License is distributed on an "AS IS" BASIS,
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 *
18 */
19
20/* Apache Runtime Headers */
21#include "httpd.h"
22#include "http_config.h"
23#include "http_protocol.h"
24#include "http_connection.h"
25#include "http_request.h"
26#include "http_core.h"
27#include "http_log.h"
28#include "apr_buckets.h"
29#include "apr_strings.h"
30#include "apr_tables.h"
31#include "ap_release.h"
32#include "apr_fnmatch.h"
33/* GnuTLS Library Headers */
34#include <gnutls/gnutls.h>
35#include <gnutls/abstract.h>
36#include <gnutls/openpgp.h>
37#include <gnutls/x509.h>
38
39#ifndef __mod_gnutls_h_inc
40#define __mod_gnutls_h_inc
41
42#define HAVE_APR_MEMCACHE    @have_apr_memcache@
43
44extern module AP_MODULE_DECLARE_DATA gnutls_module;
45
46/* IO Filter names */
47#define GNUTLS_OUTPUT_FILTER_NAME "gnutls_output_filter"
48#define GNUTLS_INPUT_FILTER_NAME "gnutls_input_filter"
49/* GnuTLS Constants */
50#define GNUTLS_ENABLED_FALSE 0
51#define GNUTLS_ENABLED_TRUE  1
52#define GNUTLS_ENABLED_UNSET  2
53/* Current module version */
54#define MOD_GNUTLS_VERSION "@MOD_GNUTLS_VERSION@"
55
56/* Module Debug Mode */
57#define MOD_GNUTLS_DEBUG @OOO_MAINTAIN@
58
59/* mod_gnutls Cache Types */
60typedef enum {
61        /* No Cache */
62    mgs_cache_none,
63        /* Use Old Berkley DB */
64    mgs_cache_dbm,
65        /* Use Gnu's version of Berkley DB */
66    mgs_cache_gdbm,
67#if HAVE_APR_MEMCACHE
68        /* Use Memcache */
69    mgs_cache_memcache,
70#endif
71    mgs_cache_unset
72} mgs_cache_e;
73
74typedef enum {
75    mgs_cvm_unset,
76    mgs_cvm_cartel,
77    mgs_cvm_msva
78} mgs_client_verification_method_e;
79
80
81/* Directory Configuration Record */
82typedef struct {
83    int client_verify_mode;
84    const char* lua_bytecode;
85    apr_size_t lua_bytecode_len;
86} mgs_dirconf_rec;
87
88
89/* The maximum number of certificates to send in a chain */
90#define MAX_CHAIN_SIZE 8
91/* The maximum number of SANs to read from a x509 certificate */
92#define MAX_CERT_SAN 5
93
94/* Server Configuration Record */
95typedef struct {
96    /* --- Configuration values --- */
97        /* Is the module enabled? */
98    int enabled;
99        /* Is mod_proxy enabled? */
100    int proxy_enabled;
101        /* A Plain HTTP request */
102    int non_ssl_request;
103
104    /* List of PKCS #11 provider modules to load, only valid in the
105     * base config, ignored in virtual hosts */
106    apr_array_header_t *p11_modules;
107
108    /* PIN used for PKCS #11 operations */
109    char *pin;
110
111    /* the SRK PIN used in TPM operations */
112    char *srk_pin;
113
114    char *x509_cert_file;
115    char *x509_key_file;
116    char *x509_ca_file;
117
118    char *pgp_cert_file;
119    char *pgp_key_file;
120    char *pgp_ring_file;
121
122    char *dh_file;
123
124    char *priorities_str;
125    char *proxy_priorities_str;
126
127    const char* srp_tpasswd_file;
128    const char* srp_tpasswd_conf_file;
129
130        /* Cache timeout value */
131    int cache_timeout;
132        /* Chose Cache Type */
133    mgs_cache_e cache_type;
134    const char* cache_config;
135    /* Mutex for cache access (used only if the cache type is not
136     * thread-safe) */
137    apr_global_mutex_t *cache_mutex;
138
139        /* GnuTLS uses Session Tickets */
140    int tickets;
141
142    /* --- Things initialized at _child_init --- */
143
144    /* x509 Certificate Structure */
145    gnutls_certificate_credentials_t certs;
146    /* x509 credentials for proxy connections */
147    gnutls_certificate_credentials_t proxy_x509_creds;
148    /* trust list for proxy_x509_creds */
149    gnutls_x509_trust_list_t proxy_x509_tl;
150    const char* proxy_x509_key_file;
151    const char* proxy_x509_cert_file;
152    const char* proxy_x509_ca_file;
153    const char* proxy_x509_crl_file;
154    /* GnuTLS priorities for proxy connections */
155    gnutls_priority_t proxy_priorities;
156    /* SRP Certificate Structure*/
157    gnutls_srp_server_credentials_t srp_creds;
158    /* Anonymous Certificate Structure */
159    gnutls_anon_server_credentials_t anon_creds;
160    /* Anonymous Client Certificate Structure, used for proxy
161     * connections */
162    gnutls_anon_client_credentials_t anon_client_creds;
163        /* Current x509 Certificate CN [Common Name] */
164    char* cert_cn;
165        /* Current x509 Certificate SAN [Subject Alternate Name]s*/
166    char* cert_san[MAX_CERT_SAN];
167        /* An x509 Certificate Chain */
168    gnutls_pcert_st *certs_x509_chain;
169    gnutls_x509_crt_t *certs_x509_crt_chain;
170        /* Number of Certificates in Chain */
171    unsigned int certs_x509_chain_num;
172
173        /* Current x509 Certificate Private Key */
174    gnutls_privkey_t privkey_x509;
175
176        /* OpenPGP Certificate */
177    gnutls_pcert_st *cert_pgp;
178    gnutls_openpgp_crt_t *cert_crt_pgp;
179
180        /* OpenPGP Certificate Private Key */
181    gnutls_privkey_t privkey_pgp;
182#if GNUTLS_VERSION_NUMBER < 0x030312
183    /* Internal structure for the OpenPGP private key, used in the
184     * workaround for a bug in gnutls_privkey_import_openpgp_raw that
185     * frees memory that is still needed. DO NOT USE for any other
186     * purpose. */
187    gnutls_openpgp_privkey_t privkey_pgp_internal;
188#endif
189
190    /* Export full certificates to CGI environment: */
191    int export_certificates_size;
192        /* GnuTLS Priorities */
193    gnutls_priority_t priorities;
194        /* GnuTLS DH Parameters */
195    gnutls_dh_params_t dh_params;
196        /* A list of CA Certificates */
197    gnutls_x509_crt_t *ca_list;
198        /* OpenPGP Key Ring */
199    gnutls_openpgp_keyring_t pgp_list;
200        /* CA Certificate list size */
201    unsigned int ca_list_size;
202        /* Client Certificate Verification Mode */
203    int client_verify_mode;
204        /* Client Certificate Verification Method */
205    mgs_client_verification_method_e client_verify_method;
206        /* Last Cache timestamp */
207    apr_time_t last_cache_check;
208
209    /* EXPERIMENTAL: OCSP response file for stapling, will go away
210     * once sending OCSP requests is implemented */
211    char *ocsp_response_file;
212    /* OCSP URI extracted from the server certificate. NULL if
213     * unset. */
214    apr_uri_t *ocsp_uri;
215    /* Trust list to verify OCSP responses for stapling. Should
216     * usually only contain the CA that signed the server
217     * certificate. */
218    gnutls_x509_trust_list_t *ocsp_trust;
219} mgs_srvconf_rec;
220
221/* Character Buffer */
222typedef struct {
223    int length;
224    char *value;
225} mgs_char_buffer_t;
226
227/* GnuTLS Handle */
228typedef struct {
229        /* Server configuration record */
230    mgs_srvconf_rec *sc;
231        /* Connection record */
232    conn_rec* c;
233        /* Is TLS enabled for this connection? */
234    int enabled;
235    /* Is this a proxy connection? */
236    int is_proxy;
237        /* GnuTLS Session handle */
238    gnutls_session_t session;
239        /* module input status */
240    apr_status_t input_rc;
241        /* Input filter */
242    ap_filter_t *input_filter;
243        /* Input Bucket Brigade */
244    apr_bucket_brigade *input_bb;
245        /* Input Read Type */
246    apr_read_type_e input_block;
247        /* Input Mode */
248    ap_input_mode_t input_mode;
249        /* Input Character Buffer */
250    mgs_char_buffer_t input_cbuf;
251        /* Input Character Array */
252    char input_buffer[AP_IOBUFSIZE];
253        /* module Output status */
254    apr_status_t output_rc;
255        /* Output filter */
256    ap_filter_t *output_filter;
257        /* Output Bucket Brigade */
258    apr_bucket_brigade *output_bb;
259        /* Output character array */
260    char output_buffer[AP_IOBUFSIZE];
261        /* Output buffer length */
262    apr_size_t output_blen;
263        /* Output length */
264    apr_size_t output_length;
265        /* General Status */
266    int status;
267} mgs_handle_t;
268
269
270
271/** Functions in gnutls_io.c **/
272
273/* apr_signal_block() for blocking SIGPIPE */
274apr_status_t apr_signal_block(int signum);
275
276 /* Proxy Support */
277/* An optional function which returns non-zero if the given connection
278is using SSL/TLS. */
279APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
280/* The ssl_proxy_enable() and ssl_engine_disable() optional functions
281 * are used by mod_proxy to enable use of SSL for outgoing
282 * connections. */
283APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
284APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
285int ssl_is_https(conn_rec *c);
286int ssl_proxy_enable(conn_rec *c);
287int ssl_engine_disable(conn_rec *c);
288const char *mgs_set_proxy_engine(cmd_parms * parms, void *dummy,
289                                 const int arg);
290apr_status_t mgs_cleanup_pre_config(void *data);
291
292/**
293 * mgs_filter_input will filter the input data
294 * by decrypting it using GnuTLS and passes it cleartext.
295 *
296 * @param f     the filter info record
297 * @param bb    the bucket brigade, where to store the result to
298 * @param mode  what shall we read?
299 * @param block a block index we shall read from?
300 * @return result status
301 */
302apr_status_t mgs_filter_input(ap_filter_t * f,
303                                     apr_bucket_brigade * bb,
304                                     ap_input_mode_t mode,
305                                     apr_read_type_e block,
306                                     apr_off_t readbytes);
307
308/**
309 * mgs_filter_output will filter the encrypt
310 * the incoming bucket using GnuTLS and passes it onto the next filter.
311 *
312 * @param f     the filter info record
313 * @param bb    the bucket brigade, where to store the result to
314 * @return result status
315 */
316apr_status_t mgs_filter_output(ap_filter_t * f,
317                                      apr_bucket_brigade * bb);
318
319
320/**
321 * mgs_transport_read is called from GnuTLS to provide encrypted
322 * data from the client.
323 *
324 * @param ptr     pointer to the filter context
325 * @param buffer  place to put data
326 * @param len     maximum size
327 * @return size   length of the data stored in buffer
328 */
329ssize_t mgs_transport_read(gnutls_transport_ptr_t ptr,
330                                  void *buffer, size_t len);
331
332/**
333 * mgs_transport_write is called from GnuTLS to
334 * write data to the client.
335 *
336 * @param ptr     pointer to the filter context
337 * @param buffer  buffer to write to the client
338 * @param len     size of the buffer
339 * @return size   length of the data written
340 */
341ssize_t mgs_transport_write(gnutls_transport_ptr_t ptr,
342                                   const void *buffer, size_t len);
343
344
345int mgs_rehandshake(mgs_handle_t * ctxt);
346
347
348
349/**
350 * Perform any reinitialization required in PKCS #11
351 */
352int mgs_pkcs11_reinit(server_rec * s);
353
354
355
356/* Configuration Functions */
357
358/* Loads all files set in the configuration */
359int mgs_load_files(apr_pool_t * p, server_rec * s);
360
361const char *mgs_set_srp_tpasswd_conf_file(cmd_parms * parms, void *dummy,
362                                        const char *arg);
363const char *mgs_set_srp_tpasswd_file(cmd_parms * parms, void *dummy,
364                                        const char *arg);
365const char *mgs_set_dh_file(cmd_parms * parms, void *dummy,
366                                        const char *arg);
367const char *mgs_set_cert_file(cmd_parms * parms, void *dummy,
368                                        const char *arg);
369
370const char *mgs_set_key_file(cmd_parms * parms, void *dummy,
371                             const char *arg);
372
373const char *mgs_set_pgpcert_file(cmd_parms * parms, void *dummy,
374                                        const char *arg);
375
376const char *mgs_set_pgpkey_file(cmd_parms * parms, void *dummy,
377                             const char *arg);
378
379const char *mgs_set_cache(cmd_parms * parms, void *dummy,
380                          const char *type, const char* arg);
381
382const char *mgs_set_cache_timeout(cmd_parms * parms, void *dummy,
383                                  const char *arg);
384
385const char *mgs_set_client_verify(cmd_parms * parms, void *dummy,
386                                  const char *arg);
387
388const char *mgs_set_client_verify_method(cmd_parms * parms, void *dummy,
389                                         const char *arg);
390
391const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy,
392                                   const char *arg);
393
394const char *mgs_set_p11_module(cmd_parms * parms, void *dummy,
395                               const char *arg);
396
397const char *mgs_set_pin(cmd_parms * parms, void *dummy,
398                                   const char *arg);
399
400const char *mgs_set_srk_pin(cmd_parms * parms, void *dummy,
401                                   const char *arg);
402
403const char *mgs_set_keyring_file(cmd_parms * parms, void *dummy,
404                                   const char *arg);
405
406const char *mgs_set_enabled(cmd_parms * parms, void *dummy,
407                            const int arg);
408const char *mgs_set_export_certificates_size(cmd_parms * parms, void *dummy,
409                            const char *arg);
410const char *mgs_set_priorities(cmd_parms * parms, void *dummy,
411                            const char *arg);
412const char *mgs_set_tickets(cmd_parms * parms, void *dummy,
413                            const int arg);
414
415const char *mgs_set_require_section(cmd_parms *cmd,
416                                    void *mconfig, const char *arg);
417void *mgs_config_server_create(apr_pool_t * p, server_rec * s);
418void *mgs_config_server_merge(apr_pool_t *p, void *BASE, void *ADD);
419
420void *mgs_config_dir_merge(apr_pool_t *p, void *basev, void *addv);
421
422void *mgs_config_dir_create(apr_pool_t *p, char *dir);
423
424const char *mgs_set_require_bytecode(cmd_parms *cmd,
425                                    void *mconfig, const char *arg);
426
427mgs_srvconf_rec* mgs_find_sni_server(gnutls_session_t session);
428
429const char *mgs_store_cred_path(cmd_parms * parms,
430                                void *dummy __attribute__((unused)),
431                                const char *arg);
432
433/* mod_gnutls Hooks. */
434
435int mgs_hook_pre_config(apr_pool_t * pconf,
436                        apr_pool_t * plog, apr_pool_t * ptemp);
437
438int mgs_hook_post_config(apr_pool_t *pconf,
439                         apr_pool_t *plog,
440                         apr_pool_t *ptemp,
441                         server_rec *base_server);
442
443void mgs_hook_child_init(apr_pool_t *p, server_rec *s);
444
445const char *mgs_hook_http_scheme(const request_rec * r);
446
447apr_port_t mgs_hook_default_port(const request_rec * r);
448
449int mgs_hook_pre_connection(conn_rec * c, void *csd);
450
451int mgs_hook_fixups(request_rec *r);
452
453int mgs_hook_authz(request_rec *r);
454
455#endif /*  __mod_gnutls_h_inc */
Note: See TracBrowser for help on using the repository browser.