4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
7 * see file 'COPYING' for use and warranty information
9 * This program is free software you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package org.fedorahosted.freeotp;
25 import java.nio.ByteBuffer;
26 import java.security.InvalidKeyException;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Locale;
32 import javax.crypto.Mac;
33 import javax.crypto.spec.SecretKeySpec;
35 import android.content.Context;
36 import android.content.SharedPreferences;
37 import android.net.Uri;
39 import com.google.android.apps.authenticator.Base32String;
40 import com.google.android.apps.authenticator.Base32String.DecodingException;
43 public static class TokenUriInvalidException extends Exception {
44 private static final long serialVersionUID = -1108624734612362345L;
47 public static enum TokenType {
51 private final String issuerInt;
52 private final String issuerExt;
53 private final String label;
54 private TokenType type;
61 public static List<Token> getTokens(Context ctx) {
62 SharedPreferences prefs = ctx.getSharedPreferences(Token.class.getName(), Context.MODE_PRIVATE);
64 List<Token> tokens = new ArrayList<Token>();
65 for (String key : prefs.getAll().keySet()) {
67 tokens.add(new Token(prefs.getString(key, null)));
68 } catch (TokenUriInvalidException e) {
76 private Token(Uri uri) throws TokenUriInvalidException {
77 if (!uri.getScheme().equals("otpauth"))
78 throw new TokenUriInvalidException();
80 if (uri.getAuthority().equals("totp"))
81 type = TokenType.TOTP;
82 else if (uri.getAuthority().equals("hotp"))
83 type = TokenType.HOTP;
85 throw new TokenUriInvalidException();
87 String path = uri.getPath();
89 throw new TokenUriInvalidException();
91 // Strip the path of its leading '/'
92 for (int i = 0; path.charAt(i) == '/'; i++)
93 path = path.substring(1);
94 if (path.length() == 0)
95 throw new TokenUriInvalidException();
97 int i = path.indexOf(':');
98 issuerExt = i < 0 ? "" : path.substring(0, i);
99 issuerInt = uri.getQueryParameter("issuer");
100 label = path.substring(i >= 0 ? i + 1 : 0);
102 algo = uri.getQueryParameter("algorithm");
105 algo = algo.toUpperCase(Locale.US);
107 Mac.getInstance("Hmac" + algo);
108 } catch (NoSuchAlgorithmException e1) {
109 throw new TokenUriInvalidException();
113 String d = uri.getQueryParameter("digits");
116 digits = Integer.parseInt(d);
117 if (digits != 6 && digits != 8)
118 throw new TokenUriInvalidException();
119 } catch (NumberFormatException e) {
120 throw new TokenUriInvalidException();
126 String c = uri.getQueryParameter("counter");
129 counter = Long.parseLong(c);
130 } catch (NumberFormatException e) {
131 throw new TokenUriInvalidException();
136 String p = uri.getQueryParameter("period");
139 period = Integer.parseInt(p);
140 } catch (NumberFormatException e) {
141 throw new TokenUriInvalidException();
147 String s = uri.getQueryParameter("secret");
148 key = Base32String.decode(s);
149 } catch (DecodingException e) {
150 throw new TokenUriInvalidException();
154 private String getHOTP(long counter) {
155 // Encode counter in network byte order
156 ByteBuffer bb = ByteBuffer.allocate(8);
159 // Create digits divisor
161 for (int i = digits; i > 0; i--)
166 Mac mac = Mac.getInstance("Hmac" + algo);
167 mac.init(new SecretKeySpec(key, "Hmac" + algo));
170 byte[] digest = mac.doFinal(bb.array());
174 int off = digest[digest.length - 1] & 0xf;
175 binary = (digest[off + 0] & 0x7f) << 0x18;
176 binary |= (digest[off + 1] & 0xff) << 0x10;
177 binary |= (digest[off + 2] & 0xff) << 0x08;
178 binary |= (digest[off + 3] & 0xff) << 0x00;
179 binary = binary % div;
182 String hotp = Integer.toString(binary);
183 while (hotp.length() != digits)
187 } catch (InvalidKeyException e) {
189 } catch (NoSuchAlgorithmException e) {
196 public Token(String uri) throws TokenUriInvalidException {
197 this(Uri.parse(uri));
200 private String getId() {
202 if (issuerInt != null && !issuerInt.equals(""))
203 id = issuerInt + ":" + label;
204 else if (issuerExt != null && !issuerExt.equals(""))
205 id = issuerExt + ":" + label;
212 public void remove(Context ctx) {
213 SharedPreferences prefs = ctx.getSharedPreferences(Token.class.getName(), Context.MODE_PRIVATE);
214 prefs.edit().remove(getId()).apply();
217 public void save(Context ctx) {
218 SharedPreferences prefs = ctx.getSharedPreferences(Token.class.getName(), Context.MODE_PRIVATE);
219 prefs.edit().putString(getId(), toString()).apply();
222 public String getTitle() {
224 if (issuerExt != null && !issuerExt.equals(""))
225 title += issuerExt + ": ";
230 public String getCurrentTokenValue(Context ctx, boolean increment) {
231 if (type == TokenType.HOTP) {
234 return getHOTP(counter++);
239 String placeholder = "";
240 for (int i = 0; i < digits; i++)
247 return getHOTP(System.currentTimeMillis() / 1000 / period);
251 String issuerLabel = !issuerExt.equals("") ? issuerExt + ":" + label : label;
253 Uri.Builder builder = new Uri.Builder()
256 .appendQueryParameter("secret", Base32String.encode(key))
257 .appendQueryParameter("issuer", issuerInt == null ? issuerExt : issuerInt)
258 .appendQueryParameter("algorithm", algo)
259 .appendQueryParameter("digits", Integer.toString(digits));
263 builder.authority("hotp");
264 builder.appendQueryParameter("counter", Long.toString(counter));
267 builder.authority("totp");
268 builder.appendQueryParameter("period", Integer.toString(period));
272 return builder.build();
275 public TokenType getType() {
279 // Progress is on a scale from 0 - 1000.
280 public int getProgress() {
283 long time = System.currentTimeMillis() / 100;
284 return (int) ((time % p) * 1000 / p);
288 public String toString() {
289 return toUri().toString();