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