Advertisement
Guest User

Program.cs

a guest
Mar 15th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 KB | None | 0 0
  1. var hStore = Libcapi.CertOpenSystemStoreA(IntPtr.Zero, "MY");
  2. Console.WriteLine("hStore = {0}", hStore);
  3. Console.WriteLine();
  4.  
  5. var pCertReceiver = Libcapi.CertFindCertificateInStore(
  6.     hStore, Libcapi.X509_ASN_ENCODING | Libcapi.PKCS_7_ASN_ENCODING,
  7.     0, Libcapi.CERT_FIND_SUBJECT_STR_A, "CRYPTO-PRO", IntPtr.Zero);
  8. Console.WriteLine("certReceiver = {0}", pCertReceiver);
  9. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  10. Console.WriteLine();
  11.  
  12. var certReceiver = Marshal.PtrToStructure<Libcapi.CertContext>(pCertReceiver);
  13. Console.WriteLine("certReceiver.hCertStore = {0}", certReceiver.hCertStore);
  14. Console.WriteLine();
  15.  
  16. var certReceiverInfo = Marshal.PtrToStructure<Libcapi.CertInfo>(certReceiver.pCertInfo);
  17. Console.WriteLine("receiver's alg id = {0}", certReceiverInfo.SignatureAlgorithm.pszObjId);
  18. Console.WriteLine("receiver's pubk alg id = {0}", certReceiverInfo.SubjectPublicKeyInfo.Algorithm.pszObjId);
  19. Console.WriteLine();
  20.  
  21. var pCertSender = Libcapi.CertFindCertificateInStore(
  22.     hStore, Libcapi.X509_ASN_ENCODING | Libcapi.PKCS_7_ASN_ENCODING,
  23.     0, Libcapi.CERT_FIND_SUBJECT_STR_A, "test@example.com", IntPtr.Zero);
  24. Console.WriteLine("certSender = {0}", pCertSender);
  25. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  26. Console.WriteLine();
  27.  
  28. var certSender = Marshal.PtrToStructure<Libcapi.CertContext>(pCertSender);
  29. Console.WriteLine("certSender.hCertStore = {0}", certSender.hCertStore);
  30. Console.WriteLine();
  31.  
  32. var certSenderInfo = Marshal.PtrToStructure<Libcapi.CertInfo>(certSender.pCertInfo);
  33. Console.WriteLine("sender's alg id = {0}", certSenderInfo.SignatureAlgorithm.pszObjId);
  34. Console.WriteLine("sender's pubk alg id = {0}", certSenderInfo.SubjectPublicKeyInfo.Algorithm.pszObjId);
  35. Console.WriteLine();
  36.  
  37. Console.WriteLine("CryptAcquireCertificatePrivateKey = {0}",
  38.     Libcapi.CryptAcquireCertificatePrivateKey(
  39.         pCertSender, 0, IntPtr.Zero,
  40.         out var hSenderProv, out var senderKeySpec, out var callerFreeProv
  41.     )
  42. );
  43. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  44. Console.WriteLine($"hSenderProv = {hSenderProv}, keySpec = {senderKeySpec}, callerFreeProv = {callerFreeProv}");
  45. Console.WriteLine();
  46.  
  47. Console.WriteLine("CryptGetUserKey = {0}",
  48.     Libcapi.CryptGetUserKey(hSenderProv, senderKeySpec, out var hSenderPrivKey)
  49. );
  50. Console.WriteLine("hSenderPrivKey = {0}", hSenderPrivKey);
  51. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  52. Console.WriteLine();
  53.  
  54. Console.WriteLine("CryptImportPublicKeyInfoEx = {0}",
  55.     Libcapi.CryptImportPublicKeyInfoEx(
  56.         hSenderProv, Libcapi.X509_ASN_ENCODING | Libcapi.PKCS_7_ASN_ENCODING,
  57.         ref certReceiverInfo.SubjectPublicKeyInfo, 0, 0, IntPtr.Zero,
  58.         out var hReceiverPubKey
  59.     )
  60. );
  61. Console.WriteLine("hReceiverPubKey = {0}", hReceiverPubKey);
  62. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  63. Console.WriteLine();
  64.  
  65. uint recPubKeyBlobLen = 256;
  66. var recPubKeyBlob = Marshal.AllocHGlobal((int)recPubKeyBlobLen);
  67. Console.WriteLine("CryptExportKey = {0}",
  68.     Libcapi.CryptExportKey(
  69.         hReceiverPubKey, IntPtr.Zero, Libcapi.PUBLICKEYBLOB, 0,
  70.         recPubKeyBlob, ref recPubKeyBlobLen
  71.     )
  72. );
  73. Console.WriteLine("recPubKeyBlobLen = {0}", recPubKeyBlobLen);
  74. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  75. Console.WriteLine();
  76.  
  77. Console.WriteLine("CryptImportKey = {0}",
  78.     Libcapi.CryptImportKey(
  79.         hSenderProv, recPubKeyBlob, recPubKeyBlobLen,
  80.         hSenderPrivKey, 0, out var hAgreeKey
  81.     )
  82. );
  83. Console.WriteLine("hAgreeKey = {0}", hAgreeKey);
  84. Console.WriteLine("GetLastError: {0} (0x{0:x})", Librdrsup.GetLastError());
  85. Console.WriteLine();
  86.  
  87. Console.WriteLine("CertCloseStore: {0}", Libcapi.CertCloseStore(hStore, 0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement