Is it legal to ask for your social security number?

Yes, it is "legal" to ask for your social security number. And it is legal because it is NOT against the law to do so. What may or may not be legal, in some cases, is to ask someone to provide a social security number as a requirement to have access, for instance, to a benefit to which that same person is entitled by law.