[API Proposal]: X509Certificate.ExportPkcs12 with PBE parameters #80314
Description
Background and motivation
Right now Export(X509ContentType.Pkcs12, ...)
assumes 3DES, SHA1, and 2000 rounds of PBKDF. These are not great in 2023 and there is no control over it today. Developers could use Pkcs12Builder
themselves, but, that's a fairly low-level mechanism that requires understanding how PKCS12 works.
I propose we expose new APIs that allow stronger PKCS12 exports.
In the proposal, there are two overloads. An enum that provides simple options that match Windows behavior. The second is PbeParameters
where people can have total control over the encryption, hash, and number ob PBES2 rounds.
Win32 by default does not give us the flexibility that is needed to implement PbeParameters
, as it does not let us choose the number of PBES2 rounds. In that case, the export will be performed using an existing Win32 API, decoded, and re-encoded with the managed implementation using the specified parameters.
API Proposal
namespace System.Security.Cryptography.X509Certificates;
public partial class X509Certificate {
public byte[] ExportPkcs12(Pkcs12ExportPbeParameters exportParameters, string? password);
public byte[] ExportPkcs12(PbeParameters exportParameters, string? password);
}
public partial class X509Certificate2Collection {
public byte[] ExportPkcs12(Pkcs12ExportPbeParameters exportParameters, string? password);
public byte[] ExportPkcs12(PbeParameters exportParameters, string? password);
}
public enum Pkcs12ExportPbeParameters {
// Initially will behave as `Pbes2TripleDesSha1`, but reserved to change behavior in the future.
Default = 0,
// TripleDes3KeyPkcs12, SHA1, 2000 PBKDF2 rounds. Matches Win32.
Pbes2TripleDesSha1 = 1,
// AES-256, SHA256, 2000 PBKDF2 rounds. Matches Win32.
Pbes2Aes256Sha256 = 2,
}
API Usage
X509Certificate certificate = GetCert();
byte[] pkcs12 = certificate.ExportPkcs12(Pkcs12ExportPbeParameters.Pbes2Aes256Sha256, "potato");