2015-09-11 2 views
5

Je construis une application console C# qui va:certificat SSL Échec de l'ajout, Erreur: 1312

[1] Générez un certificat auto-signé.

[2.] Ajoutez au personnel (magasin de l'ordinateur local)

[3.] Enfin attribuer ce certificat à un numéro de port sur la machine avec la commande netsh.

Jusqu'à présent, je me suis parties [1.] et [2.] fonctionne parfaitement, mais [3.] Je frappai avec le message d'erreur inutile et non informel:

SSL Certificate add failed, Error: 1312 A specified logon session does not exist. It may already have been terminated.

Quand je vais regarder la page officielle de Microsoft sur cette question: https://support.microsoft.com/en-us/kb/981506

Il me dit essentiellement que c'est un bug du système d'exploitation Windows et que je demander un correctif.

Mon Hack solution à ce problème:

Une façon j'ai pu enfin contourner cette erreur, a été par IIS ouverture Accueil> Ouvrir « certificats de serveur » Feature> Et puis Importer mon certificat .pfx. En important le fichier .pfx dans IIS, j'ai semblé pouvoir contourner le problème sans problème. Je ne avais besoin pour générer un .pfx en exécutant ces deux deux commandes pour

1.) C:\OpenSSL-Win32\bin>openssl req -x509 -sha256 -nodes -days 365 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

2.) C:\OpenSSL-Win32\bin>openssl pkcs12 -export -out cert.pfx -inkey privateKey.key -in certificate.crt -passout pass:

Donc, si je lance ces deux commandes à OpenSSL en ce moment, et l'importation par IIS à mon magasin de certificats Personal Local Computer, je n'aurai pas de problème SSL Certificate add failed, Error: 1312. Mais si j'ajoute le certificat nouvellement généré par programme dans mon magasin de certificats Personal Local Computer, le problème Error: 1312 apparaît.

Voici mon code:

using CERTENROLLLib; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Cryptography.X509Certificates; 
using System.Text; 
using System.Threading.Tasks; 

using System.IO; 
using System.Text; 
using System.Security.Cryptography; 
using System.Diagnostics; 

namespace Generate_X509_Certificate 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      Console.WriteLine(Guid.NewGuid().ToString()); 
      Console.ReadLine(); 

      // Launch OpenSSL: 
      string cPath = @"C:\OpenSSL-Win32\bin\"; 
      string filename = Path.Combine(cPath, @"openssl.exe"); 

      // Generate a .crt file 
      Console.WriteLine(@"Generating SSL Certificate..."); 
      ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\OpenSSL-Win32\bin\openssl.exe", @"req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt -subj ""/C=US/ST=California/L=SanFrancisco/CN=SecurityEncryption"""); 
      Process.Start(startInfo); 

      // Combine the .crt with the .key to form a more Windows friendly .pfx 
      Console.WriteLine(@"Combining Private Key With Certificate..."); 
      Process proc2 = Process.Start(filename, "pkcs12 -export -out cert.pfx -inkey privateKey.key -in certificate.crt -passout pass:"); 
      proc2.Close(); 

      // Store our newly created .pfx file as a variable 
      X509Certificate2 cert = new X509Certificate2(Directory.GetCurrentDirectory()[email protected]"\cert.pfx"); 

      // Add our .pfx file into the Personal Local Computer store: 
      var store = new X509Store(StoreLocation.LocalMachine); 
      store.Open(OpenFlags.ReadWrite); 
      store.Add(cert); 
      store.Close(); 

      // Finally, use netsh to assign this newly generated certificate to port 6613 
      string s1 = "netsh http add sslcert ipport=0.0.0.0:6613 certhash=‎‎‎" + cert.GetCertHashString() + " appid={" + Guid.NewGuid().ToString() + "}"; 
      Process p1 = new Process(); 
      p1.StartInfo.FileName = "netsh.exe"; 
      p1.StartInfo.Arguments = s1; 
      p1.StartInfo.UseShellExecute = false; 
      p1.StartInfo.RedirectStandardOutput = true; 

      // this is where I get the error "SSL Certificate add failed, Error: 1312" 
      p1.Start(); 
     } 
    } 
} 

Voici la commande netsh qui fonctionne parfaitement bien tant que je ne suis pas l'exécuter dans ce programme C#:

netsh http add sslcert ipport=0.0.0.0:6613 certhash=‎‎‎‎8834efd403687b331363ce9e5657ba4ffba0075b appid={e604f84f-e666-4ccf-af52-fdeca666b5e9}

La partie Confondre

Donc, si vous exécutez mes commandes openssl verbatim à partir de ce fil, que d'importer le .pfx fichier généré par openssl dans IIS et enfin utiliser la commande netssh comme vu ci-dessus avec le hachage de certificat approprié, que tout cela fonctionne parfaitement. Mais quand vous faites ce que je viens de dire automatiquement dans le code C# ci-dessus, je reçois l'erreur.

Une autre chose, le.pfx généré que importé dans le magasin à partir de ce code ne fonctionnera pas du tout lorsque vous essayez manuellement netsh à travers la ligne de commande.

Quelqu'un a-t-il des idées?

+0

Vous n'avez pas besoin d'exécuter netsh dans le code, comme PInvoking HTTP API est beaucoup plus facile. –

Répondre

2

essayer explicitement inclure le PFX au cert avant le stocker si vous utilisez C#

var store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
      store.Open(OpenFlags.ReadWrite); 
    //ensure pfx in cert. 
    byte[] pfx = cert.Export(X509ContentType.Pfx); 
    cert = new X509Certificate2(pfx, (string)null, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); 

    //then store 
    store.Add(cert); 
    store.Close();