00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #include <sys/types.h>
00049 #include <sys/socket.h>
00050 #include <arpa/inet.h>
00051 #include "../../locking.h"
00052 #include "../../sr_module.h"
00053 #include "../../ip_addr.h"
00054 #include "../../trim.h"
00055 #include "../../globals.h"
00056 #include "../../timer_ticks.h"
00057 #include "../../timer.h"
00058 #include "../../tls_hooks.h"
00059 #include "../../ut.h"
00060 #include "../../shm_init.h"
00061 #include "../../rpc_lookup.h"
00062 #include "../../cfg/cfg.h"
00063 #include "tls_init.h"
00064 #include "tls_server.h"
00065 #include "tls_domain.h"
00066 #include "tls_select.h"
00067 #include "tls_config.h"
00068 #include "tls_rpc.h"
00069 #include "tls_util.h"
00070 #include "tls_mod.h"
00071 #include "tls_cfg.h"
00072
00073 #ifndef TLS_HOOKS
00074 #error "TLS_HOOKS must be defined, or the tls module won't work"
00075 #endif
00076 #ifdef CORE_TLS
00077 #error "conflict: CORE_TLS must _not_ be defined"
00078 #endif
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 static int mod_init(void);
00099 static int mod_child(int rank);
00100 static void destroy(void);
00101
00102 static int is_peer_verified(struct sip_msg* msg, char* foo, char* foo2);
00103
00104 MODULE_VERSION
00105
00106
00107
00108
00109
00110 static tls_domain_t mod_params = {
00111 TLS_DOMAIN_DEF | TLS_DOMAIN_SRV,
00112 {},
00113 0,
00114 0,
00115 STR_STATIC_INIT(TLS_CERT_FILE),
00116 STR_STATIC_INIT(TLS_PKEY_FILE),
00117 0,
00118 9,
00119 STR_STATIC_INIT(TLS_CA_FILE),
00120 0,
00121 {0, },
00122 TLS_USE_TLSv1,
00123 STR_STATIC_INIT(TLS_CRL_FILE),
00124 0
00125 };
00126
00127
00128
00129
00130
00131 tls_domain_t srv_defaults = {
00132 TLS_DOMAIN_DEF | TLS_DOMAIN_SRV,
00133 {},
00134 0,
00135 0,
00136 STR_STATIC_INIT(TLS_CERT_FILE),
00137 STR_STATIC_INIT(TLS_PKEY_FILE),
00138 0,
00139 9,
00140 STR_STATIC_INIT(TLS_CA_FILE),
00141 0,
00142 {0, 0},
00143 TLS_USE_TLSv1,
00144 STR_STATIC_INIT(TLS_CRL_FILE),
00145 0
00146 };
00147
00148
00149
00150
00151
00152 tls_domain_t cli_defaults = {
00153 TLS_DOMAIN_DEF | TLS_DOMAIN_CLI,
00154 {},
00155 0,
00156 0,
00157 {0, 0},
00158 {0, 0},
00159 0,
00160 9,
00161 STR_STATIC_INIT(TLS_CA_FILE),
00162 0,
00163 {0, 0},
00164 TLS_USE_TLSv1,
00165 {0, 0},
00166 0
00167 };
00168
00169
00170
00171
00172 tls_domains_cfg_t** tls_domains_cfg = NULL;
00173
00174
00175 gen_lock_t* tls_domains_cfg_lock = NULL;
00176
00177
00178 int sr_tls_renegotiation = 0;
00179
00180
00181
00182
00183 static cmd_export_t cmds[] = {
00184 {"is_peer_verified", (cmd_function)is_peer_verified, 0, 0, 0,
00185 REQUEST_ROUTE},
00186 {0,0,0,0,0,0}
00187 };
00188
00189
00190
00191
00192
00193 static param_export_t params[] = {
00194 {"tls_method", PARAM_STR, &default_tls_cfg.method },
00195 {"verify_certificate", PARAM_INT, &default_tls_cfg.verify_cert },
00196 {"verify_depth", PARAM_INT, &default_tls_cfg.verify_depth },
00197 {"require_certificate", PARAM_INT, &default_tls_cfg.require_cert },
00198 {"private_key", PARAM_STR, &default_tls_cfg.private_key },
00199 {"ca_list", PARAM_STR, &default_tls_cfg.ca_list },
00200 {"certificate", PARAM_STR, &default_tls_cfg.certificate },
00201 {"crl", PARAM_STR, &default_tls_cfg.crl },
00202 {"cipher_list", PARAM_STR, &default_tls_cfg.cipher_list },
00203 {"connection_timeout", PARAM_INT, &default_tls_cfg.con_lifetime },
00204 {"tls_log", PARAM_INT, &default_tls_cfg.log },
00205 {"tls_debug", PARAM_INT, &default_tls_cfg.debug },
00206 {"session_cache", PARAM_INT, &default_tls_cfg.session_cache},
00207 {"session_id", PARAM_STR, &default_tls_cfg.session_id },
00208 {"config", PARAM_STR, &default_tls_cfg.config_file },
00209 {"tls_disable_compression", PARAM_INT,
00210 &default_tls_cfg.disable_compression},
00211 {"ssl_release_buffers", PARAM_INT, &default_tls_cfg.ssl_release_buffers},
00212 {"ssl_freelist_max_len", PARAM_INT, &default_tls_cfg.ssl_freelist_max},
00213 {"ssl_max_send_fragment", PARAM_INT,
00214 &default_tls_cfg.ssl_max_send_fragment},
00215 {"ssl_read_ahead", PARAM_INT, &default_tls_cfg.ssl_read_ahead},
00216 {"send_close_notify", PARAM_INT, &default_tls_cfg.send_close_notify},
00217 {"con_ct_wq_max", PARAM_INT, &default_tls_cfg.con_ct_wq_max},
00218 {"ct_wq_max", PARAM_INT, &default_tls_cfg.ct_wq_max},
00219 {"ct_wq_blk_size", PARAM_INT, &default_tls_cfg.ct_wq_blk_size},
00220 {"tls_force_run", PARAM_INT, &default_tls_cfg.force_run},
00221 {"low_mem_threshold1", PARAM_INT, &default_tls_cfg.low_mem_threshold1},
00222 {"low_mem_threshold2", PARAM_INT, &default_tls_cfg.low_mem_threshold2},
00223 {"renegotiation", PARAM_INT, &sr_tls_renegotiation},
00224 {0, 0, 0}
00225 };
00226
00227
00228
00229
00230
00231 struct module_exports exports = {
00232 "tls",
00233 DEFAULT_DLFLAGS,
00234 cmds,
00235 params,
00236 0,
00237 0,
00238 tls_pv,
00239 0,
00240 mod_init,
00241 0,
00242 destroy,
00243 mod_child
00244 };
00245
00246
00247
00248 static struct tls_hooks tls_h = {
00249 tls_read_f,
00250 tls_encode_f,
00251 tls_h_tcpconn_init,
00252 tls_h_tcpconn_clean,
00253 tls_h_close,
00254 tls_h_init_si,
00255 init_tls_h,
00256 destroy_tls_h
00257 };
00258
00259
00260
00261 #if 0
00262
00263
00264
00265 static tls_domains_cfg_t* tls_use_modparams(void)
00266 {
00267 tls_domains_cfg_t* ret;
00268
00269 ret = tls_new_cfg();
00270 if (!ret) return;
00271
00272
00273 }
00274 #endif
00275
00276 int mod_register(char *path, int *dlflags, void *p1, void *p2)
00277 {
00278
00279 if(!shm_initialized() && init_shm()<0)
00280 return -1;
00281
00282 if(tls_pre_init()<0)
00283 return -1;
00284 return 0;
00285 }
00286
00287 static int mod_init(void)
00288 {
00289 int method;
00290
00291 if (tls_disable){
00292 LOG(L_WARN, "WARNING: tls: mod_init: tls support is disabled "
00293 "(set enable_tls=1 in the config to enable it)\n");
00294 return 0;
00295 }
00296 if (fix_tls_cfg(&default_tls_cfg) < 0 ) {
00297 ERR("initial tls configuration fixup failed\n");
00298 return -1;
00299 }
00300
00301 if (cfg_declare("tls", tls_cfg_def, &default_tls_cfg,
00302 cfg_sizeof(tls), &tls_cfg)) {
00303 ERR("failed to register the configuration\n");
00304 return -1;
00305 }
00306
00307 method = tls_parse_method(&cfg_get(tls, tls_cfg, method));
00308 if (method < 0) {
00309 ERR("Invalid tls_method parameter value\n");
00310 return -1;
00311 }
00312
00313 mod_params.method = method;
00314 mod_params.verify_cert = cfg_get(tls, tls_cfg, verify_cert);
00315 mod_params.verify_depth = cfg_get(tls, tls_cfg, verify_depth);
00316 mod_params.require_cert = cfg_get(tls, tls_cfg, require_cert);
00317 mod_params.pkey_file = cfg_get(tls, tls_cfg, private_key);
00318 mod_params.ca_file = cfg_get(tls, tls_cfg, ca_list);
00319 mod_params.crl_file = cfg_get(tls, tls_cfg, crl);
00320 mod_params.cert_file = cfg_get(tls, tls_cfg, certificate);
00321 mod_params.cipher_list = cfg_get(tls, tls_cfg, cipher_list);
00322
00323 tls_domains_cfg =
00324 (tls_domains_cfg_t**)shm_malloc(sizeof(tls_domains_cfg_t*));
00325 if (!tls_domains_cfg) {
00326 ERR("Not enough shared memory left\n");
00327 goto error;
00328 }
00329 *tls_domains_cfg = NULL;
00330
00331 register_tls_hooks(&tls_h);
00332 register_select_table(tls_sel);
00333
00334 if (rpc_register_array(tls_rpc)!=0) {
00335 LOG(L_ERR, "failed to register RPC commands\n");
00336 goto error;
00337 }
00338
00339
00340
00341 tls_domains_cfg_lock = lock_alloc();
00342 if (tls_domains_cfg_lock == 0) {
00343 ERR("Unable to create TLS configuration lock\n");
00344 goto error;
00345 }
00346 if (lock_init(tls_domains_cfg_lock) == 0) {
00347 lock_dealloc(tls_domains_cfg_lock);
00348 ERR("Unable to initialize TLS configuration lock\n");
00349 goto error;
00350 }
00351 if (tls_ct_wq_init() < 0) {
00352 ERR("Unable to initialize TLS buffering\n");
00353 goto error;
00354 }
00355 if (cfg_get(tls, tls_cfg, config_file).s) {
00356 *tls_domains_cfg =
00357 tls_load_config(&cfg_get(tls, tls_cfg, config_file));
00358 if (!(*tls_domains_cfg)) goto error;
00359 } else {
00360 *tls_domains_cfg = tls_new_cfg();
00361 if (!(*tls_domains_cfg)) goto error;
00362 }
00363
00364 if (tls_check_sockets(*tls_domains_cfg) < 0)
00365 goto error;
00366
00367 return 0;
00368 error:
00369 destroy_tls_h();
00370 return -1;
00371 }
00372
00373
00374 static int mod_child(int rank)
00375 {
00376 if (tls_disable || (tls_domains_cfg==0))
00377 return 0;
00378
00379
00380 if (rank == PROC_INIT){
00381 if (cfg_get(tls, tls_cfg, config_file).s){
00382 if (tls_fix_domains_cfg(*tls_domains_cfg,
00383 &srv_defaults, &cli_defaults) < 0)
00384 return -1;
00385 }else{
00386 if (tls_fix_domains_cfg(*tls_domains_cfg,
00387 &mod_params, &mod_params) < 0)
00388 return -1;
00389 }
00390 }
00391 return 0;
00392 }
00393
00394
00395 static void destroy(void)
00396 {
00397
00398
00399 }
00400
00401
00402 static int is_peer_verified(struct sip_msg* msg, char* foo, char* foo2)
00403 {
00404 struct tcp_connection *c;
00405 SSL *ssl;
00406 long ssl_verify;
00407 X509 *x509_cert;
00408
00409 DBG("started...\n");
00410 if (msg->rcv.proto != PROTO_TLS) {
00411 ERR("proto != TLS --> peer can't be verified, return -1\n");
00412 return -1;
00413 }
00414
00415 DBG("trying to find TCP connection of received message...\n");
00416
00417 c = tcpconn_get(msg->rcv.proto_reserved1, 0, 0, 0,
00418 cfg_get(tls, tls_cfg, con_lifetime));
00419 if (c && c->type != PROTO_TLS) {
00420 ERR("Connection found but is not TLS\n");
00421 tcpconn_put(c);
00422 return -1;
00423 }
00424
00425 if (!c->extra_data) {
00426 LM_ERR("no extra_data specified in TLS/TCP connection found."
00427 " This should not happen... return -1\n");
00428 tcpconn_put(c);
00429 return -1;
00430 }
00431
00432 ssl = ((struct tls_extra_data*)c->extra_data)->ssl;
00433
00434 ssl_verify = SSL_get_verify_result(ssl);
00435 if ( ssl_verify != X509_V_OK ) {
00436 LM_WARN("verification of presented certificate failed... return -1\n");
00437 tcpconn_put(c);
00438 return -1;
00439 }
00440
00441
00442
00443
00444 x509_cert = SSL_get_peer_certificate(ssl);
00445 if ( x509_cert == NULL ) {
00446 LM_WARN("tlsops:is_peer_verified: WARNING: peer did not presented "
00447 "a certificate. Thus it could not be verified... return -1\n");
00448 tcpconn_put(c);
00449 return -1;
00450 }
00451
00452 X509_free(x509_cert);
00453
00454 tcpconn_put(c);
00455
00456 LM_DBG("tlsops:is_peer_verified: peer is successfuly verified"
00457 "...done\n");
00458 return 1;
00459 }