Yes, it is legal to purchase a Windows licence from a 3rd party retailer, however, it is not possible to check if they are official resellers or indeed if the licences they are selling are genuine consumer licences.
Avoid any websites that are selling very cheap Windows licences, because usually those are volume licences that may initially activate, but would soon after automatically deactivate, and you would not be able to re-activate the Windows licence.