New Zealand's spy agency, the SIS, broke into the Indian High Commission for MI6 and the Iranian Embassy for the CIA in the late 1980s and early 1990s to photograph code books, plant bugs and steal communications.
The operations included at least two raids on the Indian High Commission in Wellington in 1989 and 1991 to photograph thousands of pages from the commission's code books, which were used to encrypt communications.
The covert attack on the Indian High Commission was code-named Operation Dunnage and was a joint mission between the New Zealand SIS and Britain's MI6.
Thousands of photographs containing the codes were sent back to the UK so that Britain's foreign intelligence service could decipher the communications of Indian government officials and diplomats.
RNZ has also learned that in the early 1990s the New Zealand SIS targeted the Iranian embassy in Wellington in a mission named Operation Horoscope, which was driven by the CIA.
The CIA altered circuit boards on a telex machine used by the Iranian Embassy in Wellington, allowing the American intelligence agency to intercept Iranian communications.
The SIS entered the embassy for the CIA, photographed the building and installed listening devices supplied by the CIA.
Operation Horoscope involved months of covert work and remained active for many years afterwards.
RNZ learned about the raids after piecing together information gained after months of engaging with multiple sources in New Zealand, Britain and the US.
One New Zealand source, who has spent more than 20 years at the highest levels of the public sector, told RNZ he was concerned about the nature of the work the SIS carried out for its Five Eyes partners.
The source, who has had close dealings with the intelligence agencies, said New Zealand came under pressure from its Five Eyes partners, especially the US and Australia, to do their dirty work.
He felt New Zealand sometimes risked its international reputation by doing things that largely benefited Five Eyes partners.
The source said the embassy raids uncovered by RNZ needed to be made public as the disclosure might help keep the SIS more tightly "on the leash".
In a statement, the SIS said it was "unable to respond to questions about what may or not be specific operational matters".
"The mission of the NZSIS has always been to keep New Zealanders safe, protect our key national institutions and promote New Zealand's national advantage," the statement said.
It said the SIS had always been subject to processes that ensured its activities were authorised, even though the details of those processes have changed over time in line with changes in legislation.
Former prime minister Helen Clark also expressed her concern about New Zealand drifting too close to its Five Eyes partners in an interview for The Service podcast about New Zealand's role in the Cold War.
The Service, made by RNZ and Bird of Paradise Productions, revealed multiple embassy break-ins, including a joint SIS-MI6 raid on the Czechoslovakian Embassy in 1986 to steal the Warsaw Pact codes.
The SIS officer in charge of the raid and the head of the prime minister's department at the time, Gerald Hensley, both claimed in the podcast that the raid was unsuccessful and the SIS failed to get the code books.
Hensley confirmed there were multiple embassy raids over many years but that the practice ceased after fears grew about the potential for international fallout.
Breaking into an embassy is a breach of the Vienna Convention, an international treaty that states that embassies are inviolable and the host nation should never so much as open the diplomatic mailbag.
Sir Geoffrey Palmer, New Zealand's prime minister between August 1989 and September 1990, said he had not heard of the raids on the Indian and Iranian embassies but should have been alerted by the SIS if they occurred when he was in charge of the agency.
"If it was at the time I was prime minister, I most certainly should have been."
Jim Bolger, prime minister from 1990 to 1997, said he could not recall ever signing any warrants to allow the SIS to break into foreign embassies.
He expressed surprise that there had been a raid on the Indian High Commission and asked why New Zealand would want to carry out a covert attack on that country.
"I have no recollection of that ever hitting my desk and if it did, I have to say, my memory is not gone yet, I'd be very surprised if I was ever advised of any such event. I have no recollection - and that's not just a brush-off."
Former and current ministers responsible for New Zealand's intelligence agencies would not give solid answers on whether the SIS still breaks into foreign embassies.
Helen Clark refused to confirm or deny whether raids happened during her time as prime minister between 1999 and 2008.
Andrew Little, Minister for the Intelligence Agencies, refused to deny signing-off raids.
If the SIS was still conducting embassy raids like the covert attacks on the Indian and Iranian embassies, it was likely unlawful, Palmer said.
"None of that could be done under the existing law it seems to me. Quite apart from the breaches of the Vienna Convention you've got breaches of New Zealand law there, I would have thought, you've got breaches of human rights."
Asked whether he thought New Zealand should rule out breaking into foreign embassies, he said: "I think New Zealand should be in the position of saying it follows all the legal requirements of its own legislation and it does seem to me that those would rule this out."
The SIS has acted with few constraints in the past and operated without any legislation at all for the first 13 years after it was established in 1956, Palmer said
The current legislation, the Intelligence and Security Act 2017, was more stringent, he said. "They have got to act in accordance with New Zealand law, they have got to follow all human rights obligations that are contained in New Zealand. They have to act with integrity."
This extended to operations done in partnership with Five Eyes partners such as MI6 and the CIA.
"When they are dealing with requests from other agencies they [must] facilitate things that are appropriate for New Zealand law."
They also had to "abide by democratic obligations", which meant ministers should be told about the operations and properly consent to them, he said.
The motivations for the raids on the Indian and Iranian embassies remain unknown, although both countries would have been of particular interest to the Five Eyes alliance at the time.
India, a nuclear power since the mid-1970s, suffered intense political unrest in the late 1980s and early 1990s, which included the 1991 assassination of former prime minister Rajiv Gandhi by Sri Lanka's Tamil Tigers.
Meanwhile, Iran had just resumed diplomatic relations with Iraq after a decade of war and in 1990 it remained neutral during Iraq's invasion of Kuwait.
Iran had long suspected the CIA was intercepting its communications. Those fears intensified after the 1991 assassination of former Iranian prime minister Shapour Bakhtiar.
In August 1991, before Bakhtiar's body was discovered, messages were sent to Iranian embassies around the world asking whether he was dead and the message was deciphered by Western intelligence agencies.
Iran suspected the CIA had access to its coding machines used for secure communications. In 1992, Iran arrested a salesman who worked for the Swiss company that manufactured the coding machines, Crypto AG. He was released nine months later after a ransom of US$1 million was paid and he returned to Switzerland.
In February this year, the Washington Post revealed that the CIA and their West German counterparts, the BND, had controlled Crypto AG, which made coding machines for dozens of countries, including Iran and India.
The Washington Post story said weaknesses were deliberately programmed into the Crypto AG hardware, giving the CIA decades of access to the communications of the countries who used the machines.