Virginia Attorney General Mark R. Herring (D). (Jay Paul/Getty Images)

Virginia’s House and Senate on Wednesday accepted Gov. Terry McAuliffe’s plan to hire pharmacies to secretly supply the state with execution drugs, acting one day after the state’s attorney general signed off on the idea.

Virginia joins Arkansas, Missouri and Ohio as states that have placed similar shields over the pharmacies that produce lethal drugs and have faced lengthy legal challenges in state and federal courts. In Arkansas, which hoped to resume executions after a ­decade-long break, the legal challenge has delayed several lethal injections scheduled to take place last fall and winter.

The night before the General Assembly’s one-day veto session, Attorney General Mark R. Herring (D) issued a legal opinion saying that the plan would not violate state or federal laws governing controlled substances or the practice of medicine and pharmacy.

McAuliffe (D), who opposes capital punishment but has vowed to support it as a matter of Virginia law, has said the state would not be able to carry out the death penalty if it does not come up with a way to obtain increasingly scarce execution drugs.

The Senate voted, 22 to 16, to back the governor’s plan, with Sen. Minority Leader Richard L. Saslaw (D-Fairfax) arguing that complaints about secrecy had been overblown. Had the federal government decided to put Oklahoma City bomber Timothy McVeigh to death the same way he killed his victims, “do you honestly think any of your constituents would care where we bought the dynamite?” Saslaw said.

The House’s approval came with dramatic flip-flops. Conservative skeptics of government secrecy initially teamed with House Democrats to reject the governor’s amendments by a vote of 51 to 47. But after a few minutes of arm-twisting by Republican leaders, and the abrupt exit of one GOP delegate, the House reconsidered the vote. The second time, the measure passed, 59 to 40.

“It was important to preserve capital punishment,” said Matthew Moran, spokesman for House Speaker William J. Howell (R-Stafford), one of 13 Republicans who changed their votes to “yes.”

Del. Marcus B. Simon (D-Fairfax), who attempted arcane parliamentary maneuvers to derail the plan, later complained that Republican leaders had whipped members into line on a matter that he said should be left to personal conscience.

“The governor didn’t whip a single vote on this, unlike what I just saw across the aisle,” said Simon, who also questioned the notion that the state could not execute prisoners without the measure. “Nothing’s stopping the Department of Corrections from investigating new drug protocols. They just can’t do it in secret.”

Herring’s legal opinion came a little more than a week after McAuliffe drastically amended a Republican bill intended to let the state use the electric chair when it cannot obtain lethal-injection drugs. The drugs have become hard to obtain amid a European export ban and public pressure on U.S. pharmaceutical companies not to supply them.

McAuliffe’s amendment will scrap that approach and instead allow the state to specially order the drugs from compounding pharmacies, whose identities would be kept secret to shield them from political pressure.

The plan has been controversial not only among opponents of the death penalty, but also among conservative and liberal critics of government secrecy.

“The governor’s amendments will keep secret the manufacturers of drugs used to kill Virginia’s death-row inmates by lethal injection, as well as the chemical nature of those drugs,” said Claire Guthrie Gastañaga, executive director of the American Civil Liberties Union of Virginia. “Injecting secrecy into the process will authorize Virginia to use new, untested, unregulated, undisclosed drugs on human beings against their will.”

McAuliffe has said the state would not have a way to execute death-row inmates without the pharmacy plan and its secrecy provisions.

Virginia inmates facing the death penalty can choose between the electric chair and lethal ­injection. Del. Jackson H. Miller (R-Manassas) sponsored a bill this year to make the electric chair the default method of execution when the state cannot obtain ­lethal-injection drugs.

In amending Miller’s bill, McAuliffe said he was trying to find a way to avoid the use of the electric chair, which he called a “reprehensible” method of execution.

Herring, who is running for reelection in 2017, passed judgment on the legality of the plan but did not weigh in on its merits.

His opinion disappointed some Democrats opposed to capital punishment. But it pleased some Republicans, who are split over the secrecy provisions but eager to get the attorney general to weigh in on an issue unlikely to be popular with the liberal Democratic base. Herring supports capital punishment but is better known for advancing liberal causes such as gay rights, abortion access and immigration.

“The opinion is a strong legal endorsement of capital punishment in Virginia,” Moran said in an email. “The attorney general’s opinion will be helpful as the House considers the governor’s amendment Wednesday.”

Two opponents of the death penalty, Simon and state Sen. Scott A. Surovell (D-Fairfax), also jointly sought Herring’s opinion last week. Miller also separately asked for Herring’s view last week, although he said he was inclined to support McAuliffe’s amendment. Miller had asked Herring to address questions that the state’s pharmacy chief had privately raised in emails in 2014.

Caroline D. Juran, executive director of the Virginia Board of Pharmacy, had wondered whether such a plan might violate laws requiring that drugs be dispensed only with a valid prescription and only for medicinal or therapeutic purposes. She also questioned whether the secrecy provisions could prevent authorities from investigating a pharmacy in the event of a botched execution.

Herring said that the federal Food and Drug Administration “has concluded that it lacks clear regulatory authority over the use of drugs for purposes of conducting executions, and courts will likely be constrained to defer to the FDA’s reasonable construction.” He also said that ordinary prescription requirements would not apply to drugs obtained for executions.

Surovell and Simon raised some of the same issues, and they also questioned whether the secrecy surrounding the drugs would violate the constitutional right of death-row inmates to gather information about their pending executions.

“Upon a showing of good cause, a judge presiding over either civil proceeding has the authority and discretion to fashion appropriate safeguards to allow for access to relevant information by party litigants,” Herring wrote.

Surovell and Simon were not swayed by Herring’s opinion.

“The attorney general’s office has been guiding the Department of Corrections conduct for decades and if he had opined differently, he would expose the state and multiple employees to significant criminal and civil liability,” Surovell said.