-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyHelper.php
219 lines (192 loc) · 6.86 KB
/
KeyHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace Drupal\os2web_key;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\key\KeyInterface;
use Drupal\os2web_key\Exception\RuntimeException;
use Drupal\os2web_key\Plugin\KeyType\CertificateKeyType;
use Drupal\os2web_key\Plugin\KeyType\OidcKeyType;
use Psr\Log\LoggerAwareTrait;
/**
* Key helper.
*/
class KeyHelper {
use DependencySerializationTrait;
use LoggerAwareTrait;
public function __construct(
LoggerChannelInterface $logger,
) {
$this->setLogger($logger);
}
/**
* Get certificates from a key.
*
* @param \Drupal\key\KeyInterface $key
* The key.
*
* @return array{cert: string, pkey: string}
* The certificates.
*/
public function getCertificates(KeyInterface $key): array {
$type = $key->getKeyType();
if (!($type instanceof CertificateKeyType)) {
throw $this->createSslRuntimeException(sprintf('Invalid key type: %s', $type::class), $key);
}
// Check whether conversion is needed.
if ($type->getInputFormat() === $type->getOutputFormat()) {
return $this->parseCertificates($key->getKeyValue(), $type->getInputFormat(), $type->getPassphrase(), $key);
}
$parsedCertificates = $this->parseCertificates($key->getKeyValue(), $type->getInputFormat(), $type->getPassphrase(), $key);
$convertedCertificates = $this->convertCertificates($parsedCertificates, $type->getOutputFormat(), $key);
return $this->parseCertificates($convertedCertificates, $type->getOutputFormat(), $type->getPassphrase(), $key);
}
/**
* Get OIDC values from a key.
*
* @param \Drupal\key\KeyInterface $key
* The key.
*
* @return array{discovery_url: string, client_id: string, client_secret: string}
* The OIDC values.
*/
public function getOidcValues(KeyInterface $key): array {
$type = $key->getKeyType();
if (!($type instanceof OidcKeyType)) {
throw $this->createSslRuntimeException(sprintf('Invalid key type: %s', $type::class), $key);
}
$contents = $key->getKeyValue();
try {
$values = json_decode($contents, TRUE, 512, JSON_THROW_ON_ERROR);
foreach ([
OidcKeyType::DISCOVERY_URL,
OidcKeyType::CLIENT_ID,
OidcKeyType::CLIENT_SECRET,
] as $name) {
if (!isset($values[$name])) {
throw $this->createRuntimeException(sprintf("Missing OIDC value: %s", $name), $key);
}
}
return $values;
}
catch (\JsonException $e) {
throw $this->createRuntimeException(sprintf("Cannot get OIDC values: %s", $e->getMessage()), $key);
}
}
/**
* Parse certificates.
*
* @return array{cert: string, pkey: string}
* The certificates, note that the certificate has no passphrase.
*/
public function parseCertificates(
string $contents,
string $format,
?string $passphrase,
?KeyInterface $key,
): array {
$certificates = [
CertificateKeyType::CERT => NULL,
CertificateKeyType::PKEY => NULL,
];
switch ($format) {
case CertificateKeyType::FORMAT_PFX:
if (!openssl_pkcs12_read($contents, $certificates, $passphrase)) {
throw $this->createSslRuntimeException('Error reading certificate', $key);
}
break;
case CertificateKeyType::FORMAT_PEM:
$certificate = @openssl_x509_read($contents);
if (FALSE === $certificate) {
throw $this->createSslRuntimeException('Error reading certificate', $key);
}
if (!@openssl_x509_export($certificate, $certificates[CertificateKeyType::CERT])) {
throw $this->createSslRuntimeException('Error exporting x509 certificate', $key);
}
$pkey = @openssl_pkey_get_private($contents, $passphrase);
if (FALSE === $pkey) {
throw $this->createSslRuntimeException('Error reading private key', $key);
}
if (!@openssl_pkey_export($pkey, $certificates[CertificateKeyType::PKEY])) {
throw $this->createSslRuntimeException('Error exporting private key', $key);
}
break;
}
if (!isset($certificates[CertificateKeyType::CERT], $certificates[CertificateKeyType::PKEY])) {
throw $this->createRuntimeException("Cannot read certificate parts CertificateKeyType::CERT and CertificateKeyType::PKEY", $key);
}
return $certificates;
}
/**
* Converts certificates to format.
*
* @param array $certificates
* Output from parseCertificates.
* @param string $format
* Format to convert into.
* @param ?KeyInterface $key
* The key, for debugging purposes.
*
* @return string
* The converted certificate.
*
* @see self::parseCertificates()
*/
public function convertCertificates(array $certificates, string $format, ?KeyInterface $key): string {
$cert = $certificates[CertificateKeyType::CERT] ?? NULL;
if (!isset($cert)) {
throw $this->createRuntimeException('Certificate part "cert" not found', $key);
}
$pkey = $certificates[CertificateKeyType::PKEY] ?? NULL;
if (!isset($pkey)) {
throw $this->createRuntimeException('Certificate part "pkey" not found', $key);
}
$output = '';
switch ($format) {
case CertificateKeyType::FORMAT_PEM:
$parts = ['', ''];
if (!@openssl_x509_export($cert, $parts[0])) {
throw $this->createSslRuntimeException('Cannot export certificate', $key);
}
if (!@openssl_pkey_export($pkey, $parts[1])) {
throw $this->createSslRuntimeException('Cannot export private key', $key);
}
$output = implode('', $parts);
break;
case CertificateKeyType::FORMAT_PFX:
if (!@openssl_pkcs12_export($cert, $output, $pkey, '')) {
throw $this->createSslRuntimeException('Cannot export certificate', $key);
}
break;
default:
throw $this->createSslRuntimeException(sprintf('Invalid format: %s', $format), $key);
}
return $output;
}
/**
* Create a runtime exception.
*/
public function createRuntimeException(string $message, ?KeyInterface $key, ?string $sslError = NULL): RuntimeException {
if (NULL !== $sslError) {
$message .= ' (' . $sslError . ')';
}
// @fixme Error: Typed property …::$logger must not be accessed before initialization.
if (isset($this->logger)) {
$this->logger->error('@key: @message', [
'@key' => $key?->id(),
'@message' => $message,
]);
}
return new RuntimeException($message);
}
/**
* Create an SSL runtime exception.
*/
public function createSslRuntimeException(string $message, ?KeyInterface $key): RuntimeException {
// @see https://www.php.net/manual/en/function.openssl-error-string.php.
$sslError = NULL;
while ($errorMessage = openssl_error_string()) {
$sslError = $errorMessage;
}
return $this->createRuntimeException($message, $key, $sslError);
}
}