2024.8.23
Loading...
Searching...
No Matches
OpenSsl Class Reference

Public Member Functions

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

Detailed Description

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

Member Function Documentation

◆ createCrt()

OpenSsl::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 19 of file class.openssl.php.

20 {
22 $destPath = empty($destPath) ? $bearsamppRoot->getSslPath() : $destPath;
23
24 $subject = '"/C=FR/O=bearsampp/CN=' . $name . '"';
25 $password = 'pass:bearsampp';
26 $ppkPath = '"' . $destPath . '/' . $name . '.ppk"';
27 $pubPath = '"' . $destPath . '/' . $name . '.pub"';
28 $crtPath = '"' . $destPath . '/' . $name . '.crt"';
29 $extension = 'SAN';
30 $exe = '"' . $bearsamppCore->getOpenSslExe() . '"';
31
32 // ext
33 $extContent = PHP_EOL . '[' . $extension . ']' . PHP_EOL;
34 $extContent .= 'subjectAltName=DNS:*.' . $name . ',DNS:' . $name . PHP_EOL;
35
36 // tmp openssl.cfg
37 $conf = $bearsamppCore->getTmpPath() . '/openssl_' . $name . '_' . Util::random() . '.cfg';
38 file_put_contents($conf, file_get_contents($bearsamppCore->getOpenSslConf()) . $extContent);
39
40 // ppk
41 $batch = $exe . ' genrsa -des3 -passout ' . $password . ' -out ' . $ppkPath . ' 2048 -noout -config ' . $conf. PHP_EOL;
42 $batch .= 'IF %ERRORLEVEL% GEQ 1 GOTO EOF' . PHP_EOL . PHP_EOL;
43
44 // pub
45 $batch .= $exe . ' rsa -in ' . $ppkPath . ' -passin ' . $password . ' -out ' . $pubPath . PHP_EOL . PHP_EOL;
46 $batch .= 'IF %ERRORLEVEL% GEQ 1 GOTO EOF' . PHP_EOL . PHP_EOL;
47
48 // crt
49 $batch .= $exe . ' req -x509 -nodes -sha256 -new -key ' . $pubPath . ' -out ' . $crtPath . ' -passin ' . $password;
50 $batch .= ' -subj ' . $subject . ' -reqexts ' . $extension . ' -extensions ' . $extension . ' -config ' . $conf. PHP_EOL;
51 $batch .= 'IF %ERRORLEVEL% GEQ 1 GOTO EOF' . PHP_EOL . PHP_EOL;
52
53 $batch .= ':EOF' . PHP_EOL;
54 $batch .= 'SET RESULT=KO' . PHP_EOL;
55 $batch .= 'IF EXIST ' . $pubPath . ' IF EXIST ' . $crtPath . ' SET RESULT=OK' . PHP_EOL;
56 $batch .= 'ECHO %RESULT%';
57
58 $result = Batch::exec('createCertificate', $batch);
59 return isset($result[0]) && $result[0] == 'OK';
60 }
$result
global $bearsamppRoot
global $bearsamppCore
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
static random($length=32, $withNumeric=true)

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

◆ existsCrt()

OpenSsl::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 68 of file class.openssl.php.

69 {
70 global $bearsamppRoot;
71
72 $ppkPath = $bearsamppRoot->getSslPath() . '/' . $name . '.ppk';
73 $pubPath = $bearsamppRoot->getSslPath() . '/' . $name . '.pub';
74 $crtPath = $bearsamppRoot->getSslPath() . '/' . $name . '.crt';
75
76 return is_file($ppkPath) && is_file($pubPath) && is_file($crtPath);
77 }

References $bearsamppRoot.

◆ removeCrt()

OpenSsl::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 85 of file class.openssl.php.

86 {
87 global $bearsamppRoot;
88
89 $ppkPath = $bearsamppRoot->getSslPath() . '/' . $name . '.ppk';
90 $pubPath = $bearsamppRoot->getSslPath() . '/' . $name . '.pub';
91 $crtPath = $bearsamppRoot->getSslPath() . '/' . $name . '.crt';
92
93 return @unlink($ppkPath) && @unlink($pubPath) && @unlink($crtPath);
94 }

References $bearsamppRoot.


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