Jump to content

Recommended Posts

Добрый день!

Помогите, пожалуйста, с решением проблемы.

Имеется контейнер с ключевой парой. Имеется связанный с этой парой сертификат. Нужно решить задачу экспорта сессионного ключа на открытом ключе сертификата. Написал такой код (C#):


IntPtr providerHandler = IntPtr.Zero;
IntPtr sessionKeyHandler = IntPtr.Zero;
IntPtr publicKeyHandler = IntPtr.Zero;

try
{
if (!CryptoApi.CryptAcquireContext(
ref providerHandler,
null,
CryptoConstants.VPN_DEF_PROV,
CryptoConstants.VPN_PROV_TYPE,
CryptoConstants.CRYPT_VERIFYCONTEXT))
{
throw new Win32Exception();
}

if (!CryptoApi.CryptGenKey(
providerHandler,
CryptoConstants.CPCSP_ENCRYPT_ID,
CryptoConstants.CRYPT_EXPORTABLE,
ref sessionKeyHandler))
{
throw new Win32Exception();
}

X509Store store = new X509Store(STORE_NAME, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

X509Certificate2 cert = null;

try
{
cert = store.Certificates.Find(X509FindType.FindByThumbprint, "285d213829798dac59d75274b247e3a35a5a0630", false)[0];
}
finally
{
store.Close();
}

IntPtr subjectPublicKeyInfoPointer = IntPtr.Zero;

try
{
var certContextStruct = MarshalPtrToStructure<CryptoStructs.CERT_CONTEXT>(cert.Handle);
var certInfoStruct = MarshalPtrToStructure<CryptoStructs.CERT_INFO>(certContextStruct.pCertInfo);

subjectPublicKeyInfoPointer = Marshal.AllocHGlobal(Marshal.SizeOf(certInfoStruct.SubjectPublicKeyInfo));
Marshal.StructureToPtr(certInfoStruct.SubjectPublicKeyInfo, subjectPublicKeyInfoPointer, false);

if (!CryptoApi.CryptImportPublicKeyInfo(providerHandler, CERT_ENCODING_TYPE, subjectPublicKeyInfoPointer, ref publicKeyHandler))
{
throw new Win32Exception();
}
}
finally
{
Marshal.FreeHGlobal(subjectPublicKeyInfoPointer);
subjectPublicKeyInfoPointer = IntPtr.Zero;
}

uint sessionKeyBlobLength = 0;

if (!CryptoApi.CryptExportKey(
sessionKeyHandler,
publicKeyHandler,
CryptoConstants.SIMPLEBLOB,
0,
null,
ref sessionKeyBlobLength))
{
throw new Win32Exception();
}

byte[] sessionKeyBlob = new byte[sessionKeyBlobLength];

if (!CryptoApi.CryptExportKey(
sessionKeyHandler,
publicKeyHandler,
CryptoConstants.SIMPLEBLOB,
0,
sessionKeyBlob,
ref sessionKeyBlobLength))
{
throw new Win32Exception();
}
}
finally
{
if (publicKeyHandler != IntPtr.Zero)
{
CryptoApi.CryptDestroyKey(publicKeyHandler);
}

if (sessionKeyHandler != IntPtr.Zero)
{
CryptoApi.CryptDestroyKey(sessionKeyHandler);
}

if (providerHandler != IntPtr.Zero)
{
CryptoApi.CryptReleaseContext(providerHandler, 0);
}
}

На последней строчке до finally валится с ошибкой "Набор ключей не существует". Все указатели не нулевые, sessionKeyBlobLength = 71 (т.е. длина рассчитывается). Мучаюсь уже несколько дней.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.