Merge pull request #491 from igaryhe/master

Read OTP digits, algorithm, period from URI, support Steam Guard
This commit is contained in:
Harsh Shandilya 2019-03-19 20:34:41 +05:30 committed by GitHub
commit 94bf103b33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 11 deletions

View file

@ -11,7 +11,10 @@ import java.io.UnsupportedEncodingException
class PasswordEntry(private val content: String) { class PasswordEntry(private val content: String) {
val password: String val password: String
val username: String? val username: String?
val digits: String
val totpSecret: String? val totpSecret: String?
val totpPeriod: Long
val totpAlgorithm: String
val hotpSecret: String? val hotpSecret: String?
val hotpCounter: Long? val hotpCounter: Long?
var extraContent: String? = null var extraContent: String? = null
@ -24,7 +27,10 @@ class PasswordEntry(private val content: String) {
init { init {
val passContent = content.split("\n".toRegex(), 2).toTypedArray() val passContent = content.split("\n".toRegex(), 2).toTypedArray()
password = passContent[0] password = passContent[0]
digits = findOtpDigits(content)
totpSecret = findTotpSecret(content) totpSecret = findTotpSecret(content)
totpPeriod = findTotpPeriod(content)
totpAlgorithm = findTotpAlgorithm(content)
hotpSecret = findHotpSecret(content) hotpSecret = findHotpSecret(content)
hotpCounter = findHotpCounter(content) hotpCounter = findHotpCounter(content)
extraContent = findExtraContent(passContent) extraContent = findExtraContent(passContent)
@ -81,6 +87,37 @@ class PasswordEntry(private val content: String) {
return null return null
} }
private fun findOtpDigits(decryptedContent: String): String {
decryptedContent.split("\n".toRegex()).forEach { line ->
if (line.startsWith("otpauth://totp/") ||
line.startsWith("otpauth://hotp/") &&
Uri.parse(line).getQueryParameter("digits") != null) {
return Uri.parse(line).getQueryParameter("digits")!!
}
}
return "6"
}
private fun findTotpPeriod(decryptedContent: String): Long {
decryptedContent.split("\n".toRegex()).forEach { line ->
if (line.startsWith("otpauth://totp/") &&
Uri.parse(line).getQueryParameter("period") != null) {
return java.lang.Long.parseLong(Uri.parse(line).getQueryParameter("period")!!)
}
}
return 30
}
private fun findTotpAlgorithm(decryptedContent: String): String {
decryptedContent.split("\n".toRegex()).forEach { line ->
if (line.startsWith("otpauth://totp/") &&
Uri.parse(line).getQueryParameter("algorithm") != null) {
return Uri.parse(line).getQueryParameter("algorithm")!!
}
}
return "sha1"
}
private fun findHotpSecret(decryptedContent: String): String? { private fun findHotpSecret(decryptedContent: String): String? {
decryptedContent.split("\n".toRegex()).forEach { line -> decryptedContent.split("\n".toRegex()).forEach { line ->
if (line.startsWith("otpauth://hotp/")) { if (line.startsWith("otpauth://hotp/")) {

View file

@ -305,12 +305,17 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
copyOtpToClipBoard( copyOtpToClipBoard(
Otp.calculateCode( Otp.calculateCode(
entry.totpSecret, entry.totpSecret,
Date().time / (1000 * Otp.TIME_WINDOW) Date().time / (1000 * entry.totpPeriod),
) entry.totpAlgorithm,
entry.digits)
) )
} }
crypto_otp_show.text = crypto_otp_show.text =
Otp.calculateCode(entry.totpSecret, Date().time / (1000 * Otp.TIME_WINDOW)) Otp.calculateCode(
entry.totpSecret,
Date().time / (1000 * entry.totpPeriod),
entry.totpAlgorithm,
entry.digits)
} else { } else {
// we only want to calculate and show HOTP if the user requests it // we only want to calculate and show HOTP if the user requests it
crypto_copy_otp.setOnClickListener { crypto_copy_otp.setOnClickListener {
@ -494,8 +499,8 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
} }
private fun calculateHotp(entry: PasswordEntry) { private fun calculateHotp(entry: PasswordEntry) {
copyOtpToClipBoard(Otp.calculateCode(entry.hotpSecret, entry.hotpCounter!! + 1)) copyOtpToClipBoard(Otp.calculateCode(entry.hotpSecret, entry.hotpCounter!! + 1, "sha1", entry.digits))
crypto_otp_show.text = Otp.calculateCode(entry.hotpSecret, entry.hotpCounter!! + 1) crypto_otp_show.text = Otp.calculateCode(entry.hotpSecret, entry.hotpCounter!! + 1, "sha1", entry.digits)
crypto_extra_show.text = entry.extraContent crypto_extra_show.text = entry.extraContent
} }

View file

@ -12,17 +12,17 @@ import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
public class Otp { public class Otp {
public static final int TIME_WINDOW = 30;
private static final String ALGORITHM = "HmacSHA1";
private static final int CODE_DIGITS = 6;
private static final Base32 BASE_32 = new Base32(); private static final Base32 BASE_32 = new Base32();
private Otp() { private Otp() {
} }
public static String calculateCode(String secret, long counter) { public static String calculateCode(String secret, long counter, String algorithm, String digits) {
String[] steam = {"2", "3", "4", "5", "6", "7", "8", "9", "B", "C",
"D", "F", "G", "H", "J", "K", "M", "N", "P", "Q",
"R", "T", "V", "W", "X", "Y"};
String ALGORITHM = "Hmac" + algorithm.toUpperCase();
SecretKeySpec signingKey = new SecretKeySpec(BASE_32.decode(secret), ALGORITHM); SecretKeySpec signingKey = new SecretKeySpec(BASE_32.decode(secret), ALGORITHM);
Mac mac; Mac mac;
@ -42,6 +42,15 @@ public class Otp {
byte[] code = Arrays.copyOfRange(digest, offset, offset + 4); byte[] code = Arrays.copyOfRange(digest, offset, offset + 4);
code[0] = (byte) (0x7f & code[0]); code[0] = (byte) (0x7f & code[0]);
String strCode = new BigInteger(code).toString(); String strCode = new BigInteger(code).toString();
return strCode.substring(strCode.length() - CODE_DIGITS); if (digits.equals("s")) {
String output = "";
int bigInt = new BigInteger(code).intValue();
for (int i = 0; i != 5; i++) {
output += steam[bigInt % 26];
bigInt /= 26;
}
return output;
}
else return strCode.substring(strCode.length() - Integer.parseInt(digits));
} }
} }