Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 5 | #ifndef NET_ANDROID_KEYSTORE_H_ |
| 6 | #define NET_ANDROID_KEYSTORE_H_ |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 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" |
David Benjamin | 9ba36b0 | 2017-11-10 19:01:53 | [diff] [blame] | 15 | #include "base/containers/span.h" |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 16 | #include "base/strings/string_piece.h" |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 17 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 18 | // Misc functions to access the Android platform KeyStore. |
| 19 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 20 | namespace net::android { |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 21 | |
| 22 | // Define a list of constants describing private key types. The |
| 23 | // values are shared with Java through org.chromium.net.PrivateKeyType. |
| 24 | // Example: PRIVATE_KEY_TYPE_RSA. |
mkosiba | f6ebbf6b | 2014-09-30 14:42:39 | [diff] [blame] | 25 | // |
| 26 | // A Java counterpart will be generated for this enum. |
| 27 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 28 | enum PrivateKeyType { |
mkosiba | f6ebbf6b | 2014-09-30 14:42:39 | [diff] [blame] | 29 | PRIVATE_KEY_TYPE_RSA = 0, |
davidben | 370b6fa | 2015-06-09 18:34:27 | [diff] [blame] | 30 | // Obsolete: PRIVATE_KEY_TYPE_DSA = 1, |
mkosiba | f6ebbf6b | 2014-09-30 14:42:39 | [diff] [blame] | 31 | PRIVATE_KEY_TYPE_ECDSA = 2, |
| 32 | PRIVATE_KEY_TYPE_INVALID = 255, |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 33 | }; |
| 34 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 35 | // Returns the name of the class which implements the private key. |
| 36 | std::string GetPrivateKeyClassName(const base::android::JavaRef<jobject>& key); |
| 37 | |
David Benjamin | 08d50eb | 2019-04-03 21:05:34 | [diff] [blame] | 38 | // Returns whether |key| supports the signature algorithm |algorithm|. |
| 39 | bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key, |
| 40 | base::StringPiece algorithm); |
| 41 | |
| 42 | // Returns whether |key| supports the encryption algorithm |algorithm|. |
| 43 | bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key, |
| 44 | base::StringPiece algorithm); |
| 45 | |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 46 | // Compute the signature of a given input using a private key. For more |
| 47 | // details, please read the comments for the signWithPrivateKey method in |
| 48 | // AndroidKeyStore.java. |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 49 | // |
| 50 | // |private_key| is a JNI reference for the private key. |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 51 | // |algorithm| is the name of the algorithm to sign. |
| 52 | // |input| is the input to sign. |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 53 | // |signature| will receive the signature on success. |
| 54 | // Returns true on success, false on failure. |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 55 | bool SignWithPrivateKey(const base::android::JavaRef<jobject>& private_key, |
| 56 | base::StringPiece algorithm, |
| 57 | base::span<const uint8_t> input, |
| 58 | std::vector<uint8_t>* signature); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 59 | |
David Benjamin | 08d50eb | 2019-04-03 21:05:34 | [diff] [blame] | 60 | // Encrypts a given input using a private key. For more details, please read the |
| 61 | // comments for the encryptWithPrivateKey method in AndroidKeyStore.java. |
| 62 | // |
| 63 | // |private_key| is a JNI reference for the private key. |
| 64 | // |algorithm| is the name of the algorithm to use. |
| 65 | // |input| is the input to encrypt. |
| 66 | // |ciphertext| will receive the ciphertext on success. |
| 67 | // Returns true on success, false on failure. |
| 68 | bool EncryptWithPrivateKey(const base::android::JavaRef<jobject>& private_key, |
| 69 | base::StringPiece algorithm, |
| 70 | base::span<const uint8_t> input, |
| 71 | std::vector<uint8_t>* ciphertext); |
| 72 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 73 | } // namespace net::android |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 74 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 75 | #endif // NET_ANDROID_KEYSTORE_H_ |