It is illegal in Germany. The German Criminal Code (Strafgesetzbuch, StGB) says:
Causing false records
(1) Whoever causes declarations, negotiations or facts which are of relevance for rights or legal relationships to be recorded or stored in public documents, books, data files or registers as having been made or having occurred or having been stored although they were not actually made or did not occur, or were made or occurred in another manner, or by a person lacking a professed capacity or by a different person incurs a penalty of imprisonment for a term not exceeding three years or a fine.
(4) The attempt is punishable.
Procurement of false official identity documents
undertakes to import or export or
with the intention of using it to facilitate deception in legal commerce, procures for themselves or another, stores or gives to another
a counterfeit or falsified official identity document or an official identity document which contains a false notarial recording of the type indicated in sections 271 and 348 incurs a penalty of imprisonment for a term not exceeding two years or a fine.
A "public document" in the sense of § 271 I StGB is also a foreign document if it has some reference to Germany (Freund, in: Münchener Kommentar zum StGB, 3. Auflage 2019, § 271 Rn. 16; this are esp. passports, KG Berlin, Urteil vom 29. November 1979 – (4) Ss 348/79 (131/79), JR 1980, 516f.). A "official identity document" in § 276 I is also a foreign document, e.g. a passport (Erb, in: Münchener Kommentar zum StGB,
3. Auflage 2019, § 276 Rn. 2; BGH, Beschluss vom 29.7.2000, 1 StR 238/00, NJW 2000, 3148).
Though a foreigner causing false records in foreign countries does not stand in the scope of the German crimnal law, such a constallation is not mentioned in §§ 3-7 StGB (except special circumstances in § 7 II). So to fraudulently obtain a foreign passport alone is not a crime in Germany.
But to import or export such a passport or to store the passport with the intent to use it for forgery is a crime under § 276 StGB. As foreign identity documents are used domestically on a regular basis when dealing with foreigners (as a replacement for an ID, for example when signing contracts or identifying yourself on receiving some kinds of parcels), it is reasonable to punish such acts domestically.