/// <summary> /// Builds the PFX with specified friendly name. /// </summary> /// <param name="friendlyName">The friendly name.</param> /// <param name="password">The password.</param> /// <returns>The PFX data.</returns> public byte[] Build(string friendlyName, string password) { var keyPair = privateKeyInfo.CreateKeyPair(); var store = new Pkcs12StoreBuilder().Build(); var entry = new X509CertificateEntry(certificate); store.SetCertificateEntry(friendlyName, entry); if (FullChain) { var certChain = FindIssuers(); var certChainEntries = certChain.Select(c => new X509CertificateEntry(c)).ToList(); certChainEntries.Add(entry); store.SetKeyEntry(friendlyName, new AsymmetricKeyEntry(keyPair.Private), certChainEntries.ToArray()); } else { store.SetKeyEntry(friendlyName, new AsymmetricKeyEntry(keyPair.Private), new[] { entry }); } using (var buffer = new MemoryStream()) { store.Save(buffer, password.ToCharArray(), new SecureRandom()); return(buffer.ToArray()); } }
/// <summary> /// Saves the key pair to the specified stream. /// </summary> /// <param name="keyInfo">The key information.</param> /// <param name="stream">The stream.</param> public static void Save(this KeyInfo keyInfo, Stream stream) { var keyPair = keyInfo.CreateKeyPair(); using (var writer = new StreamWriter(stream)) { var pemWriter = new PemWriter(writer); pemWriter.WriteObject(keyPair); } }
public void CanReloadKeyPair() { var keyInfo = new KeyInfo { PrivateKeyInfo = Convert.FromBase64String(Helper.PrivateKey) }; var keyPair = keyInfo.CreateKeyPair(); var exported = keyPair.Export(); Assert.Equal(Helper.PrivateKey, Convert.ToBase64String(exported.PrivateKeyInfo)); }
/// <summary> /// Initializes a new instance of the <see cref="CertificationRequestBuilder"/> class. /// </summary> /// <param name="keyInfo">The key information.</param> /// <exception cref="System.NotSupportedException"> /// If the provided key is not one of the supported <seealso cref="SignatureAlgorithm"/>. /// </exception> public CertificationRequestBuilder(KeyInfo keyInfo) { this.keyInfo = keyInfo; this.KeyPair = keyInfo.CreateKeyPair(); if (this.KeyPair.Private is RsaPrivateCrtKeyParameters) { this.Algorithm = SignatureAlgorithm.Sha256WithRsaEncryption; } else { throw new NotSupportedException(); } }