Iran is a Non-Nuclear Member State of the Non-Proliferation Treaty. As such it is not allowed to make or own nuclear weapons. If Iran were to build a nuclear weapon it would be in breach of the NPT.
(The U.S. intelligence community has judged that Iran does not have a nuclear weapon program and there is no sign that it will ever attempt to make any nuclear weapon.)
But if attacked, despite being a member of the Non-Proliferation Treaty, Iran would have the right to build nuclear weapons. That is at least the legal standpoint of the United States government and of other NATO members.
NATO's doctrine and treaties include nuclear sharing which is:
a concept in NATO's policy of nuclear deterrence, which involves member countries without nuclear weapons of their own in the planning for the use of nuclear weapons by NATO, and in particular provides for the armed forces of these countries to be involved in delivering these weapons in the event of their use.
…
As of November 2009, Belgium, Germany, Italy, the Netherlands and Turkey are still hosting U.S. nuclear weapons as part of NATO's nuclear sharing policy.
The countries mentioned are, like Iran, Non-Nuclear Member States of the NPT. But they do host US-owned nuclear weapons and their pilots regularly train how to use them.
The Non-Proliferation Treaty says:
Article I: Each nuclear-weapon State Party to the Treaty undertakes not to transfer to any recipient whatsoever nuclear weapons or other nuclear explosive devices or control over such weapons or explosive devices directly, or indirectly; […]
Article II: Each non-nuclear-weapon State Party to the Treaty undertakes not to receive the transfer from any transferor whatsoever of nuclear weapons or other nuclear explosive devices or of control over such weapons or explosive devices directly, or indirectly; […]
Nuclear sharing, the planed handing over of nuclear weapons to non-nuclear weapon states in case of war, as practiced by NATO, would clearly breach Article I and II of the Non-Proliferation Treaty.
But that is fine, say NATO and the US ever since the NPT came into force. They state, as discussed in detail here, that the NPT:
"does not deal with arrangements for deployment of nuclear weapons within allied territory as these do not involve any transfer of nuclear weapons or control over them unless and until a decision were made to go to war, at which time the treaty would no longer be controlling".
In case of war, says the U.S. and NATO, the Non-Proliferation Treaty is "no longer controlling" and can therefore, they argue, then be breached without legal consequences.
If the United States, Israel or whoever attacks Iran (which would be illegal under national and international law) the NPT would be "no longer controlling" and Iran could, using the U.S. argument, build nuclear weapons without breaching its legal obligations under the NPT.
The argument might look practically irrelevant but it is not. If the U.S. attacks Iran a state of war with Iran would continue even after the immediate shooting is over until an official peace treaty would be signed. That could take years. If Iran would, during that time, build nukes there would then be no legal basis for a United Nations Security Council resolution to censure Iran for breaching the NPT.
The U.S. often likes to interpret international laws and treaties like the NPT liberally for itself. But such a view of the law can also be used by others and the above shows the implicit danger of such interpretations.