mobile/android/base/sync/CryptoRecord.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;
michael@0 6
michael@0 7 import java.io.IOException;
michael@0 8 import java.io.UnsupportedEncodingException;
michael@0 9
michael@0 10 import org.json.simple.JSONObject;
michael@0 11 import org.json.simple.parser.ParseException;
michael@0 12 import org.mozilla.apache.commons.codec.binary.Base64;
michael@0 13 import org.mozilla.gecko.sync.crypto.CryptoException;
michael@0 14 import org.mozilla.gecko.sync.crypto.CryptoInfo;
michael@0 15 import org.mozilla.gecko.sync.crypto.KeyBundle;
michael@0 16 import org.mozilla.gecko.sync.crypto.MissingCryptoInputException;
michael@0 17 import org.mozilla.gecko.sync.crypto.NoKeyBundleException;
michael@0 18 import org.mozilla.gecko.sync.repositories.domain.Record;
michael@0 19 import org.mozilla.gecko.sync.repositories.domain.RecordParseException;
michael@0 20
michael@0 21 /**
michael@0 22 * A Sync crypto record has:
michael@0 23 *
michael@0 24 * <ul>
michael@0 25 * <li>a collection of fields which are not encrypted (id and collection);</il>
michael@0 26 * <li>a set of metadata fields (index, modified, ttl);</il>
michael@0 27 * <li>a payload, which is encrypted and decrypted on request.</il>
michael@0 28 * </ul>
michael@0 29 *
michael@0 30 * The payload flips between being a blob of JSON with hmac/IV/ciphertext
michael@0 31 * attributes and the cleartext itself.
michael@0 32 *
michael@0 33 * Until there's some benefit to the abstraction, we're simply going to call
michael@0 34 * this <code>CryptoRecord</code>.
michael@0 35 *
michael@0 36 * <code>CryptoRecord</code> uses <code>CryptoInfo</code> to do the actual
michael@0 37 * encryption and decryption.
michael@0 38 */
michael@0 39 public class CryptoRecord extends Record {
michael@0 40
michael@0 41 // JSON related constants.
michael@0 42 private static final String KEY_ID = "id";
michael@0 43 private static final String KEY_COLLECTION = "collection";
michael@0 44 private static final String KEY_PAYLOAD = "payload";
michael@0 45 private static final String KEY_MODIFIED = "modified";
michael@0 46 private static final String KEY_SORTINDEX = "sortindex";
michael@0 47 private static final String KEY_TTL = "ttl";
michael@0 48 private static final String KEY_CIPHERTEXT = "ciphertext";
michael@0 49 private static final String KEY_HMAC = "hmac";
michael@0 50 private static final String KEY_IV = "IV";
michael@0 51
michael@0 52 /**
michael@0 53 * Helper method for doing actual decryption.
michael@0 54 *
michael@0 55 * Input: JSONObject containing a valid payload (cipherText, IV, HMAC),
michael@0 56 * KeyBundle with keys for decryption. Output: byte[] clearText
michael@0 57 * @throws CryptoException
michael@0 58 * @throws UnsupportedEncodingException
michael@0 59 */
michael@0 60 private static byte[] decryptPayload(ExtendedJSONObject payload, KeyBundle keybundle) throws CryptoException, UnsupportedEncodingException {
michael@0 61 byte[] ciphertext = Base64.decodeBase64(((String) payload.get(KEY_CIPHERTEXT)).getBytes("UTF-8"));
michael@0 62 byte[] iv = Base64.decodeBase64(((String) payload.get(KEY_IV)).getBytes("UTF-8"));
michael@0 63 byte[] hmac = Utils.hex2Byte((String) payload.get(KEY_HMAC));
michael@0 64
michael@0 65 return CryptoInfo.decrypt(ciphertext, iv, hmac, keybundle).getMessage();
michael@0 66 }
michael@0 67
michael@0 68 // The encrypted JSON body object.
michael@0 69 // The decrypted JSON body object. Fields are copied from `body`.
michael@0 70
michael@0 71 public ExtendedJSONObject payload;
michael@0 72 public KeyBundle keyBundle;
michael@0 73
michael@0 74 /**
michael@0 75 * Don't forget to set cleartext or body!
michael@0 76 */
michael@0 77 public CryptoRecord() {
michael@0 78 super(null, null, 0, false);
michael@0 79 }
michael@0 80
michael@0 81 public CryptoRecord(ExtendedJSONObject payload) {
michael@0 82 super(null, null, 0, false);
michael@0 83 if (payload == null) {
michael@0 84 throw new IllegalArgumentException(
michael@0 85 "No payload provided to CryptoRecord constructor.");
michael@0 86 }
michael@0 87 this.payload = payload;
michael@0 88 }
michael@0 89
michael@0 90 public CryptoRecord(String jsonString) throws IOException, ParseException, NonObjectJSONException {
michael@0 91 this(ExtendedJSONObject.parseJSONObject(jsonString));
michael@0 92 }
michael@0 93
michael@0 94 /**
michael@0 95 * Create a new CryptoRecord with the same metadata as an existing record.
michael@0 96 *
michael@0 97 * @param source
michael@0 98 */
michael@0 99 public CryptoRecord(Record source) {
michael@0 100 super(source.guid, source.collection, source.lastModified, source.deleted);
michael@0 101 this.ttl = source.ttl;
michael@0 102 }
michael@0 103
michael@0 104 @Override
michael@0 105 public Record copyWithIDs(String guid, long androidID) {
michael@0 106 CryptoRecord out = new CryptoRecord(this);
michael@0 107 out.guid = guid;
michael@0 108 out.androidID = androidID;
michael@0 109 out.sortIndex = this.sortIndex;
michael@0 110 out.ttl = this.ttl;
michael@0 111 out.payload = (this.payload == null) ? null : new ExtendedJSONObject(this.payload.object);
michael@0 112 out.keyBundle = this.keyBundle; // TODO: copy me?
michael@0 113 return out;
michael@0 114 }
michael@0 115
michael@0 116 /**
michael@0 117 * Take a whole record as JSON -- i.e., something like
michael@0 118 *
michael@0 119 * {"payload": "{...}", "id":"foobarbaz"}
michael@0 120 *
michael@0 121 * and turn it into a CryptoRecord object.
michael@0 122 *
michael@0 123 * @param jsonRecord
michael@0 124 * @return
michael@0 125 * A CryptoRecord that encapsulates the provided record.
michael@0 126 *
michael@0 127 * @throws NonObjectJSONException
michael@0 128 * @throws ParseException
michael@0 129 * @throws IOException
michael@0 130 */
michael@0 131 public static CryptoRecord fromJSONRecord(String jsonRecord)
michael@0 132 throws ParseException, NonObjectJSONException, IOException, RecordParseException {
michael@0 133 byte[] bytes = jsonRecord.getBytes("UTF-8");
michael@0 134 ExtendedJSONObject object = ExtendedJSONObject.parseUTF8AsJSONObject(bytes);
michael@0 135
michael@0 136 return CryptoRecord.fromJSONRecord(object);
michael@0 137 }
michael@0 138
michael@0 139 // TODO: defensive programming.
michael@0 140 public static CryptoRecord fromJSONRecord(ExtendedJSONObject jsonRecord)
michael@0 141 throws IOException, ParseException, NonObjectJSONException, RecordParseException {
michael@0 142 String id = (String) jsonRecord.get(KEY_ID);
michael@0 143 String collection = (String) jsonRecord.get(KEY_COLLECTION);
michael@0 144 String jsonEncodedPayload = (String) jsonRecord.get(KEY_PAYLOAD);
michael@0 145
michael@0 146 ExtendedJSONObject payload = ExtendedJSONObject.parseJSONObject(jsonEncodedPayload);
michael@0 147
michael@0 148 CryptoRecord record = new CryptoRecord(payload);
michael@0 149 record.guid = id;
michael@0 150 record.collection = collection;
michael@0 151 if (jsonRecord.containsKey(KEY_MODIFIED)) {
michael@0 152 Long timestamp = jsonRecord.getTimestamp(KEY_MODIFIED);
michael@0 153 if (timestamp == null) {
michael@0 154 throw new RecordParseException("timestamp could not be parsed");
michael@0 155 }
michael@0 156 record.lastModified = timestamp.longValue();
michael@0 157 }
michael@0 158 if (jsonRecord.containsKey(KEY_SORTINDEX)) {
michael@0 159 // getLong tries to cast to Long, and might return null. We catch all
michael@0 160 // exceptions, just to be safe.
michael@0 161 try {
michael@0 162 record.sortIndex = jsonRecord.getLong(KEY_SORTINDEX);
michael@0 163 } catch (Exception e) {
michael@0 164 throw new RecordParseException("timestamp could not be parsed");
michael@0 165 }
michael@0 166 }
michael@0 167 if (jsonRecord.containsKey(KEY_TTL)) {
michael@0 168 // TTLs are never returned by the sync server, so should never be true if
michael@0 169 // the record was fetched.
michael@0 170 try {
michael@0 171 record.ttl = jsonRecord.getLong(KEY_TTL);
michael@0 172 } catch (Exception e) {
michael@0 173 throw new RecordParseException("TTL could not be parsed");
michael@0 174 }
michael@0 175 }
michael@0 176 // TODO: deleted?
michael@0 177 return record;
michael@0 178 }
michael@0 179
michael@0 180 public void setKeyBundle(KeyBundle bundle) {
michael@0 181 this.keyBundle = bundle;
michael@0 182 }
michael@0 183
michael@0 184 public CryptoRecord decrypt() throws CryptoException, IOException, ParseException,
michael@0 185 NonObjectJSONException {
michael@0 186 if (keyBundle == null) {
michael@0 187 throw new NoKeyBundleException();
michael@0 188 }
michael@0 189
michael@0 190 // Check that payload contains all pieces for crypto.
michael@0 191 if (!payload.containsKey(KEY_CIPHERTEXT) ||
michael@0 192 !payload.containsKey(KEY_IV) ||
michael@0 193 !payload.containsKey(KEY_HMAC)) {
michael@0 194 throw new MissingCryptoInputException();
michael@0 195 }
michael@0 196
michael@0 197 // There's no difference between handling the crypto/keys object and
michael@0 198 // anything else; we just get this.keyBundle from a different source.
michael@0 199 byte[] cleartext = decryptPayload(payload, keyBundle);
michael@0 200 payload = ExtendedJSONObject.parseUTF8AsJSONObject(cleartext);
michael@0 201 return this;
michael@0 202 }
michael@0 203
michael@0 204 public CryptoRecord encrypt() throws CryptoException, UnsupportedEncodingException {
michael@0 205 if (this.keyBundle == null) {
michael@0 206 throw new NoKeyBundleException();
michael@0 207 }
michael@0 208 String cleartext = payload.toJSONString();
michael@0 209 byte[] cleartextBytes = cleartext.getBytes("UTF-8");
michael@0 210 CryptoInfo info = CryptoInfo.encrypt(cleartextBytes, keyBundle);
michael@0 211 String message = new String(Base64.encodeBase64(info.getMessage()));
michael@0 212 String iv = new String(Base64.encodeBase64(info.getIV()));
michael@0 213 String hmac = Utils.byte2Hex(info.getHMAC());
michael@0 214 ExtendedJSONObject ciphertext = new ExtendedJSONObject();
michael@0 215 ciphertext.put(KEY_CIPHERTEXT, message);
michael@0 216 ciphertext.put(KEY_HMAC, hmac);
michael@0 217 ciphertext.put(KEY_IV, iv);
michael@0 218 this.payload = ciphertext;
michael@0 219 return this;
michael@0 220 }
michael@0 221
michael@0 222 @Override
michael@0 223 public void initFromEnvelope(CryptoRecord payload) {
michael@0 224 throw new IllegalStateException("Can't do this with a CryptoRecord.");
michael@0 225 }
michael@0 226
michael@0 227 @Override
michael@0 228 public CryptoRecord getEnvelope() {
michael@0 229 throw new IllegalStateException("Can't do this with a CryptoRecord.");
michael@0 230 }
michael@0 231
michael@0 232 @Override
michael@0 233 protected void populatePayload(ExtendedJSONObject payload) {
michael@0 234 throw new IllegalStateException("Can't do this with a CryptoRecord.");
michael@0 235 }
michael@0 236
michael@0 237 @Override
michael@0 238 protected void initFromPayload(ExtendedJSONObject payload) {
michael@0 239 throw new IllegalStateException("Can't do this with a CryptoRecord.");
michael@0 240 }
michael@0 241
michael@0 242 // TODO: this only works with encrypted object, and has other limitations.
michael@0 243 public JSONObject toJSONObject() {
michael@0 244 ExtendedJSONObject o = new ExtendedJSONObject();
michael@0 245 o.put(KEY_PAYLOAD, payload.toJSONString());
michael@0 246 o.put(KEY_ID, this.guid);
michael@0 247 if (this.ttl > 0) {
michael@0 248 o.put(KEY_TTL, this.ttl);
michael@0 249 }
michael@0 250 return o.object;
michael@0 251 }
michael@0 252
michael@0 253 @Override
michael@0 254 public String toJSONString() {
michael@0 255 return toJSONObject().toJSONString();
michael@0 256 }
michael@0 257 }

mercurial