680 {
683 {
684 global::Interop.NCrypt.NCryptBuffer* ptr = stackalloc global::Interop.NCrypt.NCryptBuffer[3];
685 global::Interop.NCrypt.PBE_PARAMS pBE_PARAMS = default(global::Interop.NCrypt.PBE_PARAMS);
686 Span<byte> data = new Span<byte>(pBE_PARAMS.rgbSalt, 8);
687 RandomNumberGenerator.Fill(data);
688 pBE_PARAMS.Params.cbSalt = data.Length;
689 pBE_PARAMS.Params.iIterations = kdfCount;
690 *ptr = new global::Interop.NCrypt.NCryptBuffer
691 {
692 BufferType = global::Interop.NCrypt.BufferType.PkcsSecret,
693 cbBuffer = checked(2 * (password.
Length + 1)),
694 pvBuffer = safeUnicodeStringHandle.DangerousGetHandle()
695 };
696 if (ptr->pvBuffer ==
IntPtr.Zero)
697 {
698 ptr->cbBuffer = 0;
699 }
700 ptr[1] = new global::Interop.NCrypt.NCryptBuffer
701 {
702 BufferType = global::Interop.NCrypt.BufferType.PkcsAlgOid,
705 };
706 ptr[2] = new global::Interop.NCrypt.NCryptBuffer
707 {
708 BufferType = global::Interop.NCrypt.BufferType.PkcsAlgParam,
709 cbBuffer = sizeof(global::Interop.NCrypt.PBE_PARAMS),
710 pvBuffer = (IntPtr)(&pBE_PARAMS)
711 };
712 global::Interop.NCrypt.NCryptBufferDesc nCryptBufferDesc = default(global::Interop.NCrypt.NCryptBufferDesc);
713 nCryptBufferDesc.cBuffers = 3;
714 nCryptBufferDesc.pBuffers = (
IntPtr)ptr;
715 nCryptBufferDesc.ulVersion = 0;
716 global::Interop.NCrypt.NCryptBufferDesc pParameterList = nCryptBufferDesc;
717 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptExportKey(keyHandle,
IntPtr.Zero,
"PKCS8_PRIVATEKEY", ref pParameterList, ref
MemoryMarshal.GetReference(
default(Span<byte>)), 0, out var pcbResult, 0);
718 if (errorCode != 0)
719 {
720 throw errorCode.ToCryptographicException();
721 }
722 allocated = null;
723 if (allocate)
724 {
725 allocated = new byte[pcbResult];
727 }
729 {
730 bytesWritten = 0;
731 return false;
732 }
734 if (errorCode != 0)
735 {
736 throw errorCode.ToCryptographicException();
737 }
739 {
740 byte[]
array =
new byte[pcbResult];
742 Array.Clear(allocated, 0, pcbResult);
744 }
745 bytesWritten = pcbResult;
746 return true;
747 }
748 }
static readonly byte[] s_pkcs12TripleDesOidBytes