Bug 709321 - Generate and upload keys. r=rnewman, a=blocking-fennec

This commit is contained in:
Nick Alexander 2012-05-04 12:27:06 -07:00
Родитель 92b496304c
Коммит e1dbf4008a
3 изменённых файлов: 42 добавлений и 61 удалений

Просмотреть файл

@ -410,7 +410,15 @@ public class GlobalSession implements CredentialsSource, PrefsSource, HttpRespon
this.config.infoCollections.fetch(callback);
}
public void uploadKeys(CryptoRecord keysRecord,
/**
* Upload new crypto/keys.
*
* @param keys
* new keys.
* @param keyUploadDelegate
* a delegate.
*/
public void uploadKeys(final CollectionKeys keys,
final KeyUploadDelegate keyUploadDelegate) {
SyncStorageRecordRequest request;
final GlobalSession self = this;
@ -452,8 +460,10 @@ public class GlobalSession implements CredentialsSource, PrefsSource, HttpRespon
}
};
keysRecord.setKeyBundle(config.syncKeyBundle);
CryptoRecord keysRecord;
try {
keysRecord = keys.asCryptoRecord();
keysRecord.setKeyBundle(config.syncKeyBundle);
keysRecord.encrypt();
} catch (UnsupportedEncodingException e) {
keyUploadDelegate.onKeyUploadFailed(e);
@ -461,7 +471,12 @@ public class GlobalSession implements CredentialsSource, PrefsSource, HttpRespon
} catch (CryptoException e) {
keyUploadDelegate.onKeyUploadFailed(e);
return;
} catch (NoCollectionKeysSetException e) {
// Should not occur.
keyUploadDelegate.onKeyUploadFailed(e);
return;
}
request.put(keysRecord);
}
@ -563,7 +578,7 @@ public class GlobalSession implements CredentialsSource, PrefsSource, HttpRespon
// Generate and upload new keys.
try {
session.uploadKeys(CollectionKeys.generateCollectionKeys().asCryptoRecord(), new KeyUploadDelegate() {
session.uploadKeys(CollectionKeys.generateCollectionKeys(), new KeyUploadDelegate() {
@Override
public void onKeysUploaded() {
// Now we can download them.
@ -576,9 +591,6 @@ public class GlobalSession implements CredentialsSource, PrefsSource, HttpRespon
freshStartDelegate.onFreshStartFailed(e);
}
});
} catch (NoCollectionKeysSetException e) {
Log.e(LOG_TAG, "Got exception generating new keys.", e);
freshStartDelegate.onFreshStartFailed(e);
} catch (CryptoException e) {
Log.e(LOG_TAG, "Got exception generating new keys.", e);
freshStartDelegate.onFreshStartFailed(e);
@ -729,15 +741,16 @@ public class GlobalSession implements CredentialsSource, PrefsSource, HttpRespon
}
public void resetStagesByName(Collection<String> names) {
Collection<GlobalSyncStage> stages = new ArrayList<GlobalSyncStage>();
for (String name : names) {
try {
GlobalSyncStage stage = this.getSyncStageByName(name);
Logger.info(LOG_TAG, "Resetting " + name + "(" + stage + ")");
stage.resetLocal();
stages.add(stage);
} catch (NoSuchStageException e) {
Logger.warn(LOG_TAG, "Cannot reset stage " + name + ": no such stage.");
}
}
GlobalSession.resetStages(stages);
}
/**

Просмотреть файл

@ -1,43 +1,21 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Android Sync Client.
*
* The Initial Developer of the Original Code is
* the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Richard Newman <rnewman@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.delegates;
public interface KeyUploadDelegate {
/**
* Called when keys have been successfully uploaded to the server.
* <p>
* The uploaded keys are intentionally not exposed. It is possible for two
* clients to simultaneously upload keys and for each client to conclude that
* its keys are current (since the server returned 200 on upload). To shorten
* the window wherein two such clients can race, all clients should upload and
* then immediately re-download the fetched keys.
* <p>
* See Bug 692700, Bug 693893.
*/
void onKeysUploaded();
void onKeyUploadFailed(Exception e);
}

Просмотреть файл

@ -5,7 +5,6 @@
package org.mozilla.gecko.sync.stage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import org.json.simple.parser.ParseException;
@ -123,7 +122,9 @@ implements SyncStorageRequestDelegate, KeyUploadDelegate {
@Override
public void handleRequestFailure(SyncStorageResponse response) {
if (retrying) {
session.handleHTTPError(response, "Failure in refetching uploaded keys.");
// Should happen very rarely -- this means we uploaded our crypto/keys
// successfully, but failed to re-download.
session.handleHTTPError(response, "Failure while re-downloading already uploaded keys.");
return;
}
@ -131,25 +132,14 @@ implements SyncStorageRequestDelegate, KeyUploadDelegate {
Logger.debug(LOG_TAG, "Got " + statusCode + " fetching keys.");
if (statusCode == 404) {
// No keys. Generate and upload, then refetch.
CryptoRecord keysWBO;
CollectionKeys keys;
try {
keysWBO = CollectionKeys.generateCollectionKeysRecord();
keys = CollectionKeys.generateCollectionKeys();
} catch (CryptoException e) {
session.abort(e, "Couldn't generate new key bundle.");
return;
}
keysWBO.keyBundle = session.config.syncKeyBundle;
try {
keysWBO.encrypt();
} catch (UnsupportedEncodingException e) {
// Shouldn't occur, so let's not waste too much time on niceties. TODO
session.abort(e, "Couldn't encrypt new key bundle: unsupported encoding.");
return;
} catch (CryptoException e) {
session.abort(e, "Couldn't encrypt new key bundle.");
return;
}
session.uploadKeys(keysWBO, this);
session.uploadKeys(keys, this);
return;
}
session.handleHTTPError(response, "Failure fetching keys.");
@ -162,7 +152,7 @@ implements SyncStorageRequestDelegate, KeyUploadDelegate {
@Override
public void onKeysUploaded() {
Logger.debug(LOG_TAG, "New keys uploaded. Starting stage again to fetch them.");
Logger.debug(LOG_TAG, "New keys uploaded. Persisting before starting stage again.");
try {
retrying = true;
this.execute();