I believe the use of a contract and signatures in fact does literally make it legal, since that is the point of a contract. Everything depends on what you signed.
It is the job of HR & Legal depts to make sure a contract is legally viable, not mine, and I happen to know my rights. There are at least 5 illegal clauses in my contract, and I am certainly keeping them close by if I need to go to court against them one day.