@@ -154,6 +154,20 @@ static void scan_files() {
154
154
flash_write_data_to_file (ef , def , sizeof (def ));
155
155
}
156
156
}
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
+ }
157
171
bool reset_dek = false;
158
172
if ((ef = search_by_fid (EF_DEK , NULL , SPECIFY_ANY ))) {
159
173
if (file_get_size (ef ) == 0 || file_get_size (ef ) == IV_SIZE + 32 * 3 ) {
@@ -781,6 +795,9 @@ static int cmd_change_pin() {
781
795
return SW_INCORRECT_P1P2 ();
782
796
}
783
797
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
+ }
784
801
uint8_t * pin_data = file_get_data (ef ), pin_len = apdu .nc - pin_data [0 ];
785
802
uint8_t dhash [33 ];
786
803
double_hash_pin (apdu .data , pin_data [0 ], dhash + 1 );
@@ -791,6 +808,7 @@ static int cmd_change_pin() {
791
808
double_hash_pin (apdu .data + pin_data [0 ], pin_len , dhash + 1 );
792
809
flash_write_data_to_file (ef , dhash , sizeof (dhash ));
793
810
pin_reset_retries (ef , true);
811
+ low_flash_available ();
794
812
return SW_OK ();
795
813
}
796
814
@@ -799,6 +817,9 @@ static int cmd_reset_retry() {
799
817
return SW_INCORRECT_P1P2 ();
800
818
}
801
819
file_t * ef = search_by_fid (EF_PIV_PUK , NULL , SPECIFY_ANY );
820
+ if (!ef ) {
821
+ return SW_MEMORY_FAILURE ();
822
+ }
802
823
uint8_t * puk_data = file_get_data (ef ), pin_len = apdu .nc - puk_data [0 ];
803
824
uint8_t dhash [33 ];
804
825
double_hash_pin (apdu .data , puk_data [0 ], dhash + 1 );
@@ -810,6 +831,38 @@ static int cmd_reset_retry() {
810
831
ef = search_by_fid (EF_PIV_PIN , NULL , SPECIFY_ANY );
811
832
flash_write_data_to_file (ef , dhash , sizeof (dhash ));
812
833
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 ();
813
866
return SW_OK ();
814
867
}
815
868
@@ -827,6 +880,7 @@ static int cmd_reset_retry() {
827
880
#define INS_MOVE_KEY 0xF6
828
881
#define INS_CHANGE_PIN 0x24
829
882
#define INS_RESET_RETRY 0x2C
883
+ #define INS_SET_RETRIES 0xFA
830
884
831
885
static const cmd_t cmds [] = {
832
886
{ INS_VERSION , cmd_version },
@@ -842,6 +896,7 @@ static const cmd_t cmds[] = {
842
896
{ INS_MOVE_KEY , cmd_move_key },
843
897
{ INS_CHANGE_PIN , cmd_change_pin },
844
898
{ INS_RESET_RETRY , cmd_reset_retry },
899
+ { INS_SET_RETRIES , cmd_set_retries },
845
900
{ 0x00 , 0x0 }
846
901
};
847
902
0 commit comments