public void CanGetAndSetProperties() { int serial = 101; X509Name subject = new X509Name("CN=localhost"); X509Name issuer = new X509Name("CN=Root"); DateTime start = DateTime.Now; DateTime end = start + TimeSpan.FromMinutes(10); CryptoKey key = new CryptoKey(new DSA(true)); int bits = key.Bits; X509Name saveIssuer = null; X509Name saveSubject = null; CryptoKey savePublicKey = null; CryptoKey savePrivateKey = null; using (X509Certificate cert = new X509Certificate()) { cert.Subject = subject; cert.Issuer = issuer; cert.SerialNumber = serial; cert.NotBefore = start; cert.NotAfter = end; cert.PublicKey = key; cert.PrivateKey = key; Assert.AreEqual(subject, cert.Subject); Assert.AreEqual(issuer, cert.Issuer); Assert.AreEqual(serial, cert.SerialNumber); Assert.AreEqual(key, cert.PublicKey); Assert.AreEqual(key, cert.PrivateKey); // If the original key gets disposed before the internal private key, // make sure that memory is correctly managed key.Dispose(); // If the internal private key has already been disposed, this will blowup Assert.AreEqual(bits, cert.PublicKey.Bits); Assert.AreEqual(bits, cert.PrivateKey.Bits); // We compare short date/time strings here because the wrapper can't handle milliseconds Assert.AreEqual(start.ToShortDateString(), cert.NotBefore.ToShortDateString()); Assert.AreEqual(start.ToShortTimeString(), cert.NotBefore.ToShortTimeString()); saveSubject = cert.Subject; saveIssuer = cert.Issuer; savePublicKey = cert.PublicKey; savePrivateKey = cert.PrivateKey; } // make sure that a property torn-off from the cert is still valid Assert.AreEqual(subject, saveSubject); Assert.AreEqual(issuer, saveIssuer); Assert.AreEqual(bits, savePublicKey.Bits); Assert.AreEqual(bits, savePrivateKey.Bits); }
private void DisposeKey(CryptoKey cryptoKey, Exception rootException) { try { cryptoKey.Dispose(); } catch (Exception e) { // If called w/ root exception, fill in as the cause if (rootException != null) { // Can't inject base/root cause like in java, so use AggregateException to wrap them AggregateException aggregateException = new AggregateException(e, rootException); throw new AppEncryptionException( $"Failed to dispose/wipe key, error: {e.Message}", aggregateException); } throw new AppEncryptionException($"Failed to dispose/wipe key, error: {e.Message}", e); } }