[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 <gcrypt.h> |
---|
| 31 | #include <gnutls/gnutls.h> |
---|
| 32 | #include <gnutls/x509.h> |
---|
| 33 | |
---|
[05d56ce] | 34 | #ifndef __mod_gnutls_h_inc |
---|
| 35 | #define __mod_gnutls_h_inc |
---|
| 36 | |
---|
[6e0bfd6] | 37 | #define HAVE_APR_MEMCACHE @have_apr_memcache@ |
---|
[a66e147] | 38 | |
---|
[7e2b223] | 39 | module AP_MODULE_DECLARE_DATA gnutls_module; |
---|
| 40 | |
---|
| 41 | #define GNUTLS_OUTPUT_FILTER_NAME "gnutls_output_filter" |
---|
| 42 | #define GNUTLS_INPUT_FILTER_NAME "gnutls_input_filter" |
---|
| 43 | |
---|
| 44 | #define GNUTLS_ENABLED_FALSE 0 |
---|
| 45 | #define GNUTLS_ENABLED_TRUE 1 |
---|
| 46 | |
---|
[42307a9] | 47 | #define MOD_GNUTLS_VERSION "@MOD_GNUTLS_VERSION@" |
---|
| 48 | |
---|
[5a6446d] | 49 | #define MOD_GNUTLS_DEBUG @OOO_MAINTAIN@ |
---|
| 50 | |
---|
[482f47f] | 51 | /* Recent Versions of 2.1 renamed several hooks. This allows us to |
---|
| 52 | compile on 2.0.xx */ |
---|
| 53 | #if AP_SERVER_MINORVERSION_NUMBER >= 1 |
---|
| 54 | #if AP_SERVER_PATCHLEVEL_NUMBER >= 3 |
---|
| 55 | #define USING_2_1_RECENT 1 |
---|
| 56 | #endif |
---|
| 57 | #endif |
---|
| 58 | |
---|
| 59 | #ifndef USING_2_1_RECENT |
---|
| 60 | #define USING_2_1_RECENT 0 |
---|
| 61 | #endif |
---|
| 62 | |
---|
[fcb122d] | 63 | typedef enum |
---|
| 64 | { |
---|
[c301152] | 65 | mgs_cache_none, |
---|
| 66 | mgs_cache_dbm, |
---|
[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; |
---|
[c301152] | 75 | } mgs_dirconf_rec; |
---|
[31645b2] | 76 | |
---|
| 77 | typedef struct |
---|
| 78 | { |
---|
[7e2b223] | 79 | gnutls_certificate_credentials_t certs; |
---|
[e924ddd] | 80 | char* cert_cn; |
---|
[31645b2] | 81 | gnutls_x509_crt_t cert_x509; |
---|
| 82 | gnutls_x509_privkey_t privkey_x509; |
---|
[7e2b223] | 83 | int enabled; |
---|
| 84 | int ciphers[16]; |
---|
| 85 | int key_exchange[16]; |
---|
| 86 | int macs[16]; |
---|
| 87 | int protocol[16]; |
---|
| 88 | int compression[16]; |
---|
[fcb122d] | 89 | int cert_types[16]; |
---|
| 90 | apr_time_t cache_timeout; |
---|
[c301152] | 91 | mgs_cache_e cache_type; |
---|
[a66e147] | 92 | const char* cache_config; |
---|
[fcb122d] | 93 | const char* rsa_params_file; |
---|
| 94 | const char* dh_params_file; |
---|
[e924ddd] | 95 | int client_verify_mode; |
---|
[c301152] | 96 | } mgs_srvconf_rec; |
---|
[7e2b223] | 97 | |
---|
[dae0aec] | 98 | typedef struct { |
---|
| 99 | int length; |
---|
| 100 | char *value; |
---|
[c301152] | 101 | } mgs_char_buffer_t; |
---|
[dae0aec] | 102 | |
---|
[2e12226] | 103 | typedef struct |
---|
[7e2b223] | 104 | { |
---|
[c301152] | 105 | mgs_srvconf_rec *sc; |
---|
[dae0aec] | 106 | conn_rec* c; |
---|
[7e2b223] | 107 | gnutls_session_t session; |
---|
[dae0aec] | 108 | |
---|
| 109 | apr_status_t input_rc; |
---|
[7e2b223] | 110 | ap_filter_t *input_filter; |
---|
| 111 | apr_bucket_brigade *input_bb; |
---|
| 112 | apr_read_type_e input_block; |
---|
[dae0aec] | 113 | ap_input_mode_t input_mode; |
---|
[c301152] | 114 | mgs_char_buffer_t input_cbuf; |
---|
[dae0aec] | 115 | char input_buffer[AP_IOBUFSIZE]; |
---|
| 116 | |
---|
| 117 | apr_status_t output_rc; |
---|
| 118 | ap_filter_t *output_filter; |
---|
| 119 | apr_bucket_brigade *output_bb; |
---|
| 120 | char output_buffer[AP_IOBUFSIZE]; |
---|
| 121 | apr_size_t output_blen; |
---|
| 122 | apr_size_t output_length; |
---|
| 123 | |
---|
[7e2b223] | 124 | int status; |
---|
| 125 | int non_https; |
---|
[c301152] | 126 | } mgs_handle_t; |
---|
[7e2b223] | 127 | |
---|
| 128 | /** Functions in gnutls_io.c **/ |
---|
| 129 | |
---|
| 130 | /** |
---|
[c301152] | 131 | * mgs_filter_input will filter the input data |
---|
[7e2b223] | 132 | * by decrypting it using GnuTLS and passes it cleartext. |
---|
| 133 | * |
---|
| 134 | * @param f the filter info record |
---|
| 135 | * @param bb the bucket brigade, where to store the result to |
---|
| 136 | * @param mode what shall we read? |
---|
| 137 | * @param block a block index we shall read from? |
---|
| 138 | * @return result status |
---|
| 139 | */ |
---|
[c301152] | 140 | apr_status_t mgs_filter_input(ap_filter_t * f, |
---|
[2e12226] | 141 | apr_bucket_brigade * bb, |
---|
| 142 | ap_input_mode_t mode, |
---|
| 143 | apr_read_type_e block, |
---|
| 144 | apr_off_t readbytes); |
---|
[7e2b223] | 145 | |
---|
| 146 | /** |
---|
[c301152] | 147 | * mgs_filter_output will filter the encrypt |
---|
[7e2b223] | 148 | * the incoming bucket using GnuTLS and passes it onto the next filter. |
---|
| 149 | * |
---|
| 150 | * @param f the filter info record |
---|
| 151 | * @param bb the bucket brigade, where to store the result to |
---|
| 152 | * @return result status |
---|
| 153 | */ |
---|
[c301152] | 154 | apr_status_t mgs_filter_output(ap_filter_t * f, |
---|
[2e12226] | 155 | apr_bucket_brigade * bb); |
---|
[7e2b223] | 156 | |
---|
| 157 | |
---|
| 158 | /** |
---|
[c301152] | 159 | * mgs_transport_read is called from GnuTLS to provide encrypted |
---|
[7e2b223] | 160 | * data from the client. |
---|
| 161 | * |
---|
| 162 | * @param ptr pointer to the filter context |
---|
| 163 | * @param buffer place to put data |
---|
| 164 | * @param len maximum size |
---|
| 165 | * @return size length of the data stored in buffer |
---|
| 166 | */ |
---|
[c301152] | 167 | ssize_t mgs_transport_read(gnutls_transport_ptr_t ptr, |
---|
[2e12226] | 168 | void *buffer, size_t len); |
---|
[7e2b223] | 169 | |
---|
| 170 | /** |
---|
[c301152] | 171 | * mgs_transport_write is called from GnuTLS to |
---|
[7e2b223] | 172 | * write data to the client. |
---|
| 173 | * |
---|
| 174 | * @param ptr pointer to the filter context |
---|
| 175 | * @param buffer buffer to write to the client |
---|
| 176 | * @param len size of the buffer |
---|
| 177 | * @return size length of the data written |
---|
| 178 | */ |
---|
[c301152] | 179 | ssize_t mgs_transport_write(gnutls_transport_ptr_t ptr, |
---|
[2e12226] | 180 | const void *buffer, size_t len); |
---|
[7e2b223] | 181 | |
---|
| 182 | |
---|
[c301152] | 183 | int mgs_rehandshake(mgs_handle_t * ctxt); |
---|
[e924ddd] | 184 | |
---|
| 185 | |
---|
| 186 | |
---|
[a66e147] | 187 | /** |
---|
[fcb122d] | 188 | * Init the Cache after Configuration is done |
---|
| 189 | */ |
---|
[c301152] | 190 | int mgs_cache_post_config(apr_pool_t *p, server_rec *s, |
---|
| 191 | mgs_srvconf_rec *sc); |
---|
[fcb122d] | 192 | /** |
---|
[a66e147] | 193 | * Init the Cache inside each Process |
---|
| 194 | */ |
---|
[c301152] | 195 | int mgs_cache_child_init(apr_pool_t *p, server_rec *s, |
---|
| 196 | mgs_srvconf_rec *sc); |
---|
[a66e147] | 197 | /** |
---|
| 198 | * Setup the Session Caching |
---|
| 199 | */ |
---|
[c301152] | 200 | int mgs_cache_session_init(mgs_handle_t *ctxt); |
---|
[05d56ce] | 201 | |
---|
[42307a9] | 202 | #define GNUTLS_SESSION_ID_STRING_LEN \ |
---|
| 203 | ((GNUTLS_MAX_SESSION_ID + 1) * 2) |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Convert a SSL Session ID into a Null Terminated Hex Encoded String |
---|
| 207 | * @param id raw SSL Session ID |
---|
| 208 | * @param idlen Length of the raw Session ID |
---|
| 209 | * @param str Location to store the Hex Encoded String |
---|
| 210 | * @param strsize The Maximum Length that can be stored in str |
---|
| 211 | */ |
---|
[c301152] | 212 | char *mgs_session_id2sz(unsigned char *id, int idlen, |
---|
[42307a9] | 213 | char *str, int strsize); |
---|
| 214 | |
---|
[c301152] | 215 | |
---|
[46b85d8] | 216 | /* Configuration Functions */ |
---|
| 217 | |
---|
| 218 | const char *mgs_set_cert_file(cmd_parms * parms, void *dummy, |
---|
| 219 | const char *arg); |
---|
| 220 | |
---|
| 221 | const char *mgs_set_key_file(cmd_parms * parms, void *dummy, |
---|
| 222 | const char *arg); |
---|
| 223 | |
---|
| 224 | const char *mgs_set_cache(cmd_parms * parms, void *dummy, |
---|
| 225 | const char *type, const char* arg); |
---|
| 226 | |
---|
| 227 | const char *mgs_set_cache_timeout(cmd_parms * parms, void *dummy, |
---|
| 228 | const char *arg); |
---|
| 229 | |
---|
| 230 | const char *mgs_set_client_verify(cmd_parms * parms, void *dummy, |
---|
| 231 | const char *arg); |
---|
| 232 | |
---|
| 233 | const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy, |
---|
| 234 | const char *arg); |
---|
| 235 | |
---|
| 236 | const char *mgs_set_enabled(cmd_parms * parms, void *dummy, |
---|
| 237 | const char *arg); |
---|
| 238 | |
---|
| 239 | void *mgs_config_server_create(apr_pool_t * p, server_rec * s); |
---|
| 240 | |
---|
| 241 | void *mgs_config_dir_create(apr_pool_t *p, char *dir); |
---|
| 242 | |
---|
[836417f] | 243 | mgs_srvconf_rec* mgs_find_sni_server(gnutls_session_t session); |
---|
[c301152] | 244 | |
---|
| 245 | /* mod_gnutls Hooks. */ |
---|
| 246 | |
---|
| 247 | int mgs_hook_pre_config(apr_pool_t * pconf, |
---|
| 248 | apr_pool_t * plog, apr_pool_t * ptemp); |
---|
| 249 | |
---|
| 250 | int mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog, |
---|
| 251 | apr_pool_t * ptemp, |
---|
| 252 | server_rec * base_server); |
---|
| 253 | |
---|
| 254 | void mgs_hook_child_init(apr_pool_t *p, server_rec *s); |
---|
| 255 | |
---|
| 256 | const char *mgs_hook_http_scheme(const request_rec * r); |
---|
| 257 | |
---|
| 258 | apr_port_t mgs_hook_default_port(const request_rec * r); |
---|
| 259 | |
---|
| 260 | int mgs_hook_pre_connection(conn_rec * c, void *csd); |
---|
| 261 | |
---|
| 262 | int mgs_hook_fixups(request_rec *r); |
---|
| 263 | |
---|
| 264 | int mgs_hook_authz(request_rec *r); |
---|
| 265 | |
---|
[7e2b223] | 266 | #endif /* __mod_gnutls_h_inc */ |
---|