Skip to content

Commit 1c10b01

Browse files
committed
Added support for SET RETRIES.
Signed-off-by: Pol Henarejos <[email protected]>
1 parent e0e1b37 commit 1c10b01

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/openpgp/piv.c

+55
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ static void scan_files() {
154154
flash_write_data_to_file(ef, def, sizeof(def));
155155
}
156156
}
157+
if ((ef = search_by_fid(EF_PW_RETRIES, NULL, SPECIFY_ANY))) {
158+
if (file_get_size(ef) == 0) {
159+
printf("PW retries is empty. Initializing to default\r\n");
160+
const uint8_t def[] = { 0x1, 3, 3, 3, 3, 3 };
161+
flash_write_data_to_file(ef, def, sizeof(def));
162+
}
163+
else if (file_get_size(ef) == 4) {
164+
printf("PW retries is older. Initializing to default\r\n");
165+
uint8_t def[6] = { 0 };
166+
memcpy(def, file_get_data(ef), 4);
167+
def[4] = def[5] = 3; // PIV retries
168+
flash_write_data_to_file(ef, def, sizeof(def));
169+
}
170+
}
157171
bool reset_dek = false;
158172
if ((ef = search_by_fid(EF_DEK, NULL, SPECIFY_ANY))) {
159173
if (file_get_size(ef) == 0 || file_get_size(ef) == IV_SIZE+32*3) {
@@ -781,6 +795,9 @@ static int cmd_change_pin() {
781795
return SW_INCORRECT_P1P2();
782796
}
783797
file_t *ef = search_by_fid(pin_ref == 0x80 ? EF_PIV_PIN : EF_PIV_PUK, NULL, SPECIFY_ANY);
798+
if (!ef) {
799+
return SW_MEMORY_FAILURE();
800+
}
784801
uint8_t *pin_data = file_get_data(ef), pin_len = apdu.nc - pin_data[0];
785802
uint8_t dhash[33];
786803
double_hash_pin(apdu.data, pin_data[0], dhash + 1);
@@ -791,6 +808,7 @@ static int cmd_change_pin() {
791808
double_hash_pin(apdu.data + pin_data[0], pin_len, dhash + 1);
792809
flash_write_data_to_file(ef, dhash, sizeof(dhash));
793810
pin_reset_retries(ef, true);
811+
low_flash_available();
794812
return SW_OK();
795813
}
796814

@@ -799,6 +817,9 @@ static int cmd_reset_retry() {
799817
return SW_INCORRECT_P1P2();
800818
}
801819
file_t *ef = search_by_fid(EF_PIV_PUK, NULL, SPECIFY_ANY);
820+
if (!ef) {
821+
return SW_MEMORY_FAILURE();
822+
}
802823
uint8_t *puk_data = file_get_data(ef), pin_len = apdu.nc - puk_data[0];
803824
uint8_t dhash[33];
804825
double_hash_pin(apdu.data, puk_data[0], dhash + 1);
@@ -810,6 +831,38 @@ static int cmd_reset_retry() {
810831
ef = search_by_fid(EF_PIV_PIN, NULL, SPECIFY_ANY);
811832
flash_write_data_to_file(ef, dhash, sizeof(dhash));
812833
pin_reset_retries(ef, true);
834+
low_flash_available();
835+
return SW_OK();
836+
}
837+
838+
static int cmd_set_retries() {
839+
file_t *ef = search_by_fid(EF_PW_RETRIES, NULL, SPECIFY_ANY);
840+
if (!ef) {
841+
return SW_MEMORY_FAILURE();
842+
}
843+
uint8_t *tmp = (uint8_t *)calloc(1, file_get_size(ef));
844+
memcpy(tmp, file_get_data(ef), file_get_size(ef));
845+
tmp[4] = P1(apdu);
846+
tmp[5] = P2(apdu);
847+
flash_write_data_to_file(ef, tmp, file_get_size(ef));
848+
free(tmp);
849+
850+
ef = search_by_fid(EF_PIV_PIN, NULL, SPECIFY_ANY);
851+
const uint8_t def_pin[8] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0xFF, 0xFF };
852+
uint8_t dhash[33];
853+
dhash[0] = sizeof(def_pin);
854+
double_hash_pin(def_pin, sizeof(def_pin), dhash + 1);
855+
flash_write_data_to_file(ef, dhash, sizeof(dhash));
856+
pin_reset_retries(ef, true);
857+
858+
ef = search_by_fid(EF_PIV_PUK, NULL, SPECIFY_ANY);
859+
const uint8_t def_puk[8] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
860+
dhash[0] = sizeof(def_puk);
861+
double_hash_pin(def_puk, sizeof(def_puk), dhash + 1);
862+
flash_write_data_to_file(ef, dhash, sizeof(dhash));
863+
pin_reset_retries(ef, true);
864+
865+
low_flash_available();
813866
return SW_OK();
814867
}
815868

@@ -827,6 +880,7 @@ static int cmd_reset_retry() {
827880
#define INS_MOVE_KEY 0xF6
828881
#define INS_CHANGE_PIN 0x24
829882
#define INS_RESET_RETRY 0x2C
883+
#define INS_SET_RETRIES 0xFA
830884

831885
static const cmd_t cmds[] = {
832886
{ INS_VERSION, cmd_version },
@@ -842,6 +896,7 @@ static const cmd_t cmds[] = {
842896
{ INS_MOVE_KEY, cmd_move_key },
843897
{ INS_CHANGE_PIN, cmd_change_pin },
844898
{ INS_RESET_RETRY, cmd_reset_retry },
899+
{ INS_SET_RETRIES, cmd_set_retries },
845900
{ 0x00, 0x0 }
846901
};
847902

0 commit comments

Comments
 (0)