diff --git a/CHANGELOG.md b/CHANGELOG.md index b655b3876..f36f364a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `hf emrtd info` - now correctly pretty print algos (@iceman1001) - Fixed `trace list -t topaz` - now parses topaz reader frames up to 16 bytes (@iceman1001) - Fixed `hw bwm` - doc and comment references still named the flat `hw bwmsetcap`/`bwmcharge`/`bwmautooff` commands from before the subgroup move (@xilni) - Added TA1=96 support via pwm to sim module `sim024.bin` - v4.66 (@antiklesys) diff --git a/client/src/cmdhfemrtd.c b/client/src/cmdhfemrtd.c index 04d0748b0..cfde4c9df 100644 --- a/client/src/cmdhfemrtd.c +++ b/client/src/cmdhfemrtd.c @@ -2468,15 +2468,28 @@ static void emrtd_print_cardaccess(const emrtd_cardaccess_t *ca) { PrintAndLogEx(SUCCESS, ""); } - if (info->alg != NULL) { - PrintAndLogEx(SUCCESS, "PACE algorithm........... " _YELLOW_("%s"), info->alg->name); - } else { - PrintAndLogEx(SUCCESS, "PACE algorithm........... " _YELLOW_("unknown") " ( OID %s )", - sprint_hex_inrow((uint8_t *)info->oid, info->oidlen)); + char oidstr[64] = { 0x00 }; + emrtd_oid_to_str(info->oid, info->oidlen, oidstr, sizeof(oidstr)); + if (oidstr[0] == 0x00) { + snprintf(oidstr, sizeof(oidstr), "%s", sprint_hex_inrow((uint8_t *)info->oid, info->oidlen)); } + bool is_pace = ((info->kind == EMRTD_SI_PACE) || (info->kind == EMRTD_SI_PACE_DP)); + + if (info->protocol != NULL) { + PrintAndLogEx(SUCCESS, "%s " _YELLOW_("%s"), + is_pace ? "PACE algorithm..........." : "Protocol.................", + info->protocol); + } else { + PrintAndLogEx(SUCCESS, "Protocol................. " _YELLOW_("unknown")); + } + + PrintAndLogEx(SUCCESS, "OID...................... %s", oidstr); + if (info->version != 0) { - PrintAndLogEx(SUCCESS, "PACE version............. " _YELLOW_("%u"), info->version); + PrintAndLogEx(SUCCESS, "%s " _YELLOW_("%u"), + is_pace ? "PACE version............." : "Version..................", + info->version); } if (info->has_param) { @@ -2488,18 +2501,40 @@ static void emrtd_print_cardaccess(const emrtd_cardaccess_t *ca) { } } + if (info->has_key_id) { + PrintAndLogEx(SUCCESS, "Key id................... " _YELLOW_("%u"), info->key_id); + } + if (info->supported) { PrintAndLogEx(SUCCESS, "Supported by this client. " _GREEN_("yes") "%s", ((int)i == ca->best) ? _GREEN_(" (selected)") : ""); - } else { + } else if (is_pace) { PrintAndLogEx(SUCCESS, "Supported by this client. " _YELLOW_("no") " ( %s )", (info->reason != NULL) ? info->reason : "unsupported"); + } else { + // not a PACE protocol at all, "unsupported PACE algorithm" would be + // the wrong thing to tell the user + PrintAndLogEx(SUCCESS, "Note..................... %s", + (info->reason != NULL) ? info->reason : "not a PACE protocol"); } } if (ca->best < 0) { PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(WARNING, "None of the offered PACE algorithms is supported by this client"); + + bool pace_offered = false; + for (size_t i = 0; i < ca->count; i++) { + if (ca->infos[i].kind == EMRTD_SI_PACE) { + pace_offered = true; + break; + } + } + + if (pace_offered) { + PrintAndLogEx(WARNING, "None of the offered PACE algorithms is supported by this client"); + } else { + PrintAndLogEx(WARNING, "EF_CardAccess does not offer any PACE algorithm"); + } } } diff --git a/client/src/emrtd/emrtd_pace.c b/client/src/emrtd/emrtd_pace.c index 1af3a8674..b8aa44047 100644 --- a/client/src/emrtd/emrtd_pace.c +++ b/client/src/emrtd/emrtd_pace.c @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include "crypto/libpcrypto.h" // sha1hash, sha256hash, des_encrypt/decrypt, pcrypto_rng_* #include "ui.h" // PrintAndLogEx @@ -86,6 +88,140 @@ static const emrtd_pacesdp_t pacesdp_table[] = { {32, NULL, 0, false, MBEDTLS_ECP_DP_NONE} }; +// Every SecurityInfo protocol that can share EF_CardAccess / EF_DG14 with the +// PACE entries. EF_CardAccess is a SET OF SecurityInfo, so a document lists +// everything it supports in there, not just PACE. TR-03110 part 3, A.1.1.1 +// covers the 0.4.0.127.0.7.2.2.x arcs, ICAO 9303-11 section 9 the ICAO ones. +// We do not run these protocols, but naming them beats printing a hex blob. +typedef struct { + size_t oidlen; + uint8_t oid[EMRTD_PACE_OID_MAXLEN]; + emrtd_si_t kind; + const char *name; +} emrtd_secinfo_t; + +// 0.4.0.127.0.7.2.2, bsi-de protocols(2) smartcard(2) +#define BSI_SC 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02 +// 2.23.136.1.1, id-icao-mrtd-security +#define ICAO_SEC 0x67, 0x81, 0x08, 0x01, 0x01 + +static const emrtd_secinfo_t secinfo_table[] = { +// len oid kind name + // id-PK, ChipAuthenticationPublicKeyInfo + {8, {BSI_SC, 0x01}, EMRTD_SI_PK, "Chip Authentication Public Key"}, + {9, {BSI_SC, 0x01, 0x01}, EMRTD_SI_PK, "Chip Authentication Public Key, DH"}, + {9, {BSI_SC, 0x01, 0x02}, EMRTD_SI_PK, "Chip Authentication Public Key, ECDH"}, + + // id-TA, TerminalAuthenticationInfo. The signature arcs below it show up in + // CV certificates rather than in EF_CardAccess, they are here for lookups. + {8, {BSI_SC, 0x02}, EMRTD_SI_TA, "Terminal Authentication"}, + {9, {BSI_SC, 0x02, 0x01}, EMRTD_SI_TA, "Terminal Authentication, RSA"}, + {10, {BSI_SC, 0x02, 0x01, 0x01}, EMRTD_SI_TA, "Terminal Authentication, RSA PKCS#1 v1.5 SHA-1"}, + {10, {BSI_SC, 0x02, 0x01, 0x02}, EMRTD_SI_TA, "Terminal Authentication, RSA PKCS#1 v1.5 SHA-256"}, + {10, {BSI_SC, 0x02, 0x01, 0x03}, EMRTD_SI_TA, "Terminal Authentication, RSA PSS SHA-1"}, + {10, {BSI_SC, 0x02, 0x01, 0x04}, EMRTD_SI_TA, "Terminal Authentication, RSA PSS SHA-256"}, + {10, {BSI_SC, 0x02, 0x01, 0x05}, EMRTD_SI_TA, "Terminal Authentication, RSA PKCS#1 v1.5 SHA-512"}, + {10, {BSI_SC, 0x02, 0x01, 0x06}, EMRTD_SI_TA, "Terminal Authentication, RSA PSS SHA-512"}, + {9, {BSI_SC, 0x02, 0x02}, EMRTD_SI_TA, "Terminal Authentication, ECDSA"}, + {10, {BSI_SC, 0x02, 0x02, 0x01}, EMRTD_SI_TA, "Terminal Authentication, ECDSA SHA-1"}, + {10, {BSI_SC, 0x02, 0x02, 0x02}, EMRTD_SI_TA, "Terminal Authentication, ECDSA SHA-224"}, + {10, {BSI_SC, 0x02, 0x02, 0x03}, EMRTD_SI_TA, "Terminal Authentication, ECDSA SHA-256"}, + {10, {BSI_SC, 0x02, 0x02, 0x04}, EMRTD_SI_TA, "Terminal Authentication, ECDSA SHA-384"}, + {10, {BSI_SC, 0x02, 0x02, 0x05}, EMRTD_SI_TA, "Terminal Authentication, ECDSA SHA-512"}, + + // id-CA. The 9 byte arcs are ChipAuthenticationDomainParameterInfo, the + // 10 byte ones ChipAuthenticationInfo. + {8, {BSI_SC, 0x03}, EMRTD_SI_CA, "Chip Authentication"}, + {9, {BSI_SC, 0x03, 0x01}, EMRTD_SI_CA_DP, "Chip Authentication, DH"}, + {10, {BSI_SC, 0x03, 0x01, 0x01}, EMRTD_SI_CA, "Chip Authentication, DH, 3DES-CBC-CBC"}, + {10, {BSI_SC, 0x03, 0x01, 0x02}, EMRTD_SI_CA, "Chip Authentication, DH, AES-CMAC-128"}, + {10, {BSI_SC, 0x03, 0x01, 0x03}, EMRTD_SI_CA, "Chip Authentication, DH, AES-CMAC-192"}, + {10, {BSI_SC, 0x03, 0x01, 0x04}, EMRTD_SI_CA, "Chip Authentication, DH, AES-CMAC-256"}, + {9, {BSI_SC, 0x03, 0x02}, EMRTD_SI_CA_DP, "Chip Authentication, ECDH"}, + {10, {BSI_SC, 0x03, 0x02, 0x01}, EMRTD_SI_CA, "Chip Authentication, ECDH, 3DES-CBC-CBC"}, + {10, {BSI_SC, 0x03, 0x02, 0x02}, EMRTD_SI_CA, "Chip Authentication, ECDH, AES-CMAC-128"}, + {10, {BSI_SC, 0x03, 0x02, 0x03}, EMRTD_SI_CA, "Chip Authentication, ECDH, AES-CMAC-192"}, + {10, {BSI_SC, 0x03, 0x02, 0x04}, EMRTD_SI_CA, "Chip Authentication, ECDH, AES-CMAC-256"}, + + // id-PACE. The leaves live in pacealg_table, these two are the + // PACEDomainParameterInfo arcs. + {8, {BSI_SC, 0x04}, EMRTD_SI_PACE, "PACE"}, + {9, {BSI_SC, 0x04, 0x01}, EMRTD_SI_PACE_DP, "PACE, DH domain parameters"}, + {9, {BSI_SC, 0x04, 0x02}, EMRTD_SI_PACE_DP, "PACE, ECDH domain parameters"}, + + // id-RI. 9 byte arcs are RestrictedIdentificationDomainParameterInfo. + {8, {BSI_SC, 0x05}, EMRTD_SI_RI, "Restricted Identification"}, + {9, {BSI_SC, 0x05, 0x01}, EMRTD_SI_RI_DP, "Restricted Identification, DH"}, + {10, {BSI_SC, 0x05, 0x01, 0x01}, EMRTD_SI_RI, "Restricted Identification, DH, SHA-1"}, + {10, {BSI_SC, 0x05, 0x01, 0x02}, EMRTD_SI_RI, "Restricted Identification, DH, SHA-224"}, + {10, {BSI_SC, 0x05, 0x01, 0x03}, EMRTD_SI_RI, "Restricted Identification, DH, SHA-256"}, + {10, {BSI_SC, 0x05, 0x01, 0x04}, EMRTD_SI_RI, "Restricted Identification, DH, SHA-384"}, + {10, {BSI_SC, 0x05, 0x01, 0x05}, EMRTD_SI_RI, "Restricted Identification, DH, SHA-512"}, + {9, {BSI_SC, 0x05, 0x02}, EMRTD_SI_RI_DP, "Restricted Identification, ECDH"}, + {10, {BSI_SC, 0x05, 0x02, 0x01}, EMRTD_SI_RI, "Restricted Identification, ECDH, SHA-1"}, + {10, {BSI_SC, 0x05, 0x02, 0x02}, EMRTD_SI_RI, "Restricted Identification, ECDH, SHA-224"}, + {10, {BSI_SC, 0x05, 0x02, 0x03}, EMRTD_SI_RI, "Restricted Identification, ECDH, SHA-256"}, + {10, {BSI_SC, 0x05, 0x02, 0x04}, EMRTD_SI_RI, "Restricted Identification, ECDH, SHA-384"}, + {10, {BSI_SC, 0x05, 0x02, 0x05}, EMRTD_SI_RI, "Restricted Identification, ECDH, SHA-512"}, + + {8, {BSI_SC, 0x06}, EMRTD_SI_CI, "Card Info Locator"}, + {8, {BSI_SC, 0x07}, EMRTD_SI_EIDSECURITY, "eID Security"}, + {8, {BSI_SC, 0x08}, EMRTD_SI_PT, "Privileged Terminal"}, + + {5, {ICAO_SEC}, EMRTD_SI_UNKNOWN, "ICAO MRTD security"}, + {6, {ICAO_SEC, 0x05}, EMRTD_SI_AA, "Active Authentication"}, + {6, {ICAO_SEC, 0x0D}, EMRTD_SI_EFDIR, "EF.DIR"}, + + {0, {0}, EMRTD_SI_UNKNOWN, NULL} +}; + +#undef BSI_SC +#undef ICAO_SEC + +const char *emrtd_secinfo_name(const uint8_t *oid, size_t oidlen, emrtd_si_t *kind) { + const emrtd_secinfo_t *best = NULL; + + // longest prefix wins, so an arc we did not enumerate still resolves to its + // family instead of falling through to "unknown" + for (int i = 0; secinfo_table[i].name != NULL; i++) { + const emrtd_secinfo_t *e = &secinfo_table[i]; + if ((e->oidlen > oidlen) || (memcmp(e->oid, oid, e->oidlen) != 0)) { + continue; + } + if ((best == NULL) || (e->oidlen > best->oidlen)) { + best = e; + } + } + + if (best == NULL) { + if (kind != NULL) { + *kind = EMRTD_SI_UNKNOWN; + } + return NULL; + } + + if (kind != NULL) { + *kind = best->kind; + } + return best->name; +} + +void emrtd_oid_to_str(const uint8_t *oid, size_t oidlen, char *out, size_t outlen) { + if ((out == NULL) || (outlen == 0)) { + return; + } + out[0] = 0x00; + + mbedtls_asn1_buf buf; + buf.tag = MBEDTLS_ASN1_OID; + buf.len = oidlen; + buf.p = (uint8_t *)oid; + + if (mbedtls_oid_get_numeric_string(out, outlen, &buf) < 0) { + out[0] = 0x00; + } +} + const emrtd_pacealg_t *emrtd_pace_alg_by_oid(const uint8_t *oid, size_t oidlen) { for (int i = 0; pacealg_table[i].name != NULL; i++) { if (pacealg_table[i].oidlen == oidlen && memcmp(pacealg_table[i].oid, oid, oidlen) == 0) { @@ -492,14 +628,51 @@ static bool emrtd_asn1_uint(const uint8_t *value, size_t valuelen, uint32_t *out // The PACEDomainParameterInfo protocol OIDs, TR-03110 part 3, A.1.1. // 0.4.0.127.0.7.2.2.4.1 (DH) and .2 (ECDH) +// 0.4.0.127.0.7.2.2.4, id-PACE itself +static const uint8_t oid_pace[] = {0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04}; static const uint8_t oid_pace_dh_dp[] = {0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04, 0x01}; static const uint8_t oid_pace_ecdh_dp[] = {0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02}; static void emrtd_pace_classify(emrtd_paceinfo_t *info) { info->supported = false; + // Only PACEInfo entries are candidates for the session. The rest of + // EF_CardAccess describes protocols this client does not run, say so + // instead of calling them unsupported PACE algorithms. + switch (info->kind) { + case EMRTD_SI_PACE: + break; + case EMRTD_SI_PACE_DP: + info->reason = "domain parameter definition, informational"; + return; + case EMRTD_SI_CA: + case EMRTD_SI_CA_DP: + case EMRTD_SI_PK: + info->reason = "not a PACE protocol, EAC Chip Authentication is not implemented"; + return; + case EMRTD_SI_TA: + info->reason = "not a PACE protocol, needs an EAC inspection system certificate"; + return; + case EMRTD_SI_RI: + case EMRTD_SI_RI_DP: + info->reason = "not a PACE protocol, needs an EAC sector key"; + return; + case EMRTD_SI_AA: + info->reason = "not a PACE protocol, uses the public key in EF_DG15"; + return; + case EMRTD_SI_CI: + case EMRTD_SI_EIDSECURITY: + case EMRTD_SI_PT: + case EMRTD_SI_EFDIR: + info->reason = "informational, not a PACE protocol"; + return; + case EMRTD_SI_UNKNOWN: + info->reason = "unknown protocol OID"; + return; + } + if (info->alg == NULL) { - info->reason = "unknown protocol OID"; + info->reason = "unknown PACE algorithm OID"; return; } @@ -601,43 +774,45 @@ static void emrtd_pace_parse_securityinfo(const uint8_t *seq, size_t seqlen, emr memcpy(info->oid, val, vlen); info->oidlen = vlen; info->alg = emrtd_pace_alg_by_oid(val, vlen); + info->protocol = emrtd_secinfo_name(val, vlen, &info->kind); - if (is_domain_param) { - // SEQUENCE { OID, AlgorithmIdentifier, INTEGER parameterId OPTIONAL } - if (emrtd_tlv_next(&cur, end, &tag, &val, &vlen) == false) { - return; - } - if (emrtd_tlv_next(&cur, end, &tag, &val, &vlen) && (tag == ASN1_TAG_INTEGER)) { - uint32_t v = 0; - if (emrtd_asn1_uint(val, vlen, &v) && (v <= 0xFF)) { - info->has_param = true; - info->param_id = (uint8_t)v; - info->sdp = emrtd_pace_sdp_by_id(info->param_id); - } - } - info->supported = false; - info->reason = "domain parameter definition, informational"; - out->count++; - return; + if (info->alg != NULL) { + // a PACEInfo we can act on, pacealg_table has the better name + info->kind = EMRTD_SI_PACE; + info->protocol = info->alg->name; + } else if (is_domain_param) { + info->kind = EMRTD_SI_PACE_DP; + } else if ((vlen > sizeof(oid_pace)) && (memcmp(val, oid_pace, sizeof(oid_pace)) == 0)) { + // an id-PACE arc we do not implement, still a PACEInfo and not a + // domain parameter definition + info->kind = EMRTD_SI_PACE; } - // PACEInfo ::= SEQUENCE { protocol OID, version INTEGER, parameterId INTEGER OPTIONAL } - if (emrtd_tlv_next(&cur, end, &tag, &val, &vlen) == false) { - return; - } - if (tag != ASN1_TAG_INTEGER) { - return; - } - if (emrtd_asn1_uint(val, vlen, &info->version) == false) { - return; + // Second element: + // PACEInfo / ChipAuthenticationInfo / TerminalAuthenticationInfo version INTEGER + // PACEDomainParameterInfo / ChipAuthenticationPublicKeyInfo an AlgorithmIdentifier + // or SubjectPublicKeyInfo + // Take it as the version only when it really is an INTEGER. + if (emrtd_tlv_next(&cur, end, &tag, &val, &vlen) && (tag == ASN1_TAG_INTEGER)) { + emrtd_asn1_uint(val, vlen, &info->version); } + // Third element, an optional INTEGER. For the PACE entries that is the + // standardized domain parameter id of table A.2, for the Chip + // Authentication ones it is a keyId and must not be read as a curve. if (emrtd_tlv_next(&cur, end, &tag, &val, &vlen) && (tag == ASN1_TAG_INTEGER)) { uint32_t v = 0; - if (emrtd_asn1_uint(val, vlen, &v) && (v <= 0xFF)) { - info->has_param = true; - info->param_id = (uint8_t)v; - info->sdp = emrtd_pace_sdp_by_id(info->param_id); + if (emrtd_asn1_uint(val, vlen, &v)) { + if ((info->kind == EMRTD_SI_PACE) || (info->kind == EMRTD_SI_PACE_DP)) { + if (v <= 0xFF) { + info->has_param = true; + info->param_id = (uint8_t)v; + info->sdp = emrtd_pace_sdp_by_id(info->param_id); + } + } else { + info->has_key_id = true; + info->key_id = v; + } } } diff --git a/client/src/emrtd/emrtd_pace.h b/client/src/emrtd/emrtd_pace.h index e42f8189d..5e76ec7be 100644 --- a/client/src/emrtd/emrtd_pace.h +++ b/client/src/emrtd/emrtd_pace.h @@ -57,6 +57,27 @@ typedef enum { EMRTD_PACE_CIPHER_AES256, } emrtd_pace_cipher_t; +// EF_CardAccess is a SET OF SecurityInfo, not a list of PACE algorithms. A +// document announces everything it can do in there, so Chip Authentication and +// Terminal Authentication share the file with the PACE entries. Only +// EMRTD_SI_PACE entries are candidates for the session we are about to open. +typedef enum { + EMRTD_SI_UNKNOWN = 0, + EMRTD_SI_PACE, // PACEInfo + EMRTD_SI_PACE_DP, // PACEDomainParameterInfo + EMRTD_SI_CA, // ChipAuthenticationInfo + EMRTD_SI_CA_DP, // ChipAuthenticationDomainParameterInfo + EMRTD_SI_PK, // ChipAuthenticationPublicKeyInfo + EMRTD_SI_TA, // TerminalAuthenticationInfo + EMRTD_SI_RI, // RestrictedIdentificationInfo + EMRTD_SI_RI_DP, // RestrictedIdentificationDomainParameterInfo + EMRTD_SI_CI, // CardInfoLocator + EMRTD_SI_EIDSECURITY, // eIDSecurityInfo + EMRTD_SI_PT, // PrivilegedTerminalInfo + EMRTD_SI_AA, // ActiveAuthenticationInfo + EMRTD_SI_EFDIR, // EFDIRInfo +} emrtd_si_t; + typedef struct emrtd_pacealg_s { const char *name; emrtd_pace_ka_t ka; @@ -79,12 +100,16 @@ typedef struct emrtd_pacesdp_s { // One parsed SecurityInfo out of EF_CardAccess typedef struct { - const emrtd_pacealg_t *alg; // NULL when the OID is unknown to us + const emrtd_pacealg_t *alg; // NULL unless this is a PACEInfo we know + emrtd_si_t kind; // which SecurityInfo this is + const char *protocol; // pretty name, NULL when the OID is unknown uint8_t oid[EMRTD_PACE_OID_MAXLEN]; size_t oidlen; uint32_t version; bool has_param; uint8_t param_id; + bool has_key_id; // ChipAuthentication* keyId, not a PACE thing + uint32_t key_id; const emrtd_pacesdp_t *sdp; // NULL when param_id is unknown/absent bool supported; const char *reason; // why not supported, NULL when it is @@ -151,6 +176,11 @@ int emrtd_retail_mac(const uint8_t *key, const uint8_t *input, size_t inputlen, // Table lookups //----------------------------------------------------------------------------- const emrtd_pacealg_t *emrtd_pace_alg_by_oid(const uint8_t *oid, size_t oidlen); +// Names any SecurityInfo protocol OID, PACE or not. Longest prefix wins, so an +// arc we did not enumerate still resolves to its family. NULL when unknown. +const char *emrtd_secinfo_name(const uint8_t *oid, size_t oidlen, emrtd_si_t *kind); +// Dotted decimal form of a BER encoded OID body, for reporting +void emrtd_oid_to_str(const uint8_t *oid, size_t oidlen, char *out, size_t outlen); const emrtd_pacesdp_t *emrtd_pace_sdp_by_id(uint8_t id); const char *emrtd_pace_cipher_name(emrtd_pace_cipher_t cipher); diff --git a/client/src/emrtd/emrtd_pacetest.c b/client/src/emrtd/emrtd_pacetest.c index 463a790b9..d0646601d 100644 --- a/client/src/emrtd/emrtd_pacetest.c +++ b/client/src/emrtd/emrtd_pacetest.c @@ -547,6 +547,67 @@ static bool test_cardaccess(void) { return res; } +static bool test_cardaccess_secinfo(void) { + // EF_CardAccess is a SET OF SecurityInfo, not a list of PACE algorithms. + // This is the shape a document with EAC has: + // PACEInfo ECDH-GM-AES-CMAC-128, v2, BrainpoolP256r1 + // TerminalAuthenticationInfo id-TA, v1 + // ChipAuthenticationInfo id-CA-ECDH-AES-CBC-CMAC-128, v1, keyId 16 + const char *blob = + "3137" + "3012" "060A04007F0007020204020202010202010D" + "300D" "060804007F0007020202" "020101" + "3012" "060A04007F00070202030202" "020101" "020110"; + + uint8_t data[128] = { 0x00 }; + size_t datalen = unhex(blob, data, sizeof(data)); + + emrtd_cardaccess_t ca; + bool res = (datalen > 0); + res = res && (emrtd_pace_parse_cardaccess(data, datalen, &ca) == PM3_SUCCESS); + // the non-PACE entries must survive parsing, they used to be dropped or + // reported as unknown PACE algorithms + res = res && (ca.count == 3); + res = res && (ca.best == 0); + + res = res && (ca.infos[0].kind == EMRTD_SI_PACE) && (ca.infos[0].supported); + res = res && (ca.infos[0].has_param) && (ca.infos[0].param_id == 13); + + res = res && (ca.infos[1].kind == EMRTD_SI_TA); + res = res && (ca.infos[1].alg == NULL) && (ca.infos[1].supported == false); + res = res && (ca.infos[1].version == 1); + res = res && (ca.infos[1].protocol != NULL) && + (strcmp(ca.infos[1].protocol, "Terminal Authentication") == 0); + + res = res && (ca.infos[2].kind == EMRTD_SI_CA); + res = res && (ca.infos[2].alg == NULL) && (ca.infos[2].supported == false); + res = res && (ca.infos[2].version == 1); + res = res && (ca.infos[2].protocol != NULL) && + (strcmp(ca.infos[2].protocol, "Chip Authentication, ECDH, AES-CMAC-128") == 0); + // a ChipAuthenticationInfo keyId is not a standardized domain parameter id + res = res && (ca.infos[2].has_param == false); + res = res && (ca.infos[2].has_key_id) && (ca.infos[2].key_id == 16); + res = res && (ca.infos[2].sdp == NULL); + + // an id-TA arc we did not enumerate still resolves to its family + const uint8_t ta_odd[] = {0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x02, 0x02, 0x7F}; + emrtd_si_t kind = EMRTD_SI_UNKNOWN; + const char *name = emrtd_secinfo_name(ta_odd, sizeof(ta_odd), &kind); + res = res && (kind == EMRTD_SI_TA) && (name != NULL); + + // and something outside both roots must stay unknown + const uint8_t bogus[] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D}; + res = res && (emrtd_secinfo_name(bogus, sizeof(bogus), &kind) == NULL); + res = res && (kind == EMRTD_SI_UNKNOWN); + + char oidstr[64] = { 0x00 }; + emrtd_oid_to_str(ca.infos[1].oid, ca.infos[1].oidlen, oidstr, sizeof(oidstr)); + res = res && (strcmp(oidstr, "0.4.0.127.0.7.2.2.2") == 0); + + PrintAndLogEx(SUCCESS, "SecurityInfos.... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail")); + return res; +} + // Chip Authentication Mapping. // // The chip sends t = SK.Map.IC * SK.CA.IC^-1 mod n encrypted with KSenc, so @@ -667,6 +728,7 @@ bool emrtd_test(bool verbose) { res = test_kdf() && res; res = test_nonce() && res; res = test_cardaccess() && res; + res = test_cardaccess_secinfo() && res; res = test_gm() && res; res = test_ec_keygen() && res; res = test_token() && res;