value. */ public static function build_signed_headers( array $args ) { $timestamp = isset( $args['timestamp'] ) && '' !== $args['timestamp'] ? $args['timestamp'] : self::now_iso8601(); $source = isset( $args['source'] ) && '' !== $args['source'] ? $args['source'] : self::SOURCE; $raw_body = isset( $args['raw_body'] ) ? $args['raw_body'] : ''; return array( 'Content-Type' => 'application/json', 'X-Aha-Store' => isset( $args['store'] ) ? $args['store'] : '', 'X-Aha-Key-Id' => isset( $args['key_id'] ) ? $args['key_id'] : '', 'X-Aha-Timestamp' => $timestamp, 'X-Aha-Source' => $source, 'X-Aha-Event-Id' => isset( $args['event_id'] ) ? $args['event_id'] : '', 'X-Aha-Signature' => self::sign( $args['secret'], $timestamp, $source, $raw_body ), ); } /** * Verify an inbound request. * * The caller gives the secret that X-Aha-Key-Id selects. This method never * tries a second secret, because that leaks timing information. * * Every failure gets the same answer at the HTTP layer (401 invalid_signature). * The reason string is for the log only. * * @param array $args { * @type string $secret Secret that the key id selects. * @type string|null $timestamp X-Aha-Timestamp. * @type string|null $source X-Aha-Source. * @type string|null $signature X-Aha-Signature. * @type string $raw_body Exact body bytes. * @type int $max_skew Optional. Seconds. Defaults to 300. * @type int $now Optional. Unix time, for tests. * } * @return array { * @type bool $ok True when the request is authentic. * @type string $reason Machine readable failure reason, '' on success. * } */ public static function verify( array $args ) { $secret = isset( $args['secret'] ) ? (string) $args['secret'] : ''; if ( '' === $secret ) { return self::fail( 'missing_secret' ); } $timestamp = isset( $args['timestamp'] ) ? trim( (string) $args['timestamp'] ) : ''; $source = isset( $args['source'] ) ? trim( (string) $args['source'] ) : ''; $received = self::normalize_signature( isset( $args['signature'] ) ? $args['signature'] : '' ); if ( '' === $timestamp || '' === $source || '' === $received ) { return self::fail( 'missing_headers' ); } $timestamp_unix = self::parse_iso8601( $timestamp ); if ( null === $timestamp_unix ) { return self::fail( 'invalid_timestamp' ); } $now = isset( $args['now'] ) ? (int) $args['now'] : time(); $max_skew = isset( $args['max_skew'] ) ? (int) $args['max_skew'] : self::MAX_SKEW_SECONDS; if ( abs( $now - $timestamp_unix ) > $max_skew ) { return self::fail( 'stale_timestamp' ); } $expected = self::normalize_signature( self::sign( $secret, $timestamp, $source, isset( $args['raw_body'] ) ? $args['raw_body'] : '' ) ); // hash_equals is constant time. It needs both sides to be strings. // It compares the length first, which is safe here: the length of a hex // SHA-256 digest is public knowledge. if ( hash_equals( $expected, $received ) ) { return array( 'ok' => true, 'reason' => '', ); } return self::fail( 'invalid_signature' ); } /** * Parse an ISO 8601 timestamp. * * @param string $timestamp Timestamp. * @return int|null Unix time, or null when the value is unusable. */ public static function parse_iso8601( $timestamp ) { $timestamp = trim( (string) $timestamp ); if ( '' === $timestamp ) { return null; } // Reject anything that does not start like a date. strtotime accepts // relative words such as "now" or "tomorrow", which must never pass. if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}/', $timestamp ) ) { return null; } $parsed = strtotime( $timestamp ); if ( false === $parsed ) { return null; } return $parsed; } /** * Current time in the format the contract uses. * * @param int|null $unix Optional Unix time. * @return string */ public static function now_iso8601( $unix = null ) { $unix = ( null === $unix ) ? time() : (int) $unix; return gmdate( 'Y-m-d\TH:i:s', $unix ) . '.000Z'; } /** * Build a failure answer. * * @param string $reason Reason code. * @return array */ private static function fail( $reason ) { return array( 'ok' => false, 'reason' => $reason, ); } }