1 | /** |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * |
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
5 | * you may not use this file except in compliance with the License. |
---|
6 | * 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 | |
---|
18 | /* Apache Runtime Headers */ |
---|
19 | #include "httpd.h" |
---|
20 | #include "http_config.h" |
---|
21 | #include "http_protocol.h" |
---|
22 | #include "http_connection.h" |
---|
23 | #include "http_request.h" |
---|
24 | #include "http_core.h" |
---|
25 | #include "http_log.h" |
---|
26 | #include "apr_buckets.h" |
---|
27 | #include "apr_strings.h" |
---|
28 | #include "apr_tables.h" |
---|
29 | #include "ap_release.h" |
---|
30 | #include "apr_fnmatch.h" |
---|
31 | /* GnuTLS Library Headers */ |
---|
32 | #include <gnutls/gnutls.h> |
---|
33 | #if GNUTLS_VERSION_MAJOR == 2 |
---|
34 | #include <gnutls/extra.h> |
---|
35 | #endif |
---|
36 | #include <gnutls/openpgp.h> |
---|
37 | #include <gnutls/x509.h> |
---|
38 | |
---|
39 | #ifndef __mod_gnutls_h_inc |
---|
40 | #define __mod_gnutls_h_inc |
---|
41 | |
---|
42 | #define HAVE_APR_MEMCACHE @have_apr_memcache@ |
---|
43 | |
---|
44 | extern module AP_MODULE_DECLARE_DATA gnutls_module; |
---|
45 | |
---|
46 | /* IO Filter names */ |
---|
47 | #define GNUTLS_OUTPUT_FILTER_NAME "gnutls_output_filter" |
---|
48 | #define GNUTLS_INPUT_FILTER_NAME "gnutls_input_filter" |
---|
49 | /* GnuTLS Constants */ |
---|
50 | #define GNUTLS_ENABLED_FALSE 0 |
---|
51 | #define GNUTLS_ENABLED_TRUE 1 |
---|
52 | /* Current module version */ |
---|
53 | #define MOD_GNUTLS_VERSION "0.5.10" |
---|
54 | /* Module Debug Mode */ |
---|
55 | #define MOD_GNUTLS_DEBUG 1 |
---|
56 | |
---|
57 | /* |
---|
58 | * Recent Versions of 2.1 renamed several hooks. |
---|
59 | * This allows us to compile on 2.0.xx |
---|
60 | */ |
---|
61 | #if AP_SERVER_MINORVERSION_NUMBER >= 2 || (AP_SERVER_MINORVERSION_NUMBER == 1 && AP_SERVER_PATCHLEVEL_NUMBER >= 3) |
---|
62 | #define USING_2_1_RECENT 1 |
---|
63 | #else |
---|
64 | #define USING_2_1_RECENT 0 |
---|
65 | #endif |
---|
66 | |
---|
67 | /* mod_gnutls Cache Types */ |
---|
68 | typedef enum { |
---|
69 | /* No Cache */ |
---|
70 | mgs_cache_none, |
---|
71 | /* Use Old Berkley DB */ |
---|
72 | mgs_cache_dbm, |
---|
73 | /* Use Gnu's version of Berkley DB */ |
---|
74 | mgs_cache_gdbm, |
---|
75 | #if HAVE_APR_MEMCACHE |
---|
76 | /* Use Memcache */ |
---|
77 | mgs_cache_memcache |
---|
78 | #endif |
---|
79 | } mgs_cache_e; |
---|
80 | |
---|
81 | /* Directory Configuration Record */ |
---|
82 | typedef struct { |
---|
83 | int client_verify_mode; |
---|
84 | const char* lua_bytecode; |
---|
85 | apr_size_t lua_bytecode_len; |
---|
86 | } mgs_dirconf_rec; |
---|
87 | |
---|
88 | |
---|
89 | /* The maximum number of certificates to send in a chain */ |
---|
90 | #define MAX_CHAIN_SIZE 8 |
---|
91 | /* The maximum number of SANs to read from a x509 certificate */ |
---|
92 | #define MAX_CERT_SAN 5 |
---|
93 | |
---|
94 | /* Server Configuration Record */ |
---|
95 | typedef struct { |
---|
96 | /* x509 Certificate Structure */ |
---|
97 | gnutls_certificate_credentials_t certs; |
---|
98 | /* SRP Certificate Structure*/ |
---|
99 | gnutls_srp_server_credentials_t srp_creds; |
---|
100 | /* Annonymous Certificate Structure */ |
---|
101 | gnutls_anon_server_credentials_t anon_creds; |
---|
102 | /* Current x509 Certificate CN [Common Name] */ |
---|
103 | char* cert_cn; |
---|
104 | /* Current x509 Certificate SAN [Subject Alternate Name]s*/ |
---|
105 | char* cert_san[MAX_CERT_SAN]; |
---|
106 | /* A x509 Certificate Chain */ |
---|
107 | gnutls_x509_crt_t *certs_x509_chain; |
---|
108 | /* Current x509 Certificate Private Key */ |
---|
109 | gnutls_x509_privkey_t privkey_x509; |
---|
110 | /* OpenPGP Certificate */ |
---|
111 | gnutls_openpgp_crt_t cert_pgp; |
---|
112 | /* OpenPGP Certificate Private Key */ |
---|
113 | gnutls_openpgp_privkey_t privkey_pgp; |
---|
114 | /* Number of Certificates in Chain */ |
---|
115 | unsigned int certs_x509_chain_num; |
---|
116 | /* Is the module enabled? */ |
---|
117 | int enabled; |
---|
118 | /* GnuTLS Priorities */ |
---|
119 | gnutls_priority_t priorities; |
---|
120 | /* GnuTLS RSA Parameters [Obselete] */ |
---|
121 | gnutls_rsa_params_t rsa_params; |
---|
122 | /* GnuTLS DH Parameters */ |
---|
123 | gnutls_dh_params_t dh_params; |
---|
124 | /* Cache timeout value */ |
---|
125 | int cache_timeout; |
---|
126 | /* Chose Cache Type */ |
---|
127 | mgs_cache_e cache_type; |
---|
128 | const char* cache_config; |
---|
129 | const char* srp_tpasswd_file; |
---|
130 | const char* srp_tpasswd_conf_file; |
---|
131 | /* A list of CA Certificates */ |
---|
132 | gnutls_x509_crt_t *ca_list; |
---|
133 | /* OpenPGP Key Ring */ |
---|
134 | gnutls_openpgp_keyring_t pgp_list; |
---|
135 | /* CA Certificate list size */ |
---|
136 | unsigned int ca_list_size; |
---|
137 | /* Client Certificate Verification Mode */ |
---|
138 | int client_verify_mode; |
---|
139 | /* Last Cache timestamp */ |
---|
140 | apr_time_t last_cache_check; |
---|
141 | /* GnuTLS uses Session Tickets */ |
---|
142 | int tickets; |
---|
143 | /* Is mod_proxy enabled? */ |
---|
144 | int proxy_enabled; |
---|
145 | /* A Plain HTTP request */ |
---|
146 | int non_ssl_request; |
---|
147 | } mgs_srvconf_rec; |
---|
148 | |
---|
149 | /* Character Buffer */ |
---|
150 | typedef struct { |
---|
151 | int length; |
---|
152 | char *value; |
---|
153 | } mgs_char_buffer_t; |
---|
154 | |
---|
155 | /* GnuTLS Handle */ |
---|
156 | typedef struct { |
---|
157 | /* Server configuration record */ |
---|
158 | mgs_srvconf_rec *sc; |
---|
159 | /* Connection record */ |
---|
160 | conn_rec* c; |
---|
161 | /* GnuTLS Session handle */ |
---|
162 | gnutls_session_t session; |
---|
163 | /* module input status */ |
---|
164 | apr_status_t input_rc; |
---|
165 | /* Input filter */ |
---|
166 | ap_filter_t *input_filter; |
---|
167 | /* Input Bucket Brigade */ |
---|
168 | apr_bucket_brigade *input_bb; |
---|
169 | /* Input Read Type */ |
---|
170 | apr_read_type_e input_block; |
---|
171 | /* Input Mode */ |
---|
172 | ap_input_mode_t input_mode; |
---|
173 | /* Input Character Buffer */ |
---|
174 | mgs_char_buffer_t input_cbuf; |
---|
175 | /* Input Character Array */ |
---|
176 | char input_buffer[AP_IOBUFSIZE]; |
---|
177 | /* module Output status */ |
---|
178 | apr_status_t output_rc; |
---|
179 | /* Output filter */ |
---|
180 | ap_filter_t *output_filter; |
---|
181 | /* Output Bucket Brigade */ |
---|
182 | apr_bucket_brigade *output_bb; |
---|
183 | /* Output character array */ |
---|
184 | char output_buffer[AP_IOBUFSIZE]; |
---|
185 | /* Output buffer length */ |
---|
186 | apr_size_t output_blen; |
---|
187 | /* Output length */ |
---|
188 | apr_size_t output_length; |
---|
189 | /* General Status */ |
---|
190 | int status; |
---|
191 | } mgs_handle_t; |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | /** Functions in gnutls_io.c **/ |
---|
196 | |
---|
197 | /* apr_signal_block() for blocking SIGPIPE */ |
---|
198 | apr_status_t apr_signal_block(int signum); |
---|
199 | |
---|
200 | /* Proxy Support */ |
---|
201 | /* An optional function which returns non-zero if the given connection |
---|
202 | is using SSL/TLS. */ |
---|
203 | APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *)); |
---|
204 | /* The ssl_proxy_enable() and ssl_engine_disable() optional functions |
---|
205 | * are used by mod_proxy to enable use of SSL for outgoing |
---|
206 | * connections. */ |
---|
207 | APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *)); |
---|
208 | APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *)); |
---|
209 | int ssl_is_https(conn_rec *c); |
---|
210 | int ssl_proxy_enable(conn_rec *c); |
---|
211 | int ssl_engine_disable(conn_rec *c); |
---|
212 | const char *mgs_set_proxy_engine(cmd_parms * parms, void *dummy, |
---|
213 | const char *arg); |
---|
214 | apr_status_t mgs_cleanup_pre_config(void *data); |
---|
215 | |
---|
216 | /** |
---|
217 | * mgs_filter_input will filter the input data |
---|
218 | * by decrypting it using GnuTLS and passes it cleartext. |
---|
219 | * |
---|
220 | * @param f the filter info record |
---|
221 | * @param bb the bucket brigade, where to store the result to |
---|
222 | * @param mode what shall we read? |
---|
223 | * @param block a block index we shall read from? |
---|
224 | * @return result status |
---|
225 | */ |
---|
226 | apr_status_t mgs_filter_input(ap_filter_t * f, |
---|
227 | apr_bucket_brigade * bb, |
---|
228 | ap_input_mode_t mode, |
---|
229 | apr_read_type_e block, |
---|
230 | apr_off_t readbytes); |
---|
231 | |
---|
232 | /** |
---|
233 | * mgs_filter_output will filter the encrypt |
---|
234 | * the incoming bucket using GnuTLS and passes it onto the next filter. |
---|
235 | * |
---|
236 | * @param f the filter info record |
---|
237 | * @param bb the bucket brigade, where to store the result to |
---|
238 | * @return result status |
---|
239 | */ |
---|
240 | apr_status_t mgs_filter_output(ap_filter_t * f, |
---|
241 | apr_bucket_brigade * bb); |
---|
242 | |
---|
243 | |
---|
244 | /** |
---|
245 | * mgs_transport_read is called from GnuTLS to provide encrypted |
---|
246 | * data from the client. |
---|
247 | * |
---|
248 | * @param ptr pointer to the filter context |
---|
249 | * @param buffer place to put data |
---|
250 | * @param len maximum size |
---|
251 | * @return size length of the data stored in buffer |
---|
252 | */ |
---|
253 | ssize_t mgs_transport_read(gnutls_transport_ptr_t ptr, |
---|
254 | void *buffer, size_t len); |
---|
255 | |
---|
256 | /** |
---|
257 | * mgs_transport_write is called from GnuTLS to |
---|
258 | * write data to the client. |
---|
259 | * |
---|
260 | * @param ptr pointer to the filter context |
---|
261 | * @param buffer buffer to write to the client |
---|
262 | * @param len size of the buffer |
---|
263 | * @return size length of the data written |
---|
264 | */ |
---|
265 | ssize_t mgs_transport_write(gnutls_transport_ptr_t ptr, |
---|
266 | const void *buffer, size_t len); |
---|
267 | |
---|
268 | |
---|
269 | int mgs_rehandshake(mgs_handle_t * ctxt); |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | /** |
---|
274 | * Init the Cache after Configuration is done |
---|
275 | */ |
---|
276 | int mgs_cache_post_config(apr_pool_t *p, server_rec *s, |
---|
277 | mgs_srvconf_rec *sc); |
---|
278 | /** |
---|
279 | * Init the Cache inside each Process |
---|
280 | */ |
---|
281 | int mgs_cache_child_init(apr_pool_t *p, server_rec *s, |
---|
282 | mgs_srvconf_rec *sc); |
---|
283 | /** |
---|
284 | * Setup the Session Caching |
---|
285 | */ |
---|
286 | int mgs_cache_session_init(mgs_handle_t *ctxt); |
---|
287 | |
---|
288 | #define GNUTLS_SESSION_ID_STRING_LEN \ |
---|
289 | ((GNUTLS_MAX_SESSION_ID + 1) * 2) |
---|
290 | |
---|
291 | /** |
---|
292 | * Convert a SSL Session ID into a Null Terminated Hex Encoded String |
---|
293 | * @param id raw SSL Session ID |
---|
294 | * @param idlen Length of the raw Session ID |
---|
295 | * @param str Location to store the Hex Encoded String |
---|
296 | * @param strsize The Maximum Length that can be stored in str |
---|
297 | */ |
---|
298 | char *mgs_session_id2sz(unsigned char *id, int idlen, |
---|
299 | char *str, int strsize); |
---|
300 | |
---|
301 | /** |
---|
302 | * Convert a time_t into a Null Terminated String |
---|
303 | * @param t time_t time |
---|
304 | * @param str Location to store the Hex Encoded String |
---|
305 | * @param strsize The Maximum Length that can be stored in str |
---|
306 | */ |
---|
307 | char *mgs_time2sz(time_t t, char *str, int strsize); |
---|
308 | |
---|
309 | |
---|
310 | /* Configuration Functions */ |
---|
311 | |
---|
312 | const char *mgs_set_srp_tpasswd_conf_file(cmd_parms * parms, void *dummy, |
---|
313 | const char *arg); |
---|
314 | const char *mgs_set_srp_tpasswd_file(cmd_parms * parms, void *dummy, |
---|
315 | const char *arg); |
---|
316 | const char *mgs_set_dh_file(cmd_parms * parms, void *dummy, |
---|
317 | const char *arg); |
---|
318 | const char *mgs_set_cert_file(cmd_parms * parms, void *dummy, |
---|
319 | const char *arg); |
---|
320 | |
---|
321 | const char *mgs_set_key_file(cmd_parms * parms, void *dummy, |
---|
322 | const char *arg); |
---|
323 | |
---|
324 | const char *mgs_set_pgpcert_file(cmd_parms * parms, void *dummy, |
---|
325 | const char *arg); |
---|
326 | |
---|
327 | const char *mgs_set_pgpkey_file(cmd_parms * parms, void *dummy, |
---|
328 | const char *arg); |
---|
329 | |
---|
330 | const char *mgs_set_cache(cmd_parms * parms, void *dummy, |
---|
331 | const char *type, const char* arg); |
---|
332 | |
---|
333 | const char *mgs_set_cache_timeout(cmd_parms * parms, void *dummy, |
---|
334 | const char *arg); |
---|
335 | |
---|
336 | const char *mgs_set_client_verify(cmd_parms * parms, void *dummy, |
---|
337 | const char *arg); |
---|
338 | |
---|
339 | const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy, |
---|
340 | const char *arg); |
---|
341 | |
---|
342 | const char *mgs_set_keyring_file(cmd_parms * parms, void *dummy, |
---|
343 | const char *arg); |
---|
344 | |
---|
345 | const char *mgs_set_enabled(cmd_parms * parms, void *dummy, |
---|
346 | const char *arg); |
---|
347 | const char *mgs_set_export_certificates_enabled(cmd_parms * parms, void *dummy, |
---|
348 | const char *arg); |
---|
349 | const char *mgs_set_priorities(cmd_parms * parms, void *dummy, |
---|
350 | const char *arg); |
---|
351 | const char *mgs_set_tickets(cmd_parms * parms, void *dummy, |
---|
352 | const char *arg); |
---|
353 | |
---|
354 | const char *mgs_set_require_section(cmd_parms *cmd, |
---|
355 | void *mconfig, const char *arg); |
---|
356 | void *mgs_config_server_create(apr_pool_t * p, server_rec * s); |
---|
357 | |
---|
358 | void *mgs_config_dir_merge(apr_pool_t *p, void *basev, void *addv); |
---|
359 | |
---|
360 | void *mgs_config_dir_create(apr_pool_t *p, char *dir); |
---|
361 | |
---|
362 | const char *mgs_set_require_bytecode(cmd_parms *cmd, |
---|
363 | void *mconfig, const char *arg); |
---|
364 | |
---|
365 | mgs_srvconf_rec* mgs_find_sni_server(gnutls_session_t session); |
---|
366 | |
---|
367 | /* mod_gnutls Hooks. */ |
---|
368 | |
---|
369 | int mgs_hook_pre_config(apr_pool_t * pconf, |
---|
370 | apr_pool_t * plog, apr_pool_t * ptemp); |
---|
371 | |
---|
372 | int mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog, |
---|
373 | apr_pool_t * ptemp, |
---|
374 | server_rec * base_server); |
---|
375 | |
---|
376 | void mgs_hook_child_init(apr_pool_t *p, server_rec *s); |
---|
377 | |
---|
378 | const char *mgs_hook_http_scheme(const request_rec * r); |
---|
379 | |
---|
380 | apr_port_t mgs_hook_default_port(const request_rec * r); |
---|
381 | |
---|
382 | int mgs_hook_pre_connection(conn_rec * c, void *csd); |
---|
383 | |
---|
384 | int mgs_hook_fixups(request_rec *r); |
---|
385 | |
---|
386 | int mgs_hook_authz(request_rec *r); |
---|
387 | |
---|
388 | #endif /* __mod_gnutls_h_inc */ |
---|