-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathUtils.php
More file actions
127 lines (112 loc) · 4.8 KB
/
Utils.php
File metadata and controls
127 lines (112 loc) · 4.8 KB
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
<?php declare(strict_types=1);
/*
* This file is part of the WebPush library.
*
* (c) Louis Lagrange <lagrange.louis@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Minishlink\WebPush;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\Ecc\PublicKey;
class Utils
{
public static function safeStrlen(string $value): int
{
return mb_strlen($value, '8bit');
}
public static function serializePublicKey(PublicKey $publicKey): string
{
$hexString = '04';
$point = $publicKey->getPoint();
$hexString .= str_pad($point->getX()->toBase(16), 64, '0', STR_PAD_LEFT);
$hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT);
return $hexString;
}
public static function serializePublicKeyFromJWK(JWK $jwk): string
{
$hexString = '04';
$hexString .= str_pad(bin2hex(Base64UrlSafe::decode($jwk->get('x'))), 64, '0', STR_PAD_LEFT);
$hexString .= str_pad(bin2hex(Base64UrlSafe::decode($jwk->get('y'))), 64, '0', STR_PAD_LEFT);
return $hexString;
}
public static function unserializePublicKey(string $data): array
{
$data = bin2hex($data);
if (mb_substr($data, 0, 2, '8bit') !== '04') {
throw new \InvalidArgumentException('Invalid data: only uncompressed keys are supported.');
}
$data = mb_substr($data, 2, null, '8bit');
$dataLength = self::safeStrlen($data);
return [
hex2bin(mb_substr($data, 0, $dataLength / 2, '8bit')),
hex2bin(mb_substr($data, $dataLength / 2, null, '8bit')),
];
}
/**
* Generates user warning/notice if some requirements are not met.
* Does not throw exception to allow unusual or polyfill environments.
*/
public static function checkRequirement(): void
{
self::checkRequirementExtension();
self::checkRequirementKeyCipherHash();
}
public static function checkRequirementExtension(): void
{
$requiredExtensions = [
'curl' => '[WebPush] curl extension is not loaded but is required. You can fix this in your php.ini.',
'mbstring' => '[WebPush] mbstring extension is not loaded but is required for sending push messages with payload or for VAPID authentication. You can fix this in your php.ini.',
'openssl' => '[WebPush] openssl extension is not loaded but is required for sending push messages with payload or for VAPID authentication. You can fix this in your php.ini.',
];
foreach ($requiredExtensions as $extension => $message) {
if (!extension_loaded($extension)) {
trigger_error($message, E_USER_WARNING);
}
}
// Check optional extensions.
if (!extension_loaded('bcmath') && !extension_loaded('gmp')) {
trigger_error('It is highly recommended to install the GMP or BCMath extension to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.', E_USER_NOTICE);
}
}
public static function checkRequirementKeyCipherHash(): void
{
// Print your current openssl version with: OPENSSL_VERSION_TEXT
// Check for outdated openssl without EC support.
$requiredCurves = [
'prime256v1' => '[WebPush] Openssl does not support required curve prime256v1.',
];
$availableCurves = openssl_get_curve_names();
if ($availableCurves === false) {
trigger_error('[WebPush] Openssl does not support curves.', E_USER_WARNING);
} else {
foreach ($requiredCurves as $curve => $message) {
if (!in_array($curve, $availableCurves, true)) {
trigger_error($message, E_USER_WARNING);
}
}
}
// Check for unusual openssl without cipher support.
$requiredCiphers = [
'aes-128-gcm' => '[WebPush] Openssl does not support required cipher aes-128-gcm.',
];
$availableCiphers = openssl_get_cipher_methods();
foreach ($requiredCiphers as $cipher => $message) {
if (!in_array($cipher, $availableCiphers, true)) {
trigger_error($message, E_USER_WARNING);
}
}
// Check for unusual php without hash algo support.
$requiredHash = [
'sha256' => '[WebPush] Php does not support required hmac hash sha256.',
];
$availableHash = hash_hmac_algos();
foreach ($requiredHash as $hash => $message) {
if (!in_array($hash, $availableHash, true)) {
trigger_error($message, E_USER_WARNING);
}
}
}
}