Iranian President Hassan Rouhani flashes a victory sign as he arrives for a campaign rally in Tehran on May 9. (Abedin Taherkenareh/European Pressphoto Agency)

Iran’s moderate president, Hassan Rouhani, has delivered rare rebukes in recent days to the country’s powerful Shiite clergy and allied security forces, lashing out at rivals and their hard-line backers ahead of his reelection bid next week.

At rallies across Iran, Rouhani has blasted his opponents as “extremists” and criticized authorities for the detention of reformist leaders. On Monday, he attacked rival Ebrahim Raisi, a conservative cleric and former judicial official, for a record of “execution and imprisonment.”

Rouhani also questioned the tax-exempt status of a charitable foundation linked to Iran’s supreme leader and suggested in a televised debate Friday that Iran’s most influential security institution, the Revolutionary Guard Corps, tried to sabotage the 2015 nuclear deal he struck with world powers.

The comments came as the three-week-long presidential campaign entered its final stretch, with voting scheduled for May 19.

The remarks “are certainly a bold move that indicate a decision to forgo the calm tone of his campaign,” said Reza H. Akbari, program manager at the Institute for War and Peace Reporting in Washington, where he researches Iranian politics.

Supporters of Iranian President Hassan Rouhani hold up pictures of him during a campaign rally in Tehran on May 9. (Abedin Taherkenareh/European Pressphoto Agency)

“It is clear that Rouhani is in full offensive mode and will adopt a no-holds-barred approach prior to the final debate” scheduled for Friday, Akbari said.

Rouhani is facing five challengers, including Raisi and another leading conservative, Tehran Mayor Mohammad Bagher Ghalibaf. Iran’s 12-member Guardian Council approves all candidates, narrowing the field to those acceptable to the supreme leader, Ayatollah Ali Khamenei.

Most of Iran’s presidents have easily won second terms, but a sluggish economy has left Rouhani vulnerable to attacks from populist contenders — as well as from Khamenei himself. Rouhani’s critics say the sanctions lifted under the nuclear deal have done little to improve the lives of ordinary Iranians.

Over the weekend, Rouhani visited a coal mine where an explosion killed 42 workers. Damaging news footage showed miners kicking and beating the president’s car.

“The visit may have been a shocking revelation for Rouhani, who might be losing popularity within the working class,” Akbari said.

Rouhani’s fresh focus on political and social freedoms suggests an attempt to deflect the criticism while burnishing his pro-reform credentials to an increasingly youthful electorate.

“Our youth have chosen the path of freedom. You cannot prevent the progress and freedom of our youth,” Rouhani said at a rally Monday in the city of Hamadan, according to the official Islamic Republic News Agency.

Iranian presidential candidate Ebrahim Raisi gestures in this undated photo. (Tasnim News Agency/via Reuters)

He was elected in 2013 after promising he would end the house arrest of reformist leaders Mehdi Karroubi, Mir Hossein Mousavi and Zahra Rahnavard, Mousavi’s wife. The three were top leaders of the Green Movement protests in 2009, following the disputed reelection of the hard-line president, Mahmoud Ahmadinejad.

But Rouhani has failed to secure their release, and he has also struggled to ease political restrictions. A number of journalists, dissidents and activists remain behind bars.

“You ran this country for eight years, and the people saw what you did,” Rouhani said, referring to Ahmadinejad’s two terms from 2005 to 2013, in which Iran was isolated internationally and rights abuses worsened. The ­comments were carried by the quasi-independent Iranian Students’ News Agency.

The “era of violence and extremism is over,” Rouhani said.

Also Monday, the president questioned whether the charitable foundation managed by Raisi, which also maintains vast business and real estate holdings, had ever paid taxes to the state. The Astan Quds Razavi charity, which administers the holy Imam Reza Shrine in the northeastern city of Mashhad, is one of the largest and most influential in Iran.

Its manager is appointed directly by Khamenei, who analysts say favors Raisi for the presidency. The country’s Shiite clerical establishment wields enormous power, and “attacks against holy religious entities are typically considered off-limits,” Akbari said.

Also generally prohibited is public criticism of the Revolutionary Guard. In a televised debate Friday, Rouhani accused the elite force of trying to undermine the nuclear deal by writing Hebrew-language messages on ballistic missiles used for testing.

The deal curbs Iran’s nuclear program in exchange for sanctions relief. An accompanying U.N. resolution calls on Iran to refrain from testing ballistic missiles capable of carrying nuclear payloads, but the tests are not expressly prohibited, and Iran denies that the missiles are designed for nuclear warheads.

The United States has maintained many of its own sanctions on Iran, limiting the benefits of the deal on the Iranian economy.