本文整理汇总了C#中Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle类的典型用法代码示例。如果您正苦于以下问题:C# SafeNCryptKeyHandle类的具体用法?C# SafeNCryptKeyHandle怎么用?C# SafeNCryptKeyHandle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SafeNCryptKeyHandle类属于Microsoft.Win32.SafeHandles命名空间,在下文中一共展示了SafeNCryptKeyHandle类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NCryptVerifySignature
internal static extern int NCryptVerifySignature( SafeNCryptKeyHandle hKey,
[In] ref NCRYPT_PKCS1_PADDING_INFO pPaddingInfo,
[In, MarshalAs( UnmanagedType.LPArray )] byte[] pbHashValue,
int cbHashValue,
[In, MarshalAs( UnmanagedType.LPArray )] byte[] pbSignature,
int cbSignature,
AsymmetricPaddingMode dwFlags );
示例2: ImportKeyBlob
private void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
{
// Use generic blob type for multiple version support
string blobType = includePrivate ?
Interop.BCrypt.KeyBlobType.BCRYPT_PRIVATE_KEY_BLOB :
Interop.BCrypt.KeyBlobType.BCRYPT_PUBLIC_KEY_BLOB;
SafeNCryptKeyHandle keyHandle = CngKeyLite.ImportKeyBlob(blobType, rsaBlob);
Debug.Assert(!keyHandle.IsInvalid);
_keyHandle = keyHandle;
int newKeySize = CngKeyLite.GetKeyLength(keyHandle);
// Our LegalKeySizes value stores the values that we encoded as being the correct
// legal key size limitations for this algorithm, as documented on MSDN.
//
// But on a new OS version we might not question if our limit is accurate, or MSDN
// could have been inaccurate to start with.
//
// Since the key is already loaded, we know that Windows thought it to be valid;
// therefore we should set KeySizeValue directly to bypass the LegalKeySizes conformance
// check.
ForceSetKeySize(newKeySize);
_lastKeySize = newKeySize;
}
示例3: ImportKeyBlob
private void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
{
string blobType = includePrivate ?
Interop.BCrypt.KeyBlobType.BCRYPT_RSAPRIVATE_BLOB :
Interop.BCrypt.KeyBlobType.BCRYPT_PUBLIC_KEY_BLOB;
SafeNCryptKeyHandle keyHandle = CngKeyLite.ImportKeyBlob(blobType, rsaBlob);
Debug.Assert(!keyHandle.IsInvalid);
_keyHandle = keyHandle;
int newKeySize = CngKeyLite.GetKeyLength(keyHandle);
// Our LegalKeySizes value stores the values that we encoded as being the correct
// legal key size limitations for this algorithm, as documented on MSDN.
//
// But on a new OS version we might not question if our limit is accurate, or MSDN
// could have been inaccurate to start with.
//
// Since the key is already loaded, we know that Windows thought it to be valid;
// therefore we should set KeySizeValue directly to bypass the LegalKeySizes conformance
// check.
//
// For RSA there are known cases where this change matters. RSACryptoServiceProvider can
// create a 384-bit RSA key, which we consider too small to be legal. It can also create
// a 1032-bit RSA key, which we consider illegal because it doesn't match our 64-bit
// alignment requirement. (In both cases Windows loads it just fine)
ForceSetKeySize(newKeySize);
_lastKeySize = newKeySize;
}
示例4: CertCreateSelfSignCertificate
private static extern SafeCertContextHandle CertCreateSelfSignCertificate(SafeNCryptKeyHandle hCryptProvOrNCryptKey,
[In] ref CRYPTOAPI_BLOB pSubjectIssuerBlob,
X509CertificateCreationOptions dwFlags,
[In] ref CRYPT_KEY_PROV_INFO pKeyProvInfo,
[In] ref CRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
[In] ref SYSTEMTIME pStartTime,
[In] ref SYSTEMTIME pEndTime,
[In] ref CERT_EXTENSIONS pExtensions);
示例5: CngKey
private CngKey(SafeNCryptProviderHandle providerHandle, SafeNCryptKeyHandle keyHandle)
{
Debug.Assert(keyHandle != null && !keyHandle.IsInvalid && !keyHandle.IsClosed);
Debug.Assert(providerHandle != null && !providerHandle.IsInvalid && !providerHandle.IsClosed);
_providerHandle = providerHandle;
_keyHandle = keyHandle;
}
示例6: NCryptImportKey
internal static extern int NCryptImportKey( SafeNCryptProviderHandle hProvider,
IntPtr hImportKey,
[MarshalAs( UnmanagedType.LPWStr )] string pszBlobType,
[In, MarshalAs( UnmanagedType.LPArray )] byte[] pParameterList,
out SafeNCryptKeyHandle phKey,
[In, MarshalAs( UnmanagedType.LPArray )] byte[] pbData,
int cbData,
int dwFlags );
示例7: Open
/// <summary>
/// Wrap an existing key handle with a CngKey object
/// </summary>
public static CngKey Open(SafeNCryptKeyHandle keyHandle, CngKeyHandleOpenOptions keyHandleOpenOptions)
{
if (keyHandle == null)
throw new ArgumentNullException("keyHandle");
if (keyHandle.IsClosed || keyHandle.IsInvalid)
throw new ArgumentException(SR.Cryptography_OpenInvalidHandle, "keyHandle");
SafeNCryptKeyHandle keyHandleCopy = keyHandle.Duplicate();
// Get a handle to the key's provider.
SafeNCryptProviderHandle providerHandle = new SafeNCryptProviderHandle();
IntPtr rawProviderHandle = keyHandle.GetPropertyAsIntPtr(KeyPropertyName.ProviderHandle, CngPropertyOptions.None);
providerHandle.SetHandleValue(rawProviderHandle);
// Set up a key object wrapping the handle
CngKey key = null;
try
{
key = new CngKey(providerHandle, keyHandleCopy);
bool openingEphemeralKey = (keyHandleOpenOptions & CngKeyHandleOpenOptions.EphemeralKey) == CngKeyHandleOpenOptions.EphemeralKey;
//
// If we're wrapping a handle to an ephemeral key, we need to make sure that IsEphemeral is
// set up to return true. In the case that the handle is for an ephemeral key that was created
// by the CLR, then we don't have anything to do as the IsEphemeral CLR property will already
// be setup. However, if the key was created outside of the CLR we will need to setup our
// ephemeral detection property.
//
// This enables consumers of CngKey objects to always be able to rely on the result of
// calling IsEphemeral, and also allows them to safely access the Name property.
//
// Finally, if we detect that this is an ephemeral key that the CLR created but we were not
// told that it was an ephemeral key we'll throw an exception. This prevents us from having
// to decide who to believe -- the key property or the caller of the API. Since other code
// relies on the ephemeral flag being set properly to avoid tripping over bugs in CNG, we
// need to reject the case that we suspect that the flag is incorrect.
//
if (!key.IsEphemeral && openingEphemeralKey)
{
key.IsEphemeral = true;
}
else if (key.IsEphemeral && !openingEphemeralKey)
{
throw new ArgumentException(SR.Cryptography_OpenEphemeralKeyHandleWithoutEphemeralFlag, "keyHandleOpenOptions");
}
}
catch
{
// Make sure that we don't leak the handle the CngKey duplicated
if (key != null)
key.Dispose();
throw;
}
return key;
}
示例8: CngKey
private CngKey(SafeNCryptProviderHandle kspHandle, SafeNCryptKeyHandle keyHandle) {
Contract.Requires(keyHandle != null && !keyHandle.IsInvalid && !keyHandle.IsClosed);
Contract.Requires(kspHandle != null && !kspHandle.IsInvalid && !kspHandle.IsClosed);
Contract.Ensures(m_keyHandle != null && !m_keyHandle.IsInvalid && !m_keyHandle.IsClosed);
Contract.Ensures(kspHandle != null && !kspHandle.IsInvalid && !kspHandle.IsClosed);
m_keyHandle = keyHandle;
m_kspHandle = kspHandle;
}
示例9: ImportKeyBlob
private void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
{
string blobType = includePrivate ?
Interop.BCrypt.KeyBlobType.BCRYPT_RSAPRIVATE_BLOB :
Interop.BCrypt.KeyBlobType.BCRYPT_PUBLIC_KEY_BLOB;
SafeNCryptKeyHandle keyHandle = CngKeyLite.ImportKeyBlob(blobType, rsaBlob);
Debug.Assert(!keyHandle.IsInvalid);
_keyHandle = keyHandle;
int newKeySize = CngKeyLite.GetKeyLength(keyHandle);
KeySize = _lastKeySize = newKeySize;
}
示例10: SafeNCryptKeyHandle_InvalidKey_ParentHandle_NotKeptAlive
public static void SafeNCryptKeyHandle_InvalidKey_ParentHandle_NotKeptAlive()
{
using (SafeHandle parentHandle = new StateInformingSafeHandle())
using (var keyHandle = new SafeNCryptKeyHandle(IntPtr.Zero, parentHandle))
{
Assert.False(parentHandle.IsInvalid, "After ctor, parentHandle.IsInvalid");
Assert.False(parentHandle.IsClosed, "After ctor, parentHandle.IsClosed");
parentHandle.Dispose();
Assert.True(parentHandle.IsInvalid, "After parentHandle.Dispose, parentHandle.IsInvalid");
Assert.True(parentHandle.IsClosed, "After parentHandle.Dispose, parentHandle.IsClosed");
}
}
示例11: InitializeKeyProperties
/// <summary>
/// Setup the key properties specified in the key creation parameters
/// </summary>
private static void InitializeKeyProperties(SafeNCryptKeyHandle keyHandle, CngKeyCreationParameters creationParameters)
{
unsafe
{
if (creationParameters.ExportPolicy.HasValue)
{
CngExportPolicies exportPolicy = creationParameters.ExportPolicy.Value;
keyHandle.SetExportPolicy(exportPolicy);
}
if (creationParameters.KeyUsage.HasValue)
{
CngKeyUsages keyUsage = creationParameters.KeyUsage.Value;
ErrorCode errorCode = Interop.NCrypt.NCryptSetProperty(keyHandle, KeyPropertyName.KeyUsage, &keyUsage, sizeof(CngKeyUsages), CngPropertyOptions.Persist);
if (errorCode != ErrorCode.ERROR_SUCCESS)
throw errorCode.ToCryptographicException();
}
if (creationParameters.ParentWindowHandle != IntPtr.Zero)
{
IntPtr parentWindowHandle = creationParameters.ParentWindowHandle;
ErrorCode errorCode = Interop.NCrypt.NCryptSetProperty(keyHandle, KeyPropertyName.ParentWindowHandle, &parentWindowHandle, sizeof(IntPtr), CngPropertyOptions.None);
if (errorCode != ErrorCode.ERROR_SUCCESS)
throw errorCode.ToCryptographicException();
}
CngUIPolicy uiPolicy = creationParameters.UIPolicy;
if (uiPolicy != null)
{
InitializeKeyUiPolicyProperties(keyHandle, uiPolicy);
}
// Iterate over the custom properties, setting those as well.
foreach (CngProperty property in creationParameters.Parameters)
{
byte[] value = property.GetValueWithoutCopying();
int valueLength = (value == null) ? 0 : value.Length;
fixed (byte* pValue = value.MapZeroLengthArrayToNonNullPointer())
{
ErrorCode errorCode = Interop.NCrypt.NCryptSetProperty(keyHandle, property.Name, pValue, valueLength, property.Options);
if (errorCode != ErrorCode.ERROR_SUCCESS)
throw errorCode.ToCryptographicException();
}
}
}
}
示例12: GetDuplicatedKeyHandle
private SafeNCryptKeyHandle GetDuplicatedKeyHandle()
{
int keySize = KeySize;
if (_lastKeySize != keySize)
{
if (_keyHandle != null)
{
_keyHandle.Dispose();
}
const string BCRYPT_RSA_ALGORITHM = "RSA";
_keyHandle = CngKeyLite.GenerateNewExportableKey(BCRYPT_RSA_ALGORITHM, keySize);
_lastKeySize = keySize;
}
return new DuplicateSafeNCryptKeyHandle(_keyHandle);
}
示例13: ExportKeyBlob
internal static byte[] ExportKeyBlob(SafeNCryptKeyHandle keyHandle, string blobType)
{
Debug.Assert(!keyHandle.IsInvalid);
int numBytesNeeded;
ErrorCode errorCode = Interop.NCrypt.NCryptExportKey(
keyHandle,
IntPtr.Zero,
blobType,
IntPtr.Zero,
null,
0,
out numBytesNeeded,
0);
if (errorCode != ErrorCode.ERROR_SUCCESS)
{
throw errorCode.ToCryptographicException();
}
byte[] buffer = new byte[numBytesNeeded];
errorCode = Interop.NCrypt.NCryptExportKey(
keyHandle,
IntPtr.Zero,
blobType,
IntPtr.Zero,
buffer,
buffer.Length,
out numBytesNeeded,
0);
if (errorCode != ErrorCode.ERROR_SUCCESS)
{
throw errorCode.ToCryptographicException();
}
Array.Resize(ref buffer, numBytesNeeded);
return buffer;
}
示例14: GetDuplicatedKeyHandle
private SafeNCryptKeyHandle GetDuplicatedKeyHandle()
{
if (IsECNamedCurve(_lastAlgorithm))
{
// Curve was previously created, so use that
return new DuplicateSafeNCryptKeyHandle(_keyHandle);
}
else
{
string algorithm = null;
int keySize = KeySize;
if (_lastKeySize != keySize)
{
// Map the current key size to a CNG algorithm name
switch (keySize)
{
case 256: algorithm = AlgorithmName.ECDsaP256; break;
case 384: algorithm = AlgorithmName.ECDsaP384; break;
case 521: algorithm = AlgorithmName.ECDsaP521; break;
default:
Debug.Fail("Should not have invalid key size");
throw new ArgumentException(SR.Cryptography_InvalidKeySize);
}
if (_keyHandle != null)
{
DisposeKey();
}
_keyHandle = CngKeyLite.GenerateNewExportableKey(algorithm, keySize);
_lastKeySize = keySize;
_lastAlgorithm = algorithm;
ForceSetKeySize(keySize);
}
return new DuplicateSafeNCryptKeyHandle(_keyHandle);
}
}
示例15: SetKeyProperties
private static void SetKeyProperties(SafeNCryptKeyHandle keyHandle,
CngKeyCreationParameters creationParameters) {
Contract.Requires(keyHandle != null && !keyHandle.IsInvalid && !keyHandle.IsClosed);
Contract.Requires(creationParameters != null);
//
// Setup the well-known properties.
//
if (creationParameters.ExportPolicy.HasValue) {
NCryptNative.SetProperty(keyHandle,
NCryptNative.KeyPropertyName.ExportPolicy,
(int)creationParameters.ExportPolicy.Value,
CngPropertyOptions.Persist);
}
if (creationParameters.KeyUsage.HasValue) {
NCryptNative.SetProperty(keyHandle,
NCryptNative.KeyPropertyName.KeyUsage,
(int)creationParameters.KeyUsage.Value,
CngPropertyOptions.Persist);
}
if (creationParameters.ParentWindowHandle != IntPtr.Zero) {
NCryptNative.SetProperty(keyHandle,
NCryptNative.KeyPropertyName.ParentWindowHandle,
creationParameters.ParentWindowHandle,
CngPropertyOptions.None);
}
if (creationParameters.UIPolicy != null) {
NCryptNative.NCRYPT_UI_POLICY uiPolicy = new NCryptNative.NCRYPT_UI_POLICY();
uiPolicy.dwVersion = 1;
uiPolicy.dwFlags = creationParameters.UIPolicy.ProtectionLevel;
uiPolicy.pszCreationTitle = creationParameters.UIPolicy.CreationTitle;
uiPolicy.pszFriendlyName = creationParameters.UIPolicy.FriendlyName;
uiPolicy.pszDescription = creationParameters.UIPolicy.Description;
NCryptNative.SetProperty(keyHandle,
NCryptNative.KeyPropertyName.UIPolicy,
uiPolicy,
CngPropertyOptions.Persist);
// The use context is a seperate property from the standard UI context
if (creationParameters.UIPolicy.UseContext != null) {
NCryptNative.SetProperty(keyHandle,
NCryptNative.KeyPropertyName.UseContext,
creationParameters.UIPolicy.UseContext,
CngPropertyOptions.Persist);
}
}
// Iterate over the custom properties, setting those as well.
foreach (CngProperty property in creationParameters.ParametersNoDemand) {
NCryptNative.SetProperty(keyHandle, property.Name, property.Value, property.Options);
}
}