aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/operations/ExportTest.java
blob: 6fab979ed8a95a8de961512accfb76678f7fd4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.operations;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.security.Security;
import java.util.Iterator;

import android.app.Application;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;

import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;
import org.sufficientlysecure.keychain.WorkaroundBuildConfig;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.operations.results.ExportResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.operations.results.PgpEditKeyResult;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow;
import org.sufficientlysecure.keychain.pgp.WrappedSignature;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
import org.sufficientlysecure.keychain.service.BackupKeyringParcel;
import org.sufficientlysecure.keychain.service.ChangeUnlockParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.Algorithm;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.Passphrase;
import org.sufficientlysecure.keychain.util.ProgressScaler;
import org.sufficientlysecure.keychain.util.TestingUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = WorkaroundBuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml")
public class ExportTest {

    static Passphrase mPassphrase = TestingUtils.genPassphrase(true);

    static UncachedKeyRing mStaticRing1, mStaticRing2;
    static Passphrase mKeyPhrase1 = TestingUtils.genPassphrase(true);
    static Passphrase mKeyPhrase2 = TestingUtils.genPassphrase(true);

    static PrintStream oldShadowStream;

    @BeforeClass
    public static void setUpOnce() throws Exception {
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
        oldShadowStream = ShadowLog.stream;
        // ShadowLog.stream = System.out;

        PgpKeyOperation op = new PgpKeyOperation(null);

        {
            SaveKeyringParcel parcel = new SaveKeyringParcel();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.CERTIFY_OTHER, 0L));
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.SIGN_DATA, 0L));
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDH, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.ENCRYPT_COMMS, 0L));
            parcel.mAddUserIds.add("snips");
            parcel.setNewUnlock(new ChangeUnlockParcel(mKeyPhrase1));

            PgpEditKeyResult result = op.createSecretKeyRing(parcel);
            assertTrue("initial test key creation must succeed", result.success());
            Assert.assertNotNull("initial test key creation must succeed", result.getRing());

            mStaticRing1 = result.getRing();
        }

        {
            SaveKeyringParcel parcel = new SaveKeyringParcel();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.CERTIFY_OTHER, 0L));
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.SIGN_DATA, 0L));
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDH, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.ENCRYPT_COMMS, 0L));
            parcel.mAddUserIds.add("snails");
            parcel.setNewUnlock(new ChangeUnlockParcel(new Passphrase("1234")));

            PgpEditKeyResult result = op.createSecretKeyRing(parcel);
            assertTrue("initial test key creation must succeed", result.success());
            Assert.assertNotNull("initial test key creation must succeed", result.getRing());

            mStaticRing2 = result.getRing();
            mStaticRing2 = UncachedKeyRing.forTestingOnlyAddDummyLocalSignature(mStaticRing2, "1234");
        }

    }

    @Before
    public void setUp() {
        ProviderHelper providerHelper = new ProviderHelper(RuntimeEnvironment.application);

        // don't log verbosely here, we're not here to test imports
        ShadowLog.stream = oldShadowStream;

        providerHelper.saveSecretKeyRing(mStaticRing1, new ProgressScaler());
        providerHelper.saveSecretKeyRing(mStaticRing2, new ProgressScaler());

        // ok NOW log verbosely!
        ShadowLog.stream = System.out;
    }

    @Test
    public void testExportAllLocalStripped() throws Exception {
        BackupOperation op = new BackupOperation(RuntimeEnvironment.application,
                new ProviderHelper(RuntimeEnvironment.application), null);

        // make sure there is a local cert (so the later checks that there are none are meaningful)
        assertTrue("second keyring has local certification", checkForLocal(mStaticRing2));

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        boolean result = op.exportKeysToStream(new OperationLog(), null, false, out);

        assertTrue("export must be a success", result);

        long masterKeyId1, masterKeyId2;
        if (mStaticRing1.getMasterKeyId() < mStaticRing2.getMasterKeyId()) {
            masterKeyId1 = mStaticRing1.getMasterKeyId();
            masterKeyId2 = mStaticRing2.getMasterKeyId();
        } else {
            masterKeyId2 = mStaticRing1.getMasterKeyId();
            masterKeyId1 = mStaticRing2.getMasterKeyId();
        }

        IteratorWithIOThrow<UncachedKeyRing> unc =
                UncachedKeyRing.fromStream(new ByteArrayInputStream(out.toByteArray()));

        {
            assertTrue("export must have two keys (1/2)", unc.hasNext());
            UncachedKeyRing ring = unc.next();
            Assert.assertEquals("first exported key has correct masterkeyid",
                    masterKeyId1, ring.getMasterKeyId());
            assertFalse("first exported key must not be secret", ring.isSecret());
            assertFalse("there must be no local signatures in an exported keyring",
                    checkForLocal(ring));
        }

        {
            assertTrue("export must have two keys (2/2)", unc.hasNext());
            UncachedKeyRing ring = unc.next();
            Assert.assertEquals("second exported key has correct masterkeyid",
                    masterKeyId2, ring.getMasterKeyId());
            assertFalse("second exported key must not be secret", ring.isSecret());
            assertFalse("there must be no local signatures in an exported keyring",
                    checkForLocal(ring));
        }

        out = new ByteArrayOutputStream();
        result = op.exportKeysToStream(new OperationLog(), null, true, out);

        assertTrue("export must be a success", result);

        unc = UncachedKeyRing.fromStream(new ByteArrayInputStream(out.toByteArray()));

        {
            assertTrue("export must have four keys (1/4)", unc.hasNext());
            UncachedKeyRing ring = unc.next();
            Assert.assertEquals("1/4 exported key has correct masterkeyid",
                    masterKeyId1, ring.getMasterKeyId());
            assertFalse("1/4 exported key must not be public", ring.isSecret());
            assertFalse("there must be no local signatures in an exported keyring",
                    checkForLocal(ring));

            assertTrue("export must have four keys (2/4)", unc.hasNext());
            ring = unc.next();
            Assert.assertEquals("2/4 exported key has correct masterkeyid",
                    masterKeyId1, ring.getMasterKeyId());
            assertTrue("2/4 exported key must be public", ring.isSecret());
            assertFalse("there must be no local signatures in an exported keyring",
                    checkForLocal(ring));
        }

        {
            assertTrue("export must have four keys (3/4)", unc.hasNext());
            UncachedKeyRing ring = unc.next();
            Assert.assertEquals("3/4 exported key has correct masterkeyid",
                    masterKeyId2, ring.getMasterKeyId());
            assertFalse("3/4 exported key must not be public", ring.isSecret());
            assertFalse("there must be no local signatures in an exported keyring",
                    checkForLocal(ring));

            assertTrue("export must have four keys (4/4)", unc.hasNext());
            ring = unc.next();
            Assert.assertEquals("4/4 exported key has correct masterkeyid",
                    masterKeyId2, ring.getMasterKeyId());
            assertTrue("4/4 exported key must be public", ring.isSecret());
            assertFalse("there must be no local signatures in an exported keyring",
                    checkForLocal(ring));
        }

    }

    @Test
    public void testExportUnencrypted() throws Exception {

        ContentResolver mockResolver = mock(ContentResolver.class);

        Uri fakeOutputUri = Uri.parse("content://fake/out/1");
        ByteArrayOutputStream outStream1 = new ByteArrayOutputStream();
        when(mockResolver.openOutputStream(fakeOutputUri)).thenReturn(outStream1);

        Application spyApplication = spy(RuntimeEnvironment.application);
        when(spyApplication.getContentResolver()).thenReturn(mockResolver);

        BackupOperation op = new BackupOperation(spyApplication,
                new ProviderHelper(RuntimeEnvironment.application), null);

        BackupKeyringParcel parcel = new BackupKeyringParcel(
                new long[] { mStaticRing1.getMasterKeyId() }, false, fakeOutputUri);

        ExportResult result = op.execute(parcel, null);

        verify(mockResolver).openOutputStream(fakeOutputUri);

        assertTrue("export must succeed", result.success());

        TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header",
                "-----BEGIN PGP PUBLIC KEY BLOCK-----\n".getBytes(), outStream1.toByteArray());
        TestingUtils.assertArrayEqualsSuffix("exported data must end with ascii armor header",
                "-----END PGP PUBLIC KEY BLOCK-----\n".getBytes(), outStream1.toByteArray());

        {
            IteratorWithIOThrow<UncachedKeyRing> unc
                    = UncachedKeyRing.fromStream(new ByteArrayInputStream(outStream1.toByteArray()));

            assertTrue("export must have one key", unc.hasNext());
            UncachedKeyRing ring = unc.next();
            Assert.assertEquals("exported key has correct masterkeyid",
                    mStaticRing1.getMasterKeyId(), ring.getMasterKeyId());
            assertFalse("export must have exactly one key", unc.hasNext());
        }

    }

    @Test
    public void testExportEncrypted() throws Exception {


        Application spyApplication;
        ContentResolver mockResolver = mock(ContentResolver.class);

        Uri fakePipedUri, fakeOutputUri;
        ByteArrayOutputStream outStream; {

            fakePipedUri = Uri.parse("content://fake/pipe/1");
            PipedInputStream pipedInStream = new PipedInputStream(8192);
            PipedOutputStream pipedOutStream = new PipedOutputStream(pipedInStream);
            when(mockResolver.openOutputStream(fakePipedUri)).thenReturn(pipedOutStream);
            when(mockResolver.openInputStream(fakePipedUri)).thenReturn(pipedInStream);
            when(mockResolver.insert(eq(TemporaryFileProvider.CONTENT_URI), any(ContentValues.class)))
                    .thenReturn(fakePipedUri);

            fakeOutputUri = Uri.parse("content://fake/out/1");
            outStream = new ByteArrayOutputStream();
            when(mockResolver.openOutputStream(fakeOutputUri)).thenReturn(outStream);

            spyApplication = spy(RuntimeEnvironment.application);
            when(spyApplication.getContentResolver()).thenReturn(mockResolver);
        }

        Passphrase passphrase = new Passphrase("abcde");

        { // export encrypted
            BackupOperation op = new BackupOperation(spyApplication,
                    new ProviderHelper(RuntimeEnvironment.application), null);

            BackupKeyringParcel parcel = new BackupKeyringParcel(
                    new long[] { mStaticRing1.getMasterKeyId() }, false, fakeOutputUri);
            CryptoInputParcel inputParcel = new CryptoInputParcel(passphrase);
            ExportResult result = op.execute(parcel, inputParcel);

            verify(mockResolver).openOutputStream(fakePipedUri);
            verify(mockResolver).openInputStream(fakePipedUri);
            verify(mockResolver).openOutputStream(fakeOutputUri);

            assertTrue("export must succeed", result.success());
            TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header",
                    "-----BEGIN PGP MESSAGE-----\n".getBytes(), outStream.toByteArray());
        }

        {
            PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(RuntimeEnvironment.application,
                    new ProviderHelper(RuntimeEnvironment.application), null);

            PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(outStream.toByteArray());
            input.setAllowSymmetricDecryption(true);

            {
                DecryptVerifyResult result = op.execute(input, new CryptoInputParcel());
                assertTrue("decryption must return pending without passphrase", result.isPending());
                Assert.assertTrue("should contain pending passphrase log entry",
                        result.getLog().containsType(LogType.MSG_DC_PENDING_PASSPHRASE));
            }
            {
                DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(new Passphrase("bad")));
                assertFalse("decryption must fail with bad passphrase", result.success());
                Assert.assertTrue("should contain bad passphrase log entry",
                        result.getLog().containsType(LogType.MSG_DC_ERROR_SYM_PASSPHRASE));
            }

            DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(passphrase));
            assertTrue("decryption must succeed with passphrase", result.success());

            assertEquals("backup filename should be backup_keyid.pub.asc",
                    "backup_" + KeyFormattingUtils.convertKeyIdToHex(mStaticRing1.getMasterKeyId()) + ".pub.asc",
                    result.getDecryptionMetadata().getFilename());

            assertEquals("mime type for pgp keys must be correctly detected",
                    "application/pgp-keys", result.getDecryptionMetadata().getMimeType());

            TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header",
                    "-----BEGIN PGP PUBLIC KEY BLOCK-----\n".getBytes(), result.getOutputBytes());
            TestingUtils.assertArrayEqualsSuffix("exported data must end with ascii armor header",
                    "-----END PGP PUBLIC KEY BLOCK-----\n".getBytes(), result.getOutputBytes());

            {
                IteratorWithIOThrow<UncachedKeyRing> unc
                        = UncachedKeyRing.fromStream(new ByteArrayInputStream(result.getOutputBytes()));

                assertTrue("export must have one key", unc.hasNext());
                UncachedKeyRing ring = unc.next();
                Assert.assertEquals("exported key has correct masterkeyid",
                        mStaticRing1.getMasterKeyId(), ring.getMasterKeyId());
                assertFalse("export must have exactly one key", unc.hasNext());
            }

        }

    }


    /** This function checks whether or not there are any local signatures in a keyring. */
    private boolean checkForLocal(UncachedKeyRing ring) {
        Iterator<WrappedSignature> sigs = ring.getPublicKey().getSignatures();
        while (sigs.hasNext()) {
            if (sigs.next().isLocal()) {
                return true;
            }
        }
        return false;
    }

}