David Cameron has backed a campaign to prevent the Northern Ireland attorney general suing the former Northern Ireland secretary Peter Hain on behalf of a senior judge by using century-old legislation of "scandalising the court".

The issue was raised by the former home secretary David Blunkett at prime minister's questions in the Commons.

An all-party campaign is being launched, designed to protect the freedom of MPs to comment on judicial rulings.

In a question to Cameron, Blunkett said: "Shouldn't respect for the independence of the judiciary be balanced by the rights of individuals to fair comment on that judiciary?"

The Northern Ireland attorney general, John Larkin, has been granted leave to prosecute Hain and Biteback Publishing over claims that a strongly worded passage in his memoir "undermines the administration of justice".

Cameron said MPs should be free to criticise the judiciary, just as judges should be allowed to attack politicians.

"In terms of what is said outside parliament, let me just say this: there are occasions we all know when judges make critical remarks about politicians and there are occasionally [times] when politicians make critical remarks about judges.

"To me, this is part of life in a modern democracy and I think we should try to keep these things as far as possible out of the courts."

A Downing Street spokeswoman said his remarks represented his view that judges should be able to take criticism, but she said he did not believe the prime minister had the power to order the case to be stopped. The decision had not been taken by a minister.

Hain has vowed to defend free speech against what his publisher said was an arcane law banning criticism of judges, which had not been used in living memory. Hain's remarks about Lord Justice Girvan's handling of a case caused controversy in Belfast when the book was published.

The lord chief justice, Sir Declan Morgan, described them as "potentially an assault on the wider independence of the judiciary".

Hain declined to back down and even renewed his criticism, sparking the legal action by Larkin. The case is listed for review by the divisional court in Belfast on 24 April.

The attorney general's court submission claimed the remarks, concerning the appointment of Bertha MacDougall as interim victims' commissioner, "constitute unwarranted abuse of a judge in his judicial capacity that undermines the administration of justice in this jurisdiction, and consequently constitute a contempt of court".

The statement adds that the book "create[s] without justification a real risk that public confidence will be undermined in the judiciary".

Hain said in a statement at the time of the attorney general's decision: "I am astonished at this turn of events. I worked harder than anyone as secretary of state for Northern Ireland to uphold the rule of law and judicial independence and delivered the 2007 settlement which helped secure that.

"If free speech and comment in a political memoir is to be suppressed then people will be entitled to ask: what system of justice prevails?"

Judges such as Sir Stephen Sedley have successfully prosecuted newspapers for libel and have sometimes complained that they are shackled combatants on issues of public controversy.