Remove redundant methods from Aliro/VAS, make private key load helpers universal
This commit is contained in:
1 parent
f504af8a04
commit
ec3b249d30
9 files changed
+174
-223
No files matched your search
+84
-81
@@ -34,12 +34,15 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "crypto/libpcrypto.h"
|
||||
#include "fileutils.h"
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
#include "mbedtls/ecc_point_compression.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
|
||||
#define VAS_MAX_KEY_INPUT 8192
|
||||
|
||||
static const iso14a_polling_frame_t WUPA_FRAME = {
|
||||
.frame = { 0x52 },
|
||||
@@ -292,6 +295,9 @@ static int CreateGetVASDataCommand(const uint8_t *pidHash, const char *url, size
|
||||
|
||||
static int ParseGetVASDataResponse(const uint8_t *res, size_t resLen, uint8_t *cryptogram, size_t *cryptogramLen) {
|
||||
struct tlvdb *tlvRoot = tlvdb_parse_multi(res, resLen);
|
||||
if (tlvRoot == NULL) {
|
||||
return PM3_ECARDEXCHANGE;
|
||||
}
|
||||
|
||||
const struct tlvdb *cryptogramTlvdb = tlvdb_find_full(tlvRoot, 0x9F27);
|
||||
if (cryptogramTlvdb == NULL) {
|
||||
@@ -307,48 +313,52 @@ static int ParseGetVASDataResponse(const uint8_t *res, size_t resLen, uint8_t *c
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int LoadReaderPrivateKey(const uint8_t *buf, size_t bufLen, mbedtls_ecp_keypair *privKey) {
|
||||
struct tlvdb *derRoot = tlvdb_parse_multi(buf, bufLen);
|
||||
|
||||
const struct tlvdb *privkeyTlvdb = tlvdb_find_full(derRoot, 0x04);
|
||||
if (privkeyTlvdb == NULL) {
|
||||
tlvdb_free(derRoot);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
const struct tlv *privkeyTlv = tlvdb_get_tlv(privkeyTlvdb);
|
||||
|
||||
if (mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP256R1, privKey, privkeyTlv->value, privkeyTlv->len)) {
|
||||
tlvdb_free(derRoot);
|
||||
PrintAndLogEx(FAILED, "Unable to parse private key file. Should be DER encoded ASN1");
|
||||
return PM3_EINVARG;
|
||||
static int LoadReaderPrivateKey(const char *input_or_path, mbedtls_ecp_keypair *privKey) {
|
||||
uint8_t key_d[32] = {0};
|
||||
int res = ensure_ec_private_key(input_or_path, MBEDTLS_ECP_DP_SECP256R1, key_d, sizeof(key_d));
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(FAILED, "Invalid private key input");
|
||||
PrintAndLogEx(INFO, "Accepted formats:");
|
||||
PrintAndLogEx(INFO, " 1) PEM string with headers (BEGIN PRIVATE KEY)");
|
||||
PrintAndLogEx(INFO, " 2) DER bytes as hex or base64");
|
||||
PrintAndLogEx(INFO, " 3) Scalar as hex or base64");
|
||||
PrintAndLogEx(INFO, " 4) File path to a key in any of the formats above");
|
||||
return res;
|
||||
}
|
||||
|
||||
const struct tlvdb *pubkeyCoordsTlvdb = tlvdb_find_full(derRoot, 0x03);
|
||||
if (pubkeyCoordsTlvdb == NULL) {
|
||||
tlvdb_free(derRoot);
|
||||
PrintAndLogEx(FAILED, "Private key file should include public key component");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
const struct tlv *pubkeyCoordsTlv = tlvdb_get_tlv(pubkeyCoordsTlvdb);
|
||||
if (pubkeyCoordsTlv->len != 66 || pubkeyCoordsTlv->value[0] != 0x00 || pubkeyCoordsTlv->value[1] != 0x04) {
|
||||
tlvdb_free(derRoot);
|
||||
PrintAndLogEx(FAILED, "Invalid public key data");
|
||||
return PM3_EINVARG;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
static const uint8_t personalization[] = "pm3-vas";
|
||||
int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
personalization, sizeof(personalization) - 1);
|
||||
if (ret != 0) {
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
mbedtls_entropy_free(&entropy);
|
||||
PrintAndLogEx(FAILED, "Unable to initialize random generator for key derivation");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (mbedtls_ecp_point_read_binary(&privKey->grp, &privKey->Q, pubkeyCoordsTlv->value + 1, 65)) {
|
||||
PrintAndLogEx(FAILED, "Failed to read in public key coordinates");
|
||||
tlvdb_free(derRoot);
|
||||
ret = mbedtls_ecp_group_load(&privKey->grp, MBEDTLS_ECP_DP_SECP256R1);
|
||||
if (ret == 0) {
|
||||
ret = mbedtls_mpi_read_binary(&privKey->d, key_d, sizeof(key_d));
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = mbedtls_ecp_check_privkey(&privKey->grp, &privKey->d);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = mbedtls_ecp_mul(&privKey->grp, &privKey->Q, &privKey->d, &privKey->grp.G,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
}
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
mbedtls_entropy_free(&entropy);
|
||||
|
||||
if (ret != 0 || mbedtls_ecp_check_pubkey(&privKey->grp, &privKey->Q) != 0) {
|
||||
PrintAndLogEx(FAILED, "VAS protocol requires a valid private key on curve P-256");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if (mbedtls_ecp_check_pubkey(&privKey->grp, &privKey->Q)) {
|
||||
PrintAndLogEx(FAILED, "VAS protocol requires an elliptic key on the P-256 curve");
|
||||
tlvdb_free(derRoot);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
tlvdb_free(derRoot);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -427,7 +437,11 @@ static int DecryptVASCryptogram(uint8_t *pidHash, uint8_t *cryptogram, size_t cr
|
||||
|
||||
mbedtls_ecp_keypair mobilePubKey;
|
||||
mbedtls_ecp_keypair_init(&mobilePubKey);
|
||||
mobilePubKey.grp = privKey->grp;
|
||||
if (mbedtls_ecp_group_load(&mobilePubKey.grp, privKey->grp.id) != 0) {
|
||||
mbedtls_ecp_keypair_free(&mobilePubKey);
|
||||
PrintAndLogEx(FAILED, "Unable to initialize mobile ephemeral key");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (LoadMobileEphemeralKey(cryptogram + 4, &mobilePubKey) != PM3_SUCCESS) {
|
||||
mbedtls_ecp_keypair_free(&mobilePubKey);
|
||||
@@ -535,6 +549,7 @@ static int VASReader(uint8_t *pidHash, const char *url, size_t urlLen, uint8_t *
|
||||
|
||||
if (apduResLen == 0 || apduRes[0] != 0x70) {
|
||||
PrintAndLogEx(FAILED, "Invalid response from peer");
|
||||
return PM3_ECARDEXCHANGE;
|
||||
}
|
||||
|
||||
return ParseGetVASDataResponse(apduRes, apduResLen, cryptogram, cryptogramLen);
|
||||
@@ -545,13 +560,15 @@ static int CmdVASReader(const char *Cmd) {
|
||||
CLIParserInit(&ctx, "hf vas reader",
|
||||
"Read and decrypt Value Added Services (VAS) message",
|
||||
"hf vas reader --url https://example.com -> URL Only mode\n"
|
||||
"hf vas reader --pid pass.com.passkit.pksamples.nfcdemo -f vas_privkey.der -@\n"
|
||||
"hf vas reader --pid pass.com.passkit.pksamples.nfcdemo -k vas.passkit.der -@\n"
|
||||
"hf vas reader --pid pass.com.pronto.zebra-wallet-pass.demo -k vas.zebra.der -@\n"
|
||||
"hf vas reader --pid pass.com.springcard.springblue.generic -k vas.springcard.der -@\n"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_str0(NULL, "pid", "<str>", "PID, pass type id"),
|
||||
arg_str0("f", "file", "<fn>", "path to terminal private key file"),
|
||||
arg_str0("k", "key,file,reader-private-key,readerprivkey,rpk", "<pem|der-b64|der-hex|scalar-b64|scalar-hex|path>", "Terminal private key: PEM, DER hex, scalar hex/base64, or file path"),
|
||||
arg_str0(NULL, "url", "<str>", "a URL to provide to the mobile device"),
|
||||
arg_lit0("@", NULL, "continuous mode"),
|
||||
arg_lit0("v", "verbose", "Verbose output"),
|
||||
@@ -563,12 +580,12 @@ static int CmdVASReader(const char *Cmd) {
|
||||
char pid[512] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)pid, 512, &pidlen);
|
||||
|
||||
int keyfnlen = 0;
|
||||
char keyfn[FILE_PATH_SIZE] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)keyfn, FILE_PATH_SIZE, &keyfnlen);
|
||||
int key_input_len = 0;
|
||||
char key_input[VAS_MAX_KEY_INPUT] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)key_input, sizeof(key_input), &key_input_len);
|
||||
|
||||
if (keyfnlen == 0 && pidlen > 0) {
|
||||
PrintAndLogEx(FAILED, "Must provide path to terminal private key if a pass type id is provided");
|
||||
if (key_input_len == 0 && pidlen > 0) {
|
||||
PrintAndLogEx(FAILED, "Must provide terminal private key if a pass type id is provided");
|
||||
CLIParserFree(ctx);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
@@ -581,22 +598,14 @@ static int CmdVASReader(const char *Cmd) {
|
||||
bool verbose = arg_get_lit(ctx, 5);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
// santity checks
|
||||
uint8_t *key_data = NULL;
|
||||
size_t key_datalen = 0;
|
||||
if (loadFile_safe(keyfn, "", (void **)&key_data, &key_datalen) != PM3_SUCCESS) {
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
const bool has_pid = pidlen > 0;
|
||||
mbedtls_ecp_keypair privKey;
|
||||
mbedtls_ecp_keypair_init(&privKey);
|
||||
|
||||
if (LoadReaderPrivateKey(key_data, key_datalen, &privKey) != PM3_SUCCESS) {
|
||||
free(key_data);
|
||||
if (has_pid && LoadReaderPrivateKey(key_input, &privKey) != PM3_SUCCESS) {
|
||||
mbedtls_ecp_keypair_free(&privKey);
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
free(key_data);
|
||||
|
||||
PrintAndLogEx(INFO, "Requesting pass type id... " _GREEN_("%s"), sprint_ascii((uint8_t *) pid, pidlen));
|
||||
|
||||
@@ -619,17 +628,20 @@ static int CmdVASReader(const char *Cmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
res = VASReader((pidlen > 0) ? pidhash : NULL, url, urllen, cryptogram, &clen, verbose);
|
||||
res = VASReader(has_pid ? pidhash : NULL, url, urllen, cryptogram, &clen, verbose);
|
||||
if (res == PM3_SUCCESS) {
|
||||
|
||||
res = DecryptVASCryptogram(pidhash, cryptogram, clen, &privKey, msg, &mlen, ×tamp);
|
||||
if (res == PM3_SUCCESS) {
|
||||
PrintAndLogEx(SUCCESS, "Timestamp... " _YELLOW_("%d") " (secs since Jan 1, 2001)", timestamp);
|
||||
PrintAndLogEx(SUCCESS, "Message..... " _YELLOW_("%s"), sprint_ascii(msg, mlen));
|
||||
// extra sleep after successfull read
|
||||
if (continuous) {
|
||||
msleep(3000);
|
||||
if (has_pid) {
|
||||
res = DecryptVASCryptogram(pidhash, cryptogram, clen, &privKey, msg, &mlen, ×tamp);
|
||||
if (res == PM3_SUCCESS) {
|
||||
PrintAndLogEx(SUCCESS, "Timestamp... " _YELLOW_("%d") " (secs since Jan 1, 2001)", timestamp);
|
||||
PrintAndLogEx(SUCCESS, "Message..... " _YELLOW_("%s"), sprint_ascii(msg, mlen));
|
||||
// extra sleep after successfull read
|
||||
if (continuous) {
|
||||
msleep(3000);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
PrintAndLogEx(SUCCESS, "URL-only request completed");
|
||||
}
|
||||
}
|
||||
msleep(300);
|
||||
@@ -667,13 +679,13 @@ static int CmdVASDecrypt(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hf vas decrypt",
|
||||
"Decrypt a previously captured cryptogram",
|
||||
"hf vas decrypt --pid pass.com.passkit.pksamples.nfcdemo -f vas_privkey.der -d c0b77375eae416b79449347f9fe838c05cdb57dc7470b97b93b806cb348771d9bfbe29d58538c7c7d7c3d015fa205b68bfccd726058a62f7f44085ac98dbf877120fd9059f1507b956e0a6d56d0a\n"
|
||||
"hf vas decrypt --pid pass.com.passkit.pksamples.nfcdemo -k vas.passkit.der -d c0b77375eae416b79449347f9fe838c05cdb57dc7470b97b93b806cb348771d9bfbe29d58538c7c7d7c3d015fa205b68bfccd726058a62f7f44085ac98dbf877120fd9059f1507b956e0a6d56d0a\n"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_str0(NULL, "pid", "<str>", "PID, pass type id"),
|
||||
arg_str0("f", "file", "<fn>", "path to terminal private key file"),
|
||||
arg_str0("k", "key,file,reader-private-key,readerprivkey,rpk", "<pem|der-b64|der-hex|scalar-b64|scalar-hex|path>", "Terminal private key: PEM, DER hex, scalar hex/base64, or file path"),
|
||||
arg_str0("d", "data", "<hex>", "cryptogram to decrypt"),
|
||||
arg_param_end
|
||||
};
|
||||
@@ -683,12 +695,12 @@ static int CmdVASDecrypt(const char *Cmd) {
|
||||
char pid[512] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)pid, 512, &pidlen);
|
||||
|
||||
int keyfnlen = 0;
|
||||
char keyfn[FILE_PATH_SIZE] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)keyfn, FILE_PATH_SIZE, &keyfnlen);
|
||||
int key_input_len = 0;
|
||||
char key_input[VAS_MAX_KEY_INPUT] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)key_input, sizeof(key_input), &key_input_len);
|
||||
|
||||
if (keyfnlen == 0 && pidlen > 0) {
|
||||
PrintAndLogEx(FAILED, "Must provide path to terminal private key if a pass type id is provided");
|
||||
if (key_input_len == 0) {
|
||||
PrintAndLogEx(FAILED, "Must provide terminal private key input or file path");
|
||||
CLIParserFree(ctx);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
@@ -698,22 +710,13 @@ static int CmdVASDecrypt(const char *Cmd) {
|
||||
CLIGetHexWithReturn(ctx, 3, cryptogram, &clen);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
// santity checks
|
||||
uint8_t *key_data = NULL;
|
||||
size_t key_datalen = 0;
|
||||
if (loadFile_safe(keyfn, "", (void **)&key_data, &key_datalen) != PM3_SUCCESS) {
|
||||
return PM3_EFILE;
|
||||
}
|
||||
|
||||
mbedtls_ecp_keypair privKey;
|
||||
mbedtls_ecp_keypair_init(&privKey);
|
||||
|
||||
if (LoadReaderPrivateKey(key_data, key_datalen, &privKey) != PM3_SUCCESS) {
|
||||
free(key_data);
|
||||
if (LoadReaderPrivateKey(key_input, &privKey) != PM3_SUCCESS) {
|
||||
mbedtls_ecp_keypair_free(&privKey);
|
||||
return PM3_EFILE;
|
||||
}
|
||||
free(key_data);
|
||||
|
||||
uint8_t pidhash[32] = {0};
|
||||
sha256hash((uint8_t *) pid, pidlen, pidhash);
|
||||
|
||||
Reference in new issue
Block a user