TrustStoreExtensions & PqTrustStore

Gestione Trust Store personalizzato per infrastrutture PKI compatibili con PQ.


Panoramica

Classe Descrizione
PqTrustStore Trust Store completo con supporto PQ
TrustStoreExtensions Metodi di estensione per X509Certificate2Collection
TrustEntry Metadati per voci del Trust Store
TrustType Enum: TrustAnchor, IntermediateCA, ecc.

Classe PqTrustStore

using WvdS.System.Security.Cryptography.X509Certificates;
 
// Creare il Trust Store
using var trustStore = new PqTrustStore();
 
// Aggiungere la Root CA
var rootCa = new X509Certificate2("root-ca.cer");
trustStore.AddTrustAnchor(rootCa, alias: "Company Root CA");
 
// Aggiungere la CA intermedia
var intermediateCa = new X509Certificate2("intermediate-ca.cer");
trustStore.AddIntermediate(intermediateCa, alias: "Issuing CA");
 
// Utilizzare il Trust Store
Console.WriteLine($"Il Trust Store contiene {trustStore.Count} voci");

Integrazione con X509Chain

Validazione con Trust Store personalizzato:

using System.Security.Cryptography.X509Certificates;
using WvdS.System.Security.Cryptography;
using WvdS.System.Security.Cryptography.X509Certificates;
 
// Caricare il Trust Store
using var trustStore = PqTrustStore.LoadFromPem("company-trust-store.pem");
 
// Validare il certificato
var userCert = new X509Certificate2("user.cer");
 
using var chain = new X509Chain();
bool isValid = chain.Build(userCert, trustStore, CryptoMode.Hybrid);
 
if (isValid)
{
    Console.WriteLine("Il certificato è valido");
}
else
{
    foreach (var status in chain.ChainStatus)
    {
        Console.WriteLine($"Errore: {status.StatusInformation}");
    }
}

Con X509Certificate2Collection:

// Più semplice: direttamente con Collection
var trustAnchors = TrustStoreExtensions.LoadFromPem("trust-anchors.pem");
 
using var chain = new X509Chain();
bool isValid = chain.Build(userCert, trustAnchors, CryptoMode.Hybrid);

Import/Export Trust Store

Bundle PEM:

// Salvare
trustStore.SaveToPem("trust-store.pem");
 
// Caricare
var loadedStore = PqTrustStore.LoadFromPem("trust-store.pem");

PKCS#7 (DER):

// Salvare come PKCS#7
trustStore.SaveToPkcs7("trust-store.p7b");
 
// Caricare
var loadedStore = PqTrustStore.LoadFromPkcs7("trust-store.p7b");

Import da System Store

// Popolare il Trust Store con certificati di sistema
using var trustStore = new PqTrustStore();
 
// Importare le Root CA di Windows
trustStore.ImportFromSystemRootStore();
 
// Importare le CA intermedie di Windows
trustStore.ImportFromSystemIntermediateStore();
 
// Esportare come PEM per sistemi air-gapped
trustStore.SaveToPem("system-trust-store.pem");

Enum TrustType

Valore Descrizione
TrustAnchor Ancora di fiducia (Root CA)
IntermediateCA Autorità di certificazione intermedia
CrossCertifiedCA CA con certificazione incrociata
EndEntity Entità finale (non CA)

Query Trust Store

// Solo Trust Anchor
var roots = trustStore.TrustAnchors;
 
// Solo CA intermedie
var intermediates = trustStore.IntermediateCAs;
 
// Ricerca per Subject
var found = trustStore.FindBySubjectName("Company");
 
// Ricerca per Thumbprint
var entry = trustStore.FindByThumbprint("A1B2C3...");
 
// Verificare se contenuto
bool exists = trustStore.Contains(certificate);

Report di validazione

using var chain = new X509Chain();
chain.Build(cert, trustStore, CryptoMode.Hybrid);
 
// Creare un report dettagliato
var report = chain.GetValidationReport();
 
Console.WriteLine($"Valido: {report.IsValid}");
Console.WriteLine($"Lunghezza catena: {report.ChainLength}");
Console.WriteLine($"Completamente protetto PQ: {report.IsFullyPqProtected}");
 
// Singoli elementi
foreach (var element in report.Elements)
{
    Console.WriteLine($"  {element.Subject}");
    Console.WriteLine($"    Modalità: {element.CryptoMode}");
    Console.WriteLine($"    Chiavi PQ: {element.HasPqKeys}");
}

Output di esempio:

Chain Validation: VALID
Chain Length: 3
Fully PQ Protected: True
Elements:
  [0] CN=User Certificate
      Mode: Hybrid, PQ Keys: True
  [1] CN=Issuing CA
      Mode: Hybrid, PQ Keys: True
  [2] CN=Root CA
      Mode: Hybrid, PQ Keys: True

Verifica CRL offline

Per sistemi air-gapped con CRL precaricate:

// Caricare le CRL
var crls = new List<byte[]>
{
    File.ReadAllBytes("root-ca.crl"),
    File.ReadAllBytes("issuing-ca.crl")
};
 
using var chain = new X509Chain();
bool isValid = chain.BuildWithOfflineCrl(
    certificate,
    trustStore.ToCollection(),
    crls,
    CryptoMode.Hybrid);

Estensioni Collection

// Filtrare la collection
var collection = new X509Certificate2Collection();
collection.Import("certificates.p7b");
 
// Solo CA
var cas = collection.FilterCertificateAuthorities();
 
// Solo Root CA (autofirmate)
var roots = collection.FilterRootCertificates();
 
// Ricerca per Subject
var found = collection.FindBySubjectName("Company");
 
// Verificare lo stato PQ
bool hasPq = collection.HasPqProtectedCertificates();
 
// Salvare come PEM
collection.SaveToPem("output.pem");

Best Practice

Gerarchia Trust Store:

PqTrustStore
├── TrustAnchors (Root CA)
│   └── → chain.ChainPolicy.CustomTrustStore
└── IntermediateCAs
    └── → chain.ChainPolicy.ExtraStore

Raccomandazioni:

  • Principio del minimo: Includere solo le CA necessarie
  • Migrazione PQ: Aggiungere gradualmente chiavi PQ alle CA esistenti
  • Backup: Eseguire backup regolari del Trust Store
  • Validazione: Verificare le nuove CA prima dell'inclusione

Vedere anche


Wolfgang van der Stille @ EMSR DATA d.o.o. - Post-Quantum Cryptography Professional

Zuletzt geändert: il 30/01/2026 alle 06:26