Skip to content

Commit

Permalink
CRL defaults to PEM format
Browse files Browse the repository at this point in the history
  • Loading branch information
nomailme committed Feb 17, 2022
1 parent 7138ddc commit a783611
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public IActionResult GetRootCertificate()
[HttpGet("/api/certificate/crl")]
public IActionResult GetCrl()
{
var result = service.GenerateCrl();
var crl = service.GenerateCrl();
var result = converter.ConvertToPem(crl);
return File(result, MediaTypeNames.Application.Octet, "root.crl");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public CertificateAuthorityService(CertificateWithKey signerCertificate, RandomS
/// Generate CRL file.
/// </summary>
/// <returns>Crl file as a byte array.</returns>
public byte[] GenerateCrl()
public CrlFile GenerateCrl()
{
SecureRandom random = randomService.GenerateRandom();
ICrlBuilder crlBuilder = crlBuilderFactory(random, signerCertificate);
var crl = crlBuilder.Generate();
return crl.GetEncoded();
return new CrlFile(crl);
}

/// <summary>
Expand Down Expand Up @@ -72,4 +72,4 @@ public CertificateWithKey GenerateSslCertificate(CertificateRequest request)
return certificate;
}
}
}
}
17 changes: 12 additions & 5 deletions source/TestAuthorityCore/Service/CertificateConverterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CertificateConverterService(RandomService randomService, RootCertificateS
this.randomService = randomService;
this.rootCertificateService = rootCertificateService;
}

/// <summary>
/// Convert certificate to zip archive with certificate and key in PEM format.
/// </summary>
Expand All @@ -55,6 +55,13 @@ public byte[] ConvertToPfx(CertificateWithKey certificate, string password)
return ConvertToPfxCore(certificate.Certificate, (RsaPrivateCrtKeyParameters)certificate.KeyPair?.Private, password);
}

/// <inheritdoc />
public byte[] ConvertToPem(CrlFile crl)
{
var pem = ConvertToPemFormat(crl.Crl);
return Encoding.ASCII.GetBytes(pem);
}

private byte[] ConvertToPfxCore(X509Certificate2 x509, RsaPrivateCrtKeyParameters rsaParams, string pfxPassword)
{
var store = new Pkcs12Store();
Expand Down Expand Up @@ -103,21 +110,21 @@ private static string ConvertToPemFormat(object input)
{
var generator = new MiscPemGenerator(input);

string certificateString;
string outputString;
using (var textWriter = new StringWriter())
{
var writer = new PemWriter(textWriter);
writer.WriteObject(generator);
writer.Writer.Flush();
certificateString = textWriter.ToString();
outputString = textWriter.ToString();
}

if (string.IsNullOrWhiteSpace(certificateString))
if (string.IsNullOrWhiteSpace(outputString))
{
throw new InvalidOperationException();
}

return certificateString;
return outputString;
}
}
}
9 changes: 8 additions & 1 deletion source/TestAuthorityCore/Service/ICertificateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ public interface ICertificateConverter
/// <param name="password">Pfx password.</param>
/// <returns>Pfx file as a byte array.</returns>
byte[] ConvertToPfx(CertificateWithKey certificate, string password);

/// <summary>
/// Convert CRL to pem format.
/// </summary>
/// <param name="crl"><see cref="CrlFile"/>.</param>
/// <returns>Pem representation of Crl.</returns>
byte[] ConvertToPem(CrlFile crl);
}
}
}
24 changes: 24 additions & 0 deletions source/TestAuthorityCore/X509/CrlFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Org.BouncyCastle.X509;

namespace TestAuthorityCore.X509
{
/// <summary>
/// Crl wrapper.
/// </summary>
public class CrlFile
{
/// <summary>
/// ctor.
/// </summary>
/// <param name="crl"><see cref="X509Crl" />.</param>
public CrlFile(X509Crl crl)
{
Crl = crl;
}

/// <summary>
/// Crl file.
/// </summary>
public X509Crl Crl { get; set; }
}
}

0 comments on commit a783611

Please sign in to comment.