Skip to content

Commit e19deb2

Browse files
committed
Improve return result from SSL/TLS authenticate APIs (#407)
***NO_CI*** (cherry picked from commit 923b423)
1 parent 658c9dd commit e19deb2

4 files changed

Lines changed: 199 additions & 6 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace System.Security.Cryptography
7+
{
8+
using System;
9+
10+
/// <summary>
11+
/// The exception that is thrown when an error occurs during a
12+
/// cryptographic operation.
13+
/// </summary>
14+
[Serializable]
15+
public class CryptographicException : Exception
16+
{
17+
private int _errorCode;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="CryptographicException"/> class.
21+
/// </summary>
22+
public CryptographicException()
23+
{
24+
}
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="CryptographicException"/>
28+
/// class with a specified error code.
29+
/// </summary>
30+
/// <param name="errorCode">The error code.</param>
31+
public CryptographicException(int errorCode)
32+
{
33+
_errorCode = errorCode;
34+
}
35+
36+
/// <summary>
37+
/// Gets the error code that is associated with this exception.
38+
/// </summary>
39+
public int ErrorCode
40+
{
41+
get { return _errorCode; }
42+
}
43+
}
44+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace System.Net.Sockets
7+
{
8+
9+
10+
/// <summary>
11+
/// Defines error codes returned by native SSL initialisation.
12+
/// </summary>
13+
/// <remarks>
14+
/// Values are kept in sync with the <c>SSL_Error</c> enum in the native interpreter
15+
/// (<c>ssl_functions.h</c>). <see cref="System.Net.Security.SslStream"/> surfaces these
16+
/// through <see cref="System.Security.Cryptography.CryptographicException.ErrorCode"/>
17+
/// when SSL context setup fails.
18+
/// </remarks>
19+
///
20+
public enum SslError : byte
21+
{
22+
/// <summary>No error; SSL context initialisation succeeded.</summary>
23+
None = 0,
24+
25+
/// <summary>
26+
/// All SSL context slots are in use.
27+
/// Close an existing SSL connection before opening a new one.
28+
/// </summary>
29+
NoFreeContext,
30+
31+
/// <summary>
32+
/// A memory allocation failed while setting up the SSL context.
33+
/// The device may be low on heap.
34+
/// </summary>
35+
OutOfMemory,
36+
37+
/// <summary>
38+
/// The DRBG (Deterministic Random Bit Generator) seed step failed.
39+
/// The entropy source could not be initialised.
40+
/// </summary>
41+
DrbgSeedFailed,
42+
43+
/// <summary>
44+
/// Setting TLS configuration defaults failed.
45+
/// This is an internal mbedTLS error that should not occur under normal conditions.
46+
/// </summary>
47+
ConfigDefaultsFailed,
48+
49+
/// <summary>
50+
/// The requested TLS protocol version is not supported on this device.
51+
/// Use a protocol version that the target hardware supports.
52+
/// </summary>
53+
UnsupportedProtocolVersion,
54+
55+
/// <summary>
56+
/// The supplied private key could not be parsed.
57+
/// Verify the key is in a supported format (PEM or DER) and is not corrupted.
58+
/// </summary>
59+
PrivateKeyParseFailed,
60+
61+
/// <summary>
62+
/// The supplied certificate could not be parsed.
63+
/// Verify the certificate is in a supported format (PEM or DER) and is not corrupted.
64+
/// </summary>
65+
CertificateParseFailed,
66+
67+
/// <summary>
68+
/// Configuring the own certificate and private key pair on the SSL context failed.
69+
/// This is an internal mbedTLS error that should not occur under normal conditions.
70+
/// </summary>
71+
OwnCertConfigFailed,
72+
73+
/// <summary>
74+
/// Final SSL context setup failed.
75+
/// This is an internal mbedTLS error that should not occur under normal conditions.
76+
/// </summary>
77+
SetupFailed,
78+
}
79+
}

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

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,29 @@ public SslStream(Socket socket)
6565
}
6666

6767
/// <summary>
68-
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
68+
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
6969
/// The authentication process uses the specified SSL protocols.
7070
/// </summary>
7171
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
7272
/// <param name="enabledSslProtocols">The <see cref="SslProtocols"/> value that represents the protocol used for authentication.</param>
73+
/// <exception cref="InvalidOperationException">Authentication has already been performed on this stream, or all native SSL context slots are in.</exception>
74+
/// <exception cref="OutOfMemoryException">A memory allocation failed while setting up the SSL context. The device may be low on heap.</exception>
75+
/// <exception cref="System.Security.Cryptography.CryptographicException">
76+
/// SSL context initialisation failed. The <see cref="System.Security.Cryptography.CryptographicException.ErrorCode"/> property contains the
77+
/// corresponding <see cref="SslError"/> value:
78+
/// <see cref="SslError.DrbgSeedFailed"/> — entropy source could not be initialised;
79+
/// <see cref="SslError.ConfigDefaultsFailed"/> — internal mbedTLS configuration error;
80+
/// <see cref="SslError.UnsupportedProtocolVersion"/> — the requested TLS version is not supported on this device;
81+
/// <see cref="SslError.SetupFailed"/> — final SSL context setup failed.
82+
/// </exception>
83+
/// <exception cref="SocketException">The TLS handshake with the remote server failed.</exception>
7384
public void AuthenticateAsClient(string targetHost, SslProtocols enabledSslProtocols)
7485
{
7586
Authenticate(false, targetHost, null, null, enabledSslProtocols);
7687
}
7788

7889
/// <summary>
79-
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
90+
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
8091
/// The authentication process uses the specified certificate collections and SSL protocols.
8192
/// </summary>
8293
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
@@ -85,13 +96,27 @@ public void AuthenticateAsClient(string targetHost, SslProtocols enabledSslProto
8596
/// <remarks>
8697
/// 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.
8798
/// </remarks>
99+
/// <exception cref="InvalidOperationException">Authentication has already been performed on this stream, or all native SSL context slots are in.</exception>
100+
/// <exception cref="OutOfMemoryException">A memory allocation failed while setting up the SSL context. The device may be low on heap.</exception>
101+
/// <exception cref="System.Security.Cryptography.CryptographicException">
102+
/// SSL context initialisation failed. The <see cref="System.Security.Cryptography.CryptographicException.ErrorCode"/> property contains the
103+
/// corresponding <see cref="SslError"/> value:
104+
/// <see cref="SslError.DrbgSeedFailed"/> — entropy source could not be initialised;
105+
/// <see cref="SslError.ConfigDefaultsFailed"/> — internal mbedTLS configuration error;
106+
/// <see cref="SslError.UnsupportedProtocolVersion"/> — the requested TLS version is not supported on this device;
107+
/// <see cref="SslError.CertificateParseFailed"/> — the client certificate could not be parsed;
108+
/// <see cref="SslError.PrivateKeyParseFailed"/> — the client private key could not be parsed;
109+
/// <see cref="SslError.OwnCertConfigFailed"/> — configuring the certificate/key pair on the SSL context failed;
110+
/// <see cref="SslError.SetupFailed"/> — final SSL context setup failed.
111+
/// </exception>
112+
/// <exception cref="SocketException">The TLS handshake with the remote server failed.</exception>
88113
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, SslProtocols enabledSslProtocols)
89114
{
90115
Authenticate(false, targetHost, clientCertificate, null, enabledSslProtocols);
91116
}
92117

93118
/// <summary>
94-
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
119+
/// Called by clients to authenticate the server and optionally the client in a client-server connection.
95120
/// The authentication process uses the specified certificate collections and SSL protocols.
96121
/// </summary>
97122
/// <param name="targetHost">The name of the server that will share this SslStream.</param>
@@ -101,20 +126,48 @@ public void AuthenticateAsClient(string targetHost, X509Certificate clientCertif
101126
/// <remarks>
102127
/// 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.
103128
/// </remarks>
129+
/// <exception cref="InvalidOperationException">Authentication has already been performed on this stream, or all native SSL context slots are in.</exception>
130+
/// <exception cref="OutOfMemoryException">A memory allocation failed while setting up the SSL context. The device may be low on heap.</exception>
131+
/// <exception cref="System.Security.Cryptography.CryptographicException">
132+
/// SSL context initialisation failed. The <see cref="System.Security.Cryptography.CryptographicException.ErrorCode"/> property contains the
133+
/// corresponding <see cref="SslError"/> value:
134+
/// <see cref="SslError.DrbgSeedFailed"/> — entropy source could not be initialised;
135+
/// <see cref="SslError.ConfigDefaultsFailed"/> — internal mbedTLS configuration error;
136+
/// <see cref="SslError.UnsupportedProtocolVersion"/> — the requested TLS version is not supported on this device;
137+
/// <see cref="SslError.CertificateParseFailed"/> — the client or CA certificate could not be parsed;
138+
/// <see cref="SslError.PrivateKeyParseFailed"/> — the client private key could not be parsed;
139+
/// <see cref="SslError.OwnCertConfigFailed"/> — configuring the certificate/key pair on the SSL context failed;
140+
/// <see cref="SslError.SetupFailed"/> — final SSL context setup failed.
141+
/// </exception>
142+
/// <exception cref="SocketException">The TLS handshake with the remote server failed.</exception>
104143
public void AuthenticateAsClient(string targetHost, X509Certificate clientCertificate, X509Certificate ca, SslProtocols enabledSslProtocols)
105144
{
106145
Authenticate(false, targetHost, clientCertificate, ca, enabledSslProtocols);
107146
}
108147

109148
/// <summary>
110-
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificate,
149+
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificate,
111150
/// verification requirements and security protocol.
112151
/// </summary>
113152
/// <param name="serverCertificate">The certificate used to authenticate the server.</param>
114153
/// <param name="enabledSslProtocols">The protocols that may be used for authentication.</param>
115154
/// <remarks>
116155
/// 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.
117156
/// </remarks>
157+
/// <exception cref="InvalidOperationException">Authentication has already been performed on this stream, or all native SSL context slots are in.</exception>
158+
/// <exception cref="OutOfMemoryException">A memory allocation failed while setting up the SSL context. The device may be low on heap.</exception>
159+
/// <exception cref="System.Security.Cryptography.CryptographicException">
160+
/// SSL context initialisation failed. The <see cref="System.Security.Cryptography.CryptographicException.ErrorCode"/> property contains the
161+
/// corresponding <see cref="SslError"/> value:
162+
/// <see cref="SslError.DrbgSeedFailed"/> — entropy source could not be initialised;
163+
/// <see cref="SslError.ConfigDefaultsFailed"/> — internal mbedTLS configuration error;
164+
/// <see cref="SslError.UnsupportedProtocolVersion"/> — the requested TLS version is not supported on this device;
165+
/// <see cref="SslError.CertificateParseFailed"/> — the server certificate could not be parsed;
166+
/// <see cref="SslError.PrivateKeyParseFailed"/> — the server private key could not be parsed;
167+
/// <see cref="SslError.OwnCertConfigFailed"/> — configuring the certificate/key pair on the SSL context failed;
168+
/// <see cref="SslError.SetupFailed"/> — final SSL context setup failed.
169+
/// </exception>
170+
/// <exception cref="SocketException">The TLS handshake with the remote client failed.</exception>
118171
public void AuthenticateAsServer(X509Certificate serverCertificate, SslProtocols enabledSslProtocols)
119172
{
120173
Authenticate(true, "", serverCertificate, null, enabledSslProtocols);
@@ -124,11 +177,25 @@ public void AuthenticateAsServer(X509Certificate serverCertificate, SslProtocols
124177
/// Called by servers to authenticate the server and optionally the client in a client-server connection using the specified certificates, requirements and security protocol.
125178
/// </summary>
126179
/// <param name="serverCertificate">The X509Certificate used to authenticate the server.</param>
127-
/// <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>
180+
/// <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>
128181
/// <param name="enabledSslProtocols">The protocols that may be used for authentication.</param>
129182
/// <remarks>
130183
/// 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.
131184
/// </remarks>
185+
/// <exception cref="InvalidOperationException">Authentication has already been performed on this stream, or all native SSL context slots are in.</exception>
186+
/// <exception cref="OutOfMemoryException">A memory allocation failed while setting up the SSL context. The device may be low on heap.</exception>
187+
/// <exception cref="System.Security.Cryptography.CryptographicException">
188+
/// SSL context initialisation failed. The <see cref="System.Security.Cryptography.CryptographicException.ErrorCode"/> property contains the
189+
/// corresponding <see cref="SslError"/> value:
190+
/// <see cref="SslError.DrbgSeedFailed"/> — entropy source could not be initialised;
191+
/// <see cref="SslError.ConfigDefaultsFailed"/> — internal mbedTLS configuration error;
192+
/// <see cref="SslError.UnsupportedProtocolVersion"/> — the requested TLS version is not supported on this device;
193+
/// <see cref="SslError.CertificateParseFailed"/> — the server certificate could not be parsed;
194+
/// <see cref="SslError.PrivateKeyParseFailed"/> — the server private key could not be parsed;
195+
/// <see cref="SslError.OwnCertConfigFailed"/> — configuring the certificate/key pair on the SSL context failed;
196+
/// <see cref="SslError.SetupFailed"/> — final SSL context setup failed.
197+
/// </exception>
198+
/// <exception cref="SocketException">The TLS handshake with the remote client failed.</exception>
132199
public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols)
133200
{
134201
SslVerification = clientCertificateRequired ? SslVerification.VerifyClientOnce : SslVerification.NoVerification;

‎nanoFramework.System.Net/System.Net.nfproj‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<ProjectGuid>474A90F4-FA98-4011-AABB-7DAC8E218E6A</ProjectGuid>
1313
<OutputType>Library</OutputType>
1414
<FileAlignment>512</FileAlignment>
15-
<RootNamespace></RootNamespace>
15+
<RootNamespace>
16+
</RootNamespace>
1617
<AssemblyName>System.Net</AssemblyName>
1718
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
1819
<NF_IsCoreLibrary>True</NF_IsCoreLibrary>
@@ -61,7 +62,9 @@
6162
<Compile Include="NetworkInformation\WirelessAPConfiguration.cs" />
6263
<Compile Include="Properties\AssemblyInfo.cs" />
6364
<Compile Include="Security\CertificateManager.cs" />
65+
<Compile Include="Security\CryptographicException.cs" />
6466
<Compile Include="Security\NetworkSecurity.cs" />
67+
<Compile Include="Security\SslError.cs" />
6568
<Compile Include="Security\SslStream.cs" />
6669
<Compile Include="SocketAddress.cs" />
6770
<Compile Include="SocketException.cs" />

0 commit comments

Comments
 (0)