551 {
555 {
556 global::Interop.NCrypt.NCryptBuffer* ptr = stackalloc global::Interop.NCrypt.NCryptBuffer[1];
557 *ptr = new global::Interop.NCrypt.NCryptBuffer
558 {
559 BufferType = global::Interop.NCrypt.BufferType.PkcsSecret,
560 cbBuffer = checked(2 * (password.
Length + 1)),
562 };
563 if (ptr->pvBuffer ==
IntPtr.Zero)
564 {
565 ptr->cbBuffer = 0;
566 }
567 global::Interop.NCrypt.NCryptBufferDesc nCryptBufferDesc = default(global::Interop.NCrypt.NCryptBufferDesc);
568 nCryptBufferDesc.cBuffers = 1;
569 nCryptBufferDesc.pBuffers = (
IntPtr)ptr;
570 nCryptBufferDesc.ulVersion = 0;
571 global::Interop.NCrypt.NCryptBufferDesc pParameterList = nCryptBufferDesc;
572 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptImportKey(safeNCryptProviderHandle,
IntPtr.Zero,
"PKCS8_PRIVATEKEY", ref pParameterList, out phKey, ref
MemoryMarshal.GetReference(keyBlob), keyBlob.
Length, 0);
573 if (errorCode != 0)
574 {
575 phKey.Dispose();
576 throw errorCode.ToCryptographicException();
577 }
578 }
579 CngKey cngKey =
new CngKey(safeNCryptProviderHandle, phKey);
580 cngKey.IsEphemeral = true;
581 return cngKey;
582 }
IntPtr DangerousGetHandle()
CngKey(SafeNCryptProviderHandle providerHandle, SafeNCryptKeyHandle keyHandle)