1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Bip70\X509; |
6
|
|
|
|
7
|
|
|
use Bip70\Exception\Bip70Exception; |
8
|
|
|
use Bip70\X509\Exception\TrustStoreException; |
9
|
|
|
use Composer\CaBundle\CaBundle; |
10
|
|
|
use Sop\CryptoEncoding\PEM; |
11
|
|
|
use Sop\CryptoEncoding\PEMBundle; |
12
|
|
|
use X509\Certificate\Certificate; |
13
|
|
|
use X509\Certificate\CertificateBundle; |
14
|
|
|
|
15
|
|
|
class TrustStoreLoader |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Loads the currently installed ca-certificates file. |
19
|
|
|
* The approach taken by composer/ca-bundle is to attempt |
20
|
|
|
* to load certificates if openssl-like envvars and |
21
|
|
|
* config values are set. |
22
|
|
|
* |
23
|
|
|
* If $allowFallback is false, and the bundled composer |
24
|
|
|
* ca file is returned, an exception will be thrown. |
25
|
|
|
* |
26
|
|
|
* @see CaBundle::getSystemCaRootBundlePath() |
27
|
|
|
* @param bool $allowFallback |
28
|
|
|
* @return CertificateBundle |
29
|
|
|
*/ |
30
|
10 |
|
public static function fromSystem(bool $allowFallback = true): CertificateBundle |
31
|
|
|
{ |
32
|
10 |
|
$rootBundlePath = CaBundle::getSystemCaRootBundlePath(); |
33
|
|
|
|
34
|
10 |
|
if (!$allowFallback && CaBundle::getBundledCaBundlePath() === $rootBundlePath) { |
35
|
1 |
|
throw new TrustStoreException("Fallback to composer ca-bundle is disabled - you should install the ca-certificates package"); |
36
|
|
|
} |
37
|
|
|
|
38
|
9 |
|
if (is_dir($rootBundlePath)) { |
39
|
1 |
|
return self::fromDirectory($rootBundlePath); |
40
|
|
|
} else { |
41
|
8 |
|
return self::fromFile($rootBundlePath); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Loads a trust store completely controlled by what is |
47
|
|
|
* included in the `composer/ca-bundle` package. |
48
|
|
|
* @return CertificateBundle |
49
|
|
|
*/ |
50
|
1 |
|
public static function fromComposerBundle(): CertificateBundle |
51
|
|
|
{ |
52
|
1 |
|
return self::fromFile(CaBundle::getBundledCaBundlePath()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Load a trust store from a pem bundle file (can contain |
57
|
|
|
* multiple certificates) |
58
|
|
|
* |
59
|
|
|
* @param string $file |
60
|
|
|
* @return CertificateBundle |
61
|
|
|
* @throws Bip70Exception |
62
|
|
|
*/ |
63
|
11 |
|
public static function fromFile(string $file): CertificateBundle |
64
|
|
|
{ |
65
|
11 |
|
$pemBundle = PEMBundle::fromFile($file); |
66
|
11 |
|
$certificates = []; |
67
|
11 |
|
foreach ($pemBundle as $pem) { |
68
|
|
|
try { |
69
|
11 |
|
$certificate = Certificate::fromPEM($pem); |
70
|
11 |
|
$certificates[] = $certificate; |
71
|
11 |
|
} catch (\Exception $e) { |
72
|
|
|
// some files are invalid.. I guess? |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
11 |
|
if (count($certificates) < 1) { |
77
|
|
|
throw new TrustStoreException("No certificates in file"); |
78
|
|
|
} |
79
|
|
|
|
80
|
11 |
|
return new CertificateBundle(...$certificates); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Load a trust store from a pem bundle file (can contain |
85
|
|
|
* multiple certificates) |
86
|
|
|
* |
87
|
|
|
* @param string $dir |
88
|
|
|
* @return CertificateBundle |
89
|
|
|
* @throws Bip70Exception |
90
|
|
|
*/ |
91
|
4 |
|
public static function fromDirectory(string $dir): CertificateBundle |
92
|
|
|
{ |
93
|
4 |
|
if (!is_dir($dir)) { |
94
|
1 |
|
throw new TrustStoreException("Invalid path passed to fromDirectory, is not a directory"); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
$certificates = []; |
98
|
3 |
|
foreach (glob("$dir/*.pem") as $pemFile) { |
99
|
|
|
try { |
100
|
2 |
|
$pem = PEM::fromFile($pemFile); |
101
|
2 |
|
$certificate = Certificate::fromPEM($pem); |
102
|
2 |
|
$certificates[] = $certificate; |
103
|
2 |
|
} catch (\Exception $e) { |
104
|
|
|
// some files are invalid.. I guess? |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
if (count($certificates) < 1) { |
109
|
1 |
|
throw new TrustStoreException("No PEM files in directory"); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
return new CertificateBundle(...$certificates); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|