[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_ANDROID_KEYSTORE_H |
| 6 | #define NET_ANDROID_KEYSTORE_H |
| 7 | |
| 8 | #include <jni.h> |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 9 | #include <stdint.h> |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 14 | #include "base/android/scoped_java_ref.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 16 | #include "net/base/net_export.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 17 | #include "net/ssl/ssl_client_cert_type.h" |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 18 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 19 | // Misc functions to access the Android platform KeyStore. |
| 20 | |
| 21 | namespace net { |
| 22 | namespace android { |
| 23 | |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 24 | struct AndroidEVP_PKEY; |
| 25 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 26 | // Define a list of constants describing private key types. The |
| 27 | // values are shared with Java through org.chromium.net.PrivateKeyType. |
| 28 | // Example: PRIVATE_KEY_TYPE_RSA. |
mkosiba | f6ebbf6b | 2014-09-30 14:42:39 | [diff] [blame] | 29 | // |
| 30 | // A Java counterpart will be generated for this enum. |
| 31 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 32 | enum PrivateKeyType { |
mkosiba | f6ebbf6b | 2014-09-30 14:42:39 | [diff] [blame] | 33 | PRIVATE_KEY_TYPE_RSA = 0, |
| 34 | PRIVATE_KEY_TYPE_DSA = 1, |
| 35 | PRIVATE_KEY_TYPE_ECDSA = 2, |
| 36 | PRIVATE_KEY_TYPE_INVALID = 255, |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // Returns the modulus of a given RSAPrivateKey platform object, |
| 40 | // as a series of bytes, in big-endian representation. This can be |
| 41 | // used with BN_bin2bn() to convert to an OpenSSL BIGNUM. |
| 42 | // |
| 43 | // |private_key| is a JNI reference for the private key. |
| 44 | // |modulus| will receive the modulus bytes on success. |
| 45 | // Returns true on success, or false on failure (e.g. if the key |
| 46 | // is not RSA). |
[email protected] | 4f2b94f | 2013-02-22 21:06:37 | [diff] [blame] | 47 | NET_EXPORT bool GetRSAKeyModulus(jobject private_key, |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 48 | std::vector<uint8_t>* modulus); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 49 | |
| 50 | // Returns the Q parameter of a given DSAPrivateKey platform object, |
| 51 | // as a series of bytes, in big-endian representation. This can be used |
| 52 | // with BN_bin2bn() to convert to an OpenSSL BIGNUM. |
| 53 | // |private_key| is a JNI reference for the private key. |
| 54 | // |q| will receive the result bytes on success. |
| 55 | // Returns true on success, or false on failure (e.g. if the key is |
| 56 | // not DSA). |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 57 | NET_EXPORT bool GetDSAKeyParamQ(jobject private_key, std::vector<uint8_t>* q); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 58 | |
| 59 | // Returns the order parameter of a given ECPrivateKey platform object, |
| 60 | // as a series of bytes, in big-endian representation. This can be used |
| 61 | // with BN_bin2bn() to convert to an OpenSSL BIGNUM. |
| 62 | // |private_key| is a JNI reference for the private key. |
| 63 | // |order| will receive the result bytes on success. |
| 64 | // Returns true on success, or false on failure (e.g. if the key is |
| 65 | // not EC). |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 66 | bool GetECKeyOrder(jobject private_key, std::vector<uint8_t>* order); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 67 | |
| 68 | // Returns the encoded PKCS#8 representation of a private key. |
| 69 | // This only works on Android 4.0.3 and older releases for platform keys |
| 70 | // (i.e. all keys except those explicitely generated by the application). |
| 71 | // |private_key| is a JNI reference for the private key. |
| 72 | // |encoded| will receive the encoded data on success. |
| 73 | // Returns true on success, or false on failure (e.g. on 4.0.4 or higher). |
| 74 | bool GetPrivateKeyEncodedBytes(jobject private_key, |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 75 | std::vector<uint8_t>* encoded); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 76 | |
| 77 | // Compute the signature of a given message, which is actually a hash, |
| 78 | // using a private key. For more details, please read the comments for the |
| 79 | // rawSignDigestWithPrivateKey method in AndroidKeyStore.java. |
| 80 | // |
| 81 | // |private_key| is a JNI reference for the private key. |
| 82 | // |digest| is the input digest. |
| 83 | // |signature| will receive the signature on success. |
| 84 | // Returns true on success, false on failure. |
| 85 | // |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame^] | 86 | NET_EXPORT bool RawSignDigestWithPrivateKey(jobject private_key, |
| 87 | const base::StringPiece& digest, |
| 88 | std::vector<uint8_t>* signature); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 89 | |
| 90 | // Return the PrivateKeyType of a given private key. |
| 91 | // |private_key| is a JNI reference for the private key. |
| 92 | // Returns a PrivateKeyType, while will be CLIENT_CERT_INVALID_TYPE |
| 93 | // on error. |
[email protected] | 4f2b94f | 2013-02-22 21:06:37 | [diff] [blame] | 94 | NET_EXPORT PrivateKeyType GetPrivateKeyType(jobject private_key); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 95 | |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 96 | // Returns a handle to the system AndroidEVP_PKEY object used to back a given |
| 97 | // private_key object. This must *only* be used for RSA private keys on Android |
| 98 | // < 4.2. Technically, this is only guaranteed to work if the system image |
| 99 | // contains a vanilla implementation of the Java API frameworks based on Harmony |
| 100 | // + OpenSSL. |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 101 | // |
| 102 | // |private_key| is a JNI reference for the private key. |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 103 | // Returns an AndroidEVP_PKEY* handle, or NULL in case of error. |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 104 | // |
| 105 | // Note: Despite its name and return type, this function doesn't know |
| 106 | // anything about OpenSSL, it just type-casts a system pointer that |
| 107 | // is passed as an int through JNI. As such, it never increments |
| 108 | // the returned key's reference count. |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 109 | AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(jobject private_key); |
| 110 | |
| 111 | // Returns a JNI reference to the OpenSSLEngine object which is used to back a |
| 112 | // given private_key object. This must *only* be used for RSA private keys on |
| 113 | // Android < 4.2. Technically, this is only guaranteed to work if the system |
| 114 | // image contains a vanilla implementation of the Java API frameworks based on |
| 115 | // Harmony + OpenSSL. |
| 116 | base::android::ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey( |
| 117 | jobject private_key); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 118 | |
[email protected] | 2816e1f | 2014-02-15 00:54:27 | [diff] [blame] | 119 | NET_EXPORT void ReleaseKey(jobject private_key); |
| 120 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 121 | // Register JNI methods |
| 122 | NET_EXPORT bool RegisterKeyStore(JNIEnv* env); |
| 123 | |
| 124 | } // namespace android |
| 125 | } // namespace net |
| 126 | |
| 127 | #endif // NET_ANDROID_KEYSTORE_H |