xref: /aosp_15_r20/external/cronet/net/android/keystore.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <string_view>
13 #include <vector>
14 
15 #include "base/android/scoped_java_ref.h"
16 #include "base/containers/span.h"
17 
18 // Misc functions to access the Android platform KeyStore.
19 
20 namespace net::android {
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.
25 //
26 // A Java counterpart will be generated for this enum.
27 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
28 enum PrivateKeyType {
29   PRIVATE_KEY_TYPE_RSA = 0,
30   // Obsolete: PRIVATE_KEY_TYPE_DSA = 1,
31   PRIVATE_KEY_TYPE_ECDSA = 2,
32   PRIVATE_KEY_TYPE_INVALID = 255,
33 };
34 
35 // Returns the name of the class which implements the private key.
36 std::string GetPrivateKeyClassName(const base::android::JavaRef<jobject>& key);
37 
38 // Returns whether |key| supports the signature algorithm |algorithm|.
39 bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key,
40                                  std::string_view algorithm);
41 
42 // Returns whether |key| supports the encryption algorithm |algorithm|.
43 bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key,
44                               std::string_view algorithm);
45 
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.
49 //
50 // |private_key| is a JNI reference for the private key.
51 // |algorithm| is the name of the algorithm to sign.
52 // |input| is the input to sign.
53 // |signature| will receive the signature on success.
54 // Returns true on success, false on failure.
55 bool SignWithPrivateKey(const base::android::JavaRef<jobject>& private_key,
56                         std::string_view algorithm,
57                         base::span<const uint8_t> input,
58                         std::vector<uint8_t>* signature);
59 
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                            std::string_view algorithm,
70                            base::span<const uint8_t> input,
71                            std::vector<uint8_t>* ciphertext);
72 
73 }  // namespace net::android
74 
75 #endif  // NET_ANDROID_KEYSTORE_H_
76