@diver saidThere is a long history on the justifications for war, going back at least as far as Th. Aquinas. Philosophers distinguish:
The notion that wars can be either ‘legal’ or ‘illegal’ is to me bizarre; as is the framing that terrorism by one nation against another isn’t ‘war’.
1. legitimate reasons for going to war;
2. legitimate means of prosecuting war;
3. legitimate targets.
Any given war may fail at one or more points.
Just to give a few examples of each point:
1. any country which is invaded by a foreign military force may defend itself with military force.
1. Any country which abducts or assassinates another country's head of state may expect legitimate military retaliation.
1. Any country which gives safe harbor to pirates or terrorists may expect legitimate military confrontation from countries whose legitimate (including economic or trade) interests are compromised by piracy &/or terrorism.
2. Numerous means have been ruled illegitimate, including chemical and biological weapons, and re-functioning otherwise harmless objects of daily use (such as building remote controllable explosives into cell phones).
3. Numerous targets have been ruled illegitimate, including civilian infrastructure and medical facilities (whether civilian or military).
These points have been settled by international treaty, long ago.