Skip to content

Commit 96e226c

Browse files
authored
Add UseStoredDeviceCertificate to SecureSocket (#165)
***NO_CI***
1 parent f8d50ce commit 96e226c

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

nanoFramework.System.Net/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.1.3.3")]
15+
[assembly: AssemblyNativeVersion("100.1.3.4")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

nanoFramework.System.Net/Security/NetworkSecurity.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,20 @@ 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(
74+
int sslProtocols,
75+
int sslCertVerify,
76+
X509Certificate certificate,
77+
X509Certificate ca,
78+
bool useDeviceCertificate);
7479

7580
[MethodImplAttribute(MethodImplOptions.InternalCall)]
76-
internal static extern int SecureClientInit(int sslProtocols, int sslCertVerify, X509Certificate certificate, X509Certificate ca);
81+
internal static extern int SecureClientInit(
82+
int sslProtocols,
83+
int sslCertVerify,
84+
X509Certificate certificate,
85+
X509Certificate ca,
86+
bool useDeviceCertificate);
7787

7888
[MethodImplAttribute(MethodImplOptions.InternalCall)]
7989
internal static extern void SecureAccept(int contextHandle, object socket);

nanoFramework.System.Net/Security/SslStream.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace System.Net.Security
1818
public class SslStream : NetworkStream
1919
{
2020
private SslVerification _sslVerification;
21+
private bool _useStoredDeviceCertificate = false;
2122

2223
// Internal flags
2324
private int _sslContext;
@@ -29,6 +30,16 @@ public class SslStream : NetworkStream
2930
/// </summary>
3031
public SslVerification SslVerification { get => _sslVerification; set => _sslVerification = value; }
3132

33+
/// <summary>
34+
/// Option to use the certificate stored in the device as client or server certificate.
35+
/// The default option is <see langword="false"/>.
36+
/// </summary>
37+
/// <remarks>
38+
/// This property is exclusive of .NET nanoFramework.
39+
/// In case there is no device certificate stored, the authentication will use whatever is provided (or not) in the parameter of the method being called.
40+
/// </remarks>
41+
public bool UseStoredDeviceCertificate { get => _useStoredDeviceCertificate; set => _useStoredDeviceCertificate = value; }
42+
3243
//--//
3344

3445
/// <summary>
@@ -71,6 +82,9 @@ public void AuthenticateAsClient(string targetHost, SslProtocols enabledSslProto
7182
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
7283
/// <param name="clientCertificate">The client certificate.</param>
7384
/// <param name="enabledSslProtocols">The <see cref="SslProtocols"/> value that represents the protocol used for authentication.</param>
85+
/// <remarks>
86+
/// Instead of providing the client certificate in the <paramref name="clientCertificate"/> parameter the <see cref="UseStoredDeviceCertificate"/> property can be used to use the certificate stored in the device.
87+
/// </remarks>
7488
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, SslProtocols enabledSslProtocols)
7589
{
7690
Authenticate(false, targetHost, clientCertificate, null, enabledSslProtocols);
@@ -84,6 +98,9 @@ public void AuthenticateAsClient(string targetHost, X509Certificate clientCertif
8498
/// <param name="clientCertificate">The client certificate.</param>
8599
/// <param name="ca">Certificate Authority certificate to use for authentication with the server.</param>
86100
/// <param name="enabledSslProtocols">The <see cref="SslProtocols"/> value that represents the protocol used for authentication.</param>
101+
/// <remarks>
102+
/// Instead of providing the client certificate in the <paramref name="clientCertificate"/> parameter the <see cref="UseStoredDeviceCertificate"/> property can be used to use the certificate stored in the device.
103+
/// </remarks>
87104
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, X509Certificate ca, SslProtocols enabledSslProtocols)
88105
{
89106
Authenticate(false, targetHost, clientCertificate, ca, enabledSslProtocols);
@@ -95,6 +112,9 @@ public void AuthenticateAsClient(string targetHost, X509Certificate clientCertif
95112
/// </summary>
96113
/// <param name="serverCertificate">The certificate used to authenticate the server.</param>
97114
/// <param name="enabledSslProtocols">The protocols that may be used for authentication.</param>
115+
/// <remarks>
116+
/// Instead of providing the server certificate in the <paramref name="serverCertificate"/> parameter the <see cref="UseStoredDeviceCertificate"/> property can be used to use the certificate stored in the device.
117+
/// </remarks>
98118
public void AuthenticateAsServer(X509Certificate serverCertificate, SslProtocols enabledSslProtocols)
99119
{
100120
Authenticate(true, "", serverCertificate, null, enabledSslProtocols);
@@ -106,6 +126,9 @@ public void AuthenticateAsServer(X509Certificate serverCertificate, SslProtocols
106126
/// <param name="serverCertificate">The X509Certificate used to authenticate the server.</param>
107127
/// <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>
108128
/// <param name="enabledSslProtocols">The protocols that may be used for authentication.</param>
129+
/// <remarks>
130+
/// Instead of providing the server certificate in the <paramref name="serverCertificate"/> parameter the <see cref="UseStoredDeviceCertificate"/> property can be used to use the certificate stored in the device.
131+
/// </remarks>
109132
public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols)
110133
{
111134
SslVerification = clientCertificateRequired ? SslVerification.VerifyClientOnce : SslVerification.NoVerification;
@@ -123,12 +146,24 @@ internal void Authenticate(bool isServer, string targetHost, X509Certificate cer
123146
{
124147
if (isServer)
125148
{
126-
_sslContext = SslNative.SecureServerInit((int)enabledSslProtocols, (int)_sslVerification, certificate, ca);
149+
_sslContext = SslNative.SecureServerInit(
150+
(int)enabledSslProtocols,
151+
(int)_sslVerification,
152+
certificate,
153+
ca,
154+
_useStoredDeviceCertificate);
155+
127156
SslNative.SecureAccept(_sslContext, _socket);
128157
}
129158
else
130159
{
131-
_sslContext = SslNative.SecureClientInit((int)enabledSslProtocols, (int)_sslVerification, certificate, ca);
160+
_sslContext = SslNative.SecureClientInit(
161+
(int)enabledSslProtocols,
162+
(int)_sslVerification,
163+
certificate,
164+
ca,
165+
_useStoredDeviceCertificate);
166+
132167
SslNative.SecureConnect(_sslContext, targetHost, _socket);
133168
}
134169
}

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/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.6.4-preview.{height}",
3+
"version": "1.6.5-preview.{height}",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)