It's legal to unlock your iPhone if you own it outright with no remaining payments due. If you're still in the process of paying off your phone or you have other reasons to be beholden to the carrier, it's definitely a good idea to check with your carrier prior to unlocking your iPhone.