A group of environmental protesters have had their convictions overturned after senior judges ruled that crucial evidence gathered by an undercover police officer was withheld from their original trial.

The 29 protesters were convicted in 2009 after they blocked a train carrying coal from going into the Drax power station in North Yorkshire.

On Tuesday the lord chief justice, Lord Thomas, and two judges quashed their convictions after it was admitted that the involvement of Mark Kennedy, the undercover police officer who infiltrated environmental groups for seven years, had been hidden.

Thomas said there had been "a complete and total failure" to disclose evidence that would have been fundamental to the activists' defence. He said reasons for the failure remained unclear.

Earlier, Brian Altman QC, for the prosecution, told the court of appeal that the failure had been catastrophic, and it was unclear whether the fault lay with the police or prosecutors. The verdict brings to 56 the number of protesters who have been wrongly convicted or prosecuted as a result of undercover police operations.

The appeal court heard that Kennedy attended a private meeting where the 29 campaigners formulated their protest. He hired a van and drove some of them to the protest. The court was told that police had conceded after the original trial that Kennedy had been "the sole driver" for the protest against climate change, raising the possibility that it would not have gone ahead if he had not been involved.

Undercover police officer Mark Kennedy. Photograph: Philipp Ebeling

Thomas ruled that Kennedy's role should have been disclosed to the activists as it would have enabled their lawyers to argue at the original trial that the spy had been an agent provocateur or that there had been an abuse of the legal process.

The 29 were charged with obstructing engines or carriages on railways, which is an offence under the Malicious Damage Act 1861.

One of the acquitted campaigners, Robbie Gillett, said: "In our trial in 2009, the police and the Crown Prosecution Service deliberately withheld evidence from the jury. They're not interested in providing a fair trial to the political activists which they spy upon.

"This is political policing. It is an invasion of people's lives, a waste of public money and from the police's perspective, a legal failure."

Thomas suggested that those responsible for the misconduct should be required to pay for costs of the wrongful prosecutions. The judge said he would decide whether police or prosecutors should pay.

Kennedy was unmasked by activists in 2010 after they became suspicious of his true identity, sparking a series of revelations over the work of undercover officers who have been deployed to infiltrate political groups since 1968.

In another case, police and prosecutors withheld evidence of Kennedy's infiltration from 26 campaigners who planned to occupy Ratcliffe-on-Soar power station in 2009. Twenty had their convictions quashed, and the prosecutions of another six were dropped.

On Tuesday, Altman described some of the covert work of the spy who had transformed his appearance and pretended to be a committed environmental activist using the alias Mark Stone.

He read extracts from the notebooks of the long-haired, tattooed spy recording how he had been approached by an activist to see if he would be prepared to drive some campaigners to a protest. A day later another campaigner told him that a group of activists were going to delay the train going into the Drax power station.

Kennedy, who cultivated his image as an activist with money to spare and earned himself the nickname Flash, used £250 of the state's cash on the hire of a van.

Early on 13 June 2008 he used the van to drive some of the activists to the protest and dropped them off. Within minutes he was on the phone to his police handler reporting what the activists were doing.

At the original trial, some of the group were ordered to do 60 hours of unpaid work and others were given conditional discharges.

During the protest the freight train was stopped by two men posing as Network Rail staff, wearing orange jackets and hard hats, who held up a red flag. Moments later, the train and a nearby bridge were scaled by the protesters wearing white paper boiler suits and carrying banners.

The protest lasted 16 hours, causing delays to numerous freight and passenger services. The clean-up operation cost Network Rail nearly £37,000.

Defendants told jurors they did not believe they were doing anything criminal because they were trying to prevent climate change.

The court of appeal hearing in London came after an announcement in 2012 by the then director of public prosecutions Keir Starmer that there were concerns about the safety of convictions in the Drax case.

Starmer said he was inviting those convicted after the Drax protest to appeal after a review of the case by a senior CPS lawyer and after taking advice on the safety of the convictions from a senior QC.

On Tuesday Lord Thomas also asked counsel to prepare written submissions on the question of who should be responsible for the substantial legal costs incurred, to be decided at a later date. Querying why the Ministry of Justice should foot the bill, he commented: "This is a plain case of fault, either by the West Yorkshire police or the CPS, so why shouldn't they pay?"

After Tuesday's ruling, John Sauven, executive director of Greenpeace, said: "The sheer scale of covert policing against legitimate organisations is corrosive to the democratic values and principles we hold dear.

"I join with others including Neville and Doreen Lawrence to demand that the government hold an independent judge-led inquiry into who was responsible for instructing these officers to spy on peaceful, legitimate organisations."