aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java
blob: 472eb3b18cc8d33675d5aa88f59c00db6bafcc9c (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
/*
 * 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.service;

import android.os.Parcel;
import android.os.Parcelable;

import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import org.sufficientlysecure.keychain.util.Passphrase;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * This class is a a transferable representation for a collection of changes
 * to be done on a keyring.
 * <p/>
 * This class should include all types of operations supported in the backend.
 * <p/>
 * All changes are done in a differential manner. Besides the two key
 * identification attributes, all attributes may be null, which indicates no
 * change to the keyring. This is also the reason why boxed values are used
 * instead of primitives in the subclasses.
 * <p/>
 * Application of operations in the backend should be fail-fast, which means an
 * error in any included operation (for example revocation of a non-existent
 * subkey) will cause the operation as a whole to fail.
 */
public class SaveKeyringParcel implements Parcelable {

    // the master key id to be edited. if this is null, a new one will be created
    public Long mMasterKeyId;
    // the key fingerprint, for safety. MUST be null for a new key.
    public byte[] mFingerprint;

    public ChangeUnlockParcel mNewUnlock;

    public ArrayList<String> mAddUserIds;
    public ArrayList<WrappedUserAttribute> mAddUserAttribute;
    public ArrayList<SubkeyAdd> mAddSubKeys;

    public ArrayList<SubkeyChange> mChangeSubKeys;
    public String mChangePrimaryUserId;

    public ArrayList<String> mRevokeUserIds;
    public ArrayList<Long> mRevokeSubKeys;

    // if these are non-null, PINs will be changed on the token
    public Passphrase mSecurityTokenPin;
    public Passphrase mSecurityTokenAdminPin;

    // private because they have to be set together with setUpdateOptions
    private boolean mUpload;
    private boolean mUploadAtomic;
    private String mKeyserver;

    public SaveKeyringParcel() {
        reset();
    }

    public SaveKeyringParcel(long masterKeyId, byte[] fingerprint) {
        this();
        mMasterKeyId = masterKeyId;
        mFingerprint = fingerprint;
    }

    public void reset() {
        mNewUnlock = null;
        mAddUserIds = new ArrayList<>();
        mAddUserAttribute = new ArrayList<>();
        mAddSubKeys = new ArrayList<>();
        mChangePrimaryUserId = null;
        mChangeSubKeys = new ArrayList<>();
        mRevokeUserIds = new ArrayList<>();
        mRevokeSubKeys = new ArrayList<>();
        mSecurityTokenPin = null;
        mSecurityTokenAdminPin = null;
        mUpload = false;
        mUploadAtomic = false;
        mKeyserver = null;
    }

    public void setUpdateOptions(boolean upload, boolean uploadAtomic, String keysever) {
        mUpload = upload;
        mUploadAtomic = uploadAtomic;
        mKeyserver = keysever;
    }

    public boolean isUpload() {
        return mUpload;
    }

    public boolean isUploadAtomic() {
        return mUploadAtomic;
    }

    public String getUploadKeyserver() {
        return mKeyserver;
    }

    public boolean isEmpty() {
        return isRestrictedOnly() && mChangeSubKeys.isEmpty();
    }

    /** Returns true iff this parcel does not contain any operations which require a passphrase. */
    public boolean isRestrictedOnly() {
        if (mNewUnlock != null || !mAddUserIds.isEmpty() || !mAddUserAttribute.isEmpty()
                || !mAddSubKeys.isEmpty() || mChangePrimaryUserId != null || !mRevokeUserIds.isEmpty()
                || !mRevokeSubKeys.isEmpty()) {
            return false;
        }

        for (SubkeyChange change : mChangeSubKeys) {
            if (change.mRecertify || change.mFlags != null || change.mExpiry != null
                    || change.mMoveKeyToSecurityToken) {
                return false;
            }
        }

        return true;
    }

    // performance gain for using Parcelable here would probably be negligible,
    // use Serializable instead.
    public static class SubkeyAdd implements Serializable {
        public Algorithm mAlgorithm;
        public Integer mKeySize;
        public Curve mCurve;
        public int mFlags;
        public Long mExpiry;

        public SubkeyAdd(Algorithm algorithm, Integer keySize, Curve curve, int flags, Long expiry) {
            mAlgorithm = algorithm;
            mKeySize = keySize;
            mCurve = curve;
            mFlags = flags;
            mExpiry = expiry;
        }

        @Override
        public String toString() {
            String out = "mAlgorithm: " + mAlgorithm + ", ";
            out += "mKeySize: " + mKeySize + ", ";
            out += "mCurve: " + mCurve + ", ";
            out += "mFlags: " + mFlags;
            out += "mExpiry: " + mExpiry;

            return out;
        }
    }

    public static class SubkeyChange implements Serializable {
        public final long mKeyId;
        public Integer mFlags;
        // this is a long unix timestamp, in seconds (NOT MILLISECONDS!)
        public Long mExpiry;
        // if this flag is true, the key will be recertified even if all above
        // values are no-ops
        public boolean mRecertify;
        // if this flag is true, the subkey should be changed to a stripped key
        public boolean mDummyStrip;
        // if this flag is true, the subkey should be moved to a security token
        public boolean mMoveKeyToSecurityToken;
        // if this is non-null, the subkey will be changed to a divert-to-card
        // (security token) key for the given serial number
        public byte[] mSecurityTokenSerialNo;

        public SubkeyChange(long keyId) {
            mKeyId = keyId;
        }

        public SubkeyChange(long keyId, boolean recertify) {
            mKeyId = keyId;
            mRecertify = recertify;
        }

        public SubkeyChange(long keyId, Integer flags, Long expiry) {
            mKeyId = keyId;
            mFlags = flags;
            mExpiry = expiry;
        }

        public SubkeyChange(long keyId, boolean dummyStrip, boolean moveKeyToSecurityToken) {
            this(keyId, null, null);

            // these flags are mutually exclusive!
            if (dummyStrip && moveKeyToSecurityToken) {
                throw new AssertionError(
                        "cannot set strip and moveKeyToSecurityToken" +
                                " flags at the same time - this is a bug!");
            }
            mDummyStrip = dummyStrip;
            mMoveKeyToSecurityToken = moveKeyToSecurityToken;
        }

        @Override
        public String toString() {
            String out = "mKeyId: " + mKeyId + ", ";
            out += "mFlags: " + mFlags + ", ";
            out += "mExpiry: " + mExpiry + ", ";
            out += "mDummyStrip: " + mDummyStrip + ", ";
            out += "mMoveKeyToSecurityToken: " + mMoveKeyToSecurityToken + ", ";
            out += "mSecurityTokenSerialNo: [" + (mSecurityTokenSerialNo == null ? 0 : mSecurityTokenSerialNo.length) + " bytes]";

            return out;
        }
    }

    public SubkeyChange getSubkeyChange(long keyId) {
        for (SubkeyChange subkeyChange : mChangeSubKeys) {
            if (subkeyChange.mKeyId == keyId) {
                return subkeyChange;
            }
        }
        return null;
    }

    public SubkeyChange getOrCreateSubkeyChange(long keyId) {
        SubkeyChange foundSubkeyChange = getSubkeyChange(keyId);
        if (foundSubkeyChange != null) {
            return foundSubkeyChange;
        } else {
            // else, create a new one
            SubkeyChange newSubkeyChange = new SubkeyChange(keyId);
            mChangeSubKeys.add(newSubkeyChange);
            return newSubkeyChange;
        }
    }

    @SuppressWarnings("unchecked") // we verify the reads against writes in writeToParcel
    public SaveKeyringParcel(Parcel source) {
        mMasterKeyId = source.readInt() != 0 ? source.readLong() : null;
        mFingerprint = source.createByteArray();

        mNewUnlock = source.readParcelable(getClass().getClassLoader());

        mAddUserIds = source.createStringArrayList();
        mAddUserAttribute = (ArrayList<WrappedUserAttribute>) source.readSerializable();
        mAddSubKeys = (ArrayList<SubkeyAdd>) source.readSerializable();

        mChangeSubKeys = (ArrayList<SubkeyChange>) source.readSerializable();
        mChangePrimaryUserId = source.readString();

        mRevokeUserIds = source.createStringArrayList();
        mRevokeSubKeys = (ArrayList<Long>) source.readSerializable();

        mSecurityTokenPin = source.readParcelable(Passphrase.class.getClassLoader());
        mSecurityTokenAdminPin = source.readParcelable(Passphrase.class.getClassLoader());

        mUpload = source.readByte() != 0;
        mUploadAtomic = source.readByte() != 0;
        mKeyserver = source.readString();
    }

    @Override
    public void writeToParcel(Parcel destination, int flags) {
        destination.writeInt(mMasterKeyId == null ? 0 : 1);
        if (mMasterKeyId != null) {
            destination.writeLong(mMasterKeyId);
        }
        destination.writeByteArray(mFingerprint);

        // yes, null values are ok for parcelables
        destination.writeParcelable(mNewUnlock, flags);

        destination.writeStringList(mAddUserIds);
        destination.writeSerializable(mAddUserAttribute);
        destination.writeSerializable(mAddSubKeys);

        destination.writeSerializable(mChangeSubKeys);
        destination.writeString(mChangePrimaryUserId);

        destination.writeStringList(mRevokeUserIds);
        destination.writeSerializable(mRevokeSubKeys);

        destination.writeParcelable(mSecurityTokenPin, flags);
        destination.writeParcelable(mSecurityTokenAdminPin, flags);

        destination.writeByte((byte) (mUpload ? 1 : 0));
        destination.writeByte((byte) (mUploadAtomic ? 1 : 0));
        destination.writeString(mKeyserver);
    }

    public static final Creator<SaveKeyringParcel> CREATOR = new Creator<SaveKeyringParcel>() {
        public SaveKeyringParcel createFromParcel(final Parcel source) {
            return new SaveKeyringParcel(source);
        }

        public SaveKeyringParcel[] newArray(final int size) {
            return new SaveKeyringParcel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public String toString() {
        String out = "mMasterKeyId: " + mMasterKeyId + "\n";
        out += "mNewUnlock: " + mNewUnlock + "\n";
        out += "mAddUserIds: " + mAddUserIds + "\n";
        out += "mAddUserAttribute: " + mAddUserAttribute + "\n";
        out += "mAddSubKeys: " + mAddSubKeys + "\n";
        out += "mChangeSubKeys: " + mChangeSubKeys + "\n";
        out += "mChangePrimaryUserId: " + mChangePrimaryUserId + "\n";
        out += "mRevokeUserIds: " + mRevokeUserIds + "\n";
        out += "mRevokeSubKeys: " + mRevokeSubKeys + "\n";
        out += "mSecurityTokenPin: " + mSecurityTokenPin + "\n";
        out += "mSecurityTokenAdminPin: " + mSecurityTokenAdminPin;

        return out;
    }

    // All supported algorithms
    public enum Algorithm {
        RSA, DSA, ELGAMAL, ECDSA, ECDH
    }

    // All curves defined in the standard
    // http://www.bouncycastle.org/wiki/pages/viewpage.action?pageId=362269
    public enum Curve {
        NIST_P256, NIST_P384, NIST_P521,

        // these are supported by gpg, but they are not in rfc6637 and not supported by BouncyCastle yet
        // (adding support would be trivial though -> JcaPGPKeyConverter.java:190)
        // BRAINPOOL_P256, BRAINPOOL_P384, BRAINPOOL_P512
    }

    /** This subclass contains information on how the passphrase should be changed.
     *
     * If no changes are to be made, this class should NOT be used!
     *
     * At this point, there must be *exactly one* non-null value here, which specifies the type
     * of unlocking mechanism to use.
     *
     */
    public static class ChangeUnlockParcel implements Parcelable {

        // The new passphrase to use
        public final Passphrase mNewPassphrase;
        // A new pin to use. Must only contain [0-9]+
        public final Passphrase mNewPin;

        public ChangeUnlockParcel(Passphrase newPassphrase) {
            this(newPassphrase, null);
        }
        public ChangeUnlockParcel(Passphrase newPassphrase, Passphrase newPin) {
            if (newPassphrase == null && newPin == null) {
                throw new RuntimeException("Cannot set both passphrase and pin. THIS IS A BUG!");
            }
            mNewPassphrase = newPassphrase;
            mNewPin = newPin;
        }

        public ChangeUnlockParcel(Parcel source) {
            mNewPassphrase = source.readParcelable(Passphrase.class.getClassLoader());
            mNewPin = source.readParcelable(Passphrase.class.getClassLoader());
        }

        @Override
        public void writeToParcel(Parcel destination, int flags) {
            destination.writeParcelable(mNewPassphrase, flags);
            destination.writeParcelable(mNewPin, flags);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public static final Creator<ChangeUnlockParcel> CREATOR = new Creator<ChangeUnlockParcel>() {
            public ChangeUnlockParcel createFromParcel(final Parcel source) {
                return new ChangeUnlockParcel(source);
            }

            public ChangeUnlockParcel[] newArray(final int size) {
                return new ChangeUnlockParcel[size];
            }
        };

        public String toString() {
            return mNewPassphrase != null
                    ? ("passphrase (" + mNewPassphrase + ")")
                    : ("pin (" + mNewPin + ")");
        }

    }

}