-
Notifications
You must be signed in to change notification settings - Fork 1k
[Server] Handle ApplicationCertificate updates using events: close channels on update and block new Connections / Requests #3430
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
base: master
Are you sure you want to change the base?
Changes from all commits
cea6283
a903bd8
b8f4eee
41d8b19
50ced6d
7a1c91e
a4861ed
eb69eef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,6 +97,15 @@ public event CertificateUpdateEventHandler CertificateUpdate | |
| remove => m_CertificateUpdate -= value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Raised before an application certificate update occurs. | ||
| /// </summary> | ||
| public event CertificateUpdateEventHandler CertificateUpdateStarted | ||
| { | ||
| add => m_CertificateUpdateStarted += value; | ||
| remove => m_CertificateUpdateStarted -= value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Updates the validator with the current state of the configuration. | ||
| /// </summary> | ||
|
|
@@ -284,51 +293,69 @@ public virtual async Task UpdateCertificateAsync( | |
| string applicationUri = null, | ||
| CancellationToken ct = default) | ||
| { | ||
| await m_semaphore.WaitAsync(ct).ConfigureAwait(false); | ||
| m_updateEvent.Reset(); | ||
|
|
||
| try | ||
| { | ||
| m_applicationCertificates.Clear(); | ||
| // | ||
| // crash occurs if the cert is in use still and this has not run yet. | ||
| // This might be the intended design but this runs on a free task that | ||
| // might not be scheduled right away. | ||
| // | ||
| // TODO: We need a better way to disconnect all sessions when the cert is | ||
| // updated. (See caller of this method) | ||
| // | ||
| // foreach (CertificateIdentifier applicationCertificate in securityConfiguration | ||
| // .ApplicationCertificates) | ||
| // { | ||
| // applicationCertificate.DisposeCertificate(); | ||
| // } | ||
|
|
||
| foreach (CertificateIdentifier applicationCertificate in securityConfiguration | ||
| .ApplicationCertificates) | ||
| { | ||
| await applicationCertificate | ||
| .LoadPrivateKeyExAsync( | ||
| securityConfiguration.CertificatePasswordProvider, | ||
| applicationUri, | ||
| m_telemetry, | ||
| ct) | ||
| .ConfigureAwait(false); | ||
| CertificateUpdateEventHandler started_callback = m_CertificateUpdateStarted; | ||
| if (started_callback != null) | ||
| { | ||
| var args = new CertificateUpdateEventArgs( | ||
| securityConfiguration, | ||
| GetChannelValidator()); | ||
| await started_callback(this, args).ConfigureAwait(false); | ||
| } | ||
|
|
||
| await m_semaphore.WaitAsync(ct).ConfigureAwait(false); | ||
|
|
||
| try | ||
| { | ||
| m_applicationCertificates.Clear(); | ||
| // | ||
| // crash occurs if the cert is in use still and this has not run yet. | ||
| // This might be the intended design but this runs on a free task that | ||
| // might not be scheduled right away. | ||
| // | ||
| // TODO: We need a better way to disconnect all sessions when the cert is | ||
| // updated. (See caller of this method) | ||
| // | ||
| // foreach (CertificateIdentifier applicationCertificate in securityConfiguration | ||
| // .ApplicationCertificates) | ||
| // { | ||
| // applicationCertificate.DisposeCertificate(); | ||
| // } | ||
|
|
||
| foreach (CertificateIdentifier applicationCertificate in securityConfiguration | ||
| .ApplicationCertificates) | ||
| { | ||
| await applicationCertificate | ||
| .LoadPrivateKeyExAsync( | ||
| securityConfiguration.CertificatePasswordProvider, | ||
| applicationUri, | ||
| m_telemetry, | ||
| ct) | ||
| .ConfigureAwait(false); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| m_semaphore.Release(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| m_semaphore.Release(); | ||
| } | ||
|
|
||
| await UpdateAsync(securityConfiguration, applicationUri, ct).ConfigureAwait(false); | ||
| await UpdateAsync(securityConfiguration, applicationUri, ct).ConfigureAwait(false); | ||
|
|
||
| CertificateUpdateEventHandler callback = m_CertificateUpdate; | ||
| if (callback != null) | ||
| CertificateUpdateEventHandler callback = m_CertificateUpdate; | ||
| if (callback != null) | ||
| { | ||
| var args = new CertificateUpdateEventArgs( | ||
| securityConfiguration, | ||
| GetChannelValidator()); | ||
| await callback(this, args).ConfigureAwait(false); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| var args = new CertificateUpdateEventArgs( | ||
| securityConfiguration, | ||
| GetChannelValidator()); | ||
| callback(this, args); | ||
| m_updateEvent.Set(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2234,6 +2261,9 @@ public static bool IsECSecureForProfile( | |
| throw new NotSupportedException("Unsupported curve type."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public WaitHandle CertificateUpdateInProgress => m_updateEvent.WaitHandle; | ||
|
|
||
| /// <summary> | ||
| /// Flag to protect setting by application | ||
| /// from a modification by a SecurityConfiguration. | ||
|
|
@@ -2254,13 +2284,15 @@ private enum ProtectFlags | |
| private readonly ILogger m_logger; | ||
| private readonly ITelemetryContext m_telemetry; | ||
| private readonly Dictionary<string, X509Certificate2> m_validatedCertificates; | ||
| private readonly ManualResetEventSlim m_updateEvent = new(true); | ||
|
||
| private CertificateStoreIdentifier m_trustedCertificateStore; | ||
| private CertificateIdentifierCollection m_trustedCertificateList; | ||
| private CertificateStoreIdentifier m_issuerCertificateStore; | ||
| private CertificateIdentifierCollection m_issuerCertificateList; | ||
| private CertificateStoreIdentifier m_rejectedCertificateStore; | ||
| private event CertificateValidationEventHandler m_CertificateValidation; | ||
| private event CertificateUpdateEventHandler m_CertificateUpdate; | ||
| private event CertificateUpdateEventHandler m_CertificateUpdateStarted; | ||
| private readonly List<X509Certificate2> m_applicationCertificates; | ||
| private ProtectFlags m_protectFlags; | ||
| private bool m_autoAcceptUntrustedCertificates; | ||
|
|
@@ -2350,7 +2382,7 @@ public CertificateUpdateEventArgs( | |
| /// <summary> | ||
| /// Used to handle certificate update events. | ||
| /// </summary> | ||
| public delegate void CertificateUpdateEventHandler( | ||
| public delegate Task CertificateUpdateEventHandler( | ||
| CertificateValidator sender, | ||
| CertificateUpdateEventArgs e); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.