Is it legal? What about my terms of agreement?

It is 100% legal.

Unlocking your phone and the terms of agreement with your network provider are independent of each other.

The terms of agreement are linked to the service and not the phone. Unlocking the phone will allow its use with any network provider, but the terms of your agreement remain in place until they expire or are cancelled.

Unlock Phone