2019-03-05 06:11:31 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 2019 Merlok
|
|
|
|
//
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// NFC Data Exchange Format (NDEF) functions
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "ndef.h"
|
2019-08-08 22:57:33 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-05 06:48:02 +08:00
|
|
|
#include "ui.h"
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "util.h" // sprint_hex...
|
2019-03-05 22:41:20 +08:00
|
|
|
#include "emv/dump.h"
|
2019-03-06 00:14:30 +08:00
|
|
|
#include "crypto/asn1utils.h"
|
2020-03-31 21:47:33 +08:00
|
|
|
#include "pm3_cmd.h"
|
2019-03-05 22:41:20 +08:00
|
|
|
|
|
|
|
#define STRBOOL(p) ((p) ? "+" : "-")
|
|
|
|
|
|
|
|
static const char *TypeNameFormat_s[] = {
|
2019-03-10 06:35:06 +08:00
|
|
|
"Empty Record",
|
|
|
|
"Well Known Record",
|
|
|
|
"MIME Media Record",
|
|
|
|
"Absolute URI Record",
|
|
|
|
"External Record",
|
|
|
|
"Unknown Record",
|
|
|
|
"Unchanged Record",
|
|
|
|
"n/a"
|
2019-03-05 22:41:20 +08:00
|
|
|
};
|
2019-03-05 06:48:02 +08:00
|
|
|
|
2019-03-06 01:41:50 +08:00
|
|
|
static const char *ndefSigType_s[] = {
|
2019-03-10 06:35:06 +08:00
|
|
|
"Not present", // No signature present
|
2020-03-31 14:06:45 +08:00
|
|
|
"RSASSA_PSS_SHA_1 (1024)", // PKCS_1
|
|
|
|
"RSASSA_PKCS1_v1_5_WITH_SHA_1 (1024)", // PKCS_1
|
|
|
|
"DSA-1024",
|
|
|
|
"ECDSA-P192",
|
|
|
|
"RSASSA-PSS-2048",
|
|
|
|
"RSASSA-PKCS1-v1_5-2048",
|
|
|
|
"DSA-2048",
|
|
|
|
"ECDSA-P224",
|
|
|
|
"ECDSA-K233",
|
|
|
|
"ECDSA-B233",
|
|
|
|
"ECDSA-P256",
|
2019-03-10 06:35:06 +08:00
|
|
|
"n/a"
|
2020-03-31 14:06:45 +08:00
|
|
|
|
2019-03-06 01:41:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *ndefCertificateFormat_s[] = {
|
2019-03-10 06:35:06 +08:00
|
|
|
"X_509",
|
2020-03-31 14:06:45 +08:00
|
|
|
"X9_68 (M2M)",
|
2019-03-10 06:35:06 +08:00
|
|
|
"n/a"
|
2019-03-06 01:41:50 +08:00
|
|
|
};
|
|
|
|
|
2019-03-05 23:50:51 +08:00
|
|
|
static const char *URI_s[] = {
|
2019-03-10 06:35:06 +08:00
|
|
|
"", // 0x00
|
|
|
|
"http://www.", // 0x01
|
|
|
|
"https://www.", // 0x02
|
|
|
|
"http://", // 0x03
|
|
|
|
"https://", // 0x04
|
|
|
|
"tel:", // 0x05
|
|
|
|
"mailto:", // 0x06
|
|
|
|
"ftp://anonymous:anonymous@", // 0x07
|
|
|
|
"ftp://ftp.", // 0x08
|
|
|
|
"ftps://", // 0x09
|
|
|
|
"sftp://", // 0x0A
|
|
|
|
"smb://", // 0x0B
|
|
|
|
"nfs://", // 0x0C
|
|
|
|
"ftp://", // 0x0D
|
|
|
|
"dav://", // 0x0E
|
|
|
|
"news:", // 0x0F
|
|
|
|
"telnet://", // 0x10
|
|
|
|
"imap:", // 0x11
|
|
|
|
"rtsp://", // 0x12
|
|
|
|
"urn:", // 0x13
|
|
|
|
"pop:", // 0x14
|
|
|
|
"sip:", // 0x15
|
|
|
|
"sips:", // 0x16
|
|
|
|
"tftp:", // 0x17
|
|
|
|
"btspp://", // 0x18
|
|
|
|
"btl2cap://", // 0x19
|
|
|
|
"btgoep://", // 0x1A
|
|
|
|
"tcpobex://", // 0x1B
|
|
|
|
"irdaobex://", // 0x1C
|
|
|
|
"file://", // 0x1D
|
|
|
|
"urn:epc:id:", // 0x1E
|
|
|
|
"urn:epc:tag:", // 0x1F
|
|
|
|
"urn:epc:pat:", // 0x20
|
|
|
|
"urn:epc:raw:", // 0x21
|
|
|
|
"urn:epc:", // 0x22
|
|
|
|
"urn:nfc:" // 0x23
|
2019-03-05 23:50:51 +08:00
|
|
|
};
|
|
|
|
|
2019-04-14 03:54:04 +08:00
|
|
|
static uint16_t ndefTLVGetLength(uint8_t *data, size_t *indx) {
|
2019-03-10 06:35:06 +08:00
|
|
|
uint16_t len = 0;
|
|
|
|
if (data[0] == 0xff) {
|
|
|
|
len = (data[1] << 8) + data[2];
|
|
|
|
*indx += 3;
|
|
|
|
} else {
|
|
|
|
len = data[0];
|
|
|
|
*indx += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
2019-03-05 06:48:02 +08:00
|
|
|
}
|
2019-03-05 06:11:31 +08:00
|
|
|
|
2019-04-14 03:54:04 +08:00
|
|
|
static int ndefDecodeHeader(uint8_t *data, size_t datalen, NDEFHeader_t *header) {
|
2019-03-10 06:35:06 +08:00
|
|
|
header->Type = NULL;
|
|
|
|
header->Payload = NULL;
|
|
|
|
header->ID = NULL;
|
2019-03-05 23:25:11 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
header->MessageBegin = data[0] & 0x80;
|
|
|
|
header->MessageEnd = data[0] & 0x40;
|
|
|
|
header->ChunkFlag = data[0] & 0x20;
|
|
|
|
header->ShortRecordBit = data[0] & 0x10;
|
|
|
|
header->IDLenPresent = data[0] & 0x08;
|
|
|
|
header->TypeNameFormat = data[0] & 0x07;
|
|
|
|
header->len = 1 + 1 + (header->ShortRecordBit ? 1 : 4) + (header->IDLenPresent ? 1 : 0); // header + typelen + payloadlen + idlen
|
|
|
|
if (header->len > datalen)
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
header->TypeLen = data[1];
|
|
|
|
header->Type = data + header->len;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
header->PayloadLen = (header->ShortRecordBit ? (data[2]) : ((data[2] << 24) + (data[3] << 16) + (data[4] << 8) + data[5]));
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (header->IDLenPresent) {
|
|
|
|
header->IDLen = (header->ShortRecordBit ? (data[3]) : (data[6]));
|
|
|
|
} else {
|
|
|
|
header->IDLen = 0;
|
|
|
|
}
|
2019-03-05 23:25:11 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
header->Payload = header->Type + header->TypeLen + header->IDLen;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
header->RecLen = header->len + header->TypeLen + header->PayloadLen + header->IDLen;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (header->RecLen > datalen)
|
|
|
|
return 3;
|
2019-03-05 22:41:20 +08:00
|
|
|
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 22:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-04-14 03:54:04 +08:00
|
|
|
static int ndefPrintHeader(NDEFHeader_t *header) {
|
2019-03-10 06:35:06 +08:00
|
|
|
PrintAndLogEx(INFO, "Header:");
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tMessage Begin: %s", STRBOOL(header->MessageBegin));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tMessage End: %s", STRBOOL(header->MessageEnd));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tChunk Flag: %s", STRBOOL(header->ChunkFlag));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tShort Record Bit: %s", STRBOOL(header->ShortRecordBit));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tID Len Present: %s", STRBOOL(header->IDLenPresent));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tType Name Format: [0x%02x] %s", header->TypeNameFormat, TypeNameFormat_s[header->TypeNameFormat]);
|
|
|
|
|
|
|
|
PrintAndLogEx(SUCCESS, "\tHeader length : %zu", header->len);
|
|
|
|
PrintAndLogEx(SUCCESS, "\tType length : %zu", header->TypeLen);
|
|
|
|
PrintAndLogEx(SUCCESS, "\tPayload length : %zu", header->PayloadLen);
|
|
|
|
PrintAndLogEx(SUCCESS, "\tID length : %zu", header->IDLen);
|
|
|
|
PrintAndLogEx(SUCCESS, "\tRecord length : %zu", header->RecLen);
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 22:41:20 +08:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:09:41 +08:00
|
|
|
static int ndefDecodeSig1(uint8_t *sig, size_t siglen) {
|
|
|
|
size_t indx = 1;
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
uint8_t sigType = sig[indx] & 0x7f;
|
|
|
|
bool sigURI = sig[indx] & 0x80;
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature type: %s", ((sigType < stNA) ? ndefSigType_s[sigType] : ndefSigType_s[stNA]));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature uri: %s", (sigURI ? "present" : "not present"));
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
size_t intsiglen = (sig[indx + 1] << 8) + sig[indx + 2];
|
|
|
|
// ecdsa 0x04
|
2020-04-01 00:26:09 +08:00
|
|
|
if (sigType == stECDSA_P192 || sigType == stECDSA_P256) {
|
2019-03-10 06:35:06 +08:00
|
|
|
indx += 3;
|
2020-04-01 00:26:09 +08:00
|
|
|
int slen = 24;
|
|
|
|
if (sigType == stECDSA_P256)
|
|
|
|
slen = 32;
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature [%zu]: %s", intsiglen, sprint_hex_inrow(&sig[indx], intsiglen));
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
uint8_t rval[300] = {0};
|
|
|
|
uint8_t sval[300] = {0};
|
|
|
|
int res = ecdsa_asn1_get_signature(&sig[indx], intsiglen, rval, sval);
|
|
|
|
if (!res) {
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\t\tr: %s", sprint_hex(rval + 32 - slen, slen));
|
|
|
|
PrintAndLogEx(SUCCESS, "\t\ts: %s", sprint_hex(sval + 32 - slen, slen));
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
indx += intsiglen;
|
|
|
|
|
|
|
|
if (sigURI) {
|
|
|
|
size_t intsigurilen = (sig[indx] << 8) + sig[indx + 1];
|
|
|
|
indx += 2;
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature uri [%zu]: %.*s", intsigurilen, (int)intsigurilen, &sig[indx]);
|
2019-03-10 06:35:06 +08:00
|
|
|
indx += intsigurilen;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t certFormat = (sig[indx] >> 4) & 0x07;
|
|
|
|
uint8_t certCount = sig[indx] & 0x0f;
|
|
|
|
bool certURI = sig[indx] & 0x80;
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificate format: %s", ((certFormat < sfNA) ? ndefCertificateFormat_s[certFormat] : ndefCertificateFormat_s[sfNA]));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificates count: %d", certCount);
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
// print certificates
|
|
|
|
indx++;
|
|
|
|
for (int i = 0; i < certCount; i++) {
|
|
|
|
size_t intcertlen = (sig[indx + 1] << 8) + sig[indx + 2];
|
|
|
|
indx += 2;
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificate %d [%zu]: %s", i + 1, intcertlen, sprint_hex_inrow(&sig[indx], intcertlen));
|
2019-03-10 06:35:06 +08:00
|
|
|
indx += intcertlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// have certificate uri
|
|
|
|
if ((indx <= siglen) && certURI) {
|
|
|
|
size_t inturilen = (sig[indx] << 8) + sig[indx + 1];
|
|
|
|
indx += 2;
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificate uri [%zu]: %.*s", inturilen, (int)inturilen, &sig[indx]);
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 23:50:51 +08:00
|
|
|
};
|
|
|
|
|
2020-03-31 23:09:41 +08:00
|
|
|
// https://github.com/nfcpy/ndeflib/blob/master/src/ndef/signature.py#L292
|
|
|
|
static int ndefDecodeSig2(uint8_t *sig, size_t siglen) {
|
|
|
|
size_t indx = 1;
|
|
|
|
|
|
|
|
uint8_t sigType = sig[indx] & 0x7f;
|
|
|
|
bool sigURI = sig[indx] & 0x80;
|
|
|
|
indx++;
|
|
|
|
|
|
|
|
uint8_t hashType = sig[indx];
|
|
|
|
indx++;
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature type :\t" _GREEN_("%s"), ((sigType < stNA) ? ndefSigType_s[sigType] : ndefSigType_s[stNA]));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature uri :\t\t%s", (sigURI ? "present" : "not present"));
|
|
|
|
PrintAndLogEx(SUCCESS, "\thash type :\t\t%s", ((hashType == 0x02) ? _GREEN_("SHA-256") : _RED_("unknown")));
|
2020-03-31 23:09:41 +08:00
|
|
|
|
2020-03-31 23:25:17 +08:00
|
|
|
size_t intsiglen = (sig[indx] << 8) + sig[indx + 1];
|
|
|
|
indx += 2;
|
2020-04-02 15:34:52 +08:00
|
|
|
|
2020-03-31 23:09:41 +08:00
|
|
|
if (sigURI) {
|
|
|
|
indx += 2;
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature uri [%zu]: %.*s", intsiglen, (int)intsiglen, &sig[indx]);
|
2020-03-31 23:25:17 +08:00
|
|
|
indx += intsiglen;
|
|
|
|
} else {
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature [%zu]: %s", intsiglen, sprint_hex_inrow(&sig[indx], intsiglen));
|
2020-03-31 23:25:17 +08:00
|
|
|
if (sigType == stECDSA_P192 || sigType == stECDSA_P256) {
|
2020-03-31 23:30:44 +08:00
|
|
|
int slen = intsiglen / 2;
|
|
|
|
if (slen == 24 || slen == 32) {
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature : " _GREEN_("ECDSA-%d"), slen * 8);
|
|
|
|
PrintAndLogEx(SUCCESS, "\t\tr: %s", sprint_hex(&sig[indx], slen));
|
|
|
|
PrintAndLogEx(SUCCESS, "\t\ts: %s", sprint_hex(&sig[indx + slen], slen));
|
2020-03-31 23:25:17 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(INFO, "\tsignature: unknown type");
|
2020-03-31 23:09:41 +08:00
|
|
|
}
|
2020-03-31 23:25:17 +08:00
|
|
|
indx += intsiglen;
|
2020-03-31 23:09:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t certFormat = (sig[indx] >> 4) & 0x07;
|
|
|
|
uint8_t certCount = sig[indx] & 0x0f;
|
|
|
|
bool certURI = sig[indx] & 0x80;
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificate format : " _GREEN_("%s"), ((certFormat < sfNA) ? ndefCertificateFormat_s[certFormat] : ndefCertificateFormat_s[sfNA]));
|
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificates count : %d", certCount);
|
2020-03-31 23:09:41 +08:00
|
|
|
|
|
|
|
// print certificates
|
|
|
|
indx++;
|
|
|
|
for (int i = 0; i < certCount; i++) {
|
|
|
|
size_t intcertlen = (sig[indx + 1] << 8) + sig[indx + 2];
|
|
|
|
indx += 2;
|
|
|
|
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificate %d [%zu]: %s", i + 1, intcertlen, sprint_hex_inrow(&sig[indx], intcertlen));
|
2020-03-31 23:09:41 +08:00
|
|
|
indx += intcertlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// have certificate uri
|
|
|
|
if ((indx <= siglen) && certURI) {
|
|
|
|
size_t inturilen = (sig[indx] << 8) + sig[indx + 1];
|
|
|
|
indx += 2;
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tcertificate uri [%zu]: %.*s", inturilen, (int)inturilen, &sig[indx]);
|
2020-03-31 23:09:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return PM3_SUCCESS;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ndefDecodeSig(uint8_t *sig, size_t siglen) {
|
2020-04-02 15:34:52 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "\tsignature version : \t" _GREEN_("0x%02x"), sig[0]);
|
2020-03-31 23:09:41 +08:00
|
|
|
if (sig[0] != 0x01 && sig[0] != 0x20) {
|
|
|
|
PrintAndLogEx(ERR, "signature version unknown.");
|
|
|
|
return PM3_ESOFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig[0] == 0x01)
|
|
|
|
return ndefDecodeSig1(sig, siglen);
|
|
|
|
|
|
|
|
if (sig[0] == 0x20)
|
|
|
|
return ndefDecodeSig2(sig, siglen);
|
|
|
|
|
|
|
|
return PM3_ESOFT;
|
|
|
|
}
|
|
|
|
|
2019-04-14 03:54:04 +08:00
|
|
|
static int ndefDecodePayload(NDEFHeader_t *ndef) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
switch (ndef->TypeNameFormat) {
|
|
|
|
case tnfWellKnownRecord:
|
|
|
|
PrintAndLogEx(INFO, "Well Known Record");
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "\ttype : %.*s", (int)ndef->TypeLen, ndef->Type);
|
2019-03-10 06:35:06 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!strncmp((char *)ndef->Type, "T", ndef->TypeLen)) {
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "\ttext : " _GREEN_("%.*s"), (int)ndef->PayloadLen, ndef->Payload);
|
2019-03-10 07:00:59 +08:00
|
|
|
}
|
2019-03-10 06:35:06 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!strncmp((char *)ndef->Type, "U", ndef->TypeLen)) {
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO
|
|
|
|
, "\turi : " _GREEN_("%s%.*s")
|
2019-04-30 04:41:28 +08:00
|
|
|
, (ndef->Payload[0] <= 0x23 ? URI_s[ndef->Payload[0]] : "[err]")
|
2019-10-06 05:56:19 +08:00
|
|
|
, (int)(ndef->PayloadLen - 1)
|
2019-04-30 04:41:28 +08:00
|
|
|
, &ndef->Payload[1]
|
|
|
|
);
|
2019-03-10 07:00:59 +08:00
|
|
|
}
|
2019-03-10 06:35:06 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!strncmp((char *)ndef->Type, "Sig", ndef->TypeLen)) {
|
|
|
|
ndefDecodeSig(ndef->Payload, ndef->PayloadLen);
|
|
|
|
}
|
2019-03-10 06:35:06 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
break;
|
|
|
|
case tnfAbsoluteURIRecord:
|
|
|
|
PrintAndLogEx(INFO, "Absolute URI Record");
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "\ttype : %.*s", (int)ndef->TypeLen, ndef->Type);
|
|
|
|
PrintAndLogEx(INFO, "\tpayload : %.*s", (int)ndef->PayloadLen, ndef->Payload);
|
2019-03-10 07:00:59 +08:00
|
|
|
break;
|
2019-04-14 04:46:08 +08:00
|
|
|
case tnfEmptyRecord:
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "Empty Record");
|
|
|
|
PrintAndLogEx(INFO, "\t -to be impl-");
|
|
|
|
break;
|
2019-04-14 04:46:08 +08:00
|
|
|
case tnfMIMEMediaRecord:
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "MIME Media Record");
|
|
|
|
PrintAndLogEx(INFO, "\t -to be impl-");
|
|
|
|
break;
|
2019-04-14 04:46:08 +08:00
|
|
|
case tnfExternalRecord:
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "External Record");
|
|
|
|
PrintAndLogEx(INFO, "\t -to be impl-");
|
|
|
|
break;
|
2019-04-14 04:46:08 +08:00
|
|
|
case tnfUnchangedRecord:
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "Unchanged Record");
|
|
|
|
PrintAndLogEx(INFO, "\t -to be impl-");
|
|
|
|
break;
|
2019-04-14 04:46:08 +08:00
|
|
|
case tnfUnknownRecord:
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "Unknown Record");
|
|
|
|
PrintAndLogEx(INFO, "\t -to be impl-");
|
2019-03-10 07:00:59 +08:00
|
|
|
break;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 23:25:11 +08:00
|
|
|
}
|
|
|
|
|
2019-04-14 03:54:04 +08:00
|
|
|
static int ndefRecordDecodeAndPrint(uint8_t *ndefRecord, size_t ndefRecordLen) {
|
2019-03-10 06:35:06 +08:00
|
|
|
NDEFHeader_t NDEFHeader = {0};
|
|
|
|
int res = ndefDecodeHeader(ndefRecord, ndefRecordLen, &NDEFHeader);
|
2020-03-31 21:47:33 +08:00
|
|
|
if (res != PM3_SUCCESS)
|
2019-03-10 06:35:06 +08:00
|
|
|
return res;
|
|
|
|
|
|
|
|
ndefPrintHeader(&NDEFHeader);
|
|
|
|
|
|
|
|
if (NDEFHeader.TypeLen) {
|
|
|
|
PrintAndLogEx(INFO, "Type data:");
|
|
|
|
dump_buffer(NDEFHeader.Type, NDEFHeader.TypeLen, stdout, 1);
|
|
|
|
}
|
|
|
|
if (NDEFHeader.IDLen) {
|
|
|
|
PrintAndLogEx(INFO, "ID data:");
|
|
|
|
dump_buffer(NDEFHeader.ID, NDEFHeader.IDLen, stdout, 1);
|
|
|
|
}
|
|
|
|
if (NDEFHeader.PayloadLen) {
|
|
|
|
PrintAndLogEx(INFO, "Payload data:");
|
|
|
|
dump_buffer(NDEFHeader.Payload, NDEFHeader.PayloadLen, stdout, 1);
|
|
|
|
if (NDEFHeader.TypeLen)
|
|
|
|
ndefDecodePayload(&NDEFHeader);
|
|
|
|
}
|
|
|
|
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 22:41:20 +08:00
|
|
|
}
|
|
|
|
|
2020-03-31 21:47:33 +08:00
|
|
|
int NDEFRecordsDecodeAndPrint(uint8_t *ndefRecord, size_t ndefRecordLen) {
|
2019-03-10 06:35:06 +08:00
|
|
|
bool firstRec = true;
|
|
|
|
size_t len = 0;
|
2020-04-01 15:49:11 +08:00
|
|
|
size_t counter = 0;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
while (len < ndefRecordLen) {
|
2020-04-01 15:49:11 +08:00
|
|
|
counter++;
|
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
NDEFHeader_t NDEFHeader = {0};
|
|
|
|
int res = ndefDecodeHeader(&ndefRecord[len], ndefRecordLen - len, &NDEFHeader);
|
2020-03-31 21:47:33 +08:00
|
|
|
if (res != PM3_SUCCESS)
|
2019-03-10 06:35:06 +08:00
|
|
|
return res;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (firstRec) {
|
|
|
|
if (!NDEFHeader.MessageBegin) {
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(ERR, "NDEF first record have MessageBegin = false!");
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
firstRec = false;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (NDEFHeader.MessageEnd && len + NDEFHeader.RecLen != ndefRecordLen) {
|
2019-10-06 05:56:19 +08:00
|
|
|
PrintAndLogEx(ERR, "NDEF records have wrong length. Must be %zu, calculated %zu", ndefRecordLen, len + NDEFHeader.RecLen);
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(NORMAL, "");
|
|
|
|
PrintAndLogEx(SUCCESS, "Record " _YELLOW_("%zu"), counter);
|
|
|
|
PrintAndLogEx(INFO, "-----------------------------------------------------");
|
2019-03-10 06:35:06 +08:00
|
|
|
ndefRecordDecodeAndPrint(&ndefRecord[len], NDEFHeader.RecLen);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
len += NDEFHeader.RecLen;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (NDEFHeader.MessageEnd)
|
|
|
|
break;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 06:51:32 +08:00
|
|
|
}
|
|
|
|
|
2020-04-02 23:42:44 +08:00
|
|
|
// http://apps4android.org/nfc-specifications/NFCForum-TS-Type-2-Tag_1.1.pdf
|
2019-03-10 18:20:22 +08:00
|
|
|
int NDEFDecodeAndPrint(uint8_t *ndef, size_t ndefLen, bool verbose) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
size_t indx = 0;
|
|
|
|
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(INFO, "");
|
|
|
|
PrintAndLogEx(INFO, "NDEF parsing");
|
2019-03-10 06:35:06 +08:00
|
|
|
while (indx < ndefLen) {
|
2020-04-01 15:49:11 +08:00
|
|
|
|
|
|
|
PrintAndLogEx(INFO, "-----------------------------------------------------");
|
2019-03-10 06:35:06 +08:00
|
|
|
switch (ndef[indx]) {
|
|
|
|
case 0x00: {
|
|
|
|
indx++;
|
|
|
|
uint16_t len = ndefTLVGetLength(&ndef[indx], &indx);
|
2020-04-01 15:49:11 +08:00
|
|
|
|
|
|
|
PrintAndLogEx(SUCCESS, "-- NDEF NULL block.");
|
2019-03-10 06:35:06 +08:00
|
|
|
if (len)
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(WARNING, "NDEF NULL block size must be 0, got %d bytes", len);
|
2019-03-10 06:35:06 +08:00
|
|
|
indx += len;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-02 23:42:44 +08:00
|
|
|
case 0x01: {
|
|
|
|
indx++;
|
|
|
|
uint16_t len = ndefTLVGetLength(&ndef[indx], &indx);
|
|
|
|
PrintAndLogEx(INFO, "-- NDEF Lock Control.");
|
|
|
|
if (len != 3)
|
|
|
|
PrintAndLogEx(WARNING, "NDEF Lock Control block size must be 3 instead of %d.", len);
|
|
|
|
indx += len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0x02: {
|
|
|
|
indx++;
|
|
|
|
uint16_t len = ndefTLVGetLength(&ndef[indx], &indx);
|
|
|
|
PrintAndLogEx(INFO, "-- NDEF Memory Control.");
|
|
|
|
if (len != 3)
|
|
|
|
PrintAndLogEx(WARNING, "NDEF Memory Control block size must be 3 instead of %d.", len);
|
|
|
|
indx += len;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-10 06:35:06 +08:00
|
|
|
case 0x03: {
|
|
|
|
indx++;
|
|
|
|
uint16_t len = ndefTLVGetLength(&ndef[indx], &indx);
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "Found NDEF message (%d bytes)", len);
|
2019-03-10 06:35:06 +08:00
|
|
|
|
2020-03-31 21:47:33 +08:00
|
|
|
int res = NDEFRecordsDecodeAndPrint(&ndef[indx], len);
|
|
|
|
if (res != PM3_SUCCESS)
|
2019-03-10 06:35:06 +08:00
|
|
|
return res;
|
|
|
|
|
|
|
|
indx += len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0xfd: {
|
|
|
|
indx++;
|
|
|
|
uint16_t len = ndefTLVGetLength(&ndef[indx], &indx);
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "-- NDEF proprietary info. Skipped %d bytes.", len);
|
2019-03-10 06:35:06 +08:00
|
|
|
indx += len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0xfe: {
|
2020-04-01 15:49:11 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "-- NDEF Terminator. Done.");
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
PrintAndLogEx(ERR, "unknown tag 0x%02x", ndef[indx]);
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 21:47:33 +08:00
|
|
|
return PM3_SUCCESS;
|
2019-03-05 06:11:31 +08:00
|
|
|
}
|