Skip to content

Commit 319befa

Browse files
authored
Add SslVerification to HttpClient (#400)
***NO_CI***
1 parent 8e8511a commit 319befa

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

nanoFramework.System.Net.Http/Http/HttpClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ public TimeSpan Timeout
130130
/// </remarks>
131131
public SslProtocols SslProtocols { get; set; } = SslProtocols.Tls12;
132132

133+
/// <summary>
134+
/// Gets or sets the TLS/SSL verification mode used by the <see cref="HttpClient"/> class.
135+
/// </summary>
136+
/// <remarks>
137+
/// Default value is <see cref="SslVerification.CertificateRequired"/>.
138+
/// </remarks>
139+
public SslVerification SslVerification { get; set; } = SslVerification.CertificateRequired;
140+
133141
#region Constructors
134142

135143
/// <summary>
@@ -408,6 +416,7 @@ private HttpResponseMessage SendWorker(HttpRequestMessage request, HttpCompletio
408416
clientHandler.SetWebRequestTimeout(_timeout);
409417
clientHandler.SetWebRequestSslProcol(SslProtocols);
410418
clientHandler.SetWebRequestHttpAuthCert(HttpsAuthentCert);
419+
clientHandler.SetWebRequestSslVerification(SslVerification);
411420
}
412421

413422
HttpResponseMessage response = base.Send(request);

nanoFramework.System.Net.Http/Http/HttpClientHandler.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public partial class HttpClientHandler : HttpMessageHandler
2626
private X509Certificate _caCert;
2727
private X509Certificate _clientCert;
2828
private ClientCertificateOption _clientCertificateOptions = ClientCertificateOption.Manual;
29+
private SslVerification _sslVerification;
2930

3031
/// <summary>
3132
/// Gets or sets a value that indicates if the certificate is automatically picked from the certificate store or if the caller is allowed to pass in a specific client certificate.
@@ -306,6 +307,7 @@ private HttpWebRequest CreateWebRequest(HttpRequestMessage request)
306307

307308
wr.SslProtocols = _sslProtocols;
308309
wr.HttpsAuthentCert = _caCert;
310+
wr.SslVerification = _sslVerification;
309311

310312
if (ClientCertificateOptions == ClientCertificateOption.Manual)
311313
{
@@ -392,5 +394,10 @@ internal void SetWebRequestHttpAuthCert(X509Certificate certificate)
392394
{
393395
_caCert = certificate;
394396
}
397+
398+
internal void SetWebRequestSslVerification(SslVerification sslVerification)
399+
{
400+
_sslVerification = sslVerification;
401+
}
395402
}
396403
}

nanoFramework.System.Net.Http/Http/System.Net.HttpWebRequest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ protected override void Dispose(bool disposing)
270270
'\t',
271271
'\n'};
272272

273+
/// <summary>
274+
/// Exposes this property from <see cref="SslStream.SslVerification"/>.
275+
/// </summary>
276+
private SslVerification _sslVerification;
277+
273278
/// <summary>
274279
/// The maximum length, in kilobytes (1024 bytes), of the response
275280
/// headers.
@@ -378,6 +383,14 @@ public X509Certificate HttpsAuthentCert
378383
set { m_caCert = value; }
379384
}
380385

386+
/// <summary>
387+
/// Gets or sets
388+
/// </summary>
389+
public SslVerification SslVerification
390+
{
391+
get { return _sslVerification; }
392+
set { _sslVerification = value; }
393+
}
381394

382395
/// <summary>
383396
/// Gets or sets the TLS/SSL protocol used by the <see cref="HttpWebRequest"/> class.
@@ -1497,6 +1510,8 @@ private InputNetworkStreamWrapper EstablishConnection(Uri proxyServer, Uri targe
14971510
// Once connection established need to create secure stream and authenticate server.
14981511
SslStream sslStream = new SslStream(retStream.m_Socket);
14991512

1513+
sslStream.SslVerification = _sslVerification;
1514+
15001515
// Throws exception if it fails
15011516
sslStream.AuthenticateAsClient(m_originalUrl.Host, null, m_caCert, m_sslProtocols);
15021517

0 commit comments

Comments
 (0)