michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync; michael@0: michael@0: import java.io.BufferedReader; michael@0: import java.io.FileInputStream; michael@0: import java.io.IOException; michael@0: import java.io.InputStreamReader; michael@0: import java.io.UnsupportedEncodingException; michael@0: import java.math.BigDecimal; michael@0: import java.math.BigInteger; michael@0: import java.net.URLDecoder; michael@0: import java.security.MessageDigest; michael@0: import java.security.NoSuchAlgorithmException; michael@0: import java.security.SecureRandom; michael@0: import java.text.DecimalFormat; michael@0: import java.util.ArrayList; michael@0: import java.util.Collection; michael@0: import java.util.HashMap; michael@0: import java.util.HashSet; michael@0: import java.util.Locale; michael@0: import java.util.Map; michael@0: import java.util.TreeMap; michael@0: michael@0: import org.json.simple.JSONArray; michael@0: import org.mozilla.apache.commons.codec.binary.Base32; michael@0: import org.mozilla.apache.commons.codec.binary.Base64; michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.background.nativecode.NativeCrypto; michael@0: import org.mozilla.gecko.sync.setup.Constants; michael@0: michael@0: import android.annotation.SuppressLint; michael@0: import android.content.Context; michael@0: import android.content.SharedPreferences; michael@0: import android.os.Bundle; michael@0: michael@0: public class Utils { michael@0: michael@0: private static final String LOG_TAG = "Utils"; michael@0: michael@0: private static SecureRandom sharedSecureRandom = new SecureRandom(); michael@0: michael@0: // See michael@0: public static final int SHARED_PREFERENCES_MODE = 0; michael@0: michael@0: public static String generateGuid() { michael@0: byte[] encodedBytes = Base64.encodeBase64(generateRandomBytes(9), false); michael@0: return new String(encodedBytes).replace("+", "-").replace("/", "_"); michael@0: } michael@0: michael@0: /** michael@0: * Helper to generate secure random bytes. michael@0: * michael@0: * @param length michael@0: * Number of bytes to generate. michael@0: */ michael@0: public static byte[] generateRandomBytes(int length) { michael@0: byte[] bytes = new byte[length]; michael@0: sharedSecureRandom.nextBytes(bytes); michael@0: return bytes; michael@0: } michael@0: michael@0: /** michael@0: * Helper to generate a random integer in a specified range. michael@0: * michael@0: * @param r michael@0: * Generate an integer between 0 and r-1 inclusive. michael@0: */ michael@0: public static BigInteger generateBigIntegerLessThan(BigInteger r) { michael@0: int maxBytes = (int) Math.ceil(((double) r.bitLength()) / 8); michael@0: BigInteger randInt = new BigInteger(generateRandomBytes(maxBytes)); michael@0: return randInt.mod(r); michael@0: } michael@0: michael@0: /** michael@0: * Helper to reseed the shared secure random number generator. michael@0: */ michael@0: public static void reseedSharedRandom() { michael@0: sharedSecureRandom.setSeed(sharedSecureRandom.generateSeed(8)); michael@0: } michael@0: michael@0: /** michael@0: * Helper to convert a byte array to a hex-encoded string michael@0: */ michael@0: public static String byte2Hex(final byte[] b) { michael@0: return byte2Hex(b, 2 * b.length); michael@0: } michael@0: michael@0: public static String byte2Hex(final byte[] b, int hexLength) { michael@0: final StringBuilder hs = new StringBuilder(Math.max(2*b.length, hexLength)); michael@0: String stmp; michael@0: michael@0: for (int n = 0; n < hexLength - 2*b.length; n++) { michael@0: hs.append("0"); michael@0: } michael@0: michael@0: for (int n = 0; n < b.length; n++) { michael@0: stmp = Integer.toHexString(b[n] & 0XFF); michael@0: michael@0: if (stmp.length() == 1) { michael@0: hs.append("0"); michael@0: } michael@0: hs.append(stmp); michael@0: } michael@0: michael@0: return hs.toString(); michael@0: } michael@0: michael@0: public static byte[] concatAll(byte[] first, byte[]... rest) { michael@0: int totalLength = first.length; michael@0: for (byte[] array : rest) { michael@0: totalLength += array.length; michael@0: } michael@0: michael@0: byte[] result = new byte[totalLength]; michael@0: int offset = first.length; michael@0: michael@0: System.arraycopy(first, 0, result, 0, offset); michael@0: michael@0: for (byte[] array : rest) { michael@0: System.arraycopy(array, 0, result, offset, array.length); michael@0: offset += array.length; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Utility for Base64 decoding. Should ensure that the correct michael@0: * Apache Commons version is used. michael@0: * michael@0: * @param base64 michael@0: * An input string. Will be decoded as UTF-8. michael@0: * @return michael@0: * A byte array of decoded values. michael@0: * @throws UnsupportedEncodingException michael@0: * Should not occur. michael@0: */ michael@0: public static byte[] decodeBase64(String base64) throws UnsupportedEncodingException { michael@0: return Base64.decodeBase64(base64.getBytes("UTF-8")); michael@0: } michael@0: michael@0: @SuppressLint("DefaultLocale") michael@0: public static byte[] decodeFriendlyBase32(String base32) { michael@0: Base32 converter = new Base32(); michael@0: final String translated = base32.replace('8', 'l').replace('9', 'o'); michael@0: return converter.decode(translated.toUpperCase()); michael@0: } michael@0: michael@0: public static byte[] hex2Byte(String str, int byteLength) { michael@0: byte[] second = hex2Byte(str); michael@0: if (second.length >= byteLength) { michael@0: return second; michael@0: } michael@0: // New Java arrays are zeroed: michael@0: // http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5 michael@0: byte[] first = new byte[byteLength - second.length]; michael@0: return Utils.concatAll(first, second); michael@0: } michael@0: michael@0: public static byte[] hex2Byte(String str) { michael@0: if (str.length() % 2 == 1) { michael@0: str = "0" + str; michael@0: } michael@0: michael@0: byte[] bytes = new byte[str.length() / 2]; michael@0: for (int i = 0; i < bytes.length; i++) { michael@0: bytes[i] = (byte) Integer.parseInt(str.substring(2 * i, 2 * i + 2), 16); michael@0: } michael@0: return bytes; michael@0: } michael@0: michael@0: public static String millisecondsToDecimalSecondsString(long ms) { michael@0: return millisecondsToDecimalSeconds(ms).toString(); michael@0: } michael@0: michael@0: // For dumping into JSON without quotes. michael@0: public static BigDecimal millisecondsToDecimalSeconds(long ms) { michael@0: return new BigDecimal(ms).movePointLeft(3); michael@0: } michael@0: michael@0: // This lives until Bug 708956 lands, and we don't have to do it any more. michael@0: public static long decimalSecondsToMilliseconds(String decimal) { michael@0: try { michael@0: return new BigDecimal(decimal).movePointRight(3).longValue(); michael@0: } catch (Exception e) { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: // Oh, Java. michael@0: public static long decimalSecondsToMilliseconds(Double decimal) { michael@0: // Truncates towards 0. michael@0: return (long)(decimal * 1000); michael@0: } michael@0: michael@0: public static long decimalSecondsToMilliseconds(Long decimal) { michael@0: return decimal * 1000; michael@0: } michael@0: michael@0: public static long decimalSecondsToMilliseconds(Integer decimal) { michael@0: return (long)(decimal * 1000); michael@0: } michael@0: michael@0: public static byte[] sha256(byte[] in) michael@0: throws NoSuchAlgorithmException { michael@0: MessageDigest sha1 = MessageDigest.getInstance("SHA-256"); michael@0: return sha1.digest(in); michael@0: } michael@0: michael@0: protected static byte[] sha1(final String utf8) michael@0: throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: final byte[] bytes = utf8.getBytes("UTF-8"); michael@0: try { michael@0: return NativeCrypto.sha1(bytes); michael@0: } catch (final LinkageError e) { michael@0: // This will throw UnsatisifiedLinkError (missing mozglue) the first time it is called, and michael@0: // ClassNotDefFoundError, for the uninitialized NativeCrypto class, each subsequent time this michael@0: // is called; LinkageError is their common ancestor. michael@0: Logger.warn(LOG_TAG, "Got throwable stretching password using native sha1 implementation; " + michael@0: "ignoring and using Java implementation.", e); michael@0: final MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); michael@0: return sha1.digest(utf8.getBytes("UTF-8")); michael@0: } michael@0: } michael@0: michael@0: protected static String sha1Base32(final String utf8) michael@0: throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: return new Base32().encodeAsString(sha1(utf8)).toLowerCase(Locale.US); michael@0: } michael@0: michael@0: /** michael@0: * If we encounter characters not allowed by the API (as found for michael@0: * instance in an email address), hash the value. michael@0: * @param account michael@0: * An account string. michael@0: * @return michael@0: * An acceptable string. michael@0: * @throws UnsupportedEncodingException michael@0: * @throws NoSuchAlgorithmException michael@0: */ michael@0: public static String usernameFromAccount(final String account) throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: if (account == null || account.equals("")) { michael@0: throw new IllegalArgumentException("No account name provided."); michael@0: } michael@0: if (account.matches("^[A-Za-z0-9._-]+$")) { michael@0: return account.toLowerCase(Locale.US); michael@0: } michael@0: return sha1Base32(account.toLowerCase(Locale.US)); michael@0: } michael@0: michael@0: public static SharedPreferences getSharedPreferences(final Context context, final String product, final String username, final String serverURL, final String profile, final long version) michael@0: throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: String prefsPath = getPrefsPath(product, username, serverURL, profile, version); michael@0: return context.getSharedPreferences(prefsPath, SHARED_PREFERENCES_MODE); michael@0: } michael@0: michael@0: /** michael@0: * Get shared preferences path for a Sync account. michael@0: * michael@0: * @param product the Firefox Sync product package name (like "org.mozilla.firefox"). michael@0: * @param username the Sync account name, optionally encoded with Utils.usernameFromAccount. michael@0: * @param serverURL the Sync account server URL. michael@0: * @param profile the Firefox profile name. michael@0: * @param version the version of preferences to reference. michael@0: * @return the path. michael@0: * @throws NoSuchAlgorithmException michael@0: * @throws UnsupportedEncodingException michael@0: */ michael@0: public static String getPrefsPath(final String product, final String username, final String serverURL, final String profile, final long version) michael@0: throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: final String encodedAccount = sha1Base32(serverURL + ":" + usernameFromAccount(username)); michael@0: michael@0: if (version <= 0) { michael@0: return "sync.prefs." + encodedAccount; michael@0: } else { michael@0: final String sanitizedProduct = product.replace('.', '!').replace(' ', '!'); michael@0: return "sync.prefs." + sanitizedProduct + "." + encodedAccount + "." + profile + "." + version; michael@0: } michael@0: } michael@0: michael@0: public static void addToIndexBucketMap(TreeMap> map, long index, String value) { michael@0: ArrayList bucket = map.get(index); michael@0: if (bucket == null) { michael@0: bucket = new ArrayList(); michael@0: } michael@0: bucket.add(value); michael@0: map.put(index, bucket); michael@0: } michael@0: michael@0: /** michael@0: * Yes, an equality method that's null-safe. michael@0: */ michael@0: private static boolean same(Object a, Object b) { michael@0: if (a == b) { michael@0: return true; michael@0: } michael@0: if (a == null || b == null) { michael@0: return false; // If both null, case above applies. michael@0: } michael@0: return a.equals(b); michael@0: } michael@0: michael@0: /** michael@0: * Return true if the two arrays are both null, or are both arrays michael@0: * containing the same elements in the same order. michael@0: */ michael@0: public static boolean sameArrays(JSONArray a, JSONArray b) { michael@0: if (a == b) { michael@0: return true; michael@0: } michael@0: if (a == null || b == null) { michael@0: return false; michael@0: } michael@0: final int size = a.size(); michael@0: if (size != b.size()) { michael@0: return false; michael@0: } michael@0: for (int i = 0; i < size; ++i) { michael@0: if (!same(a.get(i), b.get(i))) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Takes a URI, extracting URI components. michael@0: * @param scheme the URI scheme on which to match. michael@0: */ michael@0: @SuppressWarnings("deprecation") michael@0: public static Map extractURIComponents(String scheme, String uri) { michael@0: if (uri.indexOf(scheme) != 0) { michael@0: throw new IllegalArgumentException("URI scheme does not match: " + scheme); michael@0: } michael@0: michael@0: // Do this the hard way to avoid taking a large dependency on michael@0: // HttpClient or getting all regex-tastic. michael@0: String components = uri.substring(scheme.length()); michael@0: HashMap out = new HashMap(); michael@0: String[] parts = components.split("&"); michael@0: for (int i = 0; i < parts.length; ++i) { michael@0: String part = parts[i]; michael@0: if (part.length() == 0) { michael@0: continue; michael@0: } michael@0: String[] pair = part.split("=", 2); michael@0: switch (pair.length) { michael@0: case 0: michael@0: continue; michael@0: case 1: michael@0: out.put(URLDecoder.decode(pair[0]), null); michael@0: break; michael@0: case 2: michael@0: out.put(URLDecoder.decode(pair[0]), URLDecoder.decode(pair[1])); michael@0: break; michael@0: } michael@0: } michael@0: return out; michael@0: } michael@0: michael@0: // Because TextUtils.join is not stubbed. michael@0: public static String toDelimitedString(String delimiter, Collection items) { michael@0: if (items == null || items.size() == 0) { michael@0: return ""; michael@0: } michael@0: michael@0: StringBuilder sb = new StringBuilder(); michael@0: int i = 0; michael@0: int c = items.size(); michael@0: for (Object object : items) { michael@0: sb.append(object.toString()); michael@0: if (++i < c) { michael@0: sb.append(delimiter); michael@0: } michael@0: } michael@0: return sb.toString(); michael@0: } michael@0: michael@0: public static String toCommaSeparatedString(Collection items) { michael@0: return toDelimitedString(", ", items); michael@0: } michael@0: michael@0: /** michael@0: * Names of stages to sync: (ALL intersect SYNC) intersect (ALL minus SKIP). michael@0: * michael@0: * @param knownStageNames collection of known stage names (set ALL above). michael@0: * @param toSync set SYNC above, or null to sync all known stages. michael@0: * @param toSkip set SKIP above, or null to not skip any stages. michael@0: * @return stage names. michael@0: */ michael@0: public static Collection getStagesToSync(final Collection knownStageNames, Collection toSync, Collection toSkip) { michael@0: if (toSkip == null) { michael@0: toSkip = new HashSet(); michael@0: } else { michael@0: toSkip = new HashSet(toSkip); michael@0: } michael@0: michael@0: if (toSync == null) { michael@0: toSync = new HashSet(knownStageNames); michael@0: } else { michael@0: toSync = new HashSet(toSync); michael@0: } michael@0: toSync.retainAll(knownStageNames); michael@0: toSync.removeAll(toSkip); michael@0: return toSync; michael@0: } michael@0: michael@0: /** michael@0: * Get names of stages to sync: (ALL intersect SYNC) intersect (ALL minus SKIP). michael@0: * michael@0: * @param knownStageNames collection of known stage names (set ALL above). michael@0: * @param extras michael@0: * a Bundle instance (possibly null) optionally containing keys michael@0: * EXTRAS_KEY_STAGES_TO_SYNC (set SYNC above) and michael@0: * EXTRAS_KEY_STAGES_TO_SKIP (set SKIP above). michael@0: * @return stage names. michael@0: */ michael@0: public static Collection getStagesToSyncFromBundle(final Collection knownStageNames, final Bundle extras) { michael@0: if (extras == null) { michael@0: return knownStageNames; michael@0: } michael@0: String toSyncString = extras.getString(Constants.EXTRAS_KEY_STAGES_TO_SYNC); michael@0: String toSkipString = extras.getString(Constants.EXTRAS_KEY_STAGES_TO_SKIP); michael@0: if (toSyncString == null && toSkipString == null) { michael@0: return knownStageNames; michael@0: } michael@0: michael@0: ArrayList toSync = null; michael@0: ArrayList toSkip = null; michael@0: if (toSyncString != null) { michael@0: try { michael@0: toSync = new ArrayList(ExtendedJSONObject.parseJSONObject(toSyncString).keySet()); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Got exception parsing stages to sync: '" + toSyncString + "'.", e); michael@0: } michael@0: } michael@0: if (toSkipString != null) { michael@0: try { michael@0: toSkip = new ArrayList(ExtendedJSONObject.parseJSONObject(toSkipString).keySet()); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Got exception parsing stages to skip: '" + toSkipString + "'.", e); michael@0: } michael@0: } michael@0: michael@0: Logger.info(LOG_TAG, "Asked to sync '" + Utils.toCommaSeparatedString(toSync) + michael@0: "' and to skip '" + Utils.toCommaSeparatedString(toSkip) + "'."); michael@0: return getStagesToSync(knownStageNames, toSync, toSkip); michael@0: } michael@0: michael@0: /** michael@0: * Put names of stages to sync and to skip into sync extras bundle. michael@0: * michael@0: * @param bundle michael@0: * a Bundle instance (possibly null). michael@0: * @param stagesToSync michael@0: * collection of stage names to sync: key michael@0: * EXTRAS_KEY_STAGES_TO_SYNC; ignored if null. michael@0: * @param stagesToSkip michael@0: * collection of stage names to skip: key michael@0: * EXTRAS_KEY_STAGES_TO_SKIP; ignored if null. michael@0: */ michael@0: public static void putStageNamesToSync(final Bundle bundle, final String[] stagesToSync, final String[] stagesToSkip) { michael@0: if (bundle == null) { michael@0: return; michael@0: } michael@0: michael@0: if (stagesToSync != null) { michael@0: ExtendedJSONObject o = new ExtendedJSONObject(); michael@0: for (String stageName : stagesToSync) { michael@0: o.put(stageName, 0); michael@0: } michael@0: bundle.putString(Constants.EXTRAS_KEY_STAGES_TO_SYNC, o.toJSONString()); michael@0: } michael@0: michael@0: if (stagesToSkip != null) { michael@0: ExtendedJSONObject o = new ExtendedJSONObject(); michael@0: for (String stageName : stagesToSkip) { michael@0: o.put(stageName, 0); michael@0: } michael@0: bundle.putString(Constants.EXTRAS_KEY_STAGES_TO_SKIP, o.toJSONString()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Read contents of file as a string. michael@0: * michael@0: * @param context Android context. michael@0: * @param filename name of file to read; must not be null. michael@0: * @return String instance. michael@0: */ michael@0: public static String readFile(final Context context, final String filename) { michael@0: if (filename == null) { michael@0: throw new IllegalArgumentException("Passed null filename in readFile."); michael@0: } michael@0: michael@0: FileInputStream fis = null; michael@0: InputStreamReader isr = null; michael@0: BufferedReader br = null; michael@0: michael@0: try { michael@0: fis = context.openFileInput(filename); michael@0: isr = new InputStreamReader(fis); michael@0: br = new BufferedReader(isr); michael@0: StringBuilder sb = new StringBuilder(); michael@0: String line; michael@0: while ((line = br.readLine()) != null) { michael@0: sb.append(line); michael@0: } michael@0: return sb.toString(); michael@0: } catch (Exception e) { michael@0: return null; michael@0: } finally { michael@0: if (isr != null) { michael@0: try { michael@0: isr.close(); michael@0: } catch (IOException e) { michael@0: // Ignore. michael@0: } michael@0: } michael@0: if (fis != null) { michael@0: try { michael@0: fis.close(); michael@0: } catch (IOException e) { michael@0: // Ignore. michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Format a duration as a string, like "0.56 seconds". michael@0: * michael@0: * @param startMillis start time in milliseconds. michael@0: * @param endMillis end time in milliseconds. michael@0: * @return formatted string. michael@0: */ michael@0: public static String formatDuration(long startMillis, long endMillis) { michael@0: final long duration = endMillis - startMillis; michael@0: return new DecimalFormat("#0.00 seconds").format(((double) duration) / 1000); michael@0: } michael@0: michael@0: /** michael@0: * This will take a string containing a UTF-8 representation of a UTF-8 michael@0: * byte array — e.g., "pïgéons1" — and return UTF-8 (e.g., "pïgéons1"). michael@0: * michael@0: * This is the format produced by desktop Firefox when exchanging credentials michael@0: * containing non-ASCII characters. michael@0: */ michael@0: public static String decodeUTF8(final String in) throws UnsupportedEncodingException { michael@0: final int length = in.length(); michael@0: final byte[] asciiBytes = new byte[length]; michael@0: for (int i = 0; i < length; ++i) { michael@0: asciiBytes[i] = (byte) in.codePointAt(i); michael@0: } michael@0: return new String(asciiBytes, "UTF-8"); michael@0: } michael@0: michael@0: /** michael@0: * Replace "foo@bar.com" with "XXX@XXX.XXX". michael@0: */ michael@0: public static String obfuscateEmail(final String in) { michael@0: return in.replaceAll("[^@\\.]", "X"); michael@0: } michael@0: michael@0: public static String nodeWeaveURL(String serverURL, String username) { michael@0: String userPart = username + "/node/weave"; michael@0: if (serverURL == null) { michael@0: return SyncConstants.DEFAULT_AUTH_SERVER + "user/1.0/" + userPart; michael@0: } michael@0: if (!serverURL.endsWith("/")) { michael@0: serverURL = serverURL + "/"; michael@0: } michael@0: return serverURL + "user/1.0/" + userPart; michael@0: } michael@0: michael@0: public static void throwIfNull(Object... objects) { michael@0: for (Object object : objects) { michael@0: if (object == null) { michael@0: throw new IllegalArgumentException("object must not be null"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Gecko uses locale codes like "es-ES", whereas a Java {@link Locale} michael@0: * stringifies as "es_ES". michael@0: * michael@0: * This method approximates the Java 7 method Locale#toLanguageTag(). michael@0: *

michael@0: * Warning: all consumers of this method will need to be audited when michael@0: * we have active locale switching. michael@0: * michael@0: * @return a locale string suitable for passing to Gecko. michael@0: */ michael@0: public static String getLanguageTag(final Locale locale) { michael@0: // If this were Java 7: michael@0: // return locale.toLanguageTag(); michael@0: michael@0: String language = locale.getLanguage(); // Can, but should never be, an empty string. michael@0: // Modernize certain language codes. michael@0: if (language.equals("iw")) { michael@0: language = "he"; michael@0: } else if (language.equals("in")) { michael@0: language = "id"; michael@0: } else if (language.equals("ji")) { michael@0: language = "yi"; michael@0: } michael@0: michael@0: String country = locale.getCountry(); // Can be an empty string. michael@0: if (country.equals("")) { michael@0: return language; michael@0: } michael@0: return language + "-" + country; michael@0: } michael@0: }