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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/api/src/org/chromium/net/CronetEngine.java
diff --git a/components/cronet/android/api/src/org/chromium/net/CronetEngine.java b/components/cronet/android/api/src/org/chromium/net/CronetEngine.java
index 5047ab6e23f289342e02621113f98ffaaf7afa9d..dea6b91e904e4279ba0df4eaa3f3e0284402ed8a 100644
--- a/components/cronet/android/api/src/org/chromium/net/CronetEngine.java
+++ b/components/cronet/android/api/src/org/chromium/net/CronetEngine.java
@@ -6,6 +6,7 @@ package org.chromium.net;
import android.content.Context;
import android.support.annotation.IntDef;
+import android.util.Base64;
import android.util.Log;
import org.json.JSONArray;
@@ -20,8 +21,12 @@ import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandlerFactory;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.Executor;
/**
@@ -303,6 +308,96 @@ public abstract class CronetEngine {
}
/**
+ * Adds public key pins for a given host.
+ *
+ * @param hostName name of the host to which public keys should be pinned.
+ * @param pinsSha256 a collection of pins. Each pin is the SHA-256 cryptographic
+ * hash of DER-encoded ASN.1 representation of Subject Public
+ * Key Info (SPKI) of the host X.509 certificate. Use
+ * {@link java.security.cert.Certificate#getPublicKey()
+ * Certificate.getPublicKey} and
+ * {@link java.security.Key#getEncoded() Key.getEncoded}
+ * to obtain DER-encoded ASN.1 representation of SPKI.
+ * @param includeSubdomains indicates whether the pinning policy should be applied to
+ * subdomains of {@code hostName}.
+ * @param expirationDate specifies the expiration date for the pins.
+ * @return the builder to facilitate chaining.
+ * @throws NullPointerException if one of the input parameters is null.
+ * @throws IllegalArgumentException if the given host name is invalid or the
+ * {@code pinsSha256} collection contains a byte array
+ * that does not represent a valid SHA-256 hash.
+ */
+ public Builder addPublicKeyPins(String hostName, Collection<byte[]> pinsSha256,
+ boolean includeSubdomains, Date expirationDate) {
+ if (hostName == null) {
+ throw new NullPointerException("The hostname cannot be null");
+ }
+ if (!isValidHostNameForPinning(hostName)) {
+ throw new IllegalArgumentException("Invalid host name: " + hostName);
+ }
+ if (pinsSha256 == null) {
+ throw new NullPointerException("The collection of SHA256 pins cannot be null");
+ }
+ try {
+ // Add PKP_LIST JSON array element if it is not present.
+ JSONArray pkpList = mConfig.optJSONArray(CronetEngineBuilderList.PKP_LIST);
+ if (pkpList == null) {
+ pkpList = new JSONArray();
+ mConfig.put(CronetEngineBuilderList.PKP_LIST, pkpList);
+ }
+
+ // Convert the pin to BASE64 encoding.
+ Set<String> hashes = new HashSet<>(pinsSha256.size());
+ for (byte[] pinSha256 : pinsSha256) {
+ hashes.add(convertSha256ToBase64WithPrefix(pinSha256));
+ }
+
+ // Add new element to PKP_LIST JSON array.
+ JSONObject pkp = new JSONObject();
+ pkp.put(CronetEngineBuilderList.PKP_HOST, hostName);
nharper 2015/11/20 19:02:47 At some point, hostName needs to undergo IDN proce
kapishnikov 2015/11/20 20:40:10 Nice catch. I will add a resolution of an internat
kapishnikov 2015/11/23 16:48:45 I have added IDN processing of the hostname to val
+ pkp.put(CronetEngineBuilderList.PKP_PIN_HASHES, new JSONArray(hashes));
+ pkp.put(CronetEngineBuilderList.PKP_INCLUDE_SUBDOMAINS, includeSubdomains);
+ // The expiration time is passed as a double, in seconds since January 1, 1970.
+ pkp.put(CronetEngineBuilderList.PKP_EXPIRATION_DATE,
+ (double) expirationDate.getTime() / 1000);
+ pkpList.put(pkp);
+ } catch (JSONException e) {
+ // This exception should never happen.
+ throw new RuntimeException(
+ "Failed to add pubic key pins with the given arguments", e);
+ }
+ return this;
+ }
+
+ /**
+ * Converts a given SHA256 array of bytes to BASE64 encoded string and prepends
+ * {@code sha256/} prefix to it. The format corresponds to the format that is expected by
+ * {@code net::HashValue} class.
+ *
+ * @param sha256 SHA256 bytes to convert to BASE64.
+ * @return the BASE64 encoded SHA256 with the prefix.
+ * @throws IllegalArgumentException if the provided pin is invalid.
+ */
+ private static String convertSha256ToBase64WithPrefix(byte[] sha256) {
+ if (sha256 == null || sha256.length != 32) {
+ throw new IllegalArgumentException("Public key pin is invalid");
+ }
+ return "sha256/" + Base64.encodeToString(sha256, Base64.NO_WRAP);
+ }
+
+ /**
+ * Checks whether the given string that represents a host name is valid for PKP.
+ * A valid host name should not match IPv4 or IPv6 address.
+ *
+ * @see <a href="https://tools.ietf.org/html/rfc7469#section-2.3.3>RFC 7469</a>
+ * @param hostName host name to check.
+ * @return true if the string is a valid host name.
+ */
+ private static boolean isValidHostNameForPinning(String hostName) {
+ return CronetUtil.isValidHostName(hostName) && !CronetUtil.isValidIPv4(hostName);
+ }
+
+ /**
* Sets experimental options to be used in Cronet.
*
* @param options JSON formatted experimental options.
« no previous file with comments | « components/cronet/android/api/package-list ('k') | components/cronet/android/api/src/org/chromium/net/CronetUtil.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698