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