2017-10-08 13 views
0

自己署名証明書(Httplistener with https supportのアドバイス)を使用してテストC#Https Listenerをセットアップしました。HttpsをサポートするLocalhost HttpListener - 一定期間後に動作しなくなる

(ローカルシステムでWindowsサービスとして実行される)サービスとは、最初に(起動後)がうまく動作します。一定の期間(〜1.5時間)後 はエッジ/ IEの訴えでの作業を停止HTTPSエンドポイントへの呼び出し:

は、このページにしっかり を接続できないサイトが古くなったり、安全でない使用していますので、これがあるかもしれませんTLSセキュリティ設定。これが起こっている場合は、ウェブサイトの所有者に連絡してみてください。このサイトは 接続がリセットされたに到達することはできません

次のように

Chromeが文句を言います。 ERR_CONNECTION_RESET

この場合、証明書ストアを確認すると、証明書(ルートと私の店舗)がまだ存在することが示されます。

をチェック

のsslcertのnetshのhttpショーはまた、ポートへの証明書の登録が場所にまだあることを示しています。

アプリケーションを再起動すると(C#httpリスナーがリッスンするポートへの証明書の再作成、再インストール、再バインド)役立ちますが、しばらくの間(〜1.5 ..2時間?)。

私は、リスナースレッドがまだ安全でないポートの要求が依然として機能していることを知っています。

何かがその間に起こると私は理解できないものを...

コード:

すべてが自己署名証明書の生成から始まる:

 // create DN for subject and issuer 
     var dn = new CX500DistinguishedName(); 
     dn.Encode("CN=localhost"); 

     // create a new private key for the certificate 
     var privateKey = new CX509PrivateKey 
     { 
      ProviderName = "Microsoft Base Cryptographic Provider v1.0", 
      MachineContext = false, 
      Length = 2048, 
      KeySpec = X509KeySpec.XCN_AT_SIGNATURE, 
      KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_SIGNING_FLAG, 
      FriendlyName = "Application Testing Key", 
      ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_FLAG 
     }; 
     privateKey.Create(); 

     var hashobj = new CObjectId(); 
     hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID, 
      ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY, 
      AlgorithmFlags.AlgorithmFlagsNone, "SHA256"); 

     // Create the self signing request 
     var certificateRequest = new CX509CertificateRequestCertificate(); 
     certificateRequest.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, String.Empty); 
     certificateRequest.Subject = dn; 
     certificateRequest.Issuer = dn; // the issuer and the subject are the same 
     certificateRequest.NotBefore = DateTime.UtcNow.AddDays(-1); 
     certificateRequest.NotAfter = DateTime.UtcNow.AddYears(10); 
     certificateRequest.HashAlgorithm = hashobj; 

     // Set up the Subject Alternative Names extension. 
     var nameslist = new CAlternativeNames(); 
     var alternativeName = new CAlternativeName(); 
     alternativeName.InitializeFromString(AlternativeNameType.XCN_CERT_ALT_NAME_DNS_NAME, "localhost"); 
     nameslist.Add(alternativeName); 
     var subjectAlternativeNamesExtension = new CX509ExtensionAlternativeNames(); 
     subjectAlternativeNamesExtension.InitializeEncode(nameslist); 
     certificateRequest.X509Extensions.Add((CX509Extension)subjectAlternativeNamesExtension); 

     var skiExtension = new CX509ExtensionSubjectKeyIdentifier(); 
     skiExtension.InitializeEncode(EncodingType.XCN_CRYPT_STRING_BASE64, Convert.ToBase64String(StringToByteArray(certSKI))); 
     certificateRequest.X509Extensions.Add((CX509Extension)skiExtension); 

     certificateRequest.Encode(); 

     // Do the final enrollment process 
     var enroll = new CX509Enrollment(); 
     enroll.InitializeFromRequest(certificateRequest); // load the certificate 
     enroll.CertificateFriendlyName = "Application Testing Cert"; 
     var csr = enroll.CreateRequest(); // Output the request in base64 
     var pwd = Guid.NewGuid().ToString(); 
     enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate, csr, EncodingType.XCN_CRYPT_STRING_BASE64, pwd); // and install it back as the response 
     var base64encoded = enroll.CreatePFX(pwd, PFXExportOptions.PFXExportChainWithRoot); 

     // instantiate the target class with the PKCS#12 data 
     return new X509Certificate2(Convert.FromBase64String(base64encoded), pwd); 

ローカルホストのルートとプライベートストアに新しく生成された証明書を追加します。

InstallCertificateToCertStore(cert, new X509Store(StoreName.Root, StoreLocation.LocalMachine)); 
InstallCertificateToCertStore(cert, new X509Store(StoreName.My, StoreLocation.LocalMachine)); 

HTTPリスナーがリッスンするポートを新しく作成してインストール証明書を登録することにより、続い:

netsh.exe http add sslcert ipport=0.0.0.0:{port} certhash={certThumbprint} appid={appid_guid} 
+0

さらにいくつかの大規模な検索後に理由があることもできるように思えます私は秘密鍵を保持しません:https://support.microsoft.com/en-us/help/950090/installing-a-pfx-file-using-x509certificate-from-a-standard--net-appli Testing今。 –

答えて

0

次のように私が経験した問題だった:

あなたが自己署名証明書を作成するとき。 NETでX509Certificate(2)のインスタンスを構築します。

あなたは、あなたの証明書に対応する秘密鍵は最終的にあなたの証明書へのすべての参照の後にガベージコレクションによって削除されます、

X509KeyStorageFlags.PersistKeySet

フラグを指定しない場合範囲の私達の行く(従って問題はすぐにではなく時間の後に再現された)。

証明書の秘密鍵を保持するには、上記のようにフラグを指定する必要があります。

これは、ここで、マイクロソフトで言及されている次のように https://support.microsoft.com/en-us/help/950090/installing-a-pfx-file-using-x509certificate-from-a-standard--net-appli

自己署名証明書の作成のための最終的な作業のコードは次のとおりです。

 // create DN for subject and issuer 
     var dn = new CX500DistinguishedName(); 
     dn.Encode("CN=localhost"); 

     // create a new private key for the certificate 
     var privateKey = new CX509PrivateKey 
     { 
      ProviderName = "Microsoft Base Cryptographic Provider v1.0", 
      MachineContext = true, 
      Length = 2048, 
      KeySpec = X509KeySpec.XCN_AT_SIGNATURE, 
      KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_ALL_USAGES, 
      FriendlyName = "App Testing Key", 
      ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_FLAG 
     }; 
     privateKey.Create(); 

     var hashobj = new CObjectId(); 
     hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID, 
      ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY, 
      AlgorithmFlags.AlgorithmFlagsNone, "SHA256"); 

     // Create the self signing request 
     // also see: https://security.stackexchange.com/a/103362 
     var certificateRequest = new CX509CertificateRequestCertificate(); 
     certificateRequest.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextMachine, privateKey, String.Empty); 
     certificateRequest.Subject = dn; 
     certificateRequest.Issuer = dn; // the issuer and the subject are the same 
     certificateRequest.NotBefore = DateTime.UtcNow.AddDays(-1); 
     certificateRequest.NotAfter = DateTime.UtcNow.AddYears(10); 
     certificateRequest.HashAlgorithm = hashobj; 

     // Set up the Subject Alternative Names extension. 
     var nameslist = new CAlternativeNames(); 
     var alternativeName = new CAlternativeName(); 
     alternativeName.InitializeFromString(AlternativeNameType.XCN_CERT_ALT_NAME_DNS_NAME, "localhost"); 
     nameslist.Add(alternativeName); 
     var subjectAlternativeNamesExtension = new CX509ExtensionAlternativeNames(); 
     subjectAlternativeNamesExtension.InitializeEncode(nameslist); 
     certificateRequest.X509Extensions.Add((CX509Extension)subjectAlternativeNamesExtension); 

     var skiExtension = new CX509ExtensionSubjectKeyIdentifier(); 
     skiExtension.InitializeEncode(EncodingType.XCN_CRYPT_STRING_BASE64, Convert.ToBase64String(StringToByteArray(certSKI))); 
     certificateRequest.X509Extensions.Add((CX509Extension)skiExtension); 

     certificateRequest.Encode(); 

     // Do the final enrollment process 
     var enroll = new CX509Enrollment(); 
     enroll.InitializeFromRequest(certificateRequest); // load the certificate 
     enroll.CertificateFriendlyName = "App Testing Cert"; 
     var csr = enroll.CreateRequest(); // Output the request in base64 
     var pwd = Guid.NewGuid().ToString(); 
     enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate, csr, EncodingType.XCN_CRYPT_STRING_BASE64, pwd); // and install it back as the response 
     var base64encoded = enroll.CreatePFX(pwd, PFXExportOptions.PFXExportChainWithRoot); 

     // instantiate the target class with the PKCS#12 data 
     return new X509Certificate2(Convert.FromBase64String(base64encoded), pwd, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); 
関連する問題