|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Utils; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Collection of Utility functions specifically for certificates |
|
11
|
|
|
*/ |
|
12
|
|
|
class Certificate |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The pattern that the contents of a certificate should adhere to |
|
16
|
|
|
*/ |
|
17
|
|
|
public const PUBLIC_KEY_PATTERN = '/^-----BEGIN CERTIFICATE-----([^-]*)^-----END CERTIFICATE-----/m'; |
|
18
|
|
|
public const PRIVATE_KEY_PATTERN = '/^-----BEGIN RSA PRIVATE KEY-----([^-]*)^-----END RSA PRIVATE KEY-----/m'; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $certificate |
|
23
|
|
|
* @param string $pattern |
|
24
|
|
|
* |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function hasValidStructure(string $certificate, string $pattern = self::PUBLIC_KEY_PATTERN): bool |
|
28
|
|
|
{ |
|
29
|
|
|
return !!preg_match($pattern, $certificate); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $X509CertificateContents |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function convertToCertificate(string $X509CertificateContents): string |
|
39
|
|
|
{ |
|
40
|
|
|
return "-----BEGIN CERTIFICATE-----\n" |
|
41
|
|
|
. chunk_split($X509CertificateContents, 64, "\n") |
|
42
|
|
|
. "-----END CERTIFICATE-----"; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $key The PEM-encoded key |
|
48
|
|
|
* @param string $pattern The pattern to use |
|
49
|
|
|
* @return string The stripped key |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function stripHeaders(string $key, string $pattern = self::PUBLIC_KEY_PATTERN) |
|
52
|
|
|
{ |
|
53
|
|
|
$matches = []; |
|
54
|
|
|
$result = preg_match($pattern, $key, $matches); |
|
55
|
|
|
if ($result === false) { |
|
56
|
|
|
throw new Exception('Could not find content matching the provided pattern.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @psalm-suppress EmptyArrayAccess */ |
|
60
|
|
|
return preg_replace('/\s+/', '', $matches[1]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array|string $issuer |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function parseIssuer($issuer): string |
|
70
|
|
|
{ |
|
71
|
|
|
if (is_array($issuer)) { |
|
72
|
|
|
$parts = []; |
|
73
|
|
|
foreach ($issuer as $key => $value) { |
|
74
|
|
|
array_unshift($parts, $key . '=' . $value); |
|
75
|
|
|
} |
|
76
|
|
|
return implode(',', $parts); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $issuer; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $subject |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function parseSubject(array $subject): string |
|
89
|
|
|
{ |
|
90
|
|
|
$parts = []; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($subject as $key => $value) { |
|
93
|
|
|
if (is_array($value)) { |
|
94
|
|
|
foreach ($value as $valueElement) { |
|
95
|
|
|
array_unshift($parts, $key . '=' . $valueElement); |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
|
|
array_unshift($parts, $key . '=' . $value); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return implode(',', $parts); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|