Skip to content

Reuse known keys in XmlKeyManager #54490

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

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions src/DataProtection/DataProtection/src/KeyManagement/DeferredKey.cs

This file was deleted.

134 changes: 130 additions & 4 deletions src/DataProtection/DataProtection/src/KeyManagement/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,156 @@

using System;
using System.Collections.Generic;
using System.Xml.Linq;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been more sensible to treat KeyBase as the diff baseline, but I don't think I can alter that. Basically, Key and DeferredKey were just constructors, so I moved those two constructors into the base and made the existing constructor private.

using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
using Microsoft.AspNetCore.DataProtection.XmlEncryption;

namespace Microsoft.AspNetCore.DataProtection.KeyManagement;

/// <summary>
/// The basic implementation of <see cref="IKey"/>, where the <see cref="IAuthenticatedEncryptorDescriptor"/>
/// has already been created.
/// The basic implementation of <see cref="IKey"/>.
/// </summary>
internal sealed class Key : KeyBase
internal sealed class Key : IKey
{
private readonly Lazy<IAuthenticatedEncryptorDescriptor> _lazyDescriptor;
private readonly IEnumerable<IAuthenticatedEncryptorFactory> _encryptorFactories;

private IAuthenticatedEncryptor? _encryptor;

/// <summary>
/// The basic implementation of <see cref="IKey"/>, where the <see cref="IAuthenticatedEncryptorDescriptor"/>
/// has already been created.
/// </summary>
public Key(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be persuaded that the constructors should be private and there should be Eager and Deferred factory methods.

Guid keyId,
DateTimeOffset creationDate,
DateTimeOffset activationDate,
DateTimeOffset expirationDate,
IAuthenticatedEncryptorDescriptor descriptor,
IEnumerable<IAuthenticatedEncryptorFactory> encryptorFactories)
: base(keyId,
: this(keyId,
creationDate,
activationDate,
expirationDate,
new Lazy<IAuthenticatedEncryptorDescriptor>(() => descriptor),
encryptorFactories)
{
}

/// <summary>
/// The basic implementation of <see cref="IKey"/>, where the incoming XML element
/// hasn't yet been fully processed.
/// </summary>
public Key(
Guid keyId,
DateTimeOffset creationDate,
DateTimeOffset activationDate,
DateTimeOffset expirationDate,
IInternalXmlKeyManager keyManager,
XElement keyElement,
IEnumerable<IAuthenticatedEncryptorFactory> encryptorFactories)
: this(keyId,
creationDate,
activationDate,
expirationDate,
new Lazy<IAuthenticatedEncryptorDescriptor>(GetLazyDescriptorDelegate(keyManager, keyElement)),
encryptorFactories)
{
}

private Key(
Guid keyId,
DateTimeOffset creationDate,
DateTimeOffset activationDate,
DateTimeOffset expirationDate,
Lazy<IAuthenticatedEncryptorDescriptor> lazyDescriptor,
IEnumerable<IAuthenticatedEncryptorFactory> encryptorFactories)
{
KeyId = keyId;
CreationDate = creationDate;
ActivationDate = activationDate;
ExpirationDate = expirationDate;
_lazyDescriptor = lazyDescriptor;
_encryptorFactories = encryptorFactories;
}

public DateTimeOffset ActivationDate { get; }

public DateTimeOffset CreationDate { get; }

public DateTimeOffset ExpirationDate { get; }

public bool IsRevoked { get; private set; }

public Guid KeyId { get; }

public IAuthenticatedEncryptorDescriptor Descriptor
{
get
{
return _lazyDescriptor.Value;
}
}

public IAuthenticatedEncryptor? CreateEncryptor()
{
if (_encryptor == null)
{
foreach (var factory in _encryptorFactories)
{
var encryptor = factory.CreateEncryptorInstance(this);
if (encryptor != null)
{
_encryptor = encryptor;
break;
}
}
}

return _encryptor;
}

internal void SetRevoked()
{
IsRevoked = true;
}

internal Key Clone()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new.

{
return new Key(
keyId: KeyId,
creationDate: CreationDate,
activationDate: ActivationDate,
expirationDate: ExpirationDate,
lazyDescriptor: _lazyDescriptor,
encryptorFactories: _encryptorFactories)
{
IsRevoked = IsRevoked,
};
}

private static Func<IAuthenticatedEncryptorDescriptor> GetLazyDescriptorDelegate(IInternalXmlKeyManager keyManager, XElement keyElement)
{
// The <key> element will be held around in memory for a potentially lengthy period
// of time. Since it might contain sensitive information, we should protect it.
var encryptedKeyElement = keyElement.ToSecret();

try
{
return GetLazyDescriptorDelegate;
}
finally
{
// It's important that the lambda above doesn't capture 'descriptorElement'. Clearing the reference here
// helps us detect if we've done this by causing a null ref at runtime.
keyElement = null!;
}

IAuthenticatedEncryptorDescriptor GetLazyDescriptorDelegate()
{
return keyManager.DeserializeDescriptorFromKeyElement(encryptedKeyElement.ToXElement());
}
}
}
77 changes: 0 additions & 77 deletions src/DataProtection/DataProtection/src/KeyManagement/KeyBase.cs

This file was deleted.

Loading