keystore.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "net/android/keystore.h"
  5. #include <vector>
  6. #include "base/android/jni_android.h"
  7. #include "base/android/jni_array.h"
  8. #include "base/android/jni_string.h"
  9. #include "base/check.h"
  10. #include "net/net_jni_headers/AndroidKeyStore_jni.h"
  11. using base::android::AttachCurrentThread;
  12. using base::android::ConvertJavaStringToUTF8;
  13. using base::android::ConvertUTF8ToJavaString;
  14. using base::android::HasException;
  15. using base::android::JavaByteArrayToByteVector;
  16. using base::android::JavaRef;
  17. using base::android::ScopedJavaLocalRef;
  18. using base::android::ToJavaByteArray;
  19. namespace net::android {
  20. std::string GetPrivateKeyClassName(const JavaRef<jobject>& key) {
  21. JNIEnv* env = AttachCurrentThread();
  22. ScopedJavaLocalRef<jstring> name =
  23. Java_AndroidKeyStore_getPrivateKeyClassName(env, key);
  24. return ConvertJavaStringToUTF8(env, name);
  25. }
  26. bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key,
  27. base::StringPiece algorithm) {
  28. JNIEnv* env = AttachCurrentThread();
  29. ScopedJavaLocalRef<jstring> algorithm_ref =
  30. ConvertUTF8ToJavaString(env, algorithm);
  31. DCHECK(!algorithm_ref.is_null());
  32. jboolean result =
  33. Java_AndroidKeyStore_privateKeySupportsSignature(env, key, algorithm_ref);
  34. return !HasException(env) && result;
  35. }
  36. bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key,
  37. base::StringPiece algorithm) {
  38. JNIEnv* env = AttachCurrentThread();
  39. ScopedJavaLocalRef<jstring> algorithm_ref =
  40. ConvertUTF8ToJavaString(env, algorithm);
  41. DCHECK(!algorithm_ref.is_null());
  42. jboolean result =
  43. Java_AndroidKeyStore_privateKeySupportsCipher(env, key, algorithm_ref);
  44. return !HasException(env) && result;
  45. }
  46. bool SignWithPrivateKey(const JavaRef<jobject>& private_key_ref,
  47. base::StringPiece algorithm,
  48. base::span<const uint8_t> input,
  49. std::vector<uint8_t>* signature) {
  50. JNIEnv* env = AttachCurrentThread();
  51. ScopedJavaLocalRef<jstring> algorithm_ref =
  52. ConvertUTF8ToJavaString(env, algorithm);
  53. DCHECK(!algorithm_ref.is_null());
  54. // Convert message to byte[] array.
  55. ScopedJavaLocalRef<jbyteArray> input_ref =
  56. ToJavaByteArray(env, input.data(), input.size());
  57. DCHECK(!input_ref.is_null());
  58. // Invoke platform API
  59. ScopedJavaLocalRef<jbyteArray> signature_ref =
  60. Java_AndroidKeyStore_signWithPrivateKey(env, private_key_ref,
  61. algorithm_ref, input_ref);
  62. if (HasException(env) || signature_ref.is_null())
  63. return false;
  64. // Write signature to string.
  65. JavaByteArrayToByteVector(env, signature_ref, signature);
  66. return true;
  67. }
  68. bool EncryptWithPrivateKey(const JavaRef<jobject>& private_key_ref,
  69. base::StringPiece algorithm,
  70. base::span<const uint8_t> input,
  71. std::vector<uint8_t>* ciphertext) {
  72. JNIEnv* env = AttachCurrentThread();
  73. ScopedJavaLocalRef<jstring> algorithm_ref =
  74. ConvertUTF8ToJavaString(env, algorithm);
  75. DCHECK(!algorithm_ref.is_null());
  76. // Convert message to byte[] array.
  77. ScopedJavaLocalRef<jbyteArray> input_ref =
  78. ToJavaByteArray(env, input.data(), input.size());
  79. DCHECK(!input_ref.is_null());
  80. // Invoke platform API
  81. ScopedJavaLocalRef<jbyteArray> ciphertext_ref =
  82. Java_AndroidKeyStore_encryptWithPrivateKey(env, private_key_ref,
  83. algorithm_ref, input_ref);
  84. if (HasException(env) || ciphertext_ref.is_null())
  85. return false;
  86. // Write ciphertext to string.
  87. JavaByteArrayToByteVector(env, ciphertext_ref, ciphertext);
  88. return true;
  89. }
  90. } // namespace net::android