4242
4343static uint8_t zeros [16 ] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
4444
45+ #define MAX_DIVERSIFIER_LEN 16
46+
4547static int CmdHelp (const char * Cmd );
4648
4749typedef struct {
@@ -681,7 +683,7 @@ static int select_df_decode(uint8_t *response, uint8_t response_length, int *ALG
681683 return PM3_SUCCESS ;
682684}
683685
684- static int select_ADF_decrypt (const char * selectADFOID , uint8_t * CRYPTOGRAM_encrypted_data_raw , uint8_t * CRYPTOGRAM_Diversifier , int encryption_algorithm , int key_index ) {
686+ static int select_ADF_decrypt (const char * selectADFOID , uint8_t * CRYPTOGRAM_encrypted_data_raw , uint8_t * CRYPTOGRAM_Diversifier , int * diversifier_length_out , int encryption_algorithm , int key_index ) {
685687 // --------------- Decrypt ----------------
686688
687689 // 1. MAC Verify - AES/CBC-decrypt (IV || cryptogram || 16 bytes after 8e 08) with the MAC key & keep the last block
@@ -716,6 +718,10 @@ static int select_ADF_decrypt(const char *selectADFOID, uint8_t *CRYPTOGRAM_encr
716718 if (CRYPTOGRAM_decrypted_data [i ] == 0x06 && CRYPTOGRAM_decrypted_data [i + 1 ] < 20 ) {
717719 adf_length = ((CRYPTOGRAM_decrypted_data [i + 1 ]));
718720 diversifier_length = CRYPTOGRAM_decrypted_data [i + adf_length + 3 ];
721+ if (* diversifier_length_out < diversifier_length ) {
722+ PrintAndLogEx (ERR , "Diversifier too long" );
723+ return PM3_ESOFT ;
724+ }
719725
720726 uint8_t CRYPTOGRAM_ADF [strlen (selectADFOID ) / 2 ];
721727
@@ -745,6 +751,7 @@ static int select_ADF_decrypt(const char *selectADFOID, uint8_t *CRYPTOGRAM_encr
745751 // PrintAndLogEx(SUCCESS, "Supplied ADF...................... "_YELLOW_("%s"), selectADFOID_UPPER); // ADF Selected
746752 return PM3_ESOFT ;
747753 }
754+ * diversifier_length_out = diversifier_length ;
748755
749756 // PrintAndLogEx(INFO, "--- " _CYAN_("Decrypted Response") " ---------------------------");
750757 // PrintAndLogEx(SUCCESS, "Decoded ADF...................... "_YELLOW_("%s"), sprint_hex_inrow(&CRYPTOGRAM_ADF[2],adf_length)); // ADF Selected
@@ -756,7 +763,7 @@ static int select_ADF_decrypt(const char *selectADFOID, uint8_t *CRYPTOGRAM_encr
756763 return PM3_ESOFT ;
757764};
758765
759- static int seos_mutual_auth (uint8_t * randomICC , uint8_t * CRYPTOGRAM_Diversifier , uint8_t diversifier_len , uint8_t * mutual_auth_randomIFD , uint8_t * mutual_auth_keyICC , uint8_t * randomIFD , uint8_t randomIFD_len , uint8_t * keyIFD , uint8_t keyIFD_len , int encryption_algorithm , int hash_algorithm , int key_index ) {
766+ static int seos_mutual_auth (uint8_t * adfOID , size_t adfoid_len , uint8_t * randomICC , uint8_t * CRYPTOGRAM_Diversifier , uint8_t diversifier_len , uint8_t * mutual_auth_randomIFD , uint8_t * mutual_auth_keyICC , uint8_t * randomIFD , uint8_t randomIFD_len , uint8_t * keyIFD , uint8_t keyIFD_len , int encryption_algorithm , int hash_algorithm , int key_index ) {
760767 uint8_t response [PM3_CMD_DATA_SIZE ];
761768
762769 // ---------------- Diversify Keys ----------------
@@ -765,18 +772,17 @@ static int seos_mutual_auth(uint8_t *randomICC, uint8_t *CRYPTOGRAM_Diversifier,
765772 uint8_t keyslot = 0x01 ; // up to 0x0F
766773 uint8_t AES_key [24 ] = {0x00 };
767774 uint8_t MAC_key [24 ] = {0x00 };
768- uint8_t adfOID [17 ] = {0x2b , 0x06 , 0x01 , 0x04 , 0x01 , 0x81 , 0xe4 , 0x38 , 0x01 , 0x01 , 0x02 , 0x01 , 0x18 , 0x01 , 0x01 , 0x02 , 0x02 };
769775
770776 // Null AES IV
771- uint8_t nullDiversifier [7 ] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
777+ uint8_t nullDiversifier [MAX_DIVERSIFIER_LEN ] = {0 };
772778
773- if (memcmp (CRYPTOGRAM_Diversifier , nullDiversifier , sizeof ( nullDiversifier ) ) == 0 ) {
779+ if (memcmp (CRYPTOGRAM_Diversifier , nullDiversifier , diversifier_len ) == 0 ) {
774780 PrintAndLogEx (ERR , "No Diversifier found" );
775781 return PM3_ESOFT ;
776782 }
777783
778- seos_kdf (true, mk , keyslot , adfOID , sizeof ( adfOID ) , CRYPTOGRAM_Diversifier , diversifier_len , AES_key , encryption_algorithm , hash_algorithm );
779- seos_kdf (false, mk , keyslot , adfOID , sizeof ( adfOID ) , CRYPTOGRAM_Diversifier , diversifier_len , MAC_key , encryption_algorithm , hash_algorithm );
784+ seos_kdf (true, mk , keyslot , adfOID , adfoid_len , CRYPTOGRAM_Diversifier , diversifier_len , AES_key , encryption_algorithm , hash_algorithm );
785+ seos_kdf (false, mk , keyslot , adfOID , adfoid_len , CRYPTOGRAM_Diversifier , diversifier_len , MAC_key , encryption_algorithm , hash_algorithm );
780786
781787 memcpy (& MAC_key [16 ], & MAC_key [0 ], 8 );
782788 memcpy (& AES_key [16 ], & AES_key [0 ], 8 );
@@ -1052,7 +1058,7 @@ static int seos_pacs_adf_select(char *oid, int oid_len, uint8_t *get_data, int g
10521058 uint8_t CRYPTOGRAM_encrypted_data [64 ]; // Encrypted Data
10531059 uint8_t MAC_value [8 ] = {0 }; // MAC Value
10541060
1055- uint8_t diversifier [7 ] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
1061+ uint8_t diversifier [MAX_DIVERSIFIER_LEN ] = {0 };
10561062 uint8_t RNDICC [8 ] = {0 };
10571063 uint8_t KeyICC [16 ] = {0 };
10581064 uint8_t RNDIFD [8 ] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
@@ -1072,9 +1078,15 @@ static int seos_pacs_adf_select(char *oid, int oid_len, uint8_t *get_data, int g
10721078 }
10731079
10741080 if (ALGORITHM_INFO_value1 == 0x09 || ALGORITHM_INFO_value1 == 0x02 ) {
1081+ uint8_t adf_bytes [124 ];
1082+ int adf_bytes_len = 0 ;
1083+ // Input into getHextoEOL is a Char string
1084+ param_gethex_to_eol (selectedOID , 0 , adf_bytes , sizeof (adf_bytes ), & adf_bytes_len );
1085+
10751086
1076- select_ADF_decrypt (selectedADF , CRYPTOGRAM_encrypted_data , diversifier , ALGORITHM_INFO_value1 , key_index );
1077- seos_mutual_auth (RNDICC , diversifier , sizeof (diversifier ), RNDIFD , KeyICC , RNDIFD , sizeof (RNDIFD ), KeyIFD , sizeof (KeyIFD ), ALGORITHM_INFO_value1 , ALGORITHM_INFO_value2 , key_index );
1087+ int diversifier_length = sizeof (diversifier );
1088+ select_ADF_decrypt (selectedADF , CRYPTOGRAM_encrypted_data , diversifier , & diversifier_length , ALGORITHM_INFO_value1 , key_index );
1089+ seos_mutual_auth (adf_bytes , adf_bytes_len , RNDICC , diversifier , diversifier_length , RNDIFD , KeyICC , RNDIFD , sizeof (RNDIFD ), KeyIFD , sizeof (KeyIFD ), ALGORITHM_INFO_value1 , ALGORITHM_INFO_value2 , key_index );
10781090 create_mutual_auth_key (KeyIFD , KeyICC , RNDICC , RNDIFD , Diversified_New_EncryptionKey , Diversified_New_MACKey , ALGORITHM_INFO_value1 , ALGORITHM_INFO_value2 );
10791091
10801092 uint8_t sio_buffer_out [PM3_CMD_DATA_SIZE ];
0 commit comments