Fix for Android PRNG bug.
There is apparently an Android vulnerability with the PRNG it provides through the JCE. This uses their suggested code to patch the PRNG, and provides the option to regenerate identity keys. http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
This commit is contained in:
parent
8ecf972534
commit
b14d9d84ad
11 changed files with 533 additions and 6 deletions
|
@ -38,7 +38,8 @@
|
|||
android:protectionLevel="signature" />
|
||||
<uses-permission android:name="org.thoughtcrime.securesms.permission.C2D_MESSAGE" />
|
||||
|
||||
<application android:icon="@drawable/icon"
|
||||
<application android:name="org.thoughtcrime.securesms.ApplicationListener"
|
||||
android:icon="@drawable/icon"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/TextSecure.LightTheme">
|
||||
|
||||
|
@ -124,6 +125,9 @@
|
|||
android:label="@string/AndroidManifest__public_identity_key"
|
||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>
|
||||
|
||||
<activity android:name=".ViewLocalIdentityActivity"
|
||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>
|
||||
|
||||
<activity android:name=".PassphraseChangeActivity"
|
||||
android:label="@string/AndroidManifest__change_passphrase"
|
||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>
|
||||
|
|
BIN
res/drawable-hdpi/refresh.png
Normal file
BIN
res/drawable-hdpi/refresh.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.1 KiB |
BIN
res/drawable-mdpi/refresh.png
Normal file
BIN
res/drawable-mdpi/refresh.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3 KiB |
BIN
res/drawable-xhdpi/refresh.png
Normal file
BIN
res/drawable-xhdpi/refresh.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.1 KiB |
7
res/menu/local_identity.xml
Normal file
7
res/menu/local_identity.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:title="@string/local_identity__regenerate_key"
|
||||
android:id="@+id/menu_regenerate_key"
|
||||
android:icon="@drawable/refresh"/>
|
||||
</menu>
|
|
@ -257,6 +257,22 @@
|
|||
<string name="MessageNotifier_mark_all_as_read">Mark all as read</string>
|
||||
<string name="MessageNotifier_mark_as_read">Mark as read</string>
|
||||
|
||||
<!-- ViewLocalIdentityActivity -->
|
||||
<string name="ViewLocalIdentityActivity_regenerating">Regenerating...</string>
|
||||
<string name="ViewLocalIdentityActivity_regenerating_identity_key">Regenerating identity
|
||||
key...
|
||||
</string>
|
||||
<string name="ViewLocalIdentityActivity_regenerated">Regenerated!</string>
|
||||
<string name="ViewLocalIdentityActivity_reset_identity_key">Reset Identity Key?</string>
|
||||
<string name="ViewLocalIdentityActivity_by_regenerating_your_identity_key_your_existing_contacts_will_receive_warnings">
|
||||
Caution! By regenerating your identity key, your current identity key will be permanently lost,
|
||||
and your existing contacts will receive warnings when establishing new secure sessions with you.
|
||||
Are you sure you would like to continue?
|
||||
</string>
|
||||
<string name="ViewLocalIdentityActivity_cancel">Cancel</string>
|
||||
<string name="ViewLocalIdentityActivity_continue">Continue</string>
|
||||
|
||||
|
||||
<!-- SmsReceiver -->
|
||||
<string name="SmsReceiver_currently_unable_to_send_your_sms_message">Currently unable to send your SMS message. It will be sent once service becomes available.</string>
|
||||
|
||||
|
@ -332,6 +348,7 @@
|
|||
<string name="import_fragment__import_a_plaintext_backup_file">
|
||||
Import a plaintext backup file. Compatible with \'SMSBackup And Restore.\'</string>
|
||||
|
||||
<string name="local_identity__regenerate_key">Regenerate Key</string>
|
||||
|
||||
<!-- prompt_passphrase_activity -->
|
||||
<string name="prompt_passphrase_activity__textsecure_passphrase">TEXTSECURE PASSPHRASE</string>
|
||||
|
@ -455,6 +472,7 @@
|
|||
<string name="preferences__appearance">Appearance</string>
|
||||
<string name="preferences__theme">Theme</string>
|
||||
<string name="preferences__default">Default</string>
|
||||
<string name="preferences__language">Language</string>
|
||||
|
||||
<!-- **************************************** -->
|
||||
<!-- menus -->
|
||||
|
@ -521,7 +539,6 @@
|
|||
|
||||
<!-- verify_keys -->
|
||||
<string name="verify_keys__menu_verified">Verified</string>
|
||||
<string name="preferences__language">Language</string>
|
||||
<!-- EOF -->
|
||||
|
||||
</resources>
|
||||
|
|
37
src/org/thoughtcrime/securesms/ApplicationListener.java
Normal file
37
src/org/thoughtcrime/securesms/ApplicationListener.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (C) 2013 Open Whisper Systems
|
||||
*
|
||||
* 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;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.PRNGFixes;
|
||||
|
||||
/**
|
||||
* Will be called once when the TextSecure process is created.
|
||||
*
|
||||
* We're using this as an insertion point to patch up the Android PRNG disaster.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*/
|
||||
public class ApplicationListener extends Application {
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
PRNGFixes.apply();
|
||||
}
|
||||
|
||||
}
|
|
@ -106,10 +106,8 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
|
|||
intent = new Intent(this, ImportExportActivity.class);
|
||||
intent.putExtra("master_secret", masterSecret);
|
||||
} else if (selected.equals("my_identity_key")) {
|
||||
intent = new Intent(this, ViewIdentityActivity.class);
|
||||
intent.putExtra("identity_key", IdentityKeyUtil.getIdentityKey(this));
|
||||
intent.putExtra("title", getString(R.string.ApplicationPreferencesActivity_my) + " " +
|
||||
getString(R.string.ViewIdentityActivity_identity_fingerprint));
|
||||
intent = new Intent(this, ViewLocalIdentityActivity.class);
|
||||
intent.putExtra("master_secret", masterSecret);
|
||||
} else if (selected.equals("contact_identity_keys")) {
|
||||
intent = new Intent(this, ReviewIdentitiesActivity.class);
|
||||
intent.putExtra("master_secret", masterSecret);
|
||||
|
|
|
@ -37,6 +37,10 @@ public class ViewIdentityActivity extends KeyScanningActivity {
|
|||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
setContentView(R.layout.view_identity_activity);
|
||||
|
||||
initialize();
|
||||
}
|
||||
|
||||
protected void initialize() {
|
||||
initializeResources();
|
||||
initializeFingerprint();
|
||||
}
|
||||
|
|
123
src/org/thoughtcrime/securesms/ViewLocalIdentityActivity.java
Normal file
123
src/org/thoughtcrime/securesms/ViewLocalIdentityActivity.java
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* 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;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
|
||||
/**
|
||||
* Activity that displays the local identity key and offers the option to regenerate it.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*/
|
||||
public class ViewLocalIdentityActivity extends ViewIdentityActivity {
|
||||
|
||||
private MasterSecret masterSecret;
|
||||
|
||||
public void onCreate(Bundle bundle) {
|
||||
this.masterSecret = getIntent().getParcelableExtra("master_secret");
|
||||
|
||||
getIntent().putExtra("identity_key", IdentityKeyUtil.getIdentityKey(this));
|
||||
getIntent().putExtra("title", getString(R.string.ApplicationPreferencesActivity_my) + " " +
|
||||
getString(R.string.ViewIdentityActivity_identity_fingerprint));
|
||||
super.onCreate(bundle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
|
||||
MenuInflater inflater = this.getSupportMenuInflater();
|
||||
inflater.inflate(R.menu.local_identity, menu);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
super.onOptionsItemSelected(item);
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_regenerate_key: promptToRegenerateIdentityKey(); return true;
|
||||
case android.R.id.home: finish(); return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void promptToRegenerateIdentityKey() {
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
|
||||
dialog.setIcon(android.R.drawable.ic_dialog_alert);
|
||||
dialog.setTitle(getString(R.string.ViewLocalIdentityActivity_reset_identity_key));
|
||||
dialog.setMessage(getString(R.string.ViewLocalIdentityActivity_by_regenerating_your_identity_key_your_existing_contacts_will_receive_warnings));
|
||||
dialog.setNegativeButton(getString(R.string.ViewLocalIdentityActivity_cancel), null);
|
||||
dialog.setPositiveButton(getString(R.string.ViewLocalIdentityActivity_continue),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
regenerateIdentityKey();
|
||||
}
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void regenerateIdentityKey() {
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
private ProgressDialog progressDialog;
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
progressDialog = ProgressDialog.show(ViewLocalIdentityActivity.this,
|
||||
getString(R.string.ViewLocalIdentityActivity_regenerating),
|
||||
getString(R.string.ViewLocalIdentityActivity_regenerating_identity_key),
|
||||
true, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void doInBackground(Void... params) {
|
||||
IdentityKeyUtil.generateIdentityKeys(ViewLocalIdentityActivity.this, masterSecret);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
if (progressDialog != null)
|
||||
progressDialog.dismiss();
|
||||
|
||||
Toast.makeText(ViewLocalIdentityActivity.this,
|
||||
getString(R.string.ViewLocalIdentityActivity_regenerated),
|
||||
Toast.LENGTH_LONG).show();
|
||||
|
||||
getIntent().putExtra("identity_key",
|
||||
IdentityKeyUtil.getIdentityKey(ViewLocalIdentityActivity.this));
|
||||
initialize();
|
||||
}
|
||||
|
||||
}.execute();
|
||||
}
|
||||
|
||||
}
|
337
src/org/thoughtcrime/securesms/crypto/PRNGFixes.java
Normal file
337
src/org/thoughtcrime/securesms/crypto/PRNGFixes.java
Normal file
|
@ -0,0 +1,337 @@
|
|||
package org.thoughtcrime.securesms.crypto;
|
||||
|
||||
import android.os.Build;
|
||||
import android.os.Process;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Provider;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.SecureRandomSpi;
|
||||
import java.security.Security;
|
||||
|
||||
/**
|
||||
* This class is taken directly from the Android blog post announcing this bug:
|
||||
* http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
|
||||
*
|
||||
* Since I still don't know exactly what the source of this bug was, I'm using
|
||||
* this class verbatim under the assumption that the Android team knows what
|
||||
* they're doing. Although, at this point, that is perhaps a foolish assumption.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fixes for the output of the default PRNG having low entropy.
|
||||
*
|
||||
* The fixes need to be applied via {@link #apply()} before any use of Java
|
||||
* Cryptography Architecture primitives. A good place to invoke them is in the
|
||||
* application's {@code onCreate}.
|
||||
*/
|
||||
public final class PRNGFixes {
|
||||
|
||||
private static final int VERSION_CODE_JELLY_BEAN = 16;
|
||||
private static final int VERSION_CODE_JELLY_BEAN_MR2 = 18;
|
||||
private static final byte[] BUILD_FINGERPRINT_AND_DEVICE_SERIAL =
|
||||
getBuildFingerprintAndDeviceSerial();
|
||||
|
||||
/** Hidden constructor to prevent instantiation. */
|
||||
private PRNGFixes() {}
|
||||
|
||||
/**
|
||||
* Applies all fixes.
|
||||
*
|
||||
* @throws SecurityException if a fix is needed but could not be applied.
|
||||
*/
|
||||
public static void apply() {
|
||||
applyOpenSSLFix();
|
||||
installLinuxPRNGSecureRandom();
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the fix for OpenSSL PRNG having low entropy. Does nothing if the
|
||||
* fix is not needed.
|
||||
*
|
||||
* @throws SecurityException if the fix is needed but could not be applied.
|
||||
*/
|
||||
private static void applyOpenSSLFix() throws SecurityException {
|
||||
if ((Build.VERSION.SDK_INT < VERSION_CODE_JELLY_BEAN)
|
||||
|| (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2)) {
|
||||
// No need to apply the fix
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Mix in the device- and invocation-specific seed.
|
||||
Class.forName("org.apache.harmony.xnet.provider.jsse.NativeCrypto")
|
||||
.getMethod("RAND_seed", byte[].class)
|
||||
.invoke(null, generateSeed());
|
||||
|
||||
// Mix output of Linux PRNG into OpenSSL's PRNG
|
||||
int bytesRead = (Integer) Class.forName(
|
||||
"org.apache.harmony.xnet.provider.jsse.NativeCrypto")
|
||||
.getMethod("RAND_load_file", String.class, long.class)
|
||||
.invoke(null, "/dev/urandom", 1024);
|
||||
if (bytesRead != 1024) {
|
||||
throw new IOException(
|
||||
"Unexpected number of bytes read from Linux PRNG: "
|
||||
+ bytesRead);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException("Failed to seed OpenSSL PRNG", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
|
||||
* default. Does nothing if the implementation is already the default or if
|
||||
* there is not need to install the implementation.
|
||||
*
|
||||
* @throws SecurityException if the fix is needed but could not be applied.
|
||||
*/
|
||||
private static void installLinuxPRNGSecureRandom()
|
||||
throws SecurityException {
|
||||
if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
|
||||
// No need to apply the fix
|
||||
return;
|
||||
}
|
||||
|
||||
// Install a Linux PRNG-based SecureRandom implementation as the
|
||||
// default, if not yet installed.
|
||||
Provider[] secureRandomProviders =
|
||||
Security.getProviders("SecureRandom.SHA1PRNG");
|
||||
if ((secureRandomProviders == null)
|
||||
|| (secureRandomProviders.length < 1)
|
||||
|| (!LinuxPRNGSecureRandomProvider.class.equals(
|
||||
secureRandomProviders[0].getClass()))) {
|
||||
Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
|
||||
}
|
||||
|
||||
// Assert that new SecureRandom() and
|
||||
// SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
|
||||
// by the Linux PRNG-based SecureRandom implementation.
|
||||
SecureRandom rng1 = new SecureRandom();
|
||||
if (!LinuxPRNGSecureRandomProvider.class.equals(
|
||||
rng1.getProvider().getClass())) {
|
||||
throw new SecurityException(
|
||||
"new SecureRandom() backed by wrong Provider: "
|
||||
+ rng1.getProvider().getClass());
|
||||
}
|
||||
|
||||
SecureRandom rng2;
|
||||
try {
|
||||
rng2 = SecureRandom.getInstance("SHA1PRNG");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new SecurityException("SHA1PRNG not available", e);
|
||||
}
|
||||
if (!LinuxPRNGSecureRandomProvider.class.equals(
|
||||
rng2.getProvider().getClass())) {
|
||||
throw new SecurityException(
|
||||
"SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
|
||||
+ " Provider: " + rng2.getProvider().getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code Provider} of {@code SecureRandom} engines which pass through
|
||||
* all requests to the Linux PRNG.
|
||||
*/
|
||||
private static class LinuxPRNGSecureRandomProvider extends Provider {
|
||||
|
||||
public LinuxPRNGSecureRandomProvider() {
|
||||
super("LinuxPRNG",
|
||||
1.0,
|
||||
"A Linux-specific random number provider that uses"
|
||||
+ " /dev/urandom");
|
||||
// Although /dev/urandom is not a SHA-1 PRNG, some apps
|
||||
// explicitly request a SHA1PRNG SecureRandom and we thus need to
|
||||
// prevent them from getting the default implementation whose output
|
||||
// may have low entropy.
|
||||
put("SecureRandom.SHA1PRNG", LinuxPRNGSecureRandom.class.getName());
|
||||
put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link SecureRandomSpi} which passes all requests to the Linux PRNG
|
||||
* ({@code /dev/urandom}).
|
||||
*/
|
||||
public static class LinuxPRNGSecureRandom extends SecureRandomSpi {
|
||||
|
||||
/*
|
||||
* IMPLEMENTATION NOTE: Requests to generate bytes and to mix in a seed
|
||||
* are passed through to the Linux PRNG (/dev/urandom). Instances of
|
||||
* this class seed themselves by mixing in the current time, PID, UID,
|
||||
* build fingerprint, and hardware serial number (where available) into
|
||||
* Linux PRNG.
|
||||
*
|
||||
* Concurrency: Read requests to the underlying Linux PRNG are
|
||||
* serialized (on sLock) to ensure that multiple threads do not get
|
||||
* duplicated PRNG output.
|
||||
*/
|
||||
|
||||
private static final File URANDOM_FILE = new File("/dev/urandom");
|
||||
|
||||
private static final Object sLock = new Object();
|
||||
|
||||
/**
|
||||
* Input stream for reading from Linux PRNG or {@code null} if not yet
|
||||
* opened.
|
||||
*
|
||||
* @GuardedBy("sLock")
|
||||
*/
|
||||
private static DataInputStream sUrandomIn;
|
||||
|
||||
/**
|
||||
* Output stream for writing to Linux PRNG or {@code null} if not yet
|
||||
* opened.
|
||||
*
|
||||
* @GuardedBy("sLock")
|
||||
*/
|
||||
private static OutputStream sUrandomOut;
|
||||
|
||||
/**
|
||||
* Whether this engine instance has been seeded. This is needed because
|
||||
* each instance needs to seed itself if the client does not explicitly
|
||||
* seed it.
|
||||
*/
|
||||
private boolean mSeeded;
|
||||
|
||||
@Override
|
||||
protected void engineSetSeed(byte[] bytes) {
|
||||
try {
|
||||
OutputStream out;
|
||||
synchronized (sLock) {
|
||||
out = getUrandomOutputStream();
|
||||
}
|
||||
out.write(bytes);
|
||||
out.flush();
|
||||
mSeeded = true;
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException(
|
||||
"Failed to mix seed into " + URANDOM_FILE, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void engineNextBytes(byte[] bytes) {
|
||||
if (!mSeeded) {
|
||||
// Mix in the device- and invocation-specific seed.
|
||||
engineSetSeed(generateSeed());
|
||||
}
|
||||
|
||||
try {
|
||||
DataInputStream in;
|
||||
synchronized (sLock) {
|
||||
in = getUrandomInputStream();
|
||||
}
|
||||
synchronized (in) {
|
||||
in.readFully(bytes);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException(
|
||||
"Failed to read from " + URANDOM_FILE, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] engineGenerateSeed(int size) {
|
||||
byte[] seed = new byte[size];
|
||||
engineNextBytes(seed);
|
||||
return seed;
|
||||
}
|
||||
|
||||
private DataInputStream getUrandomInputStream() {
|
||||
synchronized (sLock) {
|
||||
if (sUrandomIn == null) {
|
||||
// NOTE: Consider inserting a BufferedInputStream between
|
||||
// DataInputStream and FileInputStream if you need higher
|
||||
// PRNG output performance and can live with future PRNG
|
||||
// output being pulled into this process prematurely.
|
||||
try {
|
||||
sUrandomIn = new DataInputStream(
|
||||
new FileInputStream(URANDOM_FILE));
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException("Failed to open "
|
||||
+ URANDOM_FILE + " for reading", e);
|
||||
}
|
||||
}
|
||||
return sUrandomIn;
|
||||
}
|
||||
}
|
||||
|
||||
private OutputStream getUrandomOutputStream() {
|
||||
synchronized (sLock) {
|
||||
if (sUrandomOut == null) {
|
||||
try {
|
||||
sUrandomOut = new FileOutputStream(URANDOM_FILE);
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException("Failed to open "
|
||||
+ URANDOM_FILE + " for writing", e);
|
||||
}
|
||||
}
|
||||
return sUrandomOut;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a device- and invocation-specific seed to be mixed into the
|
||||
* Linux PRNG.
|
||||
*/
|
||||
private static byte[] generateSeed() {
|
||||
try {
|
||||
ByteArrayOutputStream seedBuffer = new ByteArrayOutputStream();
|
||||
DataOutputStream seedBufferOut =
|
||||
new DataOutputStream(seedBuffer);
|
||||
seedBufferOut.writeLong(System.currentTimeMillis());
|
||||
seedBufferOut.writeLong(System.nanoTime());
|
||||
seedBufferOut.writeInt(Process.myPid());
|
||||
seedBufferOut.writeInt(Process.myUid());
|
||||
seedBufferOut.write(BUILD_FINGERPRINT_AND_DEVICE_SERIAL);
|
||||
seedBufferOut.close();
|
||||
return seedBuffer.toByteArray();
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException("Failed to generate seed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the hardware serial number of this device.
|
||||
*
|
||||
* @return serial number or {@code null} if not available.
|
||||
*/
|
||||
private static String getDeviceSerialNumber() {
|
||||
// We're using the Reflection API because Build.SERIAL is only available
|
||||
// since API Level 9 (Gingerbread, Android 2.3).
|
||||
try {
|
||||
return (String) Build.class.getField("SERIAL").get(null);
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] getBuildFingerprintAndDeviceSerial() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
String fingerprint = Build.FINGERPRINT;
|
||||
if (fingerprint != null) {
|
||||
result.append(fingerprint);
|
||||
}
|
||||
String serial = getDeviceSerialNumber();
|
||||
if (serial != null) {
|
||||
result.append(serial);
|
||||
}
|
||||
try {
|
||||
return result.toString().getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException("UTF-8 encoding not supported");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue