Skip to content

Commit d513adc

Browse files
gojimmypidanielinux
authored andcommitted
Introduce keygen --no-overwrite to avoid prompt
1 parent a559b75 commit d513adc

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

docs/Signing.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following options are supported:
3131
- `--der` save generated private key in DER format.
3232
- `--exportpubkey` to export the public key (corresponding to the private key generated with `-g`) to a DER file. This option only has an effect if used in conjunction with the `-g` option.
3333
- `--nolocalkeys` to generate a keystore entry with zeroized key material. This option is only useful on platforms that support using an external key by reference, such as wolfHSM. Only has an effect if used in conjunction with the `-g` option.
34+
- `--no-overwrite` to avoid prompt warning that keyfiles files already exist. This option ensures existing files are not overwritten.
3435

3536
Arguments are not exclusive, and can be repeated more than once to populate a keystore with multiple keys.
3637

@@ -185,7 +186,7 @@ Options:
185186
By default, the sign tool appends the sha of the base image to the manifest header,
186187
so wolfBoot will refuse to start a delta update if the sha does not match the
187188
one of the existing image. However, this takes up 32 to 48 bytes extra in the
188-
manifest header, so this option is available to provide compatibility on
189+
manifest header, so this option is available to provide compatibility on
189190
existing installations without this feature, where the header size does not
190191
allow to accommodate the field
191192

tools/keytools/keygen.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
/* Globals */
113113
static FILE *fpub, *fpub_image;
114114
static int force = 0;
115+
static int no_overwrite = 0; /* when set, avoids prompt if !force and files exist */
115116
#if defined(WOLFBOOT_RENESAS_RSIP) || \
116117
defined(WOLFBOOT_RENESAS_TSIP) || \
117118
defined(WOLFBOOT_RENESAS_SCEPROTECT)
@@ -1155,18 +1156,24 @@ static void key_gen_check(const char *kfilename)
11551156
FILE *f;
11561157
f = fopen(kfilename, "rb");
11571158
if (!force && (f != NULL)) {
1158-
char reply[40];
1159-
int replySz;
1160-
printf("** Warning: key file already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1161-
fflush(stdout);
1162-
replySz = scanf("%s", reply);
1163-
printf("Reply is [%s]\n", reply);
1164-
fclose(f);
1165-
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1166-
printf("Operation aborted by user.");
1167-
exit(5);
1168-
} else {
1169-
unlink(kfilename);
1159+
if (no_overwrite) {
1160+
printf("** Warning: key file already exists and will not be overwritten!");
1161+
}
1162+
else {
1163+
char reply[40];
1164+
int replySz;
1165+
printf("** Warning: key file already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1166+
fflush(stdout);
1167+
replySz = scanf("%s", reply);
1168+
printf("Reply is [%s]\n", reply);
1169+
fclose(f);
1170+
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1171+
printf("Operation aborted by user.");
1172+
exit(5);
1173+
}
1174+
else {
1175+
unlink(kfilename);
1176+
}
11701177
}
11711178
}
11721179
}
@@ -1402,6 +1409,9 @@ int main(int argc, char** argv)
14021409
else if (strcmp(argv[i], "--force") == 0) {
14031410
force = 1;
14041411
}
1412+
else if (strcmp(argv[i], "--no-overwrite") == 0) {
1413+
no_overwrite = 1;
1414+
}
14051415
else if (strcmp(argv[i], "--der") == 0) {
14061416
saveAsDer = 1;
14071417
}
@@ -1436,6 +1446,7 @@ int main(int argc, char** argv)
14361446
i++;
14371447
sprintf(pubkeyfile,"%s%s", argv[i], "/keystore.c");
14381448
sprintf(pubkeyimg, "%s%s", argv[i], "/keystore.der");
1449+
printf("keystore file: %s\n", pubkeyfile);
14391450
i++;
14401451
continue;
14411452
}
@@ -1458,15 +1469,20 @@ int main(int argc, char** argv)
14581469
exit(0);
14591470
fpub = fopen(pubkeyfile, "rb");
14601471
if (!force && (fpub != NULL)) {
1472+
if (no_overwrite) {
1473+
printf("** Not overwriting existing keystore file: %s\n", pubkeyfile);
1474+
exit(0);
1475+
}
14611476
char reply[40];
14621477
int replySz;
1463-
printf("** Warning: keystore already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1478+
printf("** Warning: keystore file already exists! %s\n", pubkeyfile);
1479+
printf("Are you sure you want to generate a new key and overwrite the existing key ? [Type 'Yes'] : ");
14641480
fflush(stdout);
14651481
replySz = scanf("%s", reply);
14661482
printf("Reply is [%s]\n", reply);
14671483
fclose(fpub);
14681484
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1469-
printf("Operation aborted by user.");
1485+
printf("Operation aborted by user.\n");
14701486
exit(5);
14711487
} else {
14721488
unlink(pubkeyfile);

0 commit comments

Comments
 (0)