public static void CreateEphemeral_WithParameters() { CngAlgorithm alg = CngAlgorithm.ECDiffieHellmanP256; CngKeyCreationParameters p = new CngKeyCreationParameters(); p.ExportPolicy = CngExportPolicies.AllowExport; p.KeyUsage = CngKeyUsages.KeyAgreement; p.UIPolicy = new CngUIPolicy(CngUIProtectionLevels.ForceHighProtection, "MyFriendlyName", "MyDescription", "MyUseContext", "MyCreationTitle"); byte[] myPropValue1 = "23afbc".HexToByteArray(); p.Parameters.Add(new CngProperty("MyProp1", myPropValue1, CngPropertyOptions.CustomProperty)); byte[] myPropValue2 = "8765".HexToByteArray(); p.Parameters.Add(new CngProperty("MyProp2", myPropValue2, CngPropertyOptions.CustomProperty)); using (CngKey key = CngKey.Create(alg, null, p)) { Assert.Equal(CngAlgorithm.ECDiffieHellmanP256, key.Algorithm); Assert.Equal(CngExportPolicies.AllowExport, key.ExportPolicy); Assert.Equal(CngKeyUsages.KeyAgreement, key.KeyUsage); CngUIPolicy uiPolicy = key.UIPolicy; Assert.Equal(CngUIProtectionLevels.ForceHighProtection, uiPolicy.ProtectionLevel); Assert.Equal("MyFriendlyName", uiPolicy.FriendlyName); Assert.Equal("MyDescription", uiPolicy.Description); Assert.Equal("MyUseContext", uiPolicy.UseContext); Assert.Equal("MyCreationTitle", uiPolicy.CreationTitle); byte[] propValue1Actual = key.GetProperty("MyProp1", CngPropertyOptions.CustomProperty).GetValue(); Assert.Equal <byte>(myPropValue1, propValue1Actual); byte[] propValue2Actual = key.GetProperty("MyProp2", CngPropertyOptions.CustomProperty).GetValue(); Assert.Equal <byte>(myPropValue2, propValue2Actual); } }
public static void TestImportKey() { using (CngKey key = CngKey.Import(TestData.Key_ECDiffieHellmanP256, CngKeyBlobFormat.GenericPublicBlob)) { Assert.Equal(CngAlgorithm.ECDiffieHellmanP256, key.Algorithm); Assert.Equal(CngAlgorithmGroup.ECDiffieHellman, key.AlgorithmGroup); Assert.Equal(CngExportPolicies.None, key.ExportPolicy); Assert.True(key.IsEphemeral); Assert.False(key.IsMachineKey); Assert.Null(key.KeyName); Assert.Equal(0x100, key.KeySize); Assert.Equal(CngKeyUsages.AllUsages, key.KeyUsage); Assert.Equal(IntPtr.Zero, key.ParentWindowHandle); Assert.Equal(CngProvider.MicrosoftSoftwareKeyStorageProvider, key.Provider); CngUIPolicy policy = key.UIPolicy; Assert.Null(policy.CreationTitle); Assert.Null(policy.Description); Assert.Null(policy.FriendlyName); Assert.Null(policy.UseContext); Assert.Equal(CngUIProtectionLevels.None, policy.ProtectionLevel); Assert.Null(key.UniqueName); } }
private static void VerifyImportedKey(CngKey key) { Assert.Equal(new CngAlgorithm("DSA"), key.Algorithm); Assert.Equal(CngAlgorithmGroup.Dsa, key.AlgorithmGroup); Assert.Equal(CngExportPolicies.None, key.ExportPolicy); Assert.Equal(true, key.IsEphemeral); Assert.Equal(false, key.IsMachineKey); Assert.Equal(null, key.KeyName); Assert.Equal(CngKeyUsages.AllUsages, key.KeyUsage); Assert.Equal(IntPtr.Zero, key.ParentWindowHandle); Assert.Equal(CngProvider.MicrosoftSoftwareKeyStorageProvider, key.Provider); CngUIPolicy policy = key.UIPolicy; Assert.Equal(null, policy.CreationTitle); Assert.Equal(null, policy.Description); Assert.Equal(null, policy.FriendlyName); Assert.Equal(null, policy.UseContext); Assert.Equal(CngUIProtectionLevels.None, policy.ProtectionLevel); Assert.Equal(null, key.UniqueName); }
public static void OpenFromHandle() { byte[] blobOriginal = TestData.Key_ECDiffieHellmanP256; using (CngKey keyOriginal = CngKey.Import(blobOriginal, CngKeyBlobFormat.GenericPublicBlob)) { SafeNCryptKeyHandle keyHandle = keyOriginal.Handle; Assert.NotNull(keyHandle); using (CngKey key = CngKey.Open(keyHandle, CngKeyHandleOpenOptions.EphemeralKey)) { keyHandle.Dispose(); Assert.Equal(keyOriginal.Algorithm, key.Algorithm); Assert.Equal(keyOriginal.AlgorithmGroup, key.AlgorithmGroup); Assert.Equal(keyOriginal.ExportPolicy, key.ExportPolicy); Assert.Equal(keyOriginal.IsEphemeral, key.IsEphemeral); Assert.Equal(keyOriginal.IsMachineKey, key.IsMachineKey); Assert.Equal(keyOriginal.KeyName, key.KeyName); Assert.Equal(keyOriginal.KeySize, key.KeySize); Assert.Equal(keyOriginal.KeyUsage, key.KeyUsage); Assert.Equal(keyOriginal.ParentWindowHandle, key.ParentWindowHandle); Assert.Equal(keyOriginal.Provider, key.Provider); CngUIPolicy policy = key.UIPolicy; Assert.Equal(keyOriginal.UIPolicy.CreationTitle, policy.CreationTitle); Assert.Equal(keyOriginal.UIPolicy.Description, policy.Description); Assert.Equal(keyOriginal.UIPolicy.FriendlyName, policy.FriendlyName); Assert.Equal(keyOriginal.UIPolicy.UseContext, policy.UseContext); Assert.Equal(keyOriginal.UIPolicy.ProtectionLevel, policy.ProtectionLevel); Assert.Equal(keyOriginal.UniqueName, key.UniqueName); byte[] reExported = key.Export(CngKeyBlobFormat.GenericPublicBlob); Assert.Equal <byte>(blobOriginal, reExported); } } }