@@ -9187,6 +9187,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9187
9187
const char * keyring = "Keyring for the retained key." ;
9188
9188
const char * keytype = "Key type of the retained key." ;
9189
9189
const char * insert = "Insert retained key into the keyring." ;
9190
+ const char * keyfile = "Update key file with the derive TLS PSK." ;
9190
9191
9191
9192
_cleanup_free_ unsigned char * raw_secret = NULL ;
9192
9193
_cleanup_free_ char * encoded_key = NULL ;
@@ -9201,6 +9202,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9201
9202
char * hostnqn ;
9202
9203
char * subsysnqn ;
9203
9204
char * secret ;
9205
+ char * keyfile ;
9204
9206
unsigned char hmac ;
9205
9207
unsigned char version ;
9206
9208
bool insert ;
@@ -9212,6 +9214,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9212
9214
.hostnqn = NULL ,
9213
9215
.subsysnqn = NULL ,
9214
9216
.secret = NULL ,
9217
+ .keyfile = NULL ,
9215
9218
.hmac = 1 ,
9216
9219
.version = 0 ,
9217
9220
.insert = false,
@@ -9223,6 +9226,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9223
9226
OPT_STR ("hostnqn" , 'n' , & cfg .hostnqn , hostnqn ),
9224
9227
OPT_STR ("subsysnqn" , 'c' , & cfg .subsysnqn , subsysnqn ),
9225
9228
OPT_STR ("secret" , 's' , & cfg .secret , secret ),
9229
+ OPT_STR ("keyfile" , 'f' , & cfg .keyfile , keyfile ),
9226
9230
OPT_BYTE ("hmac" , 'm' , & cfg .hmac , hmac ),
9227
9231
OPT_BYTE ("identity" , 'I' , & cfg .version , version ),
9228
9232
OPT_FLAG ("insert" , 'i' , & cfg .insert , insert ));
@@ -9297,6 +9301,66 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9297
9301
9298
9302
printf ("Inserted TLS key %08x\n" , (unsigned int )tls_key );
9299
9303
}
9304
+ if (tls_key && cfg .keyfile ) {
9305
+ _cleanup_free_ unsigned char * key_data = NULL ;
9306
+ _cleanup_free_ char * exported_key = NULL ;
9307
+ _cleanup_free_ char * identity = NULL ;
9308
+ _cleanup_file_ FILE * fd = NULL ;
9309
+ mode_t old_umask ;
9310
+ int key_len ;
9311
+ long kr_id ;
9312
+
9313
+ kr_id = nvme_lookup_keyring (cfg .keyring );
9314
+ if (kr_id <= 0 ) {
9315
+ nvme_show_error ("Failed to lookup keyring '%s'" ,
9316
+ cfg .keyring );
9317
+ return - errno ;
9318
+ }
9319
+
9320
+ key_data = nvme_read_key (kr_id , tls_key , & key_len );
9321
+ if (!key_data ) {
9322
+ nvme_show_error ("Failed to read back derive TLS PSK" );
9323
+ return - errno ;
9324
+ }
9325
+
9326
+ exported_key = nvme_export_tls_key_versioned (cfg .version , cfg .hmac ,
9327
+ key_data , key_len );
9328
+ if (!exported_key ) {
9329
+ nvme_show_error ("Failed to export key" );
9330
+ return - errno ;
9331
+ }
9332
+
9333
+ identity = nvme_describe_key_serial (tls_key );
9334
+ if (!identity ) {
9335
+ nvme_show_error ("Failed to get identity info" );
9336
+ return - errno ;
9337
+ }
9338
+
9339
+ old_umask = umask (0 );
9340
+
9341
+ fd = fopen (cfg .keyfile , "a" );
9342
+ if (!fd ) {
9343
+ nvme_show_error ("Failed to open '%s', %s" ,
9344
+ cfg .keyfile , strerror (errno ));
9345
+ err = - errno ;
9346
+ goto out ;
9347
+ }
9348
+
9349
+ err = fprintf (fd , "%s %s\n" , identity , exported_key );
9350
+ if (err < 0 ) {
9351
+ nvme_show_error ("Failed to append key to '%', %s" ,
9352
+ cfg .keyfile , strerror (errno ));
9353
+ err = - errno ;
9354
+ }
9355
+
9356
+ out :
9357
+ chmod (cfg .keyfile , S_IRUSR | S_IWUSR );
9358
+ umask (old_umask );
9359
+
9360
+ if (err )
9361
+ return err ;
9362
+ }
9363
+
9300
9364
return 0 ;
9301
9365
}
9302
9366
0 commit comments