зеркало из https://github.com/mono/mono-tls.git
Cleanup the certificate code.
This commit is contained in:
Родитель
7c72be59aa
Коммит
36a32f5f7b
|
@ -8,18 +8,14 @@ using System.Collections.Generic;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using Mono.Security.NewTls.TestFramework;
|
using Mono.Security.NewTls.TestFramework;
|
||||||
using Mono.Security.NewTls.TestProvider;
|
using Mono.Security.NewTls.TestProvider;
|
||||||
|
using Xamarin.AsyncTests;
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
namespace Mono.Security.NewTls.TestProvider
|
||||||
{
|
{
|
||||||
public class OpenSslServer : OpenSslConnection, IServer
|
public class OpenSslServer : OpenSslConnection, IServer
|
||||||
{
|
{
|
||||||
public ServerCertificate Certificate {
|
public IServerCertificate Certificate {
|
||||||
get;
|
get { return Parameters.ServerCertificate; }
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
IServerCertificate IServer.Certificate {
|
|
||||||
get { return Certificate; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new public IServerParameters Parameters {
|
new public IServerParameters Parameters {
|
||||||
|
@ -29,7 +25,6 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
public OpenSslServer (IPEndPoint endpoint, IServerParameters parameters)
|
public OpenSslServer (IPEndPoint endpoint, IServerParameters parameters)
|
||||||
: base (endpoint, parameters)
|
: base (endpoint, parameters)
|
||||||
{
|
{
|
||||||
Certificate = new ServerCertificate (parameters.ServerCertificate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Initialize ()
|
protected override void Initialize ()
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
|
||||||
{
|
|
||||||
public class CertificateAndKeyAsPFX : PrivateFile
|
|
||||||
{
|
|
||||||
public X509Certificate2 Certificate {
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CertificateAndKeyAsPFX (byte[] data, string password)
|
|
||||||
: base (data, password)
|
|
||||||
{
|
|
||||||
Certificate = new X509Certificate2 (data, password);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
|
||||||
{
|
|
||||||
using TestFramework;
|
|
||||||
|
|
||||||
public class CertificateAsPEM : PrivateFile, ICertificateAsPEM
|
|
||||||
{
|
|
||||||
public CertificateAsPEM (byte[] data)
|
|
||||||
: base (data, null)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
|
||||||
{
|
|
||||||
using TestFramework;
|
|
||||||
|
|
||||||
public sealed class ClientCertificate : CertificateAndKeyAsPFX, IClientCertificate
|
|
||||||
{
|
|
||||||
public ClientCertificate (byte[] data, string password)
|
|
||||||
: base (data, password)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -32,8 +32,8 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
|
|
||||||
var clientCerts = new X509Certificate2Collection ();
|
var clientCerts = new X509Certificate2Collection ();
|
||||||
if (Parameters.ClientCertificate != null) {
|
if (Parameters.ClientCertificate != null) {
|
||||||
var clientCert = (ClientCertificate)Parameters.ClientCertificate;
|
var clientCert = new X509Certificate2 (Parameters.ClientCertificate.Data, Parameters.ClientCertificate.Password);
|
||||||
clientCerts.Add (clientCert.Certificate);
|
clientCerts.Add (clientCert);
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetHost = "Hamiller-Tube.local";
|
var targetHost = "Hamiller-Tube.local";
|
||||||
|
|
|
@ -17,13 +17,8 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
{
|
{
|
||||||
public class DotNetServer : DotNetConnection, IServer
|
public class DotNetServer : DotNetConnection, IServer
|
||||||
{
|
{
|
||||||
public ServerCertificate Certificate {
|
public IServerCertificate Certificate {
|
||||||
get;
|
get { return Parameters.ServerCertificate; }
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
IServerCertificate IServer.Certificate {
|
|
||||||
get { return Certificate; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new public IServerParameters Parameters {
|
new public IServerParameters Parameters {
|
||||||
|
@ -33,7 +28,6 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
public DotNetServer (IPEndPoint endpoint, IServerParameters parameters)
|
public DotNetServer (IPEndPoint endpoint, IServerParameters parameters)
|
||||||
: base (endpoint, parameters)
|
: base (endpoint, parameters)
|
||||||
{
|
{
|
||||||
Certificate = new ServerCertificate (parameters.ServerCertificate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task<Stream> Start (TestContext ctx, Socket socket, CancellationToken cancellationToken)
|
protected override async Task<Stream> Start (TestContext ctx, Socket socket, CancellationToken cancellationToken)
|
||||||
|
@ -43,9 +37,11 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
if (Parameters.AskForClientCertificate || Parameters.RequireClientCertificate)
|
if (Parameters.AskForClientCertificate || Parameters.RequireClientCertificate)
|
||||||
throw new NotSupportedException ();
|
throw new NotSupportedException ();
|
||||||
|
|
||||||
|
var serverCert = new X509Certificate2 (Certificate.Data, Certificate.Password);
|
||||||
|
|
||||||
var stream = new NetworkStream (socket);
|
var stream = new NetworkStream (socket);
|
||||||
var server = new SslStream (stream, false);
|
var server = new SslStream (stream, false);
|
||||||
await server.AuthenticateAsServerAsync (Certificate.Certificate, false, SslProtocols.Tls12, false);
|
await server.AuthenticateAsServerAsync (serverCert, false, SslProtocols.Tls12, false);
|
||||||
|
|
||||||
ctx.LogMessage ("Successfully authenticated.");
|
ctx.LogMessage ("Successfully authenticated.");
|
||||||
|
|
||||||
|
|
|
@ -35,17 +35,12 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="CertificateAndKeyAsPFX.cs" />
|
|
||||||
<Compile Include="CertificateAsPEM.cs" />
|
|
||||||
<Compile Include="ClientAndServer.cs" />
|
<Compile Include="ClientAndServer.cs" />
|
||||||
<Compile Include="ClientCertificate.cs" />
|
|
||||||
<Compile Include="Connection.cs" />
|
<Compile Include="Connection.cs" />
|
||||||
<Compile Include="DotNetClient.cs" />
|
<Compile Include="DotNetClient.cs" />
|
||||||
<Compile Include="DotNetConnection.cs" />
|
<Compile Include="DotNetConnection.cs" />
|
||||||
<Compile Include="DotNetServer.cs" />
|
<Compile Include="DotNetServer.cs" />
|
||||||
<Compile Include="MonoCryptoProvider.cs" />
|
<Compile Include="MonoCryptoProvider.cs" />
|
||||||
<Compile Include="PrivateFile.cs" />
|
|
||||||
<Compile Include="ServerCertificate.cs" />
|
|
||||||
<Compile Include="SymmetricAlgorithmProxy.cs" />
|
<Compile Include="SymmetricAlgorithmProxy.cs" />
|
||||||
<Compile Include="MonoClient.cs" />
|
<Compile Include="MonoClient.cs" />
|
||||||
<Compile Include="MonoConnection.cs" />
|
<Compile Include="MonoConnection.cs" />
|
||||||
|
|
|
@ -57,8 +57,8 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
|
|
||||||
var clientCerts = new X509Certificate2Collection ();
|
var clientCerts = new X509Certificate2Collection ();
|
||||||
if (Parameters.ClientCertificate != null) {
|
if (Parameters.ClientCertificate != null) {
|
||||||
var clientCert = new ClientCertificate (Parameters.ClientCertificate.Data, Parameters.ClientCertificate.Password);
|
var clientCert = new X509Certificate2 (Parameters.ClientCertificate.Data, Parameters.ClientCertificate.Password);
|
||||||
clientCerts.Add (clientCert.Certificate);
|
clientCerts.Add (clientCert);
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetHost = "Hamiller-Tube.local";
|
var targetHost = "Hamiller-Tube.local";
|
||||||
|
|
|
@ -20,18 +20,14 @@ using Mono.Security.Providers.NewTls;
|
||||||
|
|
||||||
using SSCX = System.Security.Cryptography.X509Certificates;
|
using SSCX = System.Security.Cryptography.X509Certificates;
|
||||||
using MX = Mono.Security.X509;
|
using MX = Mono.Security.X509;
|
||||||
|
using Xamarin.AsyncTests;
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
namespace Mono.Security.NewTls.TestProvider
|
||||||
{
|
{
|
||||||
public class MonoServer : MonoConnection, IServer
|
public class MonoServer : MonoConnection, IServer
|
||||||
{
|
{
|
||||||
public ServerCertificate Certificate {
|
public IServerCertificate Certificate {
|
||||||
get;
|
get { return Parameters.ServerCertificate; }
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
IServerCertificate IServer.Certificate {
|
|
||||||
get { return Certificate; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new public IServerParameters Parameters {
|
new public IServerParameters Parameters {
|
||||||
|
@ -41,7 +37,6 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
public MonoServer (IPEndPoint endpoint, IServerParameters parameters)
|
public MonoServer (IPEndPoint endpoint, IServerParameters parameters)
|
||||||
: base (endpoint, parameters)
|
: base (endpoint, parameters)
|
||||||
{
|
{
|
||||||
Certificate = new ServerCertificate (parameters.ServerCertificate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override TlsSettings GetSettings ()
|
protected override TlsSettings GetSettings ()
|
||||||
|
@ -65,10 +60,12 @@ namespace Mono.Security.NewTls.TestProvider
|
||||||
|
|
||||||
settings.ClientCertValidationCallback = ClientCertValidationCallback;
|
settings.ClientCertValidationCallback = ClientCertValidationCallback;
|
||||||
|
|
||||||
|
var serverCert = new SSCX.X509Certificate2 (Certificate.Data, Certificate.Password);
|
||||||
|
|
||||||
var stream = new NetworkStream (socket);
|
var stream = new NetworkStream (socket);
|
||||||
return MonoNewTlsStreamFactory.CreateServer (
|
return MonoNewTlsStreamFactory.CreateServer (
|
||||||
stream, false, null, null, EncryptionPolicy.RequireEncryption, settings,
|
stream, false, null, null, EncryptionPolicy.RequireEncryption, settings,
|
||||||
Certificate.Certificate, false, SslProtocols.Tls12, false);
|
serverCert, false, SslProtocols.Tls12, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientCertValidationCallback (ClientCertificateParameters certParams, MX.X509Certificate certificate, MX.X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
bool ClientCertValidationCallback (ClientCertificateParameters certParams, MX.X509Certificate certificate, MX.X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
|
|
|
@ -1,97 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
|
||||||
{
|
|
||||||
public abstract class PrivateFile : IDisposable
|
|
||||||
{
|
|
||||||
string filename;
|
|
||||||
byte[] data;
|
|
||||||
|
|
||||||
public string Password {
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PrivateFile (string filename, string password)
|
|
||||||
{
|
|
||||||
this.filename = filename;
|
|
||||||
Password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PrivateFile (byte[] data, string password)
|
|
||||||
{
|
|
||||||
this.data = data;
|
|
||||||
Password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Data {
|
|
||||||
get {
|
|
||||||
if (data == null)
|
|
||||||
data = ReadFromFile (filename);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string FileName {
|
|
||||||
get {
|
|
||||||
if (filename == null)
|
|
||||||
filename = WriteToTempFile (data);
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static byte[] ReadFromFile (string path)
|
|
||||||
{
|
|
||||||
using (var stream = new FileStream (path, FileMode.Open)) {
|
|
||||||
var buffer = new byte [stream.Length];
|
|
||||||
var ret = stream.Read (buffer, 0, buffer.Length);
|
|
||||||
if (ret != buffer.Length)
|
|
||||||
throw new IOException ();
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static string WriteToTempFile (byte[] bytes)
|
|
||||||
{
|
|
||||||
var path = Path.GetTempFileName ();
|
|
||||||
using (var stream = new FileStream (path, FileMode.Create, FileAccess.Write))
|
|
||||||
stream.Write (bytes, 0, bytes.Length);
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DeleteFile (string filename)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
if (File.Exists (filename))
|
|
||||||
File.Delete (filename);
|
|
||||||
} catch {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose ()
|
|
||||||
{
|
|
||||||
Dispose (true);
|
|
||||||
GC.SuppressFinalize (this);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void Dispose (bool disposing)
|
|
||||||
{
|
|
||||||
if (data != null) {
|
|
||||||
Array.Clear (data, 0, data.Length);
|
|
||||||
data = null;
|
|
||||||
}
|
|
||||||
Password = null;
|
|
||||||
if (filename != null) {
|
|
||||||
DeleteFile (filename);
|
|
||||||
filename = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~PrivateFile ()
|
|
||||||
{
|
|
||||||
Dispose (false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Mono.Security.NewTls.TestProvider
|
|
||||||
{
|
|
||||||
using TestFramework;
|
|
||||||
|
|
||||||
public sealed class ServerCertificate : CertificateAndKeyAsPFX, IServerCertificate
|
|
||||||
{
|
|
||||||
public ServerCertificate (byte[] data, string password)
|
|
||||||
: base (data, password)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServerCertificate (IServerCertificate certificate)
|
|
||||||
: base (certificate.Data, certificate.Password)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче