2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2013-11-10 12:15:29 +00:00
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
2011-12-20 18:20:44 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.crypto;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-04-21 15:40:19 +00:00
|
|
|
import org.whispersystems.libaxolotl.IdentityKey;
|
|
|
|
import org.whispersystems.libaxolotl.IdentityKeyPair;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidKeyException;
|
|
|
|
import org.whispersystems.libaxolotl.ecc.Curve;
|
|
|
|
import org.whispersystems.libaxolotl.ecc.ECKeyPair;
|
|
|
|
import org.whispersystems.libaxolotl.ecc.ECPrivateKey;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* Utility class for working with identity keys.
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class IdentityKeyUtil {
|
|
|
|
|
2013-11-10 12:15:29 +00:00
|
|
|
private static final String IDENTITY_PUBLIC_KEY_DJB_PREF = "pref_identity_public_curve25519";
|
|
|
|
private static final String IDENTITY_PRIVATE_KEY_DJB_PREF = "pref_identity_private_curve25519";
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-04-10 03:02:46 +00:00
|
|
|
public static boolean hasIdentityKey(Context context) {
|
2011-12-20 18:20:44 +00:00
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
2013-11-10 12:15:29 +00:00
|
|
|
|
2014-04-10 03:02:46 +00:00
|
|
|
return
|
|
|
|
preferences.contains(IDENTITY_PUBLIC_KEY_DJB_PREF) &&
|
|
|
|
preferences.contains(IDENTITY_PRIVATE_KEY_DJB_PREF);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 03:02:46 +00:00
|
|
|
public static IdentityKey getIdentityKey(Context context) {
|
|
|
|
if (!hasIdentityKey(context)) return null;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
try {
|
2014-04-10 03:02:46 +00:00
|
|
|
byte[] publicKeyBytes = Base64.decode(retrieve(context, IDENTITY_PUBLIC_KEY_DJB_PREF));
|
2011-12-20 18:20:44 +00:00
|
|
|
return new IdentityKey(publicKeyBytes, 0);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("IdentityKeyUtil", ioe);
|
|
|
|
return null;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.w("IdentityKeyUtil", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 00:25:19 +00:00
|
|
|
|
2013-11-10 12:15:29 +00:00
|
|
|
public static IdentityKeyPair getIdentityKeyPair(Context context,
|
2014-04-10 03:02:46 +00:00
|
|
|
MasterSecret masterSecret)
|
2013-11-10 12:15:29 +00:00
|
|
|
{
|
2014-04-10 03:02:46 +00:00
|
|
|
if (!hasIdentityKey(context))
|
2013-08-22 00:25:19 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
try {
|
2013-11-10 12:15:29 +00:00
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
2014-04-10 03:02:46 +00:00
|
|
|
IdentityKey publicKey = getIdentityKey(context);
|
|
|
|
ECPrivateKey privateKey = masterCipher.decryptKey(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_DJB_PREF)));
|
2013-08-22 00:25:19 +00:00
|
|
|
|
|
|
|
return new IdentityKeyPair(publicKey, privateKey);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
2013-11-10 12:15:29 +00:00
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
throw new AssertionError(e);
|
2013-08-22 00:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-10 12:15:29 +00:00
|
|
|
|
2014-04-10 03:02:46 +00:00
|
|
|
public static void generateIdentityKeys(Context context, MasterSecret masterSecret) {
|
2014-07-26 20:29:40 +00:00
|
|
|
ECKeyPair djbKeyPair = Curve.generateKeyPair();
|
2013-11-10 12:15:29 +00:00
|
|
|
|
2014-04-10 03:02:46 +00:00
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
|
|
|
IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());
|
|
|
|
byte[] djbPrivateKey = masterCipher.encryptKey(djbKeyPair.getPrivateKey());
|
2013-11-10 12:15:29 +00:00
|
|
|
|
|
|
|
save(context, IDENTITY_PUBLIC_KEY_DJB_PREF, Base64.encodeBytes(djbIdentityKey.serialize()));
|
|
|
|
save(context, IDENTITY_PRIVATE_KEY_DJB_PREF, Base64.encodeBytes(djbPrivateKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasCurve25519IdentityKeys(Context context) {
|
|
|
|
return
|
|
|
|
retrieve(context, IDENTITY_PUBLIC_KEY_DJB_PREF) != null &&
|
|
|
|
retrieve(context, IDENTITY_PRIVATE_KEY_DJB_PREF) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void generateCurve25519IdentityKeys(Context context, MasterSecret masterSecret) {
|
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
2014-07-26 20:29:40 +00:00
|
|
|
ECKeyPair djbKeyPair = Curve.generateKeyPair();
|
2013-11-10 12:15:29 +00:00
|
|
|
IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());
|
|
|
|
byte[] djbPrivateKey = masterCipher.encryptKey(djbKeyPair.getPrivateKey());
|
|
|
|
|
|
|
|
save(context, IDENTITY_PUBLIC_KEY_DJB_PREF, Base64.encodeBytes(djbIdentityKey.serialize()));
|
|
|
|
save(context, IDENTITY_PRIVATE_KEY_DJB_PREF, Base64.encodeBytes(djbPrivateKey));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-11-10 12:15:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static String retrieve(Context context, String key) {
|
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
|
|
|
return preferences.getString(key, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void save(Context context, String key, String value) {
|
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
|
|
|
Editor preferencesEditor = preferences.edit();
|
|
|
|
|
|
|
|
preferencesEditor.putString(key, value);
|
2014-05-05 18:48:26 +00:00
|
|
|
if (!preferencesEditor.commit()) throw new AssertionError("failed to save identity key/value to shared preferences");
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|