23 |
#include "CryptoOperations.h" |
#include "CryptoOperations.h" |
24 |
#include "PinOperations.h" |
#include "PinOperations.h" |
25 |
#include "PublicDataOperations.h" |
#include "PublicDataOperations.h" |
26 |
|
#include "tlv.h" |
27 |
|
|
28 |
OPENPGP_CONTAINER_INFO Containers[] = |
OPENPGP_CONTAINER_INFO Containers[] = |
29 |
{ |
{ |
85 |
BYTE modulus[sizeof(DWORD)]; |
BYTE modulus[sizeof(DWORD)]; |
86 |
} RSAPUBLICKEYBLOB, *PRSAPUBLICKEYBLOB; |
} RSAPUBLICKEYBLOB, *PRSAPUBLICKEYBLOB; |
87 |
|
|
|
DWORD getTlvSize(__in PBYTE pbPointer, __in PDWORD pdwOffset) |
|
|
{ |
|
|
DWORD dwSize; |
|
|
switch(*pbPointer) |
|
|
{ |
|
|
case 0x81: |
|
|
*pdwOffset+=2; |
|
|
dwSize = pbPointer[1]; |
|
|
break; |
|
|
case 0x82: |
|
|
*pdwOffset+=3; |
|
|
dwSize = pbPointer[1] * 0x100 + pbPointer[2]; |
|
|
break; |
|
|
default: |
|
|
dwSize = *pbPointer; |
|
|
*pdwOffset+=1; |
|
|
break; |
|
|
} |
|
|
return dwSize; |
|
|
} |
|
|
|
|
|
BOOL find_tlv(__in PBYTE pbData, __in BYTE bCode, __out PBYTE *pbDataOut, __out_opt PDWORD pdwSize) |
|
|
{ |
|
|
DWORD dwOffset = 2; |
|
|
DWORD dwSize; |
|
|
DWORD dwTotalSize = getTlvSize(pbData + 2,&dwOffset) + 2; |
|
|
while (dwOffset < dwTotalSize) |
|
|
{ |
|
|
if (bCode == pbData[dwOffset]) |
|
|
{ |
|
|
dwOffset++; |
|
|
// size sequence |
|
|
dwSize = getTlvSize(pbData + dwOffset,&dwOffset); |
|
|
if (pdwSize) |
|
|
{ |
|
|
*pdwSize = dwSize; |
|
|
} |
|
|
*pbDataOut = pbData + dwOffset; |
|
|
return TRUE; |
|
|
} |
|
|
else |
|
|
{ |
|
|
dwOffset++; |
|
|
dwSize = getTlvSize(pbData + dwOffset,&dwOffset); |
|
|
dwOffset += dwSize; |
|
|
} |
|
|
} |
|
|
return FALSE; |
|
|
} |
|
|
|
|
88 |
|
|
89 |
DWORD GetKeyAlgorithmAttributes(__in PCARD_DATA pCardData, |
DWORD GetKeyAlgorithmAttributes(__in PCARD_DATA pCardData, |
90 |
__in OPENPGP_CONTAINER dwContainer, |
__in OPENPGP_CONTAINER dwContainer, |
547 |
DWORD dwModulusSize, dwI; |
DWORD dwModulusSize, dwI; |
548 |
PBYTE pbExponent; |
PBYTE pbExponent; |
549 |
PRSAPUBLICKEYBLOB pbBlob = NULL; |
PRSAPUBLICKEYBLOB pbBlob = NULL; |
550 |
|
DWORD dwTotalTlvSize, dwOffset; |
551 |
__try |
__try |
552 |
{ |
{ |
553 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",dwContainer); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",dwContainer); |
562 |
dwCmdSize = 9; |
dwCmdSize = 9; |
563 |
if (pContext->fExtentedLeLcFields) |
if (pContext->fExtentedLeLcFields) |
564 |
{ |
{ |
565 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength / 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength / 0x100); |
566 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength % 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength % 0x100); |
567 |
} |
} |
568 |
else |
else |
569 |
{ |
{ |
575 |
{ |
{ |
576 |
__leave; |
__leave; |
577 |
} |
} |
578 |
if (!find_tlv(pbData,0x81,&pbModulus,&dwModulusSize)) |
dwOffset = 2; |
579 |
|
dwTotalTlvSize = getTlvSize(pbData + 2,&dwOffset) + 2; |
580 |
|
if (!find_tlv(pbData + dwOffset,0x81,dwTotalTlvSize,&pbModulus,&dwModulusSize)) |
581 |
{ |
{ |
582 |
dwReturn = SCARD_E_UNEXPECTED; |
dwReturn = SCARD_E_UNEXPECTED; |
583 |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
584 |
__leave; |
__leave; |
585 |
} |
} |
586 |
if (!find_tlv(pbData,0x82,(PBYTE*)&pbExponent,NULL)) |
if (!find_tlv(pbData + dwOffset,0x82,dwTotalTlvSize, (PBYTE*)&pbExponent,NULL)) |
587 |
{ |
{ |
588 |
dwReturn = SCARD_E_UNEXPECTED; |
dwReturn = SCARD_E_UNEXPECTED; |
589 |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
645 |
0x00, |
0x00, |
646 |
0x00 |
0x00 |
647 |
}; |
}; |
648 |
|
DWORD dwTotalTlvSize, dwOffset; |
649 |
__try |
__try |
650 |
{ |
{ |
651 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",dwContainer); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",dwContainer); |
657 |
} |
} |
658 |
// key len |
// key len |
659 |
dwReturn = GetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes); |
dwReturn = GetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes); |
660 |
if (dwReturn == SCARD_E_FILE_NOT_FOUND) |
if (dwReturn) |
|
{ |
|
|
Attributes.bAlgoId = 0x01; |
|
|
Attributes.bFormat = 0; |
|
|
Attributes.wExponentLength = 0x20; |
|
|
} |
|
|
else if (dwReturn) |
|
661 |
{ |
{ |
662 |
__leave; |
__leave; |
663 |
} |
} |
673 |
dwCmdSize = 9; |
dwCmdSize = 9; |
674 |
if (pContext->fExtentedLeLcFields) |
if (pContext->fExtentedLeLcFields) |
675 |
{ |
{ |
676 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength / 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength / 0x100); |
677 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength % 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength % 0x100); |
678 |
} |
} |
679 |
else |
else |
680 |
{ |
{ |
686 |
{ |
{ |
687 |
__leave; |
__leave; |
688 |
} |
} |
689 |
if (!find_tlv(pbData,0x81,&pbModulus,&dwModulusSize)) |
dwOffset = 2; |
690 |
|
dwTotalTlvSize = getTlvSize(pbData + 2,&dwOffset) + 2; |
691 |
|
if (!find_tlv(pbData + dwOffset,0x81,dwTotalTlvSize, &pbModulus,&dwModulusSize)) |
692 |
{ |
{ |
693 |
dwReturn = SCARD_E_UNEXPECTED; |
dwReturn = SCARD_E_UNEXPECTED; |
694 |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
695 |
__leave; |
__leave; |
696 |
} |
} |
697 |
if (!find_tlv(pbData,0x82,(PBYTE*)&pbExponent,NULL)) |
if (!find_tlv(pbData + dwOffset,0x82,dwTotalTlvSize, (PBYTE*)&pbExponent,NULL)) |
698 |
{ |
{ |
699 |
dwReturn = SCARD_E_UNEXPECTED; |
dwReturn = SCARD_E_UNEXPECTED; |
700 |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_UNEXPECTED 0x81"); |
762 |
} |
} |
763 |
|
|
764 |
dwReturn = GetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes); |
dwReturn = GetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes); |
765 |
if (dwReturn == SCARD_E_FILE_NOT_FOUND) |
if (dwReturn) |
|
{ |
|
|
Attributes.bAlgoId = 0x01; |
|
|
Attributes.bFormat = 0; |
|
|
Attributes.wExponentLength = 0x20; |
|
|
} |
|
|
else if (dwReturn) |
|
766 |
{ |
{ |
767 |
__leave; |
__leave; |
768 |
} |
} |
913 |
|
|
914 |
if (pContext->fExtentedLeLcFields) |
if (pContext->fExtentedLeLcFields) |
915 |
{ |
{ |
916 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength / 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength / 0x100); |
917 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength % 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength % 0x100); |
918 |
} |
} |
919 |
else |
else |
920 |
{ |
{ |
1023 |
|
|
1024 |
if (pContext->fExtentedLeLcFields) |
if (pContext->fExtentedLeLcFields) |
1025 |
{ |
{ |
1026 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength / 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength / 0x100); |
1027 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength % 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength % 0x100); |
1028 |
} |
} |
1029 |
else |
else |
1030 |
{ |
{ |
1109 |
dwCmdSize += pInfo->cbData; |
dwCmdSize += pInfo->cbData; |
1110 |
if (pContext->fExtentedLeLcFields) |
if (pContext->fExtentedLeLcFields) |
1111 |
{ |
{ |
1112 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength / 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength / 0x100); |
1113 |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxLength % 0x100); |
pbCmd[dwCmdSize++] = (BYTE)(pContext->dwMaxCommandDataLength % 0x100); |
1114 |
} |
} |
1115 |
else |
else |
1116 |
{ |
{ |