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