mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-28 22:55:49 +08:00
# Description Introduces a pluggable user-identity provider model covering all OPC UA Part 6 §6.5 mechanisms (Anonymous, UserName, X509, IssuedToken/JWT) on both client and server, with full `Microsoft.Extensions.DependencyInjection` integration, plus the Part 12 §9 `AuthorizationService` two-phase token-request flow **including refresh-token rotation**, the Part 12 §8 KeyCredential Push model, and a clean provider model for OPC 10000-12 §7.2 `ApplicationSelfAdmin`. The work is split across phase-by-phase commits so it can be reviewed independently; nothing here changes wire-level behaviour or forces existing applications to migrate. ### Documentation All under `Docs/`: - `IdentityProviders.md` — full developer guide: three-layer model, recipes, configuration reference, "what gets auto-wired" matrix, JWS algorithm table, identity-augmenters section, **"Implementing your own provider"** how-to subsections for Entra / OIDC / Windows / AspNetCore (sibling files merged in per review feedback). - `DependencyInjection.md` — `Identity` sections with `appsettings.json` examples, identity-extension matrix, `WithAuthorizationService` + `WithKeyCredentialPush` + `AddIdentityAugmenter` extensions documented. - `RoleBasedUserManagement.md` — claim-criteria callout describing `IIdentityClaims` probe surface. - `AuthorizationService.md` — Architecture diagram + `IAccessTokenProvider` conceptual narrative restored from master; modern `StartRequestToken` / `FinishRequestToken` flow leads; refresh-token recipe added. - `KeyCredentialService.md` — Push model + Pull vs Push vs Bridge decision matrix + Hybrid (Push + Pull) section; explicit experimental warning for the bridge. - `GDS.md` — KeyCredential Push, `StartRequestToken`, `GdsApplicationSelfAdminProvider`, **shipped Refresh-token support with rotation policy and audit semantics** all documented. - `MigrationGuide.md` — User Identity Providers section listing `[Obsolete]` markers + mechanical recipes; SelfAdmin migration via `IIdentityAugmenter`; `EnableRefreshTokens` default note. - `README.md` index entries. ## Checklist - [x] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [x] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [x] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [x] I have addressed **all** PR feedback received.
304 lines
12 KiB
C#
304 lines
12 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.Formats.Asn1;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
namespace Opc.Ua.Security.Certificates
|
|
{
|
|
/// <summary>
|
|
/// Supporting functions for X509 extensions.
|
|
/// </summary>
|
|
public static class X509Extensions
|
|
{
|
|
/// <summary>
|
|
/// Find a typed extension in a certificate.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the extension.</typeparam>
|
|
/// <param name="certificate">The certificate with extensions.</param>
|
|
public static T? FindExtension<T>(this Certificate certificate)
|
|
where T : X509Extension
|
|
{
|
|
return FindExtension<T>(certificate.Extensions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Find a typed extension in a extension collection.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the extension.</typeparam>
|
|
/// <param name="extensions">The extensions to search.</param>
|
|
/// <exception cref="ArgumentNullException"><paramref name="extensions"/> is <c>null</c>.</exception>
|
|
public static T? FindExtension<T>(this X509ExtensionCollection extensions)
|
|
where T : X509Extension
|
|
{
|
|
if (extensions == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(extensions));
|
|
}
|
|
|
|
lock (extensions.SyncRoot)
|
|
{
|
|
// search known custom extensions
|
|
if (typeof(T) == typeof(X509AuthorityKeyIdentifierExtension))
|
|
{
|
|
X509Extension? extension = extensions
|
|
.Cast<X509Extension>()
|
|
.FirstOrDefault(e =>
|
|
e.Oid?.Value
|
|
is X509AuthorityKeyIdentifierExtension.AuthorityKeyIdentifierOid
|
|
or X509AuthorityKeyIdentifierExtension
|
|
.AuthorityKeyIdentifier2Oid);
|
|
if (extension != null)
|
|
{
|
|
return new X509AuthorityKeyIdentifierExtension(
|
|
extension,
|
|
extension.Critical) as T;
|
|
}
|
|
}
|
|
|
|
if (typeof(T) == typeof(X509SubjectAltNameExtension))
|
|
{
|
|
X509Extension? extension = extensions
|
|
.Cast<X509Extension>()
|
|
.FirstOrDefault(e =>
|
|
e.Oid?.Value
|
|
is X509SubjectAltNameExtension.SubjectAltNameOid
|
|
or X509SubjectAltNameExtension.SubjectAltName2Oid);
|
|
if (extension != null)
|
|
{
|
|
return new X509SubjectAltNameExtension(extension, extension.Critical) as T;
|
|
}
|
|
}
|
|
|
|
if (typeof(T) == typeof(X509CrlNumberExtension))
|
|
{
|
|
X509Extension? extension = extensions
|
|
.Cast<X509Extension>()
|
|
.FirstOrDefault(e => e.Oid?.Value == X509CrlNumberExtension.CrlNumberOid);
|
|
if (extension != null)
|
|
{
|
|
return new X509CrlNumberExtension(extension, extension.Critical) as T;
|
|
}
|
|
}
|
|
|
|
// search builtin extension
|
|
return extensions.OfType<T>().FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the Authority information Access extension.
|
|
/// </summary>
|
|
/// <param name="caIssuerUrls">Array of CA Issuer Urls</param>
|
|
/// <param name="ocspResponder">optional, the OCSP responder </param>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static X509Extension BuildX509AuthorityInformationAccess(
|
|
this string[] caIssuerUrls,
|
|
string? ocspResponder = null)
|
|
{
|
|
if (string.IsNullOrEmpty(ocspResponder) &&
|
|
(caIssuerUrls == null || caIssuerUrls.Length == 0))
|
|
{
|
|
throw new ArgumentNullException(
|
|
nameof(caIssuerUrls),
|
|
"One CA Issuer Url or OCSP responder is required for the extension.");
|
|
}
|
|
|
|
var generalNameUriChoice = new Asn1Tag(TagClass.ContextSpecific, 6);
|
|
var writer = new AsnWriter(AsnEncodingRules.DER);
|
|
writer.PushSequence();
|
|
if (caIssuerUrls != null)
|
|
{
|
|
foreach (string caIssuerUrl in caIssuerUrls)
|
|
{
|
|
writer.PushSequence();
|
|
writer.WriteObjectIdentifier(Oids.CertificateAuthorityIssuers);
|
|
writer.WriteCharacterString(
|
|
UniversalTagNumber.IA5String,
|
|
caIssuerUrl,
|
|
generalNameUriChoice);
|
|
writer.PopSequence();
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(ocspResponder))
|
|
{
|
|
writer.PushSequence();
|
|
writer.WriteObjectIdentifier(Oids.OnlineCertificateStatusProtocol);
|
|
writer.WriteCharacterString(
|
|
UniversalTagNumber.IA5String,
|
|
ocspResponder!,
|
|
generalNameUriChoice);
|
|
writer.PopSequence();
|
|
}
|
|
writer.PopSequence();
|
|
return new X509Extension(Oids.AuthorityInfoAccess, writer.Encode(), false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the CRL Distribution Point extension.
|
|
/// </summary>
|
|
/// <param name="distributionPoint">The CRL distribution point</param>
|
|
public static X509Extension BuildX509CRLDistributionPoints(this string distributionPoint)
|
|
{
|
|
return BuildX509CRLDistributionPoints([distributionPoint]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the CRL Distribution Point extension with multiple distribution points.
|
|
/// </summary>
|
|
/// <param name="distributionPoints">The CRL distribution points</param>
|
|
public static X509Extension BuildX509CRLDistributionPoints(
|
|
this IEnumerable<string> distributionPoints)
|
|
{
|
|
var context0 = new Asn1Tag(TagClass.ContextSpecific, 0, true);
|
|
Asn1Tag distributionPointChoice = context0;
|
|
Asn1Tag fullNameChoice = context0;
|
|
var generalNameUriChoice = new Asn1Tag(TagClass.ContextSpecific, 6);
|
|
var writer = new AsnWriter(AsnEncodingRules.DER);
|
|
writer.PushSequence();
|
|
writer.PushSequence();
|
|
writer.PushSequence(distributionPointChoice);
|
|
writer.PushSequence(fullNameChoice);
|
|
foreach (string distributionPoint in distributionPoints)
|
|
{
|
|
writer.WriteCharacterString(
|
|
UniversalTagNumber.IA5String,
|
|
distributionPoint,
|
|
generalNameUriChoice);
|
|
}
|
|
writer.PopSequence(fullNameChoice);
|
|
writer.PopSequence(distributionPointChoice);
|
|
writer.PopSequence();
|
|
writer.PopSequence();
|
|
return new X509Extension(Oids.CRLDistributionPoint, writer.Encode(), false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read an ASN.1 extension sequence as X509Extension object.
|
|
/// </summary>
|
|
/// <param name="reader">The ASN reader.</param>
|
|
public static X509Extension? ReadExtension(this AsnReader reader)
|
|
{
|
|
if (reader.HasData)
|
|
{
|
|
var boolTag = new Asn1Tag(UniversalTagNumber.Boolean);
|
|
AsnReader extReader = reader.ReadSequence();
|
|
string extOid = extReader.ReadObjectIdentifier();
|
|
bool critical = false;
|
|
Asn1Tag peekTag = extReader.PeekTag();
|
|
if (peekTag == boolTag)
|
|
{
|
|
critical = extReader.ReadBoolean();
|
|
}
|
|
byte[] data = extReader.ReadOctetString();
|
|
extReader.ThrowIfNotEmpty();
|
|
return new X509Extension(new Oid(extOid), data, critical);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write an extension object as ASN.1.
|
|
/// </summary>
|
|
/// <exception cref="CryptographicException"></exception>
|
|
public static void WriteExtension(this AsnWriter writer, X509Extension extension)
|
|
{
|
|
Asn1Tag etag = Asn1Tag.Sequence;
|
|
writer.PushSequence(etag);
|
|
writer.WriteObjectIdentifier(extension.Oid?.Value
|
|
?? throw new CryptographicException("Extension OID value is null."));
|
|
if (extension.Critical)
|
|
{
|
|
writer.WriteBoolean(extension.Critical);
|
|
}
|
|
writer.WriteOctetString(extension.RawData);
|
|
writer.PopSequence(etag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the CRL Reason extension.
|
|
/// </summary>
|
|
public static X509Extension BuildX509CRLReason(this CRLReason reason)
|
|
{
|
|
var writer = new AsnWriter(AsnEncodingRules.DER);
|
|
writer.WriteEnumeratedValue(reason);
|
|
return new X509Extension(Oids.CrlReasonCode, writer.Encode(), false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the Authority Key Identifier from an Issuer CA certificate.
|
|
/// </summary>
|
|
/// <param name="issuerCaCertificate">The issuer CA certificate</param>
|
|
public static X509Extension BuildAuthorityKeyIdentifier(
|
|
this Certificate issuerCaCertificate)
|
|
{
|
|
// force exception if SKI is not present
|
|
X509SubjectKeyIdentifierExtension ski = issuerCaCertificate
|
|
.Extensions
|
|
.OfType<X509SubjectKeyIdentifierExtension>()
|
|
.Single();
|
|
return new X509AuthorityKeyIdentifierExtension(
|
|
ski.SubjectKeyIdentifier.FromHexString() ?? [],
|
|
issuerCaCertificate.IssuerName,
|
|
issuerCaCertificate.GetSerialNumber());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Build the CRL number.
|
|
/// </summary>
|
|
public static X509Extension BuildCRLNumber(this BigInteger crlNumber)
|
|
{
|
|
var writer = new AsnWriter(AsnEncodingRules.DER);
|
|
writer.WriteInteger(crlNumber);
|
|
return new X509Extension(Oids.CrlNumber, writer.Encode(), false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Patch serial number in a Url. byte version.
|
|
/// </summary>
|
|
public static string PatchExtensionUrl(this string extensionUrl, byte[] serialNumber)
|
|
{
|
|
return PatchExtensionUrl(extensionUrl, serialNumber.ToHexString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Patch serial number in a Url. string version.
|
|
/// </summary>
|
|
public static string PatchExtensionUrl(this string extensionUrl, string serial)
|
|
{
|
|
return extensionUrl.Replace("%serial%", serial.ToLowerInvariant(), StringComparison.Ordinal);
|
|
}
|
|
}
|
|
}
|