How do you figure that unlocking the iphone is perfectly legal? It would be perfectly legal to unlock an iPhone by flashing custom firmware and an operating system to it. But if you intend to modify apple's firmware and continue using apple's OS then you would be violating the terms of the Software License Agreement. You don't own OSX, you license it for use.
Note that violating the software agreement in this case isn't a crime; it's simply a civil breach. However, there is an establish law which says that customers have the right to unlock phones if they wish. If Apple tried to take you to court (which they wouldn't anyways; you're not worth the legal fees to them) they wouldn't fair so well simply because there would be a good defense in the form of, "every other phone is unlockable, what makes yours the exception?" If a case like this proceeded, I would imagine that the verdict would come out in favor of the defendant, and Apple's software agreement would essentially be useless in stopping unlockers (as the legal principle would have been established via a jury verdict or judicial decision).