Merge pull request #1577 from merlokk/cip_def

cipurse default parameters refactoring and add aid
This commit is contained in:
Oleg Moiseenko 2022-02-01 18:46:14 +02:00 committed by GitHub
commit c2c57d956a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 22 deletions

View file

@ -27,6 +27,7 @@
#define CIPURSE_DEFAULT_KEY {0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73} #define CIPURSE_DEFAULT_KEY {0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73}
#define CIPURSE_DEFAULT_AID {0x41, 0x44, 0x20, 0x46, 0x31}
void CIPURSEPrintInfoFile(uint8_t *data, size_t len); void CIPURSEPrintInfoFile(uint8_t *data, size_t len);

View file

@ -76,7 +76,7 @@ static const APDUSpcCodeDescription_t DeleteAPDUCodeDescriptions[] = {
static uint8_t defaultKeyId = 1; static uint8_t defaultKeyId = 1;
static uint8_t defaultKey[CIPURSE_AES_KEY_LENGTH] = CIPURSE_DEFAULT_KEY; static uint8_t defaultKey[CIPURSE_AES_KEY_LENGTH] = CIPURSE_DEFAULT_KEY;
#define CIPURSE_MAX_AID_LENGTH 16 #define CIPURSE_MAX_AID_LENGTH 16
static uint8_t defaultAID[CIPURSE_MAX_AID_LENGTH] = {0x41, 0x44, 0x20, 0x46, 0x31, 0x00}; static uint8_t defaultAID[CIPURSE_MAX_AID_LENGTH] = CIPURSE_DEFAULT_AID;
static size_t defaultAIDLength = 5; static size_t defaultAIDLength = 5;
static uint16_t defaultFileId = 0x2ff7; static uint16_t defaultFileId = 0x2ff7;
@ -1125,14 +1125,17 @@ static int CmdHFCipurseDefault(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
CLIParserInit(&ctx, "hf cipurse default", CLIParserInit(&ctx, "hf cipurse default",
"Set default parameters for access to cipurse card", "Set default parameters for access to cipurse card",
"hf cipurse default -n 1 -k 65656565656565656565656565656565 --fid 2ff7 -> Set key, key id and file id\n"); "hf cipurse default --reset -> reset parameters to default\n"
"hf cipurse default -n 1 -k 65656565656565656565656565656565 --fid 2ff7 -> Set key, key id and file id\n"
"hf cipurse default --aid 4144204632 -> set default application id\n");
void *argtable[] = { void *argtable[] = {
arg_param_begin, arg_param_begin,
arg_lit0(NULL, "clear", "resets to defaults"), arg_lit0(NULL, "clear", "resets to defaults"),
arg_int0("n", NULL, "<dec>", "Key ID"), arg_int0("n", NULL, "<dec>", "Key ID"),
arg_str0("k", "key", "<hex>", "Authentication key"), arg_str0("k", "key", "<hex>", "Authentication key"),
arg_str0(NULL, "fid", "<hex>", "File ID"), arg_str0(NULL, "aid", "<hex 1..16 bytes>", "application ID (AID)"),
arg_str0(NULL, "fid", "<hex 2 bytes>", "File ID"),
arg_param_end arg_param_end
}; };
CLIExecWithReturn(ctx, Cmd, argtable, true); CLIExecWithReturn(ctx, Cmd, argtable, true);
@ -1143,43 +1146,41 @@ static int CmdHFCipurseDefault(const char *Cmd) {
defaultFileId = 0x2ff7; defaultFileId = 0x2ff7;
uint8_t ckey[CIPURSE_AES_KEY_LENGTH] = CIPURSE_DEFAULT_KEY; uint8_t ckey[CIPURSE_AES_KEY_LENGTH] = CIPURSE_DEFAULT_KEY;
memcpy(defaultKey, ckey, CIPURSE_AES_KEY_LENGTH); memcpy(defaultKey, ckey, CIPURSE_AES_KEY_LENGTH);
uint8_t aid[CIPURSE_MAX_AID_LENGTH] = CIPURSE_DEFAULT_AID;
memcpy(defaultAID, aid, CIPURSE_MAX_AID_LENGTH);
} }
defaultKeyId = arg_get_int_def(ctx, 2, defaultKeyId); defaultKeyId = arg_get_int_def(ctx, 2, defaultKeyId);
uint8_t hdata[250] = {0}; uint8_t aid[CIPURSE_MAX_AID_LENGTH] = {0};
int hdatalen = sizeof(hdata); size_t aidLen = 0;
CLIGetHexWithReturn(ctx, 3, hdata, &hdatalen); bool useAID = false;
if (hdatalen && hdatalen != 16) { uint16_t fileId = defaultFileId;
PrintAndLogEx(ERR, _RED_("ERROR:") " key length for AES128 must be 16 bytes only"); bool useFID = false;
int res = CLIParseCommandParameters(ctx, 3, 4, 5, 0, 0, defaultKey, aid, &aidLen, &useAID, &fileId, &useFID, NULL, NULL);
if (res) {
CLIParserFree(ctx); CLIParserFree(ctx);
return PM3_EINVARG; return PM3_EINVARG;
} }
if (hdatalen) if (useFID)
memcpy(defaultKey, hdata, CIPURSE_AES_KEY_LENGTH); defaultFileId = fileId;
memset(hdata, 0, sizeof(hdata)); if (useAID) {
hdatalen = sizeof(hdata); memcpy(defaultAID, aid, CIPURSE_MAX_AID_LENGTH);
CLIGetHexWithReturn(ctx, 4, hdata, &hdatalen); defaultAIDLength = aidLen;
if (hdatalen && hdatalen != 2) {
PrintAndLogEx(ERR, _RED_("ERROR:") " file id length must be 2 bytes only");
CLIParserFree(ctx);
return PM3_EINVARG;
} }
if (hdatalen)
defaultFileId = (hdata[0] << 8) + hdata[1];
CLIParserFree(ctx); CLIParserFree(ctx);
PrintAndLogEx(INFO, "-----------" _CYAN_("Default parameters") "---------------------------------"); PrintAndLogEx(INFO, "------------------- " _CYAN_("Default parameters") " -------------------");
PrintAndLogEx(INFO, "Key ID : %d", defaultKeyId); PrintAndLogEx(INFO, "Key ID : %d", defaultKeyId);
PrintAndLogEx(INFO, "Key : %s", sprint_hex(defaultKey, sizeof(defaultKey))); PrintAndLogEx(INFO, "Key : %s", sprint_hex(defaultKey, sizeof(defaultKey)));
PrintAndLogEx(INFO, "AID : %s", sprint_hex(defaultAID, defaultAIDLength));
PrintAndLogEx(INFO, "File ID: 0x%04x", defaultFileId); PrintAndLogEx(INFO, "File ID: 0x%04x", defaultFileId);
return PM3_SUCCESS; return PM3_SUCCESS;
} }