Bearsampp 2025.8.29
Loading...
Searching...
No Matches
OpenSsl Class Reference

Public Member Functions

 createCrt ($name, $destPath=null)
 existsCrt ($name)
 removeCrt ($name)

Detailed Description

Definition at line 11 of file class.openssl.php.

Member Function Documentation

◆ createCrt()

createCrt ( $name,
$destPath = null )

Creates a certificate with the specified name and destination path.

Parameters
string$nameThe name of the certificate.
string | null$destPathThe destination path where the certificate files will be saved. If null, the default SSL path is used.
Returns
bool True if the certificate was created successfully, false otherwise.

Definition at line 20 of file class.openssl.php.

21 {
23 $destPath = empty($destPath) ? $bearsamppRoot->getSslPath() : $destPath;
24
25 $subject = '"/C=US/O=Bearsampp/CN=' . $name . '"';
26 $password = 'pass:bearsampp';
27 $ppkPath = '"' . $destPath . '/' . $name . '.ppk"';
28 $pubPath = '"' . $destPath . '/' . $name . '.pub"';
29 $crtPath = '"' . $destPath . '/' . $name . '.crt"';
30 $extension = 'SAN';
31 $exe = '"' . $bearsamppCore->getOpenSslExe() . '"';
32
33 // ext
34 $extContent = PHP_EOL . '[' . $extension . ']' . PHP_EOL;
35 $extContent .= 'subjectAltName=DNS:*.' . $name . ',DNS:' . $name . PHP_EOL;
36
37 // tmp openssl.cfg
38 $conf = $bearsamppCore->getTmpPath() . '/openssl_' . $name . '_' . Util::random() . '.cfg';
39 file_put_contents($conf, file_get_contents($bearsamppCore->getOpenSslConf()) . $extContent);
40
41 // Properly quote the config path for batch commands
42 $confPath = '"' . $conf . '"';
43
44 // ppk - Updated for OpenSSL 3.x syntax
45 $batch = $exe . ' genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -aes256 -pass ' . $password . ' -out ' . $ppkPath . ' -config ' . $confPath . PHP_EOL;
46 $batch .= 'IF %ERRORLEVEL% GEQ 1 GOTO EOF' . PHP_EOL . PHP_EOL;
47
48 // pub
49 $batch .= $exe . ' rsa -in ' . $ppkPath . ' -passin ' . $password . ' -out ' . $pubPath . PHP_EOL . PHP_EOL;
50 $batch .= 'IF %ERRORLEVEL% GEQ 1 GOTO EOF' . PHP_EOL . PHP_EOL;
51
52 // crt
53 $batch .= $exe . ' req -x509 -nodes -sha256 -new -key ' . $pubPath . ' -out ' . $crtPath . ' -passin ' . $password;
54 $batch .= ' -subj ' . $subject . ' -reqexts ' . $extension . ' -extensions ' . $extension . ' -config ' . $confPath . PHP_EOL;
55 $batch .= 'IF %ERRORLEVEL% GEQ 1 GOTO EOF' . PHP_EOL . PHP_EOL;
56
57 $batch .= ':EOF' . PHP_EOL;
58 $batch .= 'SET RESULT=KO' . PHP_EOL;
59 $batch .= 'IF EXIST ' . $pubPath . ' IF EXIST ' . $crtPath . ' SET RESULT=OK' . PHP_EOL;
60 $batch .= 'ECHO %RESULT%';
61
62 Util::logTrace('Creating SSL Certificate for "' . $name . '"');
63 $result = Batch::exec('createCertificate', $batch);
64
65 $success = isset($result[0]) && $result[0] == 'OK';
66 Util::logTrace('SSL Certificate generation for "' . $name . '": ' . ($success ? 'SUCCESS' : 'FAILURE'));
67
68 return $success;
69 }
$result
global $bearsamppRoot
global $bearsamppCore
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
static logTrace($data, $file=null)
static random($length=32, $withNumeric=true)

References $bearsamppCore, $bearsamppRoot, $result, Batch\exec(), Util\logTrace(), and Util\random().

◆ existsCrt()

existsCrt ( $name)

Checks if a certificate with the specified name exists.

Parameters
string$nameThe name of the certificate.
Returns
bool True if the certificate exists, false otherwise.

Definition at line 77 of file class.openssl.php.

78 {
79 global $bearsamppRoot;
80
81 $ppkPath = $bearsamppRoot->getSslPath() . '/' . $name . '.ppk';
82 $pubPath = $bearsamppRoot->getSslPath() . '/' . $name . '.pub';
83 $crtPath = $bearsamppRoot->getSslPath() . '/' . $name . '.crt';
84
85 return is_file($ppkPath) && is_file($pubPath) && is_file($crtPath);
86 }

References $bearsamppRoot.

◆ removeCrt()

removeCrt ( $name)

Removes a certificate with the specified name.

Parameters
string$nameThe name of the certificate.
Returns
bool True if the certificate was removed successfully, false otherwise.

Definition at line 94 of file class.openssl.php.

95 {
96 global $bearsamppRoot;
97
98 $ppkPath = $bearsamppRoot->getSslPath() . '/' . $name . '.ppk';
99 $pubPath = $bearsamppRoot->getSslPath() . '/' . $name . '.pub';
100 $crtPath = $bearsamppRoot->getSslPath() . '/' . $name . '.crt';
101
102 return @unlink($ppkPath) && @unlink($pubPath) && @unlink($crtPath);
103 }

References $bearsamppRoot.


The documentation for this class was generated from the following file: