summaryrefslogtreecommitdiffstats
path: root/keystore/jni/certtool.c
blob: 1ae8dab4281f57b62b898e2e2fefa538997335cb (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
**
** Copyright 2009, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#define LOG_TAG "CertTool"

#include <string.h>
#include <jni.h>
#include <cutils/log.h>
#include <openssl/pkcs12.h>
#include <openssl/x509v3.h>

#include "cert.h"

typedef int PKCS12_KEYSTORE_FUNC(PKCS12_KEYSTORE *store, char *buf, int size);

jstring
android_security_CertTool_generateCertificateRequest(JNIEnv* env,
                                                     jobject thiz,
                                                     jint bits,
                                                     jstring subject)

{
    char csr[REPLY_MAX];
    if (gen_csr(bits, subject, csr) == 0) {
        return (*env)->NewStringUTF(env, csr);
    }
    return NULL;
}

jboolean
android_security_CertTool_isPkcs12Keystore(JNIEnv* env,
                                           jobject thiz,
                                           jbyteArray data)
{
    int len = (*env)->GetArrayLength(env, data);

    if (len > 0) {
        PKCS12 *handle = NULL;
        char buf[len];

        (*env)->GetByteArrayRegion(env, data, 0, len, (jbyte*)buf);
        return (jboolean)is_pkcs12(buf, len);
    } else {
        return 0;
    }
}

jint
android_security_CertTool_getPkcs12Handle(JNIEnv* env,
                                          jobject thiz,
                                          jbyteArray data,
                                          jstring jPassword)
{
    jboolean bIsCopy;
    int len = (*env)->GetArrayLength(env, data);
    const char* passwd = (*env)->GetStringUTFChars(env, jPassword , &bIsCopy);

    if (len > 0) {
        PKCS12_KEYSTORE *handle = NULL;
        char buf[len];

        (*env)->GetByteArrayRegion(env, data, 0, len, (jbyte*)buf);
        handle = get_pkcs12_keystore_handle(buf, len, passwd);
        (*env)->ReleaseStringUTFChars(env, jPassword, passwd);
        return (jint)handle;
    } else {
        return 0;
    }
}

jstring call_pkcs12_ks_func(PKCS12_KEYSTORE_FUNC *func,
                            JNIEnv* env,
                            jobject thiz,
                            jint phandle)
{
    char buf[REPLY_MAX];

    if (phandle == 0) return NULL;
    if (func((PKCS12_KEYSTORE*)phandle, buf, sizeof(buf)) == 0) {
        return (*env)->NewStringUTF(env, buf);
    }
    return NULL;
}

jstring
android_security_CertTool_getPkcs12Certificate(JNIEnv* env,
                                               jobject thiz,
                                               jint phandle)
{
    return call_pkcs12_ks_func((PKCS12_KEYSTORE_FUNC *)get_pkcs12_certificate,
                               env, thiz, phandle);
}

jstring
android_security_CertTool_getPkcs12PrivateKey(JNIEnv* env,
                                              jobject thiz,
                                              jint phandle)
{
    return call_pkcs12_ks_func((PKCS12_KEYSTORE_FUNC *)get_pkcs12_private_key,
                               env, thiz, phandle);
}

jstring
android_security_CertTool_popPkcs12CertificateStack(JNIEnv* env,
                                                    jobject thiz,
                                                    jint phandle)
{
    return call_pkcs12_ks_func((PKCS12_KEYSTORE_FUNC *)pop_pkcs12_certs_stack,
                               env, thiz, phandle);
}

void android_security_CertTool_freePkcs12Handle(JNIEnv* env,
                                                jobject thiz,
                                                jint handle)
{
    if (handle != 0) free_pkcs12_keystore((PKCS12_KEYSTORE*)handle);
}

jint
android_security_CertTool_generateX509Certificate(JNIEnv* env,
                                                  jobject thiz,
                                                  jbyteArray data)
{
    char buf[REPLY_MAX];
    int len = (*env)->GetArrayLength(env, data);

    if (len > REPLY_MAX) return 0;
    (*env)->GetByteArrayRegion(env, data, 0, len, (jbyte*)buf);
    return (jint) parse_cert(buf, len);
}

jboolean android_security_CertTool_isCaCertificate(JNIEnv* env,
                                                   jobject thiz,
                                                   jint handle)
{
    return (handle == 0) ? (jboolean)0 : (jboolean) is_ca_cert((X509*)handle);
}

jstring android_security_CertTool_getIssuerDN(JNIEnv* env,
                                              jobject thiz,
                                              jint handle)
{
    char issuer[MAX_CERT_NAME_LEN];

    if (handle == 0) return NULL;
    if (get_issuer_name((X509*)handle, issuer, MAX_CERT_NAME_LEN)) return NULL;
    return (*env)->NewStringUTF(env, issuer);
}

jstring android_security_CertTool_getCertificateDN(JNIEnv* env,
                                                   jobject thiz,
                                                   jint handle)
{
    char name[MAX_CERT_NAME_LEN];
    if (handle == 0) return NULL;
    if (get_cert_name((X509*)handle, name, MAX_CERT_NAME_LEN)) return NULL;
    return (*env)->NewStringUTF(env, name);
}

jstring android_security_CertTool_getPrivateKeyPEM(JNIEnv* env,
                                                   jobject thiz,
                                                   jint handle)
{
    char pem[MAX_PEM_LENGTH];
    if (handle == 0) return NULL;
    if (get_private_key_pem((X509*)handle, pem, MAX_PEM_LENGTH)) return NULL;
    return (*env)->NewStringUTF(env, pem);
}

void android_security_CertTool_freeX509Certificate(JNIEnv* env,
                                                   jobject thiz,
                                                   jint handle)
{
    if (handle != 0) X509_free((X509*)handle);
}

/*
 * Table of methods associated with the CertTool class.
 */
static JNINativeMethod gCertToolMethods[] = {
    /* name, signature, funcPtr */
    {"generateCertificateRequest", "(ILjava/lang/String;)Ljava/lang/String;",
        (void*)android_security_CertTool_generateCertificateRequest},
    {"isPkcs12Keystore", "([B)Z",
        (void*)android_security_CertTool_isPkcs12Keystore},
    {"getPkcs12Handle", "([BLjava/lang/String;)I",
        (void*)android_security_CertTool_getPkcs12Handle},
    {"getPkcs12Certificate", "(I)Ljava/lang/String;",
        (void*)android_security_CertTool_getPkcs12Certificate},
    {"getPkcs12PrivateKey", "(I)Ljava/lang/String;",
        (void*)android_security_CertTool_getPkcs12PrivateKey},
    {"popPkcs12CertificateStack", "(I)Ljava/lang/String;",
        (void*)android_security_CertTool_popPkcs12CertificateStack},
    {"freePkcs12Handle", "(I)V",
        (void*)android_security_CertTool_freePkcs12Handle},
    {"generateX509Certificate", "([B)I",
        (void*)android_security_CertTool_generateX509Certificate},
    {"isCaCertificate", "(I)Z",
        (void*)android_security_CertTool_isCaCertificate},
    {"getIssuerDN", "(I)Ljava/lang/String;",
        (void*)android_security_CertTool_getIssuerDN},
    {"getCertificateDN", "(I)Ljava/lang/String;",
        (void*)android_security_CertTool_getCertificateDN},
    {"getPrivateKeyPEM", "(I)Ljava/lang/String;",
        (void*)android_security_CertTool_getPrivateKeyPEM},
    {"freeX509Certificate", "(I)V",
        (void*)android_security_CertTool_freeX509Certificate},
};

/*
 * Register several native methods for one class.
 */
static int registerNatives(JNIEnv* env, const char* className,
                           JNINativeMethod* gMethods, int numMethods)
{
    jclass clazz;

    clazz = (*env)->FindClass(env, className);
    if (clazz == NULL) {
        LOGE("Can not find class %s\n", className);
        return JNI_FALSE;
    }

    if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
        LOGE("Can not RegisterNatives\n");
        return JNI_FALSE;
    }

    return JNI_TRUE;
}

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;


    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        goto bail;
    }

    if (!registerNatives(env, "android/security/CertTool",
                         gCertToolMethods, nelem(gCertToolMethods))) {
        goto bail;
    }

    /* success -- return valid version number */
    result = JNI_VERSION_1_4;

bail:
    return result;
}