mobile/android/base/sync/crypto/PersistedCrypto5Keys.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.sync.crypto;
michael@0 6
michael@0 7 import org.mozilla.gecko.background.common.log.Logger;
michael@0 8 import org.mozilla.gecko.sync.CollectionKeys;
michael@0 9 import org.mozilla.gecko.sync.CryptoRecord;
michael@0 10
michael@0 11 import android.content.SharedPreferences;
michael@0 12
michael@0 13 public class PersistedCrypto5Keys {
michael@0 14 public static final String LOG_TAG = "PersistedC5Keys";
michael@0 15
michael@0 16 public static final String CRYPTO5_KEYS_SERVER_RESPONSE_BODY = "crypto5KeysServerResponseBody";
michael@0 17 public static final String CRYPTO5_KEYS_LAST_MODIFIED = "crypto5KeysLastModified";
michael@0 18
michael@0 19 protected SharedPreferences prefs;
michael@0 20 protected KeyBundle syncKeyBundle;
michael@0 21
michael@0 22 public PersistedCrypto5Keys(SharedPreferences prefs, KeyBundle syncKeyBundle) {
michael@0 23 if (syncKeyBundle == null) {
michael@0 24 throw new IllegalArgumentException("Null syncKeyBundle passed in to PersistedCrypto5Keys constructor.");
michael@0 25 }
michael@0 26 this.prefs = prefs;
michael@0 27 this.syncKeyBundle = syncKeyBundle;
michael@0 28 }
michael@0 29
michael@0 30 /**
michael@0 31 * Get persisted crypto/keys.
michael@0 32 * <p>
michael@0 33 * crypto/keys is fetched from an encrypted JSON-encoded <code>CryptoRecord</code>.
michael@0 34 *
michael@0 35 * @return A <code>CollectionKeys</code> instance or <code>null</code> if none
michael@0 36 * is currently persisted.
michael@0 37 */
michael@0 38 public CollectionKeys keys() {
michael@0 39 String keysJSON = prefs.getString(CRYPTO5_KEYS_SERVER_RESPONSE_BODY, null);
michael@0 40 if (keysJSON == null) {
michael@0 41 return null;
michael@0 42 }
michael@0 43 try {
michael@0 44 CryptoRecord cryptoRecord = CryptoRecord.fromJSONRecord(keysJSON);
michael@0 45 CollectionKeys keys = new CollectionKeys();
michael@0 46 keys.setKeyPairsFromWBO(cryptoRecord, syncKeyBundle);
michael@0 47 return keys;
michael@0 48 } catch (Exception e) {
michael@0 49 Logger.warn(LOG_TAG, "Got exception decrypting persisted crypto/keys.", e);
michael@0 50 return null;
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 /**
michael@0 55 * Persist crypto/keys.
michael@0 56 * <p>
michael@0 57 * crypto/keys is stored as an encrypted JSON-encoded <code>CryptoRecord</code>.
michael@0 58 *
michael@0 59 * @param keys
michael@0 60 * The <code>CollectionKeys</code> object to persist, which should
michael@0 61 * have the same default key bundle as the sync key bundle.
michael@0 62 */
michael@0 63 public void persistKeys(CollectionKeys keys) {
michael@0 64 if (keys == null) {
michael@0 65 Logger.debug(LOG_TAG, "Clearing persisted crypto/keys.");
michael@0 66 prefs.edit().remove(CRYPTO5_KEYS_SERVER_RESPONSE_BODY).commit();
michael@0 67 return;
michael@0 68 }
michael@0 69 try {
michael@0 70 CryptoRecord cryptoRecord = keys.asCryptoRecord();
michael@0 71 cryptoRecord.keyBundle = syncKeyBundle;
michael@0 72 cryptoRecord.encrypt();
michael@0 73 String keysJSON = cryptoRecord.toJSONString();
michael@0 74 Logger.debug(LOG_TAG, "Persisting crypto/keys.");
michael@0 75 prefs.edit().putString(CRYPTO5_KEYS_SERVER_RESPONSE_BODY, keysJSON).commit();
michael@0 76 } catch (Exception e) {
michael@0 77 Logger.warn(LOG_TAG, "Got exception encrypting while persisting crypto/keys.", e);
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 public boolean persistedKeysExist() {
michael@0 82 return lastModified() > 0;
michael@0 83 }
michael@0 84
michael@0 85 public long lastModified() {
michael@0 86 return prefs.getLong(CRYPTO5_KEYS_LAST_MODIFIED, -1);
michael@0 87 }
michael@0 88
michael@0 89 public void persistLastModified(long lastModified) {
michael@0 90 if (lastModified <= 0) {
michael@0 91 Logger.debug(LOG_TAG, "Clearing persisted crypto/keys last modified timestamp.");
michael@0 92 prefs.edit().remove(CRYPTO5_KEYS_LAST_MODIFIED).commit();
michael@0 93 return;
michael@0 94 }
michael@0 95 Logger.debug(LOG_TAG, "Persisting crypto/keys last modified timestamp " + lastModified + ".");
michael@0 96 prefs.edit().putLong(CRYPTO5_KEYS_LAST_MODIFIED, lastModified).commit();
michael@0 97 }
michael@0 98
michael@0 99 public void purge() {
michael@0 100 persistLastModified(-1);
michael@0 101 persistKeys(null);
michael@0 102 }
michael@0 103 }

mercurial