Is it legal to unlock an iPhone without getting to actual company to do it?

I'm just really curious because now that the iPhone 5 is out, iPhone 4S is much cheaper, and iPhone 4 is free on AT&T. I have T-Mobile so I feel really left out. I have an Android, Motorola Defy, but it just doesn't seem as great as when I had an iPod Touch, so clearly iPhone would be even better than that! I've been looking online and noticed on some sites like Craigslist that people unlock iPhones for really cheap. I was wondering if this could be my answer for a T-Mobile iPhone, but I don't know if it's legal to do since Apple didn't approve of it being unlocked for me. So if I did get it unlocked, but then something bad happens and I need to send it back, would I get in trouble! Can someone please help?!~

It is legal but it will void the warranty.