From a7e6ec26bca8af7ae1866364fbdb26839bae601f Mon Sep 17 00:00:00 2001 From: damienbod Date: Wed, 29 Jan 2020 10:33:46 +0100 Subject: [PATCH] docs import export pem --- Documentation.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/Documentation.md b/Documentation.md index eaf326f..711ee7d 100644 --- a/Documentation.md +++ b/Documentation.md @@ -382,6 +382,69 @@ var deviceVerifyPublicKey = importExportCertificate.ExportCertificatePublicKey(d var deviceVerifyPublicKeyBytes = deviceVerifyPublicKey.Export(X509ContentType.Cert); File.WriteAllBytes($"deviceVerify.cer", deviceVerifyPublicKeyBytes); ``` + +## Exporting Importing PEM + +RSA + +```csharp +var sp = new ServiceCollection() + .AddCertificateManager() + .BuildServiceProvider(); + +var ccRsa = sp.GetService(); +var iec = sp.GetService(); + +var rsaCert = ccRsa.CreateDevelopmentCertificate("localhost", 2, 2048); + +// export +var publicKeyPem = iec.PemExportPublicKeyCertificate(rsaCert); +var rsaPrivateKeyPem = iec.PemExportRsaPrivateKey(rsaCert); + +// import +var roundTripPublicKeyPem = iec.PemImportCertificate(publicKeyPem); +var roundTripRsaPrivateKeyPem = iec.PemImportPrivateKey(rsaPrivateKeyPem); + +var roundTripFullCert = + iec.CreateCertificateWithPrivateKey( + roundTripPublicKeyPem, + roundTripRsaPrivateKeyPem, + "1234"); + +``` + +ECDsa + +```csharp +var sp = new ServiceCollection() + .AddCertificateManager() + .BuildServiceProvider(); + +var cc = serviceProvider.GetService(); + +var root = cc.NewRootCertificate( + new DistinguishedName { CommonName = "root dev", Country = "IT" }, + new ValidityPeriod { ValidFrom = DateTime.UtcNow, ValidTo = DateTime.UtcNow.AddYears(10) }, + 3, "localhost"); +root.FriendlyName = "developement root L1 certificate"; + +var iec = sp.GetService(); + +// export +var publicKeyPem = iec.PemExportPublicKeyCertificate(root); +var eCDsaPrivateKeyPem = iec.PemExportECPrivateKey(root); + +// import +var roundTripPublicKeyPem = iec.PemImportCertificate(publicKeyPem); +var roundTripECPrivateKeyPem = iec.PemImportPrivateKey(eCDsaPrivateKeyPem); + +var roundTripFullCert = + iec.CreateCertificateWithPrivateKey( + roundTripPublicKeyPem, + roundTripECPrivateKeyPem, + "1234"); + +``` ## General Certificates, full APIs ### Self signed certificate