Yes this is legal.
You'll need to find the firmware version you want to downgrade to online. Try Googling for it.
Make a backup of anything you want to save. Next, plug the phone into your PC and wait for iTunes to connect to it.
Hold the power button + home button until it turns off. Release the power button and continue to hold the home button until iTunes reconnects to it. The screen should remain blank at this point. This is known as DFU mode.
In the iTunes window, navigate to the iPhone tab. Hold shift and click the restore button.
In the window that pops up, navigate to the firmware file you downloaded, then open it.
iTunes should now begin restoring the phone to the specified firmware.