aboutsummaryrefslogtreecommitdiffstats
path: root/src/frontends/android/jni/libandroidbridge/backend/android_private_key.c
blob: 1985f0e98e542ede5c52fc142e7d34a4f8e60ef1 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (C) 2012-2014 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "android_private_key.h"

#include "../android_jni.h"
#include <utils/debug.h>
#include <asn1/asn1.h>

typedef struct private_private_key_t private_private_key_t;

/**
 * Private data of a android_private_key_t object.
 */
struct private_private_key_t {

	/**
	 * Public interface
	 */
	private_key_t public;

	/**
	 * reference to the Java PrivateKey object
	 */
	jobject key;

	/**
	 * Java class used to build signatures
	 */
	jclass signature_class;

	/**
	 * public key that belongs to this private key
	 */
	public_key_t *pubkey;

	/**
	 * reference count
	 */
	refcount_t ref;
};

METHOD(private_key_t, sign, bool,
	private_private_key_t *this, signature_scheme_t scheme,
	chunk_t data, chunk_t *signature)
{
	JNIEnv *env;
	jmethodID method_id;
	const char *method = NULL;
	jstring jmethod;
	jobject jsignature;
	jbyteArray jdata, jsigarray;

	switch (this->pubkey->get_type(this->pubkey))
	{
		case KEY_RSA:
			switch (scheme)
			{
				case SIGN_RSA_EMSA_PKCS1_MD5:
					method = "MD5withRSA";
					break;
				case SIGN_RSA_EMSA_PKCS1_SHA1:
					method = "SHA1withRSA";
					break;
				case SIGN_RSA_EMSA_PKCS1_SHA224:
					method = "SHA224withRSA";
					break;
				case SIGN_RSA_EMSA_PKCS1_SHA256:
					method = "SHA256withRSA";
					break;
				case SIGN_RSA_EMSA_PKCS1_SHA384:
					method = "SHA384withRSA";
					break;
				case SIGN_RSA_EMSA_PKCS1_SHA512:
					method = "SHA512withRSA";
					break;
				default:
					break;
			}
			break;
		case KEY_ECDSA:
			switch (scheme)
			{
				case SIGN_ECDSA_256:
					method = "SHA256withECDSA";
					break;
				case SIGN_ECDSA_384:
					method = "SHA384withECDSA";
					break;
				case SIGN_ECDSA_521:
					method = "SHA512withECDSA";
					break;
				default:
					break;
			}
			break;
		default:
			break;
	}
	if (!method)
	{
		DBG1(DBG_LIB, "signature scheme %N not supported via JNI",
			 signature_scheme_names, scheme);
		return FALSE;
	}

	androidjni_attach_thread(&env);
	/* we use java.security.Signature to create the signature without requiring
	 * access to the actual private key */
	method_id = (*env)->GetStaticMethodID(env, this->signature_class,
				"getInstance", "(Ljava/lang/String;)Ljava/security/Signature;");
	if (!method_id)
	{
		goto failed;
	}
	jmethod = (*env)->NewStringUTF(env, method);
	if (!jmethod)
	{
		goto failed;
	}
	jsignature = (*env)->CallStaticObjectMethod(env, this->signature_class,
												method_id, jmethod);
	if (!jsignature)
	{
		goto failed;
	}
	method_id = (*env)->GetMethodID(env, this->signature_class, "initSign",
									"(Ljava/security/PrivateKey;)V");
	if (!method_id)
	{
		goto failed;
	}
	(*env)->CallVoidMethod(env, jsignature, method_id, this->key);
	if (androidjni_exception_occurred(env))
	{
		goto failed;
	}
	method_id = (*env)->GetMethodID(env, this->signature_class, "update",
									"([B)V");
	if (!method_id)
	{
		goto failed;
	}
	jdata = byte_array_from_chunk(env, data);
	(*env)->CallVoidMethod(env, jsignature, method_id, jdata);
	if (androidjni_exception_occurred(env))
	{
		goto failed;
	}
	method_id = (*env)->GetMethodID(env, this->signature_class, "sign",
									"()[B");
	if (!method_id)
	{
		goto failed;
	}
	jsigarray = (*env)->CallObjectMethod(env, jsignature, method_id);
	if (!jsigarray)
	{
		goto failed;
	}
	if (this->pubkey->get_type(this->pubkey) == KEY_ECDSA)
	{
		chunk_t encoded, parse, r, s;
		size_t len = 0;

		switch (scheme)
		{
			case SIGN_ECDSA_256:
				len = 32;
				break;
			case SIGN_ECDSA_384:
				len = 48;
				break;
			case SIGN_ECDSA_521:
				len = 66;
				break;
			default:
				break;
		}

		/* we get an ASN.1 encoded sequence of integers r and s */
		parse = encoded = chunk_from_byte_array(env, jsigarray);
		if (asn1_unwrap(&parse, &parse) != ASN1_SEQUENCE ||
			asn1_unwrap(&parse, &r) != ASN1_INTEGER ||
			asn1_unwrap(&parse, &s) != ASN1_INTEGER)
		{
			chunk_free(&encoded);
			goto failed;
		}
		r = chunk_skip_zero(r);
		s = chunk_skip_zero(s);
		if (r.len > len || s.len > len)
		{
			chunk_free(&encoded);
			goto failed;
		}

		/* concatenate r and s (forced to the defined length) */
		*signature = chunk_alloc(2*len);
		memset(signature->ptr, 0, signature->len);
		memcpy(signature->ptr + (len - r.len), r.ptr, r.len);
		memcpy(signature->ptr + len + (len - s.len), s.ptr, s.len);
		chunk_free(&encoded);
	}
	else
	{
		*signature = chunk_from_byte_array(env, jsigarray);
	}
	androidjni_detach_thread();
	return TRUE;

failed:
	DBG1(DBG_LIB, "failed to build %N signature via JNI",
		 signature_scheme_names, scheme);
	androidjni_exception_occurred(env);
	androidjni_detach_thread();
	return FALSE;
}

METHOD(private_key_t, get_type, key_type_t,
	private_private_key_t *this)
{
	return this->pubkey->get_type(this->pubkey);
}

METHOD(private_key_t, decrypt, bool,
	private_private_key_t *this, encryption_scheme_t scheme,
	chunk_t crypto, chunk_t *plain)
{
	DBG1(DBG_LIB, "private key decryption is currently not supported via JNI");
	return FALSE;
}

METHOD(private_key_t, get_keysize, int,
	private_private_key_t *this)
{
	return this->pubkey->get_keysize(this->pubkey);
}

METHOD(private_key_t, get_public_key, public_key_t*,
	private_private_key_t *this)
{
	return this->pubkey->get_ref(this->pubkey);
}

METHOD(private_key_t, get_encoding, bool,
	private_private_key_t *this, cred_encoding_type_t type,
	chunk_t *encoding)
{
	return FALSE;
}

METHOD(private_key_t, get_fingerprint, bool,
	private_private_key_t *this, cred_encoding_type_t type, chunk_t *fp)
{
	return this->pubkey->get_fingerprint(this->pubkey, type, fp);
}

METHOD(private_key_t, get_ref, private_key_t*,
	private_private_key_t *this)
{
	ref_get(&this->ref);
	return &this->public;
}

METHOD(private_key_t, destroy, void,
	private_private_key_t *this)
{
	if (ref_put(&this->ref))
	{
		JNIEnv *env;

		androidjni_attach_thread(&env);
		if (android_sdk_version == ANDROID_JELLY_BEAN)
		{	/* there is a bug in JB that causes a SIGSEGV if the key object is
			 * garbage collected so we intentionally leak the reference to it */
			DBG1(DBG_LIB, "intentionally leaking private key reference due to "
				 "a bug in the framework");
		}
		else
		{
			(*env)->DeleteGlobalRef(env, this->key);
		}
		(*env)->DeleteGlobalRef(env, this->signature_class);
		androidjni_detach_thread();
		this->pubkey->destroy(this->pubkey);
		free(this);
	}
}

/*
 * See header
 */
private_key_t *android_private_key_create(jobject key, public_key_t *pubkey)
{
	JNIEnv *env;
	private_private_key_t *this;

	INIT(this,
		.public = {
			.get_type = _get_type,
			.sign = _sign,
			.decrypt = _decrypt,
			.get_keysize = _get_keysize,
			.get_public_key = _get_public_key,
			.belongs_to = private_key_belongs_to,
			.equals = private_key_equals,
			.get_fingerprint = _get_fingerprint,
			.has_fingerprint = private_key_has_fingerprint,
			.get_encoding = _get_encoding,
			.get_ref = _get_ref,
			.destroy = _destroy,
		},
		.ref = 1,
		.pubkey = pubkey,
	);

	if (!pubkey)
	{
		free(this);
		return NULL;
	}

	/* in ICS we could simply call getEncoded and use the PKCS#8/DER encoded
	 * private key, since JB that's not possible as there is no direct access
	 * to private keys anymore (as these could now be hardware backed) */
	androidjni_attach_thread(&env);
	this->key = (*env)->NewGlobalRef(env, key);
	this->signature_class = (*env)->NewGlobalRef(env, (*env)->FindClass(env,
													"java/security/Signature"));
	androidjni_detach_thread();
	return &this->public;
}