@@ -49,6 +49,11 @@ static char *ngx_stream_lua_lowat_check(ngx_conf_t *cf, void *post, void *data);
4949#if (NGX_STREAM_SSL )
5050static ngx_int_t ngx_stream_lua_set_ssl (ngx_conf_t * cf ,
5151 ngx_stream_lua_loc_conf_t * llcf );
52+ static void key_log_callback (const ngx_ssl_conn_t * ssl_conn ,
53+ const char * line );
54+ static void ngx_stream_lua_ssl_cleanup_key_log (void * data );
55+ static ngx_int_t ngx_stream_lua_ssl_key_log (ngx_conf_t * cf , ngx_ssl_t * ssl ,
56+ ngx_str_t * file );
5257#if (nginx_version >= 1019004 )
5358static char * ngx_stream_lua_ssl_conf_command_check (ngx_conf_t * cf , void * post ,
5459 void * data );
@@ -453,6 +458,13 @@ static ngx_command_t ngx_stream_lua_cmds[] = {
453458 offsetof(ngx_stream_lua_srv_conf_t , ssl_crl ),
454459 NULL },
455460
461+ { ngx_string ("lua_ssl_key_log" ),
462+ NGX_STREAM_MAIN_CONF |NGX_STREAM_SRV_CONF |NGX_CONF_TAKE1 ,
463+ ngx_conf_set_str_slot ,
464+ NGX_STREAM_SRV_CONF_OFFSET ,
465+ offsetof(ngx_stream_lua_srv_conf_t , ssl_key_log ),
466+ NULL },
467+
456468#if (nginx_version >= 1019004 )
457469 { ngx_string ("lua_ssl_conf_command" ),
458470 NGX_STREAM_MAIN_CONF |NGX_STREAM_SRV_CONF |NGX_CONF_TAKE2 ,
@@ -975,6 +987,7 @@ ngx_stream_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
975987 ngx_conf_merge_str_value (conf -> ssl_trusted_certificate ,
976988 prev -> ssl_trusted_certificate , "" );
977989 ngx_conf_merge_str_value (conf -> ssl_crl , prev -> ssl_crl , "" );
990+ ngx_conf_merge_str_value (conf -> ssl_key_log , prev -> ssl_key_log , "" );
978991#if (nginx_version >= 1019004 )
979992 ngx_conf_merge_ptr_value (conf -> ssl_conf_commands , prev -> ssl_conf_commands ,
980993 NULL );
@@ -1105,6 +1118,12 @@ ngx_stream_lua_set_ssl(ngx_conf_t *cf, ngx_stream_lua_srv_conf_t *lscf)
11051118 return NGX_ERROR ;
11061119 }
11071120
1121+ if (ngx_stream_lua_ssl_key_log (cf , lscf -> ssl , & lscf -> ssl_key_log )
1122+ != NGX_OK )
1123+ {
1124+ return NGX_ERROR ;
1125+ }
1126+
11081127#if (nginx_version >= 1019004 )
11091128 if (ngx_ssl_conf_commands (cf , lscf -> ssl , lscf -> ssl_conf_commands )
11101129 != NGX_OK )
@@ -1117,6 +1136,101 @@ ngx_stream_lua_set_ssl(ngx_conf_t *cf, ngx_stream_lua_srv_conf_t *lscf)
11171136}
11181137
11191138
1139+ static void
1140+ key_log_callback (const ngx_ssl_conn_t * ssl_conn , const char * line )
1141+ {
1142+ ngx_stream_lua_ssl_key_log_t * ssl_key_log ;
1143+ ngx_connection_t * c ;
1144+
1145+ ssl_key_log = SSL_CTX_get_ex_data (SSL_get_SSL_CTX (ssl_conn ),
1146+ ngx_stream_lua_ssl_key_log_index );
1147+ if (ssl_key_log == NULL ) {
1148+ c = ngx_ssl_get_connection ((ngx_ssl_conn_t * ) ssl_conn );
1149+ ngx_ssl_error (NGX_LOG_DEBUG , c -> log , 0 , "get ssl key log failed" );
1150+
1151+ return ;
1152+ }
1153+
1154+ (void ) ngx_write_fd (ssl_key_log -> fd , line , ngx_strlen (line ));
1155+ (void ) ngx_write_fd (ssl_key_log -> fd , "\n" , 1 );
1156+ }
1157+
1158+
1159+ static void
1160+ ngx_stream_lua_ssl_cleanup_key_log (void * data )
1161+ {
1162+ ngx_stream_lua_ssl_key_log_t * ssl_key_log = data ;
1163+
1164+ if (ngx_close_file (ssl_key_log -> fd ) == NGX_FILE_ERROR ) {
1165+ ngx_ssl_error (NGX_LOG_ALERT , ssl_key_log -> ssl -> log , 0 ,
1166+ ngx_close_file_n "(\"%V\") failed" , ssl_key_log -> name );
1167+ }
1168+ }
1169+
1170+
1171+ static ngx_int_t
1172+ ngx_stream_lua_ssl_key_log (ngx_conf_t * cf , ngx_ssl_t * ssl , ngx_str_t * file )
1173+ {
1174+ ngx_fd_t fd ;
1175+ ngx_stream_lua_ssl_key_log_t * ssl_key_log ;
1176+ ngx_pool_cleanup_t * cln ;
1177+
1178+ if (!file -> len ) {
1179+ return NGX_OK ;
1180+ }
1181+
1182+ if (ngx_conf_full_name (cf -> cycle , file , 1 ) != NGX_OK ) {
1183+ return NGX_ERROR ;
1184+ }
1185+
1186+ if (ngx_stream_lua_ssl_init (cf -> log ) != NGX_OK ) {
1187+ return NGX_ERROR ;
1188+ }
1189+
1190+ /*
1191+ * append so that existing keylog file contents can be preserved
1192+ */
1193+ fd = ngx_open_file (file -> data , NGX_FILE_APPEND , NGX_FILE_CREATE_OR_OPEN ,
1194+ NGX_FILE_DEFAULT_ACCESS );
1195+ if (fd == NGX_INVALID_FILE ) {
1196+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 , ngx_open_file_n
1197+ "(\"%V\") failed" , file );
1198+ return NGX_ERROR ;
1199+ }
1200+
1201+ ssl_key_log = ngx_palloc (cf -> pool , sizeof (ngx_stream_lua_ssl_key_log_t ));
1202+ if (ssl_key_log == NULL ) {
1203+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 , "ngx_pcalloc() failed" );
1204+ return NGX_ERROR ;
1205+ }
1206+
1207+ ssl_key_log -> ssl = ssl ;
1208+ ssl_key_log -> fd = fd ;
1209+ ssl_key_log -> name = * file ;
1210+
1211+ if (SSL_CTX_set_ex_data (ssl -> ctx , ngx_stream_lua_ssl_key_log_index ,
1212+ ssl_key_log ) == 0 )
1213+ {
1214+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 ,
1215+ "SSL_CTX_set_ex_data() failed" );
1216+ return NGX_ERROR ;
1217+ }
1218+
1219+ cln = ngx_pool_cleanup_add (cf -> pool , 0 );
1220+ if (cln == NULL ) {
1221+ ngx_stream_lua_ssl_cleanup_key_log (ssl_key_log );
1222+ return NGX_ERROR ;
1223+ }
1224+
1225+ cln -> handler = ngx_stream_lua_ssl_cleanup_key_log ;
1226+ cln -> data = ssl_key_log ;
1227+
1228+ SSL_CTX_set_keylog_callback (ssl -> ctx , key_log_callback );
1229+
1230+ return NGX_OK ;
1231+ }
1232+
1233+
11201234#if (nginx_version >= 1019004 )
11211235static char *
11221236ngx_stream_lua_ssl_conf_command_check (ngx_conf_t * cf , void * post , void * data )
0 commit comments