Skip to content

Commit 2082bd0

Browse files
committed
Avoid to silently overwrite keystore.c
1 parent 2133155 commit 2082bd0

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ clean:
302302
$(Q)rm -f wolfboot.bin wolfboot.elf wolfboot.map test-update.rom wolfboot.hex
303303
$(Q)rm -f $(MACHINE_OBJ) $(MAIN_TARGET) $(LSCRIPT)
304304
$(Q)rm -f $(OBJS)
305+
$(Q)rm -f tools/keytools/otp/otp-keystore-gen
305306
$(Q)$(MAKE) -C test-app -s clean
306307
$(Q)$(MAKE) -C tools/check_config -s clean
307308
$(Q)$(MAKE) -C stage1 -s clean

tools/keytools/keygen.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ static void key_gen_check(const char *kfilename)
764764
if (!force && (f != NULL)) {
765765
char reply[40];
766766
int replySz;
767-
printf("** Warning: key file already exist! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
767+
printf("** Warning: key file already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
768768
fflush(stdout);
769769
replySz = scanf("%s", reply);
770770
printf("Reply is [%s]\n", reply);
@@ -776,6 +776,22 @@ static void key_gen_check(const char *kfilename)
776776
unlink(kfilename);
777777
}
778778
}
779+
f = fopen(pubkeyfile, "rb");
780+
if (!force && (f != NULL)) {
781+
char reply[40];
782+
int replySz;
783+
printf("** Warning: keystore already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
784+
fflush(stdout);
785+
replySz = scanf("%s", reply);
786+
printf("Reply is [%s]\n", reply);
787+
fclose(f);
788+
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
789+
printf("Operation aborted by user.");
790+
exit(5);
791+
} else {
792+
unlink(pubkeyfile);
793+
}
794+
}
779795
}
780796

781797
static void key_generate(uint32_t ktype, const char *kfilename, uint32_t id_mask)

tools/keytools/otp/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ SIZE?=$(CROSS_COMPILE)size
3434

3535
all: otp-keystore-primer.bin otp-keystore-gen
3636

37-
otp-keystore-gen: otp-keystore-gen.c ../../../src/keystore.c
38-
gcc -o $@ $^ -I. -I../../../ -I../../../include -DFLASH_OTP_KEYSTORE
37+
otp-keystore-gen: otp-keystore-gen.c
38+
gcc -o $@ otp-keystore-gen.c ../../../src/keystore.c -I. -I../../../ -I../../../include -DFLASH_OTP_KEYSTORE
3939

4040

4141
otp-keystore-primer.bin: otp-keystore-primer.elf

tools/keytools/otp/otp-keystore-gen.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ int main(void)
6363
slot_size = keystore_get_size(0);
6464
slot_size += KEYSTORE_HDR_SIZE;
6565
fprintf(stderr, "Slot size: %d\n", slot_size);
66+
fprintf(stderr, "Number of slots: %d\n", n_keys);
67+
fprintf(stderr, "%s size: %d\n", outfile, slot_size * n_keys + sizeof(struct wolfBoot_otp_hdr));
6668

6769
ofd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
6870
if (ofd < 0) {

0 commit comments

Comments
 (0)