Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ ExportPkcs8KeyBlob() [1/2]

static unsafe bool System.Security.Cryptography.CngKey.ExportPkcs8KeyBlob ( bool allocate,
SafeNCryptKeyHandle keyHandle,
ReadOnlySpan< char > password,
int kdfCount,
Span< byte > destination,
out int bytesWritten,
out byte[] allocated )
inlinestaticpackage

Definition at line 679 of file CngKey.cs.

680 {
682 fixed (byte* ptr2 = s_pkcs12TripleDesOidBytes)
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,
703 cbBuffer = s_pkcs12TripleDesOidBytes.Length,
704 pvBuffer = (IntPtr)ptr2
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];
726 destination = allocated;
727 }
728 else if (pcbResult > destination.Length)
729 {
730 bytesWritten = 0;
731 return false;
732 }
733 errorCode = global::Interop.NCrypt.NCryptExportKey(keyHandle, IntPtr.Zero, "PKCS8_PRIVATEKEY", ref pParameterList, ref MemoryMarshal.GetReference(destination), destination.Length, out pcbResult, 0);
734 if (errorCode != 0)
735 {
736 throw errorCode.ToCryptographicException();
737 }
738 if (allocate && pcbResult != destination.Length)
739 {
740 byte[] array = new byte[pcbResult];
741 destination.Slice(0, pcbResult).CopyTo(array);
742 Array.Clear(allocated, 0, pcbResult);
743 allocated = array;
744 }
745 bytesWritten = pcbResult;
746 return true;
747 }
748 }
static readonly byte[] s_pkcs12TripleDesOidBytes
Definition CngKey.cs:15

References System.array, System.Array.Clear(), System.destination, System.Security.Cryptography.RandomNumberGenerator.Fill(), System.ReadOnlySpan< T >.Length, System.Span< T >.Length, System.Security.Cryptography.CngKey.s_pkcs12TripleDesOidBytes, and System.IntPtr.Zero.