Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 * {@code security.cert.X509Certificate.getPublicKey() .getEncoded()} | |
| 318 * to obtain DER-encoded ASN.1 representation of SPKI. | |
| 319 * @param includeSubdomains indicates whether the pinning policy should be applied to | |
| 320 * subdomains of {@code hostName}. | |
| 321 * @param expirationDate specifies the expiration date for the pins. | |
| 322 * @return the builder to facilitate chaining. | |
| 323 * @throws NullPointerException if one of the input parameters is null. | |
| 324 * @throws IllegalArgumentException if the given host name is invalid or {@code pinsSha256} | |
| 325 * collection contains a byte array tha t does not represent | |
| 326 * a valid SHA-256 hash. | |
| 327 */ | |
| 328 public Builder addPublicKeyPins(String hostName, Collection<byte[]> pins Sha256, | |
| 329 boolean includeSubdomains, Date expirationDate) { | |
|
estark
2015/11/10 19:04:16
This doesn't seem to match the API discussed in ht
mef
2015/11/10 22:19:03
Good point. I forgot about that discussion. We've
kapishnikov
2015/11/10 22:33:33
It is a good point. Currently addPublicKeyPins() i
mef
2015/11/18 22:41:38
Emily, WDYT?
estark
2015/11/18 23:10:06
Sorry for dropping off on this. I guess this seems
| |
| 330 if (hostName == null) { | |
| 331 throw new NullPointerException("The hostname cannot be null"); | |
| 332 } | |
| 333 if (!isValidHostNameForPinning(hostName)) { | |
| 334 throw new IllegalArgumentException("Invalid host name: " + hostN ame); | |
| 335 } | |
| 336 if (pinsSha256 == null) { | |
| 337 throw new NullPointerException("The collection of SHA256 pins ca nnot be null"); | |
| 338 } | |
| 339 try { | |
| 340 // Add HPKP_LIST JSON array element if it is not present. | |
| 341 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST); | |
| 342 if (hpkpList == null) { | |
| 343 hpkpList = new JSONArray(); | |
| 344 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList); | |
| 345 } | |
| 346 | |
| 347 // Convert the pin to BASE64 encoding. | |
| 348 Set<String> hashes = new HashSet<>(pinsSha256.size()); | |
| 349 for (byte[] pinSha256 : pinsSha256) { | |
| 350 hashes.add(convertSha256ToBase64WithPrefix(pinSha256)); | |
| 351 } | |
| 352 | |
| 353 // Add new element to HPKP_LIST JSON array. | |
| 354 JSONObject hpkp = new JSONObject(); | |
| 355 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName); | |
| 356 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes)); | |
| 357 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains); | |
| 358 // The expiration time is passed as a double, in seconds since J anuary 1, 1970. | |
| 359 hpkp.put(CronetEngineBuilderList.HPKP_EXPIRATION_DATE, | |
| 360 (double) expirationDate.getTime() / 1000); | |
| 361 hpkpList.put(hpkp); | |
| 362 } catch (JSONException e) { | |
| 363 // This exception should never happen. | |
| 364 throw new RuntimeException( | |
| 365 "Failed to add pubic key pins with the given arguments", e); | |
| 366 } | |
| 367 return this; | |
| 368 } | |
| 369 | |
| 370 /** | |
| 371 * Converts a given SHA256 array of bytes to BASE64 encoding with the pr efix. The format | |
| 372 * corresponds to the format that is expected by net::HashValue class. | |
| 373 * | |
| 374 * @param sha256 SHA256 bytes to convert to BASE64. | |
| 375 * @return the BASE64 conversion. | |
| 376 * @throws IllegalArgumentException if the provided pin is invalid. | |
| 377 */ | |
| 378 private static String convertSha256ToBase64WithPrefix(byte[] sha256) { | |
| 379 if (sha256 == null || sha256.length != 32) { | |
| 380 throw new IllegalArgumentException("Public key pin is invalid"); | |
| 381 } | |
| 382 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP); | |
| 383 } | |
| 384 | |
| 385 /** | |
| 386 * Checks whether the given string that represents a host name is valid for HPKP. | |
| 387 * A valid host name should not match IPv4 address. | |
|
estark
2015/11/10 19:04:16
ipv6 shouldn't be valid either
mef
2015/11/10 22:19:03
I think ipv4 is checked explicitly as it includes
| |
| 388 * | |
| 389 * @see <a href="https://tools.ietf.org/html/rfc7469#section-2.3.3>RFC 7 469</a> | |
| 390 * @param hostName host name to check. | |
| 391 * @return true if the string is a valid host name. | |
| 392 */ | |
| 393 private static boolean isValidHostNameForPinning(String hostName) { | |
| 394 return CronetUtil.isValidHostName(hostName) && !CronetUtil.isValidIP v4(hostName); | |
| 395 } | |
| 396 | |
| 397 /** | |
| 306 * Sets experimental QUIC connection options, overwriting any pre-existi ng | 398 * Sets experimental QUIC connection options, overwriting any pre-existi ng |
| 307 * options. List of options is subject to change. | 399 * options. List of options is subject to change. |
| 308 * | 400 * |
| 309 * @param quicConnectionOptions comma-separated QUIC options (for exampl e | 401 * @param quicConnectionOptions comma-separated QUIC options (for exampl e |
| 310 * "PACE,IW10") to use if QUIC is enabled. | 402 * "PACE,IW10") to use if QUIC is enabled. |
| 311 * @return the builder to facilitate chaining. | 403 * @return the builder to facilitate chaining. |
| 312 */ | 404 */ |
| 313 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { | 405 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { |
| 314 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); | 406 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); |
| 315 } | 407 } |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 688 cronetEngine = possibleEngine; | 780 cronetEngine = possibleEngine; |
| 689 } | 781 } |
| 690 } catch (ClassNotFoundException e) { | 782 } catch (ClassNotFoundException e) { |
| 691 // Leave as null. | 783 // Leave as null. |
| 692 } catch (Exception e) { | 784 } catch (Exception e) { |
| 693 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); | 785 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); |
| 694 } | 786 } |
| 695 return cronetEngine; | 787 return cronetEngine; |
| 696 } | 788 } |
| 697 } | 789 } |
| OLD | NEW |