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

◆ ExportPkcs8KeyBlob() [1/2]

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

Definition at line 99 of file CngKeyLite.cs.

100 {
101 using SafeUnicodeStringHandle safeUnicodeStringHandle = new SafeUnicodeStringHandle(password);
102 fixed (byte* ptr2 = s_pkcs12TripleDesOidBytes)
103 {
104 global::Interop.NCrypt.NCryptBuffer* ptr = stackalloc global::Interop.NCrypt.NCryptBuffer[3];
105 global::Interop.NCrypt.PBE_PARAMS pBE_PARAMS = default(global::Interop.NCrypt.PBE_PARAMS);
106 Span<byte> data = new Span<byte>(pBE_PARAMS.rgbSalt, 8);
107 RandomNumberGenerator.Fill(data);
108 pBE_PARAMS.Params.cbSalt = data.Length;
109 pBE_PARAMS.Params.iIterations = kdfCount;
110 *ptr = new global::Interop.NCrypt.NCryptBuffer
111 {
112 BufferType = global::Interop.NCrypt.BufferType.PkcsSecret,
113 cbBuffer = checked(2 * (password.Length + 1)),
114 pvBuffer = safeUnicodeStringHandle.DangerousGetHandle()
115 };
116 if (ptr->pvBuffer == IntPtr.Zero)
117 {
118 ptr->cbBuffer = 0;
119 }
120 ptr[1] = new global::Interop.NCrypt.NCryptBuffer
121 {
122 BufferType = global::Interop.NCrypt.BufferType.PkcsAlgOid,
123 cbBuffer = s_pkcs12TripleDesOidBytes.Length,
124 pvBuffer = (IntPtr)ptr2
125 };
126 ptr[2] = new global::Interop.NCrypt.NCryptBuffer
127 {
128 BufferType = global::Interop.NCrypt.BufferType.PkcsAlgParam,
129 cbBuffer = sizeof(global::Interop.NCrypt.PBE_PARAMS),
130 pvBuffer = (IntPtr)(&pBE_PARAMS)
131 };
132 global::Interop.NCrypt.NCryptBufferDesc nCryptBufferDesc = default(global::Interop.NCrypt.NCryptBufferDesc);
133 nCryptBufferDesc.cBuffers = 3;
134 nCryptBufferDesc.pBuffers = (IntPtr)ptr;
135 nCryptBufferDesc.ulVersion = 0;
136 global::Interop.NCrypt.NCryptBufferDesc pParameterList = nCryptBufferDesc;
137 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);
138 if (errorCode != 0)
139 {
140 throw errorCode.ToCryptographicException();
141 }
142 allocated = null;
143 if (allocate)
144 {
145 allocated = new byte[pcbResult];
146 destination = allocated;
147 }
148 else if (pcbResult > destination.Length)
149 {
150 bytesWritten = 0;
151 return false;
152 }
153 errorCode = global::Interop.NCrypt.NCryptExportKey(keyHandle, IntPtr.Zero, "PKCS8_PRIVATEKEY", ref pParameterList, ref MemoryMarshal.GetReference(destination), destination.Length, out pcbResult, 0);
154 if (errorCode != 0)
155 {
156 throw errorCode.ToCryptographicException();
157 }
158 if (allocate && pcbResult != destination.Length)
159 {
160 byte[] array = new byte[pcbResult];
161 destination.Slice(0, pcbResult).CopyTo(array);
162 CryptographicOperations.ZeroMemory(allocated.AsSpan(0, pcbResult));
163 allocated = array;
164 }
165 bytesWritten = pcbResult;
166 return true;
167 }
168 }
static readonly byte[] s_pkcs12TripleDesOidBytes
Definition CngKeyLite.cs:12

References System.array, System.destination, System.Security.Cryptography.RandomNumberGenerator.Fill(), System.ReadOnlySpan< T >.Length, System.Span< T >.Length, System.Security.Cryptography.CngKeyLite.s_pkcs12TripleDesOidBytes, System.IntPtr.Zero, and System.Security.Cryptography.CryptographicOperations.ZeroMemory().