coolify/app/Helpers/SslHelper.php

88 lines
2.9 KiB
PHP
Raw Normal View History

2025-01-30 13:17:12 +00:00
<?php
namespace App\Helpers;
use App\Models\SslCertificate;
use Carbon\CarbonImmutable;
2025-01-30 13:17:12 +00:00
class SslHelper
{
private const DEFAULT_ORGANIZATION_NAME = 'Coolify';
2025-01-30 13:17:12 +00:00
public static function generateSslCertificate(
string $commonName,
array $additionalSans = [],
?string $resourceType = null,
?int $resourceId = null,
?int $serverId = null,
?string $organizationName = null,
int $validityDays = 365,
?string $caCert = null,
?string $caKey = null
2025-01-30 13:17:12 +00:00
): SslCertificate {
$organizationName ??= self::DEFAULT_ORGANIZATION_NAME;
2025-01-30 13:17:12 +00:00
try {
$privateKey = openssl_pkey_new([
'private_key_type' => OPENSSL_KEYTYPE_EC,
'curve_name' => 'secp521r1',
2025-01-30 13:17:12 +00:00
]);
if ($privateKey === false) {
throw new \RuntimeException('Failed to generate private key: '.openssl_error_string());
}
if (! openssl_pkey_export($privateKey, $privateKeyStr)) {
throw new \RuntimeException('Failed to export private key: '.openssl_error_string());
}
$dn = [
'commonName' => $commonName,
'organizationName' => $organizationName,
'subjectAltName' => implode(', ', array_merge(["DNS:$commonName"], $additionalSans)),
2025-01-30 13:17:12 +00:00
];
$csr = openssl_csr_new($dn, $privateKey, [
2025-01-30 13:37:12 +00:00
'digest_alg' => 'sha512',
2025-01-30 13:17:12 +00:00
'config' => null,
2025-01-30 13:37:12 +00:00
'encrypt_key' => false,
2025-01-30 13:17:12 +00:00
]);
if ($csr === false) {
throw new \RuntimeException('Failed to generate CSR: '.openssl_error_string());
}
$certificate = openssl_csr_sign(
$csr,
$caCert ?? null,
$caKey ?? $privateKey,
$validityDays,
2025-01-30 13:17:12 +00:00
[
2025-01-30 13:37:12 +00:00
'digest_alg' => 'sha512',
2025-01-30 13:17:12 +00:00
'config' => null,
],
random_int(PHP_INT_MIN, PHP_INT_MAX)
);
if ($certificate === false) {
throw new \RuntimeException('Failed to sign certificate: '.openssl_error_string());
}
if (! openssl_x509_export($certificate, $certificateStr)) {
throw new \RuntimeException('Failed to export certificate: '.openssl_error_string());
}
return SslCertificate::create([
'ssl_certificate' => $certificateStr,
'ssl_private_key' => $privateKeyStr,
'resource_type' => $resourceType,
'resource_id' => $resourceId,
'server_id' => $serverId,
'valid_until' => CarbonImmutable::now()->addDays($validityDays),
2025-01-30 13:17:12 +00:00
]);
} catch (\Throwable $e) {
throw new \RuntimeException('SSL Certificate generation failed: '.$e->getMessage(), 0, $e);
}
}
}