In the end, Imelda Cortez's story was too much even for El Salvador's famously harsh courts. Her case was making news around the world, and DNA had confirmed that the newborn she was accused of attempting to murder was the product of rape by a 70-year-old stepfather who'd abused her throughout her childhood.

Cortez, 20, the daughter of a poor rural family, insisted she didn't know she was pregnant until she entered an outhouse and a child came out.

The baby was born healthy and remains so, but in El Salvador, any birth outside a hospital can be deemed suspicious. Because prosecutors claimed she had intended to kill the child after it was born, she was charged with attempted aggravated murder, an offence that carries a possible sentence of 20 years in prison.

Her case is one of many in the Central American nation that are jarringly at odds with the Trudeau government's views on a woman's right to choose. But the federal government itself is helping to fund a Salvadoran justice system that takes a very harsh line on abortion prosecutions. Salvadorans pushing to change the abortion law say it often persecutes women like Cortez who have experienced miscarriages or stillbirths.

"As a government, as Canadians, we will always be unequivocal about defending a woman's right to choose, defending women's rights in general," Prime Minister Justin Trudeau said May 16 in Paris. This week, he spoke out against anti-abortion measures in Alabama and other states.

The strictest abortion law in the world

The police and prosecutors who put Cortez in prison receive support and training from the government of Canada. They enforce what many consider to be the harshest anti-abortion law in the world — with no exceptions for rape, incest or efforts to save the life of the mother.

The prosecutors' office in El Salvador typically brings charges against women suspected of abortion that carry a sentence of between 30 and 50 years in prison. Doctors who fail to call police when they suspect an abortion has happened also face prison terms.

Omar Flores is part of a group of campaigners fighting to overturn El Salvador's harsh abortion laws. (CBC News)

After a forensic examination, police charged Cortez with "aborto culposo," or abortion with criminal intent (with a maximum sentence of eight years). But lawyer Omar Flores said the Salvadoran attorney-general's office wanted to take a more aggressive approach.

Flores is project coordinator for human rights group FESPAD (Fundacion de Estudios para la Aplicacion del Derecho), which has been deeply involved in the cases of two dozen women charged with terminating or attempting to terminate pregnancies.

Flores told CBC News during Cortez's trial that prosecutors zeroed in on the fact that she hadn't spoken to anyone about her pregnancy or sought medical attention.

Charging a rape victim with murder

"The police were basically trying to attenuate or soften the charge, but the prosecutor (Marisol Caceres) wasn't having that," he said. "At her insistence the charge was elevated to attempted aggravated murder."

Prosecutors relied on the testimony of Imelda's stepfather Pablo Henriquez, who told police the teen had asked him for a dollar to buy sanitary towels four days before the birth — proof, said prosecutors, that she knew she was pregnant.

Henriquez himself was arrested after a nurse at the hospital overheard him threatening to kill Imelda if she disclosed the rapes. But the case against her continued until December 2018, when a judge tossed it out. Imelda Cortez had spent 18 months in prison.

El Salvador is waging a grim struggle against criminal gangs that have made it one of the most dangerous countries the world.

The government of Canada has contributed millions of dollars to El Salvador to assist in that fight — $3.4 million in 2017 alone. The program is administered by the Justice Education Society of Vancouver, B.C.

"The decision to provide aid to build the capacity of elements of the justice sector in El Salvador is a decision of the government of Canada," said Justice Education Society Executive Director Sonia Poulin, adding that it was only part of "a broader set of policy goals and initiatives" across Central America.

Where Canada's money goes

Canadian money has flowed both to the prosecutors' office and to the National Civil Police; some of it has been used to develop forensic skills for crime scene analysis. Forensic evidence collected at the scenes of births and stillbirths, and afterwards in hospitals, has played a major role in some controversial abortion prosecutions.

Then-minister of international development Marie-Claude Bibeau said just before Cortez's final court hearing in December that she was seeking more information about Canada's assistance.

"Such decisions taken by the attorney general are very difficult for me to understand," she told CBC News. "It's always very tempting to withdraw our support, but at the same time if we withdraw we have no voice to influence the attorney general and to advocate for these girls."

We are celebrating one year of Canada's Feminist International Assistance Policy, which promotes gender equity and equal rights for women and girls. Canada is the first country in the world to focus its international cooperation on the empowerment of women. <a href="https://twitter.com/hashtag/WomensRights?src=hash&ref_src=twsrc%5Etfw">#WomensRights</a> <a href="https://t.co/tct9UZNSwG">pic.twitter.com/tct9UZNSwG</a> —@CanEmbSV

A spokesman for Bibeau's replacement in the portfolio, Maryam Monsef, said Canada, "alongside like-minded allies and human rights organizations, closely follows cases involving criminal accusations of women accused of abortions in El Salvador."

"Our ambassador will continue to attend these trials and will continue to raise our concerns with the Government of El Salvador, including with the Attorney General. Canada will continue to express the importance of ensuring women have access to justice," said Geoffroi Montpetit, Monsef's chief of staff.

Abortion was always illegal in traditionally Catholic El Salvador, but until the late 1990s it was permitted when a doctor certified that the mother's life was in danger.

The emergence of an anti-abortion movement, with conservative politicians pushing for murder charges, parallels the rise of evangelical protestantism in Central America.

El Salvador's main right wing party ARENA pushed successfully for a full ban, but it was the leftist government, dominated by members of the former guerrilla movement FMLN, that oversaw a recent wave of incarcerations under the law.

Aggressive prosecution

While a political campaign to stop the prosecutions has been picking up steam, it's fiercely opposed by El Salvador's Catholic hierarchy, evangelical pastors and the prosecutor's office, according those who've campaigned to halt the incarceration of women and girls under the abortion law.

"It's more than just indifference from the prosecutor's office to the plight of these women," said Flores. "The prosecutors are the ones who are always pushing for the longest sentences."

Flores said a new gender policy the Salvadoran government brought in at the end of 2018 was supposed to improve the situation for women and girls. But attorney-general Douglas Melendez, who was showing signs of softening his approach, was removed by the National Assembly and replaced by hardliner Raul Melara.

"Things seem to be back where they were before," said Flores.

Ricardo Langlois is a San Salvador lawyer who is defending two women on abortion charges. One is Maricela Albizuri, 31, who says she gave birth to a stillborn baby in a bathroom. Now on conditional release because of a lifelong history of mental illness, she has been charged with aggravated murder and is facing 30 to 50 years in prison.

His other client, 20-year-old Evelyn Beatriz Hernandez Cruz, also says she didn't know she was pregnant until she delivered a child in a bathroom. She told police the baby was stillborn and that she had been raped by a local gang member.

She spent 24 months in pretrial custody before receiving a 30 year sentence. Thanks to an appeal by her lawyers, she now has a new trial that starts June 24. But she still faces a potential 50-year sentence.

Langlois said he would like to see more pressure on the attorney-general's office.

"I think that before giving any kind of contribution, you have to really find out exactly what it's going to be used for," he said.

CBC News asked Flores if he's seen improvements in El Salvador since the Cortez case ended.

"They're still pursuing the same approach: charge first and ask questions later," says Flores. "The trials continue."