(bool) $gcm, 'key' => $gcm ? hash_hmac( 'sha256', 'resp' . $request_nonce . $salt, $enc_key, true ) : '', 'mac' => $gcm ? null : self::mac_init( $sign_key, $request_nonce ), 'index' => 0, 'pieces' => array( self::MAGIC . chr( $gcm ? 1 : 0 ) . "\0\0\0" . $salt ), 'bytes' => 24, ); } /** * Adds a frame. * * @param array $enc Encoder state (by reference). * @param string $type One-letter type. * @param string $payload Plain payload. */ public static function add( array &$enc, $type, $payload ) { $plain_len = strlen( $payload ); if ( $enc['gcm'] ) { $iv = "\0\0\0\0" . pack( 'NN', 0, $enc['index'] ); $tag = ''; $payload = openssl_encrypt( $payload, 'aes-256-gcm', $enc['key'], OPENSSL_RAW_DATA, $iv, $tag, $type . pack( 'N', $plain_len ) ) . $tag; } $frame = $type . pack( 'N', strlen( $payload ) ) . $payload; if ( ! $enc['gcm'] ) { if ( 0 === $enc['index'] ) { hash_update( $enc['mac'], $enc['pieces'][0] ); } hash_update( $enc['mac'], $frame ); } $enc['pieces'][] = $frame; $enc['bytes'] += strlen( $frame ); ++$enc['index']; } /** * Adds a JSON frame. * * @param array $enc Encoder state. * @param string $type Type. * @param mixed $data Data. */ public static function add_json( array &$enc, $type, $data ) { self::add( $enc, $type, (string) wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); } /** * Finishes with an E frame and returns the whole body. * * @param array $enc Encoder state. * @param array $data End data. */ public static function finish( array &$enc, array $data = array() ) { $data['frames'] = $enc['index']; if ( ! $enc['gcm'] ) { if ( 0 === $enc['index'] ) { hash_update( $enc['mac'], $enc['pieces'][0] ); } $data['mac'] = hash_final( hash_copy( $enc['mac'] ) ); } self::add_json( $enc, 'E', $data ); return implode( '', $enc['pieces'] ); } /** * Integrity MAC for responses without GCM, bound to the request nonce so an old response * can't be replayed as the answer to a new request. * * @param string $sign_key K_sign. * @param string $request_nonce Request nonce. * @return \HashContext|resource */ private static function mac_init( $sign_key, $request_nonce ) { $mac = hash_init( 'sha256', HASH_HMAC, $sign_key ); hash_update( $mac, 'resp' . $request_nonce . "\n" ); return $mac; } /** * Decodes a whole response body. * * @param string $body Response bytes. * @param string $enc_key K_enc. * @param string $sign_key K_sign. * @param string $request_nonce Nonce sent with the request. * @return array Frames as [type, payload]. * @throws \RuntimeException When the body is not a valid, complete, authentic response. */ public static function decode( $body, $enc_key, $sign_key, $request_nonce ) { if ( strlen( $body ) < 24 || self::MAGIC !== substr( $body, 0, 4 ) ) { throw new \RuntimeException( 'not_framed' ); } $gcm = 1 === ( ord( $body[4] ) & 1 ); $salt = substr( $body, 8, 16 ); $key = $gcm ? hash_hmac( 'sha256', 'resp' . $request_nonce . $salt, $enc_key, true ) : ''; $pos = 24; $len = strlen( $body ); $index = 0; $frames = array(); $ended = false; while ( $pos < $len ) { if ( $pos + 5 > $len ) { throw new \RuntimeException( 'truncated' ); } $type = $body[ $pos ]; $size = unpack( 'N', substr( $body, $pos + 1, 4 ) )[1]; if ( $pos + 5 + $size > $len ) { throw new \RuntimeException( 'truncated' ); } $payload = substr( $body, $pos + 5, $size ); $frame_end = $pos + 5 + $size; if ( $gcm ) { if ( $size < 16 ) { throw new \RuntimeException( 'bad_frame' ); } $iv = "\0\0\0\0" . pack( 'NN', 0, $index ); $cipher = substr( $payload, 0, -16 ); $tag = substr( $payload, -16 ); $payload = openssl_decrypt( $cipher, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, $type . pack( 'N', strlen( $cipher ) ) ); if ( false === $payload ) { throw new \RuntimeException( 'auth_failed' ); } } if ( 'E' === $type ) { $end = json_decode( $payload, true ); if ( ! is_array( $end ) || (int) ( $end['frames'] ?? -1 ) !== $index ) { throw new \RuntimeException( 'bad_end' ); } if ( ! $gcm ) { $mac = self::mac_init( $sign_key, $request_nonce ); hash_update( $mac, substr( $body, 0, $pos ) ); $expected = hash_final( $mac ); if ( ! isset( $end['mac'] ) || ! hash_equals( $expected, (string) $end['mac'] ) ) { throw new \RuntimeException( 'auth_failed' ); } } $frames[] = array( 'E', $payload ); $ended = true; if ( $frame_end !== $len ) { throw new \RuntimeException( 'trailing_data' ); } break; } $frames[] = array( $type, $payload ); $pos = $frame_end; ++$index; } if ( ! $ended ) { throw new \RuntimeException( 'truncated' ); } return $frames; } }