summaryrefslogtreecommitdiffstats
path: root/net/android/keystore.cc
blob: 3a2948a71730b47ec6758ad7d75f478bf903f6a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/android/keystore.h"

#include <vector>

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/logging.h"
#include "jni/AndroidKeyStore_jni.h"
#include "net/android/android_private_key.h"

using base::android::AttachCurrentThread;
using base::android::HasException;
using base::android::JavaByteArrayToByteVector;
using base::android::ScopedJavaLocalRef;
using base::android::ToJavaByteArray;
using base::android::JavaArrayOfByteArrayToStringVector;

namespace net {
namespace android {

bool GetRSAKeyModulus(
    jobject private_key_ref,
    std::vector<uint8>* result) {
  JNIEnv* env = AttachCurrentThread();

  ScopedJavaLocalRef<jbyteArray> modulus_ref =
      Java_AndroidKeyStore_getRSAKeyModulus(env,
                                            GetKeyStore(private_key_ref).obj(),
                                            private_key_ref);
  if (modulus_ref.is_null())
    return false;

  JavaByteArrayToByteVector(env, modulus_ref.obj(), result);
  return true;
}

bool GetDSAKeyParamQ(jobject private_key_ref,
                     std::vector<uint8>* result) {
  JNIEnv* env = AttachCurrentThread();

  ScopedJavaLocalRef<jbyteArray> q_ref =
      Java_AndroidKeyStore_getDSAKeyParamQ(
          env,
          GetKeyStore(private_key_ref).obj(),
          private_key_ref);
  if (q_ref.is_null())
    return false;

  JavaByteArrayToByteVector(env, q_ref.obj(), result);
  return true;
}

bool GetECKeyOrder(jobject private_key_ref,
                   std::vector<uint8>* result) {
  JNIEnv* env = AttachCurrentThread();

  ScopedJavaLocalRef<jbyteArray> order_ref =
      Java_AndroidKeyStore_getECKeyOrder(
          env,
          GetKeyStore(private_key_ref).obj(),
          private_key_ref);

  if (order_ref.is_null())
    return false;

  JavaByteArrayToByteVector(env, order_ref.obj(), result);
  return true;
}

bool GetPrivateKeyEncodedBytes(jobject private_key_ref,
                               std::vector<uint8>* result) {
  JNIEnv* env = AttachCurrentThread();

  ScopedJavaLocalRef<jbyteArray> encoded_ref =
      Java_AndroidKeyStore_getPrivateKeyEncodedBytes(
          env,
          GetKeyStore(private_key_ref).obj(),
          private_key_ref);
  if (encoded_ref.is_null())
    return false;

  JavaByteArrayToByteVector(env, encoded_ref.obj(), result);
  return true;
}

bool RawSignDigestWithPrivateKey(
    jobject private_key_ref,
    const base::StringPiece& digest,
    std::vector<uint8>* signature) {
  JNIEnv* env = AttachCurrentThread();

  // Convert message to byte[] array.
  ScopedJavaLocalRef<jbyteArray> digest_ref =
      ToJavaByteArray(env,
                      reinterpret_cast<const uint8*>(digest.data()),
                      digest.length());
  DCHECK(!digest_ref.is_null());

  // Invoke platform API
  ScopedJavaLocalRef<jbyteArray> signature_ref =
      Java_AndroidKeyStore_rawSignDigestWithPrivateKey(
          env,
          GetKeyStore(private_key_ref).obj(),
          private_key_ref,
          digest_ref.obj());
  if (HasException(env) || signature_ref.is_null())
    return false;

  // Write signature to string.
  JavaByteArrayToByteVector(env, signature_ref.obj(), signature);
  return true;
}

PrivateKeyType GetPrivateKeyType(jobject private_key_ref) {
  JNIEnv* env = AttachCurrentThread();
  int type = Java_AndroidKeyStore_getPrivateKeyType(
      env,
      GetKeyStore(private_key_ref).obj(),
      private_key_ref);
  return static_cast<PrivateKeyType>(type);
}

AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(jobject private_key_ref) {
  JNIEnv* env = AttachCurrentThread();
  // Note: the pointer is passed as a jint here because that's how it
  // is stored in the Java object. Java doesn't have a primitive type
  // like intptr_t that matches the size of pointers on the host
  // machine, and Android only runs on 32-bit CPUs.
  //
  // Given that this routine shall only be called on Android < 4.2,
  // this won't be a problem in the far future (e.g. when Android gets
  // ported to 64-bit environments, if ever).
  long pkey = Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(
      env,
      GetKeyStore(private_key_ref).obj(),
      private_key_ref);
  return reinterpret_cast<AndroidEVP_PKEY*>(pkey);
}

ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey(
    jobject private_key_ref) {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jobject> engine =
      Java_AndroidKeyStore_getOpenSSLEngineForPrivateKey(
          env,
          GetKeyStore(private_key_ref).obj(),
          private_key_ref);
  return engine;
}

void ReleaseKey(jobject private_key_ref) {
  JNIEnv* env = AttachCurrentThread();
  Java_AndroidKeyStore_releaseKey(env,
                                  GetKeyStore(private_key_ref).obj(),
                                  private_key_ref);
  env->DeleteGlobalRef(private_key_ref);
}

bool RegisterKeyStore(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace android
}  // namespace net