Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SslVerification to HttpClient #400

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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