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