$profile['environment'], 'authority' => $profile['authority'], 'audience' => $profile['audience'], 'integration_type' => $input['integration_type'], 'protocol_version' => $input['protocol_version'], 'adapter_version' => $input['adapter_version'], 'client_attempt_id' => $input['client_attempt_id'], 'origin' => $uris['origin'], 'callback_uri' => $uris['callback_uri'], 'proof_uri' => $uris['proof_uri'], 'installation_signing_public_key_jwk' => $input['installation_signing_public_key_jwk'], 'installation_signing_key_thumbprint' => $signing_thumbprint, 'signing_key_generation' => $input['signing_key_generation'], 'installation_encryption_public_key_jwk' => $input['installation_encryption_public_key_jwk'], 'installation_encryption_key_thumbprint' => $encryption_thumbprint, 'encryption_key_generation' => $input['encryption_key_generation'], 'local_state_hash' => $input['local_state_hash'], 'adapter_claims' => $input['adapter_claims'], 'versions' => $input['versions'], ); self::validate( $payload, $profile ); return $payload; } /** * Validate an already built bootstrap request against a trusted profile. * * @param array $payload Bootstrap request. * @param array $profile Trusted deployment profile. * @return void * @throws InvalidArgumentException For invalid input. */ public static function validate( array $payload, array $profile ) { self::assert_exact_fields( $payload, self::$payload_fields, 'bootstrap_request' ); self::validate_profile( $profile ); foreach ( array( 'environment', 'authority', 'audience' ) as $field ) { if ( ! is_string( $payload[ $field ] ) || ! hash_equals( $profile[ $field ], $payload[ $field ] ) ) { throw new InvalidArgumentException( esc_html( 'invalid_' . $field ) ); } } if ( self::INTEGRATION_TYPE !== $payload['integration_type'] ) { throw new InvalidArgumentException( 'invalid_integration_type' ); } if ( self::PROTOCOL_VERSION !== $payload['protocol_version'] ) { throw new InvalidArgumentException( 'invalid_protocol_version' ); } if ( ! self::is_version( $payload['adapter_version'] ) ) { throw new InvalidArgumentException( 'invalid_adapter_version' ); } if ( ! self::is_digest( $payload['client_attempt_id'] ) || ! self::is_digest( $payload['local_state_hash'] ) ) { throw new InvalidArgumentException( 'invalid_bootstrap_digest' ); } Aha_Is_Woocommerce_Connect_Uri::assert_derived( $payload['origin'], $payload['callback_uri'], $payload['proof_uri'], $payload['client_attempt_id'] ); $signing_thumbprint = self::jwk_thumbprint( $payload['installation_signing_public_key_jwk'], 'signing' ); $encryption_thumbprint = self::jwk_thumbprint( $payload['installation_encryption_public_key_jwk'], 'encryption' ); if ( ! self::is_digest( $payload['installation_signing_key_thumbprint'] ) || ! hash_equals( $signing_thumbprint, $payload['installation_signing_key_thumbprint'] ) ) { throw new InvalidArgumentException( 'invalid_signing_key_thumbprint' ); } if ( ! self::is_digest( $payload['installation_encryption_key_thumbprint'] ) || ! hash_equals( $encryption_thumbprint, $payload['installation_encryption_key_thumbprint'] ) ) { throw new InvalidArgumentException( 'invalid_encryption_key_thumbprint' ); } if ( ! is_int( $payload['signing_key_generation'] ) || $payload['signing_key_generation'] < 1 ) { throw new InvalidArgumentException( 'invalid_signing_key_generation' ); } if ( ! is_int( $payload['encryption_key_generation'] ) || $payload['encryption_key_generation'] < 1 ) { throw new InvalidArgumentException( 'invalid_encryption_key_generation' ); } if ( ! is_array( $payload['adapter_claims'] ) ) { throw new InvalidArgumentException( 'invalid_adapter_claims' ); } self::assert_exact_fields( $payload['adapter_claims'], array( 'local_source_claim_id' ), 'adapter_claims' ); if ( ! is_string( $payload['adapter_claims']['local_source_claim_id'] ) || ! preg_match( '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/D', $payload['adapter_claims']['local_source_claim_id'] ) ) { throw new InvalidArgumentException( 'invalid_local_source_claim_id' ); } if ( ! is_array( $payload['versions'] ) ) { throw new InvalidArgumentException( 'invalid_versions' ); } self::assert_exact_fields( $payload['versions'], array( 'plugin', 'wordpress', 'woocommerce' ), 'versions' ); foreach ( $payload['versions'] as $version ) { if ( ! self::is_version( $version ) ) { throw new InvalidArgumentException( 'invalid_version_metadata' ); } } } /** * Compute an RFC 7638 SHA-256 JWK thumbprint. * * @param array $jwk Public JWK. * @param string $purpose signing or encryption. * @return string Unpadded base64url digest. * @throws InvalidArgumentException For an invalid JWK or purpose. */ public static function jwk_thumbprint( $jwk, $purpose ) { if ( ! is_array( $jwk ) ) { throw new InvalidArgumentException( esc_html( 'invalid_' . $purpose . '_jwk' ) ); } if ( 'signing' === $purpose ) { self::assert_exact_fields( $jwk, array( 'kty', 'crv', 'x', 'y' ), 'signing_jwk' ); if ( 'EC' !== $jwk['kty'] || 'P-256' !== $jwk['crv'] || ! self::is_coordinate( $jwk['x'] ) || ! self::is_coordinate( $jwk['y'] ) ) { throw new InvalidArgumentException( 'invalid_signing_jwk' ); } $members = array( 'crv' => 'P-256', 'kty' => 'EC', 'x' => $jwk['x'], 'y' => $jwk['y'], ); } elseif ( 'encryption' === $purpose ) { self::assert_exact_fields( $jwk, array( 'kty', 'crv', 'x' ), 'encryption_jwk' ); if ( 'OKP' !== $jwk['kty'] || 'X25519' !== $jwk['crv'] || ! self::is_coordinate( $jwk['x'] ) ) { throw new InvalidArgumentException( 'invalid_encryption_jwk' ); } $members = array( 'crv' => 'X25519', 'kty' => 'OKP', 'x' => $jwk['x'], ); } else { throw new InvalidArgumentException( 'invalid_jwk_purpose' ); } return Aha_Is_Woocommerce_Connect_Crypto::base64url( hash( 'sha256', json_encode( $members, JSON_UNESCAPED_SLASHES ), true ) ); } /** * Validate the trusted deployment profile without selecting one locally. * * @param array $profile Trusted profile. * @return void * @throws InvalidArgumentException For an invalid profile. */ private static function validate_profile( array $profile ) { self::assert_exact_fields( $profile, array( 'environment', 'authority', 'audience' ), 'environment_profile' ); if ( ! is_string( $profile['environment'] ) || ! preg_match( '/^[a-z][a-z0-9_-]{1,31}$/D', $profile['environment'] ) ) { throw new InvalidArgumentException( 'invalid_environment' ); } if ( ! class_exists( 'Aha_Is_Woocommerce_Connect_Dev_Profiles', false ) && Aha_Is_Woocommerce_Connect_Profile::from_operator_config() !== $profile ) { throw new InvalidArgumentException( 'invalid_authority' ); } if ( 'local' === $profile['environment'] ) { if ( ! class_exists( 'Aha_Is_Woocommerce_Connect_Dev_Profiles', false ) || ! Aha_Is_Woocommerce_Connect_Dev_Profiles::valid_local_bootstrap_profile( $profile ) ) { throw new InvalidArgumentException( 'invalid_authority' ); } } elseif ( Aha_Is_Woocommerce_Connect_Uri::canonical_origin( $profile['authority'] ) !== $profile['authority'] || false !== strpos( substr( $profile['authority'], 8 ), '/' ) ) { throw new InvalidArgumentException( 'invalid_authority' ); } if ( ! is_string( $profile['audience'] ) || ! preg_match( '/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/D', $profile['audience'] ) ) { throw new InvalidArgumentException( 'invalid_audience' ); } } /** * Require exact object keys. * * @param array $value Object-like array. * @param string[] $fields Allowed and required keys. * @param string $name Error context. * @return void * @throws InvalidArgumentException When the fields differ. */ private static function assert_exact_fields( array $value, array $fields, $name ) { $actual = array_keys( $value ); $expected = $fields; sort( $actual, SORT_STRING ); sort( $expected, SORT_STRING ); if ( $actual !== $expected ) { throw new InvalidArgumentException( esc_html( 'invalid_' . $name . '_fields' ) ); } } /** * Check a public JWK coordinate. * * @param mixed $value Coordinate. * @return bool */ private static function is_coordinate( $value ) { return Aha_Is_Woocommerce_Connect_Crypto::is_b64url32( $value ); } /** * Check an unpadded base64url SHA-256 value. * * @param mixed $value Digest. * @return bool */ private static function is_digest( $value ) { return self::is_coordinate( $value ); } /** * Check bounded safe version metadata. * * @param mixed $value Version. * @return bool */ private static function is_version( $value ) { return is_string( $value ) && preg_match( '/^[0-9]+(?:\.[0-9]+){1,3}(?:[-+][0-9A-Za-z.-]+)?$/D', $value ) && strlen( $value ) <= 64; } }