public void AsymmetricSignatureProvider_Publics() { AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SigningKey as AsymmetricSecurityKey, KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SignatureAlgorithm); SignatureProvider_SignVariation(provider, null, null, ExpectedException.ArgumentNullException()); SignatureProvider_SignVariation(provider, new byte[0], null, ExpectedException.ArgumentException("IDX10624:")); SignatureProvider_SignVariation(provider, new byte[1], null, ExpectedException.InvalidOperationException("IDX10620:")); SignatureProvider_VerifyVariation(provider, null, null, ExpectedException.ArgumentNullException()); SignatureProvider_VerifyVariation(provider, new byte[1], null, ExpectedException.ArgumentNullException()); SignatureProvider_VerifyVariation(provider, new byte[0], new byte[1], ExpectedException.ArgumentException("IDX10625:")); SignatureProvider_VerifyVariation(provider, new byte[1], new byte[0], ExpectedException.ArgumentException("IDX10626:")); }
public void SignatureProviders_SignAndVerify() { // asymmetric try { Random r = new Random(); AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_2048, SecurityAlgorithms.RsaSha256Signature); byte[] bytesin = new byte[1024]; r.NextBytes(bytesin); byte[] signature = provider.Sign(bytesin); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException)); } // asymmetric try { Random r = new Random(); AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_2048, SecurityAlgorithms.RsaSha256Signature, true); byte[] bytesin = new byte[1024]; r.NextBytes(bytesin); byte[] signature = provider.Sign(bytesin); Assert.IsTrue(provider.Verify(bytesin, signature)); } catch (Exception) { Assert.Fail("Should have thrown, it is possible that crypto config mapped this."); } // unknown algorithm try { Random r = new Random(); AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_2048, "SecurityAlgorithms.RsaSha256Signature"); Assert.Fail(string.Format("Should have thrown, it is possible that crypto config mapped this.")); } catch (Exception ex) { Assert.IsFalse(ex.GetType() != typeof(InvalidOperationException), "ex.GetType() != typeof( InvalidOperationException )"); } // symmetric try { Random r = new Random(); SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature); byte[] bytesin = new byte[1024]; r.NextBytes(bytesin); byte[] signature = provider.Sign(bytesin); Assert.IsTrue(provider.Verify(bytesin, signature), string.Format("Signature did not verify")); } catch (Exception ex) { Assert.Fail(string.Format("Unexpected exception received: '{0}'", ex)); } // symmetric different byte[] sizes try { SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature); byte[] bytesin = new byte[1024]; byte[] signature = new byte[1024]; Assert.IsFalse(provider.Verify(bytesin, signature), string.Format("Signature did not verify")); } catch (Exception ex) { Assert.Fail(string.Format("Unexpected exception received: '{0}'", ex)); } // unknown algorithm try { Random r = new Random(); SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSecurityKey_256, "SecurityAlgorithms.HmacSha256Signature"); Assert.Fail(string.Format("Should have thrown, it is possible that crypto config mapped this.")); } catch (Exception ex) { Assert.IsFalse(ex.GetType() != typeof(InvalidOperationException), "ex.GetType() != typeof( InvalidOperationException )"); } }
public void AsymmetricSignatureProvider_Dispose() { AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_Public_2048, SecurityAlgorithms.RsaSha256Signature); provider.Dispose(); ExpectedException expectedException = ExpectedException.ObjectDisposedException; try { provider.Sign(new byte[256]); } catch (Exception ex) { expectedException.ProcessException(ex); } try { provider.Verify(new byte[256], new byte[256]); } catch (Exception ex) { expectedException.ProcessException(ex); } try { provider.Dispose(); } catch (Exception ex) { Assert.Fail(string.Format("AsymmetricSignatureProvider.Dispose called twice, caught exception: '{0}'", ex)); } }
public void AsymmetricSignatureProvider_Defaults() { try { AsymmetricSignatureProvider asymmetricSignatureProvider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SigningKey as AsymmetricSecurityKey, KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SignatureAlgorithm, false); } catch (Exception) { Assert.Fail("AsymmetricSignatureProvider creation should not throw"); } try { AsymmetricSignatureProvider asymmetricSignatureProvider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SigningKey as AsymmetricSecurityKey, KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SignatureAlgorithm, true); } catch (Exception) { Assert.Fail("AsymmetricSignatureProvider creation should not throw"); } }
private void AsymmetricConstructorVariation(string testcase, AsymmetricSecurityKey key, string algorithm, ExpectedException expectedException) { Console.WriteLine(string.Format("Testcase: '{0}'", testcase)); AsymmetricSignatureProvider provider = null; try { if (testcase.StartsWith("Signing")) { provider = new AsymmetricSignatureProvider(key, algorithm, true); } else { provider = new AsymmetricSignatureProvider(key, algorithm, false); } expectedException.ProcessNoException(); } catch (Exception ex) { expectedException.ProcessException(ex); } }