using Opc.Ua; namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient; /// /// 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 CoreClientUtils.SelectEndpointAsync /// only honours a boolean "use security" flag; an operator asking for Basic256Sha256 against a server that /// also offers Basic128Rsa15 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 /// (Gitea #676 driver, #806 browser). /// public static class OpcUaEndpointSelection { /// /// Pick the first advertised endpoint whose SecurityPolicyUri AND SecurityMode match, and point its /// EndpointUrl 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. /// /// Everything GetEndpoints returned. /// The URL GetEndpoints was sent to — the operator-configured, reachable address. /// Required security policy. /// Required message security mode. /// The matching endpoint, its URL rewritten. /// No advertised endpoint matches the policy + mode pair. public static EndpointDescription SelectMatching( IEnumerable 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; } /// /// Gitea #816 — the checkDomain to hand the SDK for : the operator's choice, but /// never on a endpoint. SDK 2.0.1 checks the host in TWO places — in /// DefaultSessionFactory.CreateChannelAsync, against the certificate in the GetEndpoints reply, BEFORE any /// security-mode test (opc-plc sends one even on its None endpoint), and in Session.OpenAsync once the /// chain is trusted — so passing true on a None session refused it on a certificate the channel never /// uses. A host-name check on an unauthenticated channel proves nothing. /// /// The endpoint the session will open on. /// The configured (effective) host-name policy. /// The checkDomain argument. public static bool CheckDomainFor(EndpointDescription selected, bool validateServerHostName) { ArgumentNullException.ThrowIfNull(selected); return validateServerHostName && selected.SecurityMode != MessageSecurityMode.None; } /// Convert a driver to the OPC UA policy URI. /// The driver security policy to map. /// The corresponding OPC UA security policy URI. 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), }; /// Convert a driver to the SDK message security mode. /// The driver security mode to map. /// The corresponding . 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)), }; /// Render an OPC UA security-policy URI as its short suffix for diagnostic messages. /// Full policy URI to shorten. /// The suffix after the last #, or (null). public static string ShortPolicyName(string? policyUri) => policyUri?.Substring(policyUri.LastIndexOf('#') + 1) ?? "(null)"; }