@@ -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 ,
@@ -984,6 +996,7 @@ ngx_stream_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
984996 ngx_conf_merge_str_value (conf -> ssl_trusted_certificate ,
985997 prev -> ssl_trusted_certificate , "" );
986998 ngx_conf_merge_str_value (conf -> ssl_crl , prev -> ssl_crl , "" );
999+ ngx_conf_merge_str_value (conf -> ssl_key_log , prev -> ssl_key_log , "" );
9871000#if (nginx_version >= 1019004 )
9881001 ngx_conf_merge_ptr_value (conf -> ssl_conf_commands , prev -> ssl_conf_commands ,
9891002 NULL );
@@ -1114,6 +1127,12 @@ ngx_stream_lua_set_ssl(ngx_conf_t *cf, ngx_stream_lua_srv_conf_t *lscf)
11141127 return NGX_ERROR ;
11151128 }
11161129
1130+ if (ngx_stream_lua_ssl_key_log (cf , lscf -> ssl , & lscf -> ssl_key_log )
1131+ != NGX_OK )
1132+ {
1133+ return NGX_ERROR ;
1134+ }
1135+
11171136#if (nginx_version >= 1019004 )
11181137 if (ngx_ssl_conf_commands (cf , lscf -> ssl , lscf -> ssl_conf_commands )
11191138 != NGX_OK )
@@ -1126,6 +1145,101 @@ ngx_stream_lua_set_ssl(ngx_conf_t *cf, ngx_stream_lua_srv_conf_t *lscf)
11261145}
11271146
11281147
1148+ static void
1149+ key_log_callback (const ngx_ssl_conn_t * ssl_conn , const char * line )
1150+ {
1151+ ngx_stream_lua_ssl_key_log_t * ssl_key_log ;
1152+ ngx_connection_t * c ;
1153+
1154+ ssl_key_log = SSL_CTX_get_ex_data (SSL_get_SSL_CTX (ssl_conn ),
1155+ ngx_stream_lua_ssl_key_log_index );
1156+ if (ssl_key_log == NULL ) {
1157+ c = ngx_ssl_get_connection ((ngx_ssl_conn_t * ) ssl_conn );
1158+ ngx_ssl_error (NGX_LOG_DEBUG , c -> log , 0 , "get ssl key log failed" );
1159+
1160+ return ;
1161+ }
1162+
1163+ (void ) ngx_write_fd (ssl_key_log -> fd , (void * ) line , ngx_strlen (line ));
1164+ (void ) ngx_write_fd (ssl_key_log -> fd , (void * ) "\n" , 1 );
1165+ }
1166+
1167+
1168+ static void
1169+ ngx_stream_lua_ssl_cleanup_key_log (void * data )
1170+ {
1171+ ngx_stream_lua_ssl_key_log_t * ssl_key_log = data ;
1172+
1173+ if (ngx_close_file (ssl_key_log -> fd ) == NGX_FILE_ERROR ) {
1174+ ngx_ssl_error (NGX_LOG_ALERT , ssl_key_log -> ssl -> log , 0 ,
1175+ ngx_close_file_n "(\"%V\") failed" , ssl_key_log -> name );
1176+ }
1177+ }
1178+
1179+
1180+ static ngx_int_t
1181+ ngx_stream_lua_ssl_key_log (ngx_conf_t * cf , ngx_ssl_t * ssl , ngx_str_t * file )
1182+ {
1183+ ngx_fd_t fd ;
1184+ ngx_stream_lua_ssl_key_log_t * ssl_key_log ;
1185+ ngx_pool_cleanup_t * cln ;
1186+
1187+ if (!file -> len ) {
1188+ return NGX_OK ;
1189+ }
1190+
1191+ if (ngx_conf_full_name (cf -> cycle , file , 1 ) != NGX_OK ) {
1192+ return NGX_ERROR ;
1193+ }
1194+
1195+ if (ngx_stream_lua_ssl_init (cf -> log ) != NGX_OK ) {
1196+ return NGX_ERROR ;
1197+ }
1198+
1199+ /*
1200+ * append so that existing keylog file contents can be preserved
1201+ */
1202+ fd = ngx_open_file (file -> data , NGX_FILE_APPEND , NGX_FILE_CREATE_OR_OPEN ,
1203+ NGX_FILE_DEFAULT_ACCESS );
1204+ if (fd == NGX_INVALID_FILE ) {
1205+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 , ngx_open_file_n
1206+ "(\"%V\") failed" , file );
1207+ return NGX_ERROR ;
1208+ }
1209+
1210+ ssl_key_log = ngx_palloc (cf -> pool , sizeof (ngx_stream_lua_ssl_key_log_t ));
1211+ if (ssl_key_log == NULL ) {
1212+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 , "ngx_pcalloc() failed" );
1213+ return NGX_ERROR ;
1214+ }
1215+
1216+ ssl_key_log -> ssl = ssl ;
1217+ ssl_key_log -> fd = fd ;
1218+ ssl_key_log -> name = * file ;
1219+
1220+ if (SSL_CTX_set_ex_data (ssl -> ctx , ngx_stream_lua_ssl_key_log_index ,
1221+ ssl_key_log ) == 0 )
1222+ {
1223+ ngx_ssl_error (NGX_LOG_EMERG , ssl -> log , 0 ,
1224+ "SSL_CTX_set_ex_data() failed" );
1225+ return NGX_ERROR ;
1226+ }
1227+
1228+ cln = ngx_pool_cleanup_add (cf -> pool , 0 );
1229+ if (cln == NULL ) {
1230+ ngx_stream_lua_ssl_cleanup_key_log (ssl_key_log );
1231+ return NGX_ERROR ;
1232+ }
1233+
1234+ cln -> handler = ngx_stream_lua_ssl_cleanup_key_log ;
1235+ cln -> data = ssl_key_log ;
1236+
1237+ SSL_CTX_set_keylog_callback (ssl -> ctx , key_log_callback );
1238+
1239+ return NGX_OK ;
1240+ }
1241+
1242+
11291243#if (nginx_version >= 1019004 )
11301244static char *
11311245ngx_stream_lua_ssl_conf_command_check (ngx_conf_t * cf , void * post , void * data )
0 commit comments