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

Unified Diff: net/android/java/src/org/chromium/net/AndroidKeyStore.java

Issue 11571059: Add net/android/keystore.h (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add DSA + ECDSA test keys and signing tests Created 7 years, 11 months 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: net/android/java/src/org/chromium/net/AndroidKeyStore.java
diff --git a/net/android/java/src/org/chromium/net/AndroidKeyStore.java b/net/android/java/src/org/chromium/net/AndroidKeyStore.java
new file mode 100644
index 0000000000000000000000000000000000000000..d4c2ba6ba904b83acf8118e6738338345bddc39d
--- /dev/null
+++ b/net/android/java/src/org/chromium/net/AndroidKeyStore.java
@@ -0,0 +1,126 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
palmer 2013/01/26 02:14:20 NIT: 2013. :)
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.net;
+
+import android.util.Log;
+
+import java.security.interfaces.DSAPrivateKey;
+import java.security.interfaces.ECPrivateKey;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.PrivateKey;
+import java.security.Signature;
+
+import org.chromium.base.CalledByNative;
+import org.chromium.base.JNINamespace;
+import org.chromium.net.SSLClientCertType;
+
+@JNINamespace("net::android")
+public class AndroidKeyStore {
+
+ private static final String TAG = AndroidKeyStore.class.getName();
+
+ ////////////////////////////////////////////////////////////////////
+ //
+ // Message signing support.
+ //
+
+ /**
+ * Called from native code to sign a given message with an RSA-based
+ * PrivateKey object.
Ryan Sleevi 2013/01/26 01:51:57 Comment is wrong. It's not just RSA keys you're ha
+ * Assumes that initSignatureForKey() was called previously on the
palmer 2013/01/26 02:14:20 Eliminate the need for this assumption by having t
digit1 2013/01/28 10:16:30 I should have documented this better when refactor
+ * same privateKey handle.
+ * @param privateKey The PrivateKey handle.
+ * @param message The message to sign.
+ * @return signature as a byte buffer.
+ */
+ @CalledByNative
+ public static byte[] signWithPrivateKey(PrivateKey privateKey,
+ byte[] message) {
+ synchronized (TAG) {
Ryan Sleevi 2013/01/26 01:51:57 Performing the signing entirely in a synchronized
palmer 2013/01/26 02:14:20 Seems odd to synchronize on an unrelated object; w
digit1 2013/01/28 10:16:30 This is a static method, using |this| cannot work.
digit1 2013/01/28 10:16:30 See comment above, it's because initSignatureForPr
+ // Get the Signature singleton for this key.
+ Signature signature = null;
+ if (privateKey instanceof RSAPrivateKey) {
+ signature = sRsaSignature;
+ } else if (privateKey instanceof DSAPrivateKey) {
+ signature = sDsaSignature;
+ } else if (privateKey instanceof ECPrivateKey) {
+ signature = sEcdsaSignature;
+ }
+ if (signature == null) {
+ Log.e(TAG, "Unsupported private key algorithm: " + privateKey.getAlgorithm());
+ return null;
+ }
+
+ // Sign the message.
+ try {
+ signature.initSign(privateKey);
+ signature.update(message);
+ return signature.sign();
+ } catch (Exception e) {
+ Log.e(TAG, "Exception while signing message with " + privateKey.getAlgorithm() +
+ " private key: " + e);
+ return null;
+ }
+ }
+ }
+
+ /**
+ * Called from native code to return the type of a given PrivateKey
+ * object. This is an integer that maps to one of the values defined
+ * by org.chromium.net.SSLClientCertType, which is itself
+ * auto-generated from net/base/ssl_client_cert_type_list.h
+ * @param privateKey The PrivateKey handle
+ * @return key type, or SSLClientCertType.INVALID_TYPE if unknown.
+ */
+ @CalledByNative
+ public static int getPrivateKeySigningType(PrivateKey privateKey) {
+ if (privateKey instanceof RSAPrivateKey)
+ return SSLClientCertType.RSA_SIGN;
+ if (privateKey instanceof DSAPrivateKey)
+ return SSLClientCertType.DSS_SIGN;
+ if (privateKey instanceof ECPrivateKey)
+ return SSLClientCertType.ECDSA_SIGN;
+ else
+ return SSLClientCertType.INVALID_TYPE;
+ }
+
+ // Single signature instances. Used to perform signing with private
+ // keys. To avoid increasing Chromium's startup time, these are
+ // created lazily by calling initSignatureForKey below.
+ private static Signature sRsaSignature;
+ private static Signature sDsaSignature;
+ private static Signature sEcdsaSignature;
+
+ /**
+ * Called to ensure that the global Signature object corresponding
+ * to a given private key is initialized before a call to signWithPrivateKey.
+ * Note that this is a potentially blocking operation.
+ * @param key A PrivateKey handle.
+ */
+ public static void initSignatureForKey(PrivateKey key) {
+ String algorithm = key.getAlgorithm();
+ synchronized (TAG) {
palmer 2013/01/26 02:14:20 Same comment as above.
+ try {
+ // Hint: Algorithm names come from:
+ // http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html
+ if (algorithm.equals("RSA") && sRsaSignature == null) {
+ // IMPORTANT: Due to what looks like a platform bug, this will
+ // throw NoSuchAlgorithmException on Android 4.0.x and 4.1.x, Fixed in 4.2
palmer 2013/01/26 02:14:20 NIT: End with period, not comma.
+ // and higher. See https://android-review.googlesource.com/#/c/40352/
palmer 2013/01/26 02:14:20 Anonymous internet users can't read this; that wou
digit1 2013/01/28 10:16:30 Really? This is a link to the public AOSP gerrit i
+ sRsaSignature = Signature.getInstance("NONEwithRSA");
+ } else if (algorithm.equals("DSA") && sDsaSignature == null) {
+ sDsaSignature = Signature.getInstance("NONEwithDSA");
+ } else if (algorithm.equals("EC") && sEcdsaSignature == null) {
+ // The documentation mentions that NONEwithECDSA is ambiguous
+ // and that SHA1withECDSA, its synonym, should be used instead.
Ryan Sleevi 2013/01/26 01:51:57 No, you misread. It's talking about the algorithm
digit1 2013/01/28 10:16:30 Oh right, thanks, I'll remove this comment.
+ sEcdsaSignature = Signature.getInstance("NONEwithECDSA");
+ }
palmer 2013/01/26 02:14:20 Should there be an else clause that warns the call
digit1 2013/01/28 10:16:30 This is later handled in signWithPrivateKey().
+ } catch (Exception e) {
+ Log.w(TAG, "Could not create " + algorithm + " Signature singleton:" + e);
+ }
+ }
+ }
+
+}

Powered by Google App Engine
This is Rietveld 408576698