Sunfish,
The Iranian tanker actually stopped of its own accord to take on stores in Gibraltar's territorial waters - in exactly the same way that many other merchant ships do. It was not just conducting a transit of the area.
Membership of the EU requires any state to enforce the EU sanctions currently in place. Gibraltar did just this.
Gibraltar asked for, and was given, the assistance of Royal Marines (who specialise in this field) to assist the Gibraltar police in apprehending the vessel.
You are right. The boarding of a ship in transit of a Strait, especially by armed force, is an illegal act - which is exactly what the Iranians have done.
It is of note that the Iranian ship is full of Iranian oil; the Stena Impero is in ballast and has NO cargo.
HWB