XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFESystem.Tls;

/// <summary>Fill the complete buffer from a verified kernel CSPRNG, or throw.</summary>
public delegate void TlsRandomFill(Span<byte> destination);

/// <summary>
/// Explicit bridge to kernel-owned security services. Neither delegate has a
/// host OS, compile timestamp, deterministic seed, or jitter-only fallback.
/// The platform must verify UTC provenance and RNG health before supplying it.
/// </summary>
public sealed class TlsPlatformSecurityContext : ITlsSecurityContext
{
    private readonly Func<DateTime> readVerifiedUtc;
    private readonly TlsRandomFill fillVerifiedRandom;

    public TlsPlatformSecurityContext(Func<DateTime> readVerifiedUtc, TlsRandomFill fillVerifiedRandom)
    {
        this.readVerifiedUtc = readVerifiedUtc ?? throw new ArgumentNullException(nameof(readVerifiedUtc));
        this.fillVerifiedRandom = fillVerifiedRandom ?? throw new ArgumentNullException(nameof(fillVerifiedRandom));
    }

    public DateTime UtcNow
    {
        get
        {
            DateTime utc = readVerifiedUtc();
            TlsSecurity.ValidateUtc(utc);
            return utc;
        }
    }

    public void FillRandom(Span<byte> destination)
    {
        try { fillVerifiedRandom(destination); }
        catch
        {
            // A source may fail after partially filling the destination.
            // Never return that partial result to the TLS implementation.
            destination.Clear();
            throw;
        }
    }
}