summaryrefslogtreecommitdiff
path: root/jni/jbcrypto.cpp
blob: 0c56b97436b3853c29a5957002db29db31229649 (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
//
//  JBCyrpto.cpp
//  xcopenvpn
//
//  Created by Arne Schwabe on 12.07.12.
//  Copyright (c) 2012 Universität Paderborn. All rights reserved.
//

#include <jni.h>

#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/objects.h>
#include <openssl/md5.h>

extern "C" {
jbyteArray Java_de_blinkt_openvpn_OpenVpnManagementThread_rsasign(JNIEnv* env, jclass, jbyteArray from, jint pkeyRef);
}


jbyteArray Java_de_blinkt_openvpn_OpenVpnManagementThread_rsasign(JNIEnv* env, jclass, jbyteArray from, jint pkeyRef) {

	//	EVP_MD_CTX* ctx = reinterpret_cast<EVP_MD_CTX*>(ctxRef);
	EVP_PKEY* pkey = reinterpret_cast<EVP_PKEY*>(pkeyRef);


	if (pkey == NULL || from == NULL) {
		jniThrowException(env, "java/lang/NullPointerException", "EVP_KEY is null");
		return NULL;
	}

	jbyte* data =  env-> GetByteArrayElements (from, NULL);
	int  datalen = env-> GetArrayLength(from);

	if(data==NULL || datalen == )

		unsigned int siglen;
	unsigned char* sigret = (unsigned char*)malloc(RSA_size(pkey->pkey.rsa));


	//int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
	//           unsigned char *sigret, unsigned int *siglen, RSA *rsa);

	// adapted from s3_clnt.c
	if (RSA_sign(NID_md5_sha1, (unsigned char*) data, datalen,
			sigret, &siglen, pkey->pkey.rsa) <= 0 )
	{

		ERR_print_errors(errbio);
		jniThrowException(env, "java/security/InvalidKeyException", "rsa_sign went wrong, see logcat");

		ERR_print_errors_fp(stderr);
		return NULL;


	}


	jbyteArray jb;

	jb =env->NewByteArray(siglen);

	env->SetByteArrayRegion(jb, 0, siglen, (jbyte *) sigret);
	free(sigret);
	return jb;

}