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); if (_storePath.StartsWith (X509StoreManager.LocalMachinePath)) 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; }
public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters) { LegalKeySizesValue = new KeySizes [1]; LegalKeySizesValue [0] = new KeySizes (512, 1024, 64); // will throw an exception is key size isn't supported KeySize = dwKeySize; dsa = new DSAManaged (dwKeySize); dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated); persistKey = (parameters != null); if (parameters == null) { parameters = new CspParameters (PROV_DSS_DH); if (useMachineKeyStore) parameters.Flags |= CspProviderFlags.UseMachineKeyStore; store = new KeyPairPersistence (parameters); // no need to load - it cannot exists } else { store = new KeyPairPersistence (parameters); store.Load (); if (store.KeyValue != null) { persisted = true; this.FromXmlString (store.KeyValue); } } }
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); } }
private void Common (int dwKeySize, CspParameters p) { // Microsoft RSA CSP can do between 384 and 16384 bits keypair LegalKeySizesValue = new KeySizes [1]; LegalKeySizesValue [0] = new KeySizes (384, 16384, 8); base.KeySize = dwKeySize; rsa = new RSAManaged (KeySize); rsa.KeyGenerated += new RSAManaged.KeyGeneratedEventHandler (OnKeyGenerated); persistKey = (p != null); if (p == null) { p = new CspParameters (PROV_RSA_FULL); #if NET_1_1 if (useMachineKeyStore) p.Flags |= CspProviderFlags.UseMachineKeyStore; #endif store = new KeyPairPersistence (p); // no need to load - it cannot exists } else { store = new KeyPairPersistence (p); store.Load (); if (store.KeyValue != null) { persisted = true; this.FromXmlString (store.KeyValue); } } }
private void Common (int dwKeySize, CspParameters p) { // Microsoft RSA CSP can do between 384 and 16384 bits keypair LegalKeySizesValue = new KeySizes [1]; LegalKeySizesValue [0] = new KeySizes (384, 16384, 8); base.KeySize = dwKeySize; rsa = new RSAManaged (KeySize); rsa.KeyGenerated += new RSAManaged.KeyGeneratedEventHandler (OnKeyGenerated); persistKey = (p != null); if (p == null) { p = new CspParameters (PROV_RSA_FULL); if (useMachineKeyStore) p.Flags |= CspProviderFlags.UseMachineKeyStore; store = new KeyPairPersistence (p); // no need to load - it cannot exists } else { 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; this.FromXmlString (store.KeyValue); } } }
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 (); }
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."); } }
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."); } }