private static void SetKeyProperties(SafeNCryptKeyHandle keyHandle, CngKeyCreationParameters creationParameters) { if (creationParameters.ExportPolicy.HasValue) { NCryptNative.SetProperty(keyHandle, "Export Policy", creationParameters.ExportPolicy.Value, CngPropertyOptions.None | CngPropertyOptions.Persist); } if (creationParameters.KeyUsage.HasValue) { NCryptNative.SetProperty(keyHandle, "Key Usage", creationParameters.KeyUsage.Value, CngPropertyOptions.None | CngPropertyOptions.Persist); } if (creationParameters.ParentWindowHandle != IntPtr.Zero) { NCryptNative.SetProperty <IntPtr>(keyHandle, "HWND Handle", creationParameters.ParentWindowHandle, CngPropertyOptions.None); } if (creationParameters.UIPolicy != null) { NCryptNative.NCRYPT_UI_POLICY ncrypt_ui_policy = new NCryptNative.NCRYPT_UI_POLICY { dwVersion = 1, dwFlags = creationParameters.UIPolicy.ProtectionLevel, pszCreationTitle = creationParameters.UIPolicy.CreationTitle, pszFriendlyName = creationParameters.UIPolicy.FriendlyName, pszDescription = creationParameters.UIPolicy.Description }; NCryptNative.SetProperty <NCryptNative.NCRYPT_UI_POLICY>(keyHandle, "UI Policy", ncrypt_ui_policy, CngPropertyOptions.None | CngPropertyOptions.Persist); if (creationParameters.UIPolicy.UseContext != null) { NCryptNative.SetProperty(keyHandle, "Use Context", creationParameters.UIPolicy.UseContext, CngPropertyOptions.None | CngPropertyOptions.Persist); } } foreach (CngProperty property in creationParameters.ParametersNoDemand) { NCryptNative.SetProperty(keyHandle, property.Name, property.Value, property.Options); } }
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); } }