Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrdk committed Jun 7, 2022
1 parent 4230041 commit 14279e6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/opencertserver.ca.utils/CertificateConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenCertServer.Ca.Utils;

public static class CertificateConstants
{
public const string RsaOid = "1.2.840.113549.1.1.1";
public const string EcdsaOid = "1.2.840.10045.2.1";
}
2 changes: 1 addition & 1 deletion src/opencertserver.ca.utils/X509CertificatesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

public static class X509CertificatesExtensions
{
public static string ToPem(this X509Certificate2 cert)
Expand Down
4 changes: 2 additions & 2 deletions src/opencertserver.ca/CertificateAuthority.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public SignCertificateResponse SignCertificateRequest(
_logger.LogInformation("Creating certificate for {subjectName}", request.SubjectName.Name);
var cert = request.PublicKey.Oid.Value switch
{
"1.2.840.113549.1.1.1" => request.Create(
CertificateConstants.RsaOid => request.Create(
_rsaCertificate,
DateTimeOffset.UtcNow.Date,
DateTimeOffset.UtcNow.Date.Add(_certificateValidity),
BitConverter.GetBytes(DateTime.UtcNow.Ticks)),
"1.2.840.10045.2.1" => request.Create(
CertificateConstants.EcdsaOid => request.Create(
_ecdsaCertificate,
DateTimeOffset.UtcNow.Date,
DateTimeOffset.UtcNow.Date.Add(_certificateValidity),
Expand Down
4 changes: 2 additions & 2 deletions src/opencertserver.est.client/EstClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public async Task<X509Certificate2> ReEnroll(

var certRequest = oidValue switch
{
"1.2.840.10045.2.1" => CreateCertificateRequest(
CertificateConstants.EcdsaOid => CreateCertificateRequest(
certificate.SubjectName,
certificate.GetECDsaPrivateKey()!,
usageFlags),
"1.2.840.113549.1.1.1" => CreateCertificateRequest(certificate.SubjectName,
CertificateConstants.RsaOid => CreateCertificateRequest(certificate.SubjectName,
certificate.GetRSAPrivateKey()!,
usageFlags),
_ => throw new NotSupportedException($"{oidValue} is not supported")
Expand Down
8 changes: 3 additions & 5 deletions src/opencertserver.est.server/CertificateServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace OpenCertServer.Est.Server
{
using System;
using System.Security.Cryptography.X509Certificates;
using Ca.Utils;
using Handlers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
Expand All @@ -14,9 +15,6 @@ namespace OpenCertServer.Est.Server
/// </summary>
public static class CertificateServerExtensions
{
private const string RsaOid = "1.2.840.113549.1.1.1";
private const string EcdsaOid = "1.2.840.10045.2.1";

public static IServiceCollection AddEstServer(
this IServiceCollection services,
X500DistinguishedName distinguishedName,
Expand All @@ -43,12 +41,12 @@ public static IServiceCollection AddEstServer(
X509Certificate2 ecdsaCertificate,
Func<X509Chain, bool>? chainValidation = null)
{
if (rsaCertificate.PublicKey.Oid.Value != RsaOid)
if (rsaCertificate.PublicKey.Oid.Value != CertificateConstants.RsaOid)
{
throw new ArgumentException("Must be an RSA key certificate");
}

if (ecdsaCertificate.PublicKey.Oid.Value != EcdsaOid)
if (ecdsaCertificate.PublicKey.Oid.Value != CertificateConstants.EcdsaOid)
{
throw new ArgumentException("Must be an ECDSA key certificate");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Ca.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using OpenCertServer.Ca;
Expand Down Expand Up @@ -51,11 +52,11 @@ public async Task Handle(HttpContext ctx)

var request = cert.PublicKey.Oid.Value switch
{
"1.2.840.10045.2.1" => new CertificateRequest(
CertificateConstants.EcdsaOid => new CertificateRequest(
cert.SubjectName,
cert.GetECDsaPublicKey()!,
HashAlgorithmName.SHA256),
"1.2.840.113549.1.1.1" => new CertificateRequest(
CertificateConstants.RsaOid => new CertificateRequest(
cert.SubjectName,
cert.GetRSAPublicKey()!,
HashAlgorithmName.SHA256,
Expand Down

0 comments on commit 14279e6

Please sign in to comment.