ScramMechanism.java

  1package eu.siacs.conversations.crypto.sasl;
  2
  3import android.annotation.TargetApi;
  4import android.os.Build;
  5import android.util.Base64;
  6import android.util.LruCache;
  7
  8import org.bouncycastle.crypto.Digest;
  9import org.bouncycastle.crypto.macs.HMac;
 10import org.bouncycastle.crypto.params.KeyParameter;
 11
 12import java.math.BigInteger;
 13import java.nio.charset.Charset;
 14import java.security.InvalidKeyException;
 15import java.security.SecureRandom;
 16
 17import eu.siacs.conversations.entities.Account;
 18import eu.siacs.conversations.utils.CryptoHelper;
 19import eu.siacs.conversations.xml.TagWriter;
 20
 21@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
 22abstract class ScramMechanism extends SaslMechanism {
 23	// TODO: When channel binding (SCRAM-SHA1-PLUS) is supported in future, generalize this to indicate support and/or usage.
 24	private final static String GS2_HEADER = "n,,";
 25	private String clientFirstMessageBare;
 26	private final String clientNonce;
 27	private byte[] serverSignature = null;
 28	static HMac HMAC;
 29	static Digest DIGEST;
 30	private static final byte[] CLIENT_KEY_BYTES = "Client Key".getBytes();
 31	private static final byte[] SERVER_KEY_BYTES = "Server Key".getBytes();
 32
 33	private static class KeyPair {
 34		final byte[] clientKey;
 35		final byte[] serverKey;
 36
 37		KeyPair(final byte[] clientKey, final byte[] serverKey) {
 38			this.clientKey = clientKey;
 39			this.serverKey = serverKey;
 40		}
 41	}
 42
 43	static {
 44		CACHE = new LruCache<String, KeyPair>(10) {
 45			protected KeyPair create(final String k) {
 46				// Map keys are "bytesToHex(JID),bytesToHex(password),bytesToHex(salt),iterations".
 47				// Changing any of these values forces a cache miss. `CryptoHelper.bytesToHex()'
 48				// is applied to prevent commas in the strings breaking things.
 49				final String[] kparts = k.split(",", 4);
 50				try {
 51					final byte[] saltedPassword, serverKey, clientKey;
 52					saltedPassword = hi(CryptoHelper.hexToString(kparts[1]).getBytes(),
 53							Base64.decode(CryptoHelper.hexToString(kparts[2]), Base64.DEFAULT), Integer.valueOf(kparts[3]));
 54					serverKey = hmac(saltedPassword, SERVER_KEY_BYTES);
 55					clientKey = hmac(saltedPassword, CLIENT_KEY_BYTES);
 56
 57					return new KeyPair(clientKey, serverKey);
 58				} catch (final InvalidKeyException | NumberFormatException e) {
 59					return null;
 60				}
 61			}
 62		};
 63	}
 64
 65	private static final LruCache<String, KeyPair> CACHE;
 66
 67	protected State state = State.INITIAL;
 68
 69	ScramMechanism(final TagWriter tagWriter, final Account account, final SecureRandom rng) {
 70		super(tagWriter, account, rng);
 71
 72		// This nonce should be different for each authentication attempt.
 73		clientNonce = new BigInteger(100, this.rng).toString(32);
 74		clientFirstMessageBare = "";
 75	}
 76
 77	@Override
 78	public String getClientFirstMessage() {
 79		if (clientFirstMessageBare.isEmpty() && state == State.INITIAL) {
 80			clientFirstMessageBare = "n=" + CryptoHelper.saslEscape(CryptoHelper.saslPrep(account.getUsername())) +
 81				",r=" + this.clientNonce;
 82			state = State.AUTH_TEXT_SENT;
 83		}
 84		return Base64.encodeToString(
 85				(GS2_HEADER + clientFirstMessageBare).getBytes(Charset.defaultCharset()),
 86				Base64.NO_WRAP);
 87	}
 88
 89	@Override
 90	public String getResponse(final String challenge) throws AuthenticationException {
 91		switch (state) {
 92			case AUTH_TEXT_SENT:
 93				if (challenge == null) {
 94					throw new AuthenticationException("challenge can not be null");
 95				}
 96				byte[] serverFirstMessage = Base64.decode(challenge, Base64.DEFAULT);
 97				final Tokenizer tokenizer = new Tokenizer(serverFirstMessage);
 98				String nonce = "";
 99				int iterationCount = -1;
100				String salt = "";
101				for (final String token : tokenizer) {
102					if (token.charAt(1) == '=') {
103						switch (token.charAt(0)) {
104							case 'i':
105								try {
106									iterationCount = Integer.parseInt(token.substring(2));
107								} catch (final NumberFormatException e) {
108									throw new AuthenticationException(e);
109								}
110								break;
111							case 's':
112								salt = token.substring(2);
113								break;
114							case 'r':
115								nonce = token.substring(2);
116								break;
117							case 'm':
118								/*
119								 * RFC 5802:
120								 * m: This attribute is reserved for future extensibility.  In this
121								 * version of SCRAM, its presence in a client or a server message
122								 * MUST cause authentication failure when the attribute is parsed by
123								 * the other end.
124								 */
125								throw new AuthenticationException("Server sent reserved token: `m'");
126						}
127					}
128				}
129
130				if (iterationCount < 0) {
131					throw new AuthenticationException("Server did not send iteration count");
132				}
133				if (nonce.isEmpty() || !nonce.startsWith(clientNonce)) {
134					throw new AuthenticationException("Server nonce does not contain client nonce: " + nonce);
135				}
136				if (salt.isEmpty()) {
137					throw new AuthenticationException("Server sent empty salt");
138				}
139
140				final String clientFinalMessageWithoutProof = "c=" + Base64.encodeToString(
141						GS2_HEADER.getBytes(), Base64.NO_WRAP) + ",r=" + nonce;
142				final byte[] authMessage = (clientFirstMessageBare + ',' + new String(serverFirstMessage) + ','
143						+ clientFinalMessageWithoutProof).getBytes();
144
145				// Map keys are "bytesToHex(JID),bytesToHex(password),bytesToHex(salt),iterations".
146				final KeyPair keys = CACHE.get(
147						CryptoHelper.bytesToHex(account.getJid().toBareJid().toString().getBytes()) + ","
148						+ CryptoHelper.bytesToHex(account.getPassword().getBytes()) + ","
149						+ CryptoHelper.bytesToHex(salt.getBytes()) + ","
150						+ String.valueOf(iterationCount)
151						);
152				if (keys == null) {
153					throw new AuthenticationException("Invalid keys generated");
154				}
155				final byte[] clientSignature;
156				try {
157					serverSignature = hmac(keys.serverKey, authMessage);
158					final byte[] storedKey = digest(keys.clientKey);
159
160					clientSignature = hmac(storedKey, authMessage);
161
162				} catch (final InvalidKeyException e) {
163					throw new AuthenticationException(e);
164				}
165
166				final byte[] clientProof = new byte[keys.clientKey.length];
167
168				for (int i = 0; i < clientProof.length; i++) {
169					clientProof[i] = (byte) (keys.clientKey[i] ^ clientSignature[i]);
170				}
171
172
173				final String clientFinalMessage = clientFinalMessageWithoutProof + ",p=" +
174					Base64.encodeToString(clientProof, Base64.NO_WRAP);
175				state = State.RESPONSE_SENT;
176				return Base64.encodeToString(clientFinalMessage.getBytes(), Base64.NO_WRAP);
177			case RESPONSE_SENT:
178				try {
179					final String clientCalculatedServerFinalMessage = "v=" +
180						Base64.encodeToString(serverSignature, Base64.NO_WRAP);
181					if (!clientCalculatedServerFinalMessage.equals(new String(Base64.decode(challenge, Base64.DEFAULT)))) {
182						throw new Exception();
183					}
184					state = State.VALID_SERVER_RESPONSE;
185					return "";
186				} catch(Exception e) {
187					throw new AuthenticationException("Server final message does not match calculated final message");
188				}
189			default:
190				throw new InvalidStateException(state);
191		}
192	}
193
194	private static synchronized byte[] hmac(final byte[] key, final byte[] input)
195		throws InvalidKeyException {
196		HMAC.init(new KeyParameter(key));
197		HMAC.update(input, 0, input.length);
198		final byte[] out = new byte[HMAC.getMacSize()];
199		HMAC.doFinal(out, 0);
200		return out;
201	}
202
203	public static synchronized byte[] digest(byte[] bytes) {
204		DIGEST.reset();
205		DIGEST.update(bytes, 0, bytes.length);
206		final byte[] out = new byte[DIGEST.getDigestSize()];
207		DIGEST.doFinal(out, 0);
208		return out;
209	}
210
211	/*
212	 * Hi() is, essentially, PBKDF2 [RFC2898] with HMAC() as the
213	 * pseudorandom function (PRF) and with dkLen == output length of
214	 * HMAC() == output length of H().
215	 */
216	private static synchronized byte[] hi(final byte[] key, final byte[] salt, final int iterations)
217		throws InvalidKeyException {
218		byte[] u = hmac(key, CryptoHelper.concatenateByteArrays(salt, CryptoHelper.ONE));
219		byte[] out = u.clone();
220		for (int i = 1; i < iterations; i++) {
221			u = hmac(key, u);
222			for (int j = 0; j < u.length; j++) {
223				out[j] ^= u[j];
224			}
225		}
226		return out;
227	}
228}