Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kontalk devteam committed May 11, 2017
2 parents 4eeeda0 + b2a8e7a commit fbdb80a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ dependencies {
compile 'com.github.castorflex.smoothprogressbar:library:1.1.0'
compile 'com.nispok:snackbar:2.11.0'
compile 'com.koushikdutta.ion:ion:2.1.9'
compile 'com.github.instacart.truetime-android:library:3.0'
compile 'com.github.daniele-athome:FloatingActionButton:b976d71658'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.github.vlivanov:ListViewVariants:f606578467'
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/kontalk/crypto/PGP.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static final class PRNGFixException extends SecurityException {
}

/** Creates an ECDSA/ECDH key pair. */
public static PGPDecryptedKeyPairRing create()
public static PGPDecryptedKeyPairRing create(Date timestamp)
throws NoSuchAlgorithmException, NoSuchProviderException, PGPException, InvalidAlgorithmParameterException {

KeyPairGenerator gen;
Expand All @@ -163,17 +163,17 @@ public static PGPDecryptedKeyPairRing create()
gen = KeyPairGenerator.getInstance("RSA", PROVIDER);
gen.initialize(RSA_KEY_LENGTH);

authKp = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, gen.generateKeyPair(), new Date());
authKp = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, gen.generateKeyPair(), timestamp);

gen = KeyPairGenerator.getInstance("ECDH", PROVIDER);
gen.initialize(new ECGenParameterSpec(EC_CURVE));

encryptKp = new JcaPGPKeyPair(PGPPublicKey.ECDH, gen.generateKeyPair(), new Date());
encryptKp = new JcaPGPKeyPair(PGPPublicKey.ECDH, gen.generateKeyPair(), timestamp);

gen = KeyPairGenerator.getInstance("ECDSA", PROVIDER);
gen.initialize(new ECGenParameterSpec(EC_CURVE));

signKp = new JcaPGPKeyPair(PGPPublicKey.ECDSA, gen.generateKeyPair(), new Date());
signKp = new JcaPGPKeyPair(PGPPublicKey.ECDSA, gen.generateKeyPair(), timestamp);

return new PGPDecryptedKeyPairRing(authKp, signKp, encryptKp);
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/org/kontalk/crypto/PersonalKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Iterator;

import org.spongycastle.openpgp.PGPException;
Expand Down Expand Up @@ -388,9 +389,9 @@ public static PersonalKey load(PGPSecretKeyRing secRing, PGPPublicKeyRing pubRin
throw new PGPException("invalid key data");
}

public static PersonalKey create() throws IOException {
public static PersonalKey create(Date timestamp) throws IOException {
try {
PGPDecryptedKeyPairRing kp = PGP.create();
PGPDecryptedKeyPairRing kp = PGP.create(timestamp);
return new PersonalKey(kp, null);
}
catch (Exception e) {
Expand Down
46 changes: 42 additions & 4 deletions app/src/main/java/org/kontalk/service/KeyPairGeneratorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Date;

import com.instacart.library.truetime.TrueTime;

import android.app.Notification;
import android.app.PendingIntent;
Expand Down Expand Up @@ -59,6 +62,9 @@ public class KeyPairGeneratorService extends Service {
public static final String EXTRA_KEY = "org.kontalk.keypair.KEY";
public static final String EXTRA_FOREGROUND = "org.kontalk.keypair.FOREGROUND";

private static final String NTP_DEFAULT_SERVER = "time.google.com";
private static final int NTP_MAX_RETRIES = 3;

private GeneratorThread mThread;
private volatile PersonalKey mKey;

Expand Down Expand Up @@ -137,8 +143,8 @@ private void keypairGenerated(PersonalKey key) {
private static final class GeneratorThread extends Thread {
private WeakReference<KeyPairGeneratorService> s;

public GeneratorThread(KeyPairGeneratorService service) {
s = new WeakReference<KeyPairGeneratorService>(service);
GeneratorThread(KeyPairGeneratorService service) {
s = new WeakReference<>(service);
}

@Override
Expand All @@ -148,8 +154,11 @@ public void run() {

KeyPairGeneratorService service = s.get();
if (service != null) {
// we need the real time from the Internet
Date timestamp = getRealtime(service);

try {
PersonalKey key = PersonalKey.create();
PersonalKey key = PersonalKey.create(timestamp);
Log.v("KeyPair", "key pair generated: " + key);
service.keypairGenerated(key);
}
Expand All @@ -161,10 +170,39 @@ public void run() {
service.stopForeground();
}
}

private Date getRealtime(Context context) {
try {
return TrueTime.now();
}
catch (IllegalStateException e) {
int retryCount = 0;
while (retryCount < NTP_MAX_RETRIES) {
try {
TrueTime.build()
.withSharedPreferences(context)
.withNtpHost(NTP_DEFAULT_SERVER)
.initialize();
break;
}
catch (IOException ioe) {
retryCount++;
}
}

try {
return TrueTime.now();
}
catch (IllegalStateException ise) {
Log.w("KeyPair", "unable to retrieve real time from network, using system time");
return new Date();
}
}
}
}

public interface PersonalKeyRunnable {
public void run(PersonalKey key);
void run(PersonalKey key);
}

public final static class KeyGeneratorReceiver extends BroadcastReceiver {
Expand Down

0 comments on commit fbdb80a

Please sign in to comment.