Skip to content

Commit

Permalink
Helper methods divided in separate controllers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iskander Yarmukhametov committed Jul 12, 2019
1 parent 3c6711c commit 49483be
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 190 deletions.
190 changes: 0 additions & 190 deletions source/TestAuthorityCore/Controllers/CertificateTools.cs

This file was deleted.

51 changes: 51 additions & 0 deletions source/TestAuthorityCore/Controllers/DerToolsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;
using System.Net.Mime;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.X509;

namespace TestAuthorityCore.Controllers
{
/// <summary>
/// Provides some useful tools like certificateName conversion.
/// </summary>
[Route("api/tools")]
public class DerToolsController : Controller
{
[HttpPost("der-to-pem")]
public IActionResult ConvertCertificateToPem([FromForm] IFormFile file, string certificateName = "certificate.crt")
{
using (var streamReader = new StreamReader(file.OpenReadStream()))
{
X509Certificate certificate = new X509CertificateParser().ReadCertificate(streamReader.BaseStream);
string certificateString = ConvertCertificateToPem(certificate);
byte[] result = Encoding.ASCII.GetBytes(certificateString);
return File(result, MediaTypeNames.Application.Octet, certificateName);
}
}

private static string ConvertCertificateToPem(X509Certificate certificate)
{
var generator = new MiscPemGenerator(certificate);

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

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

return certificateString;
}
}
}
76 changes: 76 additions & 0 deletions source/TestAuthorityCore/Controllers/PemToolsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;

namespace TestAuthorityCore.Controllers
{
public class PemToolsController : Controller
{
[HttpPost("pfx-to-certificate")]
public IActionResult GetCertificateFromPfx([FromForm] IFormFile file, string password, string certificateName = "certificate.crt")
{
using (var streamReader = new StreamReader(file.OpenReadStream()))
{
var store = new Pkcs12Store(streamReader.BaseStream, password.ToCharArray());
string firstAlias = store.Aliases.OfType<string>().FirstOrDefault();
if (string.IsNullOrWhiteSpace(firstAlias))
{
throw new InvalidOperationException("Unable to find any certificateName in PFX store");
}

X509CertificateEntry certificateEntry = store.GetCertificate(firstAlias);

string certificateString = ConvertToPemFormat(certificateEntry.Certificate);
byte[] result = Encoding.ASCII.GetBytes(certificateString);
return File(result, MediaTypeNames.Application.Octet, certificateName);
}
}

[HttpPost("pfx-to-key")]
public IActionResult GetKeyFromPfx([FromForm] IFormFile file, string password, string filename = "certificate.key")
{
using (var streamReader = new StreamReader(file.OpenReadStream()))
{
var store = new Pkcs12Store(streamReader.BaseStream, password.ToCharArray());
string firstAlias = store.Aliases.OfType<string>().FirstOrDefault();
if (string.IsNullOrWhiteSpace(firstAlias))
{
throw new InvalidOperationException("Unable to find any certificateName in PFX store");
}

AsymmetricKeyEntry key = store.GetKey(firstAlias);
string convertedKey = ConvertToPemFormat(key.Key);

byte[] result = Encoding.ASCII.GetBytes(convertedKey);
return File(result, MediaTypeNames.Application.Octet, filename);
}
}

private static string ConvertToPemFormat(object input)
{
var generator = new MiscPemGenerator(input);

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

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

return certificateString;
}
}
}
Loading

0 comments on commit 49483be

Please sign in to comment.