1 // Copyright 2013 The Chromium Authors 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> 9 #include <stdint.h> 10 11 #include <string> 12 #include <vector> 13 14 #include "base/android/scoped_java_ref.h" 15 #include "base/containers/span.h" 16 #include "base/strings/string_piece.h" 17 #include "net/ssl/ssl_client_cert_type.h" 18 19 // Misc functions to access the Android platform KeyStore. 20 21 namespace net::android { 22 23 // Define a list of constants describing private key types. The 24 // values are shared with Java through org.chromium.net.PrivateKeyType. 25 // Example: PRIVATE_KEY_TYPE_RSA. 26 // 27 // A Java counterpart will be generated for this enum. 28 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net 29 enum PrivateKeyType { 30 PRIVATE_KEY_TYPE_RSA = 0, 31 // Obsolete: PRIVATE_KEY_TYPE_DSA = 1, 32 PRIVATE_KEY_TYPE_ECDSA = 2, 33 PRIVATE_KEY_TYPE_INVALID = 255, 34 }; 35 36 // Returns the name of the class which implements the private key. 37 std::string GetPrivateKeyClassName(const base::android::JavaRef<jobject>& key); 38 39 // Returns whether |key| supports the signature algorithm |algorithm|. 40 bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key, 41 base::StringPiece algorithm); 42 43 // Returns whether |key| supports the encryption algorithm |algorithm|. 44 bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key, 45 base::StringPiece algorithm); 46 47 // Compute the signature of a given input using a private key. For more 48 // details, please read the comments for the signWithPrivateKey method in 49 // AndroidKeyStore.java. 50 // 51 // |private_key| is a JNI reference for the private key. 52 // |algorithm| is the name of the algorithm to sign. 53 // |input| is the input to sign. 54 // |signature| will receive the signature on success. 55 // Returns true on success, false on failure. 56 bool SignWithPrivateKey(const base::android::JavaRef<jobject>& private_key, 57 base::StringPiece algorithm, 58 base::span<const uint8_t> input, 59 std::vector<uint8_t>* signature); 60 61 // Encrypts a given input using a private key. For more details, please read the 62 // comments for the encryptWithPrivateKey method in AndroidKeyStore.java. 63 // 64 // |private_key| is a JNI reference for the private key. 65 // |algorithm| is the name of the algorithm to use. 66 // |input| is the input to encrypt. 67 // |ciphertext| will receive the ciphertext on success. 68 // Returns true on success, false on failure. 69 bool EncryptWithPrivateKey(const base::android::JavaRef<jobject>& private_key, 70 base::StringPiece algorithm, 71 base::span<const uint8_t> input, 72 std::vector<uint8_t>* ciphertext); 73 74 } // namespace net::android 75 76 #endif // NET_ANDROID_KEYSTORE_H_ 77