22#include <freerdp/config.h>
24#include <winpr/wlog.h>
25#include <winpr/stream.h>
26#include <winpr/collections.h>
28#include <freerdp/crypto/crypto.h>
32#include "../../crypto/certificate.h"
33#include "../../crypto/privatekey.h"
34#include "smartcard_virtual_gids.h"
36#define TAG CHANNELS_TAG("smartcard.vgids")
38#define VGIDS_EFID_MASTER 0xA000
39#define VGIDS_EFID_COMMON 0xA010
43#define VGIDS_EFID_CARDID 0xA012
45#define VGIDS_EFID_CURRENTDF 0x3FFF
47#define VGIDS_DO_FILESYSTEMTABLE 0xDF1F
48#define VGIDS_DO_KEYMAP 0xDF20
49#define VGIDS_DO_CARDID 0xDF20
50#define VGIDS_DO_CARDAPPS 0xDF21
51#define VGIDS_DO_CARDCF 0xDF22
52#define VGIDS_DO_CMAPFILE 0xDF23
53#define VGIDS_DO_KXC00 0xDF24
55#define VGIDS_CARDID_SIZE 16
56#define VGIDS_MAX_PIN_SIZE 127
58#define VGIDS_KEY_TYPE_KEYEXCHANGE 0x9A
61#define VGIDS_ALGID_RSA_1024 0x06
62#define VGIDS_ALGID_RSA_2048 0x07
63#define VGIDS_ALGID_RSA_3072 0x08
64#define VGIDS_ALGID_RSA_4096 0x09
67#define VGIDS_SE_CRT_SIGN 0xB6
68#define VGIDS_SE_CRT_CONF 0xB8
70#define VGIDS_SE_ALGOID_CT_PAD_PKCS1 0x40
71#define VGIDS_SE_ALGOID_CT_PAD_OAEP 0x80
77#define VGIDS_SE_ALGOID_DST_PAD_PKCS1 0x40
88#define VGIDS_DEFAULT_KEY_REF 0x81
90#define ISO_INS_SELECT 0xA4
91#define ISO_INS_GETDATA 0xCB
92#define ISO_INS_GETRESPONSE 0xC0
93#define ISO_INS_MSE 0x22
94#define ISO_INS_PSO 0x2A
95#define ISO_INS_VERIFY 0x20
97#define ISO_STATUS_MORE_DATA 0x6100
98#define ISO_STATUS_VERIFYFAILED 0x6300
99#define ISO_STATUS_WRONGLC 0x6700
100#define ISO_STATUS_COMMANDNOTALLOWED 0x6900
101#define ISO_STATUS_SECURITYSTATUSNOTSATISFIED 0x6982
102#define ISO_STATUS_AUTHMETHODBLOCKED 0x6983
103#define ISO_STATUS_INVALIDCOMMANDDATA 0x6A80
104#define ISO_STATUS_FILENOTFOUND 0x6A82
105#define ISO_STATUS_INVALIDP1P2 0x6A86
106#define ISO_STATUS_INVALIDLC 0x6A87
107#define ISO_STATUS_REFERENCEDATANOTFOUND 0x6A88
108#define ISO_STATUS_SUCCESS 0x9000
110#define ISO_AID_MAX_SIZE 16
112#define ISO_FID_MF 0x3F00
120typedef struct vgids_ef vgidsEF;
128typedef struct vgids_se vgidsSE;
134 wStream* commandData;
135 wStream* responseData;
139 rdpCertificate* certificate;
140 rdpPrivateKey* privateKey;
143 SmartcardEmulationContext* emuContext;
147#define VGIDS_MAX_DIGEST_INFO 7
149static const BYTE g_PKCS1_SHA1[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
150 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
151static const BYTE g_PKCS1_SHA224[] = { 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
152 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c };
153static const BYTE g_PKCS1_SHA256[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
154 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
155static const BYTE g_PKCS1_SHA384[] = { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
156 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
157static const BYTE g_PKCS1_SHA512[] = { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
158 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
159static const BYTE g_PKCS1_SHA512_224[] = { 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60,
160 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
161 0x05, 0x05, 0x00, 0x04, 0x1c };
162static const BYTE g_PKCS1_SHA512_256[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
163 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
164 0x06, 0x05, 0x00, 0x04, 0x20 };
167struct vgids_digest_info_map
171 const EVP_MD* digest;
173typedef struct vgids_digest_info_map vgidsDigestInfoMap;
180static const BYTE g_MsGidsAID[] = {
181 0xA0, 0x00, 0x00, 0x03, 0x97, 0x42, 0x54, 0x46, 0x59, 0x02, 0x01
188static const BYTE g_GidsAppFCP[] = { 0x62, 0x08, 0x82, 0x01, 0x38, 0x8C, 0x03, 0x03, 0x30, 0x30 };
194static const BYTE g_GidsAppFCI[] = { 0x61, 0x12, 0x4F, 0x0B, 0xA0, 0x00, 0x00, 0x03, 0x97, 0x42,
195 0x54, 0x46, 0x59, 0x02, 0x01, 0x73, 0x03, 0x40, 0x01, 0xC0 };
205static const BYTE g_CardCFContents[] = { 0x00, 0x00, 0x01, 0x00, 0x04, 0x00 };
208static const BYTE g_CardAppsContents[] = { 0x6d, 0x73, 0x63, 0x70, 0x00, 0x00, 0x00, 0x00 };
218#define MAX_CONTAINER_NAME_LEN 39
224#define CONTAINER_MAP_VALID_CONTAINER 1
229#define CONTAINER_MAP_DEFAULT_CONTAINER 2
231struct vgids_container_map_entry
233 WCHAR wszGuid[MAX_CONTAINER_NAME_LEN + 1];
236 WORD wSigKeySizeBits;
237 WORD wKeyExchangeKeySizeBits;
239typedef struct vgids_container_map_entry vgidsContainerMapEntry;
241struct vgids_filesys_table_entry
246 UINT16 dataObjectIdentifier;
248 UINT16 fileIdentifier;
251typedef struct vgids_filesys_table_entry vgidsFilesysTableEntry;
253struct vgids_keymap_record
259 UINT16 unknownWithFFFF;
260 UINT16 unknownWith0000;
262typedef struct vgids_keymap_record vgidsKeymapRecord;
266static void vgids_ef_free(
void* ptr);
268WINPR_ATTR_MALLOC(vgids_ef_free, 1)
269static vgidsEF* vgids_ef_new(vgidsContext* ctx, USHORT
id)
271 vgidsEF* ef = calloc(1,
sizeof(vgidsEF));
274 ef->data = Stream_New(
nullptr, 1024);
277 WLog_ERR(TAG,
"Failed to create file data stream");
280 if (!Stream_SetLength(ef->data, 0))
283 if (!ArrayList_Append(ctx->files, ef))
285 WLog_ERR(TAG,
"Failed to add new ef to file list");
297static BOOL vgids_write_tlv(
wStream* s, UINT16 tag,
const void* data,
size_t dataSize)
299 WINPR_ASSERT(dataSize <= UINT16_MAX);
302 if (!Stream_EnsureRemainingCapacity(s, dataSize + 5ull))
304 WLog_ERR(TAG,
"Failed to ensure capacity of DO stream");
311 Stream_Write_UINT16_BE(s, tag);
313 Stream_Write_UINT8(s, (BYTE)tag);
316 Stream_Write_UINT8(s, (BYTE)dataSize);
318 else if (dataSize < 256)
320 Stream_Write_UINT8(s, 0x81);
321 Stream_Write_UINT8(s, (BYTE)dataSize);
325 Stream_Write_UINT8(s, 0x82);
326 Stream_Write_UINT16_BE(s, (UINT16)dataSize);
328 Stream_Write(s, data, dataSize);
329 Stream_SealLength(s);
334static BOOL vgids_ef_write_do(vgidsEF* ef, UINT16 doID,
const void* data, DWORD dataSize)
337 return vgids_write_tlv(ef->data, doID, data, dataSize);
341static BOOL vgids_ef_read_do(vgidsEF* ef, UINT16 doID, BYTE** data, DWORD* dataSize)
344 Stream_ResetPosition(ef->data);
347 while (Stream_GetRemainingLength(ef->data) > 3)
354 curPos = Stream_GetPosition(ef->data);
355 Stream_Read_UINT16_BE(ef->data, nextDOID);
356 Stream_Read_UINT8(ef->data, len);
359 BYTE lenSize = len & 0x7F;
360 if (!Stream_CheckAndLogRequiredLength(TAG, ef->data, lenSize))
366 Stream_Read_UINT8(ef->data, doSize);
369 Stream_Read_UINT16_BE(ef->data, doSize);
372 WLog_ERR(TAG,
"Unexpected tag length %" PRIu8, lenSize);
379 if (!Stream_CheckAndLogRequiredLength(TAG, ef->data, doSize))
382 if (nextDOID == doID)
384 BYTE* outData =
nullptr;
387 doSize += (UINT16)(Stream_GetPosition(ef->data) - curPos);
388 if (!Stream_SetPosition(ef->data, curPos))
391 outData = malloc(doSize);
394 WLog_ERR(TAG,
"Failed to allocate output buffer");
398 Stream_Read(ef->data, outData, doSize);
406 if (!Stream_SafeSeek(ef->data, doSize))
414void vgids_ef_free(
void* ptr)
419 Stream_Free(ef->data, TRUE);
425static BOOL vgids_prepare_fstable(
const vgidsFilesysTableEntry* fstable, DWORD numEntries,
426 BYTE** outData, DWORD* outDataSize)
432 BYTE* data = malloc(
sizeof(vgidsFilesysTableEntry) * numEntries + 1);
435 WLog_ERR(TAG,
"Failed to allocate filesystem table data blob");
440 for (UINT32 i = 0; i < numEntries; ++i)
441 memcpy(data + 1 + (
sizeof(vgidsFilesysTableEntry) * i), &fstable[i],
442 sizeof(vgidsFilesysTableEntry));
445 *outDataSize =
sizeof(vgidsFilesysTableEntry) * numEntries + 1;
451static BOOL vgids_prepare_certificate(
const rdpCertificate* cert, BYTE** kxc, DWORD* kxcSize)
460 BYTE* comprData =
nullptr;
465 BYTE* certData = freerdp_certificate_get_der(cert, &certSize);
466 if (!certData || (certSize == 0) || (certSize > UINT16_MAX))
468 WLog_ERR(TAG,
"Failed to get certificate size");
472 comprData = malloc(certSize);
475 WLog_ERR(TAG,
"Failed to allocate certificate buffer");
480 destSize = WINPR_ASSERTING_INT_CAST(uint16_t, certSize);
481 if (compress(comprData, &destSize, certData, WINPR_ASSERTING_INT_CAST(uint16_t, certSize)) !=
484 WLog_ERR(TAG,
"Failed to compress certificate data");
489 s = Stream_New(
nullptr, destSize + 4);
490 Stream_Write_UINT16(s, 0x0001);
491 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, certSize));
492 Stream_Write(s, comprData, destSize);
493 Stream_SealLength(s);
495 *kxc = Stream_Buffer(s);
496 *kxcSize = (DWORD)Stream_Length(s);
498 Stream_Free(s, FALSE);
504 Stream_Free(s, TRUE);
511static size_t get_rsa_key_size(
const rdpPrivateKey* privateKey)
513 WINPR_ASSERT(privateKey);
515 return freerdp_key_get_bits(privateKey) / 8;
519static BYTE vgids_get_algid(vgidsContext* p_Ctx)
523 switch (get_rsa_key_size(p_Ctx->privateKey))
526 return VGIDS_ALGID_RSA_1024;
528 return VGIDS_ALGID_RSA_2048;
530 return VGIDS_ALGID_RSA_3072;
532 return VGIDS_ALGID_RSA_4096;
534 WLog_ERR(TAG,
"Failed to determine algid for private key");
542static BOOL vgids_prepare_keymap(vgidsContext* context, BYTE** outData, DWORD* outDataSize)
548 BYTE* data =
nullptr;
549 vgidsKeymapRecord record = {
552 VGIDS_KEY_TYPE_KEYEXCHANGE,
553 (0xB000 | VGIDS_DEFAULT_KEY_REF),
559 BYTE algid = vgids_get_algid(context);
563 data = malloc(
sizeof(record) + 1);
566 WLog_ERR(TAG,
"Failed to allocate filesystem table data blob");
571 record.algid = algid;
572 memcpy(data + 1, &record,
sizeof(record));
575 *outDataSize =
sizeof(record) + 1;
581static BOOL vgids_parse_apdu_header(
wStream* s, BYTE* cla, BYTE* ins, BYTE* p1, BYTE* p2, BYTE* lc,
584 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
589 Stream_Read_UINT8(s, *cla);
593 Stream_Read_UINT8(s, *ins);
597 Stream_Read_UINT8(s, *p1);
601 Stream_Read_UINT8(s, *p2);
608 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
611 Stream_Read_UINT8(s, *lc);
612 if (!Stream_CheckAndLogRequiredLength(TAG, s, *lc))
619 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
621 Stream_Read_UINT8(s, *le);
628static BOOL vgids_create_response(UINT16 status,
const BYTE* answer, DWORD answerSize,
629 BYTE** outData, DWORD* outDataSize)
631 BYTE* out = malloc(answerSize + 2);
634 WLog_ERR(TAG,
"Failed to allocate memory for response data");
641 memcpy(out, answer, answerSize);
645 *out = (BYTE)((status >> 8) & 0xFF);
646 *(out + 1) = (BYTE)(status & 0xFF);
647 *outDataSize = answerSize + 2;
652static BOOL vgids_read_do_fkt(
void* data,
size_t index, va_list ap)
654 BYTE* response =
nullptr;
655 DWORD responseSize = 0;
656 vgidsEF* file = (vgidsEF*)data;
657 vgidsContext* context = va_arg(ap, vgidsContext*);
658 UINT16 efID = (UINT16)va_arg(ap,
unsigned);
659 UINT16 doID = (UINT16)va_arg(ap,
unsigned);
662 if (efID == 0x3FFF || efID == file->id)
665 if (vgids_ef_read_do(file, doID, &response, &responseSize))
667 context->responseData = Stream_New(response, (
size_t)responseSize);
668 if (!context->responseData)
678static BOOL vgids_read_do(vgidsContext* context, UINT16 efID, UINT16 doID)
680 return ArrayList_ForEach(context->files, vgids_read_do_fkt, context, efID, doID);
683static void vgids_reset_context_response(vgidsContext* context)
685 Stream_Free(context->responseData, TRUE);
686 context->responseData =
nullptr;
689static void vgids_reset_context_command_data(vgidsContext* context)
691 Stream_Free(context->commandData, TRUE);
692 context->commandData =
nullptr;
696static BOOL vgids_ins_select(vgidsContext* context,
wStream* s, BYTE** response,
702 DWORD resultDataSize = 0;
703 const BYTE* resultData =
nullptr;
704 UINT16 status = ISO_STATUS_SUCCESS;
708 if (!vgids_parse_apdu_header(s,
nullptr,
nullptr, &p1, &p2, &lc,
nullptr))
718 BYTE aid[ISO_AID_MAX_SIZE] = WINPR_C_ARRAY_INIT;
719 if (lc > ISO_AID_MAX_SIZE)
721 WLog_ERR(TAG,
"The LC byte is greater than the maximum AID length");
722 status = ISO_STATUS_INVALIDLC;
727 Stream_Read(s, aid, lc);
728 if ((lc >
sizeof(g_MsGidsAID)) || (memcmp(aid, g_MsGidsAID, lc) != 0))
730 status = ISO_STATUS_FILENOTFOUND;
740 resultData = g_GidsAppFCI;
741 resultDataSize =
sizeof(g_GidsAppFCI);
747 resultData = g_GidsAppFCP;
748 resultDataSize =
sizeof(g_GidsAppFCP);
752 status = ISO_STATUS_INVALIDP1P2;
757 context->currentDF = ISO_FID_MF;
767 WLog_ERR(TAG,
"The LC byte for the file ID must be 2");
768 status = ISO_STATUS_INVALIDLC;
772 Stream_Read_UINT16_BE(s, fid);
773 if (fid != VGIDS_EFID_CURRENTDF || context->currentDF == 0)
775 status = ISO_STATUS_FILENOTFOUND;
783 status = ISO_STATUS_INVALIDP1P2;
788 return vgids_create_response(status, resultData, resultDataSize, response, responseSize);
792static UINT16 vgids_handle_chained_response(vgidsContext* context,
const BYTE** response,
796 UINT16 status = ISO_STATUS_SUCCESS;
797 DWORD remainingBytes = (DWORD)Stream_Length(context->responseData);
798 if (remainingBytes > 256)
800 status = ISO_STATUS_MORE_DATA;
801 remainingBytes = 256;
804 *response = Stream_Buffer(context->responseData);
805 *responseSize = remainingBytes;
806 Stream_Seek(context->responseData, remainingBytes);
810 remainingBytes = (DWORD)(Stream_Length(context->responseData) - remainingBytes);
811 if (remainingBytes < 256 && remainingBytes != 0)
812 status |= (remainingBytes & 0xFF);
817static BOOL vgids_get_public_key(vgidsContext* context, UINT16 doTag)
823 WINPR_ASSERT(context);
829 char* n = freerdp_certificate_get_param(context->certificate, FREERDP_CERT_RSA_N, &nSize);
830 char* e = freerdp_certificate_get_param(context->certificate, FREERDP_CERT_RSA_E, &eSize);
835 pubKey = Stream_New(
nullptr, nSize + eSize + 0x10);
838 WLog_ERR(TAG,
"Failed to allocate public key stream");
842 response = Stream_New(
nullptr, Stream_Capacity(pubKey) + 0x10);
845 WLog_ERR(TAG,
"Failed to allocate response stream");
850 if (!vgids_write_tlv(pubKey, 0x81, n, nSize))
853 if (!vgids_write_tlv(pubKey, 0x82, e, eSize))
857 if (!vgids_write_tlv(response, doTag, Stream_Buffer(pubKey), (DWORD)Stream_Length(pubKey)))
861 Stream_ResetPosition(response);
862 context->responseData = response;
869 Stream_Free(pubKey, TRUE);
870 Stream_Free(response, TRUE);
875static BOOL vgids_ins_getdata(vgidsContext* context,
wStream* s, BYTE** response,
883 DWORD resultDataSize = 0;
884 const BYTE* resultData =
nullptr;
885 UINT16 status = ISO_STATUS_SUCCESS;
891 if (!vgids_parse_apdu_header(s,
nullptr,
nullptr, &p1, &p2, &lc,
nullptr))
895 vgids_reset_context_response(context);
898 fileId = (UINT16)(((UINT16)p1 << 8) | p2);
907 Stream_Read_UINT8(s, tag);
908 Stream_Read_UINT8(s, length);
909 if (tag != 0x5C && length != 0x02)
911 status = ISO_STATUS_INVALIDCOMMANDDATA;
915 Stream_Read_UINT16_BE(s, doId);
919 if (!vgids_read_do(context, fileId, doId))
920 WLog_WARN(TAG,
"vgids_read_do failed");
931 if (p1 != 0x3F && p2 != 0xFF)
933 status = ISO_STATUS_INVALIDP1P2;
938 Stream_Read_UINT8(s, tag);
939 Stream_Read_UINT8(s, length);
940 if (tag != 0x70 || length != 0x08)
942 status = ISO_STATUS_INVALIDCOMMANDDATA;
947 Stream_Read_UINT8(s, tag);
948 Stream_Read_UINT8(s, length);
949 Stream_Read_UINT8(s, keyRef);
950 if (tag != 0x84 || length != 0x01 || keyRef != VGIDS_DEFAULT_KEY_REF)
952 status = ISO_STATUS_INVALIDCOMMANDDATA;
957 Stream_Read_UINT8(s, tag);
958 Stream_Read_UINT8(s, length);
959 if (tag != 0xA5 || length != 0x03)
961 status = ISO_STATUS_INVALIDCOMMANDDATA;
965 Stream_Read_UINT16_BE(s, pubKeyDO);
966 Stream_Read_UINT8(s, length);
967 if (pubKeyDO != 0x7F49 || length != 0x80)
969 status = ISO_STATUS_INVALIDCOMMANDDATA;
973 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
975 status = ISO_STATUS_INVALIDLC;
980 if (!vgids_get_public_key(context, pubKeyDO))
985 status = ISO_STATUS_INVALIDCOMMANDDATA;
990 if (context->responseData)
991 status = vgids_handle_chained_response(context, &resultData, &resultDataSize);
992 else if (status == ISO_STATUS_SUCCESS)
993 status = ISO_STATUS_REFERENCEDATANOTFOUND;
995 return vgids_create_response(status, resultData, resultDataSize, response, responseSize);
999static BOOL vgids_ins_manage_security_environment(vgidsContext* context,
wStream* s,
1000 BYTE** response, DWORD* responseSize)
1007 DWORD resultDataSize = 0;
1008 const BYTE* resultData =
nullptr;
1009 UINT16 status = ISO_STATUS_SUCCESS;
1011 vgids_reset_context_command_data(context);
1012 vgids_reset_context_response(context);
1015 if (!vgids_parse_apdu_header(s,
nullptr,
nullptr, &p1, &p2, &lc,
nullptr))
1021 if (p1 != 0x41 && p2 != 0xB6 && p2 != 0xB8)
1023 status = ISO_STATUS_INVALIDP1P2;
1024 goto create_response;
1029 status = ISO_STATUS_WRONGLC;
1030 goto create_response;
1033 context->currentSE.crt = p2;
1037 Stream_Read_UINT8(s, tag);
1038 Stream_Read_UINT8(s, length);
1039 if (tag != 0x80 || length != 0x01)
1041 status = ISO_STATUS_INVALIDCOMMANDDATA;
1042 goto create_response;
1044 Stream_Read_UINT8(s, context->currentSE.algoId);
1047 Stream_Read_UINT8(s, tag);
1048 Stream_Read_UINT8(s, length);
1049 if (tag != 0x84 || length != 0x01)
1051 status = ISO_STATUS_INVALIDCOMMANDDATA;
1052 goto create_response;
1054 Stream_Read_UINT8(s, context->currentSE.keyRef);
1058 if (status != ISO_STATUS_SUCCESS)
1059 memset(&context->currentSE, 0,
sizeof(context->currentSE));
1060 return vgids_create_response(status, resultData, resultDataSize, response, responseSize);
1064static BOOL vgids_perform_digital_signature(vgidsContext* context)
1068 EVP_PKEY_CTX* ctx =
nullptr;
1069 EVP_PKEY* pk = freerdp_key_get_evp_pkey(context->privateKey);
1070 const vgidsDigestInfoMap gidsDigestInfo[VGIDS_MAX_DIGEST_INFO] = {
1071 { g_PKCS1_SHA1,
sizeof(g_PKCS1_SHA1), EVP_sha1() },
1072 { g_PKCS1_SHA224,
sizeof(g_PKCS1_SHA224), EVP_sha224() },
1073 { g_PKCS1_SHA256,
sizeof(g_PKCS1_SHA256), EVP_sha256() },
1074 { g_PKCS1_SHA384,
sizeof(g_PKCS1_SHA384), EVP_sha384() },
1075 { g_PKCS1_SHA512,
sizeof(g_PKCS1_SHA512), EVP_sha512() },
1076#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1077 { g_PKCS1_SHA512_224,
sizeof(g_PKCS1_SHA512_224), EVP_sha512_224() },
1078 { g_PKCS1_SHA512_256,
sizeof(g_PKCS1_SHA512_256), EVP_sha512_256() }
1084 WLog_ERR(TAG,
"Failed to create PKEY");
1088 vgids_reset_context_response(context);
1091 Stream_ResetPosition(context->commandData);
1092 for (
int i = 0; i < VGIDS_MAX_DIGEST_INFO; ++i)
1095 const vgidsDigestInfoMap* digest = &gidsDigestInfo[i];
1096 if (Stream_Length(context->commandData) >= digest->infoSize &&
1097 memcmp(Stream_Buffer(context->commandData), digest->info, digest->infoSize) == 0)
1100 Stream_Seek(context->commandData, digest->infoSize);
1101 if (!Stream_CheckAndLogRequiredLength(TAG, context->commandData, 2))
1103 msgSize = Stream_GetRemainingLength(context->commandData);
1106 ctx = EVP_PKEY_CTX_new(pk,
nullptr);
1109 WLog_ERR(TAG,
"Failed to create signing context");
1113 if (EVP_PKEY_sign_init(ctx) <= 0)
1115 WLog_ERR(TAG,
"Failed to init signing context");
1120 if (context->currentSE.algoId & VGIDS_SE_ALGOID_DST_PAD_PKCS1)
1122 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
1124 WLog_ERR(TAG,
"Failed to set padding mode");
1129 if (EVP_PKEY_CTX_set_signature_md(ctx, digest->digest) <= 0)
1131 WLog_ERR(TAG,
"Failed to set signing mode");
1136 if (EVP_PKEY_sign(ctx,
nullptr, &sigSize, Stream_Pointer(context->commandData),
1139 WLog_ERR(TAG,
"Failed to determine signature size");
1143 context->responseData = Stream_New(
nullptr, sigSize);
1144 if (!context->responseData)
1146 WLog_ERR(TAG,
"Failed to allocate signing buffer");
1151 if (EVP_PKEY_sign(ctx, Stream_Buffer(context->responseData), &sigSize,
1152 Stream_Pointer(context->commandData), msgSize) <= 0)
1154 WLog_ERR(TAG,
"Failed to create signature");
1158 if (!Stream_SetLength(context->responseData, sigSize))
1161 EVP_PKEY_CTX_free(ctx);
1167 vgids_reset_context_command_data(context);
1171 vgids_reset_context_command_data(context);
1172 vgids_reset_context_response(context);
1173 EVP_PKEY_CTX_free(ctx);
1179static BOOL vgids_perform_decrypt(vgidsContext* context)
1181 EVP_PKEY_CTX* ctx =
nullptr;
1184 int padding = RSA_NO_PADDING;
1186 vgids_reset_context_response(context);
1189 if (context->currentSE.algoId & VGIDS_SE_ALGOID_CT_PAD_PKCS1)
1190 padding = RSA_PKCS1_PADDING;
1191 else if (context->currentSE.algoId & VGIDS_SE_ALGOID_CT_PAD_OAEP)
1192 padding = RSA_PKCS1_OAEP_PADDING;
1195 EVP_PKEY* pkey = freerdp_key_get_evp_pkey(context->privateKey);
1197 goto decrypt_failed;
1198 ctx = EVP_PKEY_CTX_new(pkey,
nullptr);
1200 goto decrypt_failed;
1201 if (EVP_PKEY_decrypt_init(ctx) <= 0)
1202 goto decrypt_failed;
1203 if (EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0)
1204 goto decrypt_failed;
1208 const size_t inlen = Stream_Length(context->commandData);
1210 res = EVP_PKEY_decrypt(ctx,
nullptr, &outlen, Stream_Buffer(context->commandData), inlen);
1213 WLog_ERR(TAG,
"Failed to decrypt data");
1214 goto decrypt_failed;
1218 context->responseData = Stream_New(
nullptr, outlen);
1220 if (!context->responseData)
1222 WLog_ERR(TAG,
"Failed to create decryption buffer");
1223 goto decrypt_failed;
1227 res = EVP_PKEY_decrypt(ctx, Stream_Buffer(context->responseData), &outlen,
1228 Stream_Buffer(context->commandData), inlen);
1232 WLog_ERR(TAG,
"Failed to decrypt data");
1233 goto decrypt_failed;
1236 rc = Stream_SetLength(context->responseData, outlen);
1240 EVP_PKEY_CTX_free(ctx);
1241 EVP_PKEY_free(pkey);
1242 vgids_reset_context_command_data(context);
1244 vgids_reset_context_response(context);
1249static BOOL vgids_ins_perform_security_operation(vgidsContext* context,
wStream* s, BYTE** response,
1250 DWORD* responseSize)
1256 DWORD resultDataSize = 0;
1257 const BYTE* resultData =
nullptr;
1258 UINT16 status = ISO_STATUS_SUCCESS;
1261 if (!vgids_parse_apdu_header(s, &cla,
nullptr, &p1, &p2, &lc,
nullptr))
1266 status = ISO_STATUS_WRONGLC;
1267 goto create_response;
1271 if (context->currentSE.keyRef != VGIDS_DEFAULT_KEY_REF)
1273 status = ISO_STATUS_SECURITYSTATUSNOTSATISFIED;
1274 goto create_response;
1278 if (!context->pinVerified)
1280 status = ISO_STATUS_SECURITYSTATUSNOTSATISFIED;
1281 goto create_response;
1285 if (!context->commandData)
1287 context->commandData = Stream_New(
nullptr, lc);
1288 if (!context->commandData)
1291 else if (!Stream_EnsureRemainingCapacity(context->commandData, lc))
1294 Stream_Write(context->commandData, Stream_Pointer(s), lc);
1295 Stream_SealLength(context->commandData);
1298 switch (context->currentSE.crt)
1300 case VGIDS_SE_CRT_SIGN:
1302 if (p1 != 0x9E || p2 != 0x9A)
1304 status = ISO_STATUS_INVALIDP1P2;
1311 if (!vgids_perform_digital_signature(context))
1316 case VGIDS_SE_CRT_CONF:
1318 if ((p1 != 0x86 || p2 != 0x80) && (p1 != 0x80 || p2 != 0x86))
1320 status = ISO_STATUS_INVALIDP1P2;
1327 if (!vgids_perform_decrypt(context))
1333 status = ISO_STATUS_INVALIDP1P2;
1338 if (status == ISO_STATUS_SUCCESS && context->responseData)
1339 status = vgids_handle_chained_response(context, &resultData, &resultDataSize);
1343 return vgids_create_response(status, resultData, resultDataSize, response, responseSize);
1347static BOOL vgids_ins_getresponse(vgidsContext* context,
wStream* s, BYTE** response,
1348 DWORD* responseSize)
1353 DWORD resultDataSize = 0;
1354 const BYTE* resultData =
nullptr;
1355 DWORD expectedLen = 0;
1356 DWORD remainingSize = 0;
1357 UINT16 status = ISO_STATUS_SUCCESS;
1361 if (!context->responseData || !Stream_CheckAndLogRequiredLength(TAG, context->responseData, 1))
1363 status = ISO_STATUS_COMMANDNOTALLOWED;
1364 goto create_response;
1367 if (!vgids_parse_apdu_header(s,
nullptr,
nullptr, &p1, &p2,
nullptr, &le))
1371 if (p1 != 00 || p2 != 0x00)
1373 status = ISO_STATUS_INVALIDP1P2;
1374 goto create_response;
1379 if (expectedLen == 0)
1383 remainingSize = (DWORD)Stream_GetRemainingLength(context->responseData);
1384 if (remainingSize < expectedLen)
1385 expectedLen = remainingSize;
1387 resultData = Stream_Pointer(context->responseData);
1388 resultDataSize = expectedLen;
1389 Stream_Seek(context->responseData, expectedLen);
1392 remainingSize = (DWORD)Stream_GetRemainingLength(context->responseData);
1393 if (remainingSize > 0)
1395 status = ISO_STATUS_MORE_DATA;
1396 if (remainingSize < 256)
1397 status |= (remainingSize & 0xFF);
1401 return vgids_create_response(status, resultData, resultDataSize, response, responseSize);
1405static BOOL vgids_ins_verify(vgidsContext* context,
wStream* s, BYTE** response,
1406 DWORD* responseSize)
1412 UINT16 status = ISO_STATUS_SUCCESS;
1413 char pin[VGIDS_MAX_PIN_SIZE + 1] = WINPR_C_ARRAY_INIT;
1416 if (!vgids_parse_apdu_header(s,
nullptr, &ins, &p1, &p2,
nullptr,
nullptr))
1420 if (p1 != 00 && p2 != 0x80 && p2 != 0x82)
1422 status = ISO_STATUS_INVALIDP1P2;
1423 goto create_response;
1429 context->pinVerified = FALSE;
1430 goto create_response;
1434 if (Emulate_IsPinBlocked(context->emuContext, context->name))
1436 status = ISO_STATUS_AUTHMETHODBLOCKED;
1437 goto create_response;
1441 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
1443 status = ISO_STATUS_INVALIDLC;
1444 goto create_response;
1447 Stream_Read_UINT8(s, lc);
1448 if (!Stream_CheckAndLogRequiredLength(TAG, s, lc) || (lc > VGIDS_MAX_PIN_SIZE))
1450 status = ISO_STATUS_INVALIDLC;
1451 goto create_response;
1455 Stream_Read(s, pin, lc);
1457 UINT16 remaining = 0;
1458 if (!Emulate_IsPinValid(context->emuContext, context->name, pin, lc, &remaining))
1461 context->pinVerified = FALSE;
1462 status = (ISO_STATUS_VERIFYFAILED | (remaining & 0xFF));
1467 context->pinVerified = TRUE;
1471 return vgids_create_response(status,
nullptr, 0, response, responseSize);
1474vgidsContext* vgids_new(SmartcardEmulationContext* context)
1479 vgidsContext* ctx = calloc(1,
sizeof(vgidsContext));
1482 ctx->emuContext = context;
1484 ctx->files = ArrayList_New(FALSE);
1487 WLog_ERR(TAG,
"Failed to create files array list");
1491 wObject* obj = ArrayList_Object(ctx->files);
1492 obj->fnObjectFree = vgids_ef_free;
1501BOOL vgids_init(vgidsContext* ctx,
const char* cert,
const char* privateKey,
const char* pin)
1504 DWORD keymapSize = 0;
1505 DWORD fsTableSize = 0;
1507 BYTE* kxc =
nullptr;
1508 BYTE* keymap =
nullptr;
1509 BYTE* fsTable =
nullptr;
1510 vgidsEF* masterEF =
nullptr;
1511 vgidsEF* cardidEF =
nullptr;
1512 vgidsEF* commonEF =
nullptr;
1513 BYTE cardid[VGIDS_CARDID_SIZE] = WINPR_C_ARRAY_INIT;
1514 vgidsContainerMapEntry cmrec = { {
'P',
'r',
'i',
'v',
'a',
't',
'e',
' ',
'K',
'e',
'y',
' ',
1516 CONTAINER_MAP_VALID_CONTAINER |
1517 CONTAINER_MAP_DEFAULT_CONTAINER,
1521 vgidsFilesysTableEntry filesys[] = {
1522 {
"mscp",
"", 0, 0, 0, 0xA000, 0 },
1523 {
"",
"cardid", 0, 0xDF20, 0, 0xA012, 0 },
1524 {
"",
"cardapps", 0, 0xDF21, 0, 0xA010, 0 },
1525 {
"",
"cardcf", 0, 0xDF22, 0, 0xA010, 0 },
1526 {
"mscp",
"cmapfile", 0, 0xDF23, 0, 0xA010, 0 },
1527 {
"mscp",
"kxc00", 0, 0xDF24, 0, 0xA010, 0 },
1531 if (!cert || !privateKey || !pin)
1533 WLog_DBG(TAG,
"Passed invalid nullptr argument: cert=%p, privateKey=%p, pin=%p",
1534 WINPR_CXX_COMPAT_CAST(
const void*, cert),
1535 WINPR_CXX_COMPAT_CAST(
const void*, privateKey),
1536 WINPR_CXX_COMPAT_CAST(
const void*, pin));
1541 ctx->certificate = freerdp_certificate_new_from_pem(cert);
1542 if (!ctx->certificate)
1545 ctx->privateKey = freerdp_key_new_from_pem_enc(privateKey,
nullptr);
1546 if (!ctx->privateKey)
1549 ctx->name = freerdp_certificate_get_fingerprint(ctx->certificate);
1555 masterEF = vgids_ef_new(ctx, VGIDS_EFID_MASTER);
1561 cardidEF = vgids_ef_new(ctx, VGIDS_EFID_CARDID);
1564 if (winpr_RAND(cardid,
sizeof(cardid)) < 0)
1566 if (!vgids_ef_write_do(cardidEF, VGIDS_DO_CARDID, cardid,
sizeof(cardid)))
1571 commonEF = vgids_ef_new(ctx, VGIDS_EFID_COMMON);
1576 if (!vgids_ef_write_do(commonEF, VGIDS_DO_CARDCF, g_CardCFContents,
sizeof(g_CardCFContents)))
1581 const size_t size = get_rsa_key_size(ctx->privateKey);
1582 if ((size == 0) || (size > UINT16_MAX / 8))
1585 cmrec.wKeyExchangeKeySizeBits = (WORD)size * 8;
1587 if (!vgids_ef_write_do(commonEF, VGIDS_DO_CMAPFILE, &cmrec,
sizeof(cmrec)))
1591 if (!vgids_ef_write_do(commonEF, VGIDS_DO_CARDAPPS, g_CardAppsContents,
1592 sizeof(g_CardAppsContents)))
1596 if (!vgids_prepare_certificate(ctx->certificate, &kxc, &kxcSize))
1598 if (!vgids_ef_write_do(commonEF, VGIDS_DO_KXC00, kxc, kxcSize))
1602 if (!vgids_prepare_fstable(filesys, ARRAYSIZE(filesys), &fsTable, &fsTableSize))
1604 if (!vgids_ef_write_do(masterEF, VGIDS_DO_FILESYSTEMTABLE, fsTable, fsTableSize))
1608 if (!vgids_prepare_keymap(ctx, &keymap, &keymapSize))
1610 if (!vgids_ef_write_do(masterEF, VGIDS_DO_KEYMAP, keymap, keymapSize))
1614 if (!Emulate_SetupPin(ctx->emuContext, ctx->name, pin))
1629BOOL vgids_process_apdu(vgidsContext* context,
const BYTE* data, DWORD dataSize, BYTE** response,
1630 DWORD* responseSize)
1636 if (!context || !data || !response || !responseSize)
1638 WLog_ERR(TAG,
"Invalid nullptr pointer passed");
1644 WLog_ERR(TAG,
"APDU buffer is less than 4 bytes: %" PRIu32, dataSize);
1649 Stream_StaticConstInit(&s, data, dataSize);
1654 case ISO_INS_SELECT:
1655 return vgids_ins_select(context, &s, response, responseSize);
1656 case ISO_INS_GETDATA:
1657 return vgids_ins_getdata(context, &s, response, responseSize);
1658 case ISO_INS_GETRESPONSE:
1659 return vgids_ins_getresponse(context, &s, response, responseSize);
1661 return vgids_ins_manage_security_environment(context, &s, response, responseSize);
1663 return vgids_ins_perform_security_operation(context, &s, response, responseSize);
1664 case ISO_INS_VERIFY:
1665 return vgids_ins_verify(context, &s, response, responseSize);
1671 return vgids_create_response(ISO_STATUS_COMMANDNOTALLOWED,
nullptr, 0, response, responseSize);
1674void vgids_free(vgidsContext* context)
1678 freerdp_key_free(context->privateKey);
1679 freerdp_certificate_free(context->certificate);
1680 Stream_Free(context->commandData, TRUE);
1681 Stream_Free(context->responseData, TRUE);
1682 winpr_zfree(context->name);
1683 ArrayList_Free(context->files);
This struct contains function pointer to initialize/free objects.