false, 'reason' => 'missing', 'sid' => '', 'issued_at' => 0, 'age' => 0, 'device' => '', 'signed' => false, ); $token = (string) $token; if ( '' === $token ) { return $result; } $result['reason'] = 'malformed'; if ( strlen( $token ) > 260 ) { return $result; } $parts = explode( '.', $token ); if ( 5 !== count( $parts ) ) { return $result; } list( $sid, $issued_at, $sig, $device, $mac ) = $parts; if ( ! ctype_xdigit( $sid ) || strlen( $sid ) < 16 || ! ctype_digit( $issued_at ) || ! ctype_xdigit( $sig ) || ! ctype_xdigit( $device ) || ! ctype_xdigit( $mac ) ) { return $result; } $issued_at = (int) $issued_at; $result['reason'] = 'signature'; if ( ! hash_equals( self::sign( $secret, $sid, $ip_prefix, $ua_hash, $issued_at ), $sig ) ) { return $result; } if ( ! hash_equals( self::device_mac( $secret, $sig, $device ), $mac ) ) { return $result; } $age = (int) $now - $issued_at; $result['signed'] = true; $result['sid'] = $sid; $result['issued_at'] = $issued_at; $result['age'] = $age; $result['device'] = '0' === $device ? '' : $device; if ( $age < self::MIN_AGE ) { $result['reason'] = 'young'; return $result; } if ( $age > self::MAX_AGE ) { $result['reason'] = 'expired'; return $result; } $result['ok'] = true; $result['reason'] = ''; return $result; } /** * Issues an "I'm human" pass, tied to one token and one visitor. * * @param string $secret Site secret. * @param string $sid The token's session id. * @param string $ip_prefix /24 or /48 of the visitor. * @param string $ua_hash User agent fingerprint. * @param int $now Unix time. * @return string */ public static function build_pass( $secret, $sid, $ip_prefix, $ua_hash, $now ) { return (int) $now . '.' . self::pass_sig( $secret, $sid, $ip_prefix, $ua_hash, (int) $now ); } /** * Checks a pass. * * @param string $pass Pass as received. * @param string $secret Site secret. * @param string $sid The session id of the token it came with. * @param string $ip_prefix /24 or /48 of the visitor now. * @param string $ua_hash User agent fingerprint now. * @param int $now Unix time. * @return bool */ public static function verify_pass( $pass, $secret, $sid, $ip_prefix, $ua_hash, $now ) { $parts = explode( '.', (string) $pass ); if ( 2 !== count( $parts ) || ! ctype_digit( $parts[0] ) || '' === $sid ) { return false; } $issued = (int) $parts[0]; if ( $issued > (int) $now + 60 || (int) $now - $issued > self::PASS_TTL ) { return false; } return hash_equals( self::pass_sig( $secret, $sid, $ip_prefix, $ua_hash, $issued ), $parts[1] ); } /** * Signature of a pass. * * @param string $secret Site secret. * @param string $sid Session id. * @param string $ip_prefix Visitor prefix. * @param string $ua_hash User agent fingerprint. * @param int $issued Unix time. * @return string */ private static function pass_sig( $secret, $sid, $ip_prefix, $ua_hash, $issued ) { return hash_hmac( 'sha256', 'pass|' . $sid . '|' . $ip_prefix . '|' . $ua_hash . '|' . $issued, (string) $secret ); } /** * Issues a puzzle: find a number so that sha256( nonce : number ) starts with `bits` zero bits. * * @param string $secret Site secret. * @param string $nonce Random hex string. * @param int $bits Difficulty. * @param string $bind What the puzzle is tied to (the visitor). * @param int $now Unix time. * @return string */ public static function build_puzzle( $secret, $nonce, $bits, $bind, $now ) { $expires = (int) $now + self::POW_TTL; $bits = (int) $bits; return implode( '.', array( $nonce, $bits, $expires, self::puzzle_sig( $secret, $nonce, $bits, $expires, $bind ) ) ); } /** * Checks a solved puzzle. * * @param string $puzzle Puzzle as issued. * @param string $solution The number the browser found. * @param string $secret Site secret. * @param string $bind What the puzzle is tied to now. * @param int $now Unix time. * @return string|false The puzzle's nonce when solved correctly, false otherwise. */ public static function verify_puzzle( $puzzle, $solution, $secret, $bind, $now ) { $parts = explode( '.', (string) $puzzle ); $solution = (string) $solution; if ( 4 !== count( $parts ) || ! ctype_xdigit( $parts[0] ) || ! ctype_digit( $parts[1] ) || ! ctype_digit( $parts[2] ) ) { return false; } if ( '' === $solution || strlen( $solution ) > 12 || ! ctype_digit( $solution ) ) { return false; } list( $nonce, $bits, $expires, $sig ) = $parts; $bits = (int) $bits; if ( (int) $expires < (int) $now || $bits < 1 || $bits > 30 ) { return false; } if ( ! hash_equals( self::puzzle_sig( $secret, $nonce, $bits, (int) $expires, $bind ), $sig ) ) { return false; } return self::leading_zero_bits( hash( 'sha256', $nonce . ':' . $solution, true ) ) >= $bits ? $nonce : false; } /** * Signature of a puzzle. * * @param string $secret Site secret. * @param string $nonce Nonce. * @param int $bits Difficulty. * @param int $expires Unix time. * @param string $bind Visitor binding. * @return string */ private static function puzzle_sig( $secret, $nonce, $bits, $expires, $bind ) { return substr( hash_hmac( 'sha256', 'pow|' . $nonce . '|' . $bits . '|' . $expires . '|' . $bind, (string) $secret ), 0, 32 ); } /** * Counts the zero bits at the start of a binary string. * * @param string $binary Raw bytes. * @return int */ public static function leading_zero_bits( $binary ) { $bits = 0; $length = strlen( $binary ); for ( $i = 0; $i < $length; $i++ ) { $byte = ord( $binary[ $i ] ); if ( 0 === $byte ) { $bits += 8; continue; } for ( $mask = 0x80; $mask > 0 && 0 === ( $byte & $mask ); $mask >>= 1 ) { ++$bits; } break; } return $bits; } }