What does "legal" even really mean between states at war. The consequences typically come down to a popularity contest and Iran is one of the few states with fewer friends than Israel.
Was Iran's activities funding militias in Lebanon, Syria, Iraq and Yemen, which launched attacks against Israel and US forces "legal"? Which of the US' activities were "legal"? It's all mostly a bad joke.
It's tricky. Arming a country or group than then launches an attack, or uses those weapons in a war, doesn't make you a participant in that conflict. This is why Europe and the US can supply weapons to Ukraine without being participants in a conflict with Russia.
However Iran has the stated intention of destroying the state of Israel, and actively incites it's proxies to attack Israel, and this could be seen as a valid justification for taking action. Not a lawyer though.
Was Iran's activities funding militias in Lebanon, Syria, Iraq and Yemen, which launched attacks against Israel and US forces "legal"? Which of the US' activities were "legal"? It's all mostly a bad joke.