2024.8.23
Loading...
Searching...
No Matches
class.openssl.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
11{
12 /**
13 * Creates a certificate with the specified name and destination path.
14 *
15 * @param string $name The name of the certificate.
16 * @param string|null $destPath The destination path where the certificate files will be saved. If null, the default SSL path is used.
17 * @return bool True if the certificate was created successfully, false otherwise.
18 */
19 public function createCrt($name, $destPath = null)
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 }
61
62 /**
63 * Checks if a certificate with the specified name exists.
64 *
65 * @param string $name The name of the certificate.
66 * @return bool True if the certificate exists, false otherwise.
67 */
68 public function existsCrt($name)
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 }
78
79 /**
80 * Removes a certificate with the specified name.
81 *
82 * @param string $name The name of the certificate.
83 * @return bool True if the certificate was removed successfully, false otherwise.
84 */
85 public function removeCrt($name)
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 }
95}
$result
global $bearsamppRoot
global $bearsamppCore
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
createCrt($name, $destPath=null)
removeCrt($name)
existsCrt($name)
static random($length=32, $withNumeric=true)