@@ -9175,6 +9175,75 @@ static int check_dhchap_key(int argc, char **argv, struct command *command, stru
9175
9175
return 0 ;
9176
9176
}
9177
9177
9178
+ static int append_keyfile (const char * keyring , long id , const char * keyfile )
9179
+ {
9180
+ _cleanup_free_ unsigned char * key_data = NULL ;
9181
+ _cleanup_free_ char * exported_key = NULL ;
9182
+ _cleanup_free_ char * identity = NULL ;
9183
+ _cleanup_file_ FILE * fd = NULL ;
9184
+ int err , ver , hmac , key_len ;
9185
+ mode_t old_umask ;
9186
+ long kr_id ;
9187
+ char type ;
9188
+
9189
+ kr_id = nvme_lookup_keyring (keyring );
9190
+ if (kr_id <= 0 ) {
9191
+ nvme_show_error ("Failed to lookup keyring '%s', %s" ,
9192
+ keyring , strerror (errno ));
9193
+ return - errno ;
9194
+ }
9195
+
9196
+ identity = nvme_describe_key_serial (id );
9197
+ if (!identity ) {
9198
+ nvme_show_error ("Failed to get identity info, %s" ,
9199
+ strerror (errno ));
9200
+ return - errno ;
9201
+ }
9202
+
9203
+ if (sscanf (identity , "NVMe%01d%c%02d %*s" , & ver , & type , & hmac ) != 3 ) {
9204
+ nvme_show_error ("Failed to parse identity\n" );
9205
+ return - EINVAL ;
9206
+ }
9207
+
9208
+ key_data = nvme_read_key (kr_id , id , & key_len );
9209
+ if (!key_data ) {
9210
+ nvme_show_error ("Failed to read back derive TLS PSK, %s" ,
9211
+ strerror (errno ));
9212
+ return - errno ;
9213
+ }
9214
+
9215
+ exported_key = nvme_export_tls_key_versioned (ver , hmac ,
9216
+ key_data , key_len );
9217
+ if (!exported_key ) {
9218
+ nvme_show_error ("Failed to export key, %s" ,
9219
+ strerror (errno ));
9220
+ return - errno ;
9221
+ }
9222
+
9223
+ old_umask = umask (0 );
9224
+
9225
+ fd = fopen (keyfile , "a" );
9226
+ if (!fd ) {
9227
+ nvme_show_error ("Failed to open '%s', %s" ,
9228
+ keyfile , strerror (errno ));
9229
+ err = - errno ;
9230
+ goto out ;
9231
+ }
9232
+
9233
+ err = fprintf (fd , "%s %s\n" , identity , exported_key );
9234
+ if (err < 0 ) {
9235
+ nvme_show_error ("Failed to append key to '%', %s" ,
9236
+ keyfile , strerror (errno ));
9237
+ err = - errno ;
9238
+ }
9239
+
9240
+ out :
9241
+ chmod (keyfile , 0600 );
9242
+ umask (old_umask );
9243
+
9244
+ return err ;
9245
+ }
9246
+
9178
9247
static int gen_tls_key (int argc , char * * argv , struct command * command , struct plugin * plugin )
9179
9248
{
9180
9249
const char * desc = "Generate a TLS key in NVMe PSK Interchange format." ;
@@ -9187,6 +9256,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9187
9256
const char * keyring = "Keyring for the retained key." ;
9188
9257
const char * keytype = "Key type of the retained key." ;
9189
9258
const char * insert = "Insert retained key into the keyring." ;
9259
+ const char * keyfile = "Update key file with the derive TLS PSK." ;
9190
9260
9191
9261
_cleanup_free_ unsigned char * raw_secret = NULL ;
9192
9262
_cleanup_free_ char * encoded_key = NULL ;
@@ -9201,6 +9271,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9201
9271
char * hostnqn ;
9202
9272
char * subsysnqn ;
9203
9273
char * secret ;
9274
+ char * keyfile ;
9204
9275
unsigned char hmac ;
9205
9276
unsigned char version ;
9206
9277
bool insert ;
@@ -9212,6 +9283,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9212
9283
.hostnqn = NULL ,
9213
9284
.subsysnqn = NULL ,
9214
9285
.secret = NULL ,
9286
+ .keyfile = NULL ,
9215
9287
.hmac = 1 ,
9216
9288
.version = 0 ,
9217
9289
.insert = false,
@@ -9223,6 +9295,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9223
9295
OPT_STR ("hostnqn" , 'n' , & cfg .hostnqn , hostnqn ),
9224
9296
OPT_STR ("subsysnqn" , 'c' , & cfg .subsysnqn , subsysnqn ),
9225
9297
OPT_STR ("secret" , 's' , & cfg .secret , secret ),
9298
+ OPT_STR ("keyfile" , 'f' , & cfg .keyfile , keyfile ),
9226
9299
OPT_BYTE ("hmac" , 'm' , & cfg .hmac , hmac ),
9227
9300
OPT_BYTE ("identity" , 'I' , & cfg .version , version ),
9228
9301
OPT_FLAG ("insert" , 'i' , & cfg .insert , insert ));
@@ -9296,7 +9369,14 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
9296
9369
}
9297
9370
9298
9371
printf ("Inserted TLS key %08x\n" , (unsigned int )tls_key );
9372
+
9373
+ if (cfg .keyfile ) {
9374
+ err = append_keyfile (cfg .keyring , tls_key , cfg .keyfile );
9375
+ if (err )
9376
+ return err ;
9377
+ }
9299
9378
}
9379
+
9300
9380
return 0 ;
9301
9381
}
9302
9382
0 commit comments