michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.crypto; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.CollectionKeys; michael@0: import org.mozilla.gecko.sync.CryptoRecord; michael@0: michael@0: import android.content.SharedPreferences; michael@0: michael@0: public class PersistedCrypto5Keys { michael@0: public static final String LOG_TAG = "PersistedC5Keys"; michael@0: michael@0: public static final String CRYPTO5_KEYS_SERVER_RESPONSE_BODY = "crypto5KeysServerResponseBody"; michael@0: public static final String CRYPTO5_KEYS_LAST_MODIFIED = "crypto5KeysLastModified"; michael@0: michael@0: protected SharedPreferences prefs; michael@0: protected KeyBundle syncKeyBundle; michael@0: michael@0: public PersistedCrypto5Keys(SharedPreferences prefs, KeyBundle syncKeyBundle) { michael@0: if (syncKeyBundle == null) { michael@0: throw new IllegalArgumentException("Null syncKeyBundle passed in to PersistedCrypto5Keys constructor."); michael@0: } michael@0: this.prefs = prefs; michael@0: this.syncKeyBundle = syncKeyBundle; michael@0: } michael@0: michael@0: /** michael@0: * Get persisted crypto/keys. michael@0: *

michael@0: * crypto/keys is fetched from an encrypted JSON-encoded CryptoRecord. michael@0: * michael@0: * @return A CollectionKeys instance or null if none michael@0: * is currently persisted. michael@0: */ michael@0: public CollectionKeys keys() { michael@0: String keysJSON = prefs.getString(CRYPTO5_KEYS_SERVER_RESPONSE_BODY, null); michael@0: if (keysJSON == null) { michael@0: return null; michael@0: } michael@0: try { michael@0: CryptoRecord cryptoRecord = CryptoRecord.fromJSONRecord(keysJSON); michael@0: CollectionKeys keys = new CollectionKeys(); michael@0: keys.setKeyPairsFromWBO(cryptoRecord, syncKeyBundle); michael@0: return keys; michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Got exception decrypting persisted crypto/keys.", e); michael@0: return null; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Persist crypto/keys. michael@0: *

michael@0: * crypto/keys is stored as an encrypted JSON-encoded CryptoRecord. michael@0: * michael@0: * @param keys michael@0: * The CollectionKeys object to persist, which should michael@0: * have the same default key bundle as the sync key bundle. michael@0: */ michael@0: public void persistKeys(CollectionKeys keys) { michael@0: if (keys == null) { michael@0: Logger.debug(LOG_TAG, "Clearing persisted crypto/keys."); michael@0: prefs.edit().remove(CRYPTO5_KEYS_SERVER_RESPONSE_BODY).commit(); michael@0: return; michael@0: } michael@0: try { michael@0: CryptoRecord cryptoRecord = keys.asCryptoRecord(); michael@0: cryptoRecord.keyBundle = syncKeyBundle; michael@0: cryptoRecord.encrypt(); michael@0: String keysJSON = cryptoRecord.toJSONString(); michael@0: Logger.debug(LOG_TAG, "Persisting crypto/keys."); michael@0: prefs.edit().putString(CRYPTO5_KEYS_SERVER_RESPONSE_BODY, keysJSON).commit(); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Got exception encrypting while persisting crypto/keys.", e); michael@0: } michael@0: } michael@0: michael@0: public boolean persistedKeysExist() { michael@0: return lastModified() > 0; michael@0: } michael@0: michael@0: public long lastModified() { michael@0: return prefs.getLong(CRYPTO5_KEYS_LAST_MODIFIED, -1); michael@0: } michael@0: michael@0: public void persistLastModified(long lastModified) { michael@0: if (lastModified <= 0) { michael@0: Logger.debug(LOG_TAG, "Clearing persisted crypto/keys last modified timestamp."); michael@0: prefs.edit().remove(CRYPTO5_KEYS_LAST_MODIFIED).commit(); michael@0: return; michael@0: } michael@0: Logger.debug(LOG_TAG, "Persisting crypto/keys last modified timestamp " + lastModified + "."); michael@0: prefs.edit().putLong(CRYPTO5_KEYS_LAST_MODIFIED, lastModified).commit(); michael@0: } michael@0: michael@0: public void purge() { michael@0: persistLastModified(-1); michael@0: persistKeys(null); michael@0: } michael@0: }