Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nomailme committed Apr 7, 2021
1 parent aa52503 commit 7138ddc
Show file tree
Hide file tree
Showing 20 changed files with 309 additions and 306 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Linq;
using TestAuthorityCore.X509;

namespace TestAuthorityCore.Contracts
{
Expand Down
23 changes: 13 additions & 10 deletions source/TestAuthorityCore/Contracts/ErrorModel.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/// <summary>
/// Error model.
/// </summary>
public class ErrorModel
namespace TestAuthorityCore.Contracts
{
/// <summary>
/// Name of the field.
/// Error model.
/// </summary>
public string FieldName { get; set; }
public class ErrorModel
{
/// <summary>
/// Name of the field.
/// </summary>
public string FieldName { get; set; }

/// <summary>
/// Error Message.
/// </summary>
public string Message { get; set; }
/// <summary>
/// Error Message.
/// </summary>
public string Message { get; set; }

}
}
15 changes: 9 additions & 6 deletions source/TestAuthorityCore/Contracts/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Collections.Generic;

/// <summary>
/// Error response contract.
/// </summary>
public class ErrorResponse
namespace TestAuthorityCore.Contracts
{
/// <summary>
/// List of errors.
/// Error response contract.
/// </summary>
public List<ErrorModel> Errors { get; set; } = new List<ErrorModel>();
public class ErrorResponse
{
/// <summary>
/// List of errors.
/// </summary>
public List<ErrorModel> Errors { get; set; } = new List<ErrorModel>();
}
}
30 changes: 15 additions & 15 deletions source/TestAuthorityCore/Controllers/CertificateController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using TestAuthorityCore.Contracts;
Expand All @@ -9,53 +8,54 @@
namespace TestAuthorityCore.Controllers
{
/// <summary>
/// Provides functionality to work with certificates
/// Provides functionality to work with certificates
/// </summary>
[Route("api/certificate")]
public class CertificateController : Controller
{
private readonly RootCertificateService rootCertificateService;
private readonly ICertificateConverter converter;
private readonly RootCertificateService rootCertificateService;
private readonly CertificateAuthorityService service;

/// <summary>
/// Ctor.
/// Ctor.
/// </summary>
/// <param name="service"><seecref name="CertificateAuthorityService"/>.</param>
/// <param name="rootCertificateService"><seecref name="RootCertificateService"/>.</param>
/// <param name="converter"><seecref name="ICertificateConverter"/>.</param>
public CertificateController(CertificateAuthorityService service, RootCertificateService rootCertificateService, ICertificateConverter converter)
/// <param name="service"><seecref name="CertificateAuthorityService" />.</param>
/// <param name="rootCertificateService"><seecref name="RootCertificateService" />.</param>
/// <param name="converter"><seecref name="ICertificateConverter" />.</param>
public CertificateController(CertificateAuthorityService service, RootCertificateService rootCertificateService,
ICertificateConverter converter)
{
this.service = service;
this.rootCertificateService = rootCertificateService;
this.converter = converter;
}

/// <summary>
/// Download root certificate.
/// Download root certificate.
/// </summary>
/// <returns>Root certificate.</returns>
[HttpGet("/api/certificate/root")]
public IActionResult GetRootCertificate()
{
byte[] result = rootCertificateService.GetRootCertificate().Certificate.RawData;
var result = rootCertificateService.GetRootCertificate().Certificate.RawData;
return File(result, MediaTypeNames.Application.Octet, "root.cer");
}

/// <summary>
/// Generate current Crl.
/// Generate current Crl.
/// </summary>
/// <returns>Certificate.</returns>
[HttpGet("/api/certificate/crl")]
public IActionResult GetCrl()
{
byte[] result = service.GenerateCrl();
var result = service.GenerateCrl();
return File(result, MediaTypeNames.Application.Octet, "root.crl");
}


/// <summary>
/// Issue a certificate. Export in PFX format.
/// Issue a certificate. Export in PFX format.
/// </summary>
/// <param name="request">Certificate request.</param>
/// <returns>Result.</returns>
Expand Down Expand Up @@ -86,4 +86,4 @@ public IActionResult IssueCertificate([FromQuery] CertificateRequestModel reques
}
}
}
}
}
30 changes: 14 additions & 16 deletions source/TestAuthorityCore/Controllers/Pkcs12ToolsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Net.Mime;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -12,26 +11,27 @@
namespace TestAuthorityCore.Controllers
{
/// <summary>
/// Provides API for pfx tooling.
/// Provides API for pfx tooling.
/// </summary>
[Route("api/pkcs12")]
public class Pkcs12ToolsController : Controller
{
/// <summary>
/// Convert certificate and key in Pem format to Pfx(Pkcs12).
/// Convert certificate and key in Pem format to Pfx(Pkcs12).
/// </summary>
/// <param name="pemCertificate">Certificate in Pem format.</param>
/// <param name="pemKey">Private key in Pem format.</param>
/// <param name="password">Password for the private key.</param>
/// <param name="filename">Output filename.</param>
/// <returns>Certificate with private key in Pkcs12 container.</returns>
[HttpPost("from-pem")]
public IActionResult ConvertToPfx(IFormFile pemCertificate, IFormFile pemKey, string password, string filename = "certificate.pfx")
public IActionResult ConvertToPfx(IFormFile pemCertificate, IFormFile pemKey, string password,
string filename = "certificate.pfx")
{
byte[] certificate = ToArray(pemCertificate.OpenReadStream());
byte[] key = ToArray(pemKey.OpenReadStream());
var certificate = ToArray(pemCertificate.OpenReadStream());
var key = ToArray(pemKey.OpenReadStream());

byte[] result = ConvertToPfxImpl(certificate, key, password);
var result = ConvertToPfxImpl(certificate, key, password);
return File(result, MediaTypeNames.Application.Octet, filename);
}

Expand All @@ -48,30 +48,28 @@ private static TOutput ToCrypto<TOutput>(byte[] input)
{
using var stream = new MemoryStream(input);
using var streamReader = new StreamReader(stream);
object value = new PemReader(streamReader).ReadObject();
if (value is TOutput result)
{
return result;
}
var value = new PemReader(streamReader).ReadObject();
if (value is TOutput result) return result;

return null;
}

private byte[] ConvertToPfxImpl(byte[] certificate, byte[] privateKey, string password)
{
Pkcs12Store store = new Pkcs12StoreBuilder().Build();
var store = new Pkcs12StoreBuilder().Build();

var certificateEntry = new X509CertificateEntry[1];
var x509Certificate = ToCrypto<X509Certificate>(certificate);
certificateEntry[0] = new X509CertificateEntry(x509Certificate);

var asymmetricCipherKeyPair = ToCrypto<AsymmetricCipherKeyPair>(privateKey);

store.SetKeyEntry(x509Certificate.SubjectDN.ToString(), new AsymmetricKeyEntry(asymmetricCipherKeyPair.Private), certificateEntry);
store.SetKeyEntry(x509Certificate.SubjectDN.ToString(),
new AsymmetricKeyEntry(asymmetricCipherKeyPair.Private), certificateEntry);
var result = new MemoryStream();
store.Save(result, password.ToCharArray(), new SecureRandom());
result.Position = 0;
return result.ToArray();
}
}
}
}
Loading

0 comments on commit 7138ddc

Please sign in to comment.