@@ -27,19 +27,19 @@ class Encryption
2727 * @return string padded payload (plaintext)
2828 * @throws \ErrorException
2929 */
30- public static function padPayload (string $ payload , int $ maxLengthToPad , string $ contentEncoding ): string
30+ public static function padPayload (string $ payload , int $ maxLengthToPad , ContentEncoding $ contentEncoding ): string
3131 {
3232 $ payloadLen = Utils::safeStrlen ($ payload );
3333 $ padLen = $ maxLengthToPad ? $ maxLengthToPad - $ payloadLen : 0 ;
3434
35- if ($ contentEncoding === " aesgcm " ) {
35+ if ($ contentEncoding === ContentEncoding:: aesgcm) {
3636 return pack ('n* ' , $ padLen ).str_pad ($ payload , $ padLen + $ payloadLen , chr (0 ), STR_PAD_LEFT );
3737 }
38- if ($ contentEncoding === " aes128gcm " ) {
38+ if ($ contentEncoding === ContentEncoding:: aes128gcm) {
3939 return str_pad ($ payload .chr (2 ), $ padLen + $ payloadLen , chr (0 ), STR_PAD_RIGHT );
4040 }
4141
42- throw new \ErrorException ("This content encoding is not supported " );
42+ throw new \ErrorException ("This content encoding is not implemented. " );
4343 }
4444
4545 /**
@@ -49,7 +49,7 @@ public static function padPayload(string $payload, int $maxLengthToPad, string $
4949 *
5050 * @throws \ErrorException
5151 */
52- public static function encrypt (string $ payload , string $ userPublicKey , string $ userAuthToken , string $ contentEncoding ): array
52+ public static function encrypt (string $ payload , string $ userPublicKey , string $ userAuthToken , ContentEncoding $ contentEncoding ): array
5353 {
5454 return self ::deterministicEncrypt (
5555 $ payload ,
@@ -64,8 +64,14 @@ public static function encrypt(string $payload, string $userPublicKey, string $u
6464 /**
6565 * @throws \RuntimeException
6666 */
67- public static function deterministicEncrypt (string $ payload , string $ userPublicKey , string $ userAuthToken , string $ contentEncoding , array $ localKeyObject , string $ salt ): array
68- {
67+ public static function deterministicEncrypt (
68+ string $ payload ,
69+ string $ userPublicKey ,
70+ string $ userAuthToken ,
71+ ContentEncoding $ contentEncoding ,
72+ array $ localKeyObject ,
73+ string $ salt
74+ ): array {
6975 $ userPublicKey = Base64Url::decode ($ userPublicKey );
7076 $ userAuthToken = Base64Url::decode ($ userAuthToken );
7177
@@ -112,7 +118,7 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
112118 $ context = self ::createContext ($ userPublicKey , $ localPublicKey , $ contentEncoding );
113119
114120 // derive the Content Encryption Key
115- $ contentEncryptionKeyInfo = self ::createInfo ($ contentEncoding , $ context , $ contentEncoding );
121+ $ contentEncryptionKeyInfo = self ::createInfo ($ contentEncoding-> value , $ context , $ contentEncoding );
116122 $ contentEncryptionKey = self ::hkdf ($ salt , $ ikm , $ contentEncryptionKeyInfo , 16 );
117123
118124 // section 3.3, derive the nonce
@@ -132,16 +138,19 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
132138 ];
133139 }
134140
135- public static function getContentCodingHeader (string $ salt , string $ localPublicKey , string $ contentEncoding ): string
141+ public static function getContentCodingHeader (string $ salt , string $ localPublicKey , ContentEncoding $ contentEncoding ): string
136142 {
137- if ($ contentEncoding === "aes128gcm " ) {
143+ if ($ contentEncoding === ContentEncoding::aesgcm) {
144+ return "" ;
145+ }
146+ if ($ contentEncoding === ContentEncoding::aes128gcm) {
138147 return $ salt
139148 .pack ('N* ' , 4096 )
140149 .pack ('C* ' , Utils::safeStrlen ($ localPublicKey ))
141150 .$ localPublicKey ;
142151 }
143152
144- return "" ;
153+ throw new \ ValueError ( " This content encoding is not implemented. " ) ;
145154 }
146155
147156 /**
@@ -182,19 +191,19 @@ private static function hkdf(string $salt, string $ikm, string $info, int $lengt
182191 *
183192 * @throws \ErrorException
184193 */
185- private static function createContext (string $ clientPublicKey , string $ serverPublicKey , string $ contentEncoding ): ?string
194+ private static function createContext (string $ clientPublicKey , string $ serverPublicKey , ContentEncoding $ contentEncoding ): ?string
186195 {
187- if ($ contentEncoding === " aes128gcm " ) {
196+ if ($ contentEncoding === ContentEncoding:: aes128gcm) {
188197 return null ;
189198 }
190199
191200 if (Utils::safeStrlen ($ clientPublicKey ) !== 65 ) {
192- throw new \ErrorException ('Invalid client public key length ' );
201+ throw new \ErrorException ('Invalid client public key length. ' );
193202 }
194203
195204 // This one should never happen, because it's our code that generates the key
196205 if (Utils::safeStrlen ($ serverPublicKey ) !== 65 ) {
197- throw new \ErrorException ('Invalid server public key length ' );
206+ throw new \ErrorException ('Invalid server public key length. ' );
198207 }
199208
200209 $ len = chr (0 ).'A ' ; // 65 as Uint16BE
@@ -212,25 +221,25 @@ private static function createContext(string $clientPublicKey, string $serverPub
212221 *
213222 * @throws \ErrorException
214223 */
215- private static function createInfo (string $ type , ?string $ context , string $ contentEncoding ): string
224+ private static function createInfo (string $ type , ?string $ context , ContentEncoding $ contentEncoding ): string
216225 {
217- if ($ contentEncoding === " aesgcm " ) {
226+ if ($ contentEncoding === ContentEncoding:: aesgcm) {
218227 if (!$ context ) {
219- throw new \ErrorException ('Context must exist ' );
228+ throw new \ValueError ('Context must exist. ' );
220229 }
221230
222231 if (Utils::safeStrlen ($ context ) !== 135 ) {
223- throw new \ErrorException ('Context argument has invalid size ' );
232+ throw new \ValueError ('Context argument has invalid size. ' );
224233 }
225234
226235 return 'Content-Encoding: ' .$ type .chr (0 ).'P-256 ' .$ context ;
227236 }
228237
229- if ($ contentEncoding === " aes128gcm " ) {
238+ if ($ contentEncoding === ContentEncoding:: aes128gcm) {
230239 return 'Content-Encoding: ' .$ type .chr (0 );
231240 }
232241
233- throw new \ErrorException ('This content encoding is not supported . ' );
242+ throw new \ErrorException ('This content encoding is not implemented . ' );
234243 }
235244
236245 private static function createLocalKeyObject (): array
@@ -262,17 +271,17 @@ private static function createLocalKeyObject(): array
262271 /**
263272 * @throws \ValueError
264273 */
265- private static function getIKM (string $ userAuthToken , string $ userPublicKey , string $ localPublicKey , string $ sharedSecret , string $ contentEncoding ): string
274+ private static function getIKM (string $ userAuthToken , string $ userPublicKey , string $ localPublicKey , string $ sharedSecret , ContentEncoding $ contentEncoding ): string
266275 {
267276 if (empty ($ userAuthToken )) {
268277 return $ sharedSecret ;
269278 }
270- if ($ contentEncoding === " aesgcm " ) {
279+ if ($ contentEncoding === ContentEncoding:: aesgcm) {
271280 $ info = 'Content-Encoding: auth ' .chr (0 );
272- } elseif ($ contentEncoding === " aes128gcm " ) {
281+ } elseif ($ contentEncoding === ContentEncoding:: aes128gcm) {
273282 $ info = "WebPush: info " .chr (0 ).$ userPublicKey .$ localPublicKey ;
274283 } else {
275- throw new \ValueError ("This content encoding is not supported . " );
284+ throw new \ValueError ("This content encoding is not implemented . " );
276285 }
277286
278287 return self ::hkdf ($ userAuthToken , $ sharedSecret , $ info , 32 );
0 commit comments