-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper methods divided in separate controllers.
- Loading branch information
Iskander Yarmukhametov
committed
Jul 12, 2019
1 parent
3c6711c
commit 49483be
Showing
4 changed files
with
207 additions
and
190 deletions.
There are no files selected for viewing
190 changes: 0 additions & 190 deletions
190
source/TestAuthorityCore/Controllers/CertificateTools.cs
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
source/TestAuthorityCore/Controllers/DerToolsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
source/TestAuthorityCore/Controllers/PemToolsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.