mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-28 14:09:56 +08:00
## Failure MCP auto-accept mutated the shared application certificate manager and accepted every certificate error, so one permissive connection could affect concurrent or later strict sessions. The durable-subscription sample also serialized complete user identity tokens, including user-name passwords and issued bearer tokens. ## Fix - Isolate auto-accept certificate managers/configurations per connection and keep their lifetime with the managed session. - Auto-accept only `BadCertificateUntrusted`; retain strict validation for all other certificate errors and shared sessions. - Version the durable store and reject the previous unsafe format. - Remove user-name passwords before persistence and reject issued-token subscriptions rather than storing bearer credentials. - Add focused tools tests plus solution, project, and durable-subscription docs. ## Reference - OPC 10000-4 Session identity-token and application-certificate security. - Split from #4021. ## Tests - `Opc.Ua.Tools.Tests` Release: 7 passed on net10.0; 4 passed on net48. - `Quickstarts.Servers` Release builds passed on net10.0 and net48 with zero warnings/errors. - `Opc.Ua.Mcp` Release net10.0 build passed with zero warnings/errors. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
476 lines
20 KiB
C#
476 lines
20 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
|
|
*
|
|
* OPC Foundation MIT License 1.00
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* The complete license agreement can be found here:
|
|
* http://opcfoundation.org/License/MIT/1.00/
|
|
* ======================================================================*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Server;
|
|
|
|
namespace Quickstarts.Servers
|
|
{
|
|
public class SubscriptionStore : ISubscriptionStore
|
|
{
|
|
private static readonly string s_storage_path = Path.Combine(
|
|
Environment.CurrentDirectory,
|
|
"Durable Subscriptions");
|
|
|
|
private const string kFilename = "subscriptionsStore.bin";
|
|
private const uint kStoreMagic = 0x44535541;
|
|
private const uint kStoreVersion = 1;
|
|
private readonly DurableMonitoredItemQueueFactory? m_durableMonitoredItemQueueFactory;
|
|
private readonly ILogger m_logger;
|
|
private readonly IServiceMessageContext m_messageContext;
|
|
|
|
public SubscriptionStore(IServerInternal server)
|
|
{
|
|
m_logger = server.Telemetry.CreateLogger<SubscriptionStore>();
|
|
m_messageContext = server.MessageContext;
|
|
m_durableMonitoredItemQueueFactory = server
|
|
.MonitoredItemQueueFactory as DurableMonitoredItemQueueFactory;
|
|
}
|
|
|
|
public ValueTask<bool> StoreSubscriptionsAsync(
|
|
IEnumerable<IStoredSubscription> subscriptions,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return new ValueTask<bool>(StoreSubscriptionsCore(subscriptions));
|
|
}
|
|
|
|
private bool StoreSubscriptionsCore(IEnumerable<IStoredSubscription> subscriptions)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(s_storage_path))
|
|
{
|
|
Directory.CreateDirectory(s_storage_path);
|
|
}
|
|
|
|
var subs = subscriptions.Cast<StoredSubscription>().ToList();
|
|
// Validate identities before File.Create can truncate an existing store.
|
|
foreach (StoredSubscription sub in subs)
|
|
{
|
|
_ = SanitizeUserIdentityToken(sub.UserIdentityToken);
|
|
}
|
|
|
|
using (FileStream fileStream = File.Create(
|
|
Path.Combine(s_storage_path, kFilename)))
|
|
using (var encoder = new BinaryEncoder(
|
|
fileStream, m_messageContext, true))
|
|
{
|
|
WriteStoreHeader(encoder);
|
|
encoder.WriteStringArray(
|
|
null, m_messageContext.NamespaceUris.ToArrayOf());
|
|
encoder.WriteStringArray(
|
|
null, m_messageContext.ServerUris.ToArrayOf());
|
|
|
|
encoder.WriteInt32(null, subs.Count);
|
|
foreach (StoredSubscription sub in subs)
|
|
{
|
|
EncodeSubscription(encoder, sub);
|
|
}
|
|
}
|
|
|
|
if (m_durableMonitoredItemQueueFactory != null)
|
|
{
|
|
IEnumerable<uint> ids = subscriptions
|
|
.SelectMany(s => s.MonitoredItems
|
|
.Select(m => m.Id));
|
|
m_durableMonitoredItemQueueFactory.PersistQueues(ids, s_storage_path);
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.FailedToStoreSubscriptions(ex);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public ValueTask<RestoreSubscriptionResult> RestoreSubscriptionsAsync(
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return new ValueTask<RestoreSubscriptionResult>(RestoreSubscriptionsCore());
|
|
}
|
|
|
|
private RestoreSubscriptionResult RestoreSubscriptionsCore()
|
|
{
|
|
string filePath = Path.Combine(s_storage_path, kFilename);
|
|
try
|
|
{
|
|
if (File.Exists(filePath))
|
|
{
|
|
List<IStoredSubscription> result;
|
|
using (FileStream fileStream = File.OpenRead(filePath))
|
|
using (var decoder = new BinaryDecoder(
|
|
fileStream, m_messageContext, true))
|
|
{
|
|
ValidateStoreHeader(decoder);
|
|
ArrayOf<string> nsUris = decoder.ReadStringArray(null)!;
|
|
ArrayOf<string> serverUris = decoder.ReadStringArray(null)!;
|
|
decoder.SetMappingTables(
|
|
new NamespaceTable(nsUris.Memory.ToArray()),
|
|
new StringTable(serverUris.Memory.ToArray()));
|
|
|
|
int count = decoder.ReadInt32(null);
|
|
result = new List<IStoredSubscription>(count);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
result.Add(DecodeSubscription(decoder));
|
|
}
|
|
}
|
|
|
|
File.Delete(filePath);
|
|
return new RestoreSubscriptionResult(true, result);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.FailedToRestoreSubscriptions(ex);
|
|
}
|
|
|
|
return new RestoreSubscriptionResult(false, null);
|
|
}
|
|
|
|
public IDataChangeMonitoredItemQueue RestoreDataChangeMonitoredItemQueue(
|
|
uint monitoredItemId)
|
|
{
|
|
return m_durableMonitoredItemQueueFactory?.RestoreDataChangeQueue(
|
|
monitoredItemId,
|
|
s_storage_path)!;
|
|
}
|
|
|
|
public IEventMonitoredItemQueue RestoreEventMonitoredItemQueue(uint monitoredItemId)
|
|
{
|
|
return m_durableMonitoredItemQueueFactory?.RestoreEventQueue(
|
|
monitoredItemId,
|
|
s_storage_path)!;
|
|
}
|
|
|
|
public ValueTask<IDataChangeMonitoredItemQueue?> RestoreDataChangeMonitoredItemQueueAsync(
|
|
uint monitoredItemId,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return new ValueTask<IDataChangeMonitoredItemQueue?>(
|
|
RestoreDataChangeMonitoredItemQueue(monitoredItemId));
|
|
}
|
|
|
|
public ValueTask<IEventMonitoredItemQueue?> RestoreEventMonitoredItemQueueAsync(
|
|
uint monitoredItemId,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return new ValueTask<IEventMonitoredItemQueue?>(
|
|
RestoreEventMonitoredItemQueue(monitoredItemId));
|
|
}
|
|
|
|
public ValueTask OnSubscriptionRestoreCompleteAsync(
|
|
Dictionary<uint, ArrayOf<uint>> createdSubscriptions,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
string filePath = Path.Combine(s_storage_path, kFilename);
|
|
|
|
// remove old file
|
|
if (File.Exists(filePath))
|
|
{
|
|
try
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.FailedToCleanupStoredSubscriptionFiles(ex);
|
|
}
|
|
}
|
|
// remove old batches & queues
|
|
if (m_durableMonitoredItemQueueFactory != null)
|
|
{
|
|
IEnumerable<uint> ids = createdSubscriptions.SelectMany(s => s.Value.Memory.ToArray());
|
|
m_durableMonitoredItemQueueFactory.CleanStoredQueues(s_storage_path, ids);
|
|
}
|
|
return default;
|
|
}
|
|
|
|
internal static void WriteStoreHeader(BinaryEncoder encoder)
|
|
{
|
|
encoder.WriteUInt32(null, kStoreMagic);
|
|
encoder.WriteUInt32(null, kStoreVersion);
|
|
}
|
|
|
|
internal static void ValidateStoreHeader(BinaryDecoder decoder)
|
|
{
|
|
uint magic = decoder.ReadUInt32(null);
|
|
if (magic != kStoreMagic)
|
|
{
|
|
throw new InvalidDataException(
|
|
"The durable subscription store has an invalid header or uses the " +
|
|
"legacy unsafe format.");
|
|
}
|
|
|
|
uint version = decoder.ReadUInt32(null);
|
|
if (version != kStoreVersion)
|
|
{
|
|
throw new InvalidDataException(
|
|
$"Unsupported durable subscription store version {version}.");
|
|
}
|
|
}
|
|
|
|
public static void EncodeSubscription(
|
|
BinaryEncoder encoder, StoredSubscription subscription)
|
|
{
|
|
encoder.WriteUInt32(null, subscription.Id);
|
|
encoder.WriteBoolean(null, subscription.IsDurable);
|
|
encoder.WriteUInt32(null, subscription.LifetimeCounter);
|
|
encoder.WriteUInt32(null, subscription.MaxLifetimeCount);
|
|
encoder.WriteUInt32(null, subscription.MaxKeepaliveCount);
|
|
encoder.WriteUInt32(null, subscription.MaxMessageCount);
|
|
encoder.WriteUInt32(null, subscription.MaxNotificationsPerPublish);
|
|
encoder.WriteDouble(null, subscription.PublishingInterval);
|
|
encoder.WriteByte(null, subscription.Priority);
|
|
encoder.WriteInt32(null, subscription.LastSentMessage);
|
|
encoder.WriteUInt32(null, subscription.SequenceNumber);
|
|
|
|
UserIdentityToken? sanitizedIdentityToken =
|
|
SanitizeUserIdentityToken(subscription.UserIdentityToken);
|
|
encoder.WriteExtensionObject(null,
|
|
sanitizedIdentityToken != null
|
|
? new ExtensionObject(sanitizedIdentityToken)
|
|
: ExtensionObject.Null);
|
|
|
|
ExtensionObject[] sentMsgs = subscription.SentMessages?
|
|
.Select(m => new ExtensionObject(m)).ToArray() ??
|
|
[];
|
|
encoder.WriteExtensionObjectArray(null,
|
|
new ArrayOf<ExtensionObject>(sentMsgs));
|
|
|
|
List<StoredMonitoredItem> items = subscription.MonitoredItems?
|
|
.Cast<StoredMonitoredItem>().ToList() ??
|
|
[];
|
|
encoder.WriteInt32(null, items.Count);
|
|
foreach (StoredMonitoredItem item in items)
|
|
{
|
|
EncodeMonitoredItem(encoder, item);
|
|
}
|
|
}
|
|
|
|
internal static void EncodeMonitoredItem(
|
|
BinaryEncoder encoder, StoredMonitoredItem item)
|
|
{
|
|
encoder.WriteBoolean(null, item.IsRestored);
|
|
encoder.WriteUInt32(null, item.SubscriptionId);
|
|
encoder.WriteUInt32(null, item.Id);
|
|
encoder.WriteInt32(null, item.TypeMask);
|
|
encoder.WriteNodeId(null, item.NodeId);
|
|
encoder.WriteUInt32(null, item.AttributeId);
|
|
encoder.WriteString(null, item.IndexRange);
|
|
encoder.WriteQualifiedName(null, item.Encoding);
|
|
encoder.WriteEnumerated(null, item.DiagnosticsMasks);
|
|
encoder.WriteEnumerated(null, item.TimestampsToReturn);
|
|
encoder.WriteUInt32(null, item.ClientHandle);
|
|
encoder.WriteEnumerated(null, item.MonitoringMode);
|
|
encoder.WriteExtensionObject(null,
|
|
item.OriginalFilter != null
|
|
? new ExtensionObject(item.OriginalFilter)
|
|
: ExtensionObject.Null);
|
|
encoder.WriteExtensionObject(null,
|
|
item.FilterToUse != null
|
|
? new ExtensionObject(item.FilterToUse)
|
|
: ExtensionObject.Null);
|
|
encoder.WriteDouble(null, item.Range);
|
|
encoder.WriteDouble(null, item.SamplingInterval);
|
|
encoder.WriteUInt32(null, item.QueueSize);
|
|
encoder.WriteBoolean(null, item.DiscardOldest);
|
|
encoder.WriteInt32(null, item.SourceSamplingInterval);
|
|
encoder.WriteBoolean(null, item.AlwaysReportUpdates);
|
|
encoder.WriteBoolean(null, item.IsDurable);
|
|
encoder.WriteDataValue(null, item.LastValue);
|
|
encoder.WriteStatusCode(null,
|
|
item.LastError?.StatusCode ?? StatusCodes.Good);
|
|
encoder.WriteString(null, item.ParsedIndexRange.ToString());
|
|
}
|
|
|
|
public static StoredSubscription DecodeSubscription(BinaryDecoder decoder)
|
|
{
|
|
var subscription = new StoredSubscription
|
|
{
|
|
Id = decoder.ReadUInt32(null),
|
|
IsDurable = decoder.ReadBoolean(null),
|
|
LifetimeCounter = decoder.ReadUInt32(null),
|
|
MaxLifetimeCount = decoder.ReadUInt32(null),
|
|
MaxKeepaliveCount = decoder.ReadUInt32(null),
|
|
MaxMessageCount = decoder.ReadUInt32(null),
|
|
MaxNotificationsPerPublish = decoder.ReadUInt32(null),
|
|
PublishingInterval = decoder.ReadDouble(null),
|
|
Priority = decoder.ReadByte(null),
|
|
LastSentMessage = decoder.ReadInt32(null),
|
|
SequenceNumber = decoder.ReadUInt32(null)
|
|
};
|
|
|
|
ExtensionObject tokenEo = decoder.ReadExtensionObject(null);
|
|
if (!tokenEo.IsNull && tokenEo.TryGetValue(out IEncodeable? tokenBody))
|
|
{
|
|
subscription.UserIdentityToken = tokenBody as UserIdentityToken;
|
|
}
|
|
|
|
ArrayOf<ExtensionObject> sentMsgEos =
|
|
decoder.ReadExtensionObjectArray(null);
|
|
var sentList = new List<NotificationMessage>();
|
|
if (!sentMsgEos.IsNull)
|
|
{
|
|
foreach (ExtensionObject eo in sentMsgEos.Memory.ToArray())
|
|
{
|
|
if (!eo.IsNull &&
|
|
eo.TryGetValue(out IEncodeable? e) &&
|
|
e is NotificationMessage nm)
|
|
{
|
|
sentList.Add(nm);
|
|
}
|
|
}
|
|
}
|
|
subscription.SentMessages = sentList;
|
|
|
|
int itemCount = decoder.ReadInt32(null);
|
|
var items = new List<IStoredMonitoredItem>(itemCount);
|
|
for (int i = 0; i < itemCount; i++)
|
|
{
|
|
items.Add(DecodeMonitoredItem(decoder));
|
|
}
|
|
subscription.MonitoredItems = items;
|
|
return subscription;
|
|
}
|
|
|
|
internal static UserIdentityToken? SanitizeUserIdentityToken(
|
|
UserIdentityToken? identityToken)
|
|
{
|
|
return identityToken switch
|
|
{
|
|
null => null,
|
|
AnonymousIdentityToken anonymous => new AnonymousIdentityToken
|
|
{
|
|
PolicyId = anonymous.PolicyId
|
|
},
|
|
UserNameIdentityToken userName => new UserNameIdentityToken
|
|
{
|
|
PolicyId = userName.PolicyId,
|
|
UserName = userName.UserName,
|
|
Password = default,
|
|
EncryptionAlgorithm = null
|
|
},
|
|
X509IdentityToken x509 => new X509IdentityToken
|
|
{
|
|
PolicyId = x509.PolicyId,
|
|
CertificateData = x509.CertificateData
|
|
},
|
|
IssuedIdentityToken => throw new NotSupportedException(
|
|
"Durable subscriptions owned by issued-token identities cannot be " +
|
|
"persisted without storing bearer credentials."),
|
|
_ => throw new NotSupportedException(
|
|
$"User identity token type '{identityToken.GetType().Name}' is not safe " +
|
|
"for durable subscription persistence.")
|
|
};
|
|
}
|
|
|
|
internal static StoredMonitoredItem DecodeMonitoredItem(BinaryDecoder decoder)
|
|
{
|
|
var item = new StoredMonitoredItem
|
|
{
|
|
IsRestored = decoder.ReadBoolean(null),
|
|
SubscriptionId = decoder.ReadUInt32(null),
|
|
Id = decoder.ReadUInt32(null),
|
|
TypeMask = decoder.ReadInt32(null),
|
|
NodeId = decoder.ReadNodeId(null),
|
|
AttributeId = decoder.ReadUInt32(null),
|
|
IndexRange = decoder.ReadString(null)!,
|
|
Encoding = decoder.ReadQualifiedName(null),
|
|
DiagnosticsMasks = decoder.ReadEnumerated<DiagnosticsMasks>(null),
|
|
TimestampsToReturn = decoder.ReadEnumerated<TimestampsToReturn>(null),
|
|
ClientHandle = decoder.ReadUInt32(null),
|
|
MonitoringMode = decoder.ReadEnumerated<MonitoringMode>(null)
|
|
};
|
|
|
|
ExtensionObject origFilterEo = decoder.ReadExtensionObject(null);
|
|
if (!origFilterEo.IsNull &&
|
|
origFilterEo.TryGetValue(out IEncodeable? origBody) &&
|
|
origBody is MonitoringFilter origFilter)
|
|
{
|
|
item.OriginalFilter = origFilter;
|
|
}
|
|
|
|
ExtensionObject filterEo = decoder.ReadExtensionObject(null);
|
|
if (!filterEo.IsNull &&
|
|
filterEo.TryGetValue(out IEncodeable? filterBody) &&
|
|
filterBody is MonitoringFilter filterToUse)
|
|
{
|
|
item.FilterToUse = filterToUse;
|
|
}
|
|
|
|
item.Range = decoder.ReadDouble(null);
|
|
item.SamplingInterval = decoder.ReadDouble(null);
|
|
item.QueueSize = decoder.ReadUInt32(null);
|
|
item.DiscardOldest = decoder.ReadBoolean(null);
|
|
item.SourceSamplingInterval = decoder.ReadInt32(null);
|
|
item.AlwaysReportUpdates = decoder.ReadBoolean(null);
|
|
item.IsDurable = decoder.ReadBoolean(null);
|
|
item.LastValue = decoder.ReadDataValue(null)!;
|
|
|
|
StatusCode lastErrorStatus = decoder.ReadStatusCode(null);
|
|
item.LastError = lastErrorStatus == StatusCodes.Good
|
|
? null! : new ServiceResult(lastErrorStatus);
|
|
|
|
string? rangeStr = decoder.ReadString(null);
|
|
item.ParsedIndexRange = string.IsNullOrEmpty(rangeStr)
|
|
? NumericRange.Null : NumericRange.Parse(rangeStr!);
|
|
|
|
return item;
|
|
}
|
|
}
|
|
|
|
internal static partial class SubscriptionStoreLog
|
|
{
|
|
[LoggerMessage(
|
|
EventId = QuickstartsServersEventIds.SubscriptionStore + 0, Level = LogLevel.Warning,
|
|
Message = "Failed to store subscriptions")]
|
|
public static partial void FailedToStoreSubscriptions(this ILogger logger, Exception exception);
|
|
|
|
[LoggerMessage(
|
|
EventId = QuickstartsServersEventIds.SubscriptionStore + 1, Level = LogLevel.Warning,
|
|
Message = "Failed to restore subscriptions")]
|
|
public static partial void FailedToRestoreSubscriptions(this ILogger logger, Exception exception);
|
|
|
|
[LoggerMessage(
|
|
EventId = QuickstartsServersEventIds.SubscriptionStore + 2, Level = LogLevel.Warning,
|
|
Message = "Failed to cleanup files for stored subscsription")]
|
|
public static partial void FailedToCleanupStoredSubscriptionFiles(this ILogger logger, Exception exception);
|
|
}
|
|
|
|
}
|