2 using System.Runtime.InteropServices;
1172 public const int OK = 0 ;
1245 processStatus(wrapped_Init());
1250 processStatus(wrapped_End());
1259 IntPtr cryptQueryInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(
CRYPT_QUERY_INFO)));
1263 processStatus(wrapped_QueryCapability(cryptAlgo, cryptQueryInfoPtr));
1264 Marshal.PtrToStructure(cryptQueryInfoPtr, cryptQueryInfo);
1265 return cryptQueryInfo;
1269 Marshal.FreeHGlobal(cryptQueryInfoPtr);
1280 IntPtr cryptContextPtr = Marshal.AllocHGlobal(4);
1283 processStatus(wrapped_CreateContext(cryptContextPtr, cryptUser, cryptAlgo));
1284 return Marshal.ReadInt32(cryptContextPtr);
1288 Marshal.FreeHGlobal(cryptContextPtr);
1296 processStatus(wrapped_DestroyContext(cryptContext));
1305 processStatus(wrapped_DestroyObject(cryptObject));
1314 processStatus(wrapped_GenerateKey(cryptContext));
1321 processStatus(wrapped_GenerateKeyAsync(cryptContext));
1328 processStatus(wrapped_AsyncQuery(cryptObject));
1335 processStatus(wrapped_AsyncCancel(cryptObject));
1347 GCHandle bufferHandle =
new GCHandle();
1348 IntPtr bufferPtr = IntPtr.Zero;
1351 checkIndices(buffer, bufferOffset, length);
1352 getPointer(buffer, bufferOffset, ref bufferHandle, ref bufferPtr);
1353 processStatus(wrapped_Encrypt(cryptContext, bufferPtr, length));
1357 releasePointer(bufferHandle);
1363 ) {
Encrypt(cryptContext, buffer, 0, buffer == null ? 0 : buffer.Length); }
1372 GCHandle bufferHandle =
new GCHandle();
1373 IntPtr bufferPtr = IntPtr.Zero;
1376 checkIndices(buffer, bufferOffset, length);
1377 getPointer(buffer, bufferOffset, ref bufferHandle, ref bufferPtr);
1378 processStatus(wrapped_Decrypt(cryptContext, bufferPtr, length));
1382 releasePointer(bufferHandle);
1388 ) {
Decrypt(cryptContext, buffer, 0, buffer == null ? 0 : buffer.Length); }
1398 processStatus(wrapped_SetAttribute(cryptHandle, attributeType, value));
1409 GCHandle valueHandle =
new GCHandle();
1413 checkIndices(value, valueOffset, valueLength);
1414 getPointer(value, valueOffset, ref valueHandle, ref valuePtr);
1415 processStatus(wrapped_SetAttributeString(cryptHandle, attributeType, valuePtr, valueLength));
1419 releasePointer(valueHandle);
1426 ) {
SetAttributeString(cryptHandle, attributeType, value, 0, value == null ? 0 : value.Length); }
1431 ) {
SetAttributeString(cryptHandle, attributeType, value == null ? null :
new UTF8Encoding().GetBytes(value), 0, value == null ? 0 :
new UTF8Encoding().GetByteCount(value)); }
1438 IntPtr
valuePtr = Marshal.AllocHGlobal(4);
1441 processStatus(wrapped_GetAttribute(cryptHandle, attributeType, valuePtr));
1442 return Marshal.ReadInt32(valuePtr);
1446 Marshal.FreeHGlobal(valuePtr);
1457 IntPtr valueLengthPtr = Marshal.AllocHGlobal(4);
1458 GCHandle valueHandle =
new GCHandle();
1462 processStatus(wrapped_GetAttributeString(cryptHandle, attributeType, valuePtr, valueLengthPtr));
1463 int valueLength = Marshal.ReadInt32(valueLengthPtr);
1464 checkIndices(value, valueOffset, valueLength);
1465 getPointer(value, valueOffset, ref valueHandle, ref valuePtr);
1466 processStatus(wrapped_GetAttributeString(cryptHandle, attributeType, valuePtr, valueLengthPtr));
1467 return Marshal.ReadInt32(valueLengthPtr);
1471 Marshal.FreeHGlobal(valueLengthPtr);
1472 releasePointer(valueHandle);
1486 byte[] bytes =
new byte[
length];
1488 return new UTF8Encoding().GetString(bytes, 0, length);
1497 processStatus(wrapped_DeleteAttribute(cryptHandle, attributeType));
1506 int randomDataOffset,
1507 int randomDataLength
1510 GCHandle randomDataHandle =
new GCHandle();
1511 IntPtr randomDataPtr = IntPtr.Zero;
1514 checkIndices(randomData, randomDataOffset, randomDataLength);
1515 getPointer(randomData, randomDataOffset, ref randomDataHandle, ref randomDataPtr);
1516 processStatus(wrapped_AddRandom(randomDataPtr, randomDataLength));
1520 releasePointer(randomDataHandle);
1525 ) {
AddRandom(randomData, 0, randomData == null ? 0 : randomData.Length); }
1528 ) {
AddRandom(randomData == null ? null :
new UTF8Encoding().GetBytes(randomData), 0, randomData == null ? 0 :
new UTF8Encoding().GetByteCount(randomData)); }
1533 processStatus(wrapped_AddRandom(IntPtr.Zero, pollType));
1539 int objectDataOffset,
1543 IntPtr cryptObjectInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(
CRYPT_OBJECT_INFO)));
1545 GCHandle objectDataHandle =
new GCHandle();
1546 IntPtr objectDataPtr = IntPtr.Zero;
1549 checkIndices(objectData, objectDataOffset, objectDataLength);
1550 getPointer(objectData, objectDataOffset, ref objectDataHandle, ref objectDataPtr);
1551 processStatus(wrapped_QueryObject(objectDataPtr, objectDataLength, cryptObjectInfoPtr));
1552 Marshal.PtrToStructure(cryptObjectInfoPtr, cryptObjectInfo);
1557 Marshal.FreeHGlobal(cryptObjectInfoPtr);
1558 releasePointer(objectDataHandle);
1563 ) {
return QueryObject(objectData, 0, objectData == null ? 0 : objectData.Length); }
1566 ) {
return QueryObject(objectData == null ? null :
new UTF8Encoding().GetBytes(objectData), 0, objectData == null ? 0 :
new UTF8Encoding().GetByteCount(objectData)); }
1578 int encryptedKeyOffset,
1584 IntPtr encryptedKeyLengthPtr = Marshal.AllocHGlobal(4);
1585 GCHandle encryptedKeyHandle =
new GCHandle();
1586 IntPtr encryptedKeyPtr = IntPtr.Zero;
1589 processStatus(wrapped_ExportKey(encryptedKeyPtr, encryptedKeyMaxLength, encryptedKeyLengthPtr, exportKey, sessionKeyContext));
1591 checkIndices(encryptedKey, encryptedKeyOffset, encryptedKeyLength);
1592 getPointer(encryptedKey, encryptedKeyOffset, ref encryptedKeyHandle, ref encryptedKeyPtr);
1593 processStatus(wrapped_ExportKey(encryptedKeyPtr, encryptedKeyMaxLength, encryptedKeyLengthPtr, exportKey, sessionKeyContext));
1594 return Marshal.ReadInt32(encryptedKeyLengthPtr);
1598 Marshal.FreeHGlobal(encryptedKeyLengthPtr);
1599 releasePointer(encryptedKeyHandle);
1607 ) {
return ExportKey(encryptedKey, 0, encryptedKeyMaxLength, exportKey, sessionKeyContext); }
1611 int encryptedKeyOffset,
1618 IntPtr encryptedKeyLengthPtr = Marshal.AllocHGlobal(4);
1619 GCHandle encryptedKeyHandle =
new GCHandle();
1620 IntPtr encryptedKeyPtr = IntPtr.Zero;
1623 processStatus(wrapped_ExportKeyEx(encryptedKeyPtr, encryptedKeyMaxLength, encryptedKeyLengthPtr, formatType, exportKey, sessionKeyContext));
1625 checkIndices(encryptedKey, encryptedKeyOffset, encryptedKeyLength);
1626 getPointer(encryptedKey, encryptedKeyOffset, ref encryptedKeyHandle, ref encryptedKeyPtr);
1627 processStatus(wrapped_ExportKeyEx(encryptedKeyPtr, encryptedKeyMaxLength, encryptedKeyLengthPtr, formatType, exportKey, sessionKeyContext));
1628 return Marshal.ReadInt32(encryptedKeyLengthPtr);
1632 Marshal.FreeHGlobal(encryptedKeyLengthPtr);
1633 releasePointer(encryptedKeyHandle);
1642 ) {
return ExportKeyEx(encryptedKey, 0, encryptedKeyMaxLength, formatType, exportKey, sessionKeyContext); }
1646 int encryptedKeyOffset,
1652 GCHandle encryptedKeyHandle =
new GCHandle();
1653 IntPtr encryptedKeyPtr = IntPtr.Zero;
1656 checkIndices(encryptedKey, encryptedKeyOffset, encryptedKeyLength);
1657 getPointer(encryptedKey, encryptedKeyOffset, ref encryptedKeyHandle, ref encryptedKeyPtr);
1658 processStatus(wrapped_ImportKey(encryptedKeyPtr, encryptedKeyLength, importKey, sessionKeyContext));
1662 releasePointer(encryptedKeyHandle);
1669 ) {
ImportKey(encryptedKey, 0, encryptedKey == null ? 0 : encryptedKey.Length, importKey, sessionKeyContext); }
1674 ) {
ImportKey(encryptedKey == null ? null :
new UTF8Encoding().GetBytes(encryptedKey), 0, encryptedKey == null ? 0 :
new UTF8Encoding().GetByteCount(encryptedKey), importKey, sessionKeyContext); }
1678 int encryptedKeyOffset,
1684 IntPtr returnedContextPtr = Marshal.AllocHGlobal(4);
1685 GCHandle encryptedKeyHandle =
new GCHandle();
1686 IntPtr encryptedKeyPtr = IntPtr.Zero;
1689 checkIndices(encryptedKey, encryptedKeyOffset, encryptedKeyLength);
1690 getPointer(encryptedKey, encryptedKeyOffset, ref encryptedKeyHandle, ref encryptedKeyPtr);
1691 processStatus(wrapped_ImportKeyEx(encryptedKeyPtr, encryptedKeyLength, importKey, sessionKeyContext, returnedContextPtr));
1692 return Marshal.ReadInt32(returnedContextPtr);
1696 Marshal.FreeHGlobal(returnedContextPtr);
1697 releasePointer(encryptedKeyHandle);
1704 ) {
return ImportKeyEx(encryptedKey, 0, encryptedKey == null ? 0 : encryptedKey.Length, importKey, sessionKeyContext); }
1709 ) {
return ImportKeyEx(encryptedKey == null ? null :
new UTF8Encoding().GetBytes(encryptedKey), 0, encryptedKey == null ? 0 :
new UTF8Encoding().GetByteCount(encryptedKey), importKey, sessionKeyContext); }
1715 int signatureOffset,
1721 IntPtr signatureLengthPtr = Marshal.AllocHGlobal(4);
1722 GCHandle signatureHandle =
new GCHandle();
1723 IntPtr signaturePtr = IntPtr.Zero;
1726 processStatus(wrapped_CreateSignature(signaturePtr, signatureMaxLength, signatureLengthPtr, signContext, hashContext));
1728 checkIndices(signature, signatureOffset, signatureLength);
1729 getPointer(signature, signatureOffset, ref signatureHandle, ref signaturePtr);
1730 processStatus(wrapped_CreateSignature(signaturePtr, signatureMaxLength, signatureLengthPtr, signContext, hashContext));
1731 return Marshal.ReadInt32(signatureLengthPtr);
1735 Marshal.FreeHGlobal(signatureLengthPtr);
1736 releasePointer(signatureHandle);
1744 ) {
return CreateSignature(signature, 0, signatureMaxLength, signContext, hashContext); }
1748 int signatureOffset,
1756 IntPtr signatureLengthPtr = Marshal.AllocHGlobal(4);
1757 GCHandle signatureHandle =
new GCHandle();
1758 IntPtr signaturePtr = IntPtr.Zero;
1761 processStatus(wrapped_CreateSignatureEx(signaturePtr, signatureMaxLength, signatureLengthPtr, formatType, signContext, hashContext, extraData));
1763 checkIndices(signature, signatureOffset, signatureLength);
1764 getPointer(signature, signatureOffset, ref signatureHandle, ref signaturePtr);
1765 processStatus(wrapped_CreateSignatureEx(signaturePtr, signatureMaxLength, signatureLengthPtr, formatType, signContext, hashContext, extraData));
1766 return Marshal.ReadInt32(signatureLengthPtr);
1770 Marshal.FreeHGlobal(signatureLengthPtr);
1771 releasePointer(signatureHandle);
1781 ) {
return CreateSignatureEx(signature, 0, signatureMaxLength, formatType, signContext, hashContext, extraData); }
1785 int signatureOffset,
1791 GCHandle signatureHandle =
new GCHandle();
1792 IntPtr signaturePtr = IntPtr.Zero;
1795 checkIndices(signature, signatureOffset, signatureLength);
1796 getPointer(signature, signatureOffset, ref signatureHandle, ref signaturePtr);
1797 processStatus(wrapped_CheckSignature(signaturePtr, signatureLength, sigCheckKey, hashContext));
1801 releasePointer(signatureHandle);
1808 ) {
CheckSignature(signature, 0, signature == null ? 0 : signature.Length, sigCheckKey, hashContext); }
1813 ) {
CheckSignature(signature == null ? null :
new UTF8Encoding().GetBytes(signature), 0, signature == null ? 0 :
new UTF8Encoding().GetByteCount(signature), sigCheckKey, hashContext); }
1817 int signatureOffset,
1823 IntPtr extraDataPtr = Marshal.AllocHGlobal(4);
1824 GCHandle signatureHandle =
new GCHandle();
1825 IntPtr signaturePtr = IntPtr.Zero;
1828 checkIndices(signature, signatureOffset, signatureLength);
1829 getPointer(signature, signatureOffset, ref signatureHandle, ref signaturePtr);
1830 processStatus(wrapped_CheckSignatureEx(signaturePtr, signatureLength, sigCheckKey, hashContext, extraDataPtr));
1831 return Marshal.ReadInt32(extraDataPtr);
1835 Marshal.FreeHGlobal(extraDataPtr);
1836 releasePointer(signatureHandle);
1843 ) {
return CheckSignatureEx(signature, 0, signature == null ? 0 : signature.Length, sigCheckKey, hashContext); }
1848 ) {
return CheckSignatureEx(signature == null ? null :
new UTF8Encoding().GetBytes(signature), 0, signature == null ? 0 :
new UTF8Encoding().GetByteCount(signature), sigCheckKey, hashContext); }
1865 IntPtr keysetPtr = Marshal.AllocHGlobal(4);
1866 GCHandle nameHandle =
new GCHandle();
1867 IntPtr namePtr = IntPtr.Zero;
1868 byte[] nameArray =
new UTF8Encoding().GetBytes(name);
1871 getPointer(nameArray, 0, ref nameHandle, ref namePtr);
1872 processStatus(wrapped_KeysetOpen(keysetPtr, cryptUser, keysetType, namePtr, options));
1873 return Marshal.ReadInt32(keysetPtr);
1877 Marshal.FreeHGlobal(keysetPtr);
1878 releasePointer(nameHandle);
1886 processStatus(wrapped_KeysetClose(keyset));
1897 IntPtr cryptContextPtr = Marshal.AllocHGlobal(4);
1898 GCHandle keyIDHandle =
new GCHandle();
1899 IntPtr keyIDPtr = IntPtr.Zero;
1900 byte[] keyIDArray =
new UTF8Encoding().GetBytes(keyID);
1903 getPointer(keyIDArray, 0, ref keyIDHandle, ref keyIDPtr);
1904 processStatus(wrapped_GetPublicKey(keyset, cryptContextPtr, keyIDtype, keyIDPtr));
1905 return Marshal.ReadInt32(cryptContextPtr);
1909 Marshal.FreeHGlobal(cryptContextPtr);
1910 releasePointer(keyIDHandle);
1921 IntPtr cryptContextPtr = Marshal.AllocHGlobal(4);
1922 GCHandle keyIDHandle =
new GCHandle();
1923 IntPtr keyIDPtr = IntPtr.Zero;
1924 byte[] keyIDArray =
new UTF8Encoding().GetBytes(keyID);
1925 GCHandle passwordHandle =
new GCHandle();
1926 IntPtr passwordPtr = IntPtr.Zero;
1927 byte[] passwordArray =
new UTF8Encoding().GetBytes(password);
1930 getPointer(keyIDArray, 0, ref keyIDHandle, ref keyIDPtr);
1931 getPointer(passwordArray, 0, ref passwordHandle, ref passwordPtr);
1932 processStatus(wrapped_GetPrivateKey(keyset, cryptContextPtr, keyIDtype, keyIDPtr, passwordPtr));
1933 return Marshal.ReadInt32(cryptContextPtr);
1937 Marshal.FreeHGlobal(cryptContextPtr);
1938 releasePointer(keyIDHandle);
1939 releasePointer(passwordHandle);
1950 IntPtr cryptContextPtr = Marshal.AllocHGlobal(4);
1951 GCHandle keyIDHandle =
new GCHandle();
1952 IntPtr keyIDPtr = IntPtr.Zero;
1953 byte[] keyIDArray =
new UTF8Encoding().GetBytes(keyID);
1954 GCHandle passwordHandle =
new GCHandle();
1955 IntPtr passwordPtr = IntPtr.Zero;
1956 byte[] passwordArray =
new UTF8Encoding().GetBytes(password);
1959 getPointer(keyIDArray, 0, ref keyIDHandle, ref keyIDPtr);
1960 getPointer(passwordArray, 0, ref passwordHandle, ref passwordPtr);
1961 processStatus(wrapped_GetKey(keyset, cryptContextPtr, keyIDtype, keyIDPtr, passwordPtr));
1962 return Marshal.ReadInt32(cryptContextPtr);
1966 Marshal.FreeHGlobal(cryptContextPtr);
1967 releasePointer(keyIDHandle);
1968 releasePointer(passwordHandle);
1979 processStatus(wrapped_AddPublicKey(keyset, certificate));
1988 GCHandle passwordHandle =
new GCHandle();
1989 IntPtr passwordPtr = IntPtr.Zero;
1990 byte[] passwordArray =
new UTF8Encoding().GetBytes(password);
1993 getPointer(passwordArray, 0, ref passwordHandle, ref passwordPtr);
1994 processStatus(wrapped_AddPrivateKey(keyset, cryptKey, passwordPtr));
1998 releasePointer(passwordHandle);
2008 GCHandle keyIDHandle =
new GCHandle();
2009 IntPtr keyIDPtr = IntPtr.Zero;
2010 byte[] keyIDArray =
new UTF8Encoding().GetBytes(keyID);
2013 getPointer(keyIDArray, 0, ref keyIDHandle, ref keyIDPtr);
2014 processStatus(wrapped_DeleteKey(keyset, keyIDtype, keyIDPtr));
2018 releasePointer(keyIDHandle);
2035 IntPtr certificatePtr = Marshal.AllocHGlobal(4);
2038 processStatus(wrapped_CreateCert(certificatePtr, cryptUser, certType));
2039 return Marshal.ReadInt32(certificatePtr);
2043 Marshal.FreeHGlobal(certificatePtr);
2051 processStatus(wrapped_DestroyCert(certificate));
2062 int extensionOffset,
2066 IntPtr extensionLengthPtr = Marshal.AllocHGlobal(4);
2067 IntPtr criticalFlagPtr = Marshal.AllocHGlobal(4);
2068 GCHandle oidHandle =
new GCHandle();
2069 IntPtr oidPtr = IntPtr.Zero;
2070 byte[] oidArray =
new UTF8Encoding().GetBytes(oid);
2071 GCHandle extensionHandle =
new GCHandle();
2072 IntPtr extensionPtr = IntPtr.Zero;
2075 getPointer(oidArray, 0, ref oidHandle, ref oidPtr);
2076 processStatus(wrapped_GetCertExtension(certificate, oidPtr, criticalFlagPtr, extensionPtr, extensionMaxLength, extensionLengthPtr));
2078 checkIndices(extension, extensionOffset, extensionLength);
2079 getPointer(extension, extensionOffset, ref extensionHandle, ref extensionPtr);
2080 processStatus(wrapped_GetCertExtension(certificate, oidPtr, criticalFlagPtr, extensionPtr, extensionMaxLength, extensionLengthPtr));
2081 return Marshal.ReadInt32(extensionLengthPtr);
2085 Marshal.FreeHGlobal(extensionLengthPtr);
2086 releasePointer(extensionHandle);
2087 releasePointer(oidHandle);
2095 ) {
return GetCertExtension(certificate, oid, extension, 0, extensionMaxLength); }
2102 int extensionOffset,
2106 GCHandle oidHandle =
new GCHandle();
2107 IntPtr oidPtr = IntPtr.Zero;
2108 byte[] oidArray =
new UTF8Encoding().GetBytes(oid);
2109 GCHandle extensionHandle =
new GCHandle();
2110 IntPtr extensionPtr = IntPtr.Zero;
2113 getPointer(oidArray, 0, ref oidHandle, ref oidPtr);
2114 checkIndices(extension, extensionOffset, extensionLength);
2115 getPointer(extension, extensionOffset, ref extensionHandle, ref extensionPtr);
2116 processStatus(wrapped_AddCertExtension(certificate, oidPtr, criticalFlag, extensionPtr, extensionLength));
2120 releasePointer(extensionHandle);
2121 releasePointer(oidHandle);
2129 ) {
AddCertExtension(certificate, oid, criticalFlag, extension, 0, extension == null ? 0 : extension.Length); }
2135 ) {
AddCertExtension(certificate, oid, criticalFlag, extension == null ? null :
new UTF8Encoding().GetBytes(extension), 0, extension == null ? 0 :
new UTF8Encoding().GetByteCount(extension)); }
2142 GCHandle oidHandle =
new GCHandle();
2143 IntPtr oidPtr = IntPtr.Zero;
2144 byte[] oidArray =
new UTF8Encoding().GetBytes(oid);
2147 getPointer(oidArray, 0, ref oidHandle, ref oidPtr);
2148 processStatus(wrapped_DeleteCertExtension(certificate, oidPtr));
2152 releasePointer(oidHandle);
2163 processStatus(wrapped_SignCert(certificate, signContext));
2171 processStatus(wrapped_CheckCert(certificate, sigCheckKey));
2178 int certObjectOffset,
2183 IntPtr certificatePtr = Marshal.AllocHGlobal(4);
2184 GCHandle certObjectHandle =
new GCHandle();
2185 IntPtr certObjectPtr = IntPtr.Zero;
2188 checkIndices(certObject, certObjectOffset, certObjectLength);
2189 getPointer(certObject, certObjectOffset, ref certObjectHandle, ref certObjectPtr);
2190 processStatus(wrapped_ImportCert(certObjectPtr, certObjectLength, cryptUser, certificatePtr));
2191 return Marshal.ReadInt32(certificatePtr);
2195 Marshal.FreeHGlobal(certificatePtr);
2196 releasePointer(certObjectHandle);
2202 ) {
return ImportCert(certObject, 0, certObject == null ? 0 : certObject.Length, cryptUser); }
2206 ) {
return ImportCert(certObject == null ? null :
new UTF8Encoding().GetBytes(certObject), 0, certObject == null ? 0 :
new UTF8Encoding().GetByteCount(certObject), cryptUser); }
2210 int certObjectOffset,
2216 IntPtr certObjectLengthPtr = Marshal.AllocHGlobal(4);
2217 GCHandle certObjectHandle =
new GCHandle();
2218 IntPtr certObjectPtr = IntPtr.Zero;
2221 processStatus(wrapped_ExportCert(certObjectPtr, certObjectMaxLength, certObjectLengthPtr, certFormatType, certificate));
2223 checkIndices(certObject, certObjectOffset, certObjectLength);
2224 getPointer(certObject, certObjectOffset, ref certObjectHandle, ref certObjectPtr);
2225 processStatus(wrapped_ExportCert(certObjectPtr, certObjectMaxLength, certObjectLengthPtr, certFormatType, certificate));
2226 return Marshal.ReadInt32(certObjectLengthPtr);
2230 Marshal.FreeHGlobal(certObjectLengthPtr);
2231 releasePointer(certObjectHandle);
2239 ) {
return ExportCert(certObject, 0, certObjectMaxLength, certFormatType, certificate); }
2248 processStatus(wrapped_CAAddItem(keyset, certificate));
2258 IntPtr certificatePtr = Marshal.AllocHGlobal(4);
2259 GCHandle keyIDHandle =
new GCHandle();
2260 IntPtr keyIDPtr = IntPtr.Zero;
2261 byte[] keyIDArray =
new UTF8Encoding().GetBytes(keyID);
2264 getPointer(keyIDArray, 0, ref keyIDHandle, ref keyIDPtr);
2265 processStatus(wrapped_CAGetItem(keyset, certificatePtr, certType, keyIDtype, keyIDPtr));
2266 return Marshal.ReadInt32(certificatePtr);
2270 Marshal.FreeHGlobal(certificatePtr);
2271 releasePointer(keyIDHandle);
2282 GCHandle keyIDHandle =
new GCHandle();
2283 IntPtr keyIDPtr = IntPtr.Zero;
2284 byte[] keyIDArray =
new UTF8Encoding().GetBytes(keyID);
2287 getPointer(keyIDArray, 0, ref keyIDHandle, ref keyIDPtr);
2288 processStatus(wrapped_CADeleteItem(keyset, certType, keyIDtype, keyIDPtr));
2292 releasePointer(keyIDHandle);
2303 IntPtr certificatePtr = Marshal.AllocHGlobal(4);
2306 processStatus(wrapped_CACertManagement(certificatePtr, action, keyset, caKey, certRequest));
2307 return Marshal.ReadInt32(certificatePtr);
2311 Marshal.FreeHGlobal(certificatePtr);
2328 IntPtr envelopePtr = Marshal.AllocHGlobal(4);
2331 processStatus(wrapped_CreateEnvelope(envelopePtr, cryptUser, formatType));
2332 return Marshal.ReadInt32(envelopePtr);
2336 Marshal.FreeHGlobal(envelopePtr);
2344 processStatus(wrapped_DestroyEnvelope(envelope));
2354 IntPtr sessionPtr = Marshal.AllocHGlobal(4);
2357 processStatus(wrapped_CreateSession(sessionPtr, cryptUser, formatType));
2358 return Marshal.ReadInt32(sessionPtr);
2362 Marshal.FreeHGlobal(sessionPtr);
2370 processStatus(wrapped_DestroySession(session));
2382 IntPtr bytesCopiedPtr = Marshal.AllocHGlobal(4);
2383 GCHandle bufferHandle =
new GCHandle();
2384 IntPtr bufferPtr = IntPtr.Zero;
2389 checkIndices(buffer, bufferOffset, length);
2390 getPointer(buffer, bufferOffset, ref bufferHandle, ref bufferPtr);
2391 status = wrapped_PushData(envelope, bufferPtr, length, bytesCopiedPtr);
2392 bytesCopied = Marshal.ReadInt32(bytesCopiedPtr);
2393 processStatus(status, bytesCopied);
2398 Marshal.FreeHGlobal(bytesCopiedPtr);
2399 releasePointer(bufferHandle);
2405 ) {
return PushData(envelope, buffer, 0, buffer == null ? 0 : buffer.Length); }
2409 ) {
return PushData(envelope, buffer == null ? null :
new UTF8Encoding().GetBytes(buffer), 0, buffer == null ? 0 :
new UTF8Encoding().GetByteCount(buffer)); }
2415 processStatus(wrapped_FlushData(envelope));
2425 IntPtr bytesCopiedPtr = Marshal.AllocHGlobal(4);
2426 GCHandle bufferHandle =
new GCHandle();
2427 IntPtr bufferPtr = IntPtr.Zero;
2432 checkIndices(buffer, bufferOffset, bytesCopied);
2433 getPointer(buffer, bufferOffset, ref bufferHandle, ref bufferPtr);
2434 status = wrapped_PopData(envelope, bufferPtr, length, bytesCopiedPtr);
2435 bytesCopied = Marshal.ReadInt32(bytesCopiedPtr);
2436 processStatus(status, bytesCopied);
2441 Marshal.FreeHGlobal(bytesCopiedPtr);
2442 releasePointer(bufferHandle);
2449 ) {
return PopData(envelope, buffer, 0, length); }
2465 IntPtr devicePtr = Marshal.AllocHGlobal(4);
2466 GCHandle nameHandle =
new GCHandle();
2467 IntPtr namePtr = IntPtr.Zero;
2468 byte[] nameArray =
new UTF8Encoding().GetBytes(name);
2471 getPointer(nameArray, 0, ref nameHandle, ref namePtr);
2472 processStatus(wrapped_DeviceOpen(devicePtr, cryptUser, deviceType, namePtr));
2473 return Marshal.ReadInt32(devicePtr);
2477 Marshal.FreeHGlobal(devicePtr);
2478 releasePointer(nameHandle);
2486 processStatus(wrapped_DeviceClose(device));
2496 IntPtr cryptQueryInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(
CRYPT_QUERY_INFO)));
2500 processStatus(wrapped_DeviceQueryCapability(device, cryptAlgo, cryptQueryInfoPtr));
2501 Marshal.PtrToStructure(cryptQueryInfoPtr, cryptQueryInfo);
2502 return cryptQueryInfo;
2506 Marshal.FreeHGlobal(cryptQueryInfoPtr);
2517 IntPtr cryptContextPtr = Marshal.AllocHGlobal(4);
2520 processStatus(wrapped_DeviceCreateContext(device, cryptContextPtr, cryptAlgo));
2521 return Marshal.ReadInt32(cryptContextPtr);
2525 Marshal.FreeHGlobal(cryptContextPtr);
2542 IntPtr userPtr = Marshal.AllocHGlobal(4);
2543 GCHandle nameHandle =
new GCHandle();
2544 IntPtr namePtr = IntPtr.Zero;
2545 byte[] nameArray =
new UTF8Encoding().GetBytes(name);
2546 GCHandle passwordHandle =
new GCHandle();
2547 IntPtr passwordPtr = IntPtr.Zero;
2548 byte[] passwordArray =
new UTF8Encoding().GetBytes(password);
2551 getPointer(nameArray, 0, ref nameHandle, ref namePtr);
2552 getPointer(passwordArray, 0, ref passwordHandle, ref passwordPtr);
2553 processStatus(wrapped_Login(userPtr, namePtr, passwordPtr));
2554 return Marshal.ReadInt32(userPtr);
2558 Marshal.FreeHGlobal(userPtr);
2559 releasePointer(nameHandle);
2560 releasePointer(passwordHandle);
2568 processStatus(wrapped_Logout(user));
2578 [DllImport(
"cl32.dll", EntryPoint=
"cryptInit")]
2579 private static extern int wrapped_Init();
2581 [DllImport(
"cl32.dll", EntryPoint=
"cryptEnd")]
2582 private static extern int wrapped_End();
2584 [DllImport(
"cl32.dll", EntryPoint=
"cryptQueryCapability")]
2585 private static extern int wrapped_QueryCapability(
int cryptAlgo, IntPtr cryptQueryInfo);
2587 [DllImport(
"cl32.dll", EntryPoint=
"cryptCreateContext")]
2590 [DllImport(
"cl32.dll", EntryPoint=
"cryptDestroyContext")]
2591 private static extern int wrapped_DestroyContext(
int cryptContext);
2593 [DllImport(
"cl32.dll", EntryPoint=
"cryptDestroyObject")]
2594 private static extern int wrapped_DestroyObject(
int cryptObject);
2596 [DllImport(
"cl32.dll", EntryPoint=
"cryptGenerateKey")]
2597 private static extern int wrapped_GenerateKey(
int cryptContext);
2599 [DllImport(
"cl32.dll", EntryPoint=
"cryptGenerateKeyAsync")]
2600 private static extern int wrapped_GenerateKeyAsync(
int cryptContext);
2602 [DllImport(
"cl32.dll", EntryPoint=
"cryptAsyncQuery")]
2603 private static extern int wrapped_AsyncQuery(
int cryptObject);
2605 [DllImport(
"cl32.dll", EntryPoint=
"cryptAsyncCancel")]
2606 private static extern int wrapped_AsyncCancel(
int cryptObject);
2608 [DllImport(
"cl32.dll", EntryPoint=
"cryptEncrypt")]
2611 [DllImport(
"cl32.dll", EntryPoint=
"cryptDecrypt")]
2614 [DllImport(
"cl32.dll", EntryPoint=
"cryptSetAttribute")]
2617 [DllImport(
"cl32.dll", EntryPoint=
"cryptSetAttributeString")]
2620 [DllImport(
"cl32.dll", EntryPoint=
"cryptGetAttribute")]
2623 [DllImport(
"cl32.dll", EntryPoint=
"cryptGetAttributeString")]
2626 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeleteAttribute")]
2629 [DllImport(
"cl32.dll", EntryPoint=
"cryptAddRandom")]
2630 private static extern int wrapped_AddRandom(IntPtr randomData,
int randomDataLength);
2632 [DllImport(
"cl32.dll", EntryPoint=
"cryptQueryObject")]
2635 [DllImport(
"cl32.dll", EntryPoint=
"cryptExportKey")]
2638 [DllImport(
"cl32.dll", EntryPoint=
"cryptExportKeyEx")]
2641 [DllImport(
"cl32.dll", EntryPoint=
"cryptImportKey")]
2644 [DllImport(
"cl32.dll", EntryPoint=
"cryptImportKeyEx")]
2647 [DllImport(
"cl32.dll", EntryPoint=
"cryptCreateSignature")]
2650 [DllImport(
"cl32.dll", EntryPoint=
"cryptCreateSignatureEx")]
2653 [DllImport(
"cl32.dll", EntryPoint=
"cryptCheckSignature")]
2656 [DllImport(
"cl32.dll", EntryPoint=
"cryptCheckSignatureEx")]
2659 [DllImport(
"cl32.dll", EntryPoint=
"cryptKeysetOpen")]
2662 [DllImport(
"cl32.dll", EntryPoint=
"cryptKeysetClose")]
2663 private static extern int wrapped_KeysetClose(
int keyset);
2665 [DllImport(
"cl32.dll", EntryPoint=
"cryptGetPublicKey")]
2668 [DllImport(
"cl32.dll", EntryPoint=
"cryptGetPrivateKey")]
2671 [DllImport(
"cl32.dll", EntryPoint=
"cryptGetKey")]
2674 [DllImport(
"cl32.dll", EntryPoint=
"cryptAddPublicKey")]
2677 [DllImport(
"cl32.dll", EntryPoint=
"cryptAddPrivateKey")]
2680 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeleteKey")]
2683 [DllImport(
"cl32.dll", EntryPoint=
"cryptCreateCert")]
2686 [DllImport(
"cl32.dll", EntryPoint=
"cryptDestroyCert")]
2687 private static extern int wrapped_DestroyCert(
int certificate);
2689 [DllImport(
"cl32.dll", EntryPoint=
"cryptGetCertExtension")]
2692 [DllImport(
"cl32.dll", EntryPoint=
"cryptAddCertExtension")]
2695 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeleteCertExtension")]
2696 private static extern int wrapped_DeleteCertExtension(
int certificate, IntPtr
oid);
2698 [DllImport(
"cl32.dll", EntryPoint=
"cryptSignCert")]
2701 [DllImport(
"cl32.dll", EntryPoint=
"cryptCheckCert")]
2704 [DllImport(
"cl32.dll", EntryPoint=
"cryptImportCert")]
2707 [DllImport(
"cl32.dll", EntryPoint=
"cryptExportCert")]
2710 [DllImport(
"cl32.dll", EntryPoint=
"cryptCAAddItem")]
2713 [DllImport(
"cl32.dll", EntryPoint=
"cryptCAGetItem")]
2716 [DllImport(
"cl32.dll", EntryPoint=
"cryptCADeleteItem")]
2719 [DllImport(
"cl32.dll", EntryPoint=
"cryptCACertManagement")]
2720 private static extern int wrapped_CACertManagement(IntPtr
certificate,
int action,
int keyset,
int caKey,
int certRequest);
2722 [DllImport(
"cl32.dll", EntryPoint=
"cryptCreateEnvelope")]
2723 private static extern int wrapped_CreateEnvelope(IntPtr envelope,
int cryptUser,
int formatType);
2725 [DllImport(
"cl32.dll", EntryPoint=
"cryptDestroyEnvelope")]
2726 private static extern int wrapped_DestroyEnvelope(
int envelope);
2728 [DllImport(
"cl32.dll", EntryPoint=
"cryptCreateSession")]
2729 private static extern int wrapped_CreateSession(IntPtr session,
int cryptUser,
int formatType);
2731 [DllImport(
"cl32.dll", EntryPoint=
"cryptDestroySession")]
2732 private static extern int wrapped_DestroySession(
int session);
2734 [DllImport(
"cl32.dll", EntryPoint=
"cryptPushData")]
2737 [DllImport(
"cl32.dll", EntryPoint=
"cryptFlushData")]
2738 private static extern int wrapped_FlushData(
int envelope);
2740 [DllImport(
"cl32.dll", EntryPoint=
"cryptPopData")]
2743 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeviceOpen")]
2746 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeviceClose")]
2747 private static extern int wrapped_DeviceClose(
int device);
2749 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeviceQueryCapability")]
2750 private static extern int wrapped_DeviceQueryCapability(
int device,
int cryptAlgo, IntPtr cryptQueryInfo);
2752 [DllImport(
"cl32.dll", EntryPoint=
"cryptDeviceCreateContext")]
2753 private static extern int wrapped_DeviceCreateContext(
int device, IntPtr
cryptContext,
int cryptAlgo);
2755 [DllImport(
"cl32.dll", EntryPoint=
"cryptLogin")]
2756 private static extern int wrapped_Login(IntPtr user, IntPtr
name, IntPtr
password);
2758 [DllImport(
"cl32.dll", EntryPoint=
"cryptLogout")]
2759 private static extern int wrapped_Logout(
int user);
2764 private static void processStatus(
int status)
2771 private static void processStatus(
int status,
int extraInfo)
2773 if (status <
crypt.OK)
2774 throw new CryptException(status, extraInfo);
2777 private static void checkIndices(byte[] array,
int sequenceOffset,
int sequenceLength)
2781 if (sequenceOffset == 0)
2784 throw new IndexOutOfRangeException();
2787 int arrayLength = array.Length;
2789 if (sequenceOffset < 0 ||
2790 sequenceOffset >= arrayLength ||
2791 sequenceOffset + sequenceLength > arrayLength)
2792 throw new IndexOutOfRangeException();
2795 private static void getPointer(byte[]
buffer,
int bufferOffset, ref GCHandle bufferHandle, ref IntPtr bufferPtr)
2799 bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
2800 bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, bufferOffset);
2803 private static void releasePointer(GCHandle bufferHandle)
2805 if (bufferHandle.IsAllocated)
2806 bufferHandle.Free();
2810 [StructLayout(LayoutKind.Sequential, Pack=0, CharSet=CharSet.Ansi)]
2813 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public String
algoName;
2821 public CRYPT_QUERY_INFO(String newAlgoName,
int newBlockSize,
int newMinKeySize,
int newKeySize,
int newMaxKeySize)
2831 [StructLayout(LayoutKind.Sequential, Pack=0, CharSet=CharSet.Ansi)]
2838 [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public byte[]
salt;
2843 salt =
new byte[64];
2847 public CRYPT_OBJECT_INFO(
int newObjectType,
int newCryptAlgo,
int newCryptMode,
int newHashAlgo, byte[] newSalt)
2858 public int Status {
get {
return (
int)Data[
"Status"]; } }
2860 public int ExtraInfo {
get {
return (
int)Data[
"ExtraInfo"]; } }
2863 : base(convertMessage(status))
2865 Data.Add(
"Status", status);
2869 : base(convertMessage(status))
2871 Data.Add(
"Status", status);
2872 Data.Add(
"ExtraInfo", extra);
2875 private static string convertMessage(
int status)
2877 String prefix = Convert.ToString(status) +
": ";
2907 case crypt.
ERROR_READ:
return prefix +
"Cannot read item from object";
2912 default:
return prefix +
"Unknown Exception ?!?!";