"Any one may so arrange his affairs that his taxes shall be as low as possible; he is not bound to choose that pattern which will best pay the Treasury; there is not even a patriotic duty to increase one's taxes."
Gregory v. Helvering, decided by the US Supreme Court in 1935.
Edit: So if it's a legal arrangement that reduces taxes, then yes, it's legal.