mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-22 14:13:42 -07:00
added crypto polarssl with sda test
This commit is contained in:
parent
00bb9106df
commit
9b09e34eb7
5 changed files with 808 additions and 0 deletions
|
@ -121,6 +121,7 @@ CMDSRCS = crapto1/crapto1.c\
|
|||
emv/emv_tags.c\
|
||||
emv/dol.c\
|
||||
emv/emvcore.c\
|
||||
emv/sda_test.c\
|
||||
emv/cmdemv.c\
|
||||
cmdhf.c \
|
||||
cmdhf14a.c \
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "cmdemv.h"
|
||||
#include "sda_test.h"
|
||||
|
||||
int UsageCmdHFEMVSelect(void) {
|
||||
PrintAndLog("HELP : Executes select applet command:\n");
|
||||
|
@ -780,6 +781,18 @@ int CmdHFEMVExec(const char *cmd) {
|
|||
}
|
||||
|
||||
int CmdHFEMVTest(const char *cmd) {
|
||||
int res;
|
||||
bool TestFail = false;
|
||||
|
||||
res = exec_sda_test();
|
||||
if (res) TestFail = true;
|
||||
|
||||
PrintAndLog("--------------------------");
|
||||
|
||||
if (TestFail)
|
||||
PrintAndLog("One of tests is FAILED.");
|
||||
else
|
||||
PrintAndLog("Tests is PASSED.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
507
client/emv/crypto_polarssl.c
Normal file
507
client/emv/crypto_polarssl.c
Normal file
|
@ -0,0 +1,507 @@
|
|||
/*
|
||||
* libopenemv - a library to work with EMV family of smart cards
|
||||
* Copyright (C) 2015 Dmitry Eremin-Solenikov
|
||||
* Copyright (C) 2017 Merlok
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "crypto.h"
|
||||
#include "crypto_backend.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rsa.h"
|
||||
#include "sha1.h"
|
||||
|
||||
struct crypto_hash_polarssl {
|
||||
struct crypto_hash ch;
|
||||
// gcry_md_hd_t md;
|
||||
};
|
||||
|
||||
static void crypto_hash_polarssl_close(struct crypto_hash *_ch)
|
||||
{
|
||||
// struct crypto_hash_polarssl *ch = container_of(_ch, struct crypto_hash_libgcrypt, ch);
|
||||
|
||||
// gcry_md_close(ch->md);
|
||||
// free(ch);
|
||||
}
|
||||
|
||||
static void crypto_hash_polarssl_write(struct crypto_hash *_ch, const unsigned char *buf, size_t len)
|
||||
{
|
||||
// struct crypto_hash_polarssl *ch = container_of(_ch, struct crypto_hash_libgcrypt, ch);
|
||||
|
||||
// gcry_md_write(ch->md, buf, len);
|
||||
}
|
||||
|
||||
static unsigned char *crypto_hash_polarssl_read(struct crypto_hash *_ch)
|
||||
{
|
||||
// struct crypto_hash_polarssl *ch = container_of(_ch, struct crypto_hash_libgcrypt, ch);
|
||||
|
||||
// return gcry_md_read(ch->md, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static size_t crypto_hash_polarssl_get_size(const struct crypto_hash *ch)
|
||||
{
|
||||
/* int algo = GCRY_MD_NONE;
|
||||
|
||||
if (ch->algo == HASH_SHA_1)
|
||||
algo = GCRY_MD_SHA1;*/
|
||||
|
||||
// return gcry_md_get_algo_dlen(algo);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct crypto_hash *crypto_hash_polarssl_open(enum crypto_algo_hash hash)
|
||||
{
|
||||
struct crypto_hash_polarssl *ch = malloc(sizeof(*ch));
|
||||
/* gcry_error_t err;
|
||||
int algo = GCRY_MD_NONE;
|
||||
|
||||
if (hash == HASH_SHA_1)
|
||||
algo = GCRY_MD_SHA1;
|
||||
|
||||
err = gcry_md_open(&ch->md, algo, 0);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
free(ch);
|
||||
|
||||
return NULL;
|
||||
}*/
|
||||
|
||||
ch->ch.write = crypto_hash_polarssl_write;
|
||||
ch->ch.read = crypto_hash_polarssl_read;
|
||||
ch->ch.close = crypto_hash_polarssl_close;
|
||||
ch->ch.get_size = crypto_hash_polarssl_get_size;
|
||||
|
||||
return &ch->ch;
|
||||
}
|
||||
|
||||
struct crypto_pk_polarssl {
|
||||
struct crypto_pk cp;
|
||||
// gcry_sexp_t pk;
|
||||
};
|
||||
|
||||
static struct crypto_pk *crypto_pk_polarssl_open_rsa(va_list vl)
|
||||
{
|
||||
struct crypto_pk_polarssl *cp = malloc(sizeof(*cp));
|
||||
//gcry_error_t err;
|
||||
/* char *mod = va_arg(vl, char *);
|
||||
int modlen = va_arg(vl, size_t);
|
||||
char *exp = va_arg(vl, char *);
|
||||
int explen = va_arg(vl, size_t);
|
||||
*/
|
||||
/* err = gcry_sexp_build(&cp->pk, NULL, "(public-key (rsa (n %b) (e %b)))",
|
||||
modlen, mod, explen, exp);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
free(cp);
|
||||
return NULL;
|
||||
}*/
|
||||
|
||||
return &cp->cp;
|
||||
}
|
||||
|
||||
static struct crypto_pk *crypto_pk_polarssl_open_priv_rsa(va_list vl)
|
||||
{
|
||||
struct crypto_pk_polarssl *cp = malloc(sizeof(*cp));
|
||||
//gcry_error_t err;
|
||||
/* char *mod = va_arg(vl, char *);
|
||||
int modlen = va_arg(vl, size_t);
|
||||
char *exp = va_arg(vl, char *);
|
||||
int explen = va_arg(vl, size_t);
|
||||
char *d = va_arg(vl, char *);
|
||||
int dlen = va_arg(vl, size_t);
|
||||
char *p = va_arg(vl, char *);
|
||||
int plen = va_arg(vl, size_t);
|
||||
// gcry_mpi_t pmpi;
|
||||
char *q = va_arg(vl, char *);
|
||||
int qlen = va_arg(vl, size_t);
|
||||
// gcry_mpi_t qmpi;
|
||||
(void) va_arg(vl, char *);
|
||||
(void) va_arg(vl, size_t);
|
||||
(void) va_arg(vl, char *);
|
||||
(void) va_arg(vl, size_t);
|
||||
char *inv = va_arg(vl, char *);
|
||||
int invlen = va_arg(vl, size_t);*/
|
||||
/*gcry_mpi_t invmpi;
|
||||
|
||||
err = gcry_mpi_scan(&pmpi, GCRYMPI_FMT_USG, p, plen, NULL);
|
||||
if (err)
|
||||
goto err_p;
|
||||
|
||||
err = gcry_mpi_scan(&qmpi, GCRYMPI_FMT_USG, q, qlen, NULL);
|
||||
if (err)
|
||||
goto err_q;
|
||||
|
||||
err = gcry_mpi_scan(&invmpi, GCRYMPI_FMT_USG, inv, invlen, NULL);
|
||||
if (err)
|
||||
goto err_inv;
|
||||
|
||||
if (gcry_mpi_cmp (pmpi, qmpi) > 0) {
|
||||
gcry_mpi_swap (pmpi, qmpi);
|
||||
gcry_mpi_invm (invmpi, pmpi, qmpi);
|
||||
}
|
||||
|
||||
err = gcry_sexp_build(&cp->pk, NULL, "(private-key (rsa (n %b) (e %b) (d %b) (p %M) (q %M) (u %M)))",
|
||||
modlen, mod, explen, exp, dlen, d,
|
||||
pmpi, qmpi, invmpi);
|
||||
if (err)
|
||||
goto err_sexp;
|
||||
|
||||
err = gcry_pk_testkey(cp->pk);
|
||||
if (err)
|
||||
goto err_test;
|
||||
|
||||
gcry_mpi_release(invmpi);
|
||||
gcry_mpi_release(qmpi);
|
||||
gcry_mpi_release(pmpi);
|
||||
|
||||
return &cp->cp;
|
||||
|
||||
err_test:
|
||||
gcry_sexp_release(cp->pk);
|
||||
err_sexp:
|
||||
gcry_mpi_release(invmpi);
|
||||
err_inv:
|
||||
gcry_mpi_release(qmpi);
|
||||
err_q:
|
||||
gcry_mpi_release(pmpi);
|
||||
err_p:
|
||||
free(cp);
|
||||
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct crypto_pk *crypto_pk_polarssl_genkey_rsa(va_list vl)
|
||||
{
|
||||
struct crypto_pk_polarssl *cp = malloc(sizeof(*cp));
|
||||
// gcry_error_t err;
|
||||
// gcry_sexp_t params;
|
||||
/* int transient = va_arg(vl, int);
|
||||
unsigned int nbits = va_arg(vl, unsigned int);
|
||||
unsigned int exp = va_arg(vl, unsigned int);
|
||||
*/
|
||||
/* err = gcry_sexp_build(¶ms, NULL,
|
||||
transient ?
|
||||
"(genkey (rsa (nbits %u) (rsa-use-e %u) (flags transient-key)))":
|
||||
"(genkey (rsa (nbits %u) (rsa-use-e %u)))",
|
||||
nbits, exp);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
free(cp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_pk_genkey(&cp->pk, params);
|
||||
gcry_sexp_release(params);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
free(cp);
|
||||
return NULL;
|
||||
}*/
|
||||
|
||||
return &cp->cp;
|
||||
}
|
||||
|
||||
static void crypto_pk_polarssl_close(struct crypto_pk *_cp)
|
||||
{
|
||||
// struct crypto_pk_polarssl *cp = container_of(_cp, struct crypto_pk_libgcrypt, cp);
|
||||
|
||||
// gcry_sexp_release(cp->pk);
|
||||
// free(cp);
|
||||
}
|
||||
|
||||
static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk *_cp, const unsigned char *buf, size_t len, size_t *clen)
|
||||
{
|
||||
/*struct crypto_pk_polarssl *cp = container_of(_cp, struct crypto_pk_libgcrypt, cp);
|
||||
gcry_error_t err;
|
||||
int blen = len;
|
||||
gcry_sexp_t dsexp, esexp, asexp;
|
||||
gcry_mpi_t tmpi;
|
||||
size_t templen;
|
||||
size_t keysize;
|
||||
unsigned char *result;
|
||||
|
||||
err = gcry_sexp_build(&dsexp, NULL, "(data (flags raw) (value %b))",
|
||||
blen, buf);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_pk_encrypt(&esexp, dsexp, cp->pk);
|
||||
gcry_sexp_release(dsexp);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
asexp = gcry_sexp_find_token(esexp, "a", 1);
|
||||
gcry_sexp_release(esexp);
|
||||
if (!asexp)
|
||||
return NULL;
|
||||
|
||||
tmpi = gcry_sexp_nth_mpi(asexp, 1, GCRYMPI_FMT_USG);
|
||||
gcry_sexp_release(asexp);
|
||||
if (!tmpi)
|
||||
return NULL;
|
||||
|
||||
keysize = (gcry_pk_get_nbits(cp->pk) + 7) / 8;
|
||||
result = malloc(keysize);
|
||||
if (!result) {
|
||||
gcry_mpi_release(tmpi);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_mpi_print(GCRYMPI_FMT_USG, NULL, keysize, &templen, tmpi);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
gcry_mpi_release(tmpi);
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_mpi_print(GCRYMPI_FMT_USG, result + keysize - templen, templen, &templen, tmpi);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
gcry_mpi_release(tmpi);
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
memset(result, 0, keysize - templen);
|
||||
|
||||
*clen = keysize;
|
||||
gcry_mpi_release(tmpi);
|
||||
|
||||
return result;*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk *_cp, const unsigned char *buf, size_t len, size_t *clen)
|
||||
{
|
||||
/*struct crypto_pk_polarssl *cp = container_of(_cp, struct crypto_pk_libgcrypt, cp);
|
||||
gcry_error_t err;
|
||||
int blen = len;
|
||||
gcry_sexp_t esexp, dsexp;
|
||||
gcry_mpi_t tmpi;
|
||||
size_t templen;
|
||||
size_t keysize;
|
||||
unsigned char *result;
|
||||
|
||||
XXX: RSA-only!
|
||||
err = gcry_sexp_build(&esexp, NULL, "(enc-val (flags) (rsa (a %b)))",
|
||||
blen, buf);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_pk_decrypt(&dsexp, esexp, cp->pk);
|
||||
gcry_sexp_release(esexp);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tmpi = gcry_sexp_nth_mpi(dsexp, 1, GCRYMPI_FMT_USG);
|
||||
gcry_sexp_release(dsexp);
|
||||
if (!tmpi)
|
||||
return NULL;
|
||||
|
||||
keysize = (gcry_pk_get_nbits(cp->pk) + 7) / 8;
|
||||
result = malloc(keysize);
|
||||
if (!result) {
|
||||
gcry_mpi_release(tmpi);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_mpi_print(GCRYMPI_FMT_USG, NULL, keysize, &templen, tmpi);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
gcry_mpi_release(tmpi);
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_mpi_print(GCRYMPI_FMT_USG, result + keysize - templen, templen, &templen, tmpi);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
gcry_mpi_release(tmpi);
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
memset(result, 0, keysize - templen);
|
||||
|
||||
*clen = keysize;
|
||||
gcry_mpi_release(tmpi);
|
||||
|
||||
return result;*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static size_t crypto_pk_polarssl_get_nbits(const struct crypto_pk *_cp)
|
||||
{
|
||||
// struct crypto_pk_polarssl *cp = container_of(_cp, struct crypto_pk_libgcrypt, cp);
|
||||
|
||||
// return gcry_pk_get_nbits(cp->pk);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk *_cp, unsigned param, size_t *plen)
|
||||
{
|
||||
/*struct crypto_pk_polarssl *cp = container_of(_cp, struct crypto_pk_libgcrypt, cp);
|
||||
gcry_error_t err;
|
||||
gcry_sexp_t psexp;
|
||||
gcry_mpi_t tmpi;
|
||||
size_t parameter_size;
|
||||
unsigned char *result;
|
||||
const char *name;
|
||||
|
||||
XXX: RSA-only!
|
||||
if (param == 0)
|
||||
name = "n";
|
||||
else if (param == 1)
|
||||
name = "e";
|
||||
else
|
||||
return NULL;
|
||||
|
||||
psexp = gcry_sexp_find_token(cp->pk, name, 1);
|
||||
if (!psexp)
|
||||
return NULL;
|
||||
|
||||
tmpi = gcry_sexp_nth_mpi(psexp, 1, GCRYMPI_FMT_USG);
|
||||
gcry_sexp_release(psexp);
|
||||
if (!tmpi)
|
||||
return NULL;
|
||||
|
||||
parameter_size = (gcry_mpi_get_nbits(tmpi) + 7) / 8;
|
||||
result = malloc(parameter_size);
|
||||
if (!result) {
|
||||
gcry_mpi_release(tmpi);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = gcry_mpi_print(GCRYMPI_FMT_USG, result, parameter_size, NULL, tmpi);
|
||||
if (err) {
|
||||
fprintf(stderr, "LibGCrypt error %s/%s\n",
|
||||
gcry_strsource (err),
|
||||
gcry_strerror (err));
|
||||
gcry_mpi_release(tmpi);
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*plen = parameter_size;
|
||||
gcry_mpi_release(tmpi);
|
||||
|
||||
return result;*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct crypto_pk *crypto_pk_polarssl_open(enum crypto_algo_pk pk, va_list vl)
|
||||
{
|
||||
struct crypto_pk *cp;
|
||||
|
||||
if (pk == PK_RSA)
|
||||
cp = crypto_pk_polarssl_open_rsa(vl);
|
||||
else
|
||||
return NULL;
|
||||
|
||||
cp->close = crypto_pk_polarssl_close;
|
||||
cp->encrypt = crypto_pk_polarssl_encrypt;
|
||||
cp->get_parameter = crypto_pk_polarssl_get_parameter;
|
||||
cp->get_nbits = crypto_pk_polarssl_get_nbits;
|
||||
|
||||
return cp;
|
||||
}
|
||||
|
||||
static struct crypto_pk *crypto_pk_polarssl_open_priv(enum crypto_algo_pk pk, va_list vl)
|
||||
{
|
||||
struct crypto_pk *cp;
|
||||
|
||||
if (pk == PK_RSA)
|
||||
cp = crypto_pk_polarssl_open_priv_rsa(vl);
|
||||
else
|
||||
return NULL;
|
||||
|
||||
cp->close = crypto_pk_polarssl_close;
|
||||
cp->encrypt = crypto_pk_polarssl_encrypt;
|
||||
cp->decrypt = crypto_pk_polarssl_decrypt;
|
||||
cp->get_parameter = crypto_pk_polarssl_get_parameter;
|
||||
cp->get_nbits = crypto_pk_polarssl_get_nbits;
|
||||
|
||||
return cp;
|
||||
}
|
||||
|
||||
static struct crypto_pk *crypto_pk_polarssl_genkey(enum crypto_algo_pk pk, va_list vl)
|
||||
{
|
||||
struct crypto_pk *cp;
|
||||
|
||||
if (pk == PK_RSA)
|
||||
cp = crypto_pk_polarssl_genkey_rsa(vl);
|
||||
else
|
||||
return NULL;
|
||||
|
||||
cp->close = crypto_pk_polarssl_close;
|
||||
cp->encrypt = crypto_pk_polarssl_encrypt;
|
||||
cp->decrypt = crypto_pk_polarssl_decrypt;
|
||||
cp->get_parameter = crypto_pk_polarssl_get_parameter;
|
||||
cp->get_nbits = crypto_pk_polarssl_get_nbits;
|
||||
|
||||
return cp;
|
||||
}
|
||||
|
||||
static struct crypto_backend crypto_polarssl_backend = {
|
||||
.hash_open = crypto_hash_polarssl_open,
|
||||
.pk_open = crypto_pk_polarssl_open,
|
||||
.pk_open_priv = crypto_pk_polarssl_open_priv,
|
||||
.pk_genkey = crypto_pk_polarssl_genkey,
|
||||
};
|
||||
|
||||
struct crypto_backend *crypto_polarssl_init(void)
|
||||
{
|
||||
return &crypto_polarssl_backend;
|
||||
}
|
271
client/emv/sda_test.c
Normal file
271
client/emv/sda_test.c
Normal file
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
* emv-tools - a set of tools to work with EMV family of smart cards
|
||||
* Copyright (C) 2012, 2015 Dmitry Eremin-Solenikov
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "emv_pk.h"
|
||||
#include "crypto.h"
|
||||
#include "dump.h"
|
||||
#include "tlv.h"
|
||||
#include "emv_pki.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct emv_pk vsdc_01 = {
|
||||
.rid = { 0xa0, 0x00, 0x00, 0x00, 0x03, },
|
||||
.index = 1,
|
||||
.hash_algo = HASH_SHA_1,
|
||||
.pk_algo = PK_RSA,
|
||||
.hash = {
|
||||
0xd3, 0x4a, 0x6a, 0x77,
|
||||
0x60, 0x11, 0xc7, 0xe7,
|
||||
0xce, 0x3a, 0xec, 0x5f,
|
||||
0x03, 0xad, 0x2f, 0x8c,
|
||||
0xfc, 0x55, 0x03, 0xcc, },
|
||||
.exp = { 0x03, },
|
||||
.elen = 1,
|
||||
.mlen = 1024 / 8,
|
||||
.modulus = (unsigned char[]){
|
||||
0xc6, 0x96, 0x03, 0x42, 0x13, 0xd7, 0xd8, 0x54, 0x69, 0x84, 0x57, 0x9d, 0x1d, 0x0f, 0x0e, 0xa5,
|
||||
0x19, 0xcf, 0xf8, 0xde, 0xff, 0xc4, 0x29, 0x35, 0x4c, 0xf3, 0xa8, 0x71, 0xa6, 0xf7, 0x18, 0x3f,
|
||||
0x12, 0x28, 0xda, 0x5c, 0x74, 0x70, 0xc0, 0x55, 0x38, 0x71, 0x00, 0xcb, 0x93, 0x5a, 0x71, 0x2c,
|
||||
0x4e, 0x28, 0x64, 0xdf, 0x5d, 0x64, 0xba, 0x93, 0xfe, 0x7e, 0x63, 0xe7, 0x1f, 0x25, 0xb1, 0xe5,
|
||||
0xf5, 0x29, 0x85, 0x75, 0xeb, 0xe1, 0xc6, 0x3a, 0xa6, 0x17, 0x70, 0x69, 0x17, 0x91, 0x1d, 0xc2,
|
||||
0xa7, 0x5a, 0xc2, 0x8b, 0x25, 0x1c, 0x7e, 0xf4, 0x0f, 0x23, 0x65, 0x91, 0x24, 0x90, 0xb9, 0x39,
|
||||
0xbc, 0xa2, 0x12, 0x4a, 0x30, 0xa2, 0x8f, 0x54, 0x40, 0x2c, 0x34, 0xae, 0xca, 0x33, 0x1a, 0xb6,
|
||||
0x7e, 0x1e, 0x79, 0xb2, 0x85, 0xdd, 0x57, 0x71, 0xb5, 0xd9, 0xff, 0x79, 0xea, 0x63, 0x0b, 0x75,
|
||||
},
|
||||
};
|
||||
|
||||
const unsigned char issuer_cert[] = {
|
||||
0x3c, 0x5f, 0xea, 0xd4, 0xdd, 0x7b, 0xca, 0x44, 0xf9, 0x3e, 0x90, 0xc4, 0x4f, 0x76, 0xed, 0xe5,
|
||||
0x4a, 0x32, 0x88, 0xec, 0xdc, 0x78, 0x46, 0x9f, 0xcb, 0x12, 0x25, 0xc0, 0x3b, 0x2c, 0x04, 0xf2,
|
||||
0xc2, 0xf4, 0x12, 0x28, 0x1a, 0x08, 0x22, 0xdf, 0x14, 0x64, 0x92, 0x30, 0x98, 0x9f, 0xb1, 0x49,
|
||||
0x40, 0x70, 0xda, 0xf8, 0xc9, 0x53, 0x4a, 0x78, 0x81, 0x96, 0x01, 0x48, 0x61, 0x6a, 0xce, 0x58,
|
||||
0x17, 0x88, 0x12, 0x0d, 0x35, 0x06, 0xac, 0xe4, 0xce, 0xe5, 0x64, 0xfb, 0x27, 0xee, 0x53, 0x34,
|
||||
0x1c, 0x22, 0xf0, 0xb4, 0x5b, 0x31, 0x87, 0x3d, 0x05, 0xde, 0x54, 0x5e, 0xfe, 0x33, 0xbc, 0xd2,
|
||||
0x9b, 0x21, 0x85, 0xd0, 0x35, 0xa8, 0x06, 0xad, 0x08, 0xc6, 0x97, 0x6f, 0x35, 0x05, 0xa1, 0x99,
|
||||
0x99, 0x93, 0x0c, 0xa8, 0xa0, 0x3e, 0xfa, 0x32, 0x1c, 0x48, 0x60, 0x61, 0xf7, 0xdc, 0xec, 0x9f,
|
||||
};
|
||||
|
||||
const unsigned char issuer_rem[] = {
|
||||
0x1e, 0xbc, 0xa3, 0x0f, 0x00, 0xce, 0x59, 0x62, 0xa8, 0xc6, 0xe1, 0x30, 0x54, 0x4b, 0x82, 0x89,
|
||||
0x1b, 0x23, 0x6c, 0x65, 0xde, 0x29, 0x31, 0x7f, 0x36, 0x47, 0x35, 0xde, 0xe6, 0x3f, 0x65, 0x98,
|
||||
0x97, 0x58, 0x35, 0xd5
|
||||
};
|
||||
|
||||
const unsigned char issuer_exp[] = {
|
||||
0x03,
|
||||
};
|
||||
|
||||
const unsigned char ssad_cr[] = {
|
||||
0x99, 0xa5, 0x58, 0xb6, 0x2b, 0x67, 0x4a, 0xa5, 0xe7, 0xd2, 0xa5, 0x7e, 0x5e, 0xf6, 0xa6, 0xf2,
|
||||
0x25, 0x8e, 0x5d, 0xa0, 0x52, 0xd0, 0x5b, 0x54, 0xe5, 0xc1, 0x15, 0xff, 0x1c, 0xec, 0xf9, 0x4a,
|
||||
0xa2, 0xdf, 0x8f, 0x39, 0xa0, 0x1d, 0x71, 0xc6, 0x19, 0xeb, 0x81, 0x9d, 0xa5, 0x2e, 0xf3, 0x81,
|
||||
0xe8, 0x49, 0x79, 0x58, 0x6a, 0xea, 0x78, 0x55, 0xff, 0xbe, 0xf4, 0x0a, 0xa3, 0xa7, 0x1c, 0xd3,
|
||||
0xb0, 0x4c, 0xfd, 0xf2, 0x70, 0xae, 0xc8, 0x15, 0x8a, 0x27, 0x97, 0xf2, 0x4f, 0xd6, 0x13, 0xb7,
|
||||
0x48, 0x13, 0x46, 0x61, 0x13, 0x5c, 0xd2, 0x90, 0xe4, 0x5b, 0x04, 0xa8, 0xe0, 0xcc, 0xc7, 0x11,
|
||||
0xae, 0x04, 0x2f, 0x15, 0x9e, 0x73, 0xc8, 0x9c, 0x2a, 0x7e, 0x65, 0xa4, 0xc2, 0xfd, 0x1d, 0x61,
|
||||
0x06, 0x02, 0x4a, 0xa2, 0x71, 0x30, 0xb0, 0xec, 0xec, 0x02, 0x38, 0xf9, 0x16, 0x59, 0xde, 0x96,
|
||||
};
|
||||
|
||||
const unsigned char ssd1[] = {
|
||||
0x5f, 0x24, 0x03, 0x08, 0x12, 0x31, 0x5a, 0x08, 0x42, 0x76, 0x55, 0x00, 0x13, 0x23, 0x45, 0x99, 0x5f, 0x34, 0x01, 0x01, 0x9f, 0x07, 0x02, 0xff, 0x00, 0x9f, 0x0d, 0x05, 0xd0, 0x40, 0xac, 0xa8, 0x00, 0x9f, 0x0e, 0x05, 0x00, 0x10, 0x00, 0x00, 0x00, 0x9f, 0x0f, 0x05, 0xd0, 0x68, 0xbc, 0xf8, 0x00,
|
||||
0x5c, 0x00,
|
||||
};
|
||||
static const struct tlv ssd1_tlv = {
|
||||
.len = sizeof(ssd1),
|
||||
.value = ssd1,
|
||||
};
|
||||
|
||||
const unsigned char pan[] = {
|
||||
0x42, 0x76, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
static int sda_test_raw(void)
|
||||
{
|
||||
const struct emv_pk *pk = &vsdc_01;
|
||||
|
||||
struct crypto_pk *kcp = crypto_pk_open(PK_RSA,
|
||||
pk->modulus, pk->mlen,
|
||||
pk->exp, pk->elen);
|
||||
if (!kcp)
|
||||
return 1;
|
||||
printf("--open\n");
|
||||
unsigned char *ipk_data;
|
||||
size_t ipk_data_len;
|
||||
ipk_data = crypto_pk_encrypt(kcp, issuer_cert, sizeof(issuer_cert), &ipk_data_len);
|
||||
crypto_pk_close(kcp);
|
||||
|
||||
if (!ipk_data)
|
||||
return 1;
|
||||
printf("--ipk_data\n");
|
||||
|
||||
dump_buffer(ipk_data, ipk_data_len, stdout, 0);
|
||||
|
||||
size_t ipk_pk_len = ipk_data[13];
|
||||
unsigned char *ipk_pk = malloc(ipk_pk_len);
|
||||
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
|
||||
memcpy(ipk_pk + ipk_data_len - 36, issuer_rem, sizeof(issuer_rem));
|
||||
|
||||
struct crypto_hash *ch;
|
||||
ch = crypto_hash_open(HASH_SHA_1);
|
||||
if (!ch) {
|
||||
free(ipk_pk);
|
||||
free(ipk_data);
|
||||
return 1;
|
||||
}
|
||||
printf("--crypto_hash_open\n");
|
||||
|
||||
crypto_hash_write(ch, ipk_data + 1, 14);
|
||||
crypto_hash_write(ch, ipk_pk, ipk_pk_len);
|
||||
crypto_hash_write(ch, issuer_exp, sizeof(issuer_exp));
|
||||
|
||||
unsigned char *h = crypto_hash_read(ch);
|
||||
if (!h) {
|
||||
crypto_hash_close(ch);
|
||||
free(ipk_pk);
|
||||
free(ipk_data);
|
||||
return 1;
|
||||
}
|
||||
printf("--crypto_hash_read\n");
|
||||
|
||||
dump_buffer(h, 20, stdout, 0);
|
||||
|
||||
if (memcmp(ipk_data + ipk_data_len - 21, h, 20)) {
|
||||
crypto_hash_close(ch);
|
||||
free(ipk_pk);
|
||||
free(ipk_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
crypto_hash_close(ch);
|
||||
free(ipk_data);
|
||||
|
||||
struct crypto_pk *ikcp = crypto_pk_open(PK_RSA, ipk_pk, (int) ipk_pk_len,
|
||||
issuer_exp, (int) sizeof(issuer_exp));
|
||||
free(ipk_pk);
|
||||
if (!ikcp)
|
||||
return 1;
|
||||
printf("--crypto_pk_open\n");
|
||||
|
||||
size_t ssad_len;
|
||||
unsigned char *ssad = crypto_pk_encrypt(ikcp, ssad_cr, sizeof(ssad_cr), &ssad_len);
|
||||
crypto_pk_close(ikcp);
|
||||
if (!ssad)
|
||||
return 1;
|
||||
printf("--crypto_pk_encrypt\n");
|
||||
|
||||
dump_buffer(ssad, ssad_len, stdout, 0);
|
||||
|
||||
ch = crypto_hash_open(HASH_SHA_1);
|
||||
if (!ch) {
|
||||
free(ssad);
|
||||
return 1;
|
||||
}
|
||||
printf("--crypto_hash_open2\n");
|
||||
|
||||
crypto_hash_write(ch, ssad + 1, ssad_len - 22);
|
||||
crypto_hash_write(ch, ssd1, sizeof(ssd1));
|
||||
|
||||
unsigned char *h2 = crypto_hash_read(ch);
|
||||
if (!h2) {
|
||||
crypto_hash_close(ch);
|
||||
free(ssad);
|
||||
return 1;
|
||||
}
|
||||
printf("--crypto_hash_read2\n");
|
||||
|
||||
dump_buffer(h2, 20, stdout, 0);
|
||||
|
||||
crypto_hash_close(ch);
|
||||
|
||||
free(ssad);
|
||||
printf("--done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sda_test_pk(void)
|
||||
{
|
||||
const struct emv_pk *pk = &vsdc_01;
|
||||
struct tlvdb *db;
|
||||
|
||||
db = tlvdb_external(0x90, sizeof(issuer_cert), issuer_cert);
|
||||
tlvdb_add(db, tlvdb_external(0x9f32, sizeof(issuer_exp), issuer_exp));
|
||||
tlvdb_add(db, tlvdb_external(0x92, sizeof(issuer_rem), issuer_rem));
|
||||
tlvdb_add(db, tlvdb_external(0x5a, sizeof(pan), pan));
|
||||
|
||||
struct emv_pk *ipk = emv_pki_recover_issuer_cert(pk, db);
|
||||
if (!ipk) {
|
||||
fprintf(stderr, "Could not recover Issuer certificate!\n");
|
||||
tlvdb_free(db);
|
||||
return 2;
|
||||
}
|
||||
|
||||
tlvdb_add(db, tlvdb_external(0x93, sizeof(ssad_cr), ssad_cr));
|
||||
|
||||
struct tlvdb *dacdb = emv_pki_recover_dac(ipk, db, &ssd1_tlv);
|
||||
if (!dacdb) {
|
||||
fprintf(stderr, "Could not recover DAC!\n");
|
||||
emv_pk_free(ipk);
|
||||
tlvdb_free(db);
|
||||
return 2;
|
||||
}
|
||||
|
||||
const struct tlv *dac = tlvdb_get(dacdb, 0x9f45, NULL);
|
||||
if (!dac) {
|
||||
fprintf(stderr, "DAC not found!\n");
|
||||
tlvdb_free(dacdb);
|
||||
emv_pk_free(ipk);
|
||||
tlvdb_free(db);
|
||||
return 2;
|
||||
}
|
||||
|
||||
dump_buffer(dac->value, dac->len, stdout, 0);
|
||||
|
||||
tlvdb_free(dacdb);
|
||||
emv_pk_free(ipk);
|
||||
tlvdb_free(db);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exec_sda_test(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
fprintf(stdout, "SDA raw test: ");
|
||||
ret = sda_test_raw();
|
||||
if (ret) {
|
||||
fprintf(stderr, "[ERROR]\n");
|
||||
return ret;
|
||||
}
|
||||
fprintf(stdout, "[OK]\n");
|
||||
|
||||
fprintf(stdout, "SDA test pk: ");
|
||||
ret = sda_test_pk();
|
||||
if (ret) {
|
||||
fprintf(stdout, "SDA raw test: ");
|
||||
return ret;
|
||||
}
|
||||
fprintf(stdout, "[OK]\n");
|
||||
|
||||
return 0;
|
||||
}
|
16
client/emv/sda_test.h
Normal file
16
client/emv/sda_test.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* emv-tools - a set of tools to work with EMV family of smart cards
|
||||
* Copyright (C) 2012, 2015 Dmitry Eremin-Solenikov
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*/
|
||||
|
||||
extern int exec_sda_test(void);
|
Loading…
Add table
Add a link
Reference in a new issue