I have two master's degrees from Spain and Italy. I need to apply for a German visa for my PhD and they require my documents to be legalized by an apostille stamp. I live in Iran and cannot travel to Europe to stamp them. How can I get the stamps from abroad? Would the consulates of Italy/Spain in Tehran provide this service?
Here is HCCH's list of the authorities that issue apostilles for each country.
For Spain, here is a government page about getting an apostille on documents from Spain. It says:
Legalisation is free of charge when carried out by the Legalisations Section of the Ministry of Foreign Affairs and Cooperation (C/ Pechuán, 1 – 28002 Madrid. Tel: +34 91 379 16 55). If carried out at an Embassy or Consulate of Spain, it shall involve payment of a fee. For more specific information, it is advisable to directly contact the corresponding representation of Spain abroad.
For Italy, it may be trickier as it goes by each province. Here is what HCCH says for Italy.