refactor magic creates password gen to CryptoHelper

This commit is contained in:
Daniel Gultsch 2018-10-20 00:05:10 +02:00
parent 157ebbac52
commit f9cafc144b
2 changed files with 243 additions and 247 deletions

View File

@ -16,16 +16,13 @@ import java.security.SecureRandom;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.CryptoHelper;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public class MagicCreateActivity extends XmppActivity implements TextWatcher { public class MagicCreateActivity extends XmppActivity implements TextWatcher {
private TextView mFullJidDisplay; private TextView mFullJidDisplay;
private EditText mUsername; private EditText mUsername;
private SecureRandom mRandom;
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456780+-/#$!?";
private static final int PW_LENGTH = 10;
@Override @Override
protected void refreshUiReal() { protected void refreshUiReal() {
@ -57,7 +54,6 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher {
configureActionBar(getSupportActionBar()); configureActionBar(getSupportActionBar());
mFullJidDisplay = findViewById(R.id.full_jid); mFullJidDisplay = findViewById(R.id.full_jid);
mUsername = findViewById(R.id.username); mUsername = findViewById(R.id.username);
mRandom = new SecureRandom();
Button next = findViewById(R.id.create_account); Button next = findViewById(R.id.create_account);
next.setOnClickListener(v -> { next.setOnClickListener(v -> {
try { try {
@ -70,7 +66,7 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher {
mUsername.setError(null); mUsername.setError(null);
Account account = xmppConnectionService.findAccountByJid(jid); Account account = xmppConnectionService.findAccountByJid(jid);
if (account == null) { if (account == null) {
account = new Account(jid, createPassword()); account = new Account(jid, CryptoHelper.createPassword(new SecureRandom()));
account.setOption(Account.OPTION_REGISTER, true); account.setOption(Account.OPTION_REGISTER, true);
account.setOption(Account.OPTION_DISABLED, true); account.setOption(Account.OPTION_DISABLED, true);
account.setOption(Account.OPTION_MAGIC_CREATE, true); account.setOption(Account.OPTION_MAGIC_CREATE, true);
@ -92,14 +88,6 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher {
mUsername.addTextChangedListener(this); mUsername.addTextChangedListener(this);
} }
private String createPassword() {
StringBuilder builder = new StringBuilder(PW_LENGTH);
for (int i = 0; i < PW_LENGTH; ++i) {
builder.append(CHARS.charAt(mRandom.nextInt(CHARS.length() - 1)));
}
return builder.toString();
}
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {

View File

@ -36,14 +36,14 @@ import rocks.xmpp.addr.Jid;
public final class CryptoHelper { public final class CryptoHelper {
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}");
final public static byte[] ONE = new byte[]{0, 0, 0, 1};
private static final char[] CHARS = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz123456789+-/#$!?".toCharArray();
private static final int PW_LENGTH = 10;
private static final char[] VOWELS = "aeiou".toCharArray(); private static final char[] VOWELS = "aeiou".toCharArray();
private static final char[] CONSONANTS = "bcfghjklmnpqrstvwxyz".toCharArray(); private static final char[] CONSONANTS = "bcfghjklmnpqrstvwxyz".toCharArray();
private final static char[] hexArray = "0123456789abcdef".toCharArray(); private final static char[] hexArray = "0123456789abcdef".toCharArray();
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}");
final public static byte[] ONE = new byte[] { 0, 0, 0, 1 };
public static String bytesToHex(byte[] bytes) { public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2]; char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) { for (int j = 0; j < bytes.length; j++) {
@ -54,10 +54,18 @@ public final class CryptoHelper {
return new String(hexChars); return new String(hexChars);
} }
public static String createPassword(SecureRandom random) {
StringBuilder builder = new StringBuilder(PW_LENGTH);
for (int i = 0; i < PW_LENGTH; ++i) {
builder.append(CHARS[random.nextInt(CHARS.length - 1)]);
}
return builder.toString();
}
public static String pronounceable(SecureRandom random) { public static String pronounceable(SecureRandom random) {
char[] output = new char[random.nextInt(4) * 2 + 5]; char[] output = new char[random.nextInt(4) * 2 + 5];
boolean vowel = random.nextBoolean(); boolean vowel = random.nextBoolean();
for(int i = 0; i < output.length; ++i) { for (int i = 0; i < output.length; ++i) {
output[i] = vowel ? VOWELS[random.nextInt(VOWELS.length)] : CONSONANTS[random.nextInt(CONSONANTS.length)]; output[i] = vowel ? VOWELS[random.nextInt(VOWELS.length)] : CONSONANTS[random.nextInt(CONSONANTS.length)];
vowel = !vowel; vowel = !vowel;
} }
@ -114,17 +122,17 @@ public final class CryptoHelper {
public static String random(int length, SecureRandom random) { public static String random(int length, SecureRandom random) {
final byte[] bytes = new byte[length]; final byte[] bytes = new byte[length];
random.nextBytes(bytes); random.nextBytes(bytes);
return Base64.encodeToString(bytes,Base64.NO_PADDING|Base64.NO_WRAP|Base64.URL_SAFE); return Base64.encodeToString(bytes, Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE);
} }
public static String prettifyFingerprint(String fingerprint) { public static String prettifyFingerprint(String fingerprint) {
if (fingerprint==null) { if (fingerprint == null) {
return ""; return "";
} else if (fingerprint.length() < 40) { } else if (fingerprint.length() < 40) {
return fingerprint; return fingerprint;
} }
StringBuilder builder = new StringBuilder(fingerprint); StringBuilder builder = new StringBuilder(fingerprint);
for(int i=8;i<builder.length();i+=9) { for (int i = 8; i < builder.length(); i += 9) {
builder.insert(i, ' '); builder.insert(i, ' ');
} }
return builder.toString(); return builder.toString();
@ -132,8 +140,8 @@ public final class CryptoHelper {
public static String prettifyFingerprintCert(String fingerprint) { public static String prettifyFingerprintCert(String fingerprint) {
StringBuilder builder = new StringBuilder(fingerprint); StringBuilder builder = new StringBuilder(fingerprint);
for(int i=2;i < builder.length(); i+=3) { for (int i = 2; i < builder.length(); i += 3) {
builder.insert(i,':'); builder.insert(i, ':');
} }
return builder.toString(); return builder.toString();
} }
@ -162,11 +170,11 @@ public final class CryptoHelper {
} }
} }
public static Pair<Jid,String> extractJidAndName(X509Certificate certificate) throws CertificateEncodingException, IllegalArgumentException, CertificateParsingException { public static Pair<Jid, String> extractJidAndName(X509Certificate certificate) throws CertificateEncodingException, IllegalArgumentException, CertificateParsingException {
Collection<List<?>> alternativeNames = certificate.getSubjectAlternativeNames(); Collection<List<?>> alternativeNames = certificate.getSubjectAlternativeNames();
List<String> emails = new ArrayList<>(); List<String> emails = new ArrayList<>();
if (alternativeNames != null) { if (alternativeNames != null) {
for(List<?> san : alternativeNames) { for (List<?> san : alternativeNames) {
Integer type = (Integer) san.get(0); Integer type = (Integer) san.get(0);
if (type == 1) { if (type == 1) {
emails.add((String) san.get(1)); emails.add((String) san.get(1));
@ -180,11 +188,11 @@ public final class CryptoHelper {
String name = x500name.getRDNs(BCStyle.CN).length > 0 ? IETFUtils.valueToString(x500name.getRDNs(BCStyle.CN)[0].getFirst().getValue()) : null; String name = x500name.getRDNs(BCStyle.CN).length > 0 ? IETFUtils.valueToString(x500name.getRDNs(BCStyle.CN)[0].getFirst().getValue()) : null;
if (emails.size() >= 1) { if (emails.size() >= 1) {
return new Pair<>(Jid.of(emails.get(0)), name); return new Pair<>(Jid.of(emails.get(0)), name);
} else if (name != null){ } else if (name != null) {
try { try {
Jid jid = Jid.of(name); Jid jid = Jid.of(name);
if (jid.isBareJid() && jid.getLocal() != null) { if (jid.isBareJid() && jid.getLocal() != null) {
return new Pair<>(jid,null); return new Pair<>(jid, null);
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return null; return null;
@ -204,7 +212,7 @@ public final class CryptoHelper {
//ignored //ignored
} }
try { try {
information.putString("subject_o",subject.getRDNs(BCStyle.O)[0].getFirst().getValue().toString()); information.putString("subject_o", subject.getRDNs(BCStyle.O)[0].getFirst().getValue().toString());
} catch (Exception e) { } catch (Exception e) {
//ignored //ignored
} }
@ -238,7 +246,7 @@ public final class CryptoHelper {
} }
public static String getAccountFingerprint(Account account, String androidId) { public static String getAccountFingerprint(Account account, String androidId) {
return getFingerprint(account.getJid().asBareJid().toEscapedString()+"\00"+androidId); return getFingerprint(account.getJid().asBareJid().toEscapedString() + "\00" + androidId);
} }
public static String getFingerprint(String value) { public static String getFingerprint(String value) {
@ -269,7 +277,7 @@ public final class CryptoHelper {
return url; return url;
} }
try { try {
return new URL(AesGcmURLStreamHandler.PROTOCOL_NAME+url.toString().substring(url.getProtocol().length())); return new URL(AesGcmURLStreamHandler.PROTOCOL_NAME + url.toString().substring(url.getProtocol().length()));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
return url; return url;
} }
@ -280,7 +288,7 @@ public final class CryptoHelper {
return url; return url;
} }
try { try {
return new URL("https"+url.toString().substring(url.getProtocol().length())); return new URL("https" + url.toString().substring(url.getProtocol().length()));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
return url; return url;
} }