// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption; using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel; using Microsoft.AspNetCore.DataProtection.Repositories; using Microsoft.AspNetCore.DataProtection.XmlEncryption; namespace Microsoft.AspNetCore.DataProtection.KeyManagement { /// /// Options that control how an should behave. /// public class KeyManagementOptions { private static readonly TimeSpan _keyPropagationWindow = TimeSpan.FromDays(2); private static readonly TimeSpan _keyRingRefreshPeriod = TimeSpan.FromHours(24); private static readonly TimeSpan _maxServerClockSkew = TimeSpan.FromMinutes(5); private TimeSpan _newKeyLifetime = TimeSpan.FromDays(90); public KeyManagementOptions() { } // copy ctor internal KeyManagementOptions(KeyManagementOptions other) { if (other != null) { AutoGenerateKeys = other.AutoGenerateKeys; _newKeyLifetime = other._newKeyLifetime; XmlEncryptor = other.XmlEncryptor; XmlRepository = other.XmlRepository; AuthenticatedEncryptorConfiguration = other.AuthenticatedEncryptorConfiguration; foreach (var keyEscrowSink in other.KeyEscrowSinks) { KeyEscrowSinks.Add(keyEscrowSink); } foreach (var encryptorFactory in other.AuthenticatedEncryptorFactories) { AuthenticatedEncryptorFactories.Add(encryptorFactory); } } } /// /// Specifies whether the data protection system should auto-generate keys. /// /// /// If this value is 'false', the system will not generate new keys automatically. /// The key ring must contain at least one active non-revoked key, otherwise calls /// to may fail. The system may end up /// protecting payloads to expired keys if this property is set to 'false'. /// The default value is 'true'. /// public bool AutoGenerateKeys { get; set; } = true; /// /// Specifies the period before key expiration in which a new key should be generated /// so that it has time to propagate fully throughout the key ring. For example, if this /// period is 72 hours, then a new key will be created and persisted to storage /// approximately 72 hours before expiration. /// /// /// This value is currently fixed at 48 hours. /// internal TimeSpan KeyPropagationWindow { get { // This value is not settable since there's a complex interaction between // it and the key ring refresh period. return _keyPropagationWindow; } } /// /// Controls the auto-refresh period where the key ring provider will /// flush its collection of cached keys and reread the collection from /// backing storage. /// /// /// This value is currently fixed at 24 hours. /// internal TimeSpan KeyRingRefreshPeriod { get { // This value is not settable since there's a complex interaction between // it and the key expiration safety period. return _keyRingRefreshPeriod; } } /// /// Specifies the maximum clock skew allowed between servers when reading /// keys from the key ring. The key ring may use a key which has not yet /// been activated or which has expired if the key's valid lifetime is within /// the allowed clock skew window. This value can be set to /// if key activation and expiration times should be strictly honored by this server. /// /// /// This value is currently fixed at 5 minutes. /// internal TimeSpan MaxServerClockSkew { get { return _maxServerClockSkew; } } /// /// Controls the lifetime (number of days before expiration) /// for newly-generated keys. /// /// /// The lifetime cannot be less than one week. /// The default value is 90 days. /// public TimeSpan NewKeyLifetime { get { return _newKeyLifetime; } set { if (value < TimeSpan.FromDays(7)) { throw new ArgumentOutOfRangeException(nameof(value), Resources.KeyManagementOptions_MinNewKeyLifetimeViolated); } _newKeyLifetime = value; } } /// /// The instance that can be used to create /// the instance. /// public AlgorithmConfiguration AuthenticatedEncryptorConfiguration { get; set; } /// /// The list of to store the key material in. /// public IList KeyEscrowSinks { get; } = new List(); /// /// The to use for storing and retrieving XML elements. /// public IXmlRepository XmlRepository { get; set; } /// /// The to use for encrypting XML elements. /// public IXmlEncryptor XmlEncryptor { get; set; } /// /// The list of that will be used for creating /// s. /// public IList AuthenticatedEncryptorFactories { get; } = new List(); } }