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.HashSet; | |
| 23 import java.util.List; | 26 import java.util.List; |
| 24 import java.util.Map; | 27 import java.util.Map; |
| 28 import java.util.Set; | |
| 25 import java.util.concurrent.Executor; | 29 import java.util.concurrent.Executor; |
| 26 | 30 |
| 27 /** | 31 /** |
| 28 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack | 32 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack |
| 29 * available on the current platform. | 33 * available on the current platform. |
| 30 */ | 34 */ |
| 31 public abstract class CronetEngine { | 35 public abstract class CronetEngine { |
| 32 /** | 36 /** |
| 33 * A builder for {@link CronetEngine}s, which allows runtime configuration o f | 37 * A builder for {@link CronetEngine}s, which allows runtime configuration o f |
| 34 * {@code CronetEngine}. Configuration options are set on the builder and | 38 * {@code CronetEngine}. Configuration options are set on the builder and |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); | 298 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); |
| 295 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt); | 299 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt); |
| 296 quicHints.put(hint); | 300 quicHints.put(hint); |
| 297 } catch (JSONException e) { | 301 } catch (JSONException e) { |
| 298 // Intentionally do nothing. | 302 // Intentionally do nothing. |
| 299 } | 303 } |
| 300 return this; | 304 return this; |
| 301 } | 305 } |
| 302 | 306 |
| 303 /** | 307 /** |
| 308 * Adds public key pinning for a given host. | |
| 309 * | |
| 310 * @param hostName name of the host that should be pinned. | |
|
mef
2015/11/02 17:56:56
that -> which public keys
kapishnikov
2015/11/02 22:45:48
Done.
| |
| 311 * @param pinsSha256 a collection of SHA-256 pins. | |
| 312 * @param includeSubdomains indicates whether the pinning policy should be applied to | |
| 313 * subdomains of the host's domain name. | |
| 314 * @return the builder to facilitate chaining. | |
| 315 */ | |
| 316 public Builder addPublicKeyPins( | |
| 317 String hostName, Collection<byte[]> pinsSha256, boolean includeS ubdomains) { | |
| 318 // Validate the input | |
| 319 if (hostName == null) { | |
| 320 throw new NullPointerException("The hostname cannot be null"); | |
| 321 } | |
| 322 if (pinsSha256 == null) { | |
| 323 throw new NullPointerException("The collection of SHA256 pins ca nnot be null"); | |
| 324 } | |
| 325 try { | |
| 326 // Add HPKP_LIST json array element if it is not present | |
| 327 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST); | |
| 328 if (hpkpList == null) { | |
| 329 hpkpList = new JSONArray(); | |
| 330 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList); | |
| 331 } | |
| 332 | |
| 333 // Convert the pint to BASE64 encoding. | |
|
mef
2015/11/02 17:56:56
the pint?
kapishnikov
2015/11/02 22:45:49
Done.
| |
| 334 Set<String> hashes = new HashSet<>(pinsSha256.size()); | |
| 335 for (byte[] pinSha256 : pinsSha256) { | |
| 336 hashes.add(convertSha256ToBase64WithPrefix(pinSha256)); | |
| 337 } | |
| 338 | |
| 339 // Add new element to HPKP_LIST json array | |
| 340 JSONObject hpkp = new JSONObject(); | |
| 341 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName); | |
| 342 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes)); | |
| 343 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains); | |
| 344 hpkpList.put(hpkp); | |
| 345 } catch (JSONException e) { | |
| 346 Log.e(TAG, "Unable to add public key pins", e); | |
| 347 } | |
| 348 return this; | |
| 349 } | |
| 350 | |
| 351 /** | |
| 352 * Converts a given SHA256 array of bytes to BASE64 encoding with the pr efix. The format | |
| 353 * corresponds to the format that is expected by net::HashValue class. | |
| 354 * | |
| 355 * @param sha256 SHA256 bytes to convert to BASE64. | |
| 356 * @return the BASE64 conversion, | |
| 357 */ | |
| 358 private String convertSha256ToBase64WithPrefix(byte[] sha256) { | |
| 359 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP); | |
|
mef
2015/11/02 17:56:56
add validation that sha256 is 32 bytes long?
kapishnikov
2015/11/02 22:45:49
Done.
| |
| 360 } | |
| 361 | |
| 362 /** | |
| 304 * Sets experimental QUIC connection options, overwriting any pre-existi ng | 363 * Sets experimental QUIC connection options, overwriting any pre-existi ng |
| 305 * options. List of options is subject to change. | 364 * options. List of options is subject to change. |
| 306 * | 365 * |
| 307 * @param quicConnectionOptions comma-separated QUIC options (for exampl e | 366 * @param quicConnectionOptions comma-separated QUIC options (for exampl e |
| 308 * "PACE,IW10") to use if QUIC is enabled. | 367 * "PACE,IW10") to use if QUIC is enabled. |
| 309 * @return the builder to facilitate chaining. | 368 * @return the builder to facilitate chaining. |
| 310 */ | 369 */ |
| 311 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { | 370 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { |
| 312 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); | 371 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); |
| 313 } | 372 } |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 661 cronetEngine = possibleEngine; | 720 cronetEngine = possibleEngine; |
| 662 } | 721 } |
| 663 } catch (ClassNotFoundException e) { | 722 } catch (ClassNotFoundException e) { |
| 664 // Leave as null. | 723 // Leave as null. |
| 665 } catch (Exception e) { | 724 } catch (Exception e) { |
| 666 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); | 725 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); |
| 667 } | 726 } |
| 668 return cronetEngine; | 727 return cronetEngine; |
| 669 } | 728 } |
| 670 } | 729 } |
| OLD | NEW |