A top secret document reveals New Zealand's surveillance agency spied on candidates vying to be the director-general of the World Trade Organisation (WTO), a job sought by National Government minister Tim Groser.
The Government Communications Security Bureau (GCSB) programmed an internet surveillance system so it would intercept emails about the candidates from Indonesia, Mexico, South Korea, Brazil, Kenya, Ghana, Jordan and Costa Rica in the period leading up to the May 2013 appointment.
Mr Groser missed the selection.
The minister in charge of the GCSB at the time was Prime Minister John Key, raising the question of whether he knew about and personally approved the electronic eavesdropping to help Mr Groser.
Under Cabinet "no surprises" rules, officials would be likely to ensure that the responsible minister was briefed on such a sensitive operation.
One of the WTO candidates targeted in the GCSB surveillance was the South Korean Minister of Trade, Taeho Bark. Mr Bark had agreed to restart stalled South Korea-New Zealand free trade talks when he met Mr Groser in October 2012, a few months before the surveillance. Mr Key and Mr Groser are in South Korea this week to sign the trade agreement that Mr Bark helped make happen.
Deploying GCSB's surveillance capabilities to gain the upper hand in the WTO selection is far away from terrorism, the Islamic State and other security issues for which Mr Key has claimed the agency is used.
It is reminiscent of revelations in Europe about the British surveillance agency Government Communications Headquarters (GCHQ) monitoring politicians and officials attending a 2009 G20 summit meeting in London.
The Groser operation, called the "WTO Project" within the GCSB, is detailed in a top secret intelligence document that was obtained by the Herald and US news site The Intercept.
The document is stamped with a "last modified" date of May 6, 2013, about a week before the new WTO director-general was to be announced.
It shows that a team inside GCSB used the XKeyscore surveillance system to search intercepted internet communications for emails about Mr Groser and the eight other candidates for the job.
XKeyscore is run by the US National Security Agency (NSA) and is used to analyse billions of emails, internet browsing sessions and online chats that are intercepted from some 150 different locations worldwide.
GCSB has gained access to XKeyscore because New Zealand is a member of the Five Eyes surveillance alliance alongside the United States, the United Kingdom, Canada and Australia.
The secret document revealing the WTO spying shows a targeting "fingerprint", a combination of names and keywords used to extract particular information from the vast quantities of data accessible through XKeyscore.
The document notes that the WTO Project fingerprint was "used to sort traffic by priority", looking for "keywords [as they] appear in the email-body".
The intercepted emails were to be passed to the GCSB's "Trade team", which would have had the job of collating intelligence for people in Government involved in Mr Groser's bid for the WTO role.
Two different intelligence searches were carried out by the GCSB staff.
First, they looked for emails referring to Groser, the WTO, the director general candidacy, and the names of the other candidates: Alan John Kwadwo Kyerematen (Ghana); Amina Mohamed (Kenya); Anabel Gonzlez (Costa Rica); Herminio Blanco (Mexico); Mari Elka Pangestu (Indonesia); Taeho Bark (South Korea); Ahmad Thougan Hindawi (Jordan); and Roberto Carvalho de Azevdo (Brazil).
Second, they zeroed in on the Indonesian candidate, Mari Pangestu, that country's former Minister of Trade, a professional economist and the first Chinese Indonesian woman to hold a cabinet post.
A special XKeyscore fingerprint was created headed "WTO DG Candidacy issues - focus on Indonesian candidate".
This was presumably because the Government was concerned that the job might go to another Pacific candidate ahead of Groser.
The surveillance of Ms Pangestu had its own operational name: "WTO Project Pangestu".
It appears to have targeted all internet communications (not just emails) containing the name "Pangestu", the words "Indonesia", "WTO" and "candidacy", and the other candidates' names.
The searches had keyword instructions in three languages, English, French and Spanish - for instance "zealand", "zelande" and "zelandia" - in order to catch emails from more countries.
Ultimately, it was Brazil's Azevedo who was appointed the WTO's new director-general on May 14, 2013. Three weeks earlier, it had become clear that Mr Groser was not going to make the final shortlist. Mr Key expressed his disappointment that his colleague had missed out on the selection.
"At the end of the day it was always going to be a long shot - so he gave it his best go with the support of the Government," Mr Key said at the time.
What no one knew at the time was that this support had included deploying the GCSB to spy on the competitors.
Mr Groser spent the early months of 2013 travelling around the world to lobby governments to back him as director-general. The Herald reported in April 2013 that, from January-March that year, Mr Groser spent $249,000 of public money on international travel.
While the New Zealand Government collected intelligence on the other eight countries' candidates, it is unlikely that those countries were spying on Mr Groser and New Zealand's lobbying effort in return. None of the eight countries targeted in the operation have the capability to conduct surveillance against the internet on a global level.
The Groser spying document was prepared by a named staff member in the GCSB's SIGDEV unit (signals intelligence development). These staff organise intelligence collection on behalf of the intelligence analysis sections, where the intercepted communications are written into reports for the New Zealand Government and overseas allies.
Earlier this month, the Herald revealed that the GCSB helps the United States' NSA by conducting electronic spying operations against a wide range of countries including China, India, Pakistan, Vietnam, Iran, Japan, North Korea and South American and Pacific Island nations.
An April 2013 NSA document said the GCSB "continues to be especially helpful in its ability to provide NSA ready access to areas and countries that are difficult for the United States to access".
In exchange for these contributions, the New Zealand Government is allowed to use the allies' powerful surveillance systems for its own projects.
The Key Government's spying on Mr Groser's WTO competitors is an example of this.
Nicky Hager is a New Zealand-based investigative journalist and an internationally recognised expert on surveillance since the publication of his book Secret Power in 1996. Ryan Gallagher is a Scottish journalist whose work at US news organisation The Intercept is focused on government surveillance, technology and civil liberties.
Read The Intercept story here.