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

check Application URI of the server Certificate on OpenSecureChannel #2583

Merged
merged 3 commits into from May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions Libraries/Opc.Ua.Client/Session.cs
Expand Up @@ -2319,6 +2319,7 @@

if (requireEncryption)
{
ValidateServerCertificateApplicationUri(serverCertificate);
if (checkDomain)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically the spec outlines to check the responses, but since the cert in the response is already binary compared to this one this is a good place to catch a misaligned application uri before connection! 👍🏼

{
m_configuration.CertificateValidator.Validate(serverCertificateChain, m_endpoint);
Expand Down Expand Up @@ -5264,6 +5265,26 @@
!String.IsNullOrEmpty(identityPolicy.SecurityPolicyUri);
}
}
/// <summary>
/// Validates the ServerCertificate ApplicationUri to match the ApplicationUri of the Endpoint for an open call (Spec Part 4 5.4.1)
/// </summary>
private void ValidateServerCertificateApplicationUri(
X509Certificate2 serverCertificate)
{
var applicationUri = m_endpoint.Description.Server.ApplicationUri;
//check is only neccessary if the ApplicatioUri is specified for the Endpoint
romanett marked this conversation as resolved.
Show resolved Hide resolved
if (string.IsNullOrEmpty(applicationUri))
{
return;

Check warning on line 5278 in Libraries/Opc.Ua.Client/Session.cs

View check run for this annotation

Codecov / codecov/patch

Libraries/Opc.Ua.Client/Session.cs#L5278

Added line #L5278 was not covered by tests
}
string certificateApplicationUri = X509Utils.GetApplicationUriFromCertificate(serverCertificate);
if (certificateApplicationUri != applicationUri)
romanett marked this conversation as resolved.
Show resolved Hide resolved
{
throw ServiceResultException.Create(
StatusCodes.BadSecurityChecksFailed,
"Server did not return a Certificate matching the ApplicationUri specified in the EndpointDescription.");

Check warning on line 5285 in Libraries/Opc.Ua.Client/Session.cs

View check run for this annotation

Codecov / codecov/patch

Libraries/Opc.Ua.Client/Session.cs#L5283-L5285

Added lines #L5283 - L5285 were not covered by tests
}
}

private void BuildCertificateData(out byte[] clientCertificateData, out byte[] clientCertificateChainData)
{
Expand Down
1 change: 1 addition & 0 deletions Libraries/Opc.Ua.Client/SessionAsync.cs
Expand Up @@ -93,6 +93,7 @@ public partial class Session : SessionClientBatched, ISession

if (requireEncryption)
{
ValidateServerCertificateApplicationUri(serverCertificate);
if (checkDomain)
{
await m_configuration.CertificateValidator.ValidateAsync(serverCertificateChain, m_endpoint, ct).ConfigureAwait(false);
Expand Down