2011-07-30 5 views
0

저는 C# 프로젝트에서 SSL을 사용하여 서버와 클라이언트 간의 tcp 전송을 수행하고 있습니다. makecert 프로그램으로 인증서 파일을 만들었지 만, 생성 된 컴퓨터에서만 작동합니다 (.cer 파일을 설치 했음에도 불구하고). 그 문제는 내가 명령에 넣어 매개 변수에 있다고 거의 확신하지만, 나는 (다음 despit) 많은 조합과 없음을 확인에만 암호화 transmition에 사용되는 .CER 파일 SSL 인증서 생성

makecert -r -pe -n "CN=This is my certificate" -ss my -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 ca.cer 

을했다. 나는 PKI를 사용하지 않는다. 또한 SSL을 사용하는 것은 "불필요한 요구 사항"입니다. 사용하기 위해 사용해야합니다. 모든 보안 문제를 고려해서는 안됩니다.

누구든지 나를 대답해야한다면, 인증서를 만드는 방법은 X509Certificate.CreateFromCertFile 방법으로 사용할 수 있습니다. 기쁘게 생각합니다.

+1

당신이 SSH 또는 SSL 필요하십니까에서 테스트되었습니다? 이것들은 두 가지 다른 프로토콜입니다. –

+0

현재 System.Net.Security.SslStream 클래스를 사용하고 있으며 AuthenticateAsServer (X509Certificate) 메서드가 있습니다 –

+0

'.cer 파일을 설치했습니다.' CA 저장소에 설치해야합니다. 그렇지 않으면 다른 컴퓨터에서 신뢰하지 않습니다. –

답변

2

이러한 인증서를 사용할 모든 컴퓨터를 제어하는 ​​경우 모든 컴퓨터에서 신뢰할 수있는 CA를 만든 다음이를 기반으로 인증서를 발급 할 수 있습니다.

내 배치 파일은 다음과 같습니다.

:// Create a self-signed certificate (-r), 
:// with an exportable private key (-pe), 
:// using SHA1 (-r), for signing (-sky signature). 
:// The private key is written to a file (-sv). 
makecert -r -pe -n "CN=My Root Authority" -ss CA^
    -sr CurrentUser -a sha1 -sky signature -cy authority^
    -sv CA.pvk CA.cer 

가져 오기 서버에 연결해야 그 기계에 CA 인증서 저장소에 .cer 파일이 (가 CA를 신뢰해야합니다) :

:// Import that certificate into the 
:// "Trusted Root Certification Authorities" store. 
certutil -user -addstore Root CA.cer 

이 첫 번째는 CA 인증서를 생성

:// Create a server certificate, with an exportable private key (-pe), 
:// using SHA1 (-r) for key exchange (-sky exchange). 
:// It can be used as an SSL server certificate (-eku 1.3.6.1.5.5.7.3.1). 
:// The issuing certificate is in a file (-ic), as is the key (-iv). 
:// Use a particular crypto provider (-sp, -sy). 
makecert -pe -n "CN=server.example.com" -a sha1^
    -sky exchange -eku 1.3.6.1.5.5.7.3.1 
    -ic CA.cer -iv CA.pvk^
    -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12^
    -sv server.pvk server.cer 
pvk2pfx -pvk server.pvk -spc server.cer -pfx server.pfx 

.pfx 파일을 설치 한 다음이를 사용하려면 C# 서버 코드를 가져옵니다. 이것은 독자를위한 운동으로 남아 있습니다.

0

유효한 인증서를 받아야합니다. makecert 명령으로 생성 된 명령은 테스트 용이며 다른 시스템에서는 작동하지 않습니다.

+0

어떻게 달성해야합니까? 프로그램은 학술적 용도로만 사용되며, 그래서 저는 CA와 협력하고 싶지 않습니다. 두 대의 다른 컴퓨터에서 사용할 수있는 유효한 인증서를 어떻게 생성 할 수 있습니까? –

+0

올바르게 설치하면됩니다. 테스트 또는 내부 용으로 만 사용해야합니다. –

+0

"올바르게"말하는 것이 무엇을 의미합니까? –

3

감사 로저 난 당신의 블로그를 발견하고는 사용하기 쉬운, 그래서이 작업을 진행하고 주위에 래퍼를 넣어 관리하고 또한 인증서에 친화적 인-이름을 설정 관리

using System; 
using System.Collections.Generic; 
using System.Security.Cryptography.X509Certificates; 
using System.Diagnostics; 

public class SSLCertificateCreator 
{ 
    public static string RunDosCommand(string Cmd, string Arguments) 
    {//Executes a Dos command in the current directory and then returns the result 
     string TestMessageText = ""; 
     string filePath = Environment.CurrentDirectory; 
     ProcessStartInfo pi = new ProcessStartInfo() 
     { 
      FileName = filePath + "\\" + Cmd, 
      Arguments = Arguments + " ", 
      RedirectStandardOutput = true, 
      UseShellExecute = false, 
      CreateNoWindow = false 
     }; 
     try 
     { 
      using (Process p = Process.Start(pi)) 
      { 
       p.WaitForExit(); 
       TestMessageText = p.StandardOutput.ReadToEnd(); 
       return TestMessageText; 
      } 
     } 
     catch (Exception Ex) 
     { 
      return "ERROR :" +Ex.Message; 
     } 
    } 

    public static bool MakeCACertificate(string RootCertificateName, string FriendlyName) 
    {//Make a CA certificate but only if we don't already have one and then sets the friendly name 
     if (FindCertificate("Root", RootCertificateName, OpenFlags.ReadOnly) != null) return false; //We already have this root certificate 
     string Arguments="-pe -n \"CN=" + RootCertificateName + "\" -ss Root -sr CurrentUser -a sha1 -sky signature -r \"" + RootCertificateName + ".cer\" -m 12"; 
     string Result=RunDosCommand("makecert", Arguments); 
     X509Certificate2 Cert = FindCertificate("Root", RootCertificateName, OpenFlags.ReadWrite); 
     if (Cert == null || !Result.ToLower().StartsWith("succeeded")) return false; 
     Cert.FriendlyName = FriendlyName; 
     return true; 
    } 


    public static bool MakeSignedCertificate(string RootCertificateName, string CertificateName, string FriendlyName) 
    {//Makes a signed certificate but only if we have the root certificate and then sets the friendly name 
     if (FindCertificate("Root", RootCertificateName, OpenFlags.ReadOnly) == null) return false; //We must have a valid root-certificate first 
     if (FindCertificate("my",CertificateName, OpenFlags.ReadOnly)!=null) return false;//Nope we alrady have this signed certificate 
     string Arguments = "-pe -n \"CN=" + CertificateName + "\" -ss my -sr CurrentUser -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1 -in \"" + RootCertificateName + "\" -is Root -ir CurrentUser -sp \"Microsoft RSA SChannel Cryptographic Provider\" -sy 12 \"" + CertificateName + ".cer\" -m 12"; 
     string Result = RunDosCommand("makecert", Arguments); 
     X509Certificate2 Cert = FindCertificate("my", CertificateName, OpenFlags.ReadWrite); 
     if (Cert==null || !Result.ToLower().StartsWith("succeeded")) return false; 
     Cert.FriendlyName = FriendlyName; 
     return true; 
    } 

    private static X509Certificate2 FindCertificate(string Store, string Name, OpenFlags Mode) 
    {//Look to see if we can find the certificate store 
     X509Store store = new X509Store(Store,StoreLocation.CurrentUser); 
     store.Open(Mode); 
     foreach (X509Certificate2 Cert in store.Certificates) 
     { 
      if (Cert.Subject.ToLower() =="cn="+ Name.ToLower()) 
       return Cert;//Yep found it 
     } 
     return null; 
    } 
} 

샘플 useage

SSLCertificateCreator.MakeCACertificate ("DavesRoot", "Nice Name"); SSLCertificateCreator.MakeSignedCertificate ("DavesRoot", "Daves Signed Certificate5", "Nice Name");

Makecert.exe를 코드가 작동하려면 빈/릴리스 또는 디버그 디렉토리에 있어야하고이 코드는 오직 윈도우 8

+0

C#에서 인증서를 생성하는 경우 Bouncy Castle을 살펴보아야합니다. 주제에 대한 일련의 블로그 게시물이 있습니다. http://blog.differentpla.net/tag/bouncy-castle –