public static void SavePem(this X509Certificate2 @this, string certFile, string privateKeyFile = null) { try { Directory.CreateDirectory(Path.GetDirectoryName(certFile)); if (!string.IsNullOrEmpty(privateKeyFile) && @this.HasPrivateKey) { Directory.CreateDirectory(Path.GetDirectoryName(privateKeyFile)); var p = @this.GetRSAPrivateKey().ExportParameters(true); var key = new RsaPrivateCrtKeyParameters( new Org.BouncyCastle.Math.BigInteger(1, p.Modulus), new Org.BouncyCastle.Math.BigInteger(1, p.Exponent), new Org.BouncyCastle.Math.BigInteger(1, p.D), new Org.BouncyCastle.Math.BigInteger(1, p.P), new Org.BouncyCastle.Math.BigInteger(1, p.Q), new Org.BouncyCastle.Math.BigInteger(1, p.DP), new Org.BouncyCastle.Math.BigInteger(1, p.DQ), new Org.BouncyCastle.Math.BigInteger(1, p.InverseQ)); using (var sw = new StreamWriter(privateKeyFile)) { var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw); pemWriter.WriteObject(key); } } File.WriteAllText(certFile, PemCertificateHelper.ExportCertificateToPEM(@this)); } catch (Exception ex) { throw new Exception($"Certificate could not be saved. cert: {certFile} - key: {privateKeyFile}", ex); } }
public static void SavePem(this X509Certificate2 @this, out string cert, out string privateKey) { cert = string.Empty; privateKey = string.Empty; try { if (@this.HasPrivateKey) { var p = @this.GetRSAPrivateKey().ExportParameters(true); var key = new RsaPrivateCrtKeyParameters( new Org.BouncyCastle.Math.BigInteger(1, p.Modulus), new Org.BouncyCastle.Math.BigInteger(1, p.Exponent), new Org.BouncyCastle.Math.BigInteger(1, p.D), new Org.BouncyCastle.Math.BigInteger(1, p.P), new Org.BouncyCastle.Math.BigInteger(1, p.Q), new Org.BouncyCastle.Math.BigInteger(1, p.DP), new Org.BouncyCastle.Math.BigInteger(1, p.DQ), new Org.BouncyCastle.Math.BigInteger(1, p.InverseQ)); using (var stringWriter = new StringWriter()) { var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(stringWriter); pemWriter.WriteObject(key); privateKey = stringWriter.GetStringBuilder().ToString(); } } cert = PemCertificateHelper.ExportCertificateToPEM(@this); } catch (Exception ex) { throw new Exception($"Certificate could not be saved. ", ex); } }
public static X509Certificate2 GenerateQlikJWTConformCert(this X509Certificate2 @this, string subjectName, string issuerName, int keyStrength = 2048) { try { return(PemCertificateHelper.GenerateSelfSignedCertificate(subjectName, issuerName, keyStrength)); } catch (Exception ex) { throw new Exception("Certificate could not be generated.", ex); } }
public static X509Certificate2 LoadPem(this X509Certificate2 @this, byte[] certBuffer, byte[] privateKeyBuffer = null) { try { return(PemCertificateHelper.ReadPemCertificateWithPrivateKey(certBuffer, privateKeyBuffer)); } catch (Exception ex) { throw new Exception($"Pem certificate buffer could not be loaded", ex); } }
public static X509Certificate2 LoadPem(this X509Certificate2 @this, string certFile, string privateKeyFile = null) { try { return(PemCertificateHelper.ReadPemCertificateWithPrivateKey(certFile, privateKeyFile)); } catch (Exception ex) { throw new Exception($"Pem certificate {certFile} could not be loaded", ex); } }