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

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: Hostname validation using IDN.USE_STD3_ASCII_RULES and conflict resolution 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.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
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 * {@link java.security.cert.Certificate#getPublicKey( )
318 * Certificate.getPublicKey} and
319 * {@link java.security.Key#getEncoded() Key.getEncode d}
320 * to obtain DER-encoded ASN.1 representation of SPKI.
321 * @param includeSubdomains indicates whether the pinning policy should be applied to
322 * subdomains of {@code hostName}.
323 * @param expirationDate specifies the expiration date for the pins.
324 * @return the builder to facilitate chaining.
325 * @throws NullPointerException if one of the input parameters is null.
326 * @throws IllegalArgumentException if the given host name is invalid or the
327 * {@code pinsSha256} collection contai ns a byte array
328 * that does not represent a valid SHA- 256 hash.
329 */
330 public Builder addPublicKeyPins(String hostName, Collection<byte[]> pins Sha256,
331 boolean includeSubdomains, Date expirationDate) {
332 if (hostName == null) {
333 throw new NullPointerException("The hostname cannot be null");
334 }
335 if (!isValidHostNameForPinning(hostName)) {
336 throw new IllegalArgumentException("Invalid host name: " + hostN ame);
337 }
338 if (pinsSha256 == null) {
339 throw new NullPointerException("The collection of SHA256 pins ca nnot be null");
340 }
341 try {
342 // Add PKP_LIST JSON array element if it is not present.
343 JSONArray pkpList = mConfig.optJSONArray(CronetEngineBuilderList .PKP_LIST);
344 if (pkpList == null) {
345 pkpList = new JSONArray();
346 mConfig.put(CronetEngineBuilderList.PKP_LIST, pkpList);
347 }
348
349 // Convert the pin to BASE64 encoding.
350 Set<String> hashes = new HashSet<>(pinsSha256.size());
351 for (byte[] pinSha256 : pinsSha256) {
352 hashes.add(convertSha256ToBase64WithPrefix(pinSha256));
353 }
354
355 // Add new element to PKP_LIST JSON array.
356 JSONObject pkp = new JSONObject();
357 pkp.put(CronetEngineBuilderList.PKP_HOST, hostName);
358 pkp.put(CronetEngineBuilderList.PKP_PIN_HASHES, new JSONArray(ha shes));
359 pkp.put(CronetEngineBuilderList.PKP_INCLUDE_SUBDOMAINS, includeS ubdomains);
360 // The expiration time is passed as a double, in seconds since J anuary 1, 1970.
361 pkp.put(CronetEngineBuilderList.PKP_EXPIRATION_DATE,
362 (double) expirationDate.getTime() / 1000);
363 pkpList.put(pkp);
364 } catch (JSONException e) {
365 // This exception should never happen.
366 throw new RuntimeException(
367 "Failed to add pubic key pins with the given arguments", e);
368 }
369 return this;
370 }
371
372 /**
373 * Converts a given SHA256 array of bytes to BASE64 encoded string and p repends
374 * {@code sha256/} prefix to it. The format corresponds to the format th at is expected by
375 * {@code net::HashValue} class.
376 *
377 * @param sha256 SHA256 bytes to convert to BASE64.
378 * @return the BASE64 encoded SHA256 with the prefix.
379 * @throws IllegalArgumentException if the provided pin is invalid.
380 */
381 private static String convertSha256ToBase64WithPrefix(byte[] sha256) {
382 if (sha256 == null || sha256.length != 32) {
383 throw new IllegalArgumentException("Public key pin is invalid");
384 }
385 return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP);
386 }
387
388 /**
389 * Checks whether the given string that represents a host name is valid for PKP.
390 * A valid host name should not match IPv4 address.
391 *
392 * @see <a href="https://tools.ietf.org/html/rfc7469#section-2.3.3>RFC 7 469</a>
393 * @param hostName host name to check.
394 * @return true if the string is a valid host name.
395 */
396 private static boolean isValidHostNameForPinning(String hostName) {
397 return CronetUtil.isValidHostName(hostName) && !CronetUtil.isValidIP v4(hostName);
nharper 2015/11/19 23:47:54 This doesn't explicitly check that hostName isn't
kapishnikov 2015/11/20 16:38:03 Yes, any IPv6 is not a valid host name and will be
398 }
399
400 /**
306 * Sets experimental options to be used in Cronet. 401 * Sets experimental options to be used in Cronet.
307 * 402 *
308 * @param options JSON formatted experimental options. 403 * @param options JSON formatted experimental options.
309 * @return the builder to facilitate chaining. 404 * @return the builder to facilitate chaining.
310 */ 405 */
311 public Builder setExperimentalOptions(String options) { 406 public Builder setExperimentalOptions(String options) {
312 return putString(CronetEngineBuilderList.EXPERIMENTAL_OPTIONS, optio ns); 407 return putString(CronetEngineBuilderList.EXPERIMENTAL_OPTIONS, optio ns);
313 } 408 }
314 409
315 /** 410 /**
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 cronetEngine = possibleEngine; 781 cronetEngine = possibleEngine;
687 } 782 }
688 } catch (ClassNotFoundException e) { 783 } catch (ClassNotFoundException e) {
689 // Leave as null. 784 // Leave as null.
690 } catch (Exception e) { 785 } catch (Exception e) {
691 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); 786 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e);
692 } 787 }
693 return cronetEngine; 788 return cronetEngine;
694 } 789 }
695 } 790 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698