@@ -338,113 +338,134 @@ ssize_t tport_send_stream_ws(tport_t const *self, msg_t *msg,
338338 return size ;
339339}
340340
341- static int tport_ws_init_primary_secure (tport_primary_t * pri ,
342- tp_name_t tpn [1 ],
343- su_addrinfo_t * ai ,
344- tagi_t const * tags ,
345- char const * * return_culprit )
341+ /** Create a new SSL_CTX for WSS using certificates from cert_dir.
342+ * Returns a new SSL_CTX on success, NULL on failure.
343+ */
344+ SSL_CTX * tport_wss_create_ssl_ctx (char const * cert_dir )
346345{
347- tport_ws_primary_t * wspri = (tport_ws_primary_t * )pri ;
348- const char * cert = "/ssl.pem" ;
349- const char * key = "/ssl.pem" ;
350- const char * chain = NULL ;
351- char * homedir ;
352346 su_home_t autohome [SU_HOME_AUTO_SIZE (1024 )];
353- char const * path = NULL ;
354- int ret = -1 ;
355-
356- su_home_auto ( autohome , sizeof autohome ) ;
347+ const char * cert = NULL ;
348+ const char * key = NULL ;
349+ const char * chain = NULL ;
350+ SSL_CTX * ssl_ctx = NULL ;
357351
358- tl_gets (tags ,
359- TPTAG_CERTIFICATE_REF (path ),
360- TAG_END ());
352+ if (!cert_dir )
353+ return NULL ;
361354
362- if (!path ) {
363- homedir = getenv ("HOME" );
364- if (!homedir )
365- homedir = "" ;
366- path = su_sprintf (autohome , "%s/.sip/auth" , homedir );
367- }
355+ su_home_auto (autohome , sizeof autohome );
368356
369- if (path ) {
370- key = su_sprintf (autohome , "%s/%s" , path , "wss.key" );
371- if (access (key , R_OK ) != 0 ) key = NULL ;
357+ key = su_sprintf (autohome , "%s/%s" , cert_dir , "wss.key" );
358+ if (access (key , R_OK ) != 0 ) key = NULL ;
372359
373- cert = su_sprintf (autohome , "%s/%s" , path , "wss.crt" );
374- if (access (cert , R_OK ) != 0 ) cert = NULL ;
360+ cert = su_sprintf (autohome , "%s/%s" , cert_dir , "wss.crt" );
361+ if (access (cert , R_OK ) != 0 ) cert = NULL ;
375362
376- chain = su_sprintf (autohome , "%s/%s" , path , "ca-bundle.crt" );
377- if (access (chain , R_OK ) != 0 ) chain = NULL ;
363+ chain = su_sprintf (autohome , "%s/%s" , cert_dir , "ca-bundle.crt" );
364+ if (access (chain , R_OK ) != 0 ) chain = NULL ;
378365
379- if ( !key ) key = su_sprintf (autohome , "%s/%s" , path , "wss.pem" );
380- if ( !cert ) cert = su_sprintf (autohome , "%s/%s" , path , "wss.pem" );
381- if ( !chain ) chain = su_sprintf (autohome , "%s/%s" , path , "wss.pem" );
382- if (access (key , R_OK ) != 0 ) key = NULL ;
383- if (access (cert , R_OK ) != 0 ) cert = NULL ;
384- if (access (chain , R_OK ) != 0 ) chain = NULL ;
385- }
366+ if (!key ) key = su_sprintf (autohome , "%s/%s" , cert_dir , "wss.pem" );
367+ if (!cert ) cert = su_sprintf (autohome , "%s/%s" , cert_dir , "wss.pem" );
368+ if (!chain ) chain = su_sprintf (autohome , "%s/%s" , cert_dir , "wss.pem" );
369+ if (access (key , R_OK ) != 0 ) key = NULL ;
370+ if (access (cert , R_OK ) != 0 ) cert = NULL ;
371+ if (access (chain , R_OK ) != 0 ) chain = NULL ;
386372
387373 if (!(key && cert && chain )) {
388- tls_log_errors (3 , "tport_ws_init_primary_secure " , 0 );
374+ tls_log_errors (3 , "tport_wss_create_ssl_ctx " , 0 );
389375 goto done ;
390376 }
391377
392378 init_ssl ();
393379
394- // OpenSSL_add_all_algorithms(); /* load & register cryptos */
395- // SSL_load_error_strings(); /* load all error messages */
396- wspri -> ssl_method = SSLv23_server_method (); /* create server instance */
397- wspri -> ssl_ctx = SSL_CTX_new ((SSL_METHOD * )wspri -> ssl_method ); /* create context */
398-
399- if (!wspri -> ssl_ctx ) {
400- tls_log_errors (3 , "tport_ws_init_primary_secure" , 0 );
401- goto done ;
380+ ssl_ctx = SSL_CTX_new ((SSL_METHOD * )SSLv23_server_method ());
381+ if (!ssl_ctx ) {
382+ tls_log_errors (3 , "tport_wss_create_ssl_ctx" , 0 );
383+ goto done ;
402384 }
403385
404- SSL_CTX_sess_set_remove_cb (wspri -> ssl_ctx , NULL );
405- wspri -> ws_secure = 1 ;
386+ SSL_CTX_sess_set_remove_cb (ssl_ctx , NULL );
406387
407388 /* Disable SSLv2 */
408- SSL_CTX_set_options (wspri -> ssl_ctx , SSL_OP_NO_SSLv2 );
389+ SSL_CTX_set_options (ssl_ctx , SSL_OP_NO_SSLv2 );
409390 /* Disable SSLv3 */
410- SSL_CTX_set_options (wspri -> ssl_ctx , SSL_OP_NO_SSLv3 );
391+ SSL_CTX_set_options (ssl_ctx , SSL_OP_NO_SSLv3 );
411392 /* Disable TLSv1 */
412- SSL_CTX_set_options (wspri -> ssl_ctx , SSL_OP_NO_TLSv1 );
393+ SSL_CTX_set_options (ssl_ctx , SSL_OP_NO_TLSv1 );
413394 /* Disable Compression CRIME (Compression Ratio Info-leak Made Easy) */
414- SSL_CTX_set_options (wspri -> ssl_ctx , SSL_OP_NO_COMPRESSION );
415-
395+ SSL_CTX_set_options (ssl_ctx , SSL_OP_NO_COMPRESSION );
396+
416397 if (chain ) {
417- if ( !SSL_CTX_use_certificate_chain_file (wspri -> ssl_ctx , chain ) ) {
418- tls_log_errors (3 , "tport_ws_init_primary_secure " , 0 );
419- }
398+ if (!SSL_CTX_use_certificate_chain_file (ssl_ctx , chain )) {
399+ tls_log_errors (3 , "tport_wss_create_ssl_ctx " , 0 );
400+ }
420401 }
421402
422403 /* set the local certificate from CertFile */
423- if ( !SSL_CTX_use_certificate_file (wspri -> ssl_ctx , cert , SSL_FILETYPE_PEM ) ) {
424- tls_log_errors (3 , "tport_ws_init_primary_secure " , 0 );
425- goto done ;
404+ if (!SSL_CTX_use_certificate_file (ssl_ctx , cert , SSL_FILETYPE_PEM )) {
405+ tls_log_errors (3 , "tport_wss_create_ssl_ctx " , 0 );
406+ goto fail ;
426407 }
427408 /* set the private key from KeyFile */
428- if ( !SSL_CTX_use_PrivateKey_file (wspri -> ssl_ctx , key , SSL_FILETYPE_PEM ) ) {
429- tls_log_errors (3 , "tport_ws_init_primary_secure " , 0 );
430- goto done ;
409+ if (!SSL_CTX_use_PrivateKey_file (ssl_ctx , key , SSL_FILETYPE_PEM )) {
410+ tls_log_errors (3 , "tport_wss_create_ssl_ctx " , 0 );
411+ goto fail ;
431412 }
432413 /* verify private key */
433- if ( !SSL_CTX_check_private_key (wspri -> ssl_ctx ) ) {
434- tls_log_errors (3 , "tport_ws_init_primary_secure " , 0 );
435- goto done ;
414+ if (!SSL_CTX_check_private_key (ssl_ctx )) {
415+ tls_log_errors (3 , "tport_wss_create_ssl_ctx " , 0 );
416+ goto fail ;
436417 }
437418
438- if ( !SSL_CTX_set_cipher_list (wspri -> ssl_ctx , "!eNULL:!aNULL:!DSS:HIGH:@STRENGTH" ) ) {
439- tls_log_errors (3 , "tport_ws_init_primary_secure " , 0 );
440- goto done ;
419+ if (!SSL_CTX_set_cipher_list (ssl_ctx , "!eNULL:!aNULL:!DSS:HIGH:@STRENGTH" )) {
420+ tls_log_errors (3 , "tport_wss_create_ssl_ctx " , 0 );
421+ goto fail ;
441422 }
442423
443- ret = tport_ws_init_primary (pri , tpn , ai , tags , return_culprit );
424+ su_home_zap (autohome );
425+ return ssl_ctx ;
444426
427+ fail :
428+ SSL_CTX_free (ssl_ctx );
445429 done :
446430 su_home_zap (autohome );
447- return ret ;
431+ return NULL ;
432+ }
433+
434+ static int tport_ws_init_primary_secure (tport_primary_t * pri ,
435+ tp_name_t tpn [1 ],
436+ su_addrinfo_t * ai ,
437+ tagi_t const * tags ,
438+ char const * * return_culprit )
439+ {
440+ tport_ws_primary_t * wspri = (tport_ws_primary_t * )pri ;
441+ char * homedir ;
442+ su_home_t autohome [SU_HOME_AUTO_SIZE (1024 )];
443+ char const * path = NULL ;
444+
445+ su_home_auto (autohome , sizeof autohome );
446+
447+ tl_gets (tags ,
448+ TPTAG_CERTIFICATE_REF (path ),
449+ TAG_END ());
450+
451+ if (!path ) {
452+ homedir = getenv ("HOME" );
453+ if (!homedir )
454+ homedir = "" ;
455+ path = su_sprintf (autohome , "%s/.sip/auth" , homedir );
456+ }
457+
458+ wspri -> ssl_ctx = tport_wss_create_ssl_ctx (path );
459+ if (!wspri -> ssl_ctx ) {
460+ su_home_zap (autohome );
461+ return -1 ;
462+ }
463+
464+ wspri -> ssl_method = SSLv23_server_method ();
465+ wspri -> ws_secure = 1 ;
466+
467+ su_home_zap (autohome );
468+ return tport_ws_init_primary (pri , tpn , ai , tags , return_culprit );
448469}
449470
450471int tport_ws_init_primary (tport_primary_t * pri ,
0 commit comments