mobile/android/base/sync/middleware/Crypto5MiddlewareRepositorySession.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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.middleware;
michael@0 6
michael@0 7 import java.io.UnsupportedEncodingException;
michael@0 8 import java.util.concurrent.ExecutorService;
michael@0 9
michael@0 10 import org.mozilla.gecko.sync.CryptoRecord;
michael@0 11 import org.mozilla.gecko.sync.crypto.CryptoException;
michael@0 12 import org.mozilla.gecko.sync.crypto.KeyBundle;
michael@0 13 import org.mozilla.gecko.sync.repositories.InactiveSessionException;
michael@0 14 import org.mozilla.gecko.sync.repositories.NoStoreDelegateException;
michael@0 15 import org.mozilla.gecko.sync.repositories.RecordFactory;
michael@0 16 import org.mozilla.gecko.sync.repositories.RepositorySession;
michael@0 17 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate;
michael@0 18 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionStoreDelegate;
michael@0 19 import org.mozilla.gecko.sync.repositories.domain.Record;
michael@0 20
michael@0 21 /**
michael@0 22 * It's a RepositorySession that accepts Records as input, producing CryptoRecords
michael@0 23 * for submission to a remote service.
michael@0 24 * Takes a RecordFactory as a parameter. This is in charge of taking decrypted CryptoRecords
michael@0 25 * as input and producing some expected kind of Record as output for local use.
michael@0 26 *
michael@0 27 *
michael@0 28
michael@0 29
michael@0 30
michael@0 31 +------------------------------------+
michael@0 32 | Server11RepositorySession |
michael@0 33 +-------------------------+----------+
michael@0 34 ^ |
michael@0 35 | |
michael@0 36 Encrypted CryptoRecords
michael@0 37 | |
michael@0 38 | v
michael@0 39 +---------+--------------------------+
michael@0 40 | Crypto5MiddlewareRepositorySession |
michael@0 41 +------------------------------------+
michael@0 42 ^ |
michael@0 43 | | Decrypted CryptoRecords
michael@0 44 | |
michael@0 45 | +---------------+
michael@0 46 | | RecordFactory |
michael@0 47 | +--+------------+
michael@0 48 | |
michael@0 49 Local Record instances
michael@0 50 | |
michael@0 51 | v
michael@0 52 +---------+--------------------------+
michael@0 53 | Local RepositorySession instance |
michael@0 54 +------------------------------------+
michael@0 55
michael@0 56
michael@0 57 * @author rnewman
michael@0 58 *
michael@0 59 */
michael@0 60 public class Crypto5MiddlewareRepositorySession extends MiddlewareRepositorySession {
michael@0 61 private KeyBundle keyBundle;
michael@0 62 private RecordFactory recordFactory;
michael@0 63
michael@0 64 public Crypto5MiddlewareRepositorySession(RepositorySession session, Crypto5MiddlewareRepository repository, RecordFactory recordFactory) {
michael@0 65 super(session, repository);
michael@0 66 this.keyBundle = repository.keyBundle;
michael@0 67 this.recordFactory = recordFactory;
michael@0 68 }
michael@0 69
michael@0 70 public class DecryptingTransformingFetchDelegate implements RepositorySessionFetchRecordsDelegate {
michael@0 71 private RepositorySessionFetchRecordsDelegate next;
michael@0 72 private KeyBundle keyBundle;
michael@0 73 private RecordFactory recordFactory;
michael@0 74
michael@0 75 DecryptingTransformingFetchDelegate(RepositorySessionFetchRecordsDelegate next, KeyBundle bundle, RecordFactory recordFactory) {
michael@0 76 this.next = next;
michael@0 77 this.keyBundle = bundle;
michael@0 78 this.recordFactory = recordFactory;
michael@0 79 }
michael@0 80
michael@0 81 @Override
michael@0 82 public void onFetchFailed(Exception ex, Record record) {
michael@0 83 next.onFetchFailed(ex, record);
michael@0 84 }
michael@0 85
michael@0 86 @Override
michael@0 87 public void onFetchedRecord(Record record) {
michael@0 88 CryptoRecord r;
michael@0 89 try {
michael@0 90 r = (CryptoRecord) record;
michael@0 91 } catch (ClassCastException e) {
michael@0 92 next.onFetchFailed(e, record);
michael@0 93 return;
michael@0 94 }
michael@0 95 r.keyBundle = keyBundle;
michael@0 96 try {
michael@0 97 r.decrypt();
michael@0 98 } catch (Exception e) {
michael@0 99 next.onFetchFailed(e, r);
michael@0 100 return;
michael@0 101 }
michael@0 102 Record transformed;
michael@0 103 try {
michael@0 104 transformed = this.recordFactory.createRecord(r);
michael@0 105 } catch (Exception e) {
michael@0 106 next.onFetchFailed(e, r);
michael@0 107 return;
michael@0 108 }
michael@0 109 next.onFetchedRecord(transformed);
michael@0 110 }
michael@0 111
michael@0 112 @Override
michael@0 113 public void onFetchCompleted(final long fetchEnd) {
michael@0 114 next.onFetchCompleted(fetchEnd);
michael@0 115 }
michael@0 116
michael@0 117 @Override
michael@0 118 public RepositorySessionFetchRecordsDelegate deferredFetchDelegate(ExecutorService executor) {
michael@0 119 // Synchronously perform *our* work, passing through appropriately.
michael@0 120 RepositorySessionFetchRecordsDelegate deferredNext = next.deferredFetchDelegate(executor);
michael@0 121 return new DecryptingTransformingFetchDelegate(deferredNext, keyBundle, recordFactory);
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 private DecryptingTransformingFetchDelegate makeUnwrappingDelegate(RepositorySessionFetchRecordsDelegate inner) {
michael@0 126 if (inner == null) {
michael@0 127 throw new IllegalArgumentException("Inner delegate cannot be null!");
michael@0 128 }
michael@0 129 return new DecryptingTransformingFetchDelegate(inner, this.keyBundle, this.recordFactory);
michael@0 130 }
michael@0 131
michael@0 132 @Override
michael@0 133 public void fetchSince(long timestamp,
michael@0 134 RepositorySessionFetchRecordsDelegate delegate) {
michael@0 135 inner.fetchSince(timestamp, makeUnwrappingDelegate(delegate));
michael@0 136 }
michael@0 137
michael@0 138 @Override
michael@0 139 public void fetch(String[] guids,
michael@0 140 RepositorySessionFetchRecordsDelegate delegate) throws InactiveSessionException {
michael@0 141 inner.fetch(guids, makeUnwrappingDelegate(delegate));
michael@0 142 }
michael@0 143
michael@0 144 @Override
michael@0 145 public void fetchAll(RepositorySessionFetchRecordsDelegate delegate) {
michael@0 146 inner.fetchAll(makeUnwrappingDelegate(delegate));
michael@0 147 }
michael@0 148
michael@0 149 @Override
michael@0 150 public void setStoreDelegate(RepositorySessionStoreDelegate delegate) {
michael@0 151 // TODO: it remains to be seen how this will work.
michael@0 152 inner.setStoreDelegate(delegate);
michael@0 153 this.delegate = delegate; // So we can handle errors without involving inner.
michael@0 154 }
michael@0 155
michael@0 156 @Override
michael@0 157 public void store(Record record) throws NoStoreDelegateException {
michael@0 158 if (delegate == null) {
michael@0 159 throw new NoStoreDelegateException();
michael@0 160 }
michael@0 161 CryptoRecord rec = record.getEnvelope();
michael@0 162 rec.keyBundle = this.keyBundle;
michael@0 163 try {
michael@0 164 rec.encrypt();
michael@0 165 } catch (UnsupportedEncodingException e) {
michael@0 166 delegate.onRecordStoreFailed(e, record.guid);
michael@0 167 return;
michael@0 168 } catch (CryptoException e) {
michael@0 169 delegate.onRecordStoreFailed(e, record.guid);
michael@0 170 return;
michael@0 171 }
michael@0 172 // Allow the inner session to do delegate handling.
michael@0 173 inner.store(rec);
michael@0 174 }
michael@0 175 }

mercurial