Skip to content

Commit ebbcf72

Browse files
authored
Update and fix Authenticate methods (#108)
***PUBLISH_RELEASE*** ***UPDATE_DEPENDENTS***
1 parent e790217 commit ebbcf72

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public SslStream(Socket socket)
5858
/// The authentication process uses the specified SSL protocols.
5959
/// </summary>
6060
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
61-
/// <param name="sslProtocols">The protocols that may be supported.</param>
62-
public void AuthenticateAsClient(string targetHost, params SslProtocols[] sslProtocols)
61+
/// <param name="enabledSslProtocols">The <see cref="SslProtocols"/> value that represents the protocol used for authentication.</param>
62+
public void AuthenticateAsClient(string targetHost, SslProtocols enabledSslProtocols)
6363
{
64-
Authenticate(false, targetHost, null, null, sslProtocols);
64+
Authenticate(false, targetHost, null, null, enabledSslProtocols);
6565
}
6666

6767
/// <summary>
@@ -70,10 +70,10 @@ public void AuthenticateAsClient(string targetHost, params SslProtocols[] sslPro
7070
/// </summary>
7171
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
7272
/// <param name="clientCertificate">The client certificate.</param>
73-
/// <param name="sslProtocols">The protocols that may be supported.</param>
74-
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, params SslProtocols[] sslProtocols)
73+
/// <param name="enabledSslProtocols">The <see cref="SslProtocols"/> value that represents the protocol used for authentication.</param>
74+
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, SslProtocols enabledSslProtocols)
7575
{
76-
Authenticate(false, targetHost, clientCertificate, null, sslProtocols);
76+
Authenticate(false, targetHost, clientCertificate, null, enabledSslProtocols);
7777
}
7878

7979
/// <summary>
@@ -83,59 +83,52 @@ public void AuthenticateAsClient(string targetHost, X509Certificate clientCertif
8383
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
8484
/// <param name="clientCertificate">The client certificate.</param>
8585
/// <param name="ca">Certificate Authority certificate to use for authentication with the server.</param>
86-
/// <param name="sslProtocols">The protocols that may be supported.</param>
87-
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, X509Certificate ca, params SslProtocols[] sslProtocols)
86+
/// <param name="enabledSslProtocols">The <see cref="SslProtocols"/> value that represents the protocol used for authentication.</param>
87+
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, X509Certificate ca, SslProtocols enabledSslProtocols)
8888
{
89-
Authenticate(false, targetHost, clientCertificate, ca, sslProtocols);
89+
Authenticate(false, targetHost, clientCertificate, ca, enabledSslProtocols);
9090
}
9191

9292
/// <summary>
9393
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificate,
9494
/// verification requirements and security protocol.
9595
/// </summary>
9696
/// <param name="serverCertificate">The certificate used to authenticate the server.</param>
97-
/// <param name="sslProtocols">The protocols that may be used for authentication.</param>
98-
public void AuthenticateAsServer(X509Certificate serverCertificate, params SslProtocols[] sslProtocols)
97+
/// <param name="enabledSslProtocols">The protocols that may be used for authentication.</param>
98+
public void AuthenticateAsServer(X509Certificate serverCertificate, SslProtocols enabledSslProtocols)
9999
{
100-
Authenticate(true, "", null, serverCertificate, sslProtocols);
100+
Authenticate(true, "", serverCertificate, null, enabledSslProtocols);
101101
}
102102

103103
/// <summary>
104104
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificates, requirements and security protocol.
105105
/// </summary>
106106
/// <param name="serverCertificate">The X509Certificate used to authenticate the server.</param>
107107
/// <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>
108-
/// <param name="sslProtocols">The protocols that may be used for authentication.</param>
109-
public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientCertificateRequired, params SslProtocols[] sslProtocols)
108+
/// <param name="enabledSslProtocols">The protocols that may be used for authentication.</param>
109+
public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols)
110110
{
111-
SslVerification = SslVerification.VerifyClientOnce;
111+
SslVerification = clientCertificateRequired ? SslVerification.VerifyClientOnce : SslVerification.NoVerification;
112112

113-
Authenticate(true, "", null, serverCertificate, sslProtocols);
113+
Authenticate(true, "", serverCertificate, null, enabledSslProtocols);
114114
}
115115

116-
internal void Authenticate(bool isServer, string targetHost, X509Certificate certificate, X509Certificate ca, params SslProtocols[] sslProtocols)
116+
internal void Authenticate(bool isServer, string targetHost, X509Certificate certificate, X509Certificate ca, SslProtocols enabledSslProtocols)
117117
{
118-
SslProtocols vers = (SslProtocols)0;
119-
120118
if (-1 != _sslContext) throw new InvalidOperationException();
121119

122-
for (int i = sslProtocols.Length - 1; i >= 0; i--)
123-
{
124-
vers |= sslProtocols[i];
125-
}
126-
127120
_isServer = isServer;
128121

129122
try
130123
{
131124
if (isServer)
132125
{
133-
_sslContext = SslNative.SecureServerInit((int)vers, (int)_sslVerification, certificate, ca);
126+
_sslContext = SslNative.SecureServerInit((int)enabledSslProtocols, (int)_sslVerification, certificate, ca);
134127
SslNative.SecureAccept(_sslContext, _socket);
135128
}
136129
else
137130
{
138-
_sslContext = SslNative.SecureClientInit((int)vers, (int)_sslVerification, certificate, ca);
131+
_sslContext = SslNative.SecureClientInit((int)enabledSslProtocols, (int)_sslVerification, certificate, ca);
139132
SslNative.SecureConnect(_sslContext, targetHost, _socket);
140133
}
141134
}

source/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.4.0-preview.{height}",
3+
"version": "1.4.1-preview.{height}",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)