On the Republican campaign trail, he derides any such public aid. But the healthcare law he signed as Massachusetts governor allows it.

Reporting from Washington — The Massachusetts healthcare law that then-Gov. Mitt Romney signed in 2006 includes a program known as the Health Safety Net, which allows undocumented immigrants to get needed medical care along with others who lack insurance.

Uninsured, poor immigrants can walk into a health clinic or hospital in the state and get publicly subsidized care at virtually no cost to them, regardless of their immigration status.

The program, widely supported in Massachusetts, drew little attention when Romney signed the trailblazing healthcare law. But now it could prove problematic for the Republican presidential hopeful, who has been attacking Texas Gov. Rick Perry for supporting educational aid for children of undocumented immigrants in Texas.

"We have to turn off the magnet of extraordinary government benefits," Romney said at the recent Fox News-Google debate in Florida.

Perry has defended the Texas program, saying it is better to educate young people, even if they are in the country illegally, to help them become productive members of society.

Similarly, supporters of the Massachusetts program note there are ultimately higher costs for denying care to sick patients regardless of their immigration status.

The Massachusetts program, which cost more than $400 million last year, paid for 1.1 million hospital and clinic visits. It's unclear how many undocumented patients benefited because the state does not record that data.

The Romney campaign referred questions to Tim Murphy, who served as Romney's state health and human services secretary. Murphy said the governor never intended the Health Safety Net to serve undocumented immigrants.

"Our view when we signed the law was that all benefits would be for people in the commonwealth who were here legally," Murphy said, noting that the regulations implementing the program were written after Romney left office in 2007.

But Massachusetts officials involved in crafting the healthcare law said there was broad understanding when Romney signed it that at least some people who would benefit would be in the country illegally.

That's supported by language in the law. Although it explicitly bars undocumented immigrants from getting certain health benefits, it does not prohibit them from receiving aid through the Health Safety Net.

For example, the law mandates that only noncitizens "permanently residing in the United States under color of law" may receive government subsidies to buy health insurance.

The law also spells out that undocumented immigrants aren't eligible for the state's Medicaid insurance program for the poor, known as MassHealth.

But the Health Safety Net, in contrast, is off-limits only to people who "moved into the commonwealth for the sole purpose of securing health insurance" or who are eligible for another insurance plan.

"There is no question that lots of different kinds of people, including undocumented immigrants, obtain medically necessary services as result of this program," said John McDonough, a former consumer advocate who worked extensively on the healthcare effort in Massachusetts.

Massachusetts created the Health Safety Net, which built on a previous program, because state leaders worried that some residents would remain uninsured even though the state's law guarantees coverage to nearly everyone.

Those still without insurance — including some of the state's estimated 160,000 undocumented immigrants — were expected to seek care at hospital emergency rooms and community health clinics.

Hospitals are prohibited by law from turning away patients in need of care, regardless of immigration status. And many clinics do not check whether their patients are in the country legally.

The state-federal Medicaid program allows hospitals nationwide to be reimbursed for some of this care.

But some states and communities go further, offering additional aid to undocumented immigrants and the hospitals and other providers that provide them with care.

Such a policy, some healthcare experts believe, can help control costs by making less expensive care available instead of forcing critically ill, uninsured patients to go to emergency rooms, where care is far more costly.

Massachusetts funds its Health Safety Net with some state money and by assessing fees on hospitals and insurers. The money is redistributed to providers who file claims for the patients who sign up for the program.

In the state, there have been few complaints.

"We will always have people without health insurance, despite the incredible progress we've made," said Sarah Iselin, a former state health official and current president of the Blue Cross Blue Shield of Massachusetts Foundation.

"The Health Safety Net is a vital program that ensures low-income residents without coverage still have some access to healthcare — and not just in expensive emergency rooms — and shares the costs across the entire healthcare system."