/
CaRootPemBundle.php
143 lines (121 loc) · 4.01 KB
/
CaRootPemBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* This file is part of Sslurp.
* https://github.com/EvanDotPro/Sslurp
*
* (c) Evan Coury <me@evancoury.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sslurp;
use SplFileObject;
class CaRootPemBundle extends AbstractCaRootData
{
/**
* @var SplFileObject
*/
protected $fileObject = null;
/**
* @var string
*/
protected $pemContent = null;
/**
* @var MozillaCertData
*/
protected $mozCertData = null;
public function __construct($filename = null, MozillaCertData $mozCertData = null)
{
$this->fileObject = new SplFileObject($filename, 'r+');
$this->mozCertData = $mozCertData ?: new MozillaCertData();
}
/**
* Return the content of the PEM bundle
*/
public function getContent($until = false)
{
if ($this->pemContent === null) {
$this->pemContent = '';
while (!$this->fileObject->eof()) $this->pemContent .= $this->fileObject->fgets();
//$this->pemContent = $this->fileObject->getUpdatedCaRootBundle();
}
if ($until) {
return substr($this->pemContent, 0, strpos($this->pemContent, "\n", strpos($this->pemContent, $until)));
}
return $this->pemContent;
}
public function getMozillaCertData()
{
return $this->mozCertData;
}
public function isLatest()
{
return $this->getVersion() === $this->mozCertData->getVersion();
}
public function getUpdatedCaRootBundle()
{
return $this->buildBundle($this->mozCertData->getContent());
}
protected function buildBundle($rawCertData)
{
$rawCertData = explode("\n", $rawCertData);
$currentDate = date(DATE_ISO8601);
$caBundle = <<<EOT
##
## Bundle of CA Root Certificates
##
## Generated with Sslurp (https://github.com/EvanDotPro/Sslurp)
##
## This is a bundle of X.509 certificates of public Certificate Authorities.
## These were automatically extracted from Mozilla's root certificates
## file (certdata.txt). This file can be found in the Mozilla source tree:
## /mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt
##
## http://www.mozilla.org/projects/security/certs/policy/
## http://www.mozilla.org/projects/security/pki/nss/
##
## This file contains the certificates in PEM format and therefore
## can be directly used with curl / libcurl / php_curl, or with
## an Apache+mod_ssl webserver for SSL client authentication.
## Just configure this file as the SSLCACertificateFile.
##
EOT;
$caName = '';
while (($line = array_shift($rawCertData)) !== null) {
if (preg_match('/^#|^\s*$/', $line)) {
continue;
}
$line = rtrim($line);
if (preg_match('/^CVS_ID\s+\"(.*)\"/', $line, $match)) {
$caBundle .= "# {$match[1]}\n";
}
if (preg_match('/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/', $line, $match)) {
$caName = $match[1];
}
if (preg_match('/^CKA_VALUE MULTILINE_OCTAL/', $line)) {
$data = '';
while ($line = array_shift($rawCertData)) {
if (preg_match('/^END/', $line)) {
break;
}
$line = rtrim($line);
$octets = explode('\\', $line);
array_shift($octets);
foreach ($octets as $oct) {
$data .= chr(octdec($oct));
}
}
$caBundle .= $this->buildPemString($caName, $data);
}
}
return $caBundle;
}
protected function buildPemString($caName, $data)
{
return "\n{$caName}\n"
. str_repeat('=', strlen($caName)) . "\n"
. "-----BEGIN CERTIFICATE-----\n"
. chunk_split(base64_encode($data), 76, "\n")
. "-----END CERTIFICATE-----\n";
}
}