keystore.h 2.9 KB

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