Table of Contents

Class InMemoryMasterKeyBackupService

Namespace
Excalibur.Compliance.Encryption
Assembly
Excalibur.Compliance.dll

In-memory implementation of IMasterKeyBackupService for testing and development.

public sealed class InMemoryMasterKeyBackupService : IMasterKeyBackupService, IMasterKeyBackupExporter, IMasterKeyRestoreService
Inheritance
InMemoryMasterKeyBackupService
Implements
Inherited Members

Remarks

WARNING: This implementation stores data in memory only. All backups are lost when the application restarts. Use only for testing and development purposes.

For production, use a persistent implementation backed by secure storage such as an HSM, cloud KMS, or encrypted database.

Constructors

InMemoryMasterKeyBackupService(IKeyManagementProvider, IEncryptionProvider, ILogger<InMemoryMasterKeyBackupService>)

Initializes a new instance of the InMemoryMasterKeyBackupService class.

public InMemoryMasterKeyBackupService(IKeyManagementProvider keyManagementProvider, IEncryptionProvider encryptionProvider, ILogger<InMemoryMasterKeyBackupService> logger)

Parameters

keyManagementProvider IKeyManagementProvider
encryptionProvider IEncryptionProvider
logger ILogger<InMemoryMasterKeyBackupService>

Methods

ExportMasterKeyAsync(string, MasterKeyExportOptions?, CancellationToken)

Exports a master key for backup purposes.

public Task<MasterKeyBackup> ExportMasterKeyAsync(string keyId, MasterKeyExportOptions? options, CancellationToken cancellationToken)

Parameters

keyId string

The unique identifier of the master key to export.

options MasterKeyExportOptions

Options controlling the export operation.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<MasterKeyBackup>

The backup containing the encrypted key material.

Exceptions

ArgumentException

Thrown when keyId is null or empty.

KeyNotFoundException

Thrown when the specified key does not exist.

InvalidOperationException

Thrown when the key cannot be exported (e.g., non-exportable HSM key).

GenerateRecoverySplitAsync(string, int, int, BackupShareOptions?, CancellationToken)

Generates recovery shares for a master key using Shamir's Secret Sharing.

public Task<BackupShare[]> GenerateRecoverySplitAsync(string keyId, int totalShares, int threshold, BackupShareOptions? options, CancellationToken cancellationToken)

Parameters

keyId string

The unique identifier of the master key to split.

totalShares int

Total number of shares to generate (custodians).

threshold int

Minimum number of shares required for reconstruction.

options BackupShareOptions

Options controlling the share generation.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<BackupShare[]>

An array of backup shares, one for each custodian.

Remarks

The default configuration is 3-of-5, meaning any 3 custodians can reconstruct the key. Each share should be securely distributed to a different custodian.

Split-knowledge key recovery ensures no single person can access the master key.

Exceptions

ArgumentException

Thrown when keyId is null or empty.

ArgumentOutOfRangeException

Thrown when threshold < 2, totalShares < 2, or threshold > totalShares.

KeyNotFoundException

Thrown when the specified key does not exist.

GetBackupStatusAsync(string, CancellationToken)

Gets the status of existing backups for a master key.

public Task<MasterKeyBackupStatus?> GetBackupStatusAsync(string keyId, CancellationToken cancellationToken)

Parameters

keyId string

The unique identifier of the master key.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<MasterKeyBackupStatus>

The backup status including when backups were created and their validity.

ImportMasterKeyAsync(MasterKeyBackup, MasterKeyImportOptions?, CancellationToken)

Imports a previously exported master key backup.

public Task<MasterKeyImportResult> ImportMasterKeyAsync(MasterKeyBackup backup, MasterKeyImportOptions? options, CancellationToken cancellationToken)

Parameters

backup MasterKeyBackup

The backup data to import.

options MasterKeyImportOptions

Options controlling the import operation.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<MasterKeyImportResult>

The result of the import operation including the restored key metadata.

Exceptions

ArgumentNullException

Thrown when backup is null.

InvalidOperationException

Thrown when the backup is corrupted or cannot be decrypted.

MasterKeyBackupException

Thrown when the import fails due to validation errors.

ReconstructFromSharesAsync(BackupShare[], MasterKeyImportOptions?, CancellationToken)

Reconstructs a master key from the provided shares.

public Task<MasterKeyImportResult> ReconstructFromSharesAsync(BackupShare[] shares, MasterKeyImportOptions? options, CancellationToken cancellationToken)

Parameters

shares BackupShare[]

The shares to combine (must meet the threshold).

options MasterKeyImportOptions

Options controlling the reconstruction.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<MasterKeyImportResult>

The result of the reconstruction including the restored key metadata.

Exceptions

ArgumentException

Thrown when shares is null or empty.

MasterKeyBackupException

Thrown when insufficient shares are provided or reconstruction fails.

RegisterKeyMaterial(string, byte[])

Registers simulated key material for testing purposes.

public void RegisterKeyMaterial(string keyId, byte[] keyMaterial)

Parameters

keyId string

The key identifier.

keyMaterial byte[]

The key material bytes.

Remarks

This method is for testing only to allow injection of known key material.

VerifyBackupAsync(MasterKeyBackup, CancellationToken)

Verifies that a backup can be successfully restored without actually importing it.

public Task<BackupVerificationResult> VerifyBackupAsync(MasterKeyBackup backup, CancellationToken cancellationToken)

Parameters

backup MasterKeyBackup

The backup to verify.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<BackupVerificationResult>

The verification result including any warnings or issues found.