]> granicus.if.org Git - apache/blob - modules/ssl/ssl_private.h
Streamline TLS session ticket key handling (added in r1200040):
[apache] / modules / ssl / ssl_private.h
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef SSL_PRIVATE_H
18 #define SSL_PRIVATE_H
19
20 /**
21  * @file  ssl_private.h
22  * @brief Internal interfaces private to mod_ssl.
23  *
24  * @defgroup MOD_SSL_PRIVATE Private
25  * @ingroup MOD_SSL
26  * @{
27  */
28
29 /** Apache headers */
30 #include "httpd.h"
31 #include "http_config.h"
32 #include "http_core.h"
33 #include "http_log.h"
34 #include "http_main.h"
35 #include "http_connection.h"
36 #include "http_request.h"
37 #include "http_protocol.h"
38 #include "http_vhost.h"
39 #include "util_script.h"
40 #include "util_filter.h"
41 #include "util_ebcdic.h"
42 #include "util_mutex.h"
43 #include "apr.h"
44 #include "apr_strings.h"
45 #define APR_WANT_STRFUNC
46 #define APR_WANT_MEMFUNC
47 #include "apr_want.h"
48 #include "apr_tables.h"
49 #include "apr_lib.h"
50 #include "apr_fnmatch.h"
51 #include "apr_strings.h"
52 #include "apr_global_mutex.h"
53 #include "apr_optional.h"
54 #include "ap_socache.h"
55 #include "mod_auth.h"
56
57 /* The #ifdef macros are only defined AFTER including the above
58  * therefore we cannot include these system files at the top  :-(
59  */
60 #ifdef APR_HAVE_STDLIB_H
61 #include <stdlib.h>
62 #endif
63 #if APR_HAVE_SYS_TIME_H
64 #include <sys/time.h>
65 #endif
66 #if APR_HAVE_UNISTD_H
67 #include <unistd.h> /* needed for STDIN_FILENO et.al., at least on FreeBSD */
68 #endif
69
70 #ifndef FALSE
71 #define FALSE 0
72 #endif
73
74 #ifndef TRUE
75 #define TRUE !FALSE
76 #endif
77
78 #ifndef BOOL
79 #define BOOL unsigned int
80 #endif
81
82 #include "ap_expr.h"
83
84 /* OpenSSL headers */
85 #include <openssl/ssl.h>
86 #include <openssl/err.h>
87 #include <openssl/x509.h>
88 #include <openssl/pem.h>
89 #include <openssl/crypto.h>
90 #include <openssl/evp.h>
91 #include <openssl/rand.h>
92 #include <openssl/x509v3.h>
93 #include <openssl/x509_vfy.h>
94 #include <openssl/ocsp.h>
95
96 /* Avoid tripping over an engine build installed globally and detected
97  * when the user points at an explicit non-engine flavor of OpenSSL
98  */
99 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
100 #include <openssl/engine.h>
101 #endif
102
103 #if (OPENSSL_VERSION_NUMBER < 0x0090700f)
104 #error mod_ssl requires OpenSSL 0.9.7 or later
105 #endif
106
107 /* ...shifting sands of OpenSSL... */
108 #if (OPENSSL_VERSION_NUMBER >= 0x0090707f)
109 #define MODSSL_D2I_SSL_SESSION_CONST const
110 #else
111 #define MODSSL_D2I_SSL_SESSION_CONST
112 #endif
113
114 #if (OPENSSL_VERSION_NUMBER >= 0x00908000)
115 #define HAVE_GENERATE_EX
116 #define MODSSL_D2I_ASN1_type_bytes_CONST const
117 #define MODSSL_D2I_PrivateKey_CONST const
118 #define MODSSL_D2I_X509_CONST const
119 #else
120 #define MODSSL_D2I_ASN1_type_bytes_CONST
121 #define MODSSL_D2I_PrivateKey_CONST
122 #define MODSSL_D2I_X509_CONST
123 #endif
124
125 #if OPENSSL_VERSION_NUMBER >= 0x00908080 && !defined(OPENSSL_NO_OCSP) \
126     && !defined(OPENSSL_NO_TLSEXT)
127 #define HAVE_OCSP_STAPLING
128 #if (OPENSSL_VERSION_NUMBER < 0x10000000)
129 #define sk_OPENSSL_STRING_pop sk_pop
130 #endif
131 #endif
132
133 #if (OPENSSL_VERSION_NUMBER >= 0x009080a0) && defined(OPENSSL_FIPS)
134 #define HAVE_FIPS
135 #endif
136
137 #if (OPENSSL_VERSION_NUMBER >= 0x10000000)
138 #define MODSSL_SSL_CIPHER_CONST const
139 #define MODSSL_SSL_METHOD_CONST const
140 #else
141 #define MODSSL_SSL_CIPHER_CONST
142 #define MODSSL_SSL_METHOD_CONST
143 /* ECC support came along in OpenSSL 1.0.0 */
144 #define OPENSSL_NO_EC
145 #endif
146
147 #ifndef PEM_F_DEF_CALLBACK
148 #ifdef PEM_F_PEM_DEF_CALLBACK
149 /** In OpenSSL 0.9.8 PEM_F_DEF_CALLBACK was renamed */
150 #define PEM_F_DEF_CALLBACK PEM_F_PEM_DEF_CALLBACK
151 #endif
152 #endif
153
154 #ifndef OPENSSL_NO_TLSEXT
155 #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
156 #define OPENSSL_NO_TLSEXT
157 #endif
158 #endif
159
160 #ifndef OPENSSL_NO_TLSEXT
161 #ifdef SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB
162 #define HAVE_TLS_SESSION_TICKETS
163 #define TLSEXT_TICKET_KEY_LEN 48
164 #ifndef tlsext_tick_md
165 #ifdef OPENSSL_NO_SHA256
166 #define tlsext_tick_md EVP_sha1
167 #else
168 #define tlsext_tick_md EVP_sha256
169 #endif
170 #endif
171 #endif
172 #endif
173
174 /* mod_ssl headers */
175 #include "ssl_util_ssl.h"
176
177 APLOG_USE_MODULE(ssl);
178
179 /*
180  * Provide reasonable default for some defines
181  */
182 #ifndef PFALSE
183 #define PFALSE ((void *)FALSE)
184 #endif
185 #ifndef PTRUE
186 #define PTRUE ((void *)TRUE)
187 #endif
188 #ifndef UNSET
189 #define UNSET (-1)
190 #endif
191 #ifndef NUL
192 #define NUL '\0'
193 #endif
194 #ifndef RAND_MAX
195 #include <limits.h>
196 #define RAND_MAX INT_MAX
197 #endif
198
199 /**
200  * Provide reasonable defines for some types
201  */
202 #ifndef UCHAR
203 #define UCHAR unsigned char
204 #endif
205
206 /**
207  * Provide useful shorthands
208  */
209 #define strEQ(s1,s2)     (strcmp(s1,s2)        == 0)
210 #define strNE(s1,s2)     (strcmp(s1,s2)        != 0)
211 #define strEQn(s1,s2,n)  (strncmp(s1,s2,n)     == 0)
212 #define strNEn(s1,s2,n)  (strncmp(s1,s2,n)     != 0)
213
214 #define strcEQ(s1,s2)    (strcasecmp(s1,s2)    == 0)
215 #define strcNE(s1,s2)    (strcasecmp(s1,s2)    != 0)
216 #define strcEQn(s1,s2,n) (strncasecmp(s1,s2,n) == 0)
217 #define strcNEn(s1,s2,n) (strncasecmp(s1,s2,n) != 0)
218
219 #define strIsEmpty(s)    (s == NULL || s[0] == NUL)
220
221 #define myConnConfig(c) \
222 (SSLConnRec *)ap_get_module_config(c->conn_config, &ssl_module)
223 #define myCtxConfig(sslconn, sc) (sslconn->is_proxy ? sc->proxy : sc->server)
224 #define myConnConfigSet(c, val) \
225 ap_set_module_config(c->conn_config, &ssl_module, val)
226 #define mySrvConfig(srv) (SSLSrvConfigRec *)ap_get_module_config(srv->module_config,  &ssl_module)
227 #define myDirConfig(req) (SSLDirConfigRec *)ap_get_module_config(req->per_dir_config, &ssl_module)
228 #define myModConfig(srv) (mySrvConfig((srv)))->mc
229 #define mySrvFromConn(c) (myConnConfig(c))->server
230 #define mySrvConfigFromConn(c) mySrvConfig(mySrvFromConn(c))
231 #define myModConfigFromConn(c) myModConfig(mySrvFromConn(c))
232
233 #define myCtxVarSet(mc,num,val)  mc->rCtx.pV##num = val
234 #define myCtxVarGet(mc,num,type) (type)(mc->rCtx.pV##num)
235
236 /**
237  * Defaults for the configuration
238  */
239 #ifndef SSL_SESSION_CACHE_TIMEOUT
240 #define SSL_SESSION_CACHE_TIMEOUT  300
241 #endif
242
243 /* Default setting for per-dir reneg buffer. */
244 #ifndef DEFAULT_RENEG_BUFFER_SIZE
245 #define DEFAULT_RENEG_BUFFER_SIZE (128 * 1024)
246 #endif
247
248 /* Default for OCSP response validity */
249 #ifndef DEFAULT_OCSP_MAX_SKEW
250 #define DEFAULT_OCSP_MAX_SKEW (60 * 5)
251 #endif
252
253 /* Default timeout for OCSP queries */
254 #ifndef DEFAULT_OCSP_TIMEOUT
255 #define DEFAULT_OCSP_TIMEOUT 10
256 #endif
257
258 /**
259  * Define the certificate algorithm types
260  */
261
262 typedef int ssl_algo_t;
263
264 #define SSL_ALGO_UNKNOWN (0)
265 #define SSL_ALGO_RSA     (1<<0)
266 #define SSL_ALGO_DSA     (1<<1)
267 #ifndef OPENSSL_NO_EC
268 #define SSL_ALGO_ECC     (1<<2)
269 #define SSL_ALGO_ALL     (SSL_ALGO_RSA|SSL_ALGO_DSA|SSL_ALGO_ECC)
270 #else
271 #define SSL_ALGO_ALL     (SSL_ALGO_RSA|SSL_ALGO_DSA)
272 #endif
273
274 #define SSL_AIDX_RSA     (0)
275 #define SSL_AIDX_DSA     (1)
276 #ifndef OPENSSL_NO_EC
277 #define SSL_AIDX_ECC     (2)
278 #define SSL_AIDX_MAX     (3)
279 #else
280 #define SSL_AIDX_MAX     (2)
281 #endif
282
283
284 /**
285  * Define IDs for the temporary RSA keys and DH params
286  */
287
288 #define SSL_TMP_KEY_RSA_512  (0)
289 #define SSL_TMP_KEY_RSA_1024 (1)
290 #define SSL_TMP_KEY_DH_512   (2)
291 #define SSL_TMP_KEY_DH_1024  (3)
292 #define SSL_TMP_KEY_MAX      (4)
293
294 /**
295  * Define the SSL options
296  */
297 #define SSL_OPT_NONE           (0)
298 #define SSL_OPT_RELSET         (1<<0)
299 #define SSL_OPT_STDENVVARS     (1<<1)
300 #define SSL_OPT_EXPORTCERTDATA (1<<3)
301 #define SSL_OPT_FAKEBASICAUTH  (1<<4)
302 #define SSL_OPT_STRICTREQUIRE  (1<<5)
303 #define SSL_OPT_OPTRENEGOTIATE (1<<6)
304 #define SSL_OPT_LEGACYDNFORMAT (1<<7)
305 typedef int ssl_opt_t;
306
307 /**
308  * Define the SSL Protocol options
309  */
310 #define SSL_PROTOCOL_NONE  (0)
311 #define SSL_PROTOCOL_SSLV2 (1<<0)
312 #define SSL_PROTOCOL_SSLV3 (1<<1)
313 #define SSL_PROTOCOL_TLSV1 (1<<2)
314 #define SSL_PROTOCOL_ALL   (SSL_PROTOCOL_SSLV3|SSL_PROTOCOL_TLSV1)
315 typedef int ssl_proto_t;
316
317 /**
318  * Define the SSL verify levels
319  */
320 typedef enum {
321     SSL_CVERIFY_UNSET           = UNSET,
322     SSL_CVERIFY_NONE            = 0,
323     SSL_CVERIFY_OPTIONAL        = 1,
324     SSL_CVERIFY_REQUIRE         = 2,
325     SSL_CVERIFY_OPTIONAL_NO_CA  = 3
326 } ssl_verify_t;
327
328 #define SSL_VERIFY_PEER_STRICT \
329      (SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
330
331 #define ssl_verify_error_is_optional(errnum) \
332    ((errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) \
333     || (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) \
334     || (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) \
335     || (errnum == X509_V_ERR_CERT_UNTRUSTED) \
336     || (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE))
337
338 /**
339   * CRL checking modes
340   */
341 typedef enum {
342     SSL_CRLCHECK_UNSET = UNSET,
343     SSL_CRLCHECK_NONE  = 0,
344     SSL_CRLCHECK_LEAF  = 1,
345     SSL_CRLCHECK_CHAIN = 2
346 } ssl_crlcheck_t;
347
348 /**
349  * Define the SSL pass phrase dialog types
350  */
351 typedef enum {
352     SSL_PPTYPE_UNSET   = UNSET,
353     SSL_PPTYPE_BUILTIN = 0,
354     SSL_PPTYPE_FILTER  = 1,
355     SSL_PPTYPE_PIPE    = 2
356 } ssl_pphrase_t;
357
358 /**
359  * Define the Path Checking modes
360  */
361 #define SSL_PCM_EXISTS     1
362 #define SSL_PCM_ISREG      2
363 #define SSL_PCM_ISDIR      4
364 #define SSL_PCM_ISNONZERO  8
365 typedef unsigned int ssl_pathcheck_t;
366
367 /**
368  * Define the SSL enabled state
369  */
370 typedef enum {
371     SSL_ENABLED_UNSET    = UNSET,
372     SSL_ENABLED_FALSE    = 0,
373     SSL_ENABLED_TRUE     = 1,
374     SSL_ENABLED_OPTIONAL = 3
375 } ssl_enabled_t;
376
377 /**
378  * Define the SSL requirement structure
379  */
380 typedef struct {
381     char           *cpExpr;
382     ap_expr_info_t *mpExpr;
383 } ssl_require_t;
384
385 /**
386  * Define the SSL random number generator seeding source
387  */
388 typedef enum {
389     SSL_RSCTX_STARTUP = 1,
390     SSL_RSCTX_CONNECT = 2
391 } ssl_rsctx_t;
392 typedef enum {
393     SSL_RSSRC_BUILTIN = 1,
394     SSL_RSSRC_FILE    = 2,
395     SSL_RSSRC_EXEC    = 3,
396     SSL_RSSRC_EGD     = 4
397 } ssl_rssrc_t;
398 typedef struct {
399     ssl_rsctx_t  nCtx;
400     ssl_rssrc_t  nSrc;
401     char        *cpPath;
402     int          nBytes;
403 } ssl_randseed_t;
404
405 /**
406  * Define the structure of an ASN.1 anything
407  */
408 typedef struct {
409     long int       nData;
410     unsigned char *cpData;
411     apr_time_t     source_mtime;
412 } ssl_asn1_t;
413
414 /**
415  * Define the mod_ssl per-module configuration structure
416  * (i.e. the global configuration for each httpd process)
417  */
418
419 typedef enum {
420     SSL_SHUTDOWN_TYPE_UNSET,
421     SSL_SHUTDOWN_TYPE_STANDARD,
422     SSL_SHUTDOWN_TYPE_UNCLEAN,
423     SSL_SHUTDOWN_TYPE_ACCURATE
424 } ssl_shutdown_type_e;
425
426 typedef struct {
427     SSL *ssl;
428     const char *client_dn;
429     X509 *client_cert;
430     ssl_shutdown_type_e shutdown_type;
431     const char *verify_info;
432     const char *verify_error;
433     int verify_depth;
434     int is_proxy;
435     int disabled;
436     int non_ssl_request;
437
438     /* Track the handshake/renegotiation state for the connection so
439      * that all client-initiated renegotiations can be rejected, as a
440      * partial fix for CVE-2009-3555. */
441     enum {
442         RENEG_INIT = 0, /* Before initial handshake */
443         RENEG_REJECT, /* After initial handshake; any client-initiated
444                        * renegotiation should be rejected */
445         RENEG_ALLOW, /* A server-initated renegotiation is taking
446                       * place (as dictated by configuration) */
447         RENEG_ABORT /* Renegotiation initiated by client, abort the
448                      * connection */
449     } reneg_state;
450
451     server_rec *server;
452 } SSLConnRec;
453
454 /* BIG FAT WARNING: SSLModConfigRec has unusual memory lifetime: it is
455  * allocated out of the "process" pool and only a single such
456  * structure is created and used for the lifetime of the process.
457  * (The process pool is s->process->pool and is stored in the .pPool
458  * field.)  Most members of this structure are likewise allocated out
459  * of the process pool, but notably sesscache and sesscache_context
460  * are not.
461  *
462  * The structure is treated as mostly immutable after a single config
463  * parse has completed; the post_config hook (ssl_init_Module) flips
464  * the bFixed flag to true and subsequent invocations of the config
465  * callbacks hence do nothing.
466  *
467  * This odd lifetime strategy is used so that encrypted private keys
468  * can be decrypted once at startup and continue to be used across
469  * subsequent server reloads where the interactive password prompt is
470  * not possible.
471
472  * It is really an ABI nightmare waiting to happen since DSOs are
473  * reloaded across restarts, and nothing prevents the struct type
474  * changing across such reloads, yet the cached structure will be
475  * assumed to match regardless.
476  *
477  * This should really be fixed using a smaller structure which only
478  * stores that which is absolutely necessary (the private keys, maybe
479  * the random seed), and have that structure be strictly ABI-versioned
480  * for safety.
481  */
482 typedef struct {
483     pid_t           pid;
484     apr_pool_t     *pPool;
485     BOOL            bFixed;
486
487     /* OpenSSL SSL_SESS_CACHE_* flags: */
488     long            sesscache_mode;
489
490     /* The configured provider, and associated private data
491      * structure. */
492     const ap_socache_provider_t *sesscache;
493     ap_socache_instance_t *sesscache_context;
494
495     apr_global_mutex_t   *pMutex;
496     apr_array_header_t   *aRandSeed;
497     apr_hash_t     *tVHostKeys;
498     void           *pTmpKeys[SSL_TMP_KEY_MAX];
499
500     /* Two hash tables of pointers to ssl_asn1_t structures.  The
501      * structures are used to store certificates and private keys
502      * respectively, in raw DER format (serialized OpenSSL X509 and
503      * PrivateKey structures).  The tables are indexed by (vhost-id,
504      * algorithm type) using the function ssl_asn1_table_keyfmt(); for
505      * example the string "vhost.example.com:443:RSA". */
506     apr_hash_t     *tPublicCert;
507     apr_hash_t     *tPrivateKey;
508
509 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
510     const char     *szCryptoDevice;
511 #endif
512
513 #ifdef HAVE_OCSP_STAPLING
514     const ap_socache_provider_t *stapling_cache;
515     ap_socache_instance_t *stapling_cache_context;
516     apr_global_mutex_t   *stapling_mutex;
517 #endif
518
519     struct {
520         void *pV1, *pV2, *pV3, *pV4, *pV5, *pV6, *pV7, *pV8, *pV9, *pV10;
521     } rCtx;
522 } SSLModConfigRec;
523
524 /** Structure representing configured filenames for certs and keys for
525  * a given vhost, and the corresponding in-memory structures once the
526  * files are parsed.  */
527 typedef struct {
528     /* Lists of configured certs and keys for this server; from index
529      * 0 up to SSL_AIDX_MAX-1 or the first NULL pointer.  Note that
530      * these arrays are NOT indexed by algorithm type, they are simply
531      * unordered lists. */
532     const char  *cert_files[SSL_AIDX_MAX];
533     const char  *key_files[SSL_AIDX_MAX];
534     /* Loaded certs and keys; these arrays ARE indexed by the
535      * algorithm type, i.e.  keys[SSL_AIDX_RSA] maps to the RSA
536      * private key. */
537     X509        *certs[SSL_AIDX_MAX];
538     EVP_PKEY    *keys[SSL_AIDX_MAX];
539
540     /** Certificates which specify the set of CA names which should be
541      * sent in the CertificateRequest message: */
542     const char  *ca_name_path;
543     const char  *ca_name_file;
544 } modssl_pk_server_t;
545
546 typedef struct {
547     /** proxy can have any number of cert/key pairs */
548     const char  *cert_file;
549     const char  *cert_path;
550     const char  *ca_cert_file;
551     STACK_OF(X509_INFO) *certs; /* Contains End Entity certs */
552     STACK_OF(X509) **ca_certs; /* Contains ONLY chain certs for
553                                 * each item in certs.
554                                 * (ptr to array of ptrs) */
555 } modssl_pk_proxy_t;
556
557 /** stuff related to authentication that can also be per-dir */
558 typedef struct {
559     /** known/trusted CAs */
560     const char  *ca_cert_path;
561     const char  *ca_cert_file;
562
563     const char  *cipher_suite;
564
565     /** for client or downstream server authentication */
566     int          verify_depth;
567     ssl_verify_t verify_mode;
568 } modssl_auth_ctx_t;
569
570 #ifdef HAVE_TLS_SESSION_TICKETS
571 typedef struct {
572     const char *file_path;
573     unsigned char key_name[16];
574     unsigned char hmac_secret[16];
575     unsigned char aes_key[16];
576 } modssl_ticket_key_t;
577 #endif
578
579 typedef struct SSLSrvConfigRec SSLSrvConfigRec;
580
581 typedef struct {
582     SSLSrvConfigRec *sc; /** pointer back to server config */
583     SSL_CTX *ssl_ctx;
584
585     /** we are one or the other */
586     modssl_pk_server_t *pks;
587     modssl_pk_proxy_t  *pkp;
588
589 #ifdef HAVE_TLS_SESSION_TICKETS
590     modssl_ticket_key_t *ticket_key;
591 #endif
592
593     ssl_proto_t  protocol;
594
595     /** config for handling encrypted keys */
596     ssl_pphrase_t pphrase_dialog_type;
597     const char   *pphrase_dialog_path;
598
599     const char  *cert_chain;
600     const char  *pkcs7;
601
602     /** certificate revocation list */
603     const char    *crl_path;
604     const char    *crl_file;
605     ssl_crlcheck_t crl_check_mode;
606
607 #ifdef HAVE_OCSP_STAPLING
608     /** OCSP stapling options */
609     BOOL        stapling_enabled;
610     long        stapling_resptime_skew;
611     long        stapling_resp_maxage;
612     int         stapling_cache_timeout;
613     BOOL        stapling_return_errors;
614     BOOL        stapling_fake_trylater;
615     int         stapling_errcache_timeout;
616     apr_interval_time_t stapling_responder_timeout;
617     const char *stapling_force_url;
618 #endif
619
620     modssl_auth_ctx_t auth;
621
622     BOOL ocsp_enabled; /* true if OCSP verification enabled */
623     BOOL ocsp_force_default; /* true if the default responder URL is
624                               * used regardless of per-cert URL */
625     const char *ocsp_responder; /* default responder URL */
626     long ocsp_resptime_skew;
627     long ocsp_resp_maxage;
628     apr_interval_time_t ocsp_responder_timeout;
629
630 } modssl_ctx_t;
631
632 struct SSLSrvConfigRec {
633     SSLModConfigRec *mc;
634     ssl_enabled_t    enabled;
635     BOOL             proxy_enabled;
636     const char      *vhost_id;
637     int              vhost_id_len;
638     int              session_cache_timeout;
639     BOOL             cipher_server_pref;
640     BOOL             insecure_reneg;
641     modssl_ctx_t    *server;
642     modssl_ctx_t    *proxy;
643     ssl_enabled_t    proxy_ssl_check_peer_expire;
644     ssl_enabled_t    proxy_ssl_check_peer_cn;
645 #ifndef OPENSSL_NO_TLSEXT
646     ssl_enabled_t    strict_sni_vhost_check;
647 #endif
648 #ifdef HAVE_FIPS
649     BOOL             fips;
650 #endif
651 };
652
653 /**
654  * Define the mod_ssl per-directory configuration structure
655  * (i.e. the local configuration for all &lt;Directory>
656  *  and .htaccess contexts)
657  */
658 typedef struct {
659     BOOL          bSSLRequired;
660     apr_array_header_t *aRequirement;
661     ssl_opt_t     nOptions;
662     ssl_opt_t     nOptionsAdd;
663     ssl_opt_t     nOptionsDel;
664     const char   *szCipherSuite;
665     ssl_verify_t  nVerifyClient;
666     int           nVerifyDepth;
667     const char   *szCACertificatePath;
668     const char   *szCACertificateFile;
669     const char   *szUserName;
670     apr_size_t    nRenegBufferSize;
671 } SSLDirConfigRec;
672
673 /**
674  *  function prototypes
675  */
676
677 /**  API glue structures  */
678 extern module AP_MODULE_DECLARE_DATA ssl_module;
679
680 /**  configuration handling   */
681 SSLModConfigRec *ssl_config_global_create(server_rec *);
682 void         ssl_config_global_fix(SSLModConfigRec *);
683 BOOL         ssl_config_global_isfixed(SSLModConfigRec *);
684 void        *ssl_config_server_create(apr_pool_t *, server_rec *);
685 void        *ssl_config_server_merge(apr_pool_t *, void *, void *);
686 void        *ssl_config_perdir_create(apr_pool_t *, char *);
687 void        *ssl_config_perdir_merge(apr_pool_t *, void *, void *);
688 const char  *ssl_cmd_SSLPassPhraseDialog(cmd_parms *, void *, const char *);
689 const char  *ssl_cmd_SSLCryptoDevice(cmd_parms *, void *, const char *);
690 const char  *ssl_cmd_SSLRandomSeed(cmd_parms *, void *, const char *, const char *, const char *);
691 const char  *ssl_cmd_SSLEngine(cmd_parms *, void *, const char *);
692 const char  *ssl_cmd_SSLCipherSuite(cmd_parms *, void *, const char *);
693 const char  *ssl_cmd_SSLCertificateFile(cmd_parms *, void *, const char *);
694 const char  *ssl_cmd_SSLCertificateKeyFile(cmd_parms *, void *, const char *);
695 const char  *ssl_cmd_SSLCertificateChainFile(cmd_parms *, void *, const char *);
696 const char  *ssl_cmd_SSLPKCS7CertificateFile(cmd_parms *, void *, const char *);
697 const char  *ssl_cmd_SSLCACertificatePath(cmd_parms *, void *, const char *);
698 const char  *ssl_cmd_SSLCACertificateFile(cmd_parms *, void *, const char *);
699 const char  *ssl_cmd_SSLCADNRequestPath(cmd_parms *, void *, const char *);
700 const char  *ssl_cmd_SSLCADNRequestFile(cmd_parms *, void *, const char *);
701 const char  *ssl_cmd_SSLCARevocationPath(cmd_parms *, void *, const char *);
702 const char  *ssl_cmd_SSLCARevocationFile(cmd_parms *, void *, const char *);
703 const char  *ssl_cmd_SSLCARevocationCheck(cmd_parms *, void *, const char *);
704 const char  *ssl_cmd_SSLHonorCipherOrder(cmd_parms *cmd, void *dcfg, int flag);
705 const char  *ssl_cmd_SSLVerifyClient(cmd_parms *, void *, const char *);
706 const char  *ssl_cmd_SSLVerifyDepth(cmd_parms *, void *, const char *);
707 const char  *ssl_cmd_SSLSessionCache(cmd_parms *, void *, const char *);
708 const char  *ssl_cmd_SSLSessionCacheTimeout(cmd_parms *, void *, const char *);
709 const char  *ssl_cmd_SSLProtocol(cmd_parms *, void *, const char *);
710 const char  *ssl_cmd_SSLOptions(cmd_parms *, void *, const char *);
711 const char  *ssl_cmd_SSLRequireSSL(cmd_parms *, void *);
712 const char  *ssl_cmd_SSLRequire(cmd_parms *, void *, const char *);
713 const char  *ssl_cmd_SSLUserName(cmd_parms *, void *, const char *);
714 const char  *ssl_cmd_SSLRenegBufferSize(cmd_parms *cmd, void *dcfg, const char *arg);
715 const char  *ssl_cmd_SSLStrictSNIVHostCheck(cmd_parms *cmd, void *dcfg, int flag);
716 const char *ssl_cmd_SSLInsecureRenegotiation(cmd_parms *cmd, void *dcfg, int flag);
717
718 const char  *ssl_cmd_SSLProxyEngine(cmd_parms *cmd, void *dcfg, int flag);
719 const char  *ssl_cmd_SSLProxyProtocol(cmd_parms *, void *, const char *);
720 const char  *ssl_cmd_SSLProxyCipherSuite(cmd_parms *, void *, const char *);
721 const char  *ssl_cmd_SSLProxyVerify(cmd_parms *, void *, const char *);
722 const char  *ssl_cmd_SSLProxyVerifyDepth(cmd_parms *, void *, const char *);
723 const char  *ssl_cmd_SSLProxyCACertificatePath(cmd_parms *, void *, const char *);
724 const char  *ssl_cmd_SSLProxyCACertificateFile(cmd_parms *, void *, const char *);
725 const char  *ssl_cmd_SSLProxyCARevocationPath(cmd_parms *, void *, const char *);
726 const char  *ssl_cmd_SSLProxyCARevocationFile(cmd_parms *, void *, const char *);
727 const char  *ssl_cmd_SSLProxyCARevocationCheck(cmd_parms *, void *, const char *);
728 const char  *ssl_cmd_SSLProxyMachineCertificatePath(cmd_parms *, void *, const char *);
729 const char  *ssl_cmd_SSLProxyMachineCertificateFile(cmd_parms *, void *, const char *);
730 const char  *ssl_cmd_SSLProxyMachineCertificateChainFile(cmd_parms *, void *, const char *);
731 #ifdef HAVE_TLS_SESSION_TICKETS
732 const char *ssl_cmd_SSLSessionTicketKeyFile(cmd_parms *cmd, void *dcfg, const char *arg);
733 #endif
734 const char  *ssl_cmd_SSLProxyCheckPeerExpire(cmd_parms *cmd, void *dcfg, int flag);
735 const char  *ssl_cmd_SSLProxyCheckPeerCN(cmd_parms *cmd, void *dcfg, int flag);
736
737 const char *ssl_cmd_SSLOCSPOverrideResponder(cmd_parms *cmd, void *dcfg, int flag);
738 const char *ssl_cmd_SSLOCSPDefaultResponder(cmd_parms *cmd, void *dcfg, const char *arg);
739 const char *ssl_cmd_SSLOCSPResponseTimeSkew(cmd_parms *cmd, void *dcfg, const char *arg);
740 const char *ssl_cmd_SSLOCSPResponseMaxAge(cmd_parms *cmd, void *dcfg, const char *arg);
741 const char *ssl_cmd_SSLOCSPResponderTimeout(cmd_parms *cmd, void *dcfg, const char *arg);
742 const char *ssl_cmd_SSLOCSPEnable(cmd_parms *cmd, void *dcfg, int flag);
743
744 const char *ssl_cmd_SSLFIPS(cmd_parms *cmd, void *dcfg, int flag);
745
746 /**  module initialization  */
747 int          ssl_init_Module(apr_pool_t *, apr_pool_t *, apr_pool_t *, server_rec *);
748 void         ssl_init_Engine(server_rec *, apr_pool_t *);
749 void         ssl_init_ConfigureServer(server_rec *, apr_pool_t *, apr_pool_t *, SSLSrvConfigRec *);
750 void         ssl_init_CheckServers(server_rec *, apr_pool_t *);
751 STACK_OF(X509_NAME)
752             *ssl_init_FindCAList(server_rec *, apr_pool_t *, const char *, const char *);
753 void         ssl_init_Child(apr_pool_t *, server_rec *);
754 apr_status_t ssl_init_ModuleKill(void *data);
755
756 /**  Apache API hooks  */
757 int          ssl_hook_Auth(request_rec *);
758 int          ssl_hook_UserCheck(request_rec *);
759 int          ssl_hook_Access(request_rec *);
760 int          ssl_hook_Fixup(request_rec *);
761 int          ssl_hook_ReadReq(request_rec *);
762 int          ssl_hook_Upgrade(request_rec *);
763 void         ssl_hook_ConfigTest(apr_pool_t *pconf, server_rec *s);
764
765 /** Apache authz provisders */
766 extern const authz_provider ssl_authz_provider_require_ssl;
767 extern const authz_provider ssl_authz_provider_verify_client;
768
769 /**  OpenSSL callbacks */
770 RSA         *ssl_callback_TmpRSA(SSL *, int, int);
771 DH          *ssl_callback_TmpDH(SSL *, int, int);
772 #ifndef OPENSSL_NO_EC
773 EC_KEY      *ssl_callback_TmpECDH(SSL *, int, int);
774 #endif
775 int          ssl_callback_SSLVerify(int, X509_STORE_CTX *);
776 int          ssl_callback_SSLVerify_CRL(int, X509_STORE_CTX *, conn_rec *);
777 int          ssl_callback_proxy_cert(SSL *ssl, X509 **x509, EVP_PKEY **pkey);
778 int          ssl_callback_NewSessionCacheEntry(SSL *, SSL_SESSION *);
779 SSL_SESSION *ssl_callback_GetSessionCacheEntry(SSL *, unsigned char *, int, int *);
780 void         ssl_callback_DelSessionCacheEntry(SSL_CTX *, SSL_SESSION *);
781 void         ssl_callback_Info(const SSL *, int, int);
782 #ifndef OPENSSL_NO_TLSEXT
783 int          ssl_callback_ServerNameIndication(SSL *, int *, modssl_ctx_t *);
784 #endif
785 #ifdef HAVE_TLS_SESSION_TICKETS
786 int         ssl_callback_SessionTicket(SSL *, unsigned char *, unsigned char *,
787                                        EVP_CIPHER_CTX *, HMAC_CTX *, int);
788 #endif
789
790 /**  Session Cache Support  */
791 void         ssl_scache_init(server_rec *, apr_pool_t *);
792 void         ssl_scache_status_register(apr_pool_t *p);
793 void         ssl_scache_kill(server_rec *);
794 BOOL         ssl_scache_store(server_rec *, UCHAR *, int,
795                               apr_time_t, SSL_SESSION *, apr_pool_t *);
796 SSL_SESSION *ssl_scache_retrieve(server_rec *, UCHAR *, int, apr_pool_t *);
797 void         ssl_scache_remove(server_rec *, UCHAR *, int,
798                                apr_pool_t *);
799
800 /** Proxy Support */
801 int ssl_proxy_enable(conn_rec *c);
802 int ssl_engine_disable(conn_rec *c);
803
804 /** OCSP Stapling Support */
805 #ifdef HAVE_OCSP_STAPLING
806 const char *ssl_cmd_SSLStaplingCache(cmd_parms *, void *, const char *);
807 const char *ssl_cmd_SSLUseStapling(cmd_parms *, void *, int);
808 const char *ssl_cmd_SSLStaplingResponseTimeSkew(cmd_parms *, void *, const char *);
809 const char *ssl_cmd_SSLStaplingResponseMaxAge(cmd_parms *, void *, const char *);
810 const char *ssl_cmd_SSLStaplingStandardCacheTimeout(cmd_parms *, void *, const char *);
811 const char *ssl_cmd_SSLStaplingErrorCacheTimeout(cmd_parms *, void *, const char *);
812 const char *ssl_cmd_SSLStaplingReturnResponderErrors(cmd_parms *, void *, int);
813 const char *ssl_cmd_SSLStaplingFakeTryLater(cmd_parms *, void *, int);
814 const char *ssl_cmd_SSLStaplingResponderTimeout(cmd_parms *, void *, const char *);
815 const char  *ssl_cmd_SSLStaplingForceURL(cmd_parms *, void *, const char *);
816 void         modssl_init_stapling(server_rec *, apr_pool_t *, apr_pool_t *, modssl_ctx_t *);
817 void         ssl_stapling_ex_init(void);
818 int          ssl_stapling_init_cert(server_rec *s, modssl_ctx_t *mctx, X509 *x);
819 #endif
820
821 /**  I/O  */
822 void         ssl_io_filter_init(conn_rec *, request_rec *r, SSL *);
823 void         ssl_io_filter_register(apr_pool_t *);
824 long         ssl_io_data_cb(BIO *, int, const char *, int, long, long);
825
826 /* ssl_io_buffer_fill fills the setaside buffering of the HTTP request
827  * to allow an SSL renegotiation to take place. */
828 int          ssl_io_buffer_fill(request_rec *r, apr_size_t maxlen);
829
830 /**  PRNG  */
831 int          ssl_rand_seed(server_rec *, apr_pool_t *, ssl_rsctx_t, char *);
832
833 /**  Utility Functions  */
834 char        *ssl_util_vhostid(apr_pool_t *, server_rec *);
835 apr_file_t  *ssl_util_ppopen(server_rec *, apr_pool_t *, const char *,
836                              const char * const *);
837 void         ssl_util_ppclose(server_rec *, apr_pool_t *, apr_file_t *);
838 char        *ssl_util_readfilter(server_rec *, apr_pool_t *, const char *,
839                                  const char * const *);
840 BOOL         ssl_util_path_check(ssl_pathcheck_t, const char *, apr_pool_t *);
841 ssl_algo_t   ssl_util_algotypeof(X509 *, EVP_PKEY *);
842 char        *ssl_util_algotypestr(ssl_algo_t);
843 void         ssl_util_thread_setup(apr_pool_t *);
844 int          ssl_init_ssl_connection(conn_rec *c, request_rec *r);
845
846 /**  Pass Phrase Support  */
847 void         ssl_pphrase_Handle(server_rec *, apr_pool_t *);
848
849 /**  Diffie-Hellman Parameter Support  */
850 DH           *ssl_dh_GetTmpParam(int);
851 DH           *ssl_dh_GetParamFromFile(char *);
852
853 unsigned char *ssl_asn1_table_set(apr_hash_t *table,
854                                   const char *key,
855                                   long int length);
856
857 ssl_asn1_t *ssl_asn1_table_get(apr_hash_t *table,
858                                const char *key);
859
860 void ssl_asn1_table_unset(apr_hash_t *table,
861                           const char *key);
862
863 const char *ssl_asn1_keystr(int keytype);
864
865 const char *ssl_asn1_table_keyfmt(apr_pool_t *p,
866                                   const char *id,
867                                   int keytype);
868
869 STACK_OF(X509) *ssl_read_pkcs7(server_rec *s, const char *pkcs7);
870
871 /**  Mutex Support  */
872 int          ssl_mutex_init(server_rec *, apr_pool_t *);
873 int          ssl_mutex_reinit(server_rec *, apr_pool_t *);
874 int          ssl_mutex_on(server_rec *);
875 int          ssl_mutex_off(server_rec *);
876
877 int          ssl_stapling_mutex_reinit(server_rec *, apr_pool_t *);
878
879 /* mutex type names for Mutex directive */
880 #define SSL_CACHE_MUTEX_TYPE    "ssl-cache"
881 #define SSL_STAPLING_MUTEX_TYPE "ssl-stapling"
882
883 /**  Logfile Support  */
884 void         ssl_die(void);
885 void         ssl_log_ssl_error(const char *, int, int, server_rec *);
886
887 /* ssl_log_xerror, ssl_log_cxerror and ssl_log_rxerror are wrappers for the
888  * respective ap_log_*error functions and take a certificate as an
889  * additional argument (whose details are appended to the log message).
890  * The other arguments are interpreted exactly as with their ap_log_*error
891  * counterparts. */
892 void ssl_log_xerror(const char *file, int line, int level,
893                     apr_status_t rv, apr_pool_t *p, server_rec *s,
894                     X509 *cert, const char *format, ...)
895     __attribute__((format(printf,8,9)));
896
897 void ssl_log_cxerror(const char *file, int line, int level,
898                      apr_status_t rv, conn_rec *c, X509 *cert,
899                      const char *format, ...)
900     __attribute__((format(printf,7,8)));
901
902 void ssl_log_rxerror(const char *file, int line, int level,
903                      apr_status_t rv, request_rec *r, X509 *cert,
904                      const char *format, ...)
905     __attribute__((format(printf,7,8)));
906
907 #define SSLLOG_MARK              __FILE__,__LINE__
908
909 /**  Variables  */
910
911 /* Register variables for the lifetime of the process pool 'p'. */
912 void         ssl_var_register(apr_pool_t *p);
913 char        *ssl_var_lookup(apr_pool_t *, server_rec *, conn_rec *, request_rec *, char *);
914 apr_array_header_t *ssl_ext_list(apr_pool_t *p, conn_rec *c, int peer, const char *extension);
915
916 void         ssl_var_log_config_register(apr_pool_t *p);
917
918 /* Extract SSL_*_DN_* variables into table 't' from SSL object 'ssl',
919  * allocating from 'p': */
920 void modssl_var_extract_dns(apr_table_t *t, SSL *ssl, apr_pool_t *p);
921
922 #ifndef OPENSSL_NO_OCSP
923 /* Perform OCSP validation of the current cert in the given context.
924  * Returns non-zero on success or zero on failure.  On failure, the
925  * context error code is set. */
926 int modssl_verify_ocsp(X509_STORE_CTX *ctx, SSLSrvConfigRec *sc,
927                        server_rec *s, conn_rec *c, apr_pool_t *pool);
928
929 /* OCSP helper interface; dispatches the given OCSP request to the
930  * responder at the given URI.  Returns the decoded OCSP response
931  * object, or NULL on error (in which case, errors will have been
932  * logged).  Pool 'p' is used for temporary allocations. */
933 OCSP_RESPONSE *modssl_dispatch_ocsp_request(const apr_uri_t *uri,
934                                             apr_interval_time_t timeout,
935                                             OCSP_REQUEST *request,
936                                             conn_rec *c, apr_pool_t *p);
937 #endif
938
939 #endif /* SSL_PRIVATE_H */
940 /** @} */
941