Europe's top court ruled Wednesday that new British surveillance powers are illegal and infringe on people's privacy.

The surveillance legislation, enacted last month, gives British authorities broad power to access information about phone calls, emails, texts and browsing history. It also requires telecom and tech companies to collect and store a year's worth of user data.

The European Union's Court of Justice said the laws exceed "what is strictly necessary and cannot be considered to be justified within a democratic society." It said similar rules in Sweden also overreach.

The court said data collection and surveillance must be more targeted to fight serious crime.

The British Home Office said it was disappointed by the ruling. It noted that it wasn't looking at the content of people's messages, only tracking information such as where and when calls were made.

A British appeals court will decide how the legislation must be changed.

More than 200,000 people had signed a petition calling for the powers to be scrapped, saying it gave law enforcement "unprecedented levels of power regarding the surveillance of U.K. citizens."

Big tech companies including Facebook (FB), Google (GOOG), Twitter (TWTR), Microsoft (MSFT) and Yahoo (YHOO) had also opposed the legislation, which some called the "snooper's charter." Public opposition had forced the government to loosen the rules to make them more palatable.

Related: Why Snapchat's owner is hiring in China

The court ruling is at once a victory and an embarrassment for David Davis, the government minister in charge of Britain's exit from the European Union.

Before he was appointed to the Brexit job, he had challenged the surveillance law in the EU's Court of Justice -- essentially inviting the EU to meddle in British affairs. Now he's focused on separating Britain from the EU and avoiding EU interference in domestic affairs.

Related: U.S. promises to dial back spying on European data

A spokesman for the minister noted to CNNMoney that Davis withdrew his name from the case when he was appointed to his new job, and said that he "will now make his arguments on any issue within Cabinet."