1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\TestUtils; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use SimpleSAML\XMLSecurity\Key\PrivateKey; |
9
|
|
|
use SimpleSAML\XMLSecurity\Key\PublicKey; |
10
|
|
|
use SimpleSAML\XMLSecurity\Key\X509Certificate; |
11
|
|
|
use SimpleSAML\XMLSecurity\Utils\Certificate as CertificateUtils; |
12
|
|
|
|
13
|
|
|
use function dirname; |
14
|
|
|
use function file_get_contents; |
15
|
|
|
use function preg_match; |
16
|
|
|
use function preg_replace; |
17
|
|
|
use function trim; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class \SimpleSAML\TestUtils\PEMCertificatesMock |
21
|
|
|
*/ |
22
|
|
|
class PEMCertificatesMock |
23
|
|
|
{ |
24
|
|
|
public const CERTS_DIR = 'resources/certificates'; |
25
|
|
|
public const KEYS_DIR = 'resources/keys'; |
26
|
|
|
public const PASSPHRASE = '1234'; |
27
|
|
|
|
28
|
|
|
public const CERTIFICATE = 'signed.simplesamlphp.org.crt'; |
29
|
|
|
public const PUBLIC_KEY = 'signed.simplesamlphp.org.pub'; |
30
|
|
|
public const PRIVATE_KEY = 'signed.simplesamlphp.org.key'; |
31
|
|
|
public const OTHER_CERTIFICATE = 'other.simplesamlphp.org.crt'; |
32
|
|
|
public const OTHER_PUBLIC_KEY = 'other.simplesamlphp.org.pub'; |
33
|
|
|
public const OTHER_PRIVATE_KEY = 'other.simplesamlphp.org.key'; |
34
|
|
|
public const SELFSIGNED_CERTIFICATE = 'selfsigned.simplesamlphp.org.crt'; |
35
|
|
|
public const SELFSIGNED_PUBLIC_KEY = 'selfsigned.simplesamlphp.org.pub'; |
36
|
|
|
public const SELFSIGNED_PRIVATE_KEY = 'selfsigned.simplesamlphp.org.key'; |
37
|
|
|
public const BROKEN_CERTIFICATE = 'broken.simplesamlphp.org.crt'; |
38
|
|
|
public const BROKEN_PUBLIC_KEY = 'broken.simplesamlphp.org.pub'; |
39
|
|
|
public const BROKEN_PRIVATE_KEY = 'broken.simplesamlphp.org.key'; |
40
|
|
|
public const CORRUPTED_CERTIFICATE = 'corrupted.simplesamlphp.org.crt'; |
41
|
|
|
public const CORRUPTED_PUBLIC_KEY = 'corrupted.simplesamlphp.org.pub'; |
42
|
|
|
public const CORRUPTED_PRIVATE_KEY = 'corrupted.simplesamlphp.org.key'; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $file The file to use |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function buildKeysPath(string $file): string |
50
|
|
|
{ |
51
|
|
|
$base = dirname(__FILE__, 3); |
52
|
|
|
return 'file://' . $base . DIRECTORY_SEPARATOR . self::KEYS_DIR . DIRECTORY_SEPARATOR . $file; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $file The file to use |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public static function buildCertsPath(string $file): string |
61
|
|
|
{ |
62
|
|
|
$base = dirname(__FILE__, 3); |
63
|
|
|
return 'file://' . $base . DIRECTORY_SEPARATOR . self::CERTS_DIR . DIRECTORY_SEPARATOR . $file; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $file The file we should load |
69
|
|
|
* @return string The file contents |
70
|
|
|
*/ |
71
|
|
|
public static function loadPlainCertificateFile(string $file): string |
72
|
|
|
{ |
73
|
|
|
return trim(file_get_contents(self::buildCertsPath($file))); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $file The file we should load |
79
|
|
|
* @return string The file contents |
80
|
|
|
*/ |
81
|
|
|
public static function loadPlainKeyFile(string $file): string |
82
|
|
|
{ |
83
|
|
|
return trim(file_get_contents(self::buildKeysPath($file))); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $file The file to use |
89
|
|
|
* @return \SimpleSAML\XMLSecurity\Key\X509Certificate |
90
|
|
|
*/ |
91
|
|
|
public static function getCertificate(string $file): X509Certificate |
92
|
|
|
{ |
93
|
|
|
$path = self::buildCertsPath($file); |
94
|
|
|
return X509Certificate::fromFile($path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $file The file to use |
100
|
|
|
* @return \SimpleSAML\XMLSecurity\Key\PublicKey |
101
|
|
|
*/ |
102
|
|
|
public static function getPublicKey(string $file): PublicKey |
103
|
|
|
{ |
104
|
|
|
$path = self::buildKeysPath($file); |
105
|
|
|
return PublicKey::fromFile($path); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $file The file to use |
111
|
|
|
* @param string $passphrase The passphrase to use |
112
|
|
|
* @return \SimpleSAML\XMLSecurity\Key\PrivateKey |
113
|
|
|
*/ |
114
|
|
|
public static function getPrivateKey(string $file, string $passphrase = self::PASSPHRASE): PrivateKey |
115
|
|
|
{ |
116
|
|
|
$path = self::buildKeysPath($file); |
117
|
|
|
return PrivateKey::fromFile($path, $passphrase); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $file The file to use |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public static function getPlainCertificate( |
126
|
|
|
string $file = self::CERTIFICATE |
127
|
|
|
): string { |
128
|
|
|
return self::loadPlainCertificateFile($file); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $file The file to use |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public static function getPlainPublicKey( |
137
|
|
|
string $file = self::PUBLIC_KEY |
138
|
|
|
): string { |
139
|
|
|
return self::loadPlainKeyFile($file); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $file The file to use |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public static function getPlainPrivateKey( |
148
|
|
|
string $file = self::PRIVATE_KEY |
149
|
|
|
): string { |
150
|
|
|
return self::loadPlainKeyFile($file); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $file The file to use |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public static function getPlainCertificateContents( |
159
|
|
|
string $file = self::CERTIFICATE |
160
|
|
|
): string { |
161
|
|
|
return CertificateUtils::stripHeaders( |
162
|
|
|
self::loadPlainCertificateFile($file), |
163
|
|
|
CertificateUtils::CERTIFICATE_PATTERN |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $file The file to use |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public static function getPlainPublicKeyContents( |
173
|
|
|
string $file = self::PUBLIC_KEY |
174
|
|
|
): string { |
175
|
|
|
return CertificateUtils::stripHeaders( |
176
|
|
|
self::loadPlainKeyFile($file), |
177
|
|
|
CertificateUtils::PUBLIC_KEY_PATTERN |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $file The file to use |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public static function getPlainPrivateKeyContents( |
187
|
|
|
string $file = self::PRIVATE_KEY |
188
|
|
|
): string { |
189
|
|
|
return CertificateUtils::stripHeaders( |
190
|
|
|
self::loadPlainCertificateFile($file), |
191
|
|
|
CertificateUtils::PRIVATE_KEY_PATTERN |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|