Skip to content

Commit 0c5c177

Browse files
authored
[Https] Export the certificate in PEM format too (#23803)
* Changed `--key-format` to `--format`. * Changed the format of the certificate to PEM when `--format pem` is indicated.
1 parent eac4925 commit 0c5c177

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

src/Shared/CertificateGeneration/CertificateManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ internal void ExportCertificate(X509Certificate2 certificate, string path, bool
440440
Array.Clear(keyBytes, 0, keyBytes.Length);
441441
Array.Clear(pem, 0, pem.Length);
442442

443-
bytes = certificate.Export(X509ContentType.Cert);
443+
bytes = Encoding.ASCII.GetBytes(PemEncoding.Write("CERTIFICATE", certificate.Export(X509ContentType.Cert)));
444444
break;
445445
default:
446446
throw new InvalidOperationException("Unknown format.");

src/Tools/FirstRunCertGenerator/test/CertificateManagerTests.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void EnsureCreateHttpsCertificate_CanExportTheCertInPemFormat()
162162
{
163163
// Arrange
164164
var message = "plaintext";
165-
const string CertificateName = nameof(EnsureCreateHttpsCertificate_DoesNotCreateACertificate_WhenThereIsAnExistingHttpsCertificates) + ".pfx";
165+
const string CertificateName = nameof(EnsureCreateHttpsCertificate_DoesNotCreateACertificate_WhenThereIsAnExistingHttpsCertificates) + ".pem";
166166
var certificatePassword = Guid.NewGuid().ToString();
167167

168168
_fixture.CleanupCertificates();
@@ -183,10 +183,7 @@ public void EnsureCreateHttpsCertificate_CanExportTheCertInPemFormat()
183183
Assert.Equal(EnsureCertificateResult.ValidCertificatePresent, result);
184184
Assert.True(File.Exists(CertificateName));
185185

186-
var key = RSA.Create();
187-
key.ImportFromEncryptedPem(File.ReadAllText(Path.ChangeExtension(CertificateName, "key")), certificatePassword);
188-
var exportedCertificate = new X509Certificate2(File.ReadAllBytes(CertificateName));
189-
exportedCertificate = exportedCertificate.CopyWithPrivateKey(key);
186+
var exportedCertificate = X509Certificate2.CreateFromEncryptedPemFile(CertificateName, certificatePassword, Path.ChangeExtension(CertificateName, "key"));
190187
Assert.NotNull(exportedCertificate);
191188
Assert.True(exportedCertificate.HasPrivateKey);
192189

@@ -260,7 +257,7 @@ public void EnsureCreateHttpsCertificate_CanExportTheCertInPemFormat_WithoutPass
260257
{
261258
// Arrange
262259
var message = "plaintext";
263-
const string CertificateName = nameof(EnsureCreateHttpsCertificate_DoesNotCreateACertificate_WhenThereIsAnExistingHttpsCertificates) + ".pfx";
260+
const string CertificateName = nameof(EnsureCreateHttpsCertificate_DoesNotCreateACertificate_WhenThereIsAnExistingHttpsCertificates) + ".pem";
264261
_fixture.CleanupCertificates();
265262

266263
var now = DateTimeOffset.UtcNow;
@@ -277,10 +274,7 @@ public void EnsureCreateHttpsCertificate_CanExportTheCertInPemFormat_WithoutPass
277274
Assert.Equal(EnsureCertificateResult.ValidCertificatePresent, result);
278275
Assert.True(File.Exists(CertificateName));
279276

280-
var key = RSA.Create();
281-
key.ImportFromPem(File.ReadAllText(Path.ChangeExtension(CertificateName, "key")));
282-
var exportedCertificate = new X509Certificate2(File.ReadAllBytes(CertificateName));
283-
exportedCertificate = exportedCertificate.CopyWithPrivateKey(key);
277+
var exportedCertificate = X509Certificate2.CreateFromPemFile(CertificateName, Path.ChangeExtension(CertificateName, "key"));
284278
Assert.NotNull(exportedCertificate);
285279
Assert.True(exportedCertificate.HasPrivateKey);
286280

src/Tools/dotnet-dev-certs/src/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public static int Main(string[] args)
9292
"Imports the provided HTTPS development certificate into the machine. All other HTTPS developer certificates will be cleared out",
9393
CommandOptionType.SingleValue);
9494

95-
var keyFormat = c.Option(
96-
"--key-format",
97-
"Export the certificate key in the given format. Valid values are Pfx and Pem. Pfx is the default.",
95+
var format = c.Option(
96+
"--format",
97+
"Export the certificate in the given format. Valid values are Pfx and Pem. Pfx is the default.",
9898
CommandOptionType.SingleValue);
9999

100100
CommandOption trust = null;
@@ -121,7 +121,7 @@ public static int Main(string[] args)
121121

122122
if (clean.HasValue())
123123
{
124-
if (exportPath.HasValue() || trust?.HasValue() == true || keyFormat.HasValue() || noPassword.HasValue() || check.HasValue() ||
124+
if (exportPath.HasValue() || trust?.HasValue() == true || format.HasValue() || noPassword.HasValue() || check.HasValue() ||
125125
(!import.HasValue() && password.HasValue()) ||
126126
(import.HasValue() && !password.HasValue()))
127127
{
@@ -132,7 +132,7 @@ public static int Main(string[] args)
132132

133133
if (check.HasValue())
134134
{
135-
if (exportPath.HasValue() || password.HasValue() || noPassword.HasValue() || clean.HasValue() || keyFormat.HasValue() || import.HasValue())
135+
if (exportPath.HasValue() || password.HasValue() || noPassword.HasValue() || clean.HasValue() || format.HasValue() || import.HasValue())
136136
{
137137
reporter.Error(InvalidUsageErrorMessage);
138138
return CriticalError;
@@ -147,7 +147,7 @@ public static int Main(string[] args)
147147
return CriticalError;
148148
}
149149

150-
if (noPassword.HasValue() && !(keyFormat.HasValue() && string.Equals(keyFormat.Value(), "PEM", StringComparison.OrdinalIgnoreCase)))
150+
if (noPassword.HasValue() && !(format.HasValue() && string.Equals(format.Value(), "PEM", StringComparison.OrdinalIgnoreCase)))
151151
{
152152
reporter.Error(InvalidUsageErrorMessage);
153153
return CriticalError;
@@ -176,7 +176,7 @@ public static int Main(string[] args)
176176
return ImportCertificate(import, password, reporter);
177177
}
178178

179-
return EnsureHttpsCertificate(exportPath, password, noPassword, trust, keyFormat, reporter);
179+
return EnsureHttpsCertificate(exportPath, password, noPassword, trust, format, reporter);
180180
});
181181
});
182182

0 commit comments

Comments
 (0)