1 | /** |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
4 | * Copyright 2011 Dash Shendy |
---|
5 | * Copyright 2013-2014 Daniel Kahn Gillmor |
---|
6 | * |
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
8 | * you may not use this file except in compliance with the License. |
---|
9 | * You may obtain a copy of the License at |
---|
10 | * |
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
12 | * |
---|
13 | * Unless required by applicable law or agreed to in writing, software |
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
16 | * See the License for the specific language governing permissions and |
---|
17 | * limitations under the License. |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | #include "mod_gnutls.h" |
---|
22 | #include "http_vhost.h" |
---|
23 | #include "ap_mpm.h" |
---|
24 | #include "mod_status.h" |
---|
25 | |
---|
26 | #ifdef ENABLE_MSVA |
---|
27 | #include <msv/msv.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | #ifdef APLOG_USE_MODULE |
---|
31 | APLOG_USE_MODULE(gnutls); |
---|
32 | #endif |
---|
33 | |
---|
34 | #if !USING_2_1_RECENT |
---|
35 | extern server_rec *ap_server_conf; |
---|
36 | #endif |
---|
37 | |
---|
38 | #if MOD_GNUTLS_DEBUG |
---|
39 | static apr_file_t *debug_log_fp; |
---|
40 | #endif |
---|
41 | |
---|
42 | static gnutls_datum_t session_ticket_key = {NULL, 0}; |
---|
43 | |
---|
44 | static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt); |
---|
45 | /* use side==0 for server and side==1 for client */ |
---|
46 | static void mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, int side, size_t export_cert_size); |
---|
47 | static void mgs_add_common_pgpcert_vars(request_rec * r, gnutls_openpgp_crt_t cert, int side, size_t export_cert_size); |
---|
48 | static int mgs_status_hook(request_rec *r, int flags); |
---|
49 | #ifdef ENABLE_MSVA |
---|
50 | static const char* mgs_x509_construct_uid(request_rec * pool, gnutls_x509_crt_t cert); |
---|
51 | #endif |
---|
52 | |
---|
53 | /* Pool Cleanup Function */ |
---|
54 | apr_status_t mgs_cleanup_pre_config(void *data __attribute__((unused))) { |
---|
55 | /* Free all session data */ |
---|
56 | gnutls_free(session_ticket_key.data); |
---|
57 | session_ticket_key.data = NULL; |
---|
58 | session_ticket_key.size = 0; |
---|
59 | /* Deinitialize GnuTLS Library */ |
---|
60 | gnutls_global_deinit(); |
---|
61 | return APR_SUCCESS; |
---|
62 | } |
---|
63 | |
---|
64 | /* Logging Function for Maintainers */ |
---|
65 | #if MOD_GNUTLS_DEBUG |
---|
66 | static void gnutls_debug_log_all(int level, const char *str) { |
---|
67 | apr_file_printf(debug_log_fp, "<%d> %s", level, str); |
---|
68 | } |
---|
69 | #define _gnutls_log apr_file_printf |
---|
70 | #else |
---|
71 | #define _gnutls_log(...) |
---|
72 | #endif |
---|
73 | |
---|
74 | static const char* mgs_readable_cvm(mgs_client_verification_method_e m) { |
---|
75 | switch(m) { |
---|
76 | case mgs_cvm_unset: |
---|
77 | return "unset"; |
---|
78 | case mgs_cvm_cartel: |
---|
79 | return "cartel"; |
---|
80 | case mgs_cvm_msva: |
---|
81 | return "msva"; |
---|
82 | } |
---|
83 | return "unknown"; |
---|
84 | } |
---|
85 | |
---|
86 | /* Pre-Configuration HOOK: Runs First */ |
---|
87 | int mgs_hook_pre_config(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp __attribute__((unused))) { |
---|
88 | |
---|
89 | /* Maintainer Logging */ |
---|
90 | #if MOD_GNUTLS_DEBUG |
---|
91 | apr_file_open(&debug_log_fp, "/tmp/gnutls_debug", APR_APPEND | APR_WRITE | APR_CREATE, APR_OS_DEFAULT, pconf); |
---|
92 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
93 | gnutls_global_set_log_level(9); |
---|
94 | gnutls_global_set_log_function(gnutls_debug_log_all); |
---|
95 | _gnutls_log(debug_log_fp, "gnutls: %s\n", gnutls_check_version(NULL)); |
---|
96 | #endif |
---|
97 | |
---|
98 | int ret; |
---|
99 | |
---|
100 | /* Check for required GnuTLS Library Version */ |
---|
101 | if (gnutls_check_version(LIBGNUTLS_VERSION) == NULL) { |
---|
102 | ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "gnutls_check_version() failed. Required: " |
---|
103 | "gnutls-%s Found: gnutls-%s", LIBGNUTLS_VERSION, gnutls_check_version(NULL)); |
---|
104 | return DONE; |
---|
105 | } |
---|
106 | |
---|
107 | /* Initialize GnuTLS Library */ |
---|
108 | ret = gnutls_global_init(); |
---|
109 | if (ret < 0) { |
---|
110 | ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "gnutls_global_init: %s", gnutls_strerror(ret)); |
---|
111 | return DONE; |
---|
112 | } |
---|
113 | |
---|
114 | /* Generate a Session Key */ |
---|
115 | ret = gnutls_session_ticket_key_generate(&session_ticket_key); |
---|
116 | if (ret < 0) { |
---|
117 | ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "gnutls_session_ticket_key_generate: %s", gnutls_strerror(ret)); |
---|
118 | return DONE; |
---|
119 | } |
---|
120 | |
---|
121 | AP_OPTIONAL_HOOK(status_hook, mgs_status_hook, NULL, NULL, APR_HOOK_MIDDLE); |
---|
122 | |
---|
123 | /* Register a pool clean-up function */ |
---|
124 | apr_pool_cleanup_register(pconf, NULL, mgs_cleanup_pre_config, apr_pool_cleanup_null); |
---|
125 | |
---|
126 | return OK; |
---|
127 | } |
---|
128 | |
---|
129 | static int mgs_select_virtual_server_cb(gnutls_session_t session) { |
---|
130 | |
---|
131 | mgs_handle_t *ctxt = NULL; |
---|
132 | mgs_srvconf_rec *tsc = NULL; |
---|
133 | int ret = 0; |
---|
134 | |
---|
135 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
136 | |
---|
137 | ctxt = gnutls_transport_get_ptr(session); |
---|
138 | |
---|
139 | /* find the virtual server */ |
---|
140 | tsc = mgs_find_sni_server(session); |
---|
141 | |
---|
142 | if (tsc != NULL) { |
---|
143 | // Found a TLS vhost based on the SNI from the client; use it instead. |
---|
144 | ctxt->sc = tsc; |
---|
145 | } |
---|
146 | |
---|
147 | gnutls_certificate_server_set_request(session, ctxt->sc->client_verify_mode); |
---|
148 | |
---|
149 | /* Set Anon credentials */ |
---|
150 | gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, ctxt->sc->certs); |
---|
151 | /* Set x509 credentials */ |
---|
152 | gnutls_credentials_set(session, GNUTLS_CRD_ANON, ctxt->sc->anon_creds); |
---|
153 | |
---|
154 | #ifdef ENABLE_SRP |
---|
155 | /* Set SRP credentials */ |
---|
156 | if (ctxt->sc->srp_tpasswd_conf_file != NULL && ctxt->sc->srp_tpasswd_file != NULL) { |
---|
157 | gnutls_credentials_set(session, GNUTLS_CRD_SRP, ctxt->sc->srp_creds); |
---|
158 | } |
---|
159 | #endif |
---|
160 | |
---|
161 | /* update the priorities - to avoid negotiating a ciphersuite that is not |
---|
162 | * enabled on this virtual server. Note that here we ignore the version |
---|
163 | * negotiation. |
---|
164 | */ |
---|
165 | |
---|
166 | ret = gnutls_priority_set(session, ctxt->sc->priorities); |
---|
167 | /* actually it shouldn't fail since we have checked at startup */ |
---|
168 | return ret; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | static int cert_retrieve_fn(gnutls_session_t session, |
---|
173 | const gnutls_datum_t * req_ca_rdn __attribute__((unused)), |
---|
174 | int nreqs __attribute__((unused)), |
---|
175 | const gnutls_pk_algorithm_t * pk_algos __attribute__((unused)), |
---|
176 | int pk_algos_length __attribute__((unused)), |
---|
177 | gnutls_pcert_st **pcerts, |
---|
178 | unsigned int *pcert_length, |
---|
179 | gnutls_privkey_t *privkey) |
---|
180 | { |
---|
181 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
182 | |
---|
183 | mgs_handle_t *ctxt; |
---|
184 | |
---|
185 | if (session == NULL) { |
---|
186 | // ERROR INVALID SESSION |
---|
187 | return -1; |
---|
188 | } |
---|
189 | |
---|
190 | ctxt = gnutls_transport_get_ptr(session); |
---|
191 | |
---|
192 | if (gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) { |
---|
193 | // X509 CERTIFICATE |
---|
194 | *pcerts = ctxt->sc->certs_x509_chain; |
---|
195 | *pcert_length = ctxt->sc->certs_x509_chain_num; |
---|
196 | *privkey = ctxt->sc->privkey_x509; |
---|
197 | return 0; |
---|
198 | } else if (gnutls_certificate_type_get(session) == GNUTLS_CRT_OPENPGP) { |
---|
199 | // OPENPGP CERTIFICATE |
---|
200 | *pcerts = ctxt->sc->cert_pgp; |
---|
201 | *pcert_length = 1; |
---|
202 | *privkey = ctxt->sc->privkey_pgp; |
---|
203 | return 0; |
---|
204 | } else { |
---|
205 | // UNKNOWN CERTIFICATE |
---|
206 | return -1; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | /* Read the common name or the alternative name of the certificate. |
---|
211 | * We only support a single name per certificate. |
---|
212 | * |
---|
213 | * Returns negative on error. |
---|
214 | */ |
---|
215 | static int read_crt_cn(server_rec * s, apr_pool_t * p, gnutls_x509_crt_t cert, char **cert_cn) { |
---|
216 | |
---|
217 | int rv = 0, i; |
---|
218 | size_t data_len; |
---|
219 | |
---|
220 | |
---|
221 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
222 | *cert_cn = NULL; |
---|
223 | |
---|
224 | data_len = 0; |
---|
225 | rv = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0, NULL, &data_len); |
---|
226 | |
---|
227 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) { |
---|
228 | *cert_cn = apr_palloc(p, data_len); |
---|
229 | rv = gnutls_x509_crt_get_dn_by_oid(cert, |
---|
230 | GNUTLS_OID_X520_COMMON_NAME, |
---|
231 | 0, 0, *cert_cn, |
---|
232 | &data_len); |
---|
233 | } else { /* No CN return subject alternative name */ |
---|
234 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, |
---|
235 | "No common name found in certificate for '%s:%d'. Looking for subject alternative name...", |
---|
236 | s->server_hostname, s->port); |
---|
237 | rv = 0; |
---|
238 | /* read subject alternative name */ |
---|
239 | for (i = 0; !(rv < 0); i++) { |
---|
240 | data_len = 0; |
---|
241 | rv = gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
242 | NULL, |
---|
243 | &data_len, |
---|
244 | NULL); |
---|
245 | |
---|
246 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER |
---|
247 | && data_len > 1) { |
---|
248 | /* FIXME: not very efficient. What if we have several alt names |
---|
249 | * before DNSName? |
---|
250 | */ |
---|
251 | *cert_cn = apr_palloc(p, data_len + 1); |
---|
252 | |
---|
253 | rv = gnutls_x509_crt_get_subject_alt_name |
---|
254 | (cert, i, *cert_cn, &data_len, NULL); |
---|
255 | (*cert_cn)[data_len] = 0; |
---|
256 | |
---|
257 | if (rv == GNUTLS_SAN_DNSNAME) |
---|
258 | break; |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | return rv; |
---|
264 | } |
---|
265 | |
---|
266 | static int read_pgpcrt_cn(server_rec * s, apr_pool_t * p, |
---|
267 | gnutls_openpgp_crt_t cert, char **cert_cn) { |
---|
268 | int rv = 0; |
---|
269 | size_t data_len; |
---|
270 | |
---|
271 | |
---|
272 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
273 | *cert_cn = NULL; |
---|
274 | |
---|
275 | data_len = 0; |
---|
276 | rv = gnutls_openpgp_crt_get_name(cert, 0, NULL, &data_len); |
---|
277 | |
---|
278 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) { |
---|
279 | *cert_cn = apr_palloc(p, data_len); |
---|
280 | rv = gnutls_openpgp_crt_get_name(cert, 0, *cert_cn, |
---|
281 | &data_len); |
---|
282 | } else { /* No CN return subject alternative name */ |
---|
283 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, |
---|
284 | "No name found in PGP certificate for '%s:%d'.", |
---|
285 | s->server_hostname, s->port); |
---|
286 | } |
---|
287 | |
---|
288 | return rv; |
---|
289 | } |
---|
290 | |
---|
291 | int mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog __attribute__((unused)), apr_pool_t * ptemp __attribute__((unused)), server_rec * base_server) { |
---|
292 | |
---|
293 | int rv; |
---|
294 | server_rec *s; |
---|
295 | gnutls_dh_params_t dh_params = NULL; |
---|
296 | mgs_srvconf_rec *sc; |
---|
297 | mgs_srvconf_rec *sc_base; |
---|
298 | void *data = NULL; |
---|
299 | const char *userdata_key = "mgs_init"; |
---|
300 | |
---|
301 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
302 | |
---|
303 | apr_pool_userdata_get(&data, userdata_key, base_server->process->pool); |
---|
304 | if (data == NULL) { |
---|
305 | apr_pool_userdata_set((const void *) 1, userdata_key, apr_pool_cleanup_null, base_server->process->pool); |
---|
306 | } |
---|
307 | |
---|
308 | s = base_server; |
---|
309 | sc_base = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, &gnutls_module); |
---|
310 | |
---|
311 | |
---|
312 | rv = mgs_cache_post_config(p, s, sc_base); |
---|
313 | if (rv != 0) { |
---|
314 | ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, |
---|
315 | "GnuTLS: Post Config for GnuTLSCache Failed." |
---|
316 | " Shutting Down."); |
---|
317 | exit(-1); |
---|
318 | } |
---|
319 | |
---|
320 | for (s = base_server; s; s = s->next) { |
---|
321 | sc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, &gnutls_module); |
---|
322 | sc->cache_type = sc_base->cache_type; |
---|
323 | sc->cache_config = sc_base->cache_config; |
---|
324 | sc->cache_timeout = sc_base->cache_timeout; |
---|
325 | |
---|
326 | rv = mgs_load_files(p, s); |
---|
327 | if (rv != 0) { |
---|
328 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
329 | "GnuTLS: Loading required files failed." |
---|
330 | " Shutting Down."); |
---|
331 | exit(-1); |
---|
332 | } |
---|
333 | |
---|
334 | /* defaults for unset values: */ |
---|
335 | if (sc->enabled == GNUTLS_ENABLED_UNSET) |
---|
336 | sc->enabled = GNUTLS_ENABLED_FALSE; |
---|
337 | if (sc->tickets == GNUTLS_ENABLED_UNSET) |
---|
338 | sc->tickets = GNUTLS_ENABLED_TRUE; |
---|
339 | if (sc->export_certificates_size < 0) |
---|
340 | sc->export_certificates_size = 0; |
---|
341 | if (sc->client_verify_mode == -1) |
---|
342 | sc->client_verify_mode = GNUTLS_CERT_IGNORE; |
---|
343 | if (sc->client_verify_method == mgs_cvm_unset) |
---|
344 | sc->client_verify_method = mgs_cvm_cartel; |
---|
345 | |
---|
346 | /* Check if the priorities have been set */ |
---|
347 | if (sc->priorities == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
348 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
349 | "GnuTLS: Host '%s:%d' is missing the GnuTLSPriorities directive!", |
---|
350 | s->server_hostname, s->port); |
---|
351 | exit(-1); |
---|
352 | } |
---|
353 | |
---|
354 | /* Check if DH params have been set per host */ |
---|
355 | if (sc->dh_params != NULL) { |
---|
356 | gnutls_certificate_set_dh_params(sc->certs, sc->dh_params); |
---|
357 | gnutls_anon_set_server_dh_params(sc->anon_creds, sc->dh_params); |
---|
358 | } else if (dh_params) { |
---|
359 | gnutls_certificate_set_dh_params(sc->certs, dh_params); |
---|
360 | gnutls_anon_set_server_dh_params(sc->anon_creds, dh_params); |
---|
361 | } |
---|
362 | |
---|
363 | gnutls_certificate_set_retrieve_function2(sc->certs, cert_retrieve_fn); |
---|
364 | |
---|
365 | if ((sc->certs_x509_chain == NULL || sc->certs_x509_chain_num < 1) && |
---|
366 | sc->cert_pgp == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
367 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
368 | "GnuTLS: Host '%s:%d' is missing a Certificate File!", |
---|
369 | s->server_hostname, s->port); |
---|
370 | exit(-1); |
---|
371 | } |
---|
372 | if (sc->enabled == GNUTLS_ENABLED_TRUE && |
---|
373 | ((sc->certs_x509_chain_num > 0 && sc->privkey_x509 == NULL) || |
---|
374 | (sc->cert_crt_pgp[0] != NULL && sc->privkey_pgp == NULL))) { |
---|
375 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
376 | "GnuTLS: Host '%s:%d' is missing a Private Key File!", |
---|
377 | s->server_hostname, s->port); |
---|
378 | exit(-1); |
---|
379 | } |
---|
380 | |
---|
381 | if (sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
382 | rv = -1; |
---|
383 | if (sc->certs_x509_chain_num > 0) { |
---|
384 | rv = read_crt_cn(s, p, sc->certs_x509_crt_chain[0], &sc->cert_cn); |
---|
385 | } |
---|
386 | if (rv < 0 && sc->cert_pgp != NULL) { |
---|
387 | rv = read_pgpcrt_cn(s, p, sc->cert_crt_pgp[0], &sc->cert_cn); |
---|
388 | } |
---|
389 | |
---|
390 | if (rv < 0) { |
---|
391 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
392 | "GnuTLS: Cannot find a certificate for host '%s:%d'!", |
---|
393 | s->server_hostname, s->port); |
---|
394 | sc->cert_cn = NULL; |
---|
395 | continue; |
---|
396 | } |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | ap_add_version_component(p, "mod_gnutls/" MOD_GNUTLS_VERSION); |
---|
402 | |
---|
403 | { |
---|
404 | const char* libvers = gnutls_check_version(NULL); |
---|
405 | char* gnutls_version = NULL; |
---|
406 | if(libvers && (gnutls_version = apr_psprintf(p, "GnuTLS/%s", libvers))) { |
---|
407 | ap_add_version_component(p, gnutls_version); |
---|
408 | } else { |
---|
409 | // In case we could not create the above string go for the static version instead |
---|
410 | ap_add_version_component(p, "GnuTLS/" GNUTLS_VERSION "-static"); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | return OK; |
---|
415 | } |
---|
416 | |
---|
417 | void mgs_hook_child_init(apr_pool_t * p, server_rec *s) { |
---|
418 | apr_status_t rv = APR_SUCCESS; |
---|
419 | mgs_srvconf_rec *sc = |
---|
420 | (mgs_srvconf_rec *) ap_get_module_config(s->module_config, &gnutls_module); |
---|
421 | |
---|
422 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
423 | /* if we use PKCS #11 reinitialize it */ |
---|
424 | |
---|
425 | if (mgs_pkcs11_reinit(s) < 0) { |
---|
426 | ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, |
---|
427 | "GnuTLS: Failed to reinitialize PKCS #11"); |
---|
428 | exit(-1); |
---|
429 | } |
---|
430 | |
---|
431 | if (sc->cache_type != mgs_cache_none) { |
---|
432 | rv = mgs_cache_child_init(p, s, sc); |
---|
433 | if (rv != APR_SUCCESS) { |
---|
434 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
435 | "GnuTLS: Failed to run Cache Init"); |
---|
436 | } |
---|
437 | } |
---|
438 | /* Block SIGPIPE Signals */ |
---|
439 | rv = apr_signal_block(SIGPIPE); |
---|
440 | if(rv != APR_SUCCESS) { |
---|
441 | /* error sending output */ |
---|
442 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
443 | "GnuTLS: Error Blocking SIGPIPE Signal!"); |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | const char *mgs_hook_http_scheme(const request_rec * r) { |
---|
448 | mgs_srvconf_rec *sc; |
---|
449 | |
---|
450 | if (r == NULL) |
---|
451 | return NULL; |
---|
452 | |
---|
453 | sc = (mgs_srvconf_rec *) ap_get_module_config(r-> |
---|
454 | server->module_config, |
---|
455 | &gnutls_module); |
---|
456 | |
---|
457 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
458 | if (sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
459 | return NULL; |
---|
460 | } |
---|
461 | |
---|
462 | return "https"; |
---|
463 | } |
---|
464 | |
---|
465 | apr_port_t mgs_hook_default_port(const request_rec * r) { |
---|
466 | mgs_srvconf_rec *sc; |
---|
467 | |
---|
468 | if (r == NULL) |
---|
469 | return 0; |
---|
470 | |
---|
471 | sc = (mgs_srvconf_rec *) ap_get_module_config(r-> |
---|
472 | server->module_config, |
---|
473 | &gnutls_module); |
---|
474 | |
---|
475 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
476 | if (sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
477 | return 0; |
---|
478 | } |
---|
479 | |
---|
480 | return 443; |
---|
481 | } |
---|
482 | |
---|
483 | #define MAX_HOST_LEN 255 |
---|
484 | |
---|
485 | #if USING_2_1_RECENT |
---|
486 | |
---|
487 | typedef struct { |
---|
488 | mgs_handle_t *ctxt; |
---|
489 | mgs_srvconf_rec *sc; |
---|
490 | const char *sni_name; |
---|
491 | } vhost_cb_rec; |
---|
492 | |
---|
493 | /** |
---|
494 | * Matches the current vhost's ServerAlias directives |
---|
495 | * |
---|
496 | * @param x vhost callback record |
---|
497 | * @param s server record |
---|
498 | * @return true if a match, false otherwise |
---|
499 | * |
---|
500 | */ |
---|
501 | int check_server_aliases(vhost_cb_rec *x, server_rec * s, mgs_srvconf_rec *tsc) { |
---|
502 | apr_array_header_t *names; |
---|
503 | int i,rv = 0; |
---|
504 | char ** name; |
---|
505 | |
---|
506 | /* Check ServerName First! */ |
---|
507 | if(apr_strnatcasecmp(x->sni_name, s->server_hostname) == 0) { |
---|
508 | // We have a match, save this server configuration |
---|
509 | x->sc = tsc; |
---|
510 | rv = 1; |
---|
511 | /* Check any ServerAlias directives */ |
---|
512 | } else if(s->names->nelts) { |
---|
513 | names = s->names; |
---|
514 | name = (char **)names->elts; |
---|
515 | for (i = 0; i < names->nelts; ++i) { |
---|
516 | if (!name[i]) { continue; } |
---|
517 | if (apr_strnatcasecmp(x->sni_name, name[i]) == 0) { |
---|
518 | // We have a match, save this server configuration |
---|
519 | x->sc = tsc; |
---|
520 | rv = 1; |
---|
521 | } |
---|
522 | } |
---|
523 | /* Wild any ServerAlias Directives */ |
---|
524 | } else if(s->wild_names->nelts) { |
---|
525 | names = s->wild_names; |
---|
526 | name = (char **)names->elts; |
---|
527 | for (i = 0; i < names->nelts; ++i) { |
---|
528 | if (!name[i]) { continue; } |
---|
529 | if(apr_fnmatch(name[i], x->sni_name , |
---|
530 | APR_FNM_CASE_BLIND| |
---|
531 | APR_FNM_PERIOD| |
---|
532 | APR_FNM_PATHNAME| |
---|
533 | APR_FNM_NOESCAPE) == APR_SUCCESS) { |
---|
534 | x->sc = tsc; |
---|
535 | rv = 1; |
---|
536 | } |
---|
537 | } |
---|
538 | } |
---|
539 | return rv; |
---|
540 | } |
---|
541 | |
---|
542 | static int vhost_cb(void *baton, conn_rec * conn __attribute__((unused)), server_rec * s) { |
---|
543 | mgs_srvconf_rec *tsc; |
---|
544 | vhost_cb_rec *x = baton; |
---|
545 | int ret; |
---|
546 | |
---|
547 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
548 | tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
549 | &gnutls_module); |
---|
550 | |
---|
551 | if (tsc->enabled != GNUTLS_ENABLED_TRUE || tsc->cert_cn == NULL) { |
---|
552 | return 0; |
---|
553 | } |
---|
554 | |
---|
555 | if (tsc->certs_x509_chain_num > 0) { |
---|
556 | /* this check is there to warn administrator of any missing hostname |
---|
557 | * in the certificate. */ |
---|
558 | ret = gnutls_x509_crt_check_hostname(tsc->certs_x509_crt_chain[0], s->server_hostname); |
---|
559 | if (0 == ret) |
---|
560 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, |
---|
561 | "GnuTLS: the certificate doesn't match requested hostname " |
---|
562 | "'%s'", s->server_hostname); |
---|
563 | } else { |
---|
564 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, |
---|
565 | "GnuTLS: SNI request for '%s' but no X.509 certs available at all", |
---|
566 | s->server_hostname); |
---|
567 | } |
---|
568 | return check_server_aliases(x, s, tsc); |
---|
569 | } |
---|
570 | #endif |
---|
571 | |
---|
572 | mgs_srvconf_rec *mgs_find_sni_server(gnutls_session_t session) { |
---|
573 | int rv; |
---|
574 | unsigned int sni_type; |
---|
575 | size_t data_len = MAX_HOST_LEN; |
---|
576 | char sni_name[MAX_HOST_LEN]; |
---|
577 | mgs_handle_t *ctxt; |
---|
578 | #if USING_2_1_RECENT |
---|
579 | vhost_cb_rec cbx; |
---|
580 | #else |
---|
581 | server_rec *s; |
---|
582 | mgs_srvconf_rec *tsc; |
---|
583 | #endif |
---|
584 | |
---|
585 | if (session == NULL) |
---|
586 | return NULL; |
---|
587 | |
---|
588 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
589 | ctxt = gnutls_transport_get_ptr(session); |
---|
590 | |
---|
591 | rv = gnutls_server_name_get(ctxt->session, sni_name, |
---|
592 | &data_len, &sni_type, 0); |
---|
593 | |
---|
594 | if (rv != 0) { |
---|
595 | return NULL; |
---|
596 | } |
---|
597 | |
---|
598 | if (sni_type != GNUTLS_NAME_DNS) { |
---|
599 | ap_log_error(APLOG_MARK, APLOG_CRIT, 0, |
---|
600 | ctxt->c->base_server, |
---|
601 | "GnuTLS: Unknown type '%d' for SNI: " |
---|
602 | "'%s'", sni_type, sni_name); |
---|
603 | return NULL; |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | * Code in the Core already sets up the c->base_server as the base |
---|
608 | * for this IP/Port combo. Trust that the core did the 'right' thing. |
---|
609 | */ |
---|
610 | #if USING_2_1_RECENT |
---|
611 | cbx.ctxt = ctxt; |
---|
612 | cbx.sc = NULL; |
---|
613 | cbx.sni_name = sni_name; |
---|
614 | |
---|
615 | rv = ap_vhost_iterate_given_conn(ctxt->c, vhost_cb, &cbx); |
---|
616 | if (rv == 1) { |
---|
617 | return cbx.sc; |
---|
618 | } |
---|
619 | #else |
---|
620 | for (s = ap_server_conf; s; s = s->next) { |
---|
621 | |
---|
622 | tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
623 | &gnutls_module); |
---|
624 | |
---|
625 | if (tsc->enabled != GNUTLS_ENABLED_TRUE) { continue; } |
---|
626 | |
---|
627 | if(check_server_aliases(x, s, tsc)) { |
---|
628 | return tsc; |
---|
629 | } |
---|
630 | #endif |
---|
631 | return NULL; |
---|
632 | } |
---|
633 | |
---|
634 | static void create_gnutls_handle(conn_rec * c) |
---|
635 | { |
---|
636 | /* Get mod_gnutls server configuration */ |
---|
637 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
638 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
639 | |
---|
640 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
641 | |
---|
642 | /* Get connection specific configuration */ |
---|
643 | mgs_handle_t *ctxt = (mgs_handle_t *) ap_get_module_config(c->conn_config, &gnutls_module); |
---|
644 | if (ctxt == NULL) |
---|
645 | { |
---|
646 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, "%s: allocating connection memory", __func__); |
---|
647 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
648 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
649 | } |
---|
650 | ctxt->enabled = GNUTLS_ENABLED_TRUE; |
---|
651 | ctxt->c = c; |
---|
652 | ctxt->sc = sc; |
---|
653 | ctxt->status = 0; |
---|
654 | ctxt->input_rc = APR_SUCCESS; |
---|
655 | ctxt->input_bb = apr_brigade_create(c->pool, c->bucket_alloc); |
---|
656 | ctxt->input_cbuf.length = 0; |
---|
657 | ctxt->output_rc = APR_SUCCESS; |
---|
658 | ctxt->output_bb = apr_brigade_create(c->pool, c->bucket_alloc); |
---|
659 | ctxt->output_blen = 0; |
---|
660 | ctxt->output_length = 0; |
---|
661 | |
---|
662 | /* Initialize GnuTLS Library */ |
---|
663 | int err = gnutls_init(&ctxt->session, GNUTLS_SERVER); |
---|
664 | if (err != GNUTLS_E_SUCCESS) |
---|
665 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, "gnutls_init failed!"); |
---|
666 | /* Initialize Session Tickets */ |
---|
667 | if (session_ticket_key.data != NULL && ctxt->sc->tickets != 0) { |
---|
668 | err = gnutls_session_ticket_enable_server(ctxt->session, &session_ticket_key); |
---|
669 | if (err != GNUTLS_E_SUCCESS) |
---|
670 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, "gnutls_session_ticket_enable_server failed!"); |
---|
671 | } |
---|
672 | |
---|
673 | /* Set Default Priority */ |
---|
674 | err = gnutls_priority_set_direct(ctxt->session, "NORMAL", NULL); |
---|
675 | if (err != GNUTLS_E_SUCCESS) |
---|
676 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, "gnutls_priority_set_direct failed!"); |
---|
677 | /* Set Handshake function */ |
---|
678 | gnutls_handshake_set_post_client_hello_function(ctxt->session, |
---|
679 | mgs_select_virtual_server_cb); |
---|
680 | /* Initialize Session Cache */ |
---|
681 | mgs_cache_session_init(ctxt); |
---|
682 | |
---|
683 | /* Set pull, push & ptr functions */ |
---|
684 | gnutls_transport_set_pull_function(ctxt->session, |
---|
685 | mgs_transport_read); |
---|
686 | gnutls_transport_set_push_function(ctxt->session, |
---|
687 | mgs_transport_write); |
---|
688 | gnutls_transport_set_ptr(ctxt->session, ctxt); |
---|
689 | /* Add IO filters */ |
---|
690 | ctxt->input_filter = ap_add_input_filter(GNUTLS_INPUT_FILTER_NAME, |
---|
691 | ctxt, NULL, c); |
---|
692 | ctxt->output_filter = ap_add_output_filter(GNUTLS_OUTPUT_FILTER_NAME, |
---|
693 | ctxt, NULL, c); |
---|
694 | } |
---|
695 | |
---|
696 | int mgs_hook_pre_connection(conn_rec * c, void *csd __attribute__((unused))) |
---|
697 | { |
---|
698 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
699 | |
---|
700 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
701 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
702 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
703 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
704 | |
---|
705 | if ((sc && (!sc->enabled || sc->proxy_enabled == GNUTLS_ENABLED_TRUE)) |
---|
706 | || (ctxt && ctxt->enabled == GNUTLS_ENABLED_FALSE)) |
---|
707 | { |
---|
708 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, "%s declined connection", |
---|
709 | __func__); |
---|
710 | return DECLINED; |
---|
711 | } |
---|
712 | |
---|
713 | create_gnutls_handle(c); |
---|
714 | return OK; |
---|
715 | } |
---|
716 | |
---|
717 | int mgs_hook_fixups(request_rec * r) { |
---|
718 | unsigned char sbuf[GNUTLS_MAX_SESSION_ID]; |
---|
719 | char buf[AP_IOBUFSIZE]; |
---|
720 | const char *tmp; |
---|
721 | size_t len; |
---|
722 | mgs_handle_t *ctxt; |
---|
723 | int rv = OK; |
---|
724 | |
---|
725 | if (r == NULL) |
---|
726 | return DECLINED; |
---|
727 | |
---|
728 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
729 | apr_table_t *env = r->subprocess_env; |
---|
730 | |
---|
731 | ctxt = ap_get_module_config(r->connection->conn_config, |
---|
732 | &gnutls_module); |
---|
733 | |
---|
734 | if (!ctxt || ctxt->enabled != GNUTLS_ENABLED_TRUE || ctxt->session == NULL) |
---|
735 | { |
---|
736 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "request declined in %s", __func__); |
---|
737 | return DECLINED; |
---|
738 | } |
---|
739 | |
---|
740 | apr_table_setn(env, "HTTPS", "on"); |
---|
741 | |
---|
742 | apr_table_setn(env, "SSL_VERSION_LIBRARY", |
---|
743 | "GnuTLS/" LIBGNUTLS_VERSION); |
---|
744 | apr_table_setn(env, "SSL_VERSION_INTERFACE", |
---|
745 | "mod_gnutls/" MOD_GNUTLS_VERSION); |
---|
746 | |
---|
747 | apr_table_setn(env, "SSL_PROTOCOL", |
---|
748 | gnutls_protocol_get_name(gnutls_protocol_get_version(ctxt->session))); |
---|
749 | |
---|
750 | /* should have been called SSL_CIPHERSUITE instead */ |
---|
751 | apr_table_setn(env, "SSL_CIPHER", |
---|
752 | gnutls_cipher_suite_get_name(gnutls_kx_get(ctxt->session), |
---|
753 | gnutls_cipher_get(ctxt->session), |
---|
754 | gnutls_mac_get(ctxt->session))); |
---|
755 | |
---|
756 | apr_table_setn(env, "SSL_COMPRESS_METHOD", |
---|
757 | gnutls_compression_get_name(gnutls_compression_get(ctxt->session))); |
---|
758 | |
---|
759 | #ifdef ENABLE_SRP |
---|
760 | if (ctxt->sc->srp_tpasswd_conf_file != NULL && ctxt->sc->srp_tpasswd_file != NULL) { |
---|
761 | tmp = gnutls_srp_server_get_username(ctxt->session); |
---|
762 | apr_table_setn(env, "SSL_SRP_USER", (tmp != NULL) ? tmp : ""); |
---|
763 | } else { |
---|
764 | apr_table_unset(env, "SSL_SRP_USER"); |
---|
765 | } |
---|
766 | #endif |
---|
767 | |
---|
768 | if (apr_table_get(env, "SSL_CLIENT_VERIFY") == NULL) |
---|
769 | apr_table_setn(env, "SSL_CLIENT_VERIFY", "NONE"); |
---|
770 | |
---|
771 | unsigned int key_size = 8 * gnutls_cipher_get_key_size(gnutls_cipher_get(ctxt->session)); |
---|
772 | tmp = apr_psprintf(r->pool, "%u", key_size); |
---|
773 | |
---|
774 | apr_table_setn(env, "SSL_CIPHER_USEKEYSIZE", tmp); |
---|
775 | |
---|
776 | apr_table_setn(env, "SSL_CIPHER_ALGKEYSIZE", tmp); |
---|
777 | |
---|
778 | apr_table_setn(env, "SSL_CIPHER_EXPORT", |
---|
779 | (key_size <= 40) ? "true" : "false"); |
---|
780 | |
---|
781 | int dhsize = gnutls_dh_get_prime_bits(ctxt->session); |
---|
782 | if (dhsize > 0) { |
---|
783 | tmp = apr_psprintf(r->pool, "%d", dhsize); |
---|
784 | apr_table_setn(env, "SSL_DH_PRIME_BITS", tmp); |
---|
785 | } |
---|
786 | |
---|
787 | len = sizeof (sbuf); |
---|
788 | gnutls_session_get_id(ctxt->session, sbuf, &len); |
---|
789 | tmp = mgs_session_id2sz(sbuf, len, buf, sizeof (buf)); |
---|
790 | apr_table_setn(env, "SSL_SESSION_ID", apr_pstrdup(r->pool, tmp)); |
---|
791 | |
---|
792 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
793 | mgs_add_common_cert_vars(r, ctxt->sc->certs_x509_crt_chain[0], 0, ctxt->sc->export_certificates_size); |
---|
794 | } else if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_OPENPGP) { |
---|
795 | mgs_add_common_pgpcert_vars(r, ctxt->sc->cert_crt_pgp[0], 0, ctxt->sc->export_certificates_size); |
---|
796 | } |
---|
797 | |
---|
798 | return rv; |
---|
799 | } |
---|
800 | |
---|
801 | int mgs_hook_authz(request_rec * r) { |
---|
802 | int rv; |
---|
803 | mgs_handle_t *ctxt; |
---|
804 | mgs_dirconf_rec *dc; |
---|
805 | |
---|
806 | if (r == NULL) |
---|
807 | return DECLINED; |
---|
808 | |
---|
809 | dc = ap_get_module_config(r->per_dir_config, &gnutls_module); |
---|
810 | |
---|
811 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
812 | ctxt = |
---|
813 | ap_get_module_config(r->connection->conn_config, |
---|
814 | &gnutls_module); |
---|
815 | |
---|
816 | if (!ctxt || ctxt->session == NULL) { |
---|
817 | return DECLINED; |
---|
818 | } |
---|
819 | |
---|
820 | if (dc->client_verify_mode == GNUTLS_CERT_IGNORE) { |
---|
821 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
822 | "GnuTLS: Directory set to Ignore Client Certificate!"); |
---|
823 | } else { |
---|
824 | if (ctxt->sc->client_verify_mode < dc->client_verify_mode) { |
---|
825 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
826 | "GnuTLS: Attempting to rehandshake with peer. %d %d", |
---|
827 | ctxt->sc->client_verify_mode, |
---|
828 | dc->client_verify_mode); |
---|
829 | |
---|
830 | /* If we already have a client certificate, there's no point in |
---|
831 | * re-handshaking... */ |
---|
832 | rv = mgs_cert_verify(r, ctxt); |
---|
833 | if (rv != DECLINED && rv != HTTP_FORBIDDEN) |
---|
834 | return rv; |
---|
835 | |
---|
836 | gnutls_certificate_server_set_request |
---|
837 | (ctxt->session, dc->client_verify_mode); |
---|
838 | |
---|
839 | if (mgs_rehandshake(ctxt) != 0) { |
---|
840 | return HTTP_FORBIDDEN; |
---|
841 | } |
---|
842 | } else if (ctxt->sc->client_verify_mode == |
---|
843 | GNUTLS_CERT_IGNORE) { |
---|
844 | #if MOD_GNUTLS_DEBUG |
---|
845 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
846 | "GnuTLS: Peer is set to IGNORE"); |
---|
847 | #endif |
---|
848 | return DECLINED; |
---|
849 | } |
---|
850 | rv = mgs_cert_verify(r, ctxt); |
---|
851 | if (rv != DECLINED && |
---|
852 | (rv != HTTP_FORBIDDEN || |
---|
853 | dc->client_verify_mode == GNUTLS_CERT_REQUIRE)) { |
---|
854 | return rv; |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | return DECLINED; |
---|
859 | } |
---|
860 | |
---|
861 | /* variables that are not sent by default: |
---|
862 | * |
---|
863 | * SSL_CLIENT_CERT string PEM-encoded client certificate |
---|
864 | * SSL_SERVER_CERT string PEM-encoded client certificate |
---|
865 | */ |
---|
866 | |
---|
867 | /* @param side is either 0 for SERVER or 1 for CLIENT |
---|
868 | * |
---|
869 | * @param export_cert_size (int) maximum size for environment variable |
---|
870 | * to use for the PEM-encoded certificate (0 means do not export) |
---|
871 | */ |
---|
872 | #define MGS_SIDE(suffix) ((side==0) ? "SSL_SERVER" suffix : "SSL_CLIENT" suffix) |
---|
873 | |
---|
874 | static void mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, int side, size_t export_cert_size) { |
---|
875 | unsigned char sbuf[64]; /* buffer to hold serials */ |
---|
876 | char buf[AP_IOBUFSIZE]; |
---|
877 | const char *tmp; |
---|
878 | char *tmp2; |
---|
879 | size_t len; |
---|
880 | int ret, i; |
---|
881 | |
---|
882 | if (r == NULL) |
---|
883 | return; |
---|
884 | |
---|
885 | apr_table_t *env = r->subprocess_env; |
---|
886 | |
---|
887 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
888 | if (export_cert_size > 0) { |
---|
889 | len = 0; |
---|
890 | ret = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, NULL, &len); |
---|
891 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) { |
---|
892 | if (len >= export_cert_size) { |
---|
893 | apr_table_setn(env, MGS_SIDE("_CERT"), "GNUTLS_CERTIFICATE_SIZE_LIMIT_EXCEEDED"); |
---|
894 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
895 | "GnuTLS: Failed to export too-large X.509 certificate to environment"); |
---|
896 | } else { |
---|
897 | char* cert_buf = apr_palloc(r->pool, len + 1); |
---|
898 | if (cert_buf != NULL && gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, cert_buf, &len) >= 0) { |
---|
899 | cert_buf[len] = 0; |
---|
900 | apr_table_setn(env, MGS_SIDE("_CERT"), cert_buf); |
---|
901 | } else { |
---|
902 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, |
---|
903 | "GnuTLS: failed to export X.509 certificate"); |
---|
904 | } |
---|
905 | } |
---|
906 | } else { |
---|
907 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, |
---|
908 | "GnuTLS: dazed and confused about X.509 certificate size"); |
---|
909 | } |
---|
910 | } |
---|
911 | |
---|
912 | len = sizeof (buf); |
---|
913 | gnutls_x509_crt_get_dn(cert, buf, &len); |
---|
914 | apr_table_setn(env, MGS_SIDE("_S_DN"), apr_pstrmemdup(r->pool, buf, len)); |
---|
915 | |
---|
916 | len = sizeof (buf); |
---|
917 | gnutls_x509_crt_get_issuer_dn(cert, buf, &len); |
---|
918 | apr_table_setn(env, MGS_SIDE("_I_DN"), apr_pstrmemdup(r->pool, buf, len)); |
---|
919 | |
---|
920 | len = sizeof (sbuf); |
---|
921 | gnutls_x509_crt_get_serial(cert, sbuf, &len); |
---|
922 | tmp = mgs_session_id2sz(sbuf, len, buf, sizeof (buf)); |
---|
923 | apr_table_setn(env, MGS_SIDE("_M_SERIAL"), apr_pstrdup(r->pool, tmp)); |
---|
924 | |
---|
925 | ret = gnutls_x509_crt_get_version(cert); |
---|
926 | if (ret > 0) |
---|
927 | apr_table_setn(env, MGS_SIDE("_M_VERSION"), |
---|
928 | apr_psprintf(r->pool, "%u", ret)); |
---|
929 | |
---|
930 | apr_table_setn(env, MGS_SIDE("_CERT_TYPE"), "X.509"); |
---|
931 | |
---|
932 | tmp = |
---|
933 | mgs_time2sz(gnutls_x509_crt_get_expiration_time |
---|
934 | (cert), buf, sizeof (buf)); |
---|
935 | apr_table_setn(env, MGS_SIDE("_V_END"), apr_pstrdup(r->pool, tmp)); |
---|
936 | |
---|
937 | tmp = |
---|
938 | mgs_time2sz(gnutls_x509_crt_get_activation_time |
---|
939 | (cert), buf, sizeof (buf)); |
---|
940 | apr_table_setn(env, MGS_SIDE("_V_START"), apr_pstrdup(r->pool, tmp)); |
---|
941 | |
---|
942 | ret = gnutls_x509_crt_get_signature_algorithm(cert); |
---|
943 | if (ret >= 0) { |
---|
944 | apr_table_setn(env, MGS_SIDE("_A_SIG"), |
---|
945 | gnutls_sign_algorithm_get_name(ret)); |
---|
946 | } |
---|
947 | |
---|
948 | ret = gnutls_x509_crt_get_pk_algorithm(cert, NULL); |
---|
949 | if (ret >= 0) { |
---|
950 | apr_table_setn(env, MGS_SIDE("_A_KEY"), |
---|
951 | gnutls_pk_algorithm_get_name(ret)); |
---|
952 | } |
---|
953 | |
---|
954 | /* export all the alternative names (DNS, RFC822 and URI) */ |
---|
955 | for (i = 0; !(ret < 0); i++) { |
---|
956 | const char *san, *sanlabel; |
---|
957 | len = 0; |
---|
958 | ret = gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
959 | NULL, &len, |
---|
960 | NULL); |
---|
961 | |
---|
962 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER && len > 1) { |
---|
963 | tmp2 = apr_palloc(r->pool, len + 1); |
---|
964 | |
---|
965 | ret = |
---|
966 | gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
967 | tmp2, |
---|
968 | &len, |
---|
969 | NULL); |
---|
970 | tmp2[len] = 0; |
---|
971 | |
---|
972 | sanlabel = apr_psprintf(r->pool, "%s%u", MGS_SIDE("_S_AN"), i); |
---|
973 | if (ret == GNUTLS_SAN_DNSNAME) { |
---|
974 | san = apr_psprintf(r->pool, "DNSNAME:%s", tmp2); |
---|
975 | } else if (ret == GNUTLS_SAN_RFC822NAME) { |
---|
976 | san = apr_psprintf(r->pool, "RFC822NAME:%s", tmp2); |
---|
977 | } else if (ret == GNUTLS_SAN_URI) { |
---|
978 | san = apr_psprintf(r->pool, "URI:%s", tmp2); |
---|
979 | } else { |
---|
980 | san = "UNSUPPORTED"; |
---|
981 | } |
---|
982 | apr_table_setn(env, sanlabel, san); |
---|
983 | } |
---|
984 | } |
---|
985 | } |
---|
986 | |
---|
987 | |
---|
988 | /* @param side 0: server, 1: client |
---|
989 | * |
---|
990 | * @param export_cert_size (int) maximum size for environment variable |
---|
991 | * to use for the PEM-encoded certificate (0 means do not export) |
---|
992 | */ |
---|
993 | static void mgs_add_common_pgpcert_vars(request_rec * r, gnutls_openpgp_crt_t cert, int side, size_t export_cert_size) { |
---|
994 | |
---|
995 | unsigned char sbuf[64]; /* buffer to hold serials */ |
---|
996 | char buf[AP_IOBUFSIZE]; |
---|
997 | const char *tmp; |
---|
998 | size_t len; |
---|
999 | int ret; |
---|
1000 | |
---|
1001 | if (r == NULL) |
---|
1002 | return; |
---|
1003 | |
---|
1004 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1005 | apr_table_t *env = r->subprocess_env; |
---|
1006 | |
---|
1007 | if (export_cert_size > 0) { |
---|
1008 | len = 0; |
---|
1009 | ret = gnutls_openpgp_crt_export(cert, GNUTLS_OPENPGP_FMT_BASE64, NULL, &len); |
---|
1010 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) { |
---|
1011 | if (len >= export_cert_size) { |
---|
1012 | apr_table_setn(env, MGS_SIDE("_CERT"), |
---|
1013 | "GNUTLS_CERTIFICATE_SIZE_LIMIT_EXCEEDED"); |
---|
1014 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1015 | "GnuTLS: Failed to export too-large OpenPGP certificate to environment"); |
---|
1016 | } else { |
---|
1017 | char* cert_buf = apr_palloc(r->pool, len + 1); |
---|
1018 | if (cert_buf != NULL && gnutls_openpgp_crt_export(cert, GNUTLS_OPENPGP_FMT_BASE64, cert_buf, &len) >= 0) { |
---|
1019 | cert_buf[len] = 0; |
---|
1020 | apr_table_setn(env, MGS_SIDE("_CERT"), cert_buf); |
---|
1021 | } else { |
---|
1022 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, |
---|
1023 | "GnuTLS: failed to export OpenPGP certificate"); |
---|
1024 | } |
---|
1025 | } |
---|
1026 | } else { |
---|
1027 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, |
---|
1028 | "GnuTLS: dazed and confused about OpenPGP certificate size"); |
---|
1029 | } |
---|
1030 | } |
---|
1031 | |
---|
1032 | len = sizeof (buf); |
---|
1033 | gnutls_openpgp_crt_get_name(cert, 0, buf, &len); |
---|
1034 | apr_table_setn(env, MGS_SIDE("_NAME"), apr_pstrmemdup(r->pool, buf, len)); |
---|
1035 | |
---|
1036 | len = sizeof (sbuf); |
---|
1037 | gnutls_openpgp_crt_get_fingerprint(cert, sbuf, &len); |
---|
1038 | tmp = mgs_session_id2sz(sbuf, len, buf, sizeof (buf)); |
---|
1039 | apr_table_setn(env, MGS_SIDE("_FINGERPRINT"), apr_pstrdup(r->pool, tmp)); |
---|
1040 | |
---|
1041 | ret = gnutls_openpgp_crt_get_version(cert); |
---|
1042 | if (ret > 0) |
---|
1043 | apr_table_setn(env, MGS_SIDE("_M_VERSION"), |
---|
1044 | apr_psprintf(r->pool, "%u", ret)); |
---|
1045 | |
---|
1046 | apr_table_setn(env, MGS_SIDE("_CERT_TYPE"), "OPENPGP"); |
---|
1047 | |
---|
1048 | tmp = |
---|
1049 | mgs_time2sz(gnutls_openpgp_crt_get_expiration_time |
---|
1050 | (cert), buf, sizeof (buf)); |
---|
1051 | apr_table_setn(env, MGS_SIDE("_V_END"), apr_pstrdup(r->pool, tmp)); |
---|
1052 | |
---|
1053 | tmp = |
---|
1054 | mgs_time2sz(gnutls_openpgp_crt_get_creation_time |
---|
1055 | (cert), buf, sizeof (buf)); |
---|
1056 | apr_table_setn(env, MGS_SIDE("_V_START"), apr_pstrdup(r->pool, tmp)); |
---|
1057 | |
---|
1058 | ret = gnutls_openpgp_crt_get_pk_algorithm(cert, NULL); |
---|
1059 | if (ret >= 0) { |
---|
1060 | apr_table_setn(env, MGS_SIDE("_A_KEY"), gnutls_pk_algorithm_get_name(ret)); |
---|
1061 | } |
---|
1062 | |
---|
1063 | } |
---|
1064 | |
---|
1065 | /* TODO: Allow client sending a X.509 certificate chain */ |
---|
1066 | static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt) { |
---|
1067 | const gnutls_datum_t *cert_list; |
---|
1068 | unsigned int cert_list_size, status; |
---|
1069 | int rv = GNUTLS_E_NO_CERTIFICATE_FOUND, ret; |
---|
1070 | unsigned int ch_size = 0; |
---|
1071 | |
---|
1072 | union { |
---|
1073 | gnutls_x509_crt_t x509[MAX_CHAIN_SIZE]; |
---|
1074 | gnutls_openpgp_crt_t pgp; |
---|
1075 | } cert; |
---|
1076 | apr_time_t expiration_time, cur_time; |
---|
1077 | |
---|
1078 | if (r == NULL || ctxt == NULL || ctxt->session == NULL) |
---|
1079 | return HTTP_FORBIDDEN; |
---|
1080 | |
---|
1081 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1082 | cert_list = |
---|
1083 | gnutls_certificate_get_peers(ctxt->session, &cert_list_size); |
---|
1084 | |
---|
1085 | if (cert_list == NULL || cert_list_size == 0) { |
---|
1086 | /* It is perfectly OK for a client not to send a certificate if on REQUEST mode |
---|
1087 | */ |
---|
1088 | if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST) |
---|
1089 | return OK; |
---|
1090 | |
---|
1091 | /* no certificate provided by the client, but one was required. */ |
---|
1092 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1093 | "GnuTLS: Failed to Verify Peer: " |
---|
1094 | "Client did not submit a certificate"); |
---|
1095 | return HTTP_FORBIDDEN; |
---|
1096 | } |
---|
1097 | |
---|
1098 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
1099 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1100 | "GnuTLS: A Chain of %d certificate(s) was provided for validation", |
---|
1101 | cert_list_size); |
---|
1102 | |
---|
1103 | for (ch_size = 0; ch_size < cert_list_size; ch_size++) { |
---|
1104 | gnutls_x509_crt_init(&cert.x509[ch_size]); |
---|
1105 | rv = gnutls_x509_crt_import(cert.x509[ch_size], |
---|
1106 | &cert_list[ch_size], |
---|
1107 | GNUTLS_X509_FMT_DER); |
---|
1108 | // When failure to import, leave the loop |
---|
1109 | if (rv != GNUTLS_E_SUCCESS) { |
---|
1110 | if (ch_size < 1) { |
---|
1111 | ap_log_rerror(APLOG_MARK, |
---|
1112 | APLOG_INFO, 0, r, |
---|
1113 | "GnuTLS: Failed to Verify Peer: " |
---|
1114 | "Failed to import peer certificates."); |
---|
1115 | ret = HTTP_FORBIDDEN; |
---|
1116 | goto exit; |
---|
1117 | } |
---|
1118 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1119 | "GnuTLS: Failed to import some peer certificates. Using %d certificates", |
---|
1120 | ch_size); |
---|
1121 | rv = GNUTLS_E_SUCCESS; |
---|
1122 | break; |
---|
1123 | } |
---|
1124 | } |
---|
1125 | } else if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_OPENPGP) { |
---|
1126 | if (cert_list_size > 1) { |
---|
1127 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1128 | "GnuTLS: Failed to Verify Peer: " |
---|
1129 | "Chained Client Certificates are not supported."); |
---|
1130 | return HTTP_FORBIDDEN; |
---|
1131 | } |
---|
1132 | |
---|
1133 | gnutls_openpgp_crt_init(&cert.pgp); |
---|
1134 | rv = gnutls_openpgp_crt_import(cert.pgp, &cert_list[0], |
---|
1135 | GNUTLS_OPENPGP_FMT_RAW); |
---|
1136 | |
---|
1137 | } else |
---|
1138 | return HTTP_FORBIDDEN; |
---|
1139 | |
---|
1140 | if (rv < 0) { |
---|
1141 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1142 | "GnuTLS: Failed to Verify Peer: " |
---|
1143 | "Failed to import peer certificates."); |
---|
1144 | ret = HTTP_FORBIDDEN; |
---|
1145 | goto exit; |
---|
1146 | } |
---|
1147 | |
---|
1148 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
1149 | apr_time_ansi_put(&expiration_time, |
---|
1150 | gnutls_x509_crt_get_expiration_time |
---|
1151 | (cert.x509[0])); |
---|
1152 | |
---|
1153 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1154 | "GnuTLS: Verifying list of %d certificate(s) via method '%s'", |
---|
1155 | ch_size, mgs_readable_cvm(ctxt->sc->client_verify_method)); |
---|
1156 | switch(ctxt->sc->client_verify_method) { |
---|
1157 | case mgs_cvm_cartel: |
---|
1158 | rv = gnutls_x509_crt_list_verify(cert.x509, ch_size, |
---|
1159 | ctxt->sc->ca_list, |
---|
1160 | ctxt->sc->ca_list_size, |
---|
1161 | NULL, 0, 0, &status); |
---|
1162 | break; |
---|
1163 | #ifdef ENABLE_MSVA |
---|
1164 | case mgs_cvm_msva: |
---|
1165 | { |
---|
1166 | struct msv_response* resp = NULL; |
---|
1167 | struct msv_query q = { .context="https", .peertype="client", .pkctype="x509pem" }; |
---|
1168 | msv_ctxt_t ctx = msv_ctxt_init(NULL); |
---|
1169 | char cert_pem_buf[10 * 1024]; |
---|
1170 | size_t len = sizeof (cert_pem_buf); |
---|
1171 | |
---|
1172 | rv = 0; |
---|
1173 | if (gnutls_x509_crt_export(cert.x509[0], GNUTLS_X509_FMT_PEM, cert_pem_buf, &len) >= 0) { |
---|
1174 | /* FIXME : put together a name from the cert we received, instead of hard-coding this value: */ |
---|
1175 | q.peername = mgs_x509_construct_uid(r, cert.x509[0]); |
---|
1176 | q.pkcdata = cert_pem_buf; |
---|
1177 | rv = msv_query_agent(ctx, q, &resp); |
---|
1178 | if (rv == LIBMSV_ERROR_SUCCESS) { |
---|
1179 | status = 0; |
---|
1180 | } else if (rv == LIBMSV_ERROR_INVALID) { |
---|
1181 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1182 | "GnuTLS: Monkeysphere validation failed: (message: %s)", resp->message); |
---|
1183 | status = GNUTLS_CERT_INVALID; |
---|
1184 | } else { |
---|
1185 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1186 | "GnuTLS: Error communicating with the Monkeysphere Validation Agent: (%d) %s", rv, msv_strerror(ctx, rv)); |
---|
1187 | status = GNUTLS_CERT_INVALID; |
---|
1188 | rv = -1; |
---|
1189 | } |
---|
1190 | } else { |
---|
1191 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1192 | "GnuTLS: Could not convert the client certificate to PEM format"); |
---|
1193 | status = GNUTLS_CERT_INVALID; |
---|
1194 | rv = GNUTLS_E_ASN1_ELEMENT_NOT_FOUND; |
---|
1195 | } |
---|
1196 | msv_response_destroy(resp); |
---|
1197 | msv_ctxt_destroy(ctx); |
---|
1198 | } |
---|
1199 | break; |
---|
1200 | #endif |
---|
1201 | default: |
---|
1202 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1203 | "GnuTLS: Failed to Verify X.509 Peer: method '%s' is not supported", |
---|
1204 | mgs_readable_cvm(ctxt->sc->client_verify_method)); |
---|
1205 | } |
---|
1206 | |
---|
1207 | } else { |
---|
1208 | apr_time_ansi_put(&expiration_time, |
---|
1209 | gnutls_openpgp_crt_get_expiration_time |
---|
1210 | (cert.pgp)); |
---|
1211 | |
---|
1212 | switch(ctxt->sc->client_verify_method) { |
---|
1213 | case mgs_cvm_cartel: |
---|
1214 | rv = gnutls_openpgp_crt_verify_ring(cert.pgp, |
---|
1215 | ctxt->sc->pgp_list, 0, |
---|
1216 | &status); |
---|
1217 | break; |
---|
1218 | #ifdef ENABLE_MSVA |
---|
1219 | case mgs_cvm_msva: |
---|
1220 | /* need to set status and rv */ |
---|
1221 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1222 | "GnuTLS: OpenPGP verification via MSVA is not yet implemented"); |
---|
1223 | rv = GNUTLS_E_UNIMPLEMENTED_FEATURE; |
---|
1224 | break; |
---|
1225 | #endif |
---|
1226 | default: |
---|
1227 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1228 | "GnuTLS: Failed to Verify OpenPGP Peer: method '%s' is not supported", |
---|
1229 | mgs_readable_cvm(ctxt->sc->client_verify_method)); |
---|
1230 | } |
---|
1231 | } |
---|
1232 | |
---|
1233 | if (rv < 0) { |
---|
1234 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1235 | "GnuTLS: Failed to Verify Peer certificate: (%d) %s", |
---|
1236 | rv, gnutls_strerror(rv)); |
---|
1237 | if (rv == GNUTLS_E_NO_CERTIFICATE_FOUND) |
---|
1238 | ap_log_rerror(APLOG_MARK, APLOG_EMERG, 0, r, |
---|
1239 | "GnuTLS: No certificate was found for verification. Did you set the GnuTLSX509CAFile or GnuTLSPGPKeyringFile directives?"); |
---|
1240 | ret = HTTP_FORBIDDEN; |
---|
1241 | goto exit; |
---|
1242 | } |
---|
1243 | |
---|
1244 | /* TODO: X509 CRL Verification. */ |
---|
1245 | /* May add later if anyone needs it. |
---|
1246 | */ |
---|
1247 | /* ret = gnutls_x509_crt_check_revocation(crt, crl_list, crl_list_size); */ |
---|
1248 | |
---|
1249 | cur_time = apr_time_now(); |
---|
1250 | |
---|
1251 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) { |
---|
1252 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1253 | "GnuTLS: Could not find Signer for Peer Certificate"); |
---|
1254 | } |
---|
1255 | |
---|
1256 | if (status & GNUTLS_CERT_SIGNER_NOT_CA) { |
---|
1257 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1258 | "GnuTLS: Peer's Certificate signer is not a CA"); |
---|
1259 | } |
---|
1260 | |
---|
1261 | if (status & GNUTLS_CERT_INSECURE_ALGORITHM) { |
---|
1262 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1263 | "GnuTLS: Peer's Certificate is using insecure algorithms"); |
---|
1264 | } |
---|
1265 | |
---|
1266 | if (status & GNUTLS_CERT_EXPIRED |
---|
1267 | || status & GNUTLS_CERT_NOT_ACTIVATED) { |
---|
1268 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1269 | "GnuTLS: Peer's Certificate signer is expired or not yet activated"); |
---|
1270 | } |
---|
1271 | |
---|
1272 | if (status & GNUTLS_CERT_INVALID) { |
---|
1273 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1274 | "GnuTLS: Peer Certificate is invalid."); |
---|
1275 | } else if (status & GNUTLS_CERT_REVOKED) { |
---|
1276 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1277 | "GnuTLS: Peer Certificate is revoked."); |
---|
1278 | } |
---|
1279 | |
---|
1280 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) |
---|
1281 | mgs_add_common_cert_vars(r, cert.x509[0], 1, ctxt->sc->export_certificates_size); |
---|
1282 | else if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_OPENPGP) |
---|
1283 | mgs_add_common_pgpcert_vars(r, cert.pgp, 1, ctxt->sc->export_certificates_size); |
---|
1284 | |
---|
1285 | { |
---|
1286 | /* days remaining */ |
---|
1287 | unsigned long remain = |
---|
1288 | (apr_time_sec(expiration_time) - |
---|
1289 | apr_time_sec(cur_time)) / 86400; |
---|
1290 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_V_REMAIN", |
---|
1291 | apr_psprintf(r->pool, "%lu", remain)); |
---|
1292 | } |
---|
1293 | |
---|
1294 | if (status == 0) { |
---|
1295 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", |
---|
1296 | "SUCCESS"); |
---|
1297 | ret = OK; |
---|
1298 | } else { |
---|
1299 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", |
---|
1300 | "FAILED"); |
---|
1301 | if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST) |
---|
1302 | ret = OK; |
---|
1303 | else |
---|
1304 | ret = HTTP_FORBIDDEN; |
---|
1305 | } |
---|
1306 | |
---|
1307 | exit: |
---|
1308 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
1309 | unsigned int i; |
---|
1310 | for (i = 0; i < ch_size; i++) { |
---|
1311 | gnutls_x509_crt_deinit(cert.x509[i]); |
---|
1312 | } |
---|
1313 | } else if (gnutls_certificate_type_get(ctxt->session) == |
---|
1314 | GNUTLS_CRT_OPENPGP) |
---|
1315 | gnutls_openpgp_crt_deinit(cert.pgp); |
---|
1316 | return ret; |
---|
1317 | |
---|
1318 | |
---|
1319 | } |
---|
1320 | |
---|
1321 | #ifdef ENABLE_MSVA |
---|
1322 | /* this section of code is used only when trying to talk to the MSVA */ |
---|
1323 | static const char* mgs_x509_leaf_oid_from_dn(apr_pool_t *pool, const char* oid, gnutls_x509_crt_t cert) { |
---|
1324 | int rv=GNUTLS_E_SUCCESS, i; |
---|
1325 | size_t sz=0, lastsz=0; |
---|
1326 | char* data=NULL; |
---|
1327 | |
---|
1328 | i = -1; |
---|
1329 | while(rv != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { |
---|
1330 | i++; |
---|
1331 | lastsz=sz; |
---|
1332 | sz=0; |
---|
1333 | rv = gnutls_x509_crt_get_dn_by_oid (cert, oid, i, 0, NULL, &sz); |
---|
1334 | } |
---|
1335 | if (i > 0) { |
---|
1336 | data = apr_palloc(pool, lastsz); |
---|
1337 | sz=lastsz; |
---|
1338 | rv = gnutls_x509_crt_get_dn_by_oid (cert, oid, i-1, 0, data, &sz); |
---|
1339 | if (rv == GNUTLS_E_SUCCESS) |
---|
1340 | return data; |
---|
1341 | } |
---|
1342 | return NULL; |
---|
1343 | } |
---|
1344 | |
---|
1345 | static const char* mgs_x509_first_type_from_san(apr_pool_t *pool, gnutls_x509_subject_alt_name_t target, gnutls_x509_crt_t cert) { |
---|
1346 | int rv=GNUTLS_E_SUCCESS; |
---|
1347 | size_t sz; |
---|
1348 | char* data=NULL; |
---|
1349 | unsigned int i; |
---|
1350 | gnutls_x509_subject_alt_name_t thistype; |
---|
1351 | |
---|
1352 | i = 0; |
---|
1353 | while(rv != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { |
---|
1354 | sz = 0; |
---|
1355 | rv = gnutls_x509_crt_get_subject_alt_name2(cert, i, NULL, &sz, &thistype, NULL); |
---|
1356 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && thistype == target) { |
---|
1357 | data = apr_palloc(pool, sz); |
---|
1358 | rv = gnutls_x509_crt_get_subject_alt_name2(cert, i, data, &sz, &thistype, NULL); |
---|
1359 | if (rv >=0 && (thistype == target)) |
---|
1360 | return data; |
---|
1361 | } |
---|
1362 | i++; |
---|
1363 | } |
---|
1364 | return NULL; |
---|
1365 | } |
---|
1366 | |
---|
1367 | |
---|
1368 | /* Create a string representing a candidate User ID from an X.509 |
---|
1369 | * certificate |
---|
1370 | |
---|
1371 | * We need this for client certification because a client gives us a |
---|
1372 | * certificate, but doesn't tell us (in any other way) who they are |
---|
1373 | * trying to authenticate as. |
---|
1374 | |
---|
1375 | * TODO: we might need another parallel for OpenPGP, but for that it's |
---|
1376 | * much simpler: we can just assume that the first User ID marked as |
---|
1377 | * "primary" (or the first User ID, period) is the identity the user |
---|
1378 | * is trying to present as. |
---|
1379 | |
---|
1380 | * one complaint might be "but the user wanted to be another identity, |
---|
1381 | * which is also in the certificate (e.g. in a SubjectAltName)" |
---|
1382 | * However, given that any user can regenerate their own X.509 |
---|
1383 | * certificate with their own public key content, they should just do |
---|
1384 | * so, and not expect us to guess at their identity :) |
---|
1385 | |
---|
1386 | * This function allocates it's response from the pool given it. When |
---|
1387 | * that pool is reclaimed, the response will also be deallocated. |
---|
1388 | |
---|
1389 | * FIXME: what about extracting a server-style cert |
---|
1390 | * (e.g. https://imposter.example) from the DN or any sAN? |
---|
1391 | |
---|
1392 | * FIXME: what if we want to call this outside the context of a |
---|
1393 | * request? That complicates the logging. |
---|
1394 | */ |
---|
1395 | static const char* mgs_x509_construct_uid(request_rec *r, gnutls_x509_crt_t cert) { |
---|
1396 | /* basic strategy, assuming humans are the users: we are going to |
---|
1397 | * try to reconstruct a "conventional" User ID by pulling in a |
---|
1398 | * name, comment, and e-mail address. |
---|
1399 | */ |
---|
1400 | apr_pool_t *pool = r->pool; |
---|
1401 | const char *name=NULL, *comment=NULL, *email=NULL; |
---|
1402 | const char *ret=NULL; |
---|
1403 | /* subpool for temporary allocation: */ |
---|
1404 | apr_pool_t *sp=NULL; |
---|
1405 | |
---|
1406 | if (APR_SUCCESS != apr_pool_create(&sp, pool)) |
---|
1407 | return NULL; /* i'm assuming that libapr would log this kind |
---|
1408 | * of error on its own */ |
---|
1409 | |
---|
1410 | /* Name |
---|
1411 | |
---|
1412 | the name comes from the leaf commonName of the cert's Subject. |
---|
1413 | |
---|
1414 | (MAYBE: should we look at trying to assemble a candidate from |
---|
1415 | givenName, surName, suffix, etc? the "name" field |
---|
1416 | appears to be case-insensitive, which seems problematic |
---|
1417 | from what we expect; see: |
---|
1418 | http://www.itu.int/rec/T-REC-X.520-200102-s/e ) |
---|
1419 | |
---|
1420 | (MAYBE: should we try pulling a commonName or otherName or |
---|
1421 | something from subjectAltName? see: |
---|
1422 | https://tools.ietf.org/html/rfc5280#section-4.2.1.6 |
---|
1423 | GnuTLS does not support looking for Common Names in the |
---|
1424 | SAN yet) |
---|
1425 | */ |
---|
1426 | name = mgs_x509_leaf_oid_from_dn(sp, GNUTLS_OID_X520_COMMON_NAME, cert); |
---|
1427 | |
---|
1428 | /* Comment |
---|
1429 | |
---|
1430 | I am inclined to punt on this for now, as Comment has been so |
---|
1431 | atrociously misused in OpenPGP. Perhaps if there is a |
---|
1432 | pseudonym (OID 2.5.4.65, aka GNUTLS_OID_X520_PSEUDONYM) field |
---|
1433 | in the subject or sAN? |
---|
1434 | */ |
---|
1435 | comment = mgs_x509_leaf_oid_from_dn(sp, GNUTLS_OID_X520_PSEUDONYM, cert); |
---|
1436 | |
---|
1437 | /* E-mail |
---|
1438 | |
---|
1439 | This should be the the first rfc822Name from the sAN. |
---|
1440 | |
---|
1441 | failing that, we'll take the leaf email in the certificate's |
---|
1442 | subject; this is a deprecated use though. |
---|
1443 | */ |
---|
1444 | email = mgs_x509_first_type_from_san(sp, GNUTLS_SAN_RFC822NAME, cert); |
---|
1445 | if (email == NULL) |
---|
1446 | email = mgs_x509_leaf_oid_from_dn(sp, GNUTLS_OID_PKCS9_EMAIL, cert); |
---|
1447 | |
---|
1448 | /* assemble all the parts: */ |
---|
1449 | |
---|
1450 | /* must have at least a name or an e-mail. */ |
---|
1451 | if (name == NULL && email == NULL) { |
---|
1452 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1453 | "GnuTLS: Need either a name or an e-mail address to get a User ID from an X.509 certificate."); |
---|
1454 | goto end; |
---|
1455 | } |
---|
1456 | if (name) { |
---|
1457 | if (comment) { |
---|
1458 | if (email) { |
---|
1459 | ret = apr_psprintf(pool, "%s (%s) <%s>", name, comment, email); |
---|
1460 | } else { |
---|
1461 | ret = apr_psprintf(pool, "%s (%s)", name, comment); |
---|
1462 | } |
---|
1463 | } else { |
---|
1464 | if (email) { |
---|
1465 | ret = apr_psprintf(pool, "%s <%s>", name, email); |
---|
1466 | } else { |
---|
1467 | ret = apr_pstrdup(pool, name); |
---|
1468 | } |
---|
1469 | } |
---|
1470 | } else { |
---|
1471 | if (comment) { |
---|
1472 | ret = apr_psprintf(pool, "(%s) <%s>", comment, email); |
---|
1473 | } else { |
---|
1474 | ret = apr_psprintf(pool, "<%s>", email); |
---|
1475 | } |
---|
1476 | } |
---|
1477 | |
---|
1478 | end: |
---|
1479 | apr_pool_destroy(sp); |
---|
1480 | return ret; |
---|
1481 | } |
---|
1482 | #endif /* ENABLE_MSVA */ |
---|
1483 | |
---|
1484 | static int mgs_status_hook(request_rec *r, int flags __attribute__((unused))) |
---|
1485 | { |
---|
1486 | mgs_srvconf_rec *sc; |
---|
1487 | |
---|
1488 | if (r == NULL) |
---|
1489 | return OK; |
---|
1490 | |
---|
1491 | sc = (mgs_srvconf_rec *) ap_get_module_config(r->server->module_config, &gnutls_module); |
---|
1492 | |
---|
1493 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1494 | |
---|
1495 | ap_rputs("<hr>\n", r); |
---|
1496 | ap_rputs("<h2>GnuTLS Information:</h2>\n<dl>\n", r); |
---|
1497 | |
---|
1498 | ap_rprintf(r, "<dt>GnuTLS version:</dt><dd>%s</dd>\n", gnutls_check_version(NULL)); |
---|
1499 | ap_rputs("<dt>Built against:</dt><dd>" GNUTLS_VERSION "</dd>\n", r); |
---|
1500 | ap_rprintf(r, "<dt>using TLS:</dt><dd>%s</dd>\n", (sc->enabled == GNUTLS_ENABLED_FALSE ? "no" : "yes")); |
---|
1501 | if (sc->enabled != GNUTLS_ENABLED_FALSE) { |
---|
1502 | mgs_handle_t* ctxt; |
---|
1503 | ctxt = ap_get_module_config(r->connection->conn_config, &gnutls_module); |
---|
1504 | if (ctxt && ctxt->session != NULL) { |
---|
1505 | #if GNUTLS_VERSION_MAJOR < 3 |
---|
1506 | ap_rprintf(r, "<dt>This TLS Session:</dt><dd>%s</dd>\n", |
---|
1507 | gnutls_cipher_suite_get_name(gnutls_kx_get(ctxt->session), |
---|
1508 | gnutls_cipher_get(ctxt->session), |
---|
1509 | gnutls_mac_get(ctxt->session))); |
---|
1510 | #else |
---|
1511 | char* z = NULL; |
---|
1512 | z = gnutls_session_get_desc(ctxt->session); |
---|
1513 | if (z) { |
---|
1514 | ap_rprintf(r, "<dt>This TLS Session:</dt><dd>%s</dd>\n", z); |
---|
1515 | gnutls_free(z); |
---|
1516 | } |
---|
1517 | #endif |
---|
1518 | } |
---|
1519 | } |
---|
1520 | |
---|
1521 | ap_rputs("</dl>\n", r); |
---|
1522 | return OK; |
---|
1523 | } |
---|
1524 | |
---|