aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperationTest.java
blob: 03b923d905922febbd73816ff29e7065ae4bba64 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
/*
 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
 * 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.pgp;

import junit.framework.AssertionFailedError;

import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.robolectric.*;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.Packet;
import org.bouncycastle.bcpg.PacketTags;
import org.bouncycastle.bcpg.S2K;
import org.bouncycastle.bcpg.SecretKeyPacket;
import org.bouncycastle.bcpg.SecretSubkeyPacket;
import org.bouncycastle.bcpg.SignaturePacket;
import org.bouncycastle.bcpg.UserAttributePacket;
import org.bouncycastle.bcpg.UserAttributeSubpacket;
import org.bouncycastle.bcpg.UserIDPacket;
import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPSignature;
import org.sufficientlysecure.keychain.WorkaroundBuildConfig;
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.service.ChangeUnlockParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.Algorithm;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyChange;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.RequiredInputType;
import org.sufficientlysecure.keychain.support.KeyringBuilder;
import org.sufficientlysecure.keychain.support.KeyringTestingHelper;
import org.sufficientlysecure.keychain.support.KeyringTestingHelper.RawPacket;
import org.sufficientlysecure.keychain.support.TestDataUtil;
import org.sufficientlysecure.keychain.util.Passphrase;
import org.sufficientlysecure.keychain.util.ProgressScaler;
import org.sufficientlysecure.keychain.util.TestingUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

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

    static UncachedKeyRing staticRing;
    final static Passphrase passphrase = TestingUtils.genPassphrase();

    UncachedKeyRing ring;
    PgpKeyOperation op;
    SaveKeyringParcel parcel;
    ArrayList<RawPacket> onlyA = new ArrayList<>();
    ArrayList<RawPacket> onlyB = new ArrayList<>();

    static CryptoInputParcel cryptoInput;

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

        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("twi");
        parcel.mAddUserIds.add("pink");

        {
            int type = 42;
            byte[] data = new byte[] { 0, 1, 2, 3, 4 };
            WrappedUserAttribute uat = WrappedUserAttribute.fromSubpacket(type, data);
            parcel.mAddUserAttribute.add(uat);
        }

        parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));
        PgpKeyOperation op = new PgpKeyOperation(null);

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

        staticRing = result.getRing();
        staticRing = staticRing.canonicalize(new OperationLog(), 0).getUncachedKeyRing();

        // we sleep here for a second, to make sure all new certificates have different timestamps
        Thread.sleep(1000);

        cryptoInput = new CryptoInputParcel(new Date(), passphrase);

    }

    @Before public void setUp() throws Exception {
        // show Log.x messages in system.out
        ShadowLog.stream = System.out;
        ring = staticRing;

        // setting up some parameters just to reduce code duplication
        op = new PgpKeyOperation(new ProgressScaler(null, 0, 100, 100));

        // set this up, gonna need it more than once
        parcel = new SaveKeyringParcel();
        parcel.mMasterKeyId = ring.getMasterKeyId();
        parcel.mFingerprint = ring.getFingerprint();

    }

    @Test
    public void createSecretKeyRingTests() {

        {
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.RSA, new Random().nextInt(256)+255, null, KeyFlags.CERTIFY_OTHER, 0L));
            parcel.mAddUserIds.add("shy");
            parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));

            assertFailure("creating ring with < 2048 bit keysize should fail", parcel,
                    LogType.MSG_CR_ERROR_KEYSIZE_2048);
        }

        {
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ELGAMAL, 2048, null, KeyFlags.CERTIFY_OTHER, 0L));
            parcel.mAddUserIds.add("shy");
            parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));

            assertFailure("creating ring with ElGamal master key should fail", parcel,
                    LogType.MSG_CR_ERROR_FLAGS_ELGAMAL);
        }

        {
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.CERTIFY_OTHER, null));
            parcel.mAddUserIds.add("lotus");
            parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));

            assertFailure("creating master key with null expiry should fail", parcel,
                    LogType.MSG_CR_ERROR_NULL_EXPIRY);
        }

        {
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.SIGN_DATA, 0L));
            parcel.mAddUserIds.add("shy");
            parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));

            assertFailure("creating ring with non-certifying master key should fail", parcel,
                    LogType.MSG_CR_ERROR_NO_CERTIFY);
        }

        {
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.CERTIFY_OTHER, 0L));
            parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));

            assertFailure("creating ring without user ids should fail", parcel,
                    LogType.MSG_CR_ERROR_NO_USER_ID);
        }

        {
            parcel.reset();
            parcel.mAddUserIds.add("shy");
            parcel.setNewUnlock(new ChangeUnlockParcel(passphrase));

            assertFailure("creating ring with no master key should fail", parcel,
                    LogType.MSG_CR_ERROR_NO_MASTER);
        }

    }

    @Test
    // this is a special case since the flags are in user id certificates rather than
    // subkey binding certificates
    public void testMasterFlags() throws Exception {
        SaveKeyringParcel parcel = new SaveKeyringParcel();
        parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA, 0L));
        parcel.mAddUserIds.add("luna");
        ring = assertCreateSuccess("creating ring with master key flags must succeed", parcel);

        Assert.assertEquals("the keyring should contain only the master key",
                1, KeyringTestingHelper.itToList(ring.getPublicKeys()).size());
        Assert.assertEquals("first (master) key must have both flags",
                KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA, (long) ring.getPublicKey().getKeyUsage());

    }

    @Test
    public void testCreatedKey() throws Exception {

        // an empty modification should change nothing. this also ensures the keyring
        // is constant through canonicalization.
        // applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertNotNull("key creation failed", ring);

        Assert.assertNull("primary user id must be empty",
                ring.getPublicKey().getPrimaryUserId());

        Assert.assertEquals("number of user ids must be two",
                2, ring.getPublicKey().getUnorderedUserIds().size());

        ArrayList<WrappedUserAttribute> attributes =
            ring.getPublicKey().getUnorderedUserAttributes();
        Assert.assertEquals("number of user attributes must be one",
                1, attributes.size());
        Assert.assertEquals("user attribute must be correct type",
                42, attributes.get(0).getType());
        Assert.assertEquals("user attribute must have one subpacket",
                1, attributes.get(0).getSubpackets().length);
        Assert.assertArrayEquals("user attribute must have correct data",
                new byte[] { 0, 1, 2, 3, 4 }, attributes.get(0).getSubpackets()[0]);

        List<UncachedPublicKey> subkeys = KeyringTestingHelper.itToList(ring.getPublicKeys());
        Assert.assertEquals("number of subkeys must be three", 3, subkeys.size());

        Assert.assertTrue("key ring should have been created in the last 360 seconds",
                ring.getPublicKey().getCreationTime().after(new Date(new Date().getTime()-1000*360)));

        Assert.assertNull("key ring should not expire",
                ring.getPublicKey().getUnsafeExpiryTimeForTesting());

        Assert.assertEquals("first (master) key can certify",
                KeyFlags.CERTIFY_OTHER, (long) subkeys.get(0).getKeyUsage());

        Assert.assertEquals("second key can sign",
                KeyFlags.SIGN_DATA, (long) subkeys.get(1).getKeyUsage());
        ArrayList<WrappedSignature> sigs = subkeys.get(1).getSignatures().next().getEmbeddedSignatures();
        Assert.assertEquals("signing key signature should have one embedded signature",
                1, sigs.size());
        Assert.assertEquals("embedded signature should be of primary key binding type",
                PGPSignature.PRIMARYKEY_BINDING, sigs.get(0).getSignatureType());
        Assert.assertEquals("primary key binding signature issuer should be signing subkey",
                subkeys.get(1).getKeyId(), sigs.get(0).getKeyId());

        Assert.assertEquals("third key can encrypt",
                KeyFlags.ENCRYPT_COMMS, (long) subkeys.get(2).getKeyUsage());

    }

    @Test
    public void testBadKeyModification() throws Exception {

        {
            SaveKeyringParcel parcel = new SaveKeyringParcel();
            // off by one
            parcel.mMasterKeyId = ring.getMasterKeyId() -1;
            parcel.mFingerprint = ring.getFingerprint();

            assertModifyFailure("keyring modification with bad master key id should fail",
                    ring, parcel, LogType.MSG_MF_ERROR_KEYID);
        }

        {
            SaveKeyringParcel parcel = new SaveKeyringParcel();
            // off by one
            parcel.mMasterKeyId = null;
            parcel.mFingerprint = ring.getFingerprint();

            assertModifyFailure("keyring modification with null master key id should fail",
                    ring, parcel, LogType.MSG_MF_ERROR_KEYID);
        }

        {
            SaveKeyringParcel parcel = new SaveKeyringParcel();
            parcel.mMasterKeyId = ring.getMasterKeyId();
            parcel.mFingerprint = ring.getFingerprint();
            // some byte, off by one
            parcel.mFingerprint[5] += 1;

            assertModifyFailure("keyring modification with bad fingerprint should fail",
                    ring, parcel, LogType.MSG_MF_ERROR_FINGERPRINT);
        }

        {
            SaveKeyringParcel parcel = new SaveKeyringParcel();
            parcel.mMasterKeyId = ring.getMasterKeyId();
            parcel.mFingerprint = null;

            assertModifyFailure("keyring modification with null fingerprint should fail",
                    ring, parcel, LogType.MSG_MF_ERROR_FINGERPRINT);
        }

        {
            Passphrase badphrase = new Passphrase();
            if (badphrase.equals(passphrase)) {
                badphrase = new Passphrase("a");
            }
            parcel.mAddUserIds.add("allure");

            assertModifyFailure("keyring modification with bad passphrase should fail",
                    ring, parcel, new CryptoInputParcel(badphrase), LogType.MSG_MF_UNLOCK_ERROR);
        }

        {
            parcel.reset();
            assertModifyFailure("no-op should fail",
                    ring, parcel, cryptoInput, LogType.MSG_MF_ERROR_NOOP);
        }

    }

    @Test
    public void testSubkeyAdd() throws Exception {

        long expiry = new Date().getTime() / 1000 + 159;
        int flags = KeyFlags.SIGN_DATA;
        parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, flags, expiry));

        UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertEquals("no extra packets in original", 0, onlyA.size());
        Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size());

        Packet p;

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertTrue("first new packet must be secret subkey", p instanceof SecretSubkeyPacket);

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(1).buf)).readPacket();
        Assert.assertTrue("second new packet must be signature", p instanceof SignaturePacket);
        Assert.assertEquals("signature type must be subkey binding certificate",
                PGPSignature.SUBKEY_BINDING, ((SignaturePacket) p).getSignatureType());
        Assert.assertEquals("signature must have been created by master key",
                ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

        // get new key from ring. it should be the last one (add a check to make sure?)
        UncachedPublicKey newKey = null;
        {
            Iterator<UncachedPublicKey> it = modified.getPublicKeys();
            while (it.hasNext()) {
                newKey = it.next();
            }
        }

        Assert.assertNotNull("new key is not null", newKey);
        Assert.assertNotNull("added key must have an expiry date",
                newKey.getUnsafeExpiryTimeForTesting());
        Assert.assertEquals("added key must have expected expiry date",
                expiry, newKey.getUnsafeExpiryTimeForTesting().getTime()/1000);
        Assert.assertEquals("added key must have expected flags",
                flags, (long) newKey.getKeyUsage());

        { // bad keysize should fail
            parcel.reset();
            parcel.mAddSubKeys.add(new SubkeyAdd(
                    Algorithm.RSA, new Random().nextInt(512), null, KeyFlags.SIGN_DATA, 0L));
            assertModifyFailure("creating a subkey with keysize < 2048 should fail", ring, parcel,
                    LogType.MSG_CR_ERROR_KEYSIZE_2048);
        }

        { // null expiry should fail
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.SIGN_DATA, null));
            assertModifyFailure("creating master key with null expiry should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_NULL_EXPIRY);
        }

        { // a past expiry should fail
            parcel.reset();
            parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                    Algorithm.ECDSA, 0, SaveKeyringParcel.Curve.NIST_P256, KeyFlags.SIGN_DATA, new Date().getTime()/1000-10));
            assertModifyFailure("creating subkey with past expiry date should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_PAST_EXPIRY);
        }

    }

    @Test
    public void testSubkeyModify() throws Exception {

        long expiry = new Date().getTime()/1000 + 1024;
        long keyId = KeyringTestingHelper.getSubkeyId(ring, 1);

        UncachedKeyRing modified = ring;
        {
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, expiry));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("one extra packet in original", 1, onlyA.size());
            Assert.assertEquals("one extra packet in modified", 1, onlyB.size());

            Assert.assertEquals("old packet must be signature",
                    PacketTags.SIGNATURE, onlyA.get(0).tag);

            Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("first new packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("signature type must be subkey binding certificate",
                    PGPSignature.SUBKEY_BINDING, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            Assert.assertNotNull("modified key must have an expiry date",
                    modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
            Assert.assertEquals("modified key must have expected expiry date",
                    expiry, modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting().getTime()/1000);
            Assert.assertEquals("modified key must have same flags as before",
                    ring.getPublicKey(keyId).getKeyUsage(), modified.getPublicKey(keyId).getKeyUsage());
        }

        { // change expiry
            expiry += 60*60*24;

            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, expiry));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertNotNull("modified key must have an expiry date",
                    modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
            Assert.assertEquals("modified key must have expected expiry date",
                    expiry, modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting().getTime()/1000);
            Assert.assertEquals("modified key must have same flags as before",
                    ring.getPublicKey(keyId).getKeyUsage(), modified.getPublicKey(keyId).getKeyUsage());
        }

        {
            int flags = KeyFlags.SIGN_DATA | KeyFlags.ENCRYPT_COMMS;
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, flags, null));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("old packet must be signature",
                    PacketTags.SIGNATURE, onlyA.get(0).tag);

            Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("first new packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("signature type must be subkey binding certificate",
                    PGPSignature.SUBKEY_BINDING, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            Assert.assertEquals("modified key must have expected flags",
                    flags, (long) modified.getPublicKey(keyId).getKeyUsage());
            Assert.assertNotNull("key must retain its expiry",
                    modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
            Assert.assertEquals("key expiry must be unchanged",
                    expiry, modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting().getTime()/1000);
        }

        { // expiry of 0 should be "no expiry"
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, 0L));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("old packet must be signature",
                    PacketTags.SIGNATURE, onlyA.get(0).tag);

            Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("first new packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("signature type must be subkey binding certificate",
                    PGPSignature.SUBKEY_BINDING, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            Assert.assertNull("key must not expire anymore", modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
        }

        { // a past expiry should fail
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, new Date().getTime()/1000-10));

            assertModifyFailure("setting subkey expiry to a past date should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_PAST_EXPIRY);
        }

        { // modifying nonexistent subkey should fail
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(123, null, null));

            assertModifyFailure("modifying non-existent subkey should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_SUBKEY_MISSING);
        }

    }

    @Test
    public void testMasterModify() throws Exception {

        long expiry = new Date().getTime()/1000 + 1024;
        long keyId = ring.getMasterKeyId();

        UncachedKeyRing modified = ring;

        // to make this check less trivial, we add a user id, change the primary one and revoke one
        parcel.mAddUserIds.add("aloe");
        parcel.mChangePrimaryUserId = "aloe";
        parcel.mRevokeUserIds.add("pink");
        modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

        {
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, expiry));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            // this implies that only the two non-revoked signatures were changed!
            Assert.assertEquals("two extra packets in original", 2, onlyA.size());
            Assert.assertEquals("two extra packets in modified", 2, onlyB.size());

            Assert.assertEquals("first original packet must be a signature",
                    PacketTags.SIGNATURE, onlyA.get(0).tag);
            Assert.assertEquals("second original packet must be a signature",
                    PacketTags.SIGNATURE, onlyA.get(1).tag);
            Assert.assertEquals("first new packet must be signature",
                    PacketTags.SIGNATURE, onlyB.get(0).tag);
            Assert.assertEquals("first new packet must be signature",
                    PacketTags.SIGNATURE, onlyB.get(1).tag);

            Assert.assertNotNull("modified key must have an expiry date",
                    modified.getPublicKey().getUnsafeExpiryTimeForTesting());
            Assert.assertEquals("modified key must have expected expiry date",
                    expiry, modified.getPublicKey().getUnsafeExpiryTimeForTesting().getTime() / 1000);
            Assert.assertEquals("modified key must have same flags as before",
                    ring.getPublicKey().getKeyUsage(), modified.getPublicKey().getKeyUsage());
        }

        { // change expiry
            expiry += 60*60*24;

            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, expiry));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertNotNull("modified key must have an expiry date",
                    modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
            Assert.assertEquals("modified key must have expected expiry date",
                    expiry, modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting().getTime() / 1000);
            Assert.assertEquals("modified key must have same flags as before",
                    ring.getPublicKey(keyId).getKeyUsage(), modified.getPublicKey(keyId).getKeyUsage());

            Date date = modified.canonicalize(new OperationLog(), 0).getPublicKey().getExpiryTime();
            Assert.assertNotNull("modified key must have an expiry date", date);
            Assert.assertEquals("modified key must have expected expiry date",
                    expiry, date.getTime() / 1000);

        }

        {
            int flags = KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA;
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, flags, null));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("modified key must have expected flags",
                    flags, (long) modified.getPublicKey(keyId).getKeyUsage());
            Assert.assertNotNull("key must retain its expiry",
                    modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
            Assert.assertEquals("key expiry must be unchanged",
                    expiry, modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting().getTime()/1000);
        }

        { // expiry of 0 should be "no expiry"

            // even if there is a non-expiring user id while all others are revoked, it doesn't count!
            // for this purpose we revoke one while they still have expiry times
            parcel.reset();
            parcel.mRevokeUserIds.add("aloe");
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, 0L));
            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            // for this check, it is relevant that we DON'T use the unsafe one!
            Assert.assertNull("key must not expire anymore",
                    modified.canonicalize(new OperationLog(), 0).getPublicKey().getExpiryTime());
            // make sure the unsafe one behaves incorrectly as expected
            Assert.assertNotNull("unsafe expiry must yield wrong result from revoked user id",
                    modified.getPublicKey(keyId).getUnsafeExpiryTimeForTesting());
        }

        { // if we revoke everything, nothing is left to properly sign...
            parcel.reset();
            parcel.mRevokeUserIds.add("twi");
            parcel.mRevokeUserIds.add("pink");
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, KeyFlags.CERTIFY_OTHER, null));

            assertModifyFailure("master key modification with all user ids revoked should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_MASTER_NONE);
        }

        { // any flag not including CERTIFY_OTHER should fail
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, KeyFlags.SIGN_DATA, null));

            assertModifyFailure("setting master key flags without certify should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_NO_CERTIFY);
        }

        { // a past expiry should fail
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, null, new Date().getTime()/1000-10));

            assertModifyFailure("setting subkey expiry to a past date should fail", ring, parcel,
                    LogType.MSG_MF_ERROR_PAST_EXPIRY);
        }

    }

    @Test
    public void testMasterRevoke() throws Exception {

        parcel.reset();
        parcel.mRevokeSubKeys.add(ring.getMasterKeyId());

        UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertEquals("no extra packets in original", 0, onlyA.size());
        Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size());

        Packet p;

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertTrue("first new packet must be secret subkey", p instanceof SignaturePacket);
        Assert.assertEquals("signature type must be subkey binding certificate",
                PGPSignature.KEY_REVOCATION, ((SignaturePacket) p).getSignatureType());
        Assert.assertEquals("signature must have been created by master key",
                ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

        Assert.assertTrue("subkey must actually be revoked",
                modified.getPublicKey().isMaybeRevoked());

    }

    @Test
    public void testSubkeyRevoke() throws Exception {

        long keyId = KeyringTestingHelper.getSubkeyId(ring, 1);
        int flags = ring.getPublicKey(keyId).getKeyUsage();

        UncachedKeyRing modified;

        {

            parcel.reset();
            parcel.mRevokeSubKeys.add(123L);

            CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(ring.getEncoded(), false, 0);
            UncachedKeyRing otherModified = op.modifySecretKeyRing(secretRing, cryptoInput, parcel).getRing();

            Assert.assertNull("revoking a nonexistent subkey should fail", otherModified);

        }

        { // revoked second subkey

            parcel.reset();
            parcel.mRevokeSubKeys.add(keyId);

            modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB,
                    new CryptoInputParcel(new Date(), passphrase));

            Assert.assertEquals("no extra packets in original", 0, onlyA.size());
            Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size());

            Packet p;

            p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("first new packet must be secret subkey", p instanceof SignaturePacket);
            Assert.assertEquals("signature type must be subkey binding certificate",
                    PGPSignature.SUBKEY_REVOCATION, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            Assert.assertTrue("subkey must actually be revoked",
                    modified.getPublicKey(keyId).isMaybeRevoked());
        }

        { // re-add second subkey

            parcel.reset();
            // re-certify the revoked subkey
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, true));

            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("exactly two outdated packets in original", 2, onlyA.size());
            Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size());

            Packet p;

            p = new BCPGInputStream(new ByteArrayInputStream(onlyA.get(0).buf)).readPacket();
            Assert.assertTrue("first outdated packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("first outdated signature type must be subkey binding certification",
                    PGPSignature.SUBKEY_BINDING, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("first outdated signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            p = new BCPGInputStream(new ByteArrayInputStream(onlyA.get(1).buf)).readPacket();
            Assert.assertTrue("second outdated packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("second outdated signature type must be subkey revocation",
                    PGPSignature.SUBKEY_REVOCATION, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("second outdated signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("new packet must be signature ", p instanceof SignaturePacket);
            Assert.assertEquals("new signature type must be subkey binding certification",
                    PGPSignature.SUBKEY_BINDING, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            Assert.assertFalse("subkey must no longer be revoked",
                    modified.getPublicKey(keyId).isMaybeRevoked());
            Assert.assertEquals("subkey must have the same usage flags as before",
                    flags, (long) modified.getPublicKey(keyId).getKeyUsage());

        }
    }

    @Test
    public void testSubkeyStrip() throws Exception {

        long keyId = KeyringTestingHelper.getSubkeyId(ring, 1);
        parcel.mChangeSubKeys.add(new SubkeyChange(keyId, true, false));
        applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertEquals("one extra packet in original", 1, onlyA.size());
        Assert.assertEquals("one extra packet in modified", 1, onlyB.size());

        Assert.assertEquals("old packet must be secret subkey",
                PacketTags.SECRET_SUBKEY, onlyA.get(0).tag);
        Assert.assertEquals("new packet must be secret subkey",
                PacketTags.SECRET_SUBKEY, onlyB.get(0).tag);

        Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertEquals("new packet should have GNU_DUMMY S2K type",
                S2K.GNU_DUMMY_S2K, ((SecretSubkeyPacket) p).getS2K().getType());
        Assert.assertEquals("new packet should have GNU_DUMMY protection mode 0x1",
                0x1, ((SecretSubkeyPacket) p).getS2K().getProtectionMode());
        Assert.assertEquals("new packet secret key data should have length zero",
                0, ((SecretSubkeyPacket) p).getSecretKeyData().length);
        Assert.assertNull("new packet should have no iv data", ((SecretSubkeyPacket) p).getIV());

    }

    @Test
    public void testMasterStrip() throws Exception {

        long keyId = ring.getMasterKeyId();
        parcel.mChangeSubKeys.add(new SubkeyChange(keyId, true, false));
        applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertEquals("one extra packet in original", 1, onlyA.size());
        Assert.assertEquals("one extra packet in modified", 1, onlyB.size());

        Assert.assertEquals("old packet must be secret key",
                PacketTags.SECRET_KEY, onlyA.get(0).tag);
        Assert.assertEquals("new packet must be secret key",
                PacketTags.SECRET_KEY, onlyB.get(0).tag);

        Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertEquals("new packet should have GNU_DUMMY S2K type",
                S2K.GNU_DUMMY_S2K, ((SecretKeyPacket) p).getS2K().getType());
        Assert.assertEquals("new packet should have GNU_DUMMY protection mode 0x1",
                0x1, ((SecretKeyPacket) p).getS2K().getProtectionMode());
        Assert.assertEquals("new packet secret key data should have length zero",
                0, ((SecretKeyPacket) p).getSecretKeyData().length);
        Assert.assertNull("new packet should have no iv data", ((SecretKeyPacket) p).getIV());
    }

    @Test
    public void testRestrictedStrip() throws Exception {

        long keyId = KeyringTestingHelper.getSubkeyId(ring, 1);
        UncachedKeyRing modified;

        { // we should be able to change the stripped status of subkeys without passphrase
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, true, false));
            modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB, new CryptoInputParcel());
            Assert.assertEquals("one extra packet in modified", 1, onlyB.size());
            Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertEquals("new packet should have GNU_DUMMY S2K type",
                    S2K.GNU_DUMMY_S2K, ((SecretKeyPacket) p).getS2K().getType());
            Assert.assertEquals("new packet should have GNU_DUMMY protection mode stripped",
                    S2K.GNU_PROTECTION_MODE_NO_PRIVATE_KEY, ((SecretKeyPacket) p).getS2K().getProtectionMode());
        }

        { // trying to edit a subkey with signing capability should fail
            parcel.reset();
            parcel.mChangeSubKeys.add(new SubkeyChange(keyId, true));

            assertModifyFailure("subkey modification for signing-enabled but stripped subkey should fail",
                    modified, parcel, LogType.MSG_MF_ERROR_SUB_STRIPPED);
        }

    }

    @Test
    public void testKeyToSecurityToken() throws Exception {

        // Special keyring for security token tests with 2048 bit RSA as a subkey
        SaveKeyringParcel parcelKey = new SaveKeyringParcel();
        parcelKey.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                Algorithm.DSA, 2048, null, KeyFlags.CERTIFY_OTHER, 0L));
        parcelKey.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                Algorithm.RSA, 2048, null, KeyFlags.SIGN_DATA, 0L));
        parcelKey.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
                Algorithm.RSA, 3072, null, KeyFlags.ENCRYPT_COMMS, 0L));

        parcelKey.mAddUserIds.add("yubikey");

        parcelKey.setNewUnlock(new ChangeUnlockParcel(passphrase));
        PgpKeyOperation opSecurityToken = new PgpKeyOperation(null);

        PgpEditKeyResult resultSecurityToken = opSecurityToken.createSecretKeyRing(parcelKey);
        Assert.assertTrue("initial test key creation must succeed", resultSecurityToken.success());
        Assert.assertNotNull("initial test key creation must succeed", resultSecurityToken.getRing());

        UncachedKeyRing ringSecurityToken = resultSecurityToken.getRing();

        SaveKeyringParcel parcelSecurityToken = new SaveKeyringParcel();
        parcelSecurityToken.mMasterKeyId = ringSecurityToken.getMasterKeyId();
        parcelSecurityToken.mFingerprint = ringSecurityToken.getFingerprint();

        UncachedKeyRing modified;

        { // moveKeyToSecurityToken should fail with BAD_NFC_SIZE when presented with the RSA-3072 key
            long keyId = KeyringTestingHelper.getSubkeyId(ringSecurityToken, 2);
            parcelSecurityToken.reset();
            parcelSecurityToken.mChangeSubKeys.add(new SubkeyChange(keyId, false, true));

            assertModifyFailure("moveKeyToSecurityToken operation should fail on invalid key size", ringSecurityToken,
                    parcelSecurityToken, cryptoInput, LogType.MSG_MF_ERROR_BAD_SECURITY_TOKEN_SIZE);
        }

        { // moveKeyToSecurityToken should fail with BAD_NFC_ALGO when presented with the DSA-1024 key
            long keyId = KeyringTestingHelper.getSubkeyId(ringSecurityToken, 0);
            parcelSecurityToken.reset();
            parcelSecurityToken.mChangeSubKeys.add(new SubkeyChange(keyId, false, true));

            assertModifyFailure("moveKeyToSecurityToken operation should fail on invalid key algorithm", ringSecurityToken,
                    parcelSecurityToken, cryptoInput, LogType.MSG_MF_ERROR_BAD_SECURITY_TOKEN_ALGO);
        }

        long keyId = KeyringTestingHelper.getSubkeyId(ringSecurityToken, 1);

        { // moveKeyToSecurityToken should return a pending SECURITY_TOKEN_MOVE_KEY_TO_CARD result when presented with the RSA-2048
          // key, and then make key divert-to-card when it gets a serial in the cryptoInputParcel.
            parcelSecurityToken.reset();
            parcelSecurityToken.mChangeSubKeys.add(new SubkeyChange(keyId, false, true));

            CanonicalizedSecretKeyRing secretRing =
                    new CanonicalizedSecretKeyRing(ringSecurityToken.getEncoded(), false, 0);
            PgpKeyOperation op = new PgpKeyOperation(null);
            PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, cryptoInput, parcelSecurityToken);
            Assert.assertTrue("moveKeyToSecurityToken operation should be pending", result.isPending());
            Assert.assertEquals("required input should be RequiredInputType.SECURITY_TOKEN_MOVE_KEY_TO_CARD",
                    result.getRequiredInputParcel().mType, RequiredInputType.SECURITY_TOKEN_MOVE_KEY_TO_CARD);

            // Create a cryptoInputParcel that matches what the SecurityTokenOperationActivity would return.
            byte[] keyIdBytes = new byte[8];
            ByteBuffer buf = ByteBuffer.wrap(keyIdBytes);
            buf.putLong(keyId).rewind();
            byte[] serial = new byte[] {
                    0x6a, 0x6f, 0x6c, 0x6f, 0x73, 0x77, 0x61, 0x67,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            };
            CryptoInputParcel inputParcel = new CryptoInputParcel();
            inputParcel.addCryptoData(keyIdBytes, serial);

            modified = applyModificationWithChecks(parcelSecurityToken, ringSecurityToken, onlyA, onlyB, inputParcel);
            Assert.assertEquals("one extra packet in modified", 1, onlyB.size());
            Packet p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertEquals("new packet should have GNU_DUMMY S2K type",
                    S2K.GNU_DUMMY_S2K, ((SecretKeyPacket) p).getS2K().getType());
            Assert.assertEquals("new packet should have GNU_DUMMY protection mode divert-to-card",
                    S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD, ((SecretKeyPacket) p).getS2K().getProtectionMode());
            Assert.assertArrayEquals("new packet should have correct serial number as iv",
                    serial, ((SecretKeyPacket) p).getIV());
        }

        { // editing a signing subkey requires a primary key binding sig -> pendinginput
            parcelSecurityToken.reset();
            parcelSecurityToken.mChangeSubKeys.add(new SubkeyChange(keyId, true));

            CanonicalizedSecretKeyRing secretRing =
                    new CanonicalizedSecretKeyRing(modified.getEncoded(), false, 0);
            PgpKeyOperation op = new PgpKeyOperation(null);
            PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, cryptoInput, parcelSecurityToken);
            Assert.assertTrue("moveKeyToSecurityToken operation should be pending", result.isPending());
            Assert.assertEquals("required input should be RequiredInputType.SECURITY_TOKEN_SIGN",
                    RequiredInputType.SECURITY_TOKEN_SIGN, result.getRequiredInputParcel().mType);
        }

    }

    @Test
    public void testUserIdRevoke() throws Exception {

        UncachedKeyRing modified;
        String uid = ring.getPublicKey().getUnorderedUserIds().get(1);

        { // revoke second user id

            parcel.mRevokeUserIds.add(uid);

            modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);

            Assert.assertEquals("no extra packets in original", 0, onlyA.size());
            Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size());

            Packet p;

            p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("first new packet must be secret subkey", p instanceof SignaturePacket);
            Assert.assertEquals("signature type must be subkey binding certificate",
                    PGPSignature.CERTIFICATION_REVOCATION, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

        }

        { // re-add second user id

            parcel.reset();
            parcel.mChangePrimaryUserId = uid;

            assertModifyFailure("setting primary user id to a revoked user id should fail", modified, parcel,
                    LogType.MSG_MF_ERROR_REVOKED_PRIMARY);

        }

        { // re-add second user id

            parcel.reset();
            parcel.mAddUserIds.add(uid);

            applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("exactly two outdated packets in original", 2, onlyA.size());
            Assert.assertEquals("exactly one extra packet in modified", 1, onlyB.size());

            Packet p;

            p = new BCPGInputStream(new ByteArrayInputStream(onlyA.get(0).buf)).readPacket();
            Assert.assertTrue("first outdated packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("first outdated signature type must be positive certification",
                    PGPSignature.POSITIVE_CERTIFICATION, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("first outdated signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            p = new BCPGInputStream(new ByteArrayInputStream(onlyA.get(1).buf)).readPacket();
            Assert.assertTrue("second outdated packet must be signature", p instanceof SignaturePacket);
            Assert.assertEquals("second outdated signature type must be certificate revocation",
                    PGPSignature.CERTIFICATION_REVOCATION, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("second outdated signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());

            p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
            Assert.assertTrue("new packet must be signature ", p instanceof SignaturePacket);
            Assert.assertEquals("new signature type must be positive certification",
                    PGPSignature.POSITIVE_CERTIFICATION, ((SignaturePacket) p).getSignatureType());
            Assert.assertEquals("signature must have been created by master key",
                    ring.getMasterKeyId(), ((SignaturePacket) p).getKeyID());
        }

        { // revocation of non-existent user id should fail
            parcel.reset();
            parcel.mRevokeUserIds.add("nonexistent");

            assertModifyFailure("revocation of nonexistent user id should fail", modified, parcel,
                    LogType.MSG_MF_ERROR_NOEXIST_REVOKE);
        }

    }

    @Test
    public void testUserIdAdd() throws Exception {

        {
            parcel.mAddUserIds.add("");
            assertModifyFailure("adding an empty user id should fail", ring, parcel,
                    LogType.MSG_MF_UID_ERROR_EMPTY);
        }

        parcel.reset();
        parcel.mAddUserIds.add("rainbow");

        UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertTrue("keyring must contain added user id",
                modified.getPublicKey().getUnorderedUserIds().contains("rainbow"));

        Assert.assertEquals("no extra packets in original", 0, onlyA.size());
        Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size());

        Assert.assertTrue("keyring must contain added user id",
                modified.getPublicKey().getUnorderedUserIds().contains("rainbow"));

        Packet p;

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertTrue("first new packet must be user id", p instanceof UserIDPacket);
        Assert.assertEquals("user id packet must match added user id",
                "rainbow", ((UserIDPacket) p).getID());

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(1).buf)).readPacket();
        Assert.assertTrue("second new packet must be signature", p instanceof SignaturePacket);
        Assert.assertEquals("signature type must be positive certification",
                PGPSignature.POSITIVE_CERTIFICATION, ((SignaturePacket) p).getSignatureType());

    }

    @Test
    public void testUserAttributeAdd() throws Exception {

        {
            parcel.mAddUserAttribute.add(WrappedUserAttribute.fromData(new byte[0]));
            assertModifyFailure("adding an empty user attribute should fail", ring, parcel,
                    LogType.MSG_MF_UAT_ERROR_EMPTY);
        }

        parcel.reset();

        Random r = new Random();
        int type = r.nextInt(110)+2; // any type except image attribute, to avoid interpretation of these
        byte[] data = new byte[r.nextInt(2000)];
        new Random().nextBytes(data);

        WrappedUserAttribute uat = WrappedUserAttribute.fromSubpacket(type, data);
        parcel.mAddUserAttribute.add(uat);

        UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB);

        Assert.assertEquals("no extra packets in original", 0, onlyA.size());
        Assert.assertEquals("exactly two extra packets in modified", 2, onlyB.size());

        Assert.assertTrue("keyring must contain added user attribute",
                modified.getPublicKey().getUnorderedUserAttributes().contains(uat));

        Packet p;

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertTrue("first new packet must be user attribute", p instanceof UserAttributePacket);
        {
            UserAttributeSubpacket[] subpackets = ((UserAttributePacket) p).getSubpackets();
            Assert.assertEquals("user attribute packet must contain one subpacket",
                    1, subpackets.length);
            Assert.assertEquals("user attribute subpacket type must be as specified above",
                    type, subpackets[0].getType());
            Assert.assertArrayEquals("user attribute subpacket data must be as specified above",
                    data, subpackets[0].getData());
        }

        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(1).buf)).readPacket();
        Assert.assertTrue("second new packet must be signature", p instanceof SignaturePacket);
        Assert.assertEquals("signature type must be positive certification",
                PGPSignature.POSITIVE_CERTIFICATION, ((SignaturePacket) p).getSignatureType());

        Thread.sleep(1000);

        // applying the same modification AGAIN should not add more certifications but drop those
        // as duplicates
        modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB,
                new CryptoInputParcel(new Date(), passphrase), true, false);

        Assert.assertEquals("duplicate modification: one extra packet in original", 1, onlyA.size());
        Assert.assertEquals("duplicate modification: one extra packet in modified", 1, onlyB.size());

        p = new BCPGInputStream(new ByteArrayInputStream(onlyA.get(0).buf)).readPacket();
        Assert.assertTrue("lost packet must be signature", p instanceof SignaturePacket);
        p = new BCPGInputStream(new ByteArrayInputStream(onlyB.get(0).buf)).readPacket();
        Assert.assertTrue("new packet must be signature", p instanceof SignaturePacket);

    }


    @Test
    public void testUserIdPrimary() throws Exception {

        UncachedKeyRing modified = ring;
        String uid = ring.getPublicKey().getUnorderedUserIds().get(1);

        { // first part, add new user id which is also primary
            parcel.mAddUserIds.add("jack");
            parcel.mChangePrimaryUserId = "jack";

            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("primary user id must be the one added",
                    "jack", modified.getPublicKey().getPrimaryUserId());
        }

        { // second part, change primary to a different one
            parcel.reset();
            parcel.mChangePrimaryUserId = uid;

            modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB);

            Assert.assertEquals("old keyring must have two outdated certificates", 2, onlyA.size());
            Assert.assertEquals("new keyring must have two new packets", 2, onlyB.size());

            Assert.assertEquals("primary user id must be the one changed to",
                    "pink", modified.getPublicKey().getPrimaryUserId());
        }

        { // third part, change primary to a non-existent one
            parcel.reset();
            //noinspection SpellCheckingInspection
            parcel.mChangePrimaryUserId = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            if (parcel.mChangePrimaryUserId.equals(passphrase)) {
                parcel.mChangePrimaryUserId += "A";
            }

            assertModifyFailure("changing primary user id to a non-existent one should fail",
                    ring, parcel, LogType.MSG_MF_ERROR_NOEXIST_PRIMARY);
        }

        // check for revoked primary user id already done in revoke test

    }

    @Test
    public void testPassphraseChange() throws Exception {

        // change passphrase to empty
        parcel.setNewUnlock(new ChangeUnlockParcel(new Passphrase()));
        // note that canonicalization here necessarily strips the empty notation packet
        UncachedKeyRing modified = applyModificationWithChecks(parcel, ring, onlyA, onlyB, cryptoInput);

        Assert.assertEquals("exactly three packets should have been modified (the secret keys)",
                3, onlyB.size());

        // remember secret key packet with no passphrase for later
        RawPacket sKeyNoPassphrase = onlyB.get(1);
        Assert.assertEquals("extracted packet should be a secret subkey",
                PacketTags.SECRET_SUBKEY, sKeyNoPassphrase.tag);

        // modify keyring, change to non-empty passphrase
        Passphrase otherPassphrase = TestingUtils.genPassphrase(true);
        CryptoInputParcel otherCryptoInput = new CryptoInputParcel(otherPassphrase);
        parcel.setNewUnlock(new ChangeUnlockParcel(otherPassphrase));
        modified = applyModificationWithChecks(parcel, modified, onlyA, onlyB,
                new CryptoInputParcel(new Date(), new Passphrase()));

        Assert.assertEquals("exactly three packets should have been modified (the secret keys)",
                3, onlyB.size());

        { // quick check to make sure no two secret keys have the same IV
            HashSet<ByteBuffer> ivs = new HashSet<ByteBuffer>();
            for (int i = 0; i < 3; i++) {
                SecretKeyPacket p = (SecretKeyPacket) new BCPGInputStream(
                        new ByteArrayInputStream(onlyB.get(i).buf)).readPacket();
                ByteBuffer iv = ByteBuffer.wrap(p.getIV());
                Assert.assertFalse(
                        "no two secret keys should have the same s2k iv (slightly non-deterministic!)",
                        ivs.contains(iv)
                );
                ivs.add(iv);
            }
        }

        RawPacket sKeyWithPassphrase = onlyB.get(1);
        Assert.assertEquals("extracted packet should be a secret subkey",
                PacketTags.SECRET_SUBKEY, sKeyNoPassphrase.tag);

        Passphrase otherPassphrase2 = TestingUtils.genPassphrase(true);
        parcel.setNewUnlock(new ChangeUnlockParcel(otherPassphrase2));
        {
            // if we replace a secret key with one without passphrase
            modified = KeyringTestingHelper.removePacket(modified, sKeyNoPassphrase.position);
            modified = KeyringTestingHelper.injectPacket(modified, sKeyNoPassphrase.buf, sKeyNoPassphrase.position);

            // we should still be able to modify it (and change its passphrase) without errors
            PgpKeyOperation op = new PgpKeyOperation(null);
            CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(modified.getEncoded(), false, 0);
            PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, otherCryptoInput, parcel);
            Assert.assertTrue("key modification must succeed", result.success());
            Assert.assertFalse("log must not contain a warning",
                    result.getLog().containsWarnings());
            Assert.assertTrue("log must contain an empty passphrase retry notice",
                result.getLog().containsType(LogType.MSG_MF_PASSPHRASE_EMPTY_RETRY));
            modified = result.getRing();
        }

        {
            // if we add one subkey with a different passphrase, that should produce a warning but also work
            modified = KeyringTestingHelper.removePacket(modified, sKeyWithPassphrase.position);
            modified = KeyringTestingHelper.injectPacket(modified, sKeyWithPassphrase.buf, sKeyWithPassphrase.position);

            PgpKeyOperation op = new PgpKeyOperation(null);
            CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(modified.getEncoded(), false, 0);
            PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, new CryptoInputParcel(otherPassphrase2), parcel);
            Assert.assertTrue("key modification must succeed", result.success());
            Assert.assertTrue("log must contain a failed passphrase change warning",
                    result.getLog().containsType(LogType.MSG_MF_PASSPHRASE_FAIL));
        }

    }

    @Test
    public void testRestricted() throws Exception {

        CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(ring.getEncoded(), false, 0);

        parcel.mAddUserIds.add("discord");
        PgpKeyOperation op = new PgpKeyOperation(null);
        PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, new CryptoInputParcel(new Date()), parcel);
        Assert.assertFalse("non-restricted operations should fail without passphrase", result.success());
    }

    public static UncachedKeyRing applyModificationWithChecks(SaveKeyringParcel parcel,
                                                               UncachedKeyRing ring,
                                                               ArrayList<RawPacket> onlyA,
                                                               ArrayList<RawPacket> onlyB) {
        return applyModificationWithChecks(parcel, ring, onlyA, onlyB, cryptoInput, true, true);
    }

    public static UncachedKeyRing applyModificationWithChecks(SaveKeyringParcel parcel,
                                                               UncachedKeyRing ring,
                                                               ArrayList<RawPacket> onlyA,
                                                               ArrayList<RawPacket> onlyB,
                                                               CryptoInputParcel cryptoInput) {
        return applyModificationWithChecks(parcel, ring, onlyA, onlyB, cryptoInput, true, true);
    }

    // applies a parcel modification while running some integrity checks
    public static UncachedKeyRing applyModificationWithChecks(SaveKeyringParcel parcel,
                                                               UncachedKeyRing ring,
                                                               ArrayList<RawPacket> onlyA,
                                                               ArrayList<RawPacket> onlyB,
                                                               CryptoInputParcel cryptoInput,
                                                               boolean canonicalize,
                                                               boolean constantCanonicalize) {

        try {

            Assert.assertTrue("modified keyring must be secret", ring.isSecret());
            CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(ring.getEncoded(), false, 0);

            PgpKeyOperation op = new PgpKeyOperation(null);
            PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, cryptoInput, parcel);
            Assert.assertTrue("key modification must succeed", result.success());
            UncachedKeyRing rawModified = result.getRing();
            Assert.assertNotNull("key modification must not return null", rawModified);

            if (!canonicalize) {
                Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
                        ring.getEncoded(), rawModified.getEncoded(), onlyA, onlyB));
                return rawModified;
            }

            CanonicalizedKeyRing modified = rawModified.canonicalize(new OperationLog(), 0);
            if (constantCanonicalize) {
                Assert.assertTrue("key must be constant through canonicalization",
                        !KeyringTestingHelper.diffKeyrings(
                                modified.getEncoded(), rawModified.getEncoded(), onlyA, onlyB)
                );
            }
            Assert.assertTrue("keyring must differ from original", KeyringTestingHelper.diffKeyrings(
                    ring.getEncoded(), modified.getEncoded(), onlyA, onlyB));

            return modified.getUncachedKeyRing();

        } catch (IOException e) {
            throw new AssertionFailedError("error during encoding!");
        }
    }

    @Test
    public void testVerifySuccess() throws Exception {

        UncachedKeyRing expectedKeyRing = KeyringBuilder.correctRing();
        UncachedKeyRing inputKeyRing = KeyringBuilder.ringWithExtraIncorrectSignature();

        CanonicalizedKeyRing canonicalized = inputKeyRing.canonicalize(new OperationLog(), 0);
        Assert.assertNotNull("canonicalization must succeed", canonicalized);

        ArrayList onlyA = new ArrayList<RawPacket>();
        ArrayList onlyB = new ArrayList<RawPacket>();
        //noinspection unchecked
        Assert.assertTrue("keyrings differ", !KeyringTestingHelper.diffKeyrings(
                expectedKeyRing.getEncoded(), expectedKeyRing.getEncoded(), onlyA, onlyB));

    }

    /**
     * Just testing my own test code. Should really be using a library for this.
     */
    @Test
    public void testConcat() throws Exception {
        byte[] actual = TestDataUtil.concatAll(new byte[]{1}, new byte[]{2, -2}, new byte[]{5}, new byte[]{3});
        byte[] expected = new byte[]{1,2,-2,5,3};
        Assert.assertEquals(java.util.Arrays.toString(expected), java.util.Arrays.toString(actual));
    }

    private void assertFailure(String reason, SaveKeyringParcel parcel, LogType expected) {

        PgpEditKeyResult result = op.createSecretKeyRing(parcel);

        Assert.assertFalse(reason, result.success());
        Assert.assertNull(reason, result.getRing());
        Assert.assertTrue(reason + "(with correct error)",
                result.getLog().containsType(expected));

    }

    private void assertModifyFailure(String reason, UncachedKeyRing ring,
                                     SaveKeyringParcel parcel, CryptoInputParcel cryptoInput, LogType expected)
            throws Exception {

        CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(ring.getEncoded(), false, 0);
        PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, cryptoInput, parcel);

        Assert.assertFalse(reason, result.success());
        Assert.assertNull(reason, result.getRing());
        Assert.assertTrue(reason + "(with correct error)",
                result.getLog().containsType(expected));

    }

    private void assertModifyFailure(String reason, UncachedKeyRing ring, SaveKeyringParcel parcel,
                                     LogType expected)
            throws Exception {

        CanonicalizedSecretKeyRing secretRing = new CanonicalizedSecretKeyRing(ring.getEncoded(), false, 0);
        PgpEditKeyResult result = op.modifySecretKeyRing(secretRing, cryptoInput, parcel);

        Assert.assertFalse(reason, result.success());
        Assert.assertNull(reason, result.getRing());
        Assert.assertTrue(reason + "(with correct error)",
                result.getLog().containsType(expected));

    }

    private UncachedKeyRing assertCreateSuccess(String reason, SaveKeyringParcel parcel) {

        PgpEditKeyResult result = op.createSecretKeyRing(parcel);

        Assert.assertTrue(reason, result.success());
        Assert.assertNotNull(reason, result.getRing());

        return result.getRing();

    }

}