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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 * ({@link #HTTP_CACHE_DISK}) is needed to take advantage of 0-RTT | 278 * ({@link #HTTP_CACHE_DISK}) is needed to take advantage of 0-RTT |
274 * connection establishment between sessions. | 279 * connection establishment between sessions. |
275 * | 280 * |
276 * @param host hostname of the server that supports QUIC. | 281 * @param host hostname of the server that supports QUIC. |
277 * @param port host of the server that supports QUIC. | 282 * @param port host of the server that supports QUIC. |
278 * @param alternatePort alternate port to use for QUIC. | 283 * @param alternatePort alternate port to use for QUIC. |
279 * @return the builder to facilitate chaining. | 284 * @return the builder to facilitate chaining. |
280 */ | 285 */ |
281 public Builder addQuicHint(String host, int port, int alternatePort) { | 286 public Builder addQuicHint(String host, int port, int alternatePort) { |
282 if (host.contains("/")) { | 287 if (host.contains("/")) { |
283 throw new IllegalArgumentException("Illegal QUIC Hint Host: " + host); | 288 throw new IllegalArgumentException("Illegal QUIC Hint Host: " + host); |
mef
2015/11/06 18:02:16
In the past there were some misuses of API with ur
kapishnikov
2015/11/06 19:45:08
I will add the proper check.
| |
284 } | 289 } |
285 try { | 290 try { |
286 JSONArray quicHints = mConfig.optJSONArray(CronetEngineBuilderLi st.QUIC_HINTS); | 291 JSONArray quicHints = mConfig.optJSONArray(CronetEngineBuilderLi st.QUIC_HINTS); |
287 if (quicHints == null) { | 292 if (quicHints == null) { |
288 quicHints = new JSONArray(); | 293 quicHints = new JSONArray(); |
289 mConfig.put(CronetEngineBuilderList.QUIC_HINTS, quicHints); | 294 mConfig.put(CronetEngineBuilderList.QUIC_HINTS, quicHints); |
290 } | 295 } |
291 | 296 |
292 JSONObject hint = new JSONObject(); | 297 JSONObject hint = new JSONObject(); |
293 hint.put(CronetEngineBuilderList.QUIC_HINT_HOST, host); | 298 hint.put(CronetEngineBuilderList.QUIC_HINT_HOST, host); |
294 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); | 299 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); |
295 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt); | 300 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt); |
296 quicHints.put(hint); | 301 quicHints.put(hint); |
297 } catch (JSONException e) { | 302 } catch (JSONException e) { |
298 // Intentionally do nothing. | 303 // Intentionally do nothing. |
299 } | 304 } |
300 return this; | 305 return this; |
301 } | 306 } |
302 | 307 |
303 /** | 308 /** |
309 * Adds public key pinning for a given host. | |
310 * | |
311 * @param hostName name of the host to which public keys should be pinne d. | |
312 * @param pinsSha256 a collection of pins. Each pin is the SHA-256 crypt ographic | |
313 * hash of DER-encoded ASN.1 representation of Subject Public | |
mef
2015/11/06 18:02:17
pinning -> pins
kapishnikov
2015/11/06 19:45:08
Done.
| |
314 * Key Info (SPKI) of the host X.509 certificate. You can use | |
mef
2015/11/06 18:02:16
Usually Chrome frowns upon use of 'you', 'we', etc
kapishnikov
2015/11/06 19:45:08
Done.
| |
315 * {@code security.cert.X509Certificate.getPublicKey() .getEncoded()} | |
316 * to obtain DER-encoded ASN.1 representation of SPKI. | |
317 * @param includeSubdomains indicates whether the pinning policy should be applied to | |
318 * subdomains of {@code hostName}. | |
319 * @param expirationDate specifies the expiration date for the pins. | |
320 * @return the builder to facilitate chaining. | |
321 * @throws NullPointerException if one of the input parameters is null. | |
322 * @throws IllegalArgumentException if {@code pinsSha256} collection con tains a byte array | |
323 * that does not represent a valid SHA- 256 hash. | |
324 * | |
325 */ | |
326 public Builder addPublicKeyPins(String hostName, Collection<byte[]> pins Sha256, | |
327 boolean includeSubdomains, Date expirationDate) { | |
328 // Validate the input | |
mef
2015/11/06 18:02:16
comments should end in period, but reasonably we d
kapishnikov
2015/11/06 19:45:08
Done.
| |
329 if (hostName == null) { | |
330 throw new NullPointerException("The hostname cannot be null"); | |
331 } | |
332 if (pinsSha256 == null) { | |
333 throw new NullPointerException("The collection of SHA256 pins ca nnot be null"); | |
334 } | |
335 try { | |
336 // Add HPKP_LIST JSON array element if it is not present. | |
337 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST); | |
338 if (hpkpList == null) { | |
339 hpkpList = new JSONArray(); | |
340 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList); | |
341 } | |
342 | |
343 // Convert the pin to BASE64 encoding. | |
344 Set<String> hashes = new HashSet<>(pinsSha256.size()); | |
345 for (byte[] pinSha256 : pinsSha256) { | |
346 hashes.add(convertSha256ToBase64WithPrefix(pinSha256)); | |
347 } | |
348 | |
349 // Add new element to HPKP_LIST JSON array. | |
350 JSONObject hpkp = new JSONObject(); | |
351 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName); | |
352 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes)); | |
353 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains); | |
354 // The expiration time is passed as a double, in seconds since J anuary 1, 1970. | |
355 hpkp.put(CronetEngineBuilderList.HPKP_EXPIRATION_DATE, | |
356 (double) expirationDate.getTime() / 1000); | |
mef
2015/11/06 18:02:16
why double? Can it be long?
kapishnikov
2015/11/06 19:45:08
Our current C++ JsonValueConverter (https://code.g
| |
357 hpkpList.put(hpkp); | |
358 } catch (JSONException e) { | |
359 Log.e(TAG, "Unable to add public key pins", e); | |
mef
2015/11/06 18:02:16
hrm, why wouldn't we re-throw it? The builder is w
kapishnikov
2015/11/06 19:45:08
I agree that re-throwing an exception would be the
| |
360 } | |
361 return this; | |
362 } | |
363 | |
364 /** | |
365 * Converts a given SHA256 array of bytes to BASE64 encoding with the pr efix. The format | |
366 * corresponds to the format that is expected by net::HashValue class. | |
367 * | |
368 * @param sha256 SHA256 bytes to convert to BASE64. | |
369 * @return the BASE64 conversion. | |
370 * @throws IllegalArgumentException if the provided pin is invalid. | |
371 */ | |
372 private String convertSha256ToBase64WithPrefix(byte[] sha256) { | |
373 if (sha256 == null || sha256.length != 32) { | |
374 throw new IllegalArgumentException("The provided pin is invalid" ); | |
mef
2015/11/06 18:02:16
provided -> public key
kapishnikov
2015/11/06 19:45:08
Done.
| |
375 } | |
376 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP); | |
377 } | |
378 | |
379 /** | |
304 * Sets experimental QUIC connection options, overwriting any pre-existi ng | 380 * Sets experimental QUIC connection options, overwriting any pre-existi ng |
305 * options. List of options is subject to change. | 381 * options. List of options is subject to change. |
306 * | 382 * |
307 * @param quicConnectionOptions comma-separated QUIC options (for exampl e | 383 * @param quicConnectionOptions comma-separated QUIC options (for exampl e |
308 * "PACE,IW10") to use if QUIC is enabled. | 384 * "PACE,IW10") to use if QUIC is enabled. |
309 * @return the builder to facilitate chaining. | 385 * @return the builder to facilitate chaining. |
310 */ | 386 */ |
311 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { | 387 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { |
312 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); | 388 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); |
313 } | 389 } |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
662 cronetEngine = possibleEngine; | 738 cronetEngine = possibleEngine; |
663 } | 739 } |
664 } catch (ClassNotFoundException e) { | 740 } catch (ClassNotFoundException e) { |
665 // Leave as null. | 741 // Leave as null. |
666 } catch (Exception e) { | 742 } catch (Exception e) { |
667 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); | 743 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); |
668 } | 744 } |
669 return cronetEngine; | 745 return cronetEngine; |
670 } | 746 } |
671 } | 747 } |
OLD | NEW |