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

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: Refactored X509UtilTest to avoid code duplication 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 to which public keys should be pinne d.
311 * @param pinsSha256 a collection of pins. Each pin is the SHA-256 crypt ographic
312 * hash of DER-encoded ASN.1 representation of Subject Public
313 * Key Info (SPKI) of the host X.509 certificate. You can use
314 * {@code security.cert.X509Certificate.getPublicKey() .getEncoded()}
315 * to obtain DER-encoded ASN.1 representation of SPKI.
316 * @param includeSubdomains indicates whether the pinning policy should be applied to
317 * subdomains of {@code hostName}.
318 * @return the builder to facilitate chaining.
319 * @throws NullPointerException if one of the input parameters is null.
320 * @throws IllegalArgumentException if {@code pinsSha256} collection con tains a byte array
321 * that does not represent a valid SHA- 256 hash.
322 *
323 */
324 public Builder addPublicKeyPins(
325 String hostName, Collection<byte[]> pinsSha256, boolean includeS ubdomains) {
326 // Validate the input
327 if (hostName == null) {
328 throw new NullPointerException("The hostname cannot be null");
329 }
330 if (pinsSha256 == null) {
331 throw new NullPointerException("The collection of SHA256 pins ca nnot be null");
332 }
333 try {
334 // Add HPKP_LIST json array element if it is not present
xunjieli 2015/11/06 15:47:54 nit: s/json/JSON. And add a period at the end.
kapishnikov 2015/11/06 16:18:13 I looked at Java style guide and could not find ex
335 JSONArray hpkpList = mConfig.optJSONArray(CronetEngineBuilderLis t.HPKP_LIST);
336 if (hpkpList == null) {
337 hpkpList = new JSONArray();
338 mConfig.put(CronetEngineBuilderList.HPKP_LIST, hpkpList);
339 }
340
341 // Convert the pin to BASE64 encoding.
342 Set<String> hashes = new HashSet<>(pinsSha256.size());
343 for (byte[] pinSha256 : pinsSha256) {
344 hashes.add(convertSha256ToBase64WithPrefix(pinSha256));
345 }
346
347 // Add new element to HPKP_LIST json array
xunjieli 2015/11/06 15:47:54 nit: s/json/JSON. And add a period at the end. s/n
348 JSONObject hpkp = new JSONObject();
349 hpkp.put(CronetEngineBuilderList.HPKP_HOST, hostName);
350 hpkp.put(CronetEngineBuilderList.HPKP_PIN_HASHES, new JSONArray( hashes));
351 hpkp.put(CronetEngineBuilderList.HPKP_INCLUDE_SUBDOMAINS, includ eSubdomains);
352 hpkpList.put(hpkp);
353 } catch (JSONException e) {
354 Log.e(TAG, "Unable to add public key pins", e);
355 }
356 return this;
357 }
358
359 /**
360 * Converts a given SHA256 array of bytes to BASE64 encoding with the pr efix. The format
361 * corresponds to the format that is expected by net::HashValue class.
362 *
363 * @param sha256 SHA256 bytes to convert to BASE64.
364 * @return the BASE64 conversion.
365 * @throws IllegalArgumentException if the provided pin is invalid.
366 */
367 private String convertSha256ToBase64WithPrefix(byte[] sha256) {
368 if (sha256 == null || sha256.length != 32) {
369 throw new IllegalArgumentException("The provided pin is invalid" );
370 }
371 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP);
372 }
373
374 /**
304 * Sets experimental QUIC connection options, overwriting any pre-existi ng 375 * Sets experimental QUIC connection options, overwriting any pre-existi ng
305 * options. List of options is subject to change. 376 * options. List of options is subject to change.
306 * 377 *
307 * @param quicConnectionOptions comma-separated QUIC options (for exampl e 378 * @param quicConnectionOptions comma-separated QUIC options (for exampl e
308 * "PACE,IW10") to use if QUIC is enabled. 379 * "PACE,IW10") to use if QUIC is enabled.
309 * @return the builder to facilitate chaining. 380 * @return the builder to facilitate chaining.
310 */ 381 */
311 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) { 382 public Builder setExperimentalQuicConnectionOptions(String quicConnectio nOptions) {
312 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions); 383 return putString(CronetEngineBuilderList.QUIC_OPTIONS, quicConnectio nOptions);
313 } 384 }
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 cronetEngine = possibleEngine; 733 cronetEngine = possibleEngine;
663 } 734 }
664 } catch (ClassNotFoundException e) { 735 } catch (ClassNotFoundException e) {
665 // Leave as null. 736 // Leave as null.
666 } catch (Exception e) { 737 } catch (Exception e) {
667 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); 738 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e);
668 } 739 }
669 return cronetEngine; 740 return cronetEngine;
670 } 741 }
671 } 742 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698