From 2862a851b5957d931840b0cb4f3f279e0602213c Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Fri, 9 Jul 2021 15:15:09 +0200 Subject: Rename opvpnutil to ovpnutil opvpn is a rather unusual abbreviation for openvpn --- main/src/main/cpp/CMakeLists.txt | 10 +- main/src/main/cpp/opvpnutil/jniglue.c | 45 --- main/src/main/cpp/opvpnutil/jniglue.h | 21 -- main/src/main/cpp/opvpnutil/scan_ifs.c | 109 ------- main/src/main/cpp/opvpnutil/sslspeed.c | 315 --------------------- main/src/main/cpp/ovpnutil/jniglue.c | 45 +++ main/src/main/cpp/ovpnutil/jniglue.h | 21 ++ main/src/main/cpp/ovpnutil/scan_ifs.c | 109 +++++++ main/src/main/cpp/ovpnutil/sslspeed.c | 315 +++++++++++++++++++++ .../java/de/blinkt/openvpn/core/NativeUtils.java | 2 +- 10 files changed, 496 insertions(+), 496 deletions(-) delete mode 100644 main/src/main/cpp/opvpnutil/jniglue.c delete mode 100644 main/src/main/cpp/opvpnutil/jniglue.h delete mode 100644 main/src/main/cpp/opvpnutil/scan_ifs.c delete mode 100644 main/src/main/cpp/opvpnutil/sslspeed.c create mode 100644 main/src/main/cpp/ovpnutil/jniglue.c create mode 100644 main/src/main/cpp/ovpnutil/jniglue.h create mode 100644 main/src/main/cpp/ovpnutil/scan_ifs.c create mode 100644 main/src/main/cpp/ovpnutil/sslspeed.c diff --git a/main/src/main/cpp/CMakeLists.txt b/main/src/main/cpp/CMakeLists.txt index e06c76ac..4057b4e2 100644 --- a/main/src/main/cpp/CMakeLists.txt +++ b/main/src/main/cpp/CMakeLists.txt @@ -98,16 +98,16 @@ else () message("Not budiling OpenVPN for output dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") endif () -add_library(opvpnutil SHARED opvpnutil/jniglue.c opvpnutil/scan_ifs.c) -target_compile_definitions(opvpnutil PRIVATE -DTARGET_ARCH_ABI=\"${ANDROID_ABI}\" +add_library(ovpnutil SHARED ovpnutil/jniglue.c ovpnutil/scan_ifs.c) +target_compile_definitions(ovpnutil PRIVATE -DTARGET_ARCH_ABI=\"${ANDROID_ABI}\" -DOPENVPN2_GIT_REVISION=\"${OPENVPN2_GIT}\" -DOPENVPN3_GIT_REVISION=\"${OPENVPN3_GIT}\" ) -target_link_libraries(opvpnutil log) +target_link_libraries(ovpnutil log) if (NOT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} MATCHES "build/intermediates/cmake/.*skeleton.*/") - add_library(osslspeedtest SHARED opvpnutil/sslspeed.c) + add_library(osslspeedtest SHARED ovpnutil/sslspeed.c) target_link_libraries(osslspeedtest log crypto ssl) else () message("Not budiling SSLSpeedTest for output dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") @@ -268,6 +268,6 @@ add_custom_command(TARGET pie_openvpn.${ANDROID_ABI} POST_BUILD ) # Hack that these targets are really executed -add_dependencies(opvpnutil pie_openvpn.${ANDROID_ABI} nopie_openvpn.${ANDROID_ABI}) +add_dependencies(ovpnutil pie_openvpn.${ANDROID_ABI} nopie_openvpn.${ANDROID_ABI}) add_dependencies(pie_openvpn.${ANDROID_ABI} makeassetdir) add_dependencies(nopie_openvpn.${ANDROID_ABI} makeassetdir) diff --git a/main/src/main/cpp/opvpnutil/jniglue.c b/main/src/main/cpp/opvpnutil/jniglue.c deleted file mode 100644 index 264b18a8..00000000 --- a/main/src/main/cpp/opvpnutil/jniglue.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include -#include - - -#include "jniglue.h" - -jint JNI_OnLoad(JavaVM *vm, void *reserved) { -#ifndef NDEBUG - __android_log_write(ANDROID_LOG_DEBUG,"openvpn", "Loading openvpn native library $id$ compiled on " __DATE__ " " __TIME__ ); -#endif - return JNI_VERSION_1_2; -} - - -void android_openvpn_log(int level,const char* prefix,const char* prefix_sep,const char* m1) -{ - __android_log_print(ANDROID_LOG_DEBUG,"openvpn","%s%s%s",prefix,prefix_sep,m1); -} - -void Java_de_blinkt_openvpn_core_NativeUtils_jniclose(JNIEnv *env,jclass jo, jint fd) -{ - int ret = close(fd); -} - - -//! Hack to get the current installed ABI of the libraries. See also https://github.com/schwabe/ics-openvpn/issues/391 -jstring Java_de_blinkt_openvpn_core_NativeUtils_getJNIAPI(JNIEnv *env, jclass jo) -{ - - return (*env)->NewStringUTF(env, TARGET_ARCH_ABI); -} - -jstring Java_de_blinkt_openvpn_core_NativeUtils_getOpenVPN2GitVersion(JNIEnv *env, jclass jo) -{ - - return (*env)->NewStringUTF(env, OPENVPN2_GIT_REVISION); -} - -jstring Java_de_blinkt_openvpn_core_NativeUtils_getOpenVPN3GitVersion(JNIEnv *env, jclass jo) -{ - - return (*env)->NewStringUTF(env, OPENVPN3_GIT_REVISION); -} diff --git a/main/src/main/cpp/opvpnutil/jniglue.h b/main/src/main/cpp/opvpnutil/jniglue.h deleted file mode 100644 index 8f813b64..00000000 --- a/main/src/main/cpp/opvpnutil/jniglue.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// jniglue.h -// xcopenvpn -// -// Created by Arne Schwabe on 29.03.12. -// Copyright (c) 2012 Universität Paderborn. All rights reserved. -// - -#ifndef xcopenvpn_jniglue_h -#define xcopenvpn_jniglue_h -void android_openvpn_log(int level,const char* prefix,const char* prefix_sep,const char* m1); -#endif - -#ifdef __cplusplus -extern "C" { -#endif - int jniThrowException(JNIEnv* env, const char* className, const char* msg); - -#ifdef __cplusplus -} -#endif diff --git a/main/src/main/cpp/opvpnutil/scan_ifs.c b/main/src/main/cpp/opvpnutil/scan_ifs.c deleted file mode 100644 index 85021d88..00000000 --- a/main/src/main/cpp/opvpnutil/scan_ifs.c +++ /dev/null @@ -1,109 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "jniglue.h" - -jobjectArray Java_de_blinkt_openvpn_core_NativeUtils_getIfconfig(JNIEnv* env) -{ - int sd; - if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "Opening socket for intface get failed"); - //jniThrowException(env, "java/lang/IllegalArgumentException", "Opening socket for intface get failed"); - return NULL; - } - - struct ifreq ifs[23]; - - struct ifconf ifc; - ifc.ifc_req = ifs; - ifc.ifc_len = sizeof (ifs); - - if (ioctl (sd, SIOCGIFCONF, &ifc) < 0) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "IOCTL for intface get failed"); - //jniThrowException(env, "java/lang/IllegalArgumentException", "IOTCL socket for intface get failed"); - close(sd); - return NULL; - } - - - - - char buf[NI_MAXHOST]; - - int ji=0; - - /* - jtmp = (*env)->NewStringUTF(env, "HALLO WELT"); - (*env)->SetObjectArrayElement(env, ret, ji++, jtmp); - */ - - size_t num_intf=ifc.ifc_len / sizeof(struct ifreq); - jobjectArray ret= (jobjectArray) (*env)->NewObjectArray(env, num_intf*3,(*env)->FindClass(env, "java/lang/String"), NULL); - - for (struct ifreq* ifr = ifc.ifc_req; ifr < ifs + num_intf; ifr++) { - - if (ifr->ifr_addr.sa_family != AF_INET) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "NOT AF_INET: %s", ifr->ifr_name); - continue; - } - - /* get interface addr, prefilled by SIOGIFCONF */ - - int err; - if ((err=getnameinfo(&ifr->ifr_addr, sizeof(struct sockaddr_in), buf, NI_MAXHOST, NULL, 0, - NI_NUMERICHOST)) !=0) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "getnameinfo failed for %s: %s", ifr->ifr_name, gai_strerror(err)); - continue; - } - jstring jaddr = (*env)->NewStringUTF(env, buf); - jstring jname = (*env)->NewStringUTF(env, ifr->ifr_name); - - - struct ifreq ifreq; - strncpy (ifreq.ifr_name, ifr->ifr_name, sizeof (ifreq.ifr_name)); - - /* interface is up */ - if (ioctl (sd, SIOCGIFFLAGS, &ifreq) < 0) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "SIOCGIFFLAGS failed for %s: %s", ifr->ifr_name, strerror(errno)); - continue; - } - - if (!(ifreq.ifr_flags & IFF_UP)) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "IFF_UP failed for %s", ifr->ifr_name); - continue; - } - - /* interface netmask */ - if (ioctl (sd, SIOCGIFNETMASK, &ifreq) < 0) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "SIOCIFNETMASK failed for %s: %s", ifr->ifr_name, strerror(errno)); - continue; - } - - if ((err=getnameinfo(&ifreq.ifr_netmask, sizeof(struct sockaddr_in), buf, NI_MAXHOST, NULL, 0, - NI_NUMERICHOST)) !=0) { - __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "getnameinfo failed for %s: %s", ifr->ifr_name, gai_strerror(err)); - continue; - } - jstring jnetmask = (*env)->NewStringUTF(env, buf); - - (*env)->SetObjectArrayElement(env, ret, ji++, jname); - (*env)->SetObjectArrayElement(env, ret, ji++, jaddr); - (*env)->SetObjectArrayElement(env, ret, ji++, jnetmask); - } - if (sd >= 0) - close (sd); - - return ret; -} - diff --git a/main/src/main/cpp/opvpnutil/sslspeed.c b/main/src/main/cpp/opvpnutil/sslspeed.c deleted file mode 100644 index 1881998e..00000000 --- a/main/src/main/cpp/opvpnutil/sslspeed.c +++ /dev/null @@ -1,315 +0,0 @@ -/* - * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. - * - * Licensed under the OpenSSL license (the "License"). You may not use - * this file except in compliance with the License. You can obtain a copy - * in the file LICENSE in the source distribution or at - * https://www.openssl.org/source/license.html - */ - -/* ==================================================================== - * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. - * - * Portions of the attached software ("Contribution") are developed by - * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. - * - * The Contribution is licensed pursuant to the OpenSSL open source - * license provided above. - * - * The ECDH and ECDSA speed test software is originally written by - * Sumit Gupta of Sun Microsystems Laboratories. - * - */ - -// Modified by Arne Schwabe to give a simple openssl evp speed java api - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "jniglue.h" -#include - -#include -#include -#include -#include -#include -#include - - -/* This file just contains code thrown together until it works */ - - -#undef SECONDS -#define SECONDS 3 -#define PRIME_SECONDS 10 -#define RSA_SECONDS 10 -#define DSA_SECONDS 10 -#define ECDSA_SECONDS 10 -#define ECDH_SECONDS 10 - - -typedef struct loopargs_st { - unsigned char *buf; - unsigned char *buf2; - unsigned char *buf_malloc; - unsigned char *buf2_malloc; - unsigned int siglen; - EVP_CIPHER_CTX *ctx; - HMAC_CTX *hctx; -} loopargs_t; - -#undef BUFSIZE -#define BUFSIZE (1024*16+1) -#define MAX_MISALIGNMENT 63 - - -#define MAX_BLOCK_SIZE 128 -static unsigned char iv[2 * MAX_BLOCK_SIZE / 8]; - -#define SIZE_NUM 6 -static const int lengths[SIZE_NUM] = { - 16, 64, 256, 1024, 8 * 1024, 16 * 1024 -}; - -static int testnum; - -# define COND(unused_cond) (run && count<0x7fffffff) - -static volatile int run = 0; - -#ifdef SIGALRM -# if defined(__STDC__) || defined(sgi) || defined(_AIX) -# define SIGRETTYPE void -# else -# define SIGRETTYPE int -# endif - - -#define START 0 -#define STOP 1 -#define TM_START 0 -#define TM_STOP 1 - -# include - -static int usertime = 1; - -double app_tminterval(int stop, int usertime) -{ - double ret = 0; - struct tms rus; - clock_t now = times(&rus); - static clock_t tmstart; - - if (usertime) - now = rus.tms_utime; - - if (stop == TM_START) - tmstart = now; - else { - long int tck = sysconf(_SC_CLK_TCK); - ret = (now - tmstart) / (double)tck; - } - - return (ret); -} - - - - -static double Time_F(int s) -{ - double ret = app_tminterval(s, usertime); - if (s == STOP) - alarm(0); - return ret; -} - -#endif - - -static long save_count = 0; -static int decrypt = 0; -static int EVP_Update_loop(void *args) -{ - loopargs_t *tempargs = *(loopargs_t **)args; - unsigned char *buf = tempargs->buf; - EVP_CIPHER_CTX *ctx = tempargs->ctx; - int outl, count; - - if (decrypt) - for (count = 0; COND(nb_iter); count++) - EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]); - else - for (count = 0; COND(nb_iter); count++) - EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]); - if (decrypt) - EVP_DecryptFinal_ex(ctx, buf, &outl); - else - EVP_EncryptFinal_ex(ctx, buf, &outl); - return count; -} - -static const EVP_MD *evp_md = NULL; -static int EVP_Digest_loop(void *args) -{ - loopargs_t *tempargs = *(loopargs_t **)args; - unsigned char *buf = tempargs->buf; - unsigned char md[EVP_MAX_MD_SIZE]; - int count; - - for (count = 0; COND(nb_iter); count++) { - if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL)) - return -1; - } - return count; -} - - -static int run_benchmark(int async_jobs, - int (*loop_function)(void *), loopargs_t *loopargs) -{ - int job_op_count = 0; - int total_op_count = 0; - int num_inprogress = 0; - int error = 0, i = 0, ret = 0; - OSSL_ASYNC_FD job_fd = 0; - size_t num_job_fds = 0; - - run = 1; - - if (async_jobs == 0) { - return loop_function((void *)&loopargs); - } - return 1234567; -} - - -static void* stop_run(void* arg) -{ - __android_log_write(ANDROID_LOG_DEBUG,"openvpn", "stop run thread started"); - sleep(3); - run=0; - __android_log_write(ANDROID_LOG_DEBUG,"openvpn", "stop run thread stopped"); - return NULL; -} - -jdoubleArray Java_de_blinkt_openvpn_core_NativeUtils_getOpenSSLSpeed(JNIEnv* env, jclass thiz, jstring algorithm, jint testnumber) -{ - static const unsigned char key16[16] = { - 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, - 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 - }; - const EVP_CIPHER *evp_cipher = NULL; - - const char* alg = (*env)->GetStringUTFChars( env, algorithm , NULL ) ; - - evp_cipher = EVP_get_cipherbyname(alg); - if (evp_cipher == NULL) - evp_md = EVP_get_digestbyname(alg); - if (evp_cipher == NULL && evp_md == NULL) { - // BIO_printf(bio_err, "%s: %s is an unknown cipher or digest\n", prog, opt_arg()); - //jniThrowException(env, "java/security/NoSuchAlgorithmException", "Algorithm not found"); - return NULL; - } - - - const char* name; - - loopargs_t *loopargs = NULL; - int loopargs_len = 1; - int async_jobs=0; - loopargs = malloc(loopargs_len * sizeof(loopargs_t)); - memset(loopargs, 0, loopargs_len * sizeof(loopargs_t)); - - - jdoubleArray ret = (*env)->NewDoubleArray(env, 3); - - if (testnum < 0 || testnum >= SIZE_NUM) - goto error; - - testnum = testnumber; - - - for (int i = 0; i < loopargs_len; i++) { - int misalign=0; - loopargs[i].buf_malloc = malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1); - loopargs[i].buf2_malloc = malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1); - /* Align the start of buffers on a 64 byte boundary */ - loopargs[i].buf = loopargs[i].buf_malloc + misalign; - loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign; - } - - - int count; - float d; - if (evp_cipher) { - name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)); - /* - * -O3 -fschedule-insns messes up an optimization here! - * names[D_EVP] somehow becomes NULL - */ - - - for (int k = 0; k < loopargs_len; k++) { - loopargs[k].ctx = EVP_CIPHER_CTX_new(); - if (decrypt) - EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv); - else - EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv); - EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0); - } - - Time_F(START); - pthread_t timer_thread; - - if (pthread_create(&timer_thread, NULL, stop_run, NULL)) - goto error; - - count = run_benchmark(async_jobs, EVP_Update_loop, loopargs); - d = Time_F(STOP); - for (int k = 0; k < loopargs_len; k++) { - EVP_CIPHER_CTX_free(loopargs[k].ctx); - } - } - if (evp_md) { - name = OBJ_nid2ln(EVP_MD_type(evp_md)); - // print_message(names[D_EVP], save_count, lengths[testnum]); - - pthread_t timer_thread; - if (pthread_create(&timer_thread, NULL, stop_run, NULL)) - goto error; - - Time_F(START); - count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs); - d = Time_F(STOP); - } - - // Save results in hacky way - double results[] = {(double) lengths[testnum], (double) count, d}; - - - (*env)->SetDoubleArrayRegion(env, ret, 0, 3, results); - // print_result(D_EVP, testnum, count, d); - - - return ret; -error: - free(loopargs); - for (int k = 0; k < loopargs_len; k++) { - EVP_CIPHER_CTX_free(loopargs[k].ctx); - } - return NULL; -} diff --git a/main/src/main/cpp/ovpnutil/jniglue.c b/main/src/main/cpp/ovpnutil/jniglue.c new file mode 100644 index 00000000..264b18a8 --- /dev/null +++ b/main/src/main/cpp/ovpnutil/jniglue.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + + +#include "jniglue.h" + +jint JNI_OnLoad(JavaVM *vm, void *reserved) { +#ifndef NDEBUG + __android_log_write(ANDROID_LOG_DEBUG,"openvpn", "Loading openvpn native library $id$ compiled on " __DATE__ " " __TIME__ ); +#endif + return JNI_VERSION_1_2; +} + + +void android_openvpn_log(int level,const char* prefix,const char* prefix_sep,const char* m1) +{ + __android_log_print(ANDROID_LOG_DEBUG,"openvpn","%s%s%s",prefix,prefix_sep,m1); +} + +void Java_de_blinkt_openvpn_core_NativeUtils_jniclose(JNIEnv *env,jclass jo, jint fd) +{ + int ret = close(fd); +} + + +//! Hack to get the current installed ABI of the libraries. See also https://github.com/schwabe/ics-openvpn/issues/391 +jstring Java_de_blinkt_openvpn_core_NativeUtils_getJNIAPI(JNIEnv *env, jclass jo) +{ + + return (*env)->NewStringUTF(env, TARGET_ARCH_ABI); +} + +jstring Java_de_blinkt_openvpn_core_NativeUtils_getOpenVPN2GitVersion(JNIEnv *env, jclass jo) +{ + + return (*env)->NewStringUTF(env, OPENVPN2_GIT_REVISION); +} + +jstring Java_de_blinkt_openvpn_core_NativeUtils_getOpenVPN3GitVersion(JNIEnv *env, jclass jo) +{ + + return (*env)->NewStringUTF(env, OPENVPN3_GIT_REVISION); +} diff --git a/main/src/main/cpp/ovpnutil/jniglue.h b/main/src/main/cpp/ovpnutil/jniglue.h new file mode 100644 index 00000000..8f813b64 --- /dev/null +++ b/main/src/main/cpp/ovpnutil/jniglue.h @@ -0,0 +1,21 @@ +// +// jniglue.h +// xcopenvpn +// +// Created by Arne Schwabe on 29.03.12. +// Copyright (c) 2012 Universität Paderborn. All rights reserved. +// + +#ifndef xcopenvpn_jniglue_h +#define xcopenvpn_jniglue_h +void android_openvpn_log(int level,const char* prefix,const char* prefix_sep,const char* m1); +#endif + +#ifdef __cplusplus +extern "C" { +#endif + int jniThrowException(JNIEnv* env, const char* className, const char* msg); + +#ifdef __cplusplus +} +#endif diff --git a/main/src/main/cpp/ovpnutil/scan_ifs.c b/main/src/main/cpp/ovpnutil/scan_ifs.c new file mode 100644 index 00000000..85021d88 --- /dev/null +++ b/main/src/main/cpp/ovpnutil/scan_ifs.c @@ -0,0 +1,109 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "jniglue.h" + +jobjectArray Java_de_blinkt_openvpn_core_NativeUtils_getIfconfig(JNIEnv* env) +{ + int sd; + if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "Opening socket for intface get failed"); + //jniThrowException(env, "java/lang/IllegalArgumentException", "Opening socket for intface get failed"); + return NULL; + } + + struct ifreq ifs[23]; + + struct ifconf ifc; + ifc.ifc_req = ifs; + ifc.ifc_len = sizeof (ifs); + + if (ioctl (sd, SIOCGIFCONF, &ifc) < 0) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "IOCTL for intface get failed"); + //jniThrowException(env, "java/lang/IllegalArgumentException", "IOTCL socket for intface get failed"); + close(sd); + return NULL; + } + + + + + char buf[NI_MAXHOST]; + + int ji=0; + + /* + jtmp = (*env)->NewStringUTF(env, "HALLO WELT"); + (*env)->SetObjectArrayElement(env, ret, ji++, jtmp); + */ + + size_t num_intf=ifc.ifc_len / sizeof(struct ifreq); + jobjectArray ret= (jobjectArray) (*env)->NewObjectArray(env, num_intf*3,(*env)->FindClass(env, "java/lang/String"), NULL); + + for (struct ifreq* ifr = ifc.ifc_req; ifr < ifs + num_intf; ifr++) { + + if (ifr->ifr_addr.sa_family != AF_INET) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "NOT AF_INET: %s", ifr->ifr_name); + continue; + } + + /* get interface addr, prefilled by SIOGIFCONF */ + + int err; + if ((err=getnameinfo(&ifr->ifr_addr, sizeof(struct sockaddr_in), buf, NI_MAXHOST, NULL, 0, + NI_NUMERICHOST)) !=0) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "getnameinfo failed for %s: %s", ifr->ifr_name, gai_strerror(err)); + continue; + } + jstring jaddr = (*env)->NewStringUTF(env, buf); + jstring jname = (*env)->NewStringUTF(env, ifr->ifr_name); + + + struct ifreq ifreq; + strncpy (ifreq.ifr_name, ifr->ifr_name, sizeof (ifreq.ifr_name)); + + /* interface is up */ + if (ioctl (sd, SIOCGIFFLAGS, &ifreq) < 0) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "SIOCGIFFLAGS failed for %s: %s", ifr->ifr_name, strerror(errno)); + continue; + } + + if (!(ifreq.ifr_flags & IFF_UP)) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "IFF_UP failed for %s", ifr->ifr_name); + continue; + } + + /* interface netmask */ + if (ioctl (sd, SIOCGIFNETMASK, &ifreq) < 0) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "SIOCIFNETMASK failed for %s: %s", ifr->ifr_name, strerror(errno)); + continue; + } + + if ((err=getnameinfo(&ifreq.ifr_netmask, sizeof(struct sockaddr_in), buf, NI_MAXHOST, NULL, 0, + NI_NUMERICHOST)) !=0) { + __android_log_print(ANDROID_LOG_DEBUG, "openvpn", "getnameinfo failed for %s: %s", ifr->ifr_name, gai_strerror(err)); + continue; + } + jstring jnetmask = (*env)->NewStringUTF(env, buf); + + (*env)->SetObjectArrayElement(env, ret, ji++, jname); + (*env)->SetObjectArrayElement(env, ret, ji++, jaddr); + (*env)->SetObjectArrayElement(env, ret, ji++, jnetmask); + } + if (sd >= 0) + close (sd); + + return ret; +} + diff --git a/main/src/main/cpp/ovpnutil/sslspeed.c b/main/src/main/cpp/ovpnutil/sslspeed.c new file mode 100644 index 00000000..1881998e --- /dev/null +++ b/main/src/main/cpp/ovpnutil/sslspeed.c @@ -0,0 +1,315 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* ==================================================================== + * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. + * + * Portions of the attached software ("Contribution") are developed by + * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. + * + * The Contribution is licensed pursuant to the OpenSSL open source + * license provided above. + * + * The ECDH and ECDSA speed test software is originally written by + * Sumit Gupta of Sun Microsystems Laboratories. + * + */ + +// Modified by Arne Schwabe to give a simple openssl evp speed java api + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "jniglue.h" +#include + +#include +#include +#include +#include +#include +#include + + +/* This file just contains code thrown together until it works */ + + +#undef SECONDS +#define SECONDS 3 +#define PRIME_SECONDS 10 +#define RSA_SECONDS 10 +#define DSA_SECONDS 10 +#define ECDSA_SECONDS 10 +#define ECDH_SECONDS 10 + + +typedef struct loopargs_st { + unsigned char *buf; + unsigned char *buf2; + unsigned char *buf_malloc; + unsigned char *buf2_malloc; + unsigned int siglen; + EVP_CIPHER_CTX *ctx; + HMAC_CTX *hctx; +} loopargs_t; + +#undef BUFSIZE +#define BUFSIZE (1024*16+1) +#define MAX_MISALIGNMENT 63 + + +#define MAX_BLOCK_SIZE 128 +static unsigned char iv[2 * MAX_BLOCK_SIZE / 8]; + +#define SIZE_NUM 6 +static const int lengths[SIZE_NUM] = { + 16, 64, 256, 1024, 8 * 1024, 16 * 1024 +}; + +static int testnum; + +# define COND(unused_cond) (run && count<0x7fffffff) + +static volatile int run = 0; + +#ifdef SIGALRM +# if defined(__STDC__) || defined(sgi) || defined(_AIX) +# define SIGRETTYPE void +# else +# define SIGRETTYPE int +# endif + + +#define START 0 +#define STOP 1 +#define TM_START 0 +#define TM_STOP 1 + +# include + +static int usertime = 1; + +double app_tminterval(int stop, int usertime) +{ + double ret = 0; + struct tms rus; + clock_t now = times(&rus); + static clock_t tmstart; + + if (usertime) + now = rus.tms_utime; + + if (stop == TM_START) + tmstart = now; + else { + long int tck = sysconf(_SC_CLK_TCK); + ret = (now - tmstart) / (double)tck; + } + + return (ret); +} + + + + +static double Time_F(int s) +{ + double ret = app_tminterval(s, usertime); + if (s == STOP) + alarm(0); + return ret; +} + +#endif + + +static long save_count = 0; +static int decrypt = 0; +static int EVP_Update_loop(void *args) +{ + loopargs_t *tempargs = *(loopargs_t **)args; + unsigned char *buf = tempargs->buf; + EVP_CIPHER_CTX *ctx = tempargs->ctx; + int outl, count; + + if (decrypt) + for (count = 0; COND(nb_iter); count++) + EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]); + else + for (count = 0; COND(nb_iter); count++) + EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]); + if (decrypt) + EVP_DecryptFinal_ex(ctx, buf, &outl); + else + EVP_EncryptFinal_ex(ctx, buf, &outl); + return count; +} + +static const EVP_MD *evp_md = NULL; +static int EVP_Digest_loop(void *args) +{ + loopargs_t *tempargs = *(loopargs_t **)args; + unsigned char *buf = tempargs->buf; + unsigned char md[EVP_MAX_MD_SIZE]; + int count; + + for (count = 0; COND(nb_iter); count++) { + if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL)) + return -1; + } + return count; +} + + +static int run_benchmark(int async_jobs, + int (*loop_function)(void *), loopargs_t *loopargs) +{ + int job_op_count = 0; + int total_op_count = 0; + int num_inprogress = 0; + int error = 0, i = 0, ret = 0; + OSSL_ASYNC_FD job_fd = 0; + size_t num_job_fds = 0; + + run = 1; + + if (async_jobs == 0) { + return loop_function((void *)&loopargs); + } + return 1234567; +} + + +static void* stop_run(void* arg) +{ + __android_log_write(ANDROID_LOG_DEBUG,"openvpn", "stop run thread started"); + sleep(3); + run=0; + __android_log_write(ANDROID_LOG_DEBUG,"openvpn", "stop run thread stopped"); + return NULL; +} + +jdoubleArray Java_de_blinkt_openvpn_core_NativeUtils_getOpenSSLSpeed(JNIEnv* env, jclass thiz, jstring algorithm, jint testnumber) +{ + static const unsigned char key16[16] = { + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, + 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 + }; + const EVP_CIPHER *evp_cipher = NULL; + + const char* alg = (*env)->GetStringUTFChars( env, algorithm , NULL ) ; + + evp_cipher = EVP_get_cipherbyname(alg); + if (evp_cipher == NULL) + evp_md = EVP_get_digestbyname(alg); + if (evp_cipher == NULL && evp_md == NULL) { + // BIO_printf(bio_err, "%s: %s is an unknown cipher or digest\n", prog, opt_arg()); + //jniThrowException(env, "java/security/NoSuchAlgorithmException", "Algorithm not found"); + return NULL; + } + + + const char* name; + + loopargs_t *loopargs = NULL; + int loopargs_len = 1; + int async_jobs=0; + loopargs = malloc(loopargs_len * sizeof(loopargs_t)); + memset(loopargs, 0, loopargs_len * sizeof(loopargs_t)); + + + jdoubleArray ret = (*env)->NewDoubleArray(env, 3); + + if (testnum < 0 || testnum >= SIZE_NUM) + goto error; + + testnum = testnumber; + + + for (int i = 0; i < loopargs_len; i++) { + int misalign=0; + loopargs[i].buf_malloc = malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1); + loopargs[i].buf2_malloc = malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1); + /* Align the start of buffers on a 64 byte boundary */ + loopargs[i].buf = loopargs[i].buf_malloc + misalign; + loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign; + } + + + int count; + float d; + if (evp_cipher) { + name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)); + /* + * -O3 -fschedule-insns messes up an optimization here! + * names[D_EVP] somehow becomes NULL + */ + + + for (int k = 0; k < loopargs_len; k++) { + loopargs[k].ctx = EVP_CIPHER_CTX_new(); + if (decrypt) + EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv); + else + EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv); + EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0); + } + + Time_F(START); + pthread_t timer_thread; + + if (pthread_create(&timer_thread, NULL, stop_run, NULL)) + goto error; + + count = run_benchmark(async_jobs, EVP_Update_loop, loopargs); + d = Time_F(STOP); + for (int k = 0; k < loopargs_len; k++) { + EVP_CIPHER_CTX_free(loopargs[k].ctx); + } + } + if (evp_md) { + name = OBJ_nid2ln(EVP_MD_type(evp_md)); + // print_message(names[D_EVP], save_count, lengths[testnum]); + + pthread_t timer_thread; + if (pthread_create(&timer_thread, NULL, stop_run, NULL)) + goto error; + + Time_F(START); + count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs); + d = Time_F(STOP); + } + + // Save results in hacky way + double results[] = {(double) lengths[testnum], (double) count, d}; + + + (*env)->SetDoubleArrayRegion(env, ret, 0, 3, results); + // print_result(D_EVP, testnum, count, d); + + + return ret; +error: + free(loopargs); + for (int k = 0; k < loopargs_len; k++) { + EVP_CIPHER_CTX_free(loopargs[k].ctx); + } + return NULL; +} diff --git a/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java b/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java index d6c1cdb9..d8315c9f 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java +++ b/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java @@ -39,7 +39,7 @@ public class NativeUtils { static { if (!isRoboUnitTest()) { - System.loadLibrary("opvpnutil"); + System.loadLibrary("ovpnutil"); if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) System.loadLibrary("jbcrypto"); -- cgit v1.2.3