Skip to content

Commit 685678a

Browse files
Adds API to download cert (#1094)
1 parent 0f5a4a1 commit 685678a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dev-proxy/ApiControllers/ProxyController.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using Microsoft.AspNetCore.Mvc;
66
using DevProxy.Jwt;
7+
using System.Security.Cryptography.X509Certificates;
8+
using System.ComponentModel.DataAnnotations;
79

810
namespace DevProxy.ApiControllers;
911

@@ -65,4 +67,33 @@ public IActionResult CreateJwtToken([FromBody] JwtOptions jwtOptions)
6567

6668
return Ok(new JwtInfo { Token = token });
6769
}
70+
71+
[HttpGet("rootCertificate")]
72+
public IActionResult GetRootCertificate([FromQuery][Required] string format)
73+
{
74+
if (!format.Equals("crt", StringComparison.OrdinalIgnoreCase))
75+
{
76+
ModelState.AddModelError("format", "Invalid format. Supported format is 'crt'.");
77+
return ValidationProblem(ModelState);
78+
}
79+
80+
var certificate = ProxyEngine.ProxyServer.CertificateManager.RootCertificate;
81+
if (certificate == null)
82+
{
83+
var problemDetails = new ProblemDetails
84+
{
85+
Title = "Certificate Not Found",
86+
Detail = "No root certificate found.",
87+
Status = StatusCodes.Status404NotFound
88+
};
89+
return NotFound(problemDetails);
90+
}
91+
92+
var certBytes = certificate.Export(X509ContentType.Cert);
93+
var base64Cert = Convert.ToBase64String(certBytes, Base64FormattingOptions.InsertLineBreaks);
94+
var pem = "-----BEGIN CERTIFICATE-----\n" + base64Cert + "\n-----END CERTIFICATE-----";
95+
var pemBytes = System.Text.Encoding.ASCII.GetBytes(pem);
96+
97+
return File(pemBytes, "application/x-x509-ca-cert", "devProxy.pem");
98+
}
6899
}

0 commit comments

Comments
 (0)