Spain's government has finalised an amnesty measure that it estimates could enable half a million undocumented migrants to apply for legal status.