]> granicus.if.org Git - apache/blob - modules/ssl/ssl_private.h
Support for OpenSSL 1.1.0:
[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 #if 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/opensslv.h>
86 #if (OPENSSL_VERSION_NUMBER >= 0x10001000)
87 /* must be defined before including ssl.h */
88 #define OPENSSL_NO_SSL_INTERN
89 #endif
90 #include <openssl/ssl.h>
91 #include <openssl/err.h>
92 #include <openssl/x509.h>
93 #include <openssl/pem.h>
94 #include <openssl/crypto.h>
95 #include <openssl/evp.h>
96 #include <openssl/rand.h>
97 #include <openssl/x509v3.h>
98 #include <openssl/x509_vfy.h>
99 #include <openssl/ocsp.h>
100
101 /* Avoid tripping over an engine build installed globally and detected
102  * when the user points at an explicit non-engine flavor of OpenSSL
103  */
104 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
105 #include <openssl/engine.h>
106 #endif
107
108 #if (OPENSSL_VERSION_NUMBER < 0x0090801f)
109 #error mod_ssl requires OpenSSL 0.9.8a or later
110 #endif
111
112 /**
113  * ...shifting sands of OpenSSL...
114  * Note: when adding support for new OpenSSL features, avoid explicit
115  * version number checks whenever possible, and use "feature-based"
116  * detection instead (check for definitions of constants or functions)
117  */
118 #if (OPENSSL_VERSION_NUMBER >= 0x10000000)
119 #define MODSSL_SSL_CIPHER_CONST const
120 #define MODSSL_SSL_METHOD_CONST const
121 #else
122 #define MODSSL_SSL_CIPHER_CONST
123 #define MODSSL_SSL_METHOD_CONST
124 #endif
125
126 #if defined(OPENSSL_FIPS)
127 #define HAVE_FIPS
128 #endif
129
130 #if defined(SSL_OP_NO_TLSv1_2)
131 #define HAVE_TLSV1_X
132 #endif
133
134 #if defined(SSL_CONF_FLAG_FILE)
135 #define HAVE_SSL_CONF_CMD
136 #endif
137
138 /**
139   * The following features all depend on TLS extension support.
140   * Within this block, check again for features (not version numbers).
141   */
142 #if !defined(OPENSSL_NO_TLSEXT) && defined(SSL_set_tlsext_host_name)
143
144 #define HAVE_TLSEXT
145
146 /* ECC: make sure we have at least 1.0.0 */
147 #if !defined(OPENSSL_NO_EC) && defined(TLSEXT_ECPOINTFORMAT_uncompressed)
148 #define HAVE_ECC
149 #endif
150
151 /* OCSP stapling */
152 #if !defined(OPENSSL_NO_OCSP) && defined(SSL_CTX_set_tlsext_status_cb)
153 #define HAVE_OCSP_STAPLING
154 #if OPENSSL_VERSION_NUMBER < 0x10100000L
155 /* backward compatibility with OpenSSL < 1.0 */
156 #ifndef sk_OPENSSL_STRING_num
157 #define sk_OPENSSL_STRING_num sk_num
158 #endif
159 #ifndef sk_OPENSSL_STRING_value
160 #define sk_OPENSSL_STRING_value sk_value
161 #endif
162 #ifndef sk_OPENSSL_STRING_pop
163 #define sk_OPENSSL_STRING_pop sk_pop
164 #endif
165 #endif /* if OPENSSL_VERSION_NUMBER < 0x10100000L */
166 #endif /* if !defined(OPENSSL_NO_OCSP) && defined(SSL_CTX_set_tlsext_status_cb) */
167
168 /* TLS session tickets */
169 #if defined(SSL_CTX_set_tlsext_ticket_key_cb)
170 #define HAVE_TLS_SESSION_TICKETS
171 #define TLSEXT_TICKET_KEY_LEN 48
172 #ifndef tlsext_tick_md
173 #ifdef OPENSSL_NO_SHA256
174 #define tlsext_tick_md EVP_sha1
175 #else
176 #define tlsext_tick_md EVP_sha256
177 #endif
178 #endif
179 #endif
180
181 /* Secure Remote Password */
182 #if !defined(OPENSSL_NO_SRP) && defined(SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB)
183 #define HAVE_SRP
184 #include <openssl/srp.h>
185 #endif
186
187 /* ALPN Protocol Negotiation */
188 #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
189 #define HAVE_TLS_ALPN
190 #endif
191
192 #endif /* !defined(OPENSSL_NO_TLSEXT) && defined(SSL_set_tlsext_host_name) */
193
194 /* mod_ssl headers */
195 #include "ssl_util_ssl.h"
196
197 APLOG_USE_MODULE(ssl);
198
199 /*
200  * Provide reasonable default for some defines
201  */
202 #ifndef PFALSE
203 #define PFALSE ((void *)FALSE)
204 #endif
205 #ifndef PTRUE
206 #define PTRUE ((void *)TRUE)
207 #endif
208 #ifndef UNSET
209 #define UNSET (-1)
210 #endif
211 #ifndef NUL
212 #define NUL '\0'
213 #endif
214 #ifndef RAND_MAX
215 #include <limits.h>
216 #define RAND_MAX INT_MAX
217 #endif
218
219 /**
220  * Provide reasonable defines for some types
221  */
222 #ifndef UCHAR
223 #define UCHAR unsigned char
224 #endif
225
226 /**
227  * Provide useful shorthands
228  */
229 #define strEQ(s1,s2)     (strcmp(s1,s2)        == 0)
230 #define strNE(s1,s2)     (strcmp(s1,s2)        != 0)
231 #define strEQn(s1,s2,n)  (strncmp(s1,s2,n)     == 0)
232 #define strNEn(s1,s2,n)  (strncmp(s1,s2,n)     != 0)
233
234 #define strcEQ(s1,s2)    (strcasecmp(s1,s2)    == 0)
235 #define strcNE(s1,s2)    (strcasecmp(s1,s2)    != 0)
236 #define strcEQn(s1,s2,n) (strncasecmp(s1,s2,n) == 0)
237 #define strcNEn(s1,s2,n) (strncasecmp(s1,s2,n) != 0)
238
239 #define strIsEmpty(s)    (s == NULL || s[0] == NUL)
240
241 #define myConnConfig(c) \
242 (SSLConnRec *)ap_get_module_config(c->conn_config, &ssl_module)
243 #define myCtxConfig(sslconn, sc) (sslconn->is_proxy ? sc->proxy : sc->server)
244 #define myConnConfigSet(c, val) \
245 ap_set_module_config(c->conn_config, &ssl_module, val)
246 #define mySrvConfig(srv) (SSLSrvConfigRec *)ap_get_module_config(srv->module_config,  &ssl_module)
247 #define myDirConfig(req) (SSLDirConfigRec *)ap_get_module_config(req->per_dir_config, &ssl_module)
248 #define myModConfig(srv) (mySrvConfig((srv)))->mc
249 #define mySrvFromConn(c) (myConnConfig(c))->server
250 #define mySrvConfigFromConn(c) mySrvConfig(mySrvFromConn(c))
251 #define myModConfigFromConn(c) myModConfig(mySrvFromConn(c))
252
253 /**
254  * Defaults for the configuration
255  */
256 #ifndef SSL_SESSION_CACHE_TIMEOUT
257 #define SSL_SESSION_CACHE_TIMEOUT  300
258 #endif
259
260 /* Default setting for per-dir reneg buffer. */
261 #ifndef DEFAULT_RENEG_BUFFER_SIZE
262 #define DEFAULT_RENEG_BUFFER_SIZE (128 * 1024)
263 #endif
264
265 /* Default for OCSP response validity */
266 #ifndef DEFAULT_OCSP_MAX_SKEW
267 #define DEFAULT_OCSP_MAX_SKEW (60 * 5)
268 #endif
269
270 /* Default timeout for OCSP queries */
271 #ifndef DEFAULT_OCSP_TIMEOUT
272 #define DEFAULT_OCSP_TIMEOUT 10
273 #endif
274
275 /**
276  * Define the SSL options
277  */
278 #define SSL_OPT_NONE           (0)
279 #define SSL_OPT_RELSET         (1<<0)
280 #define SSL_OPT_STDENVVARS     (1<<1)
281 #define SSL_OPT_EXPORTCERTDATA (1<<3)
282 #define SSL_OPT_FAKEBASICAUTH  (1<<4)
283 #define SSL_OPT_STRICTREQUIRE  (1<<5)
284 #define SSL_OPT_OPTRENEGOTIATE (1<<6)
285 #define SSL_OPT_LEGACYDNFORMAT (1<<7)
286 typedef int ssl_opt_t;
287
288 /**
289  * Define the SSL Protocol options
290  */
291 #define SSL_PROTOCOL_NONE  (0)
292 #ifndef OPENSSL_NO_SSL3
293 #define SSL_PROTOCOL_SSLV3 (1<<1)
294 #endif
295 #define SSL_PROTOCOL_TLSV1 (1<<2)
296 #ifndef OPENSSL_NO_SSL3
297 #define SSL_PROTOCOL_BASIC (SSL_PROTOCOL_SSLV3|SSL_PROTOCOL_TLSV1)
298 #else
299 #define SSL_PROTOCOL_BASIC (SSL_PROTOCOL_TLSV1)
300 #endif
301 #ifdef HAVE_TLSV1_X
302 #define SSL_PROTOCOL_TLSV1_1 (1<<3)
303 #define SSL_PROTOCOL_TLSV1_2 (1<<4)
304 #define SSL_PROTOCOL_ALL   (SSL_PROTOCOL_BASIC| \
305                             SSL_PROTOCOL_TLSV1_1|SSL_PROTOCOL_TLSV1_2)
306 #else
307 #define SSL_PROTOCOL_ALL   (SSL_PROTOCOL_BASIC)
308 #endif
309 #ifndef OPENSSL_NO_SSL3
310 #define SSL_PROTOCOL_DEFAULT (SSL_PROTOCOL_ALL & ~SSL_PROTOCOL_SSLV3)
311 #else
312 #define SSL_PROTOCOL_DEFAULT (SSL_PROTOCOL_ALL)
313 #endif
314 typedef int ssl_proto_t;
315
316 /**
317  * Define the SSL verify levels
318  */
319 typedef enum {
320     SSL_CVERIFY_UNSET           = UNSET,
321     SSL_CVERIFY_NONE            = 0,
322     SSL_CVERIFY_OPTIONAL        = 1,
323     SSL_CVERIFY_REQUIRE         = 2,
324     SSL_CVERIFY_OPTIONAL_NO_CA  = 3
325 } ssl_verify_t;
326
327 #define SSL_VERIFY_PEER_STRICT \
328      (SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
329
330 #define ssl_verify_error_is_optional(errnum) \
331    ((errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) \
332     || (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) \
333     || (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) \
334     || (errnum == X509_V_ERR_CERT_UNTRUSTED) \
335     || (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE))
336
337 /**
338   * CRL checking modes
339   */
340 typedef enum {
341     SSL_CRLCHECK_UNSET = UNSET,
342     SSL_CRLCHECK_NONE  = 0,
343     SSL_CRLCHECK_LEAF  = 1,
344     SSL_CRLCHECK_CHAIN = 2
345 } ssl_crlcheck_t;
346
347 /**
348  * Define the SSL pass phrase dialog types
349  */
350 typedef enum {
351     SSL_PPTYPE_UNSET   = UNSET,
352     SSL_PPTYPE_BUILTIN = 0,
353     SSL_PPTYPE_FILTER  = 1,
354     SSL_PPTYPE_PIPE    = 2
355 } ssl_pphrase_t;
356
357 /**
358  * Define the Path Checking modes
359  */
360 #define SSL_PCM_EXISTS     1
361 #define SSL_PCM_ISREG      2
362 #define SSL_PCM_ISDIR      4
363 #define SSL_PCM_ISNONZERO  8
364 typedef unsigned int ssl_pathcheck_t;
365
366 /**
367  * Define the SSL enabled state
368  */
369 typedef enum {
370     SSL_ENABLED_UNSET    = UNSET,
371     SSL_ENABLED_FALSE    = 0,
372     SSL_ENABLED_TRUE     = 1,
373     SSL_ENABLED_OPTIONAL = 3
374 } ssl_enabled_t;
375
376 /**
377  * Define the SSL requirement structure
378  */
379 typedef struct {
380     const char     *cpExpr;
381     ap_expr_info_t *mpExpr;
382 } ssl_require_t;
383
384 /**
385  * Define the SSL random number generator seeding source
386  */
387 typedef enum {
388     SSL_RSCTX_STARTUP = 1,
389     SSL_RSCTX_CONNECT = 2
390 } ssl_rsctx_t;
391 typedef enum {
392     SSL_RSSRC_BUILTIN = 1,
393     SSL_RSSRC_FILE    = 2,
394     SSL_RSSRC_EXEC    = 3,
395     SSL_RSSRC_EGD     = 4
396 } ssl_rssrc_t;
397 typedef struct {
398     ssl_rsctx_t  nCtx;
399     ssl_rssrc_t  nSrc;
400     char        *cpPath;
401     int          nBytes;
402 } ssl_randseed_t;
403
404 /**
405  * Define the structure of an ASN.1 anything
406  */
407 typedef struct {
408     long int       nData;
409     unsigned char *cpData;
410     apr_time_t     source_mtime;
411 } ssl_asn1_t;
412
413 /**
414  * Define the mod_ssl per-module configuration structure
415  * (i.e. the global configuration for each httpd process)
416  */
417
418 typedef enum {
419     SSL_SHUTDOWN_TYPE_UNSET,
420     SSL_SHUTDOWN_TYPE_STANDARD,
421     SSL_SHUTDOWN_TYPE_UNCLEAN,
422     SSL_SHUTDOWN_TYPE_ACCURATE
423 } ssl_shutdown_type_e;
424
425 typedef struct {
426     SSL *ssl;
427     const char *client_dn;
428     X509 *client_cert;
429     ssl_shutdown_type_e shutdown_type;
430     const char *verify_info;
431     const char *verify_error;
432     int verify_depth;
433     int is_proxy;
434     int disabled;
435     enum {
436         NON_SSL_OK = 0,        /* is SSL request, or error handling completed */
437         NON_SSL_SEND_REQLINE,  /* Need to send the fake request line */
438         NON_SSL_SEND_HDR_SEP,  /* Need to send the header separator */
439         NON_SSL_SET_ERROR_MSG  /* Need to set the error message */
440     } non_ssl_request;
441
442     /* Track the handshake/renegotiation state for the connection so
443      * that all client-initiated renegotiations can be rejected, as a
444      * partial fix for CVE-2009-3555. */
445     enum {
446         RENEG_INIT = 0, /* Before initial handshake */
447         RENEG_REJECT,   /* After initial handshake; any client-initiated
448                          * renegotiation should be rejected */
449         RENEG_ALLOW,    /* A server-initiated renegotiation is taking
450                          * place (as dictated by configuration) */
451 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
452         RENEG_STARTED,  /* A renegotiation has started after RENEG_ALLOW */
453         RENEG_DONE,     /* A renegotiation has finished after RENEG_STARTED */
454         RENEG_ALERT,    /* A renegotiation has finished with an SSL Alert */
455 #endif
456         RENEG_ABORT     /* Renegotiation initiated by client, abort the
457                          * connection */
458     } reneg_state;
459
460     server_rec *server;
461     
462     const char *cipher_suite; /* cipher suite used in last reneg */
463 } SSLConnRec;
464
465 /* BIG FAT WARNING: SSLModConfigRec has unusual memory lifetime: it is
466  * allocated out of the "process" pool and only a single such
467  * structure is created and used for the lifetime of the process.
468  * (The process pool is s->process->pool and is stored in the .pPool
469  * field.)  Most members of this structure are likewise allocated out
470  * of the process pool, but notably sesscache and sesscache_context
471  * are not.
472  *
473  * The structure is treated as mostly immutable after a single config
474  * parse has completed; the post_config hook (ssl_init_Module) flips
475  * the bFixed flag to true and subsequent invocations of the config
476  * callbacks hence do nothing.
477  *
478  * This odd lifetime strategy is used so that encrypted private keys
479  * can be decrypted once at startup and continue to be used across
480  * subsequent server reloads where the interactive password prompt is
481  * not possible.
482
483  * It is really an ABI nightmare waiting to happen since DSOs are
484  * reloaded across restarts, and nothing prevents the struct type
485  * changing across such reloads, yet the cached structure will be
486  * assumed to match regardless.
487  *
488  * This should really be fixed using a smaller structure which only
489  * stores that which is absolutely necessary (the private keys, maybe
490  * the random seed), and have that structure be strictly ABI-versioned
491  * for safety.
492  */
493 typedef struct {
494     pid_t           pid;
495     apr_pool_t     *pPool;
496     BOOL            bFixed;
497
498     /* OpenSSL SSL_SESS_CACHE_* flags: */
499     long            sesscache_mode;
500
501     /* The configured provider, and associated private data
502      * structure. */
503     const ap_socache_provider_t *sesscache;
504     ap_socache_instance_t *sesscache_context;
505
506     apr_global_mutex_t   *pMutex;
507     apr_array_header_t   *aRandSeed;
508     apr_hash_t     *tVHostKeys;
509
510     /* A hash table of pointers to ssl_asn1_t structures.  The structures
511      * are used to store private keys in raw DER format (serialized OpenSSL
512      * PrivateKey structures).  The table is indexed by (vhost-id,
513      * index), for example the string "vhost.example.com:443:0". */
514     apr_hash_t     *tPrivateKey;
515
516 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
517     const char     *szCryptoDevice;
518 #endif
519
520 #ifdef HAVE_OCSP_STAPLING
521     const ap_socache_provider_t *stapling_cache;
522     ap_socache_instance_t *stapling_cache_context;
523     apr_global_mutex_t   *stapling_cache_mutex;
524     apr_global_mutex_t   *stapling_refresh_mutex;
525 #endif
526 } SSLModConfigRec;
527
528 /** Structure representing configured filenames for certs and keys for
529  * a given vhost */
530 typedef struct {
531     /* Lists of configured certs and keys for this server */
532     apr_array_header_t *cert_files;
533     apr_array_header_t *key_files;
534
535     /** Certificates which specify the set of CA names which should be
536      * sent in the CertificateRequest message: */
537     const char  *ca_name_path;
538     const char  *ca_name_file;
539 } modssl_pk_server_t;
540
541 typedef struct {
542     /** proxy can have any number of cert/key pairs */
543     const char  *cert_file;
544     const char  *cert_path;
545     const char  *ca_cert_file;
546     STACK_OF(X509_INFO) *certs; /* Contains End Entity certs */
547     STACK_OF(X509) **ca_certs; /* Contains ONLY chain certs for
548                                 * each item in certs.
549                                 * (ptr to array of ptrs) */
550 } modssl_pk_proxy_t;
551
552 /** stuff related to authentication that can also be per-dir */
553 typedef struct {
554     /** known/trusted CAs */
555     const char  *ca_cert_path;
556     const char  *ca_cert_file;
557
558     const char  *cipher_suite;
559
560     /** for client or downstream server authentication */
561     int          verify_depth;
562     ssl_verify_t verify_mode;
563 } modssl_auth_ctx_t;
564
565 #ifdef HAVE_TLS_SESSION_TICKETS
566 typedef struct {
567     const char *file_path;
568     unsigned char key_name[16];
569     unsigned char hmac_secret[16];
570     unsigned char aes_key[16];
571 } modssl_ticket_key_t;
572 #endif
573
574 #ifdef HAVE_SSL_CONF_CMD
575 typedef struct {
576     const char *name;
577     const char *value;
578 } ssl_ctx_param_t;
579 #endif
580
581 typedef struct SSLSrvConfigRec SSLSrvConfigRec;
582
583 typedef struct {
584     SSLSrvConfigRec *sc; /** pointer back to server config */
585     SSL_CTX *ssl_ctx;
586
587     /** we are one or the other */
588     modssl_pk_server_t *pks;
589     modssl_pk_proxy_t  *pkp;
590
591 #ifdef HAVE_TLS_SESSION_TICKETS
592     modssl_ticket_key_t *ticket_key;
593 #endif
594
595     ssl_proto_t  protocol;
596     int protocol_set;
597
598     /** config for handling encrypted keys */
599     ssl_pphrase_t pphrase_dialog_type;
600     const char   *pphrase_dialog_path;
601
602     const char  *cert_chain;
603
604     /** certificate revocation list */
605     const char    *crl_path;
606     const char    *crl_file;
607     ssl_crlcheck_t crl_check_mode;
608
609 #ifdef HAVE_OCSP_STAPLING
610     /** OCSP stapling options */
611     BOOL        stapling_enabled;
612     long        stapling_resptime_skew;
613     long        stapling_resp_maxage;
614     int         stapling_cache_timeout;
615     BOOL        stapling_return_errors;
616     BOOL        stapling_fake_trylater;
617     int         stapling_errcache_timeout;
618     apr_interval_time_t stapling_responder_timeout;
619     const char *stapling_force_url;
620 #endif
621
622 #ifdef HAVE_SRP
623     char *srp_vfile;
624     char *srp_unknown_user_seed;
625     SRP_VBASE  *srp_vbase;
626 #endif
627
628     modssl_auth_ctx_t auth;
629
630     BOOL ocsp_enabled; /* true if OCSP verification enabled */
631     BOOL ocsp_force_default; /* true if the default responder URL is
632                               * used regardless of per-cert URL */
633     const char *ocsp_responder; /* default responder URL */
634     long ocsp_resptime_skew;
635     long ocsp_resp_maxage;
636     apr_interval_time_t ocsp_responder_timeout;
637     BOOL ocsp_use_request_nonce;
638     apr_uri_t *proxy_uri;
639
640 #ifdef HAVE_SSL_CONF_CMD
641     SSL_CONF_CTX *ssl_ctx_config; /* Configuration context */
642     apr_array_header_t *ssl_ctx_param; /* parameters to pass to SSL_CTX */
643 #endif
644 } modssl_ctx_t;
645
646 struct SSLSrvConfigRec {
647     SSLModConfigRec *mc;
648     ssl_enabled_t    enabled;
649     BOOL             proxy_enabled;
650     const char      *vhost_id;
651     int              vhost_id_len;
652     int              session_cache_timeout;
653     BOOL             cipher_server_pref;
654     BOOL             insecure_reneg;
655     modssl_ctx_t    *server;
656     modssl_ctx_t    *proxy;
657     ssl_enabled_t    proxy_ssl_check_peer_expire;
658     ssl_enabled_t    proxy_ssl_check_peer_cn;
659     ssl_enabled_t    proxy_ssl_check_peer_name;
660 #ifdef HAVE_TLSEXT
661     ssl_enabled_t    strict_sni_vhost_check;
662 #endif
663 #ifdef HAVE_FIPS
664     BOOL             fips;
665 #endif
666 #ifndef OPENSSL_NO_COMP
667     BOOL             compression;
668 #endif
669     BOOL             session_tickets;
670 };
671
672 /**
673  * Define the mod_ssl per-directory configuration structure
674  * (i.e. the local configuration for all &lt;Directory>
675  *  and .htaccess contexts)
676  */
677 typedef struct {
678     BOOL          bSSLRequired;
679     apr_array_header_t *aRequirement;
680     ssl_opt_t     nOptions;
681     ssl_opt_t     nOptionsAdd;
682     ssl_opt_t     nOptionsDel;
683     const char   *szCipherSuite;
684     ssl_verify_t  nVerifyClient;
685     int           nVerifyDepth;
686     const char   *szCACertificatePath;
687     const char   *szCACertificateFile;
688     const char   *szUserName;
689     apr_size_t    nRenegBufferSize;
690 } SSLDirConfigRec;
691
692 /**
693  *  function prototypes
694  */
695
696 /**  API glue structures  */
697 extern module AP_MODULE_DECLARE_DATA ssl_module;
698
699 /**  configuration handling   */
700 SSLModConfigRec *ssl_config_global_create(server_rec *);
701 void         ssl_config_global_fix(SSLModConfigRec *);
702 BOOL         ssl_config_global_isfixed(SSLModConfigRec *);
703 void        *ssl_config_server_create(apr_pool_t *, server_rec *);
704 void        *ssl_config_server_merge(apr_pool_t *, void *, void *);
705 void        *ssl_config_perdir_create(apr_pool_t *, char *);
706 void        *ssl_config_perdir_merge(apr_pool_t *, void *, void *);
707 const char  *ssl_cmd_SSLPassPhraseDialog(cmd_parms *, void *, const char *);
708 const char  *ssl_cmd_SSLCryptoDevice(cmd_parms *, void *, const char *);
709 const char  *ssl_cmd_SSLRandomSeed(cmd_parms *, void *, const char *, const char *, const char *);
710 const char  *ssl_cmd_SSLEngine(cmd_parms *, void *, const char *);
711 const char  *ssl_cmd_SSLCipherSuite(cmd_parms *, void *, const char *);
712 const char  *ssl_cmd_SSLCertificateFile(cmd_parms *, void *, const char *);
713 const char  *ssl_cmd_SSLCertificateKeyFile(cmd_parms *, void *, const char *);
714 const char  *ssl_cmd_SSLCertificateChainFile(cmd_parms *, void *, const char *);
715 const char  *ssl_cmd_SSLCACertificatePath(cmd_parms *, void *, const char *);
716 const char  *ssl_cmd_SSLCACertificateFile(cmd_parms *, void *, const char *);
717 const char  *ssl_cmd_SSLCADNRequestPath(cmd_parms *, void *, const char *);
718 const char  *ssl_cmd_SSLCADNRequestFile(cmd_parms *, void *, const char *);
719 const char  *ssl_cmd_SSLCARevocationPath(cmd_parms *, void *, const char *);
720 const char  *ssl_cmd_SSLCARevocationFile(cmd_parms *, void *, const char *);
721 const char  *ssl_cmd_SSLCARevocationCheck(cmd_parms *, void *, const char *);
722 const char  *ssl_cmd_SSLHonorCipherOrder(cmd_parms *cmd, void *dcfg, int flag);
723 const char  *ssl_cmd_SSLCompression(cmd_parms *, void *, int flag);
724 const char  *ssl_cmd_SSLSessionTickets(cmd_parms *, void *, int flag);
725 const char  *ssl_cmd_SSLVerifyClient(cmd_parms *, void *, const char *);
726 const char  *ssl_cmd_SSLVerifyDepth(cmd_parms *, void *, const char *);
727 const char  *ssl_cmd_SSLSessionCache(cmd_parms *, void *, const char *);
728 const char  *ssl_cmd_SSLSessionCacheTimeout(cmd_parms *, void *, const char *);
729 const char  *ssl_cmd_SSLProtocol(cmd_parms *, void *, const char *);
730 const char  *ssl_cmd_SSLOptions(cmd_parms *, void *, const char *);
731 const char  *ssl_cmd_SSLRequireSSL(cmd_parms *, void *);
732 const char  *ssl_cmd_SSLRequire(cmd_parms *, void *, const char *);
733 const char  *ssl_cmd_SSLUserName(cmd_parms *, void *, const char *);
734 const char  *ssl_cmd_SSLRenegBufferSize(cmd_parms *cmd, void *dcfg, const char *arg);
735 const char  *ssl_cmd_SSLStrictSNIVHostCheck(cmd_parms *cmd, void *dcfg, int flag);
736 const char *ssl_cmd_SSLInsecureRenegotiation(cmd_parms *cmd, void *dcfg, int flag);
737
738 const char  *ssl_cmd_SSLProxyEngine(cmd_parms *cmd, void *dcfg, int flag);
739 const char  *ssl_cmd_SSLProxyProtocol(cmd_parms *, void *, const char *);
740 const char  *ssl_cmd_SSLProxyCipherSuite(cmd_parms *, void *, const char *);
741 const char  *ssl_cmd_SSLProxyVerify(cmd_parms *, void *, const char *);
742 const char  *ssl_cmd_SSLProxyVerifyDepth(cmd_parms *, void *, const char *);
743 const char  *ssl_cmd_SSLProxyCACertificatePath(cmd_parms *, void *, const char *);
744 const char  *ssl_cmd_SSLProxyCACertificateFile(cmd_parms *, void *, const char *);
745 const char  *ssl_cmd_SSLProxyCARevocationPath(cmd_parms *, void *, const char *);
746 const char  *ssl_cmd_SSLProxyCARevocationFile(cmd_parms *, void *, const char *);
747 const char  *ssl_cmd_SSLProxyCARevocationCheck(cmd_parms *, void *, const char *);
748 const char  *ssl_cmd_SSLProxyMachineCertificatePath(cmd_parms *, void *, const char *);
749 const char  *ssl_cmd_SSLProxyMachineCertificateFile(cmd_parms *, void *, const char *);
750 const char  *ssl_cmd_SSLProxyMachineCertificateChainFile(cmd_parms *, void *, const char *);
751 #ifdef HAVE_TLS_SESSION_TICKETS
752 const char *ssl_cmd_SSLSessionTicketKeyFile(cmd_parms *cmd, void *dcfg, const char *arg);
753 #endif
754 const char  *ssl_cmd_SSLProxyCheckPeerExpire(cmd_parms *cmd, void *dcfg, int flag);
755 const char  *ssl_cmd_SSLProxyCheckPeerCN(cmd_parms *cmd, void *dcfg, int flag);
756 const char  *ssl_cmd_SSLProxyCheckPeerName(cmd_parms *cmd, void *dcfg, int flag);
757
758 const char *ssl_cmd_SSLOCSPOverrideResponder(cmd_parms *cmd, void *dcfg, int flag);
759 const char *ssl_cmd_SSLOCSPDefaultResponder(cmd_parms *cmd, void *dcfg, const char *arg);
760 const char *ssl_cmd_SSLOCSPResponseTimeSkew(cmd_parms *cmd, void *dcfg, const char *arg);
761 const char *ssl_cmd_SSLOCSPResponseMaxAge(cmd_parms *cmd, void *dcfg, const char *arg);
762 const char *ssl_cmd_SSLOCSPResponderTimeout(cmd_parms *cmd, void *dcfg, const char *arg);
763 const char *ssl_cmd_SSLOCSPUseRequestNonce(cmd_parms *cmd, void *dcfg, int flag);
764 const char *ssl_cmd_SSLOCSPEnable(cmd_parms *cmd, void *dcfg, int flag);
765 const char *ssl_cmd_SSLOCSPProxyURL(cmd_parms *cmd, void *dcfg, const char *arg);
766
767 #ifdef HAVE_SSL_CONF_CMD
768 const char *ssl_cmd_SSLOpenSSLConfCmd(cmd_parms *cmd, void *dcfg, const char *arg1, const char *arg2);
769 #endif
770
771 #ifdef HAVE_SRP
772 const char *ssl_cmd_SSLSRPVerifierFile(cmd_parms *cmd, void *dcfg, const char *arg);
773 const char *ssl_cmd_SSLSRPUnknownUserSeed(cmd_parms *cmd, void *dcfg, const char *arg);
774 #endif
775
776 const char *ssl_cmd_SSLFIPS(cmd_parms *cmd, void *dcfg, int flag);
777
778 /**  module initialization  */
779 apr_status_t ssl_init_Module(apr_pool_t *, apr_pool_t *, apr_pool_t *, server_rec *);
780 apr_status_t ssl_init_Engine(server_rec *, apr_pool_t *);
781 apr_status_t ssl_init_ConfigureServer(server_rec *, apr_pool_t *, apr_pool_t *, SSLSrvConfigRec *,
782                                       apr_array_header_t *);
783 apr_status_t ssl_init_CheckServers(server_rec *, apr_pool_t *);
784 STACK_OF(X509_NAME)
785             *ssl_init_FindCAList(server_rec *, apr_pool_t *, const char *, const char *);
786 void         ssl_init_Child(apr_pool_t *, server_rec *);
787 apr_status_t ssl_init_ModuleKill(void *data);
788
789 /**  Apache API hooks  */
790 int          ssl_hook_Auth(request_rec *);
791 int          ssl_hook_UserCheck(request_rec *);
792 int          ssl_hook_Access(request_rec *);
793 int          ssl_hook_Fixup(request_rec *);
794 int          ssl_hook_ReadReq(request_rec *);
795 int          ssl_hook_Upgrade(request_rec *);
796 void         ssl_hook_ConfigTest(apr_pool_t *pconf, server_rec *s);
797
798 /** Apache authz provisders */
799 extern const authz_provider ssl_authz_provider_require_ssl;
800 extern const authz_provider ssl_authz_provider_verify_client;
801
802 /**  OpenSSL callbacks */
803 DH          *ssl_callback_TmpDH(SSL *, int, int);
804 int          ssl_callback_SSLVerify(int, X509_STORE_CTX *);
805 int          ssl_callback_SSLVerify_CRL(int, X509_STORE_CTX *, conn_rec *);
806 int          ssl_callback_proxy_cert(SSL *ssl, X509 **x509, EVP_PKEY **pkey);
807 int          ssl_callback_NewSessionCacheEntry(SSL *, SSL_SESSION *);
808 SSL_SESSION *ssl_callback_GetSessionCacheEntry(SSL *, unsigned char *, int, int *);
809 void         ssl_callback_DelSessionCacheEntry(SSL_CTX *, SSL_SESSION *);
810 void         ssl_callback_Info(const SSL *, int, int);
811 #ifdef HAVE_TLSEXT
812 int          ssl_callback_ServerNameIndication(SSL *, int *, modssl_ctx_t *);
813 #endif
814 #ifdef HAVE_TLS_SESSION_TICKETS
815 int         ssl_callback_SessionTicket(SSL *, unsigned char *, unsigned char *,
816                                        EVP_CIPHER_CTX *, HMAC_CTX *, int);
817 #endif
818
819 #ifdef HAVE_TLS_ALPN
820 int ssl_callback_alpn_select(SSL *ssl, const unsigned char **out,
821                              unsigned char *outlen, const unsigned char *in,
822                              unsigned int inlen, void *arg);
823 #endif
824
825 /**  Session Cache Support  */
826 apr_status_t ssl_scache_init(server_rec *, apr_pool_t *);
827 void         ssl_scache_status_register(apr_pool_t *p);
828 void         ssl_scache_kill(server_rec *);
829 BOOL         ssl_scache_store(server_rec *, UCHAR *, int,
830                               apr_time_t, SSL_SESSION *, apr_pool_t *);
831 SSL_SESSION *ssl_scache_retrieve(server_rec *, UCHAR *, int, apr_pool_t *);
832 void         ssl_scache_remove(server_rec *, UCHAR *, int,
833                                apr_pool_t *);
834
835 /** OCSP Stapling Support */
836 #ifdef HAVE_OCSP_STAPLING
837 const char *ssl_cmd_SSLStaplingCache(cmd_parms *, void *, const char *);
838 const char *ssl_cmd_SSLUseStapling(cmd_parms *, void *, int);
839 const char *ssl_cmd_SSLStaplingResponseTimeSkew(cmd_parms *, void *, const char *);
840 const char *ssl_cmd_SSLStaplingResponseMaxAge(cmd_parms *, void *, const char *);
841 const char *ssl_cmd_SSLStaplingStandardCacheTimeout(cmd_parms *, void *, const char *);
842 const char *ssl_cmd_SSLStaplingErrorCacheTimeout(cmd_parms *, void *, const char *);
843 const char *ssl_cmd_SSLStaplingReturnResponderErrors(cmd_parms *, void *, int);
844 const char *ssl_cmd_SSLStaplingFakeTryLater(cmd_parms *, void *, int);
845 const char *ssl_cmd_SSLStaplingResponderTimeout(cmd_parms *, void *, const char *);
846 const char *ssl_cmd_SSLStaplingForceURL(cmd_parms *, void *, const char *);
847 apr_status_t modssl_init_stapling(server_rec *, apr_pool_t *, apr_pool_t *, modssl_ctx_t *);
848 void         ssl_stapling_certinfo_hash_init(apr_pool_t *);
849 int          ssl_stapling_init_cert(server_rec *, apr_pool_t *, apr_pool_t *,
850                                     modssl_ctx_t *, X509 *);
851 #endif
852 #ifdef HAVE_SRP
853 int          ssl_callback_SRPServerParams(SSL *, int *, void *);
854 #endif
855
856 /**  I/O  */
857 void         ssl_io_filter_init(conn_rec *, request_rec *r, SSL *);
858 void         ssl_io_filter_register(apr_pool_t *);
859 long         ssl_io_data_cb(BIO *, int, const char *, int, long, long);
860
861 /* ssl_io_buffer_fill fills the setaside buffering of the HTTP request
862  * to allow an SSL renegotiation to take place. */
863 int          ssl_io_buffer_fill(request_rec *r, apr_size_t maxlen);
864
865 /**  PRNG  */
866 int          ssl_rand_seed(server_rec *, apr_pool_t *, ssl_rsctx_t, char *);
867
868 /**  Utility Functions  */
869 char        *ssl_util_vhostid(apr_pool_t *, server_rec *);
870 apr_file_t  *ssl_util_ppopen(server_rec *, apr_pool_t *, const char *,
871                              const char * const *);
872 void         ssl_util_ppclose(server_rec *, apr_pool_t *, apr_file_t *);
873 char        *ssl_util_readfilter(server_rec *, apr_pool_t *, const char *,
874                                  const char * const *);
875 BOOL         ssl_util_path_check(ssl_pathcheck_t, const char *, apr_pool_t *);
876 void         ssl_util_thread_setup(apr_pool_t *);
877 int          ssl_init_ssl_connection(conn_rec *c, request_rec *r);
878
879 BOOL         ssl_util_vhost_matches(const char *servername, server_rec *s);
880
881 /**  Pass Phrase Support  */
882 apr_status_t ssl_load_encrypted_pkey(server_rec *, apr_pool_t *, int,
883                                      const char *, apr_array_header_t **);
884
885 /**  Diffie-Hellman Parameter Support  */
886 DH           *ssl_dh_GetParamFromFile(const char *);
887 #ifdef HAVE_ECC
888 EC_GROUP     *ssl_ec_GetParamFromFile(const char *);
889 #endif
890
891 unsigned char *ssl_asn1_table_set(apr_hash_t *table,
892                                   const char *key,
893                                   long int length);
894
895 ssl_asn1_t *ssl_asn1_table_get(apr_hash_t *table,
896                                const char *key);
897
898 void ssl_asn1_table_unset(apr_hash_t *table,
899                           const char *key);
900
901 /**  Mutex Support  */
902 int          ssl_mutex_init(server_rec *, apr_pool_t *);
903 int          ssl_mutex_reinit(server_rec *, apr_pool_t *);
904 int          ssl_mutex_on(server_rec *);
905 int          ssl_mutex_off(server_rec *);
906
907 int          ssl_stapling_mutex_reinit(server_rec *, apr_pool_t *);
908
909 /* mutex type names for Mutex directive */
910 #define SSL_CACHE_MUTEX_TYPE    "ssl-cache"
911 #define SSL_STAPLING_CACHE_MUTEX_TYPE "ssl-stapling"
912 #define SSL_STAPLING_REFRESH_MUTEX_TYPE "ssl-stapling-refresh"
913
914 apr_status_t ssl_die(server_rec *);
915
916 /**  Logfile Support  */
917 void         ssl_log_ssl_error(const char *, int, int, server_rec *);
918
919 /* ssl_log_xerror, ssl_log_cxerror and ssl_log_rxerror are wrappers for the
920  * respective ap_log_*error functions and take a certificate as an
921  * additional argument (whose details are appended to the log message).
922  * The other arguments are interpreted exactly as with their ap_log_*error
923  * counterparts. */
924 void ssl_log_xerror(const char *file, int line, int level,
925                     apr_status_t rv, apr_pool_t *p, server_rec *s,
926                     X509 *cert, const char *format, ...)
927     __attribute__((format(printf,8,9)));
928
929 void ssl_log_cxerror(const char *file, int line, int level,
930                      apr_status_t rv, conn_rec *c, X509 *cert,
931                      const char *format, ...)
932     __attribute__((format(printf,7,8)));
933
934 void ssl_log_rxerror(const char *file, int line, int level,
935                      apr_status_t rv, request_rec *r, X509 *cert,
936                      const char *format, ...)
937     __attribute__((format(printf,7,8)));
938
939 #define SSLLOG_MARK              __FILE__,__LINE__
940
941 /**  Variables  */
942
943 /* Register variables for the lifetime of the process pool 'p'. */
944 void         ssl_var_register(apr_pool_t *p);
945 char        *ssl_var_lookup(apr_pool_t *, server_rec *, conn_rec *, request_rec *, char *);
946 apr_array_header_t *ssl_ext_list(apr_pool_t *p, conn_rec *c, int peer, const char *extension);
947
948 void         ssl_var_log_config_register(apr_pool_t *p);
949
950 /* Extract SSL_*_DN_* variables into table 't' from SSL object 'ssl',
951  * allocating from 'p': */
952 void modssl_var_extract_dns(apr_table_t *t, SSL *ssl, apr_pool_t *p);
953
954 /* Extract SSL_*_SAN_* variables (subjectAltName entries) into table 't'
955  * from SSL object 'ssl', allocating from 'p'. */
956 void modssl_var_extract_san_entries(apr_table_t *t, SSL *ssl, apr_pool_t *p);
957
958 #ifndef OPENSSL_NO_OCSP
959 /* Perform OCSP validation of the current cert in the given context.
960  * Returns non-zero on success or zero on failure.  On failure, the
961  * context error code is set. */
962 int modssl_verify_ocsp(X509_STORE_CTX *ctx, SSLSrvConfigRec *sc,
963                        server_rec *s, conn_rec *c, apr_pool_t *pool);
964
965 /* OCSP helper interface; dispatches the given OCSP request to the
966  * responder at the given URI.  Returns the decoded OCSP response
967  * object, or NULL on error (in which case, errors will have been
968  * logged).  Pool 'p' is used for temporary allocations. */
969 OCSP_RESPONSE *modssl_dispatch_ocsp_request(const apr_uri_t *uri,
970                                             apr_interval_time_t timeout,
971                                             OCSP_REQUEST *request,
972                                             conn_rec *c, apr_pool_t *p);
973 #endif
974
975 /* Retrieve DH parameters for given key length.  Return value should
976  * be treated as unmutable, since it is stored in process-global
977  * memory. */
978 DH *modssl_get_dh_params(unsigned keylen);
979
980 #if HAVE_VALGRIND
981 extern int ssl_running_on_valgrind;
982 #endif
983
984 #endif /* SSL_PRIVATE_H */
985 /** @} */
986