mobile/android/base/sync/net/HMACAuthHeaderProvider.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.sync.net;
michael@0 6
michael@0 7 import java.io.UnsupportedEncodingException;
michael@0 8 import java.net.URI;
michael@0 9 import java.security.GeneralSecurityException;
michael@0 10 import java.security.InvalidKeyException;
michael@0 11 import java.security.NoSuchAlgorithmException;
michael@0 12
michael@0 13 import javax.crypto.Mac;
michael@0 14 import javax.crypto.spec.SecretKeySpec;
michael@0 15
michael@0 16 import org.mozilla.apache.commons.codec.binary.Base64;
michael@0 17 import org.mozilla.gecko.background.common.log.Logger;
michael@0 18 import org.mozilla.gecko.sync.Utils;
michael@0 19
michael@0 20 import ch.boye.httpclientandroidlib.Header;
michael@0 21 import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase;
michael@0 22 import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest;
michael@0 23 import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
michael@0 24 import ch.boye.httpclientandroidlib.message.BasicHeader;
michael@0 25 import ch.boye.httpclientandroidlib.protocol.BasicHttpContext;
michael@0 26
michael@0 27 /**
michael@0 28 * An <code>AuthHeaderProvider</code> that returns an Authorization header for
michael@0 29 * HMAC-SHA1-signed requests in the format expected by Mozilla Services
michael@0 30 * identity-attached services and specified by the MAC Authentication spec, available at
michael@0 31 * <a href="https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac">https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac</a>.
michael@0 32 * <p>
michael@0 33 * See <a href="https://wiki.mozilla.org/Services/Sagrada/ServiceClientFlow#Access">https://wiki.mozilla.org/Services/Sagrada/ServiceClientFlow#Access</a>.
michael@0 34 */
michael@0 35 public class HMACAuthHeaderProvider implements AuthHeaderProvider {
michael@0 36 public static final String LOG_TAG = "HMACAuthHeaderProvider";
michael@0 37
michael@0 38 public static final int NONCE_LENGTH_IN_BYTES = 8;
michael@0 39
michael@0 40 public static final String HMAC_SHA1_ALGORITHM = "hmacSHA1";
michael@0 41
michael@0 42 public final String identifier;
michael@0 43 public final String key;
michael@0 44
michael@0 45 public HMACAuthHeaderProvider(String identifier, String key) {
michael@0 46 // Validate identifier string. From the MAC Authentication spec:
michael@0 47 // id = "id" "=" string-value
michael@0 48 // string-value = ( <"> plain-string <"> ) / plain-string
michael@0 49 // plain-string = 1*( %x20-21 / %x23-5B / %x5D-7E )
michael@0 50 // We add quotes around the id string, so input identifier must be a plain-string.
michael@0 51 if (identifier == null) {
michael@0 52 throw new IllegalArgumentException("identifier must not be null.");
michael@0 53 }
michael@0 54 if (!isPlainString(identifier)) {
michael@0 55 throw new IllegalArgumentException("identifier must be a plain-string.");
michael@0 56 }
michael@0 57
michael@0 58 if (key == null) {
michael@0 59 throw new IllegalArgumentException("key must not be null.");
michael@0 60 }
michael@0 61
michael@0 62 this.identifier = identifier;
michael@0 63 this.key = key;
michael@0 64 }
michael@0 65
michael@0 66 @Override
michael@0 67 public Header getAuthHeader(HttpRequestBase request, BasicHttpContext context, DefaultHttpClient client) throws GeneralSecurityException {
michael@0 68 long timestamp = System.currentTimeMillis() / 1000;
michael@0 69 String nonce = Base64.encodeBase64String(Utils.generateRandomBytes(NONCE_LENGTH_IN_BYTES));
michael@0 70 String extra = "";
michael@0 71
michael@0 72 try {
michael@0 73 return getAuthHeader(request, context, client, timestamp, nonce, extra);
michael@0 74 } catch (InvalidKeyException e) {
michael@0 75 // We lie a little and make every exception a GeneralSecurityException.
michael@0 76 throw new GeneralSecurityException(e);
michael@0 77 } catch (UnsupportedEncodingException e) {
michael@0 78 throw new GeneralSecurityException(e);
michael@0 79 } catch (NoSuchAlgorithmException e) {
michael@0 80 throw new GeneralSecurityException(e);
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 /**
michael@0 85 * Test if input is a <code>plain-string</code>.
michael@0 86 * <p>
michael@0 87 * A plain-string is defined by the MAC Authentication spec as
michael@0 88 * <code>plain-string = 1*( %x20-21 / %x23-5B / %x5D-7E )</code>.
michael@0 89 *
michael@0 90 * @param input
michael@0 91 * as a String of "US-ASCII" bytes.
michael@0 92 * @return true if input is a <code>plain-string</code>; false otherwise.
michael@0 93 * @throws UnsupportedEncodingException
michael@0 94 */
michael@0 95 protected static boolean isPlainString(String input) {
michael@0 96 if (input == null || input.length() == 0) {
michael@0 97 return false;
michael@0 98 }
michael@0 99
michael@0 100 byte[] bytes;
michael@0 101 try {
michael@0 102 bytes = input.getBytes("US-ASCII");
michael@0 103 } catch (UnsupportedEncodingException e) {
michael@0 104 // Should never happen.
michael@0 105 Logger.warn(LOG_TAG, "Got exception in isPlainString; returning false.", e);
michael@0 106 return false;
michael@0 107 }
michael@0 108
michael@0 109 for (byte b : bytes) {
michael@0 110 if ((0x20 <= b && b <= 0x21) || (0x23 <= b && b <= 0x5B) || (0x5D <= b && b <= 0x7E)) {
michael@0 111 continue;
michael@0 112 }
michael@0 113 return false;
michael@0 114 }
michael@0 115
michael@0 116 return true;
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Helper function that generates an HTTP Authorization header given
michael@0 121 * additional MAC Authentication specific data.
michael@0 122 *
michael@0 123 * @throws UnsupportedEncodingException
michael@0 124 * @throws NoSuchAlgorithmException
michael@0 125 * @throws InvalidKeyException
michael@0 126 */
michael@0 127 protected Header getAuthHeader(HttpRequestBase request, BasicHttpContext context, DefaultHttpClient client,
michael@0 128 long timestamp, String nonce, String extra)
michael@0 129 throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException {
michael@0 130 // Validate timestamp. From the MAC Authentication spec:
michael@0 131 // timestamp = 1*DIGIT
michael@0 132 // This is equivalent to timestamp >= 0.
michael@0 133 if (timestamp < 0) {
michael@0 134 throw new IllegalArgumentException("timestamp must contain only [0-9].");
michael@0 135 }
michael@0 136
michael@0 137 // Validate nonce string. From the MAC Authentication spec:
michael@0 138 // nonce = "nonce" "=" string-value
michael@0 139 // string-value = ( <"> plain-string <"> ) / plain-string
michael@0 140 // plain-string = 1*( %x20-21 / %x23-5B / %x5D-7E )
michael@0 141 // We add quotes around the nonce string, so input nonce must be a plain-string.
michael@0 142 if (nonce == null) {
michael@0 143 throw new IllegalArgumentException("nonce must not be null.");
michael@0 144 }
michael@0 145 if (nonce.length() == 0) {
michael@0 146 throw new IllegalArgumentException("nonce must not be empty.");
michael@0 147 }
michael@0 148 if (!isPlainString(nonce)) {
michael@0 149 throw new IllegalArgumentException("nonce must be a plain-string.");
michael@0 150 }
michael@0 151
michael@0 152 // Validate extra string. From the MAC Authentication spec:
michael@0 153 // ext = "ext" "=" string-value
michael@0 154 // string-value = ( <"> plain-string <"> ) / plain-string
michael@0 155 // plain-string = 1*( %x20-21 / %x23-5B / %x5D-7E )
michael@0 156 // We add quotes around the extra string, so input extra must be a plain-string.
michael@0 157 // We break the spec by allowing ext to be an empty string, i.e. to match 0*(...).
michael@0 158 if (extra == null) {
michael@0 159 throw new IllegalArgumentException("extra must not be null.");
michael@0 160 }
michael@0 161 if (extra.length() > 0 && !isPlainString(extra)) {
michael@0 162 throw new IllegalArgumentException("extra must be a plain-string.");
michael@0 163 }
michael@0 164
michael@0 165 String requestString = getRequestString(request, timestamp, nonce, extra);
michael@0 166 String macString = getSignature(requestString, this.key);
michael@0 167
michael@0 168 String h = "MAC id=\"" + this.identifier + "\", " +
michael@0 169 "ts=\"" + timestamp + "\", " +
michael@0 170 "nonce=\"" + nonce + "\", " +
michael@0 171 "mac=\"" + macString + "\"";
michael@0 172
michael@0 173 if (extra != null) {
michael@0 174 h += ", ext=\"" + extra + "\"";
michael@0 175 }
michael@0 176
michael@0 177 Header header = new BasicHeader("Authorization", h);
michael@0 178
michael@0 179 return header;
michael@0 180 }
michael@0 181
michael@0 182 protected static byte[] sha1(byte[] message, byte[] key)
michael@0 183 throws NoSuchAlgorithmException, InvalidKeyException {
michael@0 184
michael@0 185 SecretKeySpec keySpec = new SecretKeySpec(key, HMAC_SHA1_ALGORITHM);
michael@0 186
michael@0 187 Mac hasher = Mac.getInstance(HMAC_SHA1_ALGORITHM);
michael@0 188 hasher.init(keySpec);
michael@0 189 hasher.update(message);
michael@0 190
michael@0 191 byte[] hmac = hasher.doFinal();
michael@0 192
michael@0 193 return hmac;
michael@0 194 }
michael@0 195
michael@0 196 /**
michael@0 197 * Sign an HMAC request string.
michael@0 198 *
michael@0 199 * @param requestString to sign.
michael@0 200 * @param key as <code>String</code>.
michael@0 201 * @return signature as base-64 encoded string.
michael@0 202 * @throws InvalidKeyException
michael@0 203 * @throws NoSuchAlgorithmException
michael@0 204 * @throws UnsupportedEncodingException
michael@0 205 */
michael@0 206 protected static String getSignature(String requestString, String key)
michael@0 207 throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
michael@0 208 String macString = Base64.encodeBase64String(sha1(requestString.getBytes("UTF-8"), key.getBytes("UTF-8")));
michael@0 209
michael@0 210 return macString;
michael@0 211 }
michael@0 212
michael@0 213 /**
michael@0 214 * Generate an HMAC request string.
michael@0 215 * <p>
michael@0 216 * This method trusts its inputs to be valid as per the MAC Authentication spec.
michael@0 217 *
michael@0 218 * @param request HTTP request.
michael@0 219 * @param timestamp to use.
michael@0 220 * @param nonce to use.
michael@0 221 * @param extra to use.
michael@0 222 * @return request string.
michael@0 223 */
michael@0 224 protected static String getRequestString(HttpUriRequest request, long timestamp, String nonce, String extra) {
michael@0 225 String method = request.getMethod().toUpperCase();
michael@0 226
michael@0 227 URI uri = request.getURI();
michael@0 228 String host = uri.getHost();
michael@0 229
michael@0 230 String path = uri.getRawPath();
michael@0 231 if (uri.getRawQuery() != null) {
michael@0 232 path += "?";
michael@0 233 path += uri.getRawQuery();
michael@0 234 }
michael@0 235 if (uri.getRawFragment() != null) {
michael@0 236 path += "#";
michael@0 237 path += uri.getRawFragment();
michael@0 238 }
michael@0 239
michael@0 240 int port = uri.getPort();
michael@0 241 String scheme = uri.getScheme();
michael@0 242 if (port != -1) {
michael@0 243 } else if ("http".equalsIgnoreCase(scheme)) {
michael@0 244 port = 80;
michael@0 245 } else if ("https".equalsIgnoreCase(scheme)) {
michael@0 246 port = 443;
michael@0 247 } else {
michael@0 248 throw new IllegalArgumentException("Unsupported URI scheme: " + scheme + ".");
michael@0 249 }
michael@0 250
michael@0 251 String requestString = timestamp + "\n" +
michael@0 252 nonce + "\n" +
michael@0 253 method + "\n" +
michael@0 254 path + "\n" +
michael@0 255 host + "\n" +
michael@0 256 port + "\n" +
michael@0 257 extra + "\n";
michael@0 258
michael@0 259 return requestString;
michael@0 260 }
michael@0 261 }

mercurial