31 if (propertyAsString ==
null)
57 Unsafe.SkipInit(out
byte b);
58 if (global::Interop.NCrypt.NCryptGetProperty(
_keyHandle,
"CLR IsEphemeral", &b, 1, out var pcbResult,
CngPropertyOptions.CustomProperty) != 0)
74 byte b = (byte)(
value ? 1 : 0);
75 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSetProperty(
_keyHandle,
"CLR IsEphemeral", &b, 1,
CngPropertyOptions.CustomProperty);
78 throw errorCode.ToCryptographicException();
109 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptGetIntProperty(
_keyHandle,
"PublicKeyLength", ref result);
112 errorCode = global::Interop.NCrypt.NCryptGetIntProperty(
_keyHandle,
"Length", ref result);
116 throw errorCode.ToCryptographicException();
141 if (propertyAsString ==
null)
155 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptGetProperty(
_keyHandle,
"UI Policy",
null, 0, out var pcbResult,
CngPropertyOptions.None);
156 if (errorCode != 0 && errorCode != global::Interop.NCrypt.ErrorCode.NTE_NOT_FOUND)
158 throw errorCode.ToCryptographicException();
163 string creationTitle;
164 if (errorCode != 0 || pcbResult == 0)
169 creationTitle =
null;
173 if (pcbResult <
sizeof(global::Interop.NCrypt.NCRYPT_UI_POLICY))
175 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
177 byte[]
array =
new byte[pcbResult];
178 fixed (
byte* ptr = &
array[0])
183 throw errorCode.ToCryptographicException();
185 global::Interop.NCrypt.NCRYPT_UI_POLICY* ptr2 = (global::Interop.NCrypt.NCRYPT_UI_POLICY*)ptr;
186 protectionLevel = ptr2->dwFlags;
193 return new CngUIPolicy(protectionLevel, friendlyName, description, propertyAsString, creationTitle);
236 throw global::Interop.NCrypt.ErrorCode.NTE_NOT_FOUND.ToCryptographicException();
238 if (
array.Length == 0)
252 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptGetProperty(
_keyHandle, name,
null, 0, out pcbResult,
options);
253 return errorCode
switch
255 global::Interop.NCrypt.ErrorCode.NTE_NOT_FOUND =>
false,
256 global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS =>
true,
257 _ =>
throw errorCode.ToCryptographicException(),
263 byte[] valueWithoutCopying =
property.GetValueWithoutCopying();
264 if (valueWithoutCopying ==
null)
266 throw global::Interop.NCrypt.ErrorCode.NTE_INVALID_PARAMETER.ToCryptographicException();
268 fixed (
byte* pbInput = valueWithoutCopying.MapZeroLengthArrayToNonNullPointer())
270 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSetProperty(
_keyHandle, property.
Name, pbInput, valueWithoutCopying.Length, property.
Options);
273 throw errorCode.ToCryptographicException();
280 return Create(algorithm,
null);
285 return Create(algorithm, keyName,
null);
290 if (algorithm ==
null)
294 if (creationParameters ==
null)
299 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptCreatePersistedKey(safeNCryptProviderHandle, out var phKey, algorithm.
Algorithm, keyName, 0, creationParameters.
KeyCreationOptions);
302 throw errorCode.ToCryptographicException();
305 errorCode = global::Interop.NCrypt.NCryptFinalizeKey(phKey, 0);
308 throw errorCode.ToCryptographicException();
310 CngKey cngKey =
new CngKey(safeNCryptProviderHandle, phKey);
313 cngKey.IsEphemeral =
true;
323 keyHandle.SetExportPolicy(
value);
325 if (creationParameters.
KeyUsage.HasValue)
328 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSetProperty(keyHandle,
"Key Usage", &value2, 4,
CngPropertyOptions.Persist);
331 throw errorCode.ToCryptographicException();
337 global::Interop.NCrypt.ErrorCode errorCode2 = global::Interop.NCrypt.NCryptSetProperty(keyHandle,
"HWND Handle", &parentWindowHandle,
sizeof(
IntPtr),
CngPropertyOptions.None);
340 throw errorCode2.ToCryptographicException();
344 if (uIPolicy !=
null)
348 foreach (
CngProperty parameter
in creationParameters.Parameters)
351 int cbInput = ((valueWithoutCopying !=
null) ? valueWithoutCopying.Length : 0);
352 fixed (
byte* pbInput = valueWithoutCopying.MapZeroLengthArrayToNonNullPointer())
354 global::Interop.NCrypt.ErrorCode errorCode3 = global::Interop.NCrypt.NCryptSetProperty(keyHandle, parameter.
Name, pbInput, cbInput, parameter.
Options);
357 throw errorCode3.ToCryptographicException();
371 global::Interop.NCrypt.NCRYPT_UI_POLICY nCRYPT_UI_POLICY2;
372 if (friendlyName ==
null)
375 intPtr = (
value =
null);
377 fixed (
char* ptr = description)
380 global::Interop.NCrypt.NCRYPT_UI_POLICY nCRYPT_UI_POLICY =
default(global::Interop.NCrypt.NCRYPT_UI_POLICY);
381 nCRYPT_UI_POLICY.dwVersion = 1;
383 nCRYPT_UI_POLICY.pszCreationTitle =
new IntPtr(value3);
384 nCRYPT_UI_POLICY.pszFriendlyName =
new IntPtr(
value);
385 nCRYPT_UI_POLICY.pszDescription =
new IntPtr(value2);
386 nCRYPT_UI_POLICY2 = nCRYPT_UI_POLICY;
387 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSetProperty(keyHandle,
"UI Policy", &nCRYPT_UI_POLICY2,
sizeof(global::Interop.NCrypt.NCRYPT_UI_POLICY),
CngPropertyOptions.Persist);
390 throw errorCode.ToCryptographicException();
396 fixed (
char* ptr2 = &friendlyName.GetPinnableReference())
399 intPtr = (
value = ptr2);
401 fixed (
char* ptr = description)
404 global::Interop.NCrypt.NCRYPT_UI_POLICY nCRYPT_UI_POLICY =
default(global::Interop.NCrypt.NCRYPT_UI_POLICY);
405 nCRYPT_UI_POLICY.dwVersion = 1;
407 nCRYPT_UI_POLICY.pszCreationTitle =
new IntPtr(value3);
408 nCRYPT_UI_POLICY.pszFriendlyName =
new IntPtr(
value);
409 nCRYPT_UI_POLICY.pszDescription =
new IntPtr(value2);
410 nCRYPT_UI_POLICY2 = nCRYPT_UI_POLICY;
411 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSetProperty(keyHandle,
"UI Policy", &nCRYPT_UI_POLICY2,
sizeof(global::Interop.NCrypt.NCRYPT_UI_POLICY),
CngPropertyOptions.Persist);
414 throw errorCode.ToCryptographicException();
421 if (useContext ==
null)
425 int cbInput = checked((useContext.Length + 1) * 2);
426 fixed (
char* pbInput = useContext)
428 global::Interop.NCrypt.ErrorCode errorCode2 = global::Interop.NCrypt.NCryptSetProperty(keyHandle,
"Use Context", pbInput, cbInput,
CngPropertyOptions.Persist);
431 throw errorCode2.ToCryptographicException();
465 oidValue =
"1.2.840.10045.3.1.7";
470 oidValue =
"1.3.132.0.34";
475 oidValue =
"1.3.132.0.35";
484 byte[]
array =
new byte[(friendlyName.Length + 1) * 2];
556 global::Interop.NCrypt.NCryptBuffer* ptr = stackalloc global::Interop.NCrypt.NCryptBuffer[1];
557 *ptr =
new global::Interop.NCrypt.NCryptBuffer
559 BufferType = global::Interop.NCrypt.BufferType.PkcsSecret,
560 cbBuffer = checked(2 * (password.
Length + 1)),
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);
576 throw errorCode.ToCryptographicException();
579 CngKey cngKey =
new CngKey(safeNCryptProviderHandle, phKey);
580 cngKey.IsEphemeral =
true;
599 if (provider ==
null)
605 if (curveName ==
null)
607 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptImportKey(safeNCryptProviderHandle,
IntPtr.
Zero,
format.Format,
IntPtr.
Zero, out phKey, ref
MemoryMarshal.GetReference(keyBlob), keyBlob.
Length, 0);
610 throw errorCode.ToCryptographicException();
617 CngKey cngKey =
new CngKey(safeNCryptProviderHandle, phKey);
631 throw errorCode.ToCryptographicException();
633 byte[]
array =
new byte[pcbResult];
637 throw errorCode.ToCryptographicException();
649 throw errorCode.ToCryptographicException();
659 throw errorCode.ToCryptographicException();
661 bytesWritten = pcbResult;
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);
688 pBE_PARAMS.Params.cbSalt = data.
Length;
689 pBE_PARAMS.Params.iIterations = kdfCount;
690 *ptr =
new global::Interop.NCrypt.NCryptBuffer
692 BufferType = global::Interop.NCrypt.BufferType.PkcsSecret,
693 cbBuffer = checked(2 * (password.
Length + 1)),
694 pvBuffer = safeUnicodeStringHandle.DangerousGetHandle()
700 ptr[1] =
new global::Interop.NCrypt.NCryptBuffer
702 BufferType = global::Interop.NCrypt.BufferType.PkcsAlgOid,
706 ptr[2] =
new global::Interop.NCrypt.NCryptBuffer
708 BufferType = global::Interop.NCrypt.BufferType.PkcsAlgParam,
709 cbBuffer =
sizeof(global::Interop.NCrypt.PBE_PARAMS),
710 pvBuffer = (
IntPtr)(&pBE_PARAMS)
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);
720 throw errorCode.ToCryptographicException();
725 allocated =
new byte[pcbResult];
736 throw errorCode.ToCryptographicException();
740 byte[]
array =
new byte[pcbResult];
745 bytesWritten = pcbResult;
752 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptDeleteKey(
_keyHandle, 0);
755 throw errorCode.ToCryptographicException();
761 public static bool Exists(
string keyName)
777 if (provider ==
null)
785 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptOpenKey(hProvider, out phKey, keyName, 0,
options);
786 return errorCode
switch
788 global::Interop.NCrypt.ErrorCode.NTE_BAD_KEYSET =>
false,
789 global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS =>
true,
790 _ =>
throw errorCode.ToCryptographicException(),
815 if (provider ==
null)
821 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptOpenKey(safeNCryptProviderHandle, out phKey, keyName, 0, openOptions);
824 throw errorCode.ToCryptographicException();
826 return new CngKey(safeNCryptProviderHandle, phKey);
831 if (keyHandle ==
null)
846 cngKey =
new CngKey(safeNCryptProviderHandle, keyHandle2);
850 cngKey.IsEphemeral =
true;
SafeNCryptKeyHandle Duplicate()
void SetHandleValue(IntPtr newHandleValue)
SafeNCryptProviderHandle Duplicate()
static unsafe void Clear(Array array)
static unsafe? string PtrToStringUni(IntPtr ptr)
IntPtr DangerousGetHandle()
void SetHandleAsInvalid()
static string Cryptography_CurveNotSupported
static string Format(string resourceFormat, object p1)
static string Cryptography_OpenEphemeralKeyHandleWithoutEphemeralFlag
static string Cryptography_OpenInvalidHandle
static CngAlgorithm ECDiffieHellmanP256
static CngAlgorithm ECDiffieHellman
static CngAlgorithm ECDsa
static CngAlgorithm ECDiffieHellmanP384
static CngAlgorithm ECDsaP256
static CngAlgorithm ECDsaP384
static CngAlgorithm ECDsaP521
static CngAlgorithm ECDiffieHellmanP521
IntPtr ParentWindowHandle
CngExportPolicies? ExportPolicy
CngKeyCreationOptions KeyCreationOptions
static CngKey ImportEncryptedPkcs8(ReadOnlySpan< byte > keyBlob, ReadOnlySpan< char > password)
static CngKey Open(SafeNCryptKeyHandle keyHandle, CngKeyHandleOpenOptions keyHandleOpenOptions)
static unsafe CngKey ImportEncryptedPkcs8(ReadOnlySpan< byte > keyBlob, ReadOnlySpan< char > password, CngProvider provider)
bool TryExportKeyBlob(string blobType, Span< byte > destination, out int bytesWritten)
CngProperty GetProperty(string name, CngPropertyOptions options)
CngKey(SafeNCryptProviderHandle providerHandle, SafeNCryptKeyHandle keyHandle)
static CngAlgorithm EcdsaCurveNameToAlgorithm(string name)
unsafe bool HasProperty(string name, CngPropertyOptions options)
static CngAlgorithm EcdhCurveNameToAlgorithm(string name)
static CngKey Create(CngAlgorithm algorithm, string? keyName, CngKeyCreationParameters? creationParameters)
static CngKey Create(CngAlgorithm algorithm)
static CngKey Open(string keyName, CngProvider provider, CngKeyOpenOptions openOptions)
static bool Exists(string keyName)
static bool Exists(string keyName, CngProvider provider)
string GetECSpecificCurveName(out string oidValue)
static bool Exists(string keyName, CngProvider provider, CngKeyOpenOptions options)
static bool IsECNamedCurve(string algorithm)
static CngKey Open(string keyName, CngProvider provider)
unsafe CngUIPolicy UIPolicy
static CngProperty GetPropertyFromNamedCurve(ECCurve curve)
static CngKey Import(byte[] keyBlob, CngKeyBlobFormat format)
readonly SafeNCryptProviderHandle _providerHandle
static CngKey Open(string keyName)
static CngKey Import(ReadOnlySpan< byte > keyBlob, string curveName, CngKeyBlobFormat format, CngProvider provider)
byte[] Export(CngKeyBlobFormat format)
static CngKey Import(byte[] keyBlob, CngKeyBlobFormat format, CngProvider provider)
SafeNCryptProviderHandle ProviderHandle
static unsafe bool ExportPkcs8KeyBlob(bool allocate, SafeNCryptKeyHandle keyHandle, ReadOnlySpan< char > password, int kdfCount, Span< byte > destination, out int bytesWritten, out byte[] allocated)
unsafe void SetProperty(CngProperty property)
static readonly byte[] s_pkcs12TripleDesOidBytes
static CngKey Create(CngAlgorithm algorithm, string? keyName)
unsafe IntPtr ParentWindowHandle
static unsafe void InitializeKeyProperties(SafeNCryptKeyHandle keyHandle, CngKeyCreationParameters creationParameters)
string GetCurveName(out string oidValue)
byte[] ExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount)
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
CngExportPolicies ExportPolicy
bool TryExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount, Span< byte > destination, out int bytesWritten)
static CngKey Import(byte[] keyBlob, string curveName, CngKeyBlobFormat format, CngProvider provider)
static unsafe void InitializeKeyUiPolicyProperties(SafeNCryptKeyHandle keyHandle, CngUIPolicy uiPolicy)
readonly SafeNCryptKeyHandle _keyHandle
SafeNCryptKeyHandle Handle
static CngKey Import(byte[] keyBlob, string curveName, CngKeyBlobFormat format)
CngAlgorithmGroup? AlgorithmGroup
static CngProvider MicrosoftSoftwareKeyStorageProvider
CngUIProtectionLevels ProtectionLevel
static SafeNCryptKeyHandle ImportKeyBlob(string blobType, ReadOnlySpan< byte > keyBlob, string curveName, SafeNCryptProviderHandle provider)
static void Fill(Span< byte > data)
static readonly IntPtr Zero
CngPropertyOptions Options
byte[] GetValueWithoutCopying()