Bearsampp 2025.8.29
Loading...
Searching...
No Matches
class.openssl.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
12{
20 public function createCrt($name, $destPath = null)
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 }
70
77 public function existsCrt($name)
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 }
87
94 public function removeCrt($name)
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 }
104}
$result
global $bearsamppRoot
global $bearsamppCore
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
createCrt($name, $destPath=null)
existsCrt($name)
removeCrt($name)
static logTrace($data, $file=null)
static random($length=32, $withNumeric=true)