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

#include <jni.h>

#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

// Minimal defines for openssl 1.0.x
typedef void *RSA;

struct EVP_PKEY
{
  int type;
  int save_type;
  int references;
  void *ameth;
  void *engine;
  union {
    RSA *rsa;
  } pkey;
};

# define RSA_PKCS1_PADDING       1

extern "C" {
    jbyteArray Java_de_blinkt_openvpn_core_NativeUtils_rsasign(JNIEnv* env, jclass, jbyteArray from, jint pkeyRef);
    int jniThrowException(JNIEnv* env, const char* className, const char* msg);

    int (*RSA_size_dyn)(const RSA *);
    int (*RSA_private_encrypt_dyn)(int, const unsigned char *, unsigned char *, RSA *, int);

    unsigned long (*ERR_get_error_dyn)();
    void (*ERR_error_string_n_dyn)(unsigned long, char *, size_t);

    void (*ERR_print_errors_fp_dyn)(FILE *);

}

int jniThrowException(JNIEnv* env, const char* className, const char* msg) {

    jclass exceptionClass = env->FindClass(className);

    if (exceptionClass == NULL) {
        __android_log_print(ANDROID_LOG_DEBUG,"openvpn","Unable to find exception class %s", className);
        /* ClassNotFoundException now pending */
        return -1;
    }

    if (env->ThrowNew( exceptionClass, msg) != JNI_OK) {
    	__android_log_print(ANDROID_LOG_DEBUG,"openvpn","Failed throwing '%s' '%s'", className, msg);
        /* an exception, most likely OOM, will now be pending */
        return -1;
    }

    env->DeleteLocalRef(exceptionClass);
    return 0;
}

static char opensslerr[1024];
jbyteArray Java_de_blinkt_openvpn_core_NativeUtils_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 )
		jniThrowException(env, "java/lang/NullPointerException", "data is null");

    int siglen;
    RSA_size_dyn= (int (*) (const RSA *)) dlsym(RTLD_DEFAULT, "RSA_size");
	unsigned char* sigret = (unsigned char*)malloc(RSA_size_dyn(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 ) */

    RSA_private_encrypt_dyn=(int (*)(int, const unsigned char *, unsigned char *, RSA *, int)) dlsym(RTLD_DEFAULT, "RSA_private_encrypt");
    siglen = RSA_private_encrypt_dyn(datalen,(unsigned char*) data,sigret,pkey->pkey.rsa,RSA_PKCS1_PADDING);

    if (siglen < 0)
	{
        ERR_get_error_dyn = (unsigned long (*)()) dlsym(RTLD_DEFAULT, "ERR_get_error");
        ERR_error_string_n_dyn = (void (*)(unsigned long, char *, size_t)) dlsym(RTLD_DEFAULT, "ERR_error_string_n");

        ERR_error_string_n_dyn(ERR_get_error_dyn(), opensslerr ,1024);
		jniThrowException(env, "java/security/InvalidKeyException", opensslerr);

        ERR_print_errors_fp_dyn = (void (*)(FILE *)) dlsym(RTLD_DEFAULT, "ERR_print_errors_fp");
		ERR_print_errors_fp_dyn(stderr);
		return NULL;

	}


	jbyteArray jb;

	jb =env->NewByteArray(siglen);

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

}