私はiPhone用のクライアントサーバーアプリを構築しています。サーバーはc#で記述されており、TcpListenerを使用して着信接続をリッスンし、TcpClientを使用して各接続を処理します。コードは次のようになります。
private void startAcceptingConnections()
{
m_Listener = new TcpListener(i_IPAddress, i_Port);
m_Listener.Start();
while (true)
{
//blocking
TcpClient tcpConnection = m_Listener.AcceptTcpClient();
Thread ClientAuthenticationThread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
ClientAuthenticationThread.Start(tcpConnection);
}
}
private void HandleClientConnection(object i_Client)
{
TcpClient client = i_Client as TcpClient;
if (client != null)
{
NetworkStream clientStream = client.GetStream();
byte[] buffer = new byte[4096];
int bytesRead = 0;
//blocking
bytesRead = clientStream.Read(buffer, 0, 4096);
if (bytesRead == 0)
{
client.Close();
}
else
{
// do something with bytesRead
}
}
}
アプリに SSL を追加したいので、これまでに行ったことは次のとおりです。
- 私のMacBookでは、Keycahinで認証局を作成しました
- 次に、手順1で作成したCAを使用して、サーバーSSL証明書を発行しました(サーバーIDと呼ばれると思います)
- CA を iPhone アプリにバンドルします。
- サーバー ID を .p12 ファイルとしてエクスポートしました
このファイルをサーバーにインストールする必要があることはわかっていますが、方法がわかりません。誰かが私を案内してもらえますか?
@@@@@ MSDN でこのコードを見つけました。SSL をサポートするために追加する必要があるのはそれだけですか?
private void startAcceptingConnections()
{
string certPath = "C:\\...Path...\\ServerCertificates.p12";
serverCertificate = new X509Certificate(certPath, "serverCertPassword");
m_Listener = new TcpListener(i_IPAddress, i_Port);
m_Listener.Start();
while (true)
{
//blocking
TcpClient tcpConnection = m_Listener.AcceptTcpClient();
Thread ClientAuthenticationThread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
ClientAuthenticationThread.Start(tcpConnection);
}
}
private void HandleClientConnection(object i_Client)
{
TcpClient client = i_Client as TcpClient;
try
{
NetworkStream clientStream = client.GetStream();
// A client has connected. Create the
// SslStream using the client's network stream.
SslStream sslStream = new SslStream(clientStream, false);
sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);
byte[] buffer = new byte[4096];
int bytesRead = 0;
//blocking
bytesRead = clientStream.Read(buffer, 0, 4096);
if (bytesRead == 0)
{
sslStream.Close();
client.Close();
}
else
{
// do something with bytesRead
}
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine ("Authentication failed - closing the connection.");
sslStream.Close();
client.Close();
return;
}
}