@@ -50,6 +50,11 @@ static char *ngx_stream_lua_lowat_check(ngx_conf_t *cf, void *post, void *data);
5050#if (NGX_STREAM_SSL )
5151static ngx_int_t ngx_stream_lua_set_ssl (ngx_conf_t * cf ,
5252 ngx_stream_lua_loc_conf_t * llcf );
53+ static void key_log_callback (const ngx_ssl_conn_t * ssl_conn ,
54+ const char * line );
55+ static void ngx_stream_lua_ssl_cleanup_key_log (void * data );
56+ static ngx_int_t ngx_stream_lua_ssl_key_log (ngx_conf_t * cf , ngx_ssl_t * ssl ,
57+ ngx_str_t * file );
5358#if (nginx_version >= 1019004 )
5459static char * ngx_stream_lua_ssl_conf_command_check (ngx_conf_t * cf , void * post ,
5560 void * data );
@@ -476,6 +481,13 @@ static ngx_command_t ngx_stream_lua_cmds[] = {
476481 offsetof(ngx_stream_lua_srv_conf_t , ssl_crl ),
477482 NULL },
478483
484+ { ngx_string ("lua_ssl_key_log" ),
485+ NGX_STREAM_MAIN_CONF |NGX_STREAM_SRV_CONF |NGX_CONF_TAKE1 ,
486+ ngx_conf_set_str_slot ,
487+ NGX_STREAM_SRV_CONF_OFFSET ,
488+ offsetof(ngx_stream_lua_srv_conf_t , ssl_key_log ),
489+ NULL },
490+
479491#if (nginx_version >= 1019004 )
480492 { ngx_string ("lua_ssl_conf_command" ),
481493 NGX_STREAM_MAIN_CONF |NGX_STREAM_SRV_CONF |NGX_CONF_TAKE2 ,
@@ -1012,6 +1024,7 @@ ngx_stream_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
10121024 ngx_conf_merge_str_value (conf -> ssl_trusted_certificate ,
10131025 prev -> ssl_trusted_certificate , "" );
10141026 ngx_conf_merge_str_value (conf -> ssl_crl , prev -> ssl_crl , "" );
1027+ ngx_conf_merge_str_value (conf -> ssl_key_log , prev -> ssl_key_log , "" );
10151028#if (nginx_version >= 1019004 )
10161029 ngx_conf_merge_ptr_value (conf -> ssl_conf_commands , prev -> ssl_conf_commands ,
10171030 NULL );
@@ -1157,6 +1170,12 @@ ngx_stream_lua_set_ssl(ngx_conf_t *cf, ngx_stream_lua_srv_conf_t *lscf)
11571170 return NGX_ERROR ;
11581171 }
11591172
1173+ if (ngx_stream_lua_ssl_key_log (cf , lscf -> ssl , & lscf -> ssl_key_log )
1174+ != NGX_OK )
1175+ {
1176+ return NGX_ERROR ;
1177+ }
1178+
11601179#if (nginx_version >= 1019004 )
11611180 if (ngx_ssl_conf_commands (cf , lscf -> ssl , lscf -> ssl_conf_commands )
11621181 != NGX_OK )
@@ -1169,6 +1188,101 @@ ngx_stream_lua_set_ssl(ngx_conf_t *cf, ngx_stream_lua_srv_conf_t *lscf)
11691188}
11701189
11711190
1191+ static void
1192+ key_log_callback (const ngx_ssl_conn_t * ssl_conn , const char * line )
1193+ {
1194+ ngx_stream_lua_ssl_key_log_t * ssl_key_log ;
1195+ ngx_connection_t * c ;
1196+
1197+ ssl_key_log = SSL_CTX_get_ex_data (SSL_get_SSL_CTX (ssl_conn ),
1198+ ngx_stream_lua_ssl_key_log_index );
1199+ if (ssl_key_log == NULL ) {
1200+ c = ngx_ssl_get_connection ((ngx_ssl_conn_t * ) ssl_conn );
1201+ ngx_ssl_error (NGX_LOG_DEBUG , c -> log , 0 , "get ssl key log failed" );
1202+
1203+ return ;
1204+ }
1205+
1206+ (void ) ngx_write_fd (ssl_key_log -> fd , (void * ) line , ngx_strlen (line ));
1207+ (void ) ngx_write_fd (ssl_key_log -> fd , (void * ) "\n" , 1 );
1208+ }
1209+
1210+
1211+ static void
1212+ ngx_stream_lua_ssl_cleanup_key_log (void * data )
1213+ {
1214+ ngx_stream_lua_ssl_key_log_t * ssl_key_log = data ;
1215+
1216+ if (ngx_close_file (ssl_key_log -> fd ) == NGX_FILE_ERROR ) {
1217+ ngx_ssl_error (NGX_LOG_ALERT , ssl_key_log -> ssl -> log , 0 ,
1218+ ngx_close_file_n "(\"%V\") failed" , ssl_key_log -> name );
1219+ }
1220+ }
1221+
1222+
1223+ static ngx_int_t
1224+ ngx_stream_lua_ssl_key_log (ngx_conf_t * cf , ngx_ssl_t * ssl , ngx_str_t * file )
1225+ {
1226+ ngx_fd_t fd ;
1227+ ngx_stream_lua_ssl_key_log_t * ssl_key_log ;
1228+ ngx_pool_cleanup_t * cln ;
1229+
1230+ if (!file -> len ) {
1231+ return NGX_OK ;
1232+ }
1233+
1234+ if (ngx_conf_full_name (cf -> cycle , file , 1 ) != NGX_OK ) {
1235+ return NGX_ERROR ;
1236+ }
1237+
1238+ if (ngx_stream_lua_ssl_init (cf -> log ) != NGX_OK ) {
1239+ return NGX_ERROR ;
1240+ }
1241+
1242+ /*
1243+ * append so that existing keylog file contents can be preserved
1244+ */
1245+ fd = ngx_open_file (file -> data , NGX_FILE_APPEND , NGX_FILE_CREATE_OR_OPEN ,
1246+ NGX_FILE_DEFAULT_ACCESS );
1247+ if (fd == NGX_INVALID_FILE ) {
1248+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 , ngx_open_file_n
1249+ "(\"%V\") failed" , file );
1250+ return NGX_ERROR ;
1251+ }
1252+
1253+ ssl_key_log = ngx_palloc (cf -> pool , sizeof (ngx_stream_lua_ssl_key_log_t ));
1254+ if (ssl_key_log == NULL ) {
1255+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 , "ngx_pcalloc() failed" );
1256+ return NGX_ERROR ;
1257+ }
1258+
1259+ ssl_key_log -> ssl = ssl ;
1260+ ssl_key_log -> fd = fd ;
1261+ ssl_key_log -> name = * file ;
1262+
1263+ if (SSL_CTX_set_ex_data (ssl -> ctx , ngx_stream_lua_ssl_key_log_index ,
1264+ ssl_key_log ) == 0 )
1265+ {
1266+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 ,
1267+ "SSL_CTX_set_ex_data() failed" );
1268+ return NGX_ERROR ;
1269+ }
1270+
1271+ cln = ngx_pool_cleanup_add (cf -> pool , 0 );
1272+ if (cln == NULL ) {
1273+ ngx_stream_lua_ssl_cleanup_key_log (ssl_key_log );
1274+ return NGX_ERROR ;
1275+ }
1276+
1277+ cln -> handler = ngx_stream_lua_ssl_cleanup_key_log ;
1278+ cln -> data = ssl_key_log ;
1279+
1280+ SSL_CTX_set_keylog_callback (ssl -> ctx , key_log_callback );
1281+
1282+ return NGX_OK ;
1283+ }
1284+
1285+
11721286#if (nginx_version >= 1019004 )
11731287static char *
11741288ngx_stream_lua_ssl_conf_command_check (ngx_conf_t * cf , void * post , void * data )
0 commit comments