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 */ | |
329 public Builder addPublicKeyPins(String hostName, Collection<byte[]> pins Sha256, | |
330 boolean includeSubdomains, Date expirationDate) { | |
331 if (hostName == null) { | |
332 throw new NullPointerException("The hostname cannot be null"); | |
333 } | |
334 if (!isValidHostNameForPinning(hostName)) { | |
335 throw new IllegalArgumentException("Invalid host name: " + hostN ame); | |
336 } | |
337 if (pinsSha256 == null) { | |
338 throw new NullPointerException("The collection of SHA256 pins ca nnot be null"); | |
339 } | |
340 try { | |
341 // Add HPKP_LIST JSON array element if it is not present. | |
342 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST); | |
343 if (hpkpList == null) { | |
344 hpkpList = new JSONArray(); | |
345 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList); | |
346 } | |
347 | |
348 // Convert the pin to BASE64 encoding. | |
349 Set<String> hashes = new HashSet<>(pinsSha256.size()); | |
350 for (byte[] pinSha256 : pinsSha256) { | |
351 hashes.add(convertSha256ToBase64WithPrefix(pinSha256)); | |
352 } | |
353 | |
354 // Add new element to HPKP_LIST JSON array. | |
355 JSONObject hpkp = new JSONObject(); | |
356 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName); | |
357 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes)); | |
358 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains); | |
359 // The expiration time is passed as a double, in seconds since J anuary 1, 1970. | |
360 hpkp.put(CronetEngineBuilderList.HPKP_EXPIRATION_DATE, | |
361 (double) expirationDate.getTime() / 1000); | |
362 hpkpList.put(hpkp); | |
363 } catch (JSONException e) { | |
364 // This exception should never happen. | |
365 throw new RuntimeException( | |
366 "Failed to add pubic key pins with the given arguments", e); | |
367 } | |
368 return this; | |
369 } | |
370 | |
371 /** | |
372 * Converts a given SHA256 array of bytes to BASE64 encoding with the pr efix. The format | |
373 * corresponds to the format that is expected by net::HashValue class. | |
374 * | |
375 * @param sha256 SHA256 bytes to convert to BASE64. | |
376 * @return the BASE64 conversion. | |
377 * @throws IllegalArgumentException if the provided pin is invalid. | |
378 */ | |
379 private static String convertSha256ToBase64WithPrefix(byte[] sha256) { | |
mef
2015/11/09 16:59:28
I wonder whether private methods should go after p
kapishnikov
2015/11/09 19:14:20
I could not find anything regarding the method ord
| |
380 if (sha256 == null || sha256.length != 32) { | |
381 throw new IllegalArgumentException("Public key pin is invalid"); | |
382 } | |
383 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP); | |
384 } | |
385 | |
386 /** | |
387 * Checks whether the given string that represents a host name is valid for HPKP. | |
388 * A valid host name should not match IPv4 address. | |
389 * | |
390 * @see <a href="https://tools.ietf.org/html/rfc7469#section-2.3.3>RFC 7 469</a> | |
391 * @param hostName host name to check. | |
392 * @return true if the string is a valid host name. | |
393 */ | |
394 private static boolean isValidHostNameForPinning(String hostName) { | |
395 return CronetUtil.isValidHostName(hostName) && !CronetUtil.isValidIP v4(hostName); | |
396 } | |
397 | |
398 /** | |
306 * Sets experimental QUIC connection options, overwriting any pre-existi ng | 399 * Sets experimental QUIC connection options, overwriting any pre-existi ng |
307 * options. List of options is subject to change. | 400 * options. List of options is subject to change. |
308 * | 401 * |
309 * @param quicConnectionOptions comma-separated QUIC options (for exampl e | 402 * @param quicConnectionOptions comma-separated QUIC options (for exampl e |
310 * "PACE,IW10") to use if QUIC is enabled. | 403 * "PACE,IW10") to use if QUIC is enabled. |
311 * @return the builder to facilitate chaining. | 404 * @return the builder to facilitate chaining. |
312 */ | 405 */ |
313 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { | 406 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { |
314 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); | 407 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); |
315 } | 408 } |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
688 cronetEngine = possibleEngine; | 781 cronetEngine = possibleEngine; |
689 } | 782 } |
690 } catch (ClassNotFoundException e) { | 783 } catch (ClassNotFoundException e) { |
691 // Leave as null. | 784 // Leave as null. |
692 } catch (Exception e) { | 785 } catch (Exception e) { |
693 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); | 786 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); |
694 } | 787 } |
695 return cronetEngine; | 788 return cronetEngine; |
696 } | 789 } |
697 } | 790 } |
OLD | NEW |