9aff90f884
SDK 2.0.1 checks the server host name in TWO places: Session.OpenAsync (after the chain is trusted) and DefaultSessionFactory.CreateChannelAsync, against the certificate in the GetEndpoints reply and before any security-mode test. opc-plc sends a certificate on its None endpoint, so `connect -u opc.tcp://10.100.0.35:50000 --validate-host-name` was refused on an unauthenticated channel, where a host check proves nothing. All three call sites now pass checkDomain only when the selected endpoint's SecurityMode != None: OpcUaEndpointSelection.CheckDomainFor for the driver (OpenSessionOnEndpointAsync, so the failover sweep too) and the AdminUI browser; the same expression in Client.Shared ConnectToEndpointAsync (shared by failover). New live tests: driver None + flag on vs :50000 reads; browser None + flag on vs the NAT fixture browses; the secure + flag refusals still hold. Docs: name both check sites; a zone-qualified IPv6 host never matches (FindDomain compares the zone too), so link-local needs validateServerHostName=false; the match is loose (first label alone, subject DC= parts count, no wildcards), which caveats the CA recommendation.
97 lines
5.8 KiB
C#
97 lines
5.8 KiB
C#
using Opc.Ua;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient;
|
|
|
|
/// <summary>
|
|
/// Policy-aware endpoint selection shared by the runtime driver and the AdminUI browse picker, so the browser
|
|
/// connects exactly where (and how) the deployed driver will. The SDK's <c>CoreClientUtils.SelectEndpointAsync</c>
|
|
/// only honours a boolean "use security" flag; an operator asking for <c>Basic256Sha256</c> against a server that
|
|
/// also offers <c>Basic128Rsa15</c> must not silently end up on the weaker cipher, so the policy + mode filter is
|
|
/// explicit and fails loudly. The selected endpoint's URL is then normalised by
|
|
/// <see cref="OpcUaEndpointUrlRewriter.Rewrite"/> (Gitea #676 driver, #806 browser).
|
|
/// </summary>
|
|
public static class OpcUaEndpointSelection
|
|
{
|
|
/// <summary>
|
|
/// Pick the first advertised endpoint whose <c>SecurityPolicyUri</c> AND <c>SecurityMode</c> match, and point its
|
|
/// <c>EndpointUrl</c> at the configured host/port (advertised path kept). Security policy, mode, server
|
|
/// certificate and user-token policies are taken from the server's advertisement unchanged.
|
|
/// </summary>
|
|
/// <param name="advertised">Everything GetEndpoints returned.</param>
|
|
/// <param name="configuredUrl">The URL GetEndpoints was sent to — the operator-configured, reachable address.</param>
|
|
/// <param name="policy">Required security policy.</param>
|
|
/// <param name="mode">Required message security mode.</param>
|
|
/// <returns>The matching endpoint, its URL rewritten.</returns>
|
|
/// <exception cref="InvalidOperationException">No advertised endpoint matches the policy + mode pair.</exception>
|
|
public static EndpointDescription SelectMatching(
|
|
IEnumerable<EndpointDescription> advertised, string configuredUrl,
|
|
OpcUaSecurityPolicy policy, OpcUaSecurityMode mode)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(advertised);
|
|
var all = advertised.ToList();
|
|
var wantedPolicyUri = MapSecurityPolicy(policy);
|
|
var wantedMode = ToMessageSecurityMode(mode);
|
|
|
|
var match = all.FirstOrDefault(e => e.SecurityPolicyUri == wantedPolicyUri && e.SecurityMode == wantedMode);
|
|
if (match is null)
|
|
{
|
|
var list = string.Join(", ", all.Select(e => $"{ShortPolicyName(e.SecurityPolicyUri)}/{e.SecurityMode}"));
|
|
throw new InvalidOperationException(
|
|
$"No endpoint at '{configuredUrl}' matches SecurityPolicy={policy} + SecurityMode={mode}. " +
|
|
$"Server advertises: {list}");
|
|
}
|
|
|
|
// Connect where the operator pointed us, not where the server says it lives.
|
|
match.EndpointUrl = OpcUaEndpointUrlRewriter.Rewrite(match.EndpointUrl, configuredUrl);
|
|
return match;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gitea #816 — the <c>checkDomain</c> to hand the SDK for <paramref name="selected"/>: the operator's choice, but
|
|
/// never on a <see cref="MessageSecurityMode.None"/> endpoint. SDK 2.0.1 checks the host in TWO places — in
|
|
/// <c>DefaultSessionFactory.CreateChannelAsync</c>, against the certificate in the GetEndpoints reply, BEFORE any
|
|
/// security-mode test (opc-plc sends one even on its None endpoint), and in <c>Session.OpenAsync</c> once the
|
|
/// chain is trusted — so passing <c>true</c> on a None session refused it on a certificate the channel never
|
|
/// uses. A host-name check on an unauthenticated channel proves nothing.
|
|
/// </summary>
|
|
/// <param name="selected">The endpoint the session will open on.</param>
|
|
/// <param name="validateServerHostName">The configured (effective) host-name policy.</param>
|
|
/// <returns>The <c>checkDomain</c> argument.</returns>
|
|
public static bool CheckDomainFor(EndpointDescription selected, bool validateServerHostName)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(selected);
|
|
return validateServerHostName && selected.SecurityMode != MessageSecurityMode.None;
|
|
}
|
|
|
|
/// <summary>Convert a driver <see cref="OpcUaSecurityPolicy"/> to the OPC UA policy URI.</summary>
|
|
/// <param name="policy">The driver security policy to map.</param>
|
|
/// <returns>The corresponding OPC UA security policy URI.</returns>
|
|
public static string MapSecurityPolicy(OpcUaSecurityPolicy policy) => policy switch
|
|
{
|
|
OpcUaSecurityPolicy.None => SecurityPolicies.None,
|
|
OpcUaSecurityPolicy.Basic128Rsa15 => SecurityPolicies.Basic128Rsa15,
|
|
OpcUaSecurityPolicy.Basic256 => SecurityPolicies.Basic256,
|
|
OpcUaSecurityPolicy.Basic256Sha256 => SecurityPolicies.Basic256Sha256,
|
|
OpcUaSecurityPolicy.Aes128_Sha256_RsaOaep => SecurityPolicies.Aes128_Sha256_RsaOaep,
|
|
OpcUaSecurityPolicy.Aes256_Sha256_RsaPss => SecurityPolicies.Aes256_Sha256_RsaPss,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(policy), policy, null),
|
|
};
|
|
|
|
/// <summary>Convert a driver <see cref="OpcUaSecurityMode"/> to the SDK message security mode.</summary>
|
|
/// <param name="mode">The driver security mode to map.</param>
|
|
/// <returns>The corresponding <see cref="MessageSecurityMode"/>.</returns>
|
|
public static MessageSecurityMode ToMessageSecurityMode(OpcUaSecurityMode mode) => mode switch
|
|
{
|
|
OpcUaSecurityMode.None => MessageSecurityMode.None,
|
|
OpcUaSecurityMode.Sign => MessageSecurityMode.Sign,
|
|
OpcUaSecurityMode.SignAndEncrypt => MessageSecurityMode.SignAndEncrypt,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(mode)),
|
|
};
|
|
|
|
/// <summary>Render an OPC UA security-policy URI as its short suffix for diagnostic messages.</summary>
|
|
/// <param name="policyUri">Full policy URI to shorten.</param>
|
|
/// <returns>The suffix after the last <c>#</c>, or <c>(null)</c>.</returns>
|
|
public static string ShortPolicyName(string? policyUri) =>
|
|
policyUri?.Substring(policyUri.LastIndexOf('#') + 1) ?? "(null)";
|
|
}
|