Skip to content

Commit

Permalink
Add SslVerification to HttpClient (#400)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Oct 4, 2023
1 parent 8e8511a commit 319befa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions nanoFramework.System.Net.Http/Http/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ public TimeSpan Timeout
/// </remarks>
public SslProtocols SslProtocols { get; set; } = SslProtocols.Tls12;

/// <summary>
/// Gets or sets the TLS/SSL verification mode used by the <see cref="HttpClient"/> class.
/// </summary>
/// <remarks>
/// Default value is <see cref="SslVerification.CertificateRequired"/>.
/// </remarks>
public SslVerification SslVerification { get; set; } = SslVerification.CertificateRequired;

#region Constructors

/// <summary>
Expand Down Expand Up @@ -408,6 +416,7 @@ private HttpResponseMessage SendWorker(HttpRequestMessage request, HttpCompletio
clientHandler.SetWebRequestTimeout(_timeout);
clientHandler.SetWebRequestSslProcol(SslProtocols);
clientHandler.SetWebRequestHttpAuthCert(HttpsAuthentCert);
clientHandler.SetWebRequestSslVerification(SslVerification);
}

HttpResponseMessage response = base.Send(request);
Expand Down
7 changes: 7 additions & 0 deletions nanoFramework.System.Net.Http/Http/HttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class HttpClientHandler : HttpMessageHandler
private X509Certificate _caCert;
private X509Certificate _clientCert;
private ClientCertificateOption _clientCertificateOptions = ClientCertificateOption.Manual;
private SslVerification _sslVerification;

/// <summary>
/// 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.
Expand Down Expand Up @@ -306,6 +307,7 @@ private HttpWebRequest CreateWebRequest(HttpRequestMessage request)

wr.SslProtocols = _sslProtocols;
wr.HttpsAuthentCert = _caCert;
wr.SslVerification = _sslVerification;

if (ClientCertificateOptions == ClientCertificateOption.Manual)
{
Expand Down Expand Up @@ -392,5 +394,10 @@ internal void SetWebRequestHttpAuthCert(X509Certificate certificate)
{
_caCert = certificate;
}

internal void SetWebRequestSslVerification(SslVerification sslVerification)
{
_sslVerification = sslVerification;
}
}
}
15 changes: 15 additions & 0 deletions nanoFramework.System.Net.Http/Http/System.Net.HttpWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ protected override void Dispose(bool disposing)
'\t',
'\n'};

/// <summary>
/// Exposes this property from <see cref="SslStream.SslVerification"/>.
/// </summary>
private SslVerification _sslVerification;

/// <summary>
/// The maximum length, in kilobytes (1024 bytes), of the response
/// headers.
Expand Down Expand Up @@ -378,6 +383,14 @@ public X509Certificate HttpsAuthentCert
set { m_caCert = value; }
}

/// <summary>
/// Gets or sets
/// </summary>
public SslVerification SslVerification
{
get { return _sslVerification; }
set { _sslVerification = value; }
}

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

sslStream.SslVerification = _sslVerification;

// Throws exception if it fails
sslStream.AuthenticateAsClient(m_originalUrl.Host, null, m_caCert, m_sslProtocols);

Expand Down

0 comments on commit 319befa

Please sign in to comment.