mirror of
https://github.com/opensim/opensim.git
synced 2026-08-09 18:55:58 +08:00
Implementation of a basic PEM encoded to OpenSim compatible PKCS12 certificates converter.
As you know most CAs return PEM encoded certificates and require a conversion using OpenSSL to make them compatible with OpenSim. This implementation does the automatic conversion from .pem to .p12 and .pfx at OpenSim startup which updates the certificate in case of automatic certificates renewal... Note: still under testing using certbot and Let's Encrypt certs... Thank you and good continuaion Web Rain :)
This commit is contained in:
@@ -1550,6 +1550,53 @@ namespace OpenSim.Framework
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ConvertPemToPKCS12(string certFileName, string fullChainPath, string privateKeyPath)
|
||||
{
|
||||
ConvertPemToPKCS12Certificate(certFileName, fullChainPath, privateKeyPath, null);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ConvertPemToPKCS12(string certFileName, string fullChainPath, string privateKeyPath, string outputPassword)
|
||||
{
|
||||
ConvertPemToPKCS12Certificate(certFileName, fullChainPath, privateKeyPath, outputPassword);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert or renew .pem certificate to PKCS12 .pfx and .p12 usable by OpenSim.
|
||||
/// the parameters are set in the startup section of OpenSim.ini
|
||||
/// </summary>
|
||||
/// <param name="certFileName">The output certificate file name.</param>
|
||||
/// <param name="certPath">The path of fullchain.pem. If your CA don't provide
|
||||
/// the fullchain file, you can set the cert.pem instead.</param>
|
||||
/// <param name="keyPath">The path of the private key (privkey.pem).</param>
|
||||
/// <param name="certPassword">The output certificates password.</param>
|
||||
private static void ConvertPemToPKCS12Certificate(string certFileName, string certPath, string keyPath, string outputPassword)
|
||||
{
|
||||
if(string.IsNullOrEmpty(certPath) || string.IsNullOrEmpty(keyPath)){
|
||||
m_log.ErrorFormat("[UTIL]: Missing or invalid fullchain.pem / privkey.pem path!.");
|
||||
return;
|
||||
}
|
||||
// Create the SSL folder and sub folders if not exists.
|
||||
if (!Directory.Exists("SSL\\ssl\\"))
|
||||
Directory.CreateDirectory("SSL\\ssl\\");
|
||||
|
||||
// Convert .pem (like Let's Encrypt files) to X509Certificate2 certificate.
|
||||
X509Certificate2 certificate = X509Certificate2.CreateFromPemFile(certPath, keyPath);
|
||||
|
||||
// Export and store the .pfx and .p12 certificates in SSL\ssl\.
|
||||
byte[] pfxCertBytes = string.IsNullOrEmpty(outputPassword)
|
||||
? certificate.Export(X509ContentType.Pfx)
|
||||
: certificate.Export(X509ContentType.Pfx, outputPassword);
|
||||
File.WriteAllBytes($"SSL\\ssl\\{certFileName}.pfx", pfxCertBytes);
|
||||
|
||||
byte[] p12CertBytes = string.IsNullOrEmpty(outputPassword)
|
||||
? certificate.Export(X509ContentType.Pkcs12)
|
||||
: certificate.Export(X509ContentType.Pkcs12, outputPassword);
|
||||
File.WriteAllBytes($"SSL\\ssl\\{certFileName}.p12", p12CertBytes);
|
||||
|
||||
}
|
||||
|
||||
public static int fast_distance2d(int x, int y)
|
||||
{
|
||||
x = Math.Abs(x);
|
||||
|
||||
Reference in New Issue
Block a user