[fcb122d] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[7e2b223] | 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 | #include "httpd.h" |
---|
| 19 | #include "http_config.h" |
---|
| 20 | #include "http_protocol.h" |
---|
| 21 | #include "http_connection.h" |
---|
[76bd3bf] | 22 | #include "http_request.h" |
---|
[7e2b223] | 23 | #include "http_core.h" |
---|
| 24 | #include "http_log.h" |
---|
| 25 | #include "apr_buckets.h" |
---|
| 26 | #include "apr_strings.h" |
---|
| 27 | #include "apr_tables.h" |
---|
[482f47f] | 28 | #include "ap_release.h" |
---|
[7e2b223] | 29 | |
---|
[31645b2] | 30 | #include <gnutls/gnutls.h> |
---|
[e5bbda4] | 31 | #include <gnutls/extra.h> |
---|
| 32 | #include <gnutls/openpgp.h> |
---|
[31645b2] | 33 | #include <gnutls/x509.h> |
---|
| 34 | |
---|
[05d56ce] | 35 | #ifndef __mod_gnutls_h_inc |
---|
| 36 | #define __mod_gnutls_h_inc |
---|
| 37 | |
---|
[6e0bfd6] | 38 | #define HAVE_APR_MEMCACHE @have_apr_memcache@ |
---|
[a66e147] | 39 | |
---|
[6d4de37] | 40 | extern module AP_MODULE_DECLARE_DATA gnutls_module; |
---|
[7e2b223] | 41 | |
---|
| 42 | #define GNUTLS_OUTPUT_FILTER_NAME "gnutls_output_filter" |
---|
| 43 | #define GNUTLS_INPUT_FILTER_NAME "gnutls_input_filter" |
---|
| 44 | |
---|
| 45 | #define GNUTLS_ENABLED_FALSE 0 |
---|
| 46 | #define GNUTLS_ENABLED_TRUE 1 |
---|
| 47 | |
---|
[42307a9] | 48 | #define MOD_GNUTLS_VERSION "@MOD_GNUTLS_VERSION@" |
---|
| 49 | |
---|
[5a6446d] | 50 | #define MOD_GNUTLS_DEBUG @OOO_MAINTAIN@ |
---|
| 51 | |
---|
[482f47f] | 52 | /* Recent Versions of 2.1 renamed several hooks. This allows us to |
---|
| 53 | compile on 2.0.xx */ |
---|
[7e67487] | 54 | #if AP_SERVER_MINORVERSION_NUMBER >= 2 || (AP_SERVER_MINORVERSION_NUMBER == 1 && AP_SERVER_PATCHLEVEL_NUMBER >= 3) |
---|
[482f47f] | 55 | #define USING_2_1_RECENT 1 |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | #ifndef USING_2_1_RECENT |
---|
| 59 | #define USING_2_1_RECENT 0 |
---|
| 60 | #endif |
---|
| 61 | |
---|
[fcb122d] | 62 | typedef enum |
---|
| 63 | { |
---|
[c301152] | 64 | mgs_cache_none, |
---|
| 65 | mgs_cache_dbm, |
---|
[d8c7cf4] | 66 | mgs_cache_gdbm, |
---|
[fcb122d] | 67 | #if HAVE_APR_MEMCACHE |
---|
[c301152] | 68 | mgs_cache_memcache |
---|
[2e12226] | 69 | #endif |
---|
[c301152] | 70 | } mgs_cache_e; |
---|
[2e12226] | 71 | |
---|
[7e2b223] | 72 | typedef struct |
---|
| 73 | { |
---|
[e924ddd] | 74 | int client_verify_mode; |
---|
[84cb5b2] | 75 | const char* lua_bytecode; |
---|
| 76 | apr_size_t lua_bytecode_len; |
---|
[c301152] | 77 | } mgs_dirconf_rec; |
---|
[31645b2] | 78 | |
---|
[7bebb42] | 79 | |
---|
[5e81262] | 80 | /* The maximum number of certificates to send in a chain |
---|
| 81 | */ |
---|
| 82 | #define MAX_CHAIN_SIZE 8 |
---|
[7bebb42] | 83 | |
---|
[31645b2] | 84 | typedef struct |
---|
| 85 | { |
---|
[7e2b223] | 86 | gnutls_certificate_credentials_t certs; |
---|
[7bebb42] | 87 | gnutls_srp_server_credentials_t srp_creds; |
---|
| 88 | gnutls_anon_server_credentials_t anon_creds; |
---|
[e924ddd] | 89 | char* cert_cn; |
---|
[5e81262] | 90 | gnutls_x509_crt_t certs_x509[MAX_CHAIN_SIZE]; /* A certificate chain */ |
---|
| 91 | unsigned int certs_x509_num; |
---|
[31645b2] | 92 | gnutls_x509_privkey_t privkey_x509; |
---|
[e5bbda4] | 93 | gnutls_openpgp_crt_t cert_pgp; /* A certificate chain */ |
---|
| 94 | gnutls_openpgp_privkey_t privkey_pgp; |
---|
[7e2b223] | 95 | int enabled; |
---|
[7bebb42] | 96 | /* whether to send the PEM encoded certificates |
---|
| 97 | * to CGIs |
---|
| 98 | */ |
---|
| 99 | int export_certificates_enabled; |
---|
| 100 | gnutls_priority_t priorities; |
---|
[a3c97d1] | 101 | gnutls_rsa_params_t rsa_params; |
---|
| 102 | gnutls_dh_params_t dh_params; |
---|
[7bebb42] | 103 | int cache_timeout; |
---|
[c301152] | 104 | mgs_cache_e cache_type; |
---|
[a66e147] | 105 | const char* cache_config; |
---|
[7bebb42] | 106 | const char* srp_tpasswd_file; |
---|
| 107 | const char* srp_tpasswd_conf_file; |
---|
[8663ace] | 108 | gnutls_x509_crt_t *ca_list; |
---|
[e5bbda4] | 109 | gnutls_openpgp_keyring_t pgp_list; |
---|
[7bebb42] | 110 | unsigned int ca_list_size; |
---|
[e924ddd] | 111 | int client_verify_mode; |
---|
[f10ab4f] | 112 | apr_time_t last_cache_check; |
---|
[ae233c2] | 113 | int tickets; /* whether session tickets are allowed */ |
---|
[c301152] | 114 | } mgs_srvconf_rec; |
---|
[7e2b223] | 115 | |
---|
[dae0aec] | 116 | typedef struct { |
---|
| 117 | int length; |
---|
| 118 | char *value; |
---|
[c301152] | 119 | } mgs_char_buffer_t; |
---|
[dae0aec] | 120 | |
---|
[2e12226] | 121 | typedef struct |
---|
[7e2b223] | 122 | { |
---|
[c301152] | 123 | mgs_srvconf_rec *sc; |
---|
[dae0aec] | 124 | conn_rec* c; |
---|
[7e2b223] | 125 | gnutls_session_t session; |
---|
[dae0aec] | 126 | |
---|
| 127 | apr_status_t input_rc; |
---|
[7e2b223] | 128 | ap_filter_t *input_filter; |
---|
| 129 | apr_bucket_brigade *input_bb; |
---|
| 130 | apr_read_type_e input_block; |
---|
[dae0aec] | 131 | ap_input_mode_t input_mode; |
---|
[c301152] | 132 | mgs_char_buffer_t input_cbuf; |
---|
[dae0aec] | 133 | char input_buffer[AP_IOBUFSIZE]; |
---|
| 134 | |
---|
| 135 | apr_status_t output_rc; |
---|
| 136 | ap_filter_t *output_filter; |
---|
| 137 | apr_bucket_brigade *output_bb; |
---|
| 138 | char output_buffer[AP_IOBUFSIZE]; |
---|
| 139 | apr_size_t output_blen; |
---|
| 140 | apr_size_t output_length; |
---|
| 141 | |
---|
[7e2b223] | 142 | int status; |
---|
| 143 | int non_https; |
---|
[c301152] | 144 | } mgs_handle_t; |
---|
[7e2b223] | 145 | |
---|
| 146 | /** Functions in gnutls_io.c **/ |
---|
| 147 | |
---|
| 148 | /** |
---|
[8fffed1] | 149 | * write_flush will flush data |
---|
| 150 | */ |
---|
| 151 | static ssize_t write_flush(mgs_handle_t * ctxt); |
---|
| 152 | |
---|
| 153 | /** |
---|
[c301152] | 154 | * mgs_filter_input will filter the input data |
---|
[7e2b223] | 155 | * by decrypting it using GnuTLS and passes it cleartext. |
---|
| 156 | * |
---|
| 157 | * @param f the filter info record |
---|
| 158 | * @param bb the bucket brigade, where to store the result to |
---|
| 159 | * @param mode what shall we read? |
---|
| 160 | * @param block a block index we shall read from? |
---|
| 161 | * @return result status |
---|
| 162 | */ |
---|
[c301152] | 163 | apr_status_t mgs_filter_input(ap_filter_t * f, |
---|
[2e12226] | 164 | apr_bucket_brigade * bb, |
---|
| 165 | ap_input_mode_t mode, |
---|
| 166 | apr_read_type_e block, |
---|
| 167 | apr_off_t readbytes); |
---|
[7e2b223] | 168 | |
---|
| 169 | /** |
---|
[c301152] | 170 | * mgs_filter_output will filter the encrypt |
---|
[7e2b223] | 171 | * the incoming bucket using GnuTLS and passes it onto the next filter. |
---|
| 172 | * |
---|
| 173 | * @param f the filter info record |
---|
| 174 | * @param bb the bucket brigade, where to store the result to |
---|
| 175 | * @return result status |
---|
| 176 | */ |
---|
[c301152] | 177 | apr_status_t mgs_filter_output(ap_filter_t * f, |
---|
[2e12226] | 178 | apr_bucket_brigade * bb); |
---|
[7e2b223] | 179 | |
---|
| 180 | |
---|
| 181 | /** |
---|
[c301152] | 182 | * mgs_transport_read is called from GnuTLS to provide encrypted |
---|
[7e2b223] | 183 | * data from the client. |
---|
| 184 | * |
---|
| 185 | * @param ptr pointer to the filter context |
---|
| 186 | * @param buffer place to put data |
---|
| 187 | * @param len maximum size |
---|
| 188 | * @return size length of the data stored in buffer |
---|
| 189 | */ |
---|
[c301152] | 190 | ssize_t mgs_transport_read(gnutls_transport_ptr_t ptr, |
---|
[2e12226] | 191 | void *buffer, size_t len); |
---|
[7e2b223] | 192 | |
---|
| 193 | /** |
---|
[c301152] | 194 | * mgs_transport_write is called from GnuTLS to |
---|
[7e2b223] | 195 | * write data to the client. |
---|
| 196 | * |
---|
| 197 | * @param ptr pointer to the filter context |
---|
| 198 | * @param buffer buffer to write to the client |
---|
| 199 | * @param len size of the buffer |
---|
| 200 | * @return size length of the data written |
---|
| 201 | */ |
---|
[c301152] | 202 | ssize_t mgs_transport_write(gnutls_transport_ptr_t ptr, |
---|
[2e12226] | 203 | const void *buffer, size_t len); |
---|
[7e2b223] | 204 | |
---|
| 205 | |
---|
[c301152] | 206 | int mgs_rehandshake(mgs_handle_t * ctxt); |
---|
[e924ddd] | 207 | |
---|
| 208 | |
---|
| 209 | |
---|
[a66e147] | 210 | /** |
---|
[fcb122d] | 211 | * Init the Cache after Configuration is done |
---|
| 212 | */ |
---|
[c301152] | 213 | int mgs_cache_post_config(apr_pool_t *p, server_rec *s, |
---|
| 214 | mgs_srvconf_rec *sc); |
---|
[fcb122d] | 215 | /** |
---|
[a66e147] | 216 | * Init the Cache inside each Process |
---|
| 217 | */ |
---|
[c301152] | 218 | int mgs_cache_child_init(apr_pool_t *p, server_rec *s, |
---|
| 219 | mgs_srvconf_rec *sc); |
---|
[a66e147] | 220 | /** |
---|
| 221 | * Setup the Session Caching |
---|
| 222 | */ |
---|
[c301152] | 223 | int mgs_cache_session_init(mgs_handle_t *ctxt); |
---|
[05d56ce] | 224 | |
---|
[42307a9] | 225 | #define GNUTLS_SESSION_ID_STRING_LEN \ |
---|
| 226 | ((GNUTLS_MAX_SESSION_ID + 1) * 2) |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Convert a SSL Session ID into a Null Terminated Hex Encoded String |
---|
| 230 | * @param id raw SSL Session ID |
---|
| 231 | * @param idlen Length of the raw Session ID |
---|
| 232 | * @param str Location to store the Hex Encoded String |
---|
| 233 | * @param strsize The Maximum Length that can be stored in str |
---|
| 234 | */ |
---|
[c301152] | 235 | char *mgs_session_id2sz(unsigned char *id, int idlen, |
---|
[42307a9] | 236 | char *str, int strsize); |
---|
| 237 | |
---|
[7bebb42] | 238 | /** |
---|
| 239 | * Convert a time_t into a Null Terminated String |
---|
| 240 | * @param t time_t time |
---|
| 241 | * @param str Location to store the Hex Encoded String |
---|
| 242 | * @param strsize The Maximum Length that can be stored in str |
---|
| 243 | */ |
---|
| 244 | char *mgs_time2sz(time_t t, char *str, int strsize); |
---|
| 245 | |
---|
[c301152] | 246 | |
---|
[46b85d8] | 247 | /* Configuration Functions */ |
---|
| 248 | |
---|
[7bebb42] | 249 | const char *mgs_set_srp_tpasswd_conf_file(cmd_parms * parms, void *dummy, |
---|
| 250 | const char *arg); |
---|
| 251 | const char *mgs_set_srp_tpasswd_file(cmd_parms * parms, void *dummy, |
---|
| 252 | const char *arg); |
---|
| 253 | const char *mgs_set_dh_file(cmd_parms * parms, void *dummy, |
---|
| 254 | const char *arg); |
---|
| 255 | const char *mgs_set_rsa_export_file(cmd_parms * parms, void *dummy, |
---|
| 256 | const char *arg); |
---|
[46b85d8] | 257 | const char *mgs_set_cert_file(cmd_parms * parms, void *dummy, |
---|
| 258 | const char *arg); |
---|
| 259 | |
---|
| 260 | const char *mgs_set_key_file(cmd_parms * parms, void *dummy, |
---|
| 261 | const char *arg); |
---|
| 262 | |
---|
[e5bbda4] | 263 | const char *mgs_set_pgpcert_file(cmd_parms * parms, void *dummy, |
---|
| 264 | const char *arg); |
---|
| 265 | |
---|
| 266 | const char *mgs_set_pgpkey_file(cmd_parms * parms, void *dummy, |
---|
| 267 | const char *arg); |
---|
| 268 | |
---|
[46b85d8] | 269 | const char *mgs_set_cache(cmd_parms * parms, void *dummy, |
---|
| 270 | const char *type, const char* arg); |
---|
| 271 | |
---|
| 272 | const char *mgs_set_cache_timeout(cmd_parms * parms, void *dummy, |
---|
| 273 | const char *arg); |
---|
| 274 | |
---|
| 275 | const char *mgs_set_client_verify(cmd_parms * parms, void *dummy, |
---|
| 276 | const char *arg); |
---|
| 277 | |
---|
| 278 | const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy, |
---|
| 279 | const char *arg); |
---|
| 280 | |
---|
[e5bbda4] | 281 | const char *mgs_set_keyring_file(cmd_parms * parms, void *dummy, |
---|
| 282 | const char *arg); |
---|
| 283 | |
---|
[46b85d8] | 284 | const char *mgs_set_enabled(cmd_parms * parms, void *dummy, |
---|
| 285 | const char *arg); |
---|
[7bebb42] | 286 | const char *mgs_set_export_certificates_enabled(cmd_parms * parms, void *dummy, |
---|
| 287 | const char *arg); |
---|
| 288 | const char *mgs_set_priorities(cmd_parms * parms, void *dummy, |
---|
| 289 | const char *arg); |
---|
[ae233c2] | 290 | const char *mgs_set_tickets(cmd_parms * parms, void *dummy, |
---|
| 291 | const char *arg); |
---|
[84cb5b2] | 292 | |
---|
| 293 | const char *mgs_set_require_section(cmd_parms *cmd, |
---|
| 294 | void *mconfig, const char *arg); |
---|
[46b85d8] | 295 | void *mgs_config_server_create(apr_pool_t * p, server_rec * s); |
---|
| 296 | |
---|
[84cb5b2] | 297 | void *mgs_config_dir_merge(apr_pool_t *p, void *basev, void *addv); |
---|
| 298 | |
---|
[46b85d8] | 299 | void *mgs_config_dir_create(apr_pool_t *p, char *dir); |
---|
| 300 | |
---|
[84cb5b2] | 301 | const char *mgs_set_require_bytecode(cmd_parms *cmd, |
---|
| 302 | void *mconfig, const char *arg); |
---|
| 303 | |
---|
[836417f] | 304 | mgs_srvconf_rec* mgs_find_sni_server(gnutls_session_t session); |
---|
[c301152] | 305 | |
---|
| 306 | /* mod_gnutls Hooks. */ |
---|
| 307 | |
---|
| 308 | int mgs_hook_pre_config(apr_pool_t * pconf, |
---|
| 309 | apr_pool_t * plog, apr_pool_t * ptemp); |
---|
| 310 | |
---|
| 311 | int mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog, |
---|
| 312 | apr_pool_t * ptemp, |
---|
| 313 | server_rec * base_server); |
---|
| 314 | |
---|
| 315 | void mgs_hook_child_init(apr_pool_t *p, server_rec *s); |
---|
| 316 | |
---|
| 317 | const char *mgs_hook_http_scheme(const request_rec * r); |
---|
| 318 | |
---|
| 319 | apr_port_t mgs_hook_default_port(const request_rec * r); |
---|
| 320 | |
---|
| 321 | int mgs_hook_pre_connection(conn_rec * c, void *csd); |
---|
| 322 | |
---|
| 323 | int mgs_hook_fixups(request_rec *r); |
---|
| 324 | |
---|
| 325 | int mgs_hook_authz(request_rec *r); |
---|
| 326 | |
---|
[84cb5b2] | 327 | int mgs_authz_lua(request_rec* r); |
---|
| 328 | |
---|
[7e2b223] | 329 | #endif /* __mod_gnutls_h_inc */ |
---|