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

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: Addressed review comments 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.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
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, which public keys should be pinned.
pauljensen 2015/11/03 19:59:30 either change "which" to "to which" or change "pin
kapishnikov 2015/11/03 23:58:16 Done.
311 * @param pinsSha256 a collection of SHA-256 pins.
pauljensen 2015/11/03 19:59:30 I'm not well versed in key pinning, but can we ela
kapishnikov 2015/11/03 23:58:16 Done.
kapishnikov 2015/11/03 23:58:16 Done.
312 * @param includeSubdomains indicates whether the pinning policy should be applied to
313 * subdomains of the host's domain name.
pauljensen 2015/11/03 19:59:30 "the host's domain name"->"{@code hostName}"
kapishnikov 2015/11/03 23:58:16 Done.
314 * @return the builder to facilitate chaining.
315 * @throws NullPointerException if one of the input parameters is null.
316 * @throws IllegalArgumentException if the provided collection of pins i s invalid.
pauljensen 2015/11/03 19:59:30 can you elaborate on what "invalid" means. You me
kapishnikov 2015/11/03 23:58:16 Done.
317 */
318 public Builder addPublicKeyPins(
319 String hostName, Collection<byte[]> pinsSha256, boolean includeS ubdomains) {
320 // Validate the input
321 if (hostName == null) {
322 throw new NullPointerException("The hostname cannot be null");
323 }
324 if (pinsSha256 == null) {
325 throw new NullPointerException("The collection of SHA256 pins ca nnot be null");
326 }
327 try {
328 // Add HPKP_LIST json array element if it is not present
329 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST);
330 if (hpkpList == null) {
331 hpkpList = new JSONArray();
332 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList);
333 }
334
335 // Convert the pin to BASE64 encoding.
336 Set<String> hashes = new HashSet<>(pinsSha256.size());
337 for (byte[] pinSha256 : pinsSha256) {
338 hashes.add(convertSha256ToBase64WithPrefix(pinSha256));
339 }
340
341 // Add new element to HPKP_LIST json array
342 JSONObject hpkp = new JSONObject();
343 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName);
344 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes));
345 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains);
346 hpkpList.put(hpkp);
347 } catch (JSONException e) {
348 Log.e(TAG, "Unable to add public key pins", e);
349 }
350 return this;
351 }
352
353 /**
354 * Converts a given SHA256 array of bytes to BASE64 encoding with the pr efix. The format
355 * corresponds to the format that is expected by net::HashValue class.
356 *
357 * @param sha256 SHA256 bytes to convert to BASE64.
358 * @return the BASE64 conversion.
359 * @throws IllegalArgumentException if the provided pin is invalid.
360 */
361 private String convertSha256ToBase64WithPrefix(byte[] sha256) {
362 if (sha256 == null || sha256.length != 32) {
363 throw new IllegalArgumentException("The provided pin is invalid" );
364 }
365 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP);
366 }
367
368 /**
304 * Sets experimental QUIC connection options, overwriting any pre-existi ng 369 * Sets experimental QUIC connection options, overwriting any pre-existi ng
305 * options. List of options is subject to change. 370 * options. List of options is subject to change.
306 * 371 *
307 * @param quicConnectionOptions comma-separated QUIC options (for exampl e 372 * @param quicConnectionOptions comma-separated QUIC options (for exampl e
308 * "PACE,IW10") to use if QUIC is enabled. 373 * "PACE,IW10") to use if QUIC is enabled.
309 * @return the builder to facilitate chaining. 374 * @return the builder to facilitate chaining.
310 */ 375 */
311 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { 376 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) {
312 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); 377 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions);
313 } 378 }
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 cronetEngine = possibleEngine; 727 cronetEngine = possibleEngine;
663 } 728 }
664 } catch (ClassNotFoundException e) { 729 } catch (ClassNotFoundException e) {
665 // Leave as null. 730 // Leave as null.
666 } catch (Exception e) { 731 } catch (Exception e) {
667 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); 732 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e);
668 } 733 }
669 return cronetEngine; 734 return cronetEngine;
670 } 735 }
671 } 736 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698