Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: components/cronet/android/api/src/org/chromium/net/CronetEngine.java

Issue 1407263010: [Cronet] Public key pinning for Java API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Shutdown CronetEngine between HPKP tests Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.support.annotation.IntDef; 8 import android.support.annotation.IntDef;
9 import android.util.Base64;
9 import android.util.Log; 10 import android.util.Log;
10 11
11 import org.json.JSONArray; 12 import org.json.JSONArray;
12 import org.json.JSONException; 13 import org.json.JSONException;
13 import org.json.JSONObject; 14 import org.json.JSONObject;
14 15
15 import java.io.File; 16 import java.io.File;
16 import java.lang.annotation.Retention; 17 import java.lang.annotation.Retention;
17 import java.lang.annotation.RetentionPolicy; 18 import java.lang.annotation.RetentionPolicy;
18 import java.lang.reflect.Constructor; 19 import java.lang.reflect.Constructor;
19 import java.net.Proxy; 20 import java.net.Proxy;
20 import java.net.URL; 21 import java.net.URL;
21 import java.net.URLConnection; 22 import java.net.URLConnection;
22 import java.net.URLStreamHandlerFactory; 23 import java.net.URLStreamHandlerFactory;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.HashSet;
23 import java.util.List; 27 import java.util.List;
24 import java.util.Map; 28 import java.util.Map;
29 import java.util.Set;
25 import java.util.concurrent.Executor; 30 import java.util.concurrent.Executor;
26 31
27 /** 32 /**
28 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack 33 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack
29 * available on the current platform. 34 * available on the current platform.
30 */ 35 */
31 public abstract class CronetEngine { 36 public abstract class CronetEngine {
32 /** 37 /**
33 * A builder for {@link CronetEngine}s, which allows runtime configuration o f 38 * A builder for {@link CronetEngine}s, which allows runtime configuration o f
34 * {@code CronetEngine}. Configuration options are set on the builder and 39 * {@code CronetEngine}. Configuration options are set on the builder and
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); 301 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port);
297 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt); 302 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt);
298 quicHints.put(hint); 303 quicHints.put(hint);
299 } catch (JSONException e) { 304 } catch (JSONException e) {
300 // Intentionally do nothing. 305 // Intentionally do nothing.
301 } 306 }
302 return this; 307 return this;
303 } 308 }
304 309
305 /** 310 /**
311 * Adds public key pins for a given host.
312 *
313 * @param hostName name of the host to which public keys should be pinne d.
314 * @param pinsSha256 a collection of pins. Each pin is the SHA-256 crypt ographic
315 * hash of DER-encoded ASN.1 representation of Subject Public
316 * Key Info (SPKI) of the host X.509 certificate. Use
317 * {@link java.security.cert.Certificate#getPublicKey( )
318 * Certificate.getPublicKey} and
319 * {@link java.security.Key#getEncoded() Key.getEncode d}
320 * to obtain DER-encoded ASN.1 representation of SPKI.
321 * @param includeSubdomains indicates whether the pinning policy should be applied to
322 * subdomains of {@code hostName}.
323 * @param expirationDate specifies the expiration date for the pins.
324 * @return the builder to facilitate chaining.
325 * @throws NullPointerException if one of the input parameters is null.
326 * @throws IllegalArgumentException if the given host name is invalid or the
327 * {@code pinsSha256} collection contai ns a byte array
328 * that does not represent a valid SHA- 256 hash.
329 */
330 public Builder addPublicKeyPins(String hostName, Collection<byte[]> pins Sha256,
331 boolean includeSubdomains, Date expirationDate) {
332 if (hostName == null) {
333 throw new NullPointerException("The hostname cannot be null");
334 }
335 if (!isValidHostNameForPinning(hostName)) {
336 throw new IllegalArgumentException("Invalid host name: " + hostN ame);
337 }
338 if (pinsSha256 == null) {
339 throw new NullPointerException("The collection of SHA256 pins ca nnot be null");
340 }
341 try {
342 // Add HPKP_LIST JSON array element if it is not present.
343 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST);
estark 2015/11/18 23:10:06 totally optional nit: it's a little confusing to u
kapishnikov 2015/11/19 00:06:56 Done. Renamed HPKP to PKP everywhere in the new co
344 if (hpkpList == null) {
345 hpkpList = new JSONArray();
346 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList);
347 }
348
349 // Convert the pin to BASE64 encoding.
350 Set<String> hashes = new HashSet<>(pinsSha256.size());
351 for (byte[] pinSha256 : pinsSha256) {
352 hashes.add(convertSha256ToBase64WithPrefix(pinSha256));
353 }
354
355 // Add new element to HPKP_LIST JSON array.
356 JSONObject hpkp = new JSONObject();
357 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName);
358 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes));
359 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains);
360 // The expiration time is passed as a double, in seconds since J anuary 1, 1970.
361 hpkp.put(CronetEngineBuilderList.HPKP_EXPIRATION_DATE,
362 (double) expirationDate.getTime() / 1000);
363 hpkpList.put(hpkp);
364 } catch (JSONException e) {
365 // This exception should never happen.
366 throw new RuntimeException(
367 "Failed to add pubic key pins with the given arguments", e);
368 }
369 return this;
370 }
371
372 /**
373 * Converts a given SHA256 array of bytes to BASE64 encoded string and p repends
374 * {@code sha256/} prefix to it. The format corresponds to the format th at is expected by
375 * {@code net::HashValue} class.
376 *
377 * @param sha256 SHA256 bytes to convert to BASE64.
378 * @return the BASE64 encoded SHA256 with the prefix.
379 * @throws IllegalArgumentException if the provided pin is invalid.
380 */
381 private static String convertSha256ToBase64WithPrefix(byte[] sha256) {
382 if (sha256 == null || sha256.length != 32) {
383 throw new IllegalArgumentException("Public key pin is invalid");
384 }
385 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP);
386 }
387
388 /**
389 * Checks whether the given string that represents a host name is valid for HPKP.
390 * A valid host name should not match IPv4 address.
391 *
392 * @see <a href="https://tools.ietf.org/html/rfc7469#section-2.3.3>RFC 7 469</a>
393 * @param hostName host name to check.
394 * @return true if the string is a valid host name.
395 */
396 private static boolean isValidHostNameForPinning(String hostName) {
397 return CronetUtil.isValidHostName(hostName) && !CronetUtil.isValidIP v4(hostName);
398 }
399
400 /**
306 * Sets experimental QUIC connection options, overwriting any pre-existi ng 401 * Sets experimental QUIC connection options, overwriting any pre-existi ng
307 * options. List of options is subject to change. 402 * options. List of options is subject to change.
308 * 403 *
309 * @param quicConnectionOptions comma-separated QUIC options (for exampl e 404 * @param quicConnectionOptions comma-separated QUIC options (for exampl e
310 * "PACE,IW10") to use if QUIC is enabled. 405 * "PACE,IW10") to use if QUIC is enabled.
311 * @return the builder to facilitate chaining. 406 * @return the builder to facilitate chaining.
312 */ 407 */
313 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { 408 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) {
314 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); 409 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions);
315 } 410 }
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 cronetEngine = possibleEngine; 783 cronetEngine = possibleEngine;
689 } 784 }
690 } catch (ClassNotFoundException e) { 785 } catch (ClassNotFoundException e) {
691 // Leave as null. 786 // Leave as null.
692 } catch (Exception e) { 787 } catch (Exception e) {
693 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); 788 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e);
694 } 789 }
695 return cronetEngine; 790 return cronetEngine;
696 } 791 }
697 } 792 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698