Iran 's supreme leader Ayatollah Ali Khamenei has formally endorsed Mahmoud Ahmadinejad as the country 's next president .