The legal problem with Afghanistan was that it did not declare war.
Yes Afghanistan or the Taliban did declare a Jihad/ war when we refused negotiations to turn over Bin Laden to a Middle Eastern country for trial. I am a bit hazy on the exact timeline but it was before the first bomb dropped.