Skip to content

Commit e661d7a

Browse files
authored
Rework SslStream API (#53)
1 parent 69590df commit e661d7a

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

source/nanoFramework.System.Net/Security/NetworkSecurity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public enum SslVerification
7070
internal static class SslNative
7171
{
7272
[MethodImplAttribute(MethodImplOptions.InternalCall)]
73-
internal static extern int SecureServerInit(int sslProtocols, int sslCertVerify, X509Certificate certificate, X509Certificate[] ca);
73+
internal static extern int SecureServerInit(int sslProtocols, int sslCertVerify, X509Certificate certificate, X509Certificate ca);
7474

7575
[MethodImplAttribute(MethodImplOptions.InternalCall)]
76-
internal static extern int SecureClientInit(int sslProtocols, int sslCertVerify, X509Certificate certificate, X509Certificate[] ca);
76+
internal static extern int SecureClientInit(int sslProtocols, int sslCertVerify, X509Certificate certificate, X509Certificate ca);
7777

7878
[MethodImplAttribute(MethodImplOptions.InternalCall)]
7979
internal static extern void UpdateCertificates(int contextHandle, X509Certificate certificate, X509Certificate[] ca);

source/nanoFramework.System.Net/Security/SslStream.cs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ namespace System.Net.Security
1717
/// </summary>
1818
public class SslStream : NetworkStream
1919
{
20+
private SslVerification _sslVerification;
21+
2022
// Internal flags
2123
private int _sslContext;
2224
private bool _isServer;
2325

26+
/// <summary>
27+
/// Option for SSL verification.
28+
/// The default behaviour is <see cref="SslVerification.CertificateRequired"/>.
29+
/// </summary>
30+
public SslVerification SslVerification { get => _sslVerification; set => _sslVerification = value; }
31+
2432
//--//
2533

2634
/// <summary>
@@ -41,6 +49,8 @@ public SslStream(Socket socket)
4149

4250
_sslContext = -1;
4351
_isServer = false;
52+
53+
_sslVerification = SslVerification.CertificateRequired;
4454
}
4555

4656
/// <summary>
@@ -51,59 +61,56 @@ public SslStream(Socket socket)
5161
/// <param name="sslProtocols">The protocols that may be supported.</param>
5262
public void AuthenticateAsClient(string targetHost, params SslProtocols[] sslProtocols)
5363
{
54-
AuthenticateAsClient(targetHost, null, null, SslVerification.NoVerification, sslProtocols);
64+
Authenticate(false, targetHost, null, null, sslProtocols);
5565
}
5666

5767
/// <summary>
5868
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
5969
/// The authentication process uses the specified certificate collections and SSL protocols.
6070
/// </summary>
6171
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
62-
/// <param name="cert">The client certificate.</param>
63-
/// <param name="verify">The type of verification required for authentication.</param>
72+
/// <param name="clientCertificate">The client certificate.</param>
6473
/// <param name="sslProtocols">The protocols that may be supported.</param>
65-
public void AuthenticateAsClient(string targetHost, X509Certificate cert, SslVerification verify, params SslProtocols[] sslProtocols)
74+
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, params SslProtocols[] sslProtocols)
6675
{
67-
AuthenticateAsClient(targetHost, cert, null, verify, sslProtocols);
76+
Authenticate(false, targetHost, clientCertificate, null, sslProtocols);
6877
}
6978

7079
/// <summary>
7180
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
7281
/// The authentication process uses the specified certificate collections and SSL protocols.
7382
/// </summary>
7483
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
75-
/// <param name="cert">The client certificate.</param>
76-
/// <param name="ca">The collection of certificates for client authorities to use for authentication.</param>
77-
/// <param name="verify">The type of verification required for authentication.</param>
84+
/// <param name="clientCertificate">The client certificate.</param>
85+
/// <param name="ca">Certificate Authority certificate to use for authentication with the server.</param>
7886
/// <param name="sslProtocols">The protocols that may be supported.</param>
79-
public void AuthenticateAsClient(string targetHost, X509Certificate cert, X509Certificate[] ca, SslVerification verify, params SslProtocols[] sslProtocols)
87+
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, X509Certificate ca, params SslProtocols[] sslProtocols)
8088
{
81-
Authenticate(false, targetHost, cert, ca, verify, sslProtocols);
89+
Authenticate(false, targetHost, clientCertificate, ca, sslProtocols);
8290
}
8391

8492
/// <summary>
85-
/// Called by servers to authenticate the server and optionally the client in a client-server connection.
86-
/// This member is overloaded.For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
93+
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificate,
94+
/// verification requirements and security protocol.
8795
/// </summary>
88-
/// <param name="cert">The certificate used to authenticate the server.</param>
89-
/// <param name="verify">An enumeration value that specifies the degree of verification required, such as whether the client must supply a certificate for authentication.</param>
96+
/// <param name="serverCertificate">The certificate used to authenticate the server.</param>
9097
/// <param name="sslProtocols">The protocols that may be used for authentication.</param>
91-
public void AuthenticateAsServer(X509Certificate cert, SslVerification verify, params SslProtocols[] sslProtocols)
98+
public void AuthenticateAsServer(X509Certificate serverCertificate, params SslProtocols[] sslProtocols)
9299
{
93-
AuthenticateAsServer(cert, null, verify, sslProtocols);
100+
Authenticate(true, "", null, serverCertificate, sslProtocols);
94101
}
95102

96103
/// <summary>
97-
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificate,
98-
/// verification requirements and security protocol.
104+
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificates, requirements and security protocol.
99105
/// </summary>
100-
/// <param name="cert">The certificate used to authenticate the server.</param>
101-
/// <param name="ca">The certifcates for certificate authorities to use for authentication.</param>
102-
/// <param name="verify">An enumeration value that specifies the degree of verification required, such as whether the client must supply a certificate for authentication.</param>
106+
/// <param name="serverCertificate">The X509Certificate used to authenticate the server.</param>
107+
/// <param name="clientCertificateRequired">A <see cref="Boolean"/> value that specifies whether the client is asked for a certificate for authentication. Note that this is only a request, if no certificate is provided, the server still accepts the connection request.</param>
103108
/// <param name="sslProtocols">The protocols that may be used for authentication.</param>
104-
public void AuthenticateAsServer(X509Certificate cert, X509Certificate[] ca, SslVerification verify, params SslProtocols[] sslProtocols)
109+
public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientCertificateRequired, params SslProtocols[] sslProtocols)
105110
{
106-
Authenticate(true, "", cert, ca, verify, sslProtocols);
111+
SslVerification = SslVerification.VerifyClientOnce;
112+
113+
Authenticate(true, "", null, serverCertificate, sslProtocols);
107114
}
108115

109116
/// <summary>
@@ -118,7 +125,7 @@ public void UpdateCertificates(X509Certificate cert, X509Certificate[] ca)
118125
SslNative.UpdateCertificates(_sslContext, cert, ca);
119126
}
120127

121-
internal void Authenticate(bool isServer, string targetHost, X509Certificate certificate, X509Certificate[] ca, SslVerification verify, params SslProtocols[] sslProtocols)
128+
internal void Authenticate(bool isServer, string targetHost, X509Certificate certificate, X509Certificate ca, params SslProtocols[] sslProtocols)
122129
{
123130
SslProtocols vers = (SslProtocols)0;
124131

@@ -135,12 +142,12 @@ internal void Authenticate(bool isServer, string targetHost, X509Certificate cer
135142
{
136143
if (isServer)
137144
{
138-
_sslContext = SslNative.SecureServerInit((int)vers, (int)verify, certificate, ca);
145+
_sslContext = SslNative.SecureServerInit((int)vers, (int)_sslVerification, certificate, ca);
139146
SslNative.SecureAccept(_sslContext, _socket);
140147
}
141148
else
142149
{
143-
_sslContext = SslNative.SecureClientInit((int)vers, (int)verify, certificate, ca);
150+
_sslContext = SslNative.SecureClientInit((int)vers, (int)_sslVerification, certificate, ca);
144151
SslNative.SecureConnect(_sslContext, targetHost, _socket);
145152
}
146153
}

0 commit comments

Comments
 (0)