Skip to content

Commit

Permalink
chore: lint C++ with clang-format and cleanup of dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Jul 9, 2022
1 parent fa55df1 commit af15a68
Show file tree
Hide file tree
Showing 77 changed files with 7,265 additions and 9,536 deletions.
1,029 changes: 497 additions & 532 deletions libs/pkcs11/src/CSP/AbilitaCIE.cpp

Large diffs are not rendered by default.

41 changes: 20 additions & 21 deletions libs/pkcs11/src/CSP/AbilitaCIE.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@
// Created by ugo chirico on 02/09/18. http://www.ugochirico.com
// Copyright © 2018 IPZS. All rights reserved.
//
#include "../PKCS11/cryptoki.h"
#include <string>

#include "../PCSC/PCSC.h"
#include "../PKCS11/cryptoki.h"

#define SCARD_ATTR_VALUE(Class, Tag) ((((uint32_t)(Class)) << 16) | ((uint32_t)(Tag)))
#define SCARD_CLASS_ICC_STATE 9 /**< ICC State specific definitions */
#define SCARD_ATTR_ATR_STRING SCARD_ATTR_VALUE(SCARD_CLASS_ICC_STATE, 0x0303) /**< Answer to reset (ATR) string. */
#define SCARD_ATTR_VALUE(Class, Tag) \
((((uint32_t)(Class)) << 16) | ((uint32_t)(Tag)))
#define SCARD_CLASS_ICC_STATE 9 /**< ICC State specific definitions */
#define SCARD_ATTR_ATR_STRING \
SCARD_ATTR_VALUE(SCARD_CLASS_ICC_STATE, \
0x0303) /**< Answer to reset (ATR) string. */

//using namespace std
// using namespace std

/* CK_NOTIFY is an application callback that processes events */
typedef CK_CALLBACK_FUNCTION(CK_RV, PROGRESS_CALLBACK)(
const int progress,
const char* szMessage);

typedef CK_CALLBACK_FUNCTION(CK_RV, COMPLETED_CALLBACK)(
const char* szPan,
const char* szName,
const char* ef_seriale);

typedef CK_RV (*AbilitaCIEfn)(const char* szPAN,
const char* szPIN,
int* attempts,
PROGRESS_CALLBACK progressCallBack,
typedef CK_CALLBACK_FUNCTION(CK_RV, PROGRESS_CALLBACK)(const int progress,
const char* szMessage);

typedef CK_CALLBACK_FUNCTION(CK_RV, COMPLETED_CALLBACK)(const char* szPan,
const char* szName,
const char* ef_seriale);

typedef CK_RV (*AbilitaCIEfn)(const char* szPAN, const char* szPIN,
int* attempts, PROGRESS_CALLBACK progressCallBack,
COMPLETED_CALLBACK completedCallBack);

typedef CK_RV (*VerificaCIEAbilitatafn)();
typedef CK_RV (*DisabilitaCIEfn)();

int TokenTransmitCallback(safeConnection *data, uint8_t *apdu, DWORD apduSize, uint8_t *resp, DWORD *respSize);


int TokenTransmitCallback(safeConnection* data, uint8_t* apdu, DWORD apduSize,
uint8_t* resp, DWORD* respSize);
39 changes: 22 additions & 17 deletions libs/pkcs11/src/CSP/ExtAuthKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
#include "../Util/Array.h"

BYTE ExtAuth_PrivExp[] = {
0x18, 0x6B, 0x31, 0x48, 0x8C, 0x25, 0xDC, 0xF8, 0x5D, 0x95, 0x3D, 0x36, 0x30, 0xC0, 0xD0, 0x73,
0xBA, 0x1C, 0x6A, 0xA2, 0x45, 0x81, 0xAD, 0x25, 0x4F, 0x3B, 0x67, 0x19, 0xC5, 0xD7, 0x2C, 0xCA,
0x3E, 0x5C, 0xDC, 0x5A, 0x1E, 0x53, 0x16, 0x57, 0x8D, 0x75, 0x95, 0x4F, 0xF7, 0x3B, 0x23, 0x7B,
0x53, 0x2C, 0x9F, 0x8D, 0xE4, 0xA2, 0xC4, 0xC9, 0x11, 0x38, 0x5A, 0x23, 0xE6, 0x3E, 0x33, 0xE4,
0x7E, 0xE4, 0x5E, 0x66, 0xEF, 0xD4, 0x9B, 0x18, 0xE0, 0x2C, 0xFF, 0x87, 0x59, 0x8C, 0x39, 0x10,
0x9E, 0x8F, 0x86, 0xA6, 0x6B, 0xC3, 0x30, 0x24, 0x9C, 0xE3, 0xFC, 0xAD, 0x65, 0x5D, 0xCD, 0xBF,
0x98, 0xC9, 0xC5, 0xE4, 0x79, 0x32, 0x1A, 0x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4B, 0x3C, 0x8D, 0xFA, 0xB4,
0xD9, 0x0C, 0xEC, 0xFC, 0x5E, 0x60, 0x8D, 0x67, 0x3E, 0x67, 0x62, 0xC6,
0x2C, 0xB7, 0x98, 0x34, 0x12, 0x71, 0x14, 0x9B, 0xA6, 0x88, 0x16, 0x2E,
0xC7, 0xD0, 0xE3, 0x46, 0x8F, 0x65, 0xA9, 0x4A, 0xB4, 0xAD, 0x1A, 0xB6,
0x7E, 0x37, 0xBF, 0xC1};

ByteArray baExtAuth_PrivExp(ExtAuth_PrivExp, sizeof(ExtAuth_PrivExp));
255 changes: 131 additions & 124 deletions libs/pkcs11/src/CSP/FirmaConCIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,167 +5,174 @@
//

#include "FirmaConCIE.h"
#include "IAS.h"
#include "../PKCS11/wintypes.h"

#include "../Crypto/ASNParser.h"
#include "../LOGGER/Logger.h"
#include "../PCSC/PCSC.h"
#include "../PKCS11/PKCS11Functions.h"
#include "../PKCS11/Slot.h"
#include "../Util/ModuleInfo.h"
#include "../PCSC/PCSC.h"
#include "../Crypto/ASNParser.h"
#include "../PKCS11/wintypes.h"
#include "../Sign/CIESign.h"
#include "../Util/ModuleInfo.h"
#include "AbilitaCIE.h"
#include "../LOGGER/Logger.h"
#include "IAS.h"

using namespace CieIDLogger;


#define CARD_PAN_MISMATCH (int)(0x000000F1)
#define CARD_PAN_MISMATCH (int)(0x000000F1)

extern "C" {
CK_RV CK_ENTRY firmaConCIE(const char* inFilePath, const char* type, const char* pin, const char* pan, int page, float x, float y, float w, float h, const char* imagePathFile, const char* outFilePath, PROGRESS_CALLBACK progressCallBack, SIGN_COMPLETED_CALLBACK completedCallBack);
CK_RV CK_ENTRY firmaConCIE(const char* inFilePath, const char* type,
const char* pin, const char* pan, int page, float x,
float y, float w, float h, const char* imagePathFile,
const char* outFilePath,
PROGRESS_CALLBACK progressCallBack,
SIGN_COMPLETED_CALLBACK completedCallBack);
}

CK_RV CK_ENTRY firmaConCIE(const char* inFilePath, const char* type,
const char* pin, const char* pan, int page, float x,
float y, float w, float h, const char* imagePathFile,
const char* outFilePath,
PROGRESS_CALLBACK progressCallBack,
SIGN_COMPLETED_CALLBACK completedCallBack) {
LOG_INFO("****** Starting firmaConCIE ******");
LOG_DEBUG("firmaConCIE - page: %d, x: %f, y: %f, w: %f, h: %f", page, x, y, w,
h);

char* readers = NULL;
char* ATR = NULL;
try {
std::map<uint8_t, ByteDynArray> hashSet;

DWORD len = 0;
ByteDynArray CertCIE;
ByteDynArray SOD;

SCARDCONTEXT hSC;

long nRet = SCardEstablishContext(SCARD_SCOPE_USER, nullptr, nullptr, &hSC);
if (nRet != SCARD_S_SUCCESS) {
LOG_ERROR("firmaConCIE - List readers error: %d\n", nRet);
return CKR_DEVICE_ERROR;
}
LOG_INFO("firmaConCIE - Establish Context ok\n");

CK_RV CK_ENTRY firmaConCIE(const char* inFilePath, const char* type, const char* pin, const char* pan, int page, float x, float y, float w, float h, const char* imagePathFile, const char* outFilePath, PROGRESS_CALLBACK progressCallBack, SIGN_COMPLETED_CALLBACK completedCallBack) {

LOG_INFO("****** Starting firmaConCIE ******");
LOG_DEBUG("firmaConCIE - page: %d, x: %f, y: %f, w: %f, h: %f", page, x, y, w, h);

char* readers = NULL;
char* ATR = NULL;
try {
std::map<uint8_t, ByteDynArray> hashSet;

DWORD len = 0;
ByteDynArray CertCIE;
ByteDynArray SOD;

SCARDCONTEXT hSC;
nRet = SCardListReaders(hSC, nullptr, NULL, &len);
if (nRet != SCARD_S_SUCCESS) {
LOG_ERROR("firmaConCIE - List readers error: %d\n", nRet);
return CKR_TOKEN_NOT_PRESENT;
}

long nRet = SCardEstablishContext(SCARD_SCOPE_USER, nullptr, nullptr, &hSC);
if (nRet != SCARD_S_SUCCESS) {
LOG_ERROR("firmaConCIE - List readers error: %d\n", nRet);
return CKR_DEVICE_ERROR;
}
LOG_INFO("firmaConCIE - Establish Context ok\n");
if (len == 1) return CKR_TOKEN_NOT_PRESENT;

nRet = SCardListReaders(hSC, nullptr, NULL, &len);
if ( nRet!= SCARD_S_SUCCESS) {
LOG_ERROR("firmaConCIE - List readers error: %d\n", nRet);
return CKR_TOKEN_NOT_PRESENT;
}
readers = (char*)malloc(len);

if (len == 1)
return CKR_TOKEN_NOT_PRESENT;
if (SCardListReaders(hSC, nullptr, (char*)readers, &len) !=
SCARD_S_SUCCESS) {
free(readers);
return CKR_TOKEN_NOT_PRESENT;
}

readers = (char*)malloc(len);
char* curreader = readers;
bool foundCIE = false;
for (; curreader[0] != 0; curreader += strnlen(curreader, len) + 1) {
safeConnection conn(hSC, curreader, SCARD_SHARE_SHARED);
if (!conn.hCard) continue;

if (SCardListReaders(hSC, nullptr, (char*)readers, &len) != SCARD_S_SUCCESS) {
free(readers);
return CKR_TOKEN_NOT_PRESENT;
}
DWORD atrLen = 40;
if (SCardGetAttrib(conn.hCard, SCARD_ATTR_ATR_STRING, (uint8_t*)ATR,
&atrLen) != SCARD_S_SUCCESS) {
free(readers);
return CKR_DEVICE_ERROR;
}

char *curreader = readers;
bool foundCIE = false;
for (; curreader[0] != 0; curreader += strnlen(curreader, len) + 1) {
safeConnection conn(hSC, curreader, SCARD_SHARE_SHARED);
if (!conn.hCard)
continue;
ATR = (char*)malloc(atrLen);

DWORD atrLen = 40;
if(SCardGetAttrib(conn.hCard, SCARD_ATTR_ATR_STRING, (uint8_t*)ATR, &atrLen) != SCARD_S_SUCCESS) {
free(readers);
return CKR_DEVICE_ERROR;
}
if (SCardGetAttrib(conn.hCard, SCARD_ATTR_ATR_STRING, (uint8_t*)ATR,
&atrLen) != SCARD_S_SUCCESS) {
free(readers);
free(ATR);
return CKR_DEVICE_ERROR;
}

ATR = (char*)malloc(atrLen);
ByteArray atrBa((BYTE*)ATR, atrLen);

if(SCardGetAttrib(conn.hCard, SCARD_ATTR_ATR_STRING, (uint8_t*)ATR, &atrLen) != SCARD_S_SUCCESS) {
free(readers);
free(ATR);
return CKR_DEVICE_ERROR;
}
progressCallBack(20, "");

ByteArray atrBa((BYTE*)ATR, atrLen);
IAS* ias =
new IAS((CToken::TokenTransmitCallback)TokenTransmitCallback, atrBa);
ias->SetCardContext(&conn);

progressCallBack(20, "");
foundCIE = false;
ias->token.Reset();
ias->SelectAID_IAS();
ias->ReadPAN();

IAS* ias = new IAS((CToken::TokenTransmitCallback)TokenTransmitCallback, atrBa);
ias->SetCardContext(&conn);
foundCIE = true;
ByteDynArray IntAuth;
ias->SelectAID_CIE();
ias->ReadDappPubKey(IntAuth);
ias->SelectAID_CIE();
ias->InitEncKey();

foundCIE = false;
ias->token.Reset();
ias->SelectAID_IAS();
ias->ReadPAN();
ByteDynArray IdServizi;
ias->ReadIdServizi(IdServizi);
ByteArray baPan = ByteArray((uint8_t*)pan, strlen(pan));

foundCIE = true;
ByteDynArray IntAuth;
ias->SelectAID_CIE();
ias->ReadDappPubKey(IntAuth);
ias->SelectAID_CIE();
ias->InitEncKey();
if (memcmp(baPan.data(), IdServizi.data(), IdServizi.size()) != 0) {
return CARD_PAN_MISMATCH;
}

ByteDynArray IdServizi;
ias->ReadIdServizi(IdServizi);
ByteArray baPan = ByteArray((uint8_t*)pan, strlen(pan));
ByteDynArray FullPIN;
ByteArray LastPIN = ByteArray((uint8_t*)pin, strlen(pin));
ias->GetFirstPIN(FullPIN);
FullPIN.append(LastPIN);
ias->token.Reset();

if (memcmp(baPan.data(), IdServizi.data(), IdServizi.size()) != 0) {
return CARD_PAN_MISMATCH;
}
progressCallBack(40, "");

ByteDynArray FullPIN;
ByteArray LastPIN = ByteArray((uint8_t*)pin, strlen(pin));
ias->GetFirstPIN(FullPIN);
FullPIN.append(LastPIN);
ias->token.Reset();
char fullPinCStr[9];
memcpy(fullPinCStr, FullPIN.data(), 8);
fullPinCStr[8] = 0;

progressCallBack(40, "");
CIESign* cieSign = new CIESign(ias);

char fullPinCStr[9];
memcpy(fullPinCStr, FullPIN.data(), 8);
fullPinCStr[8] = 0;
uint16_t ret = cieSign->sign(inFilePath, type, fullPinCStr, page, x, y, w,
h, imagePathFile, outFilePath);
if ((ret & (0x63C0)) == 0x63C0) {
return CKR_PIN_INCORRECT;
} else if (ret == 0x6983) {
return CKR_PIN_LOCKED;
}

CIESign* cieSign = new CIESign(ias);

uint16_t ret = cieSign->sign(inFilePath, type, fullPinCStr, page, x, y, w, h, imagePathFile, outFilePath);
if((ret & (0x63C0)) == 0x63C0) {
return CKR_PIN_INCORRECT;
} else if (ret == 0x6983) {
return CKR_PIN_LOCKED;
}
progressCallBack(100, "");

LOG_INFO("firmaConCIE - completed, res: %d", ret);

progressCallBack(100, "");

LOG_INFO("firmaConCIE - completed, res: %d", ret);
free(ias);
free(cieSign);

free(ias);
free(cieSign);

completedCallBack(ret);
}

if (!foundCIE) {
free(ATR);
free(readers);
return CKR_TOKEN_NOT_RECOGNIZED;

}
} catch (std::exception &ex) {
LOG_ERROR(ex.what());
if (ATR)
free(ATR);
LOG_ERROR("firmaConCIE - Eccezione: %s", ex.what());
if (readers)
free(readers);
completedCallBack(ret);
}

LOG_ERROR("firmaConCIE - General error\n");
return CKR_GENERAL_ERROR;
if (!foundCIE) {
free(ATR);
free(readers);
return CKR_TOKEN_NOT_RECOGNIZED;
}
} catch (std::exception& ex) {
LOG_ERROR(ex.what());
if (ATR) free(ATR);
LOG_ERROR("firmaConCIE - Eccezione: %s", ex.what());
if (readers) free(readers);

if (ATR)
free(ATR);
LOG_ERROR("firmaConCIE - General error\n");
return CKR_GENERAL_ERROR;
}

if (ATR) free(ATR);

free(readers);
return SCARD_S_SUCCESS;
free(readers);
return SCARD_S_SUCCESS;
}
Loading

0 comments on commit af15a68

Please sign in to comment.