1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/crypto/PersistedCrypto5Keys.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,103 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync.crypto; 1.9 + 1.10 +import org.mozilla.gecko.background.common.log.Logger; 1.11 +import org.mozilla.gecko.sync.CollectionKeys; 1.12 +import org.mozilla.gecko.sync.CryptoRecord; 1.13 + 1.14 +import android.content.SharedPreferences; 1.15 + 1.16 +public class PersistedCrypto5Keys { 1.17 + public static final String LOG_TAG = "PersistedC5Keys"; 1.18 + 1.19 + public static final String CRYPTO5_KEYS_SERVER_RESPONSE_BODY = "crypto5KeysServerResponseBody"; 1.20 + public static final String CRYPTO5_KEYS_LAST_MODIFIED = "crypto5KeysLastModified"; 1.21 + 1.22 + protected SharedPreferences prefs; 1.23 + protected KeyBundle syncKeyBundle; 1.24 + 1.25 + public PersistedCrypto5Keys(SharedPreferences prefs, KeyBundle syncKeyBundle) { 1.26 + if (syncKeyBundle == null) { 1.27 + throw new IllegalArgumentException("Null syncKeyBundle passed in to PersistedCrypto5Keys constructor."); 1.28 + } 1.29 + this.prefs = prefs; 1.30 + this.syncKeyBundle = syncKeyBundle; 1.31 + } 1.32 + 1.33 + /** 1.34 + * Get persisted crypto/keys. 1.35 + * <p> 1.36 + * crypto/keys is fetched from an encrypted JSON-encoded <code>CryptoRecord</code>. 1.37 + * 1.38 + * @return A <code>CollectionKeys</code> instance or <code>null</code> if none 1.39 + * is currently persisted. 1.40 + */ 1.41 + public CollectionKeys keys() { 1.42 + String keysJSON = prefs.getString(CRYPTO5_KEYS_SERVER_RESPONSE_BODY, null); 1.43 + if (keysJSON == null) { 1.44 + return null; 1.45 + } 1.46 + try { 1.47 + CryptoRecord cryptoRecord = CryptoRecord.fromJSONRecord(keysJSON); 1.48 + CollectionKeys keys = new CollectionKeys(); 1.49 + keys.setKeyPairsFromWBO(cryptoRecord, syncKeyBundle); 1.50 + return keys; 1.51 + } catch (Exception e) { 1.52 + Logger.warn(LOG_TAG, "Got exception decrypting persisted crypto/keys.", e); 1.53 + return null; 1.54 + } 1.55 + } 1.56 + 1.57 + /** 1.58 + * Persist crypto/keys. 1.59 + * <p> 1.60 + * crypto/keys is stored as an encrypted JSON-encoded <code>CryptoRecord</code>. 1.61 + * 1.62 + * @param keys 1.63 + * The <code>CollectionKeys</code> object to persist, which should 1.64 + * have the same default key bundle as the sync key bundle. 1.65 + */ 1.66 + public void persistKeys(CollectionKeys keys) { 1.67 + if (keys == null) { 1.68 + Logger.debug(LOG_TAG, "Clearing persisted crypto/keys."); 1.69 + prefs.edit().remove(CRYPTO5_KEYS_SERVER_RESPONSE_BODY).commit(); 1.70 + return; 1.71 + } 1.72 + try { 1.73 + CryptoRecord cryptoRecord = keys.asCryptoRecord(); 1.74 + cryptoRecord.keyBundle = syncKeyBundle; 1.75 + cryptoRecord.encrypt(); 1.76 + String keysJSON = cryptoRecord.toJSONString(); 1.77 + Logger.debug(LOG_TAG, "Persisting crypto/keys."); 1.78 + prefs.edit().putString(CRYPTO5_KEYS_SERVER_RESPONSE_BODY, keysJSON).commit(); 1.79 + } catch (Exception e) { 1.80 + Logger.warn(LOG_TAG, "Got exception encrypting while persisting crypto/keys.", e); 1.81 + } 1.82 + } 1.83 + 1.84 + public boolean persistedKeysExist() { 1.85 + return lastModified() > 0; 1.86 + } 1.87 + 1.88 + public long lastModified() { 1.89 + return prefs.getLong(CRYPTO5_KEYS_LAST_MODIFIED, -1); 1.90 + } 1.91 + 1.92 + public void persistLastModified(long lastModified) { 1.93 + if (lastModified <= 0) { 1.94 + Logger.debug(LOG_TAG, "Clearing persisted crypto/keys last modified timestamp."); 1.95 + prefs.edit().remove(CRYPTO5_KEYS_LAST_MODIFIED).commit(); 1.96 + return; 1.97 + } 1.98 + Logger.debug(LOG_TAG, "Persisting crypto/keys last modified timestamp " + lastModified + "."); 1.99 + prefs.edit().putLong(CRYPTO5_KEYS_LAST_MODIFIED, lastModified).commit(); 1.100 + } 1.101 + 1.102 + public void purge() { 1.103 + persistLastModified(-1); 1.104 + persistKeys(null); 1.105 + } 1.106 +}