internal X509Certificate CertificateFromData(byte[] data) { X509Certificate cert = new X509Certificate(data); #if !NET_2_1 // If privateKey it's available, load it too.. CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = CryptoConvert.ToHex(cert.Hash); if (_storePath.StartsWith(X509StoreManager.LocalMachinePath)) { cspParams.Flags = CspProviderFlags.UseMachineKeyStore; } KeyPairPersistence kpp = new KeyPairPersistence(cspParams); if (!kpp.Load()) { return(cert); } if (cert.RSA != null) { cert.RSA = new RSACryptoServiceProvider(cspParams); } else if (cert.DSA != null) { cert.DSA = new DSACryptoServiceProvider(cspParams); } #endif return(cert); }
public void CspFlagsDefaultMachine() { try { CspParameters cp = new CspParameters(-7, "Provider", "Container"); cp.Flags = CspProviderFlags.UseDefaultKeyContainer | CspProviderFlags.UseMachineKeyStore; KeyPairPersistence kpp = new KeyPairPersistence(cp, "<keypair/>"); kpp.Save(); Assert.IsTrue(File.Exists(kpp.Filename), "Save-Exists"); KeyPairPersistence kpp2 = new KeyPairPersistence(cp); Assert.IsTrue(kpp2.Load(), "Load"); Compare(kpp, kpp2); kpp.Remove(); Assert.IsFalse(File.Exists(kpp.Filename), "Remove-!Exists"); } catch (CryptographicException ce) { // not everyone can write to the machine store if (!(ce.InnerException is UnauthorizedAccessException)) { throw; } Assert.Ignore("Access denied to key containers files."); } catch (UnauthorizedAccessException) { Assert.Ignore("Access denied to key containers files."); } }
static void DisplayCertificate(X509Certificate x509, bool machine, bool verbose) { Console.WriteLine("{0}X.509 v{1} Certificate", (x509.IsSelfSigned ? "Self-signed " : String.Empty), x509.Version); Console.WriteLine(" Serial Number: {0}", CryptoConvert.ToHex(x509.SerialNumber)); Console.WriteLine(" Issuer Name: {0}", x509.IssuerName); Console.WriteLine(" Subject Name: {0}", x509.SubjectName); Console.WriteLine(" Valid From: {0}", x509.ValidFrom); Console.WriteLine(" Valid Until: {0}", x509.ValidUntil); Console.WriteLine(" Unique Hash: {0}", CryptoConvert.ToHex(x509.Hash)); if (verbose) { Console.WriteLine(" Key Algorithm: {0}", x509.KeyAlgorithm); Console.WriteLine(" Algorithm Parameters: {0}", (x509.KeyAlgorithmParameters == null) ? "None" : CryptoConvert.ToHex(x509.KeyAlgorithmParameters)); Console.WriteLine(" Public Key: {0}", CryptoConvert.ToHex(x509.PublicKey)); Console.WriteLine(" Signature Algorithm: {0}", x509.SignatureAlgorithm); Console.WriteLine(" Algorithm Parameters: {0}", (x509.SignatureAlgorithmParameters == null) ? "None" : CryptoConvert.ToHex(x509.SignatureAlgorithmParameters)); Console.WriteLine(" Signature: {0}", CryptoConvert.ToHex(x509.Signature)); RSACryptoServiceProvider rsaCsp = x509.RSA as RSACryptoServiceProvider; RSAManaged rsaManaged = x509.RSA as RSAManaged; Console.WriteLine(" Private Key: {0}", ((rsaCsp != null && !rsaCsp.PublicOnly) || (rsaManaged != null && !rsaManaged.PublicOnly))); CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = CryptoConvert.ToHex(x509.Hash); cspParams.Flags = machine ? CspProviderFlags.UseMachineKeyStore : 0; KeyPairPersistence kpp = new KeyPairPersistence(cspParams); Console.WriteLine(" KeyPair Key: {0}", kpp.Load()); } Console.WriteLine(); }
private X509Certificate LoadCertificate(string filename) { byte[] data = Load(filename); X509Certificate cert = new X509Certificate(data); #if !NET_2_1 // If privateKey it's available, load it too.. CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = CryptoConvert.ToHex(cert.Hash); cspParams.Flags = CspProviderFlags.UseMachineKeyStore; KeyPairPersistence kpp = new KeyPairPersistence(cspParams); if (!kpp.Load()) { return(cert); } if (cert.RSA != null) { cert.RSA = new RSACryptoServiceProvider(cspParams); } else if (cert.DSA != null) { cert.DSA = new DSACryptoServiceProvider(cspParams); } #endif return(cert); }
void Common(CspParameters parameters) { store = new KeyPairPersistence(parameters); store.Load(); if (store.KeyValue != null) { persisted = true; this.FromXmlString(store.KeyValue); } }
void Common(CspParameters p) { store = new KeyPairPersistence(p); bool exists = store.Load(); bool required = (p.Flags & CspProviderFlags.UseExistingKey) != 0; if (required && !exists) { throw new CryptographicException("Keyset does not exist"); } if (store.KeyValue != null) { persisted = true; FromXmlString(store.KeyValue); } }
public void CspTypeProviderContainer() { try { CspParameters cp = new CspParameters(-3, "Provider", "Container"); KeyPairPersistence kpp = new KeyPairPersistence(cp, "<keypair/>"); kpp.Save(); Assert.IsTrue(File.Exists(kpp.Filename), "Save-Exists"); KeyPairPersistence kpp2 = new KeyPairPersistence(cp); Assert.IsTrue(kpp2.Load(), "Load"); Compare(kpp, kpp2); kpp.Remove(); Assert.IsFalse(File.Exists(kpp.Filename), "Remove-!Exists"); } catch (UnauthorizedAccessException) { Assert.Ignore("Access denied to key containers files."); } }
private X509Certificate LoadCertificate(string filename) { byte[] data = Load(filename); X509Certificate cert = new X509Certificate(data); #if !MOBILE // If privateKey it's available, load it too.. CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = CryptoConvert.ToHex(cert.Hash); if (_storePath.StartsWith(X509StoreManager.LocalMachinePath) || _storePath.StartsWith(X509StoreManager.NewLocalMachinePath)) { cspParams.Flags = CspProviderFlags.UseMachineKeyStore; } KeyPairPersistence kpp = new KeyPairPersistence(cspParams); try { if (!kpp.Load()) { return(cert); } } catch { return(cert); } if (cert.RSA != null) { cert.RSA = new RSACryptoServiceProvider(cspParams); } else if (cert.DSA != null) { cert.DSA = new DSACryptoServiceProvider(cspParams); } #endif return(cert); }