|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gameap\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
|
6
|
|
|
|
|
7
|
|
|
class CertificateService |
|
8
|
|
|
{ |
|
9
|
|
|
const ROOT_CA = 'root.crt'; |
|
10
|
|
|
const ROOT_KEY = 'root.key'; |
|
11
|
|
|
|
|
12
|
|
|
const CERT_DN = [ |
|
13
|
|
|
"countryName" => "RU", |
|
14
|
|
|
"stateOrProvinceName" => "GameAP", |
|
15
|
|
|
"localityName" => "GameAP", |
|
16
|
|
|
"organizationName" => "GameAP.ru", |
|
17
|
|
|
"organizationalUnitName" => "Development", |
|
18
|
|
|
"commonName" => "GameAP", |
|
19
|
|
|
"emailAddress" => "[email protected]" |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
const CERT_DAYS = 3650; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Generate root certificate |
|
26
|
|
|
*/ |
|
27
|
|
|
static public function generateRoot() |
|
28
|
|
|
{ |
|
29
|
|
|
$privateKey = openssl_pkey_new(); |
|
30
|
|
|
$certificate = openssl_csr_new(self::CERT_DN, $privateKey); |
|
31
|
|
|
$certificate = openssl_csr_sign($certificate, null, $privateKey, 3650); |
|
32
|
|
|
|
|
33
|
|
|
openssl_x509_export($certificate, $pemCertificate); |
|
34
|
|
|
openssl_pkey_export($privateKey, $pemPrivateKey); |
|
35
|
|
|
|
|
36
|
|
|
Storage::put(self::ROOT_CA, $pemCertificate); |
|
37
|
|
|
Storage::put(self::ROOT_KEY, $pemPrivateKey); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $certificatePath string path to certificate in storage |
|
42
|
|
|
* @param $keyPath string path to key in storage |
|
43
|
|
|
*/ |
|
44
|
|
|
static public function generate($certificatePath, $keyPath) |
|
45
|
|
|
{ |
|
46
|
|
|
$privateKey = openssl_pkey_new(); |
|
47
|
|
|
$certificate = openssl_csr_new(self::CERT_DN, $privateKey); |
|
48
|
|
|
|
|
49
|
|
|
if (!Storage::exists(self::ROOT_CA)) { |
|
50
|
|
|
self::generateRoot(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$rootCa = Storage::get(self::ROOT_CA); |
|
54
|
|
|
$rootKey = Storage::get(self::ROOT_KEY); |
|
55
|
|
|
$certificate = openssl_csr_sign($certificate, $rootCa, $rootKey, 3650); |
|
56
|
|
|
|
|
57
|
|
|
openssl_x509_export($certificate, $pemCertificate); |
|
58
|
|
|
openssl_pkey_export($privateKey, $pemPrivateKey); |
|
59
|
|
|
|
|
60
|
|
|
Storage::makeDirectory('client_certificates'); |
|
61
|
|
|
|
|
62
|
|
|
Storage::put($certificatePath, $pemCertificate); |
|
63
|
|
|
Storage::put($keyPath, $pemPrivateKey); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $certificatePath string path to certificate in storage |
|
68
|
|
|
*/ |
|
69
|
|
|
static public function signCertificate($certificatePath) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!Storage::exists(self::ROOT_CA)) { |
|
72
|
|
|
self::generateRoot(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$rootCa = Storage::get(self::ROOT_CA); |
|
76
|
|
|
$rootKey = Storage::get(self::ROOT_KEY); |
|
77
|
|
|
$certificate = Storage::get($certificatePath); |
|
78
|
|
|
|
|
79
|
|
|
$signedCertificate = openssl_csr_sign($certificate, $rootCa, $rootKey, 3650); |
|
80
|
|
|
openssl_x509_export($signedCertificate, $pemCertificate); |
|
81
|
|
|
$pathinfo = pathinfo($certificatePath); |
|
82
|
|
|
|
|
83
|
|
|
Storage::put($pathinfo['dirname'] . '/' . $pathinfo['filename'] . 'crt', $pemCertificate); |
|
84
|
|
|
} |
|
85
|
|
|
} |