1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <jni.h>
18
19 #include <conscrypt/compatibility_close_monitor.h>
20 #include <conscrypt/jniutil.h>
21 #include <conscrypt/logging.h>
22 #include <conscrypt/native_crypto.h>
23
24 #ifndef CONSCRYPT_JNI_VERSION
25 #define CONSCRYPT_JNI_VERSION JNI_VERSION_1_6
26 #endif // !CONSCRYPT_JNI_VERSION
27
28 using conscrypt::CompatibilityCloseMonitor;
29 using conscrypt::NativeCrypto;
30
31 // Give client libs everything they need to initialize our JNI
libconscrypt_JNI_OnLoad(JavaVM * vm,void *)32 jint libconscrypt_JNI_OnLoad(JavaVM* vm, void*) {
33 JNIEnv* env;
34 if (vm->GetEnv(reinterpret_cast<void**>(&env), CONSCRYPT_JNI_VERSION) != JNI_OK) {
35 CONSCRYPT_LOG_ERROR("Could not get JNIEnv");
36 return JNI_ERR;
37 }
38
39 // Initialize the JNI constants.
40 conscrypt::jniutil::init(vm, env);
41
42 // Register all of the native JNI methods.
43 NativeCrypto::registerNativeMethods(env);
44
45 // Perform static initialization of the close monitor (if required on this platform).
46 CompatibilityCloseMonitor::init();
47 return CONSCRYPT_JNI_VERSION;
48 }
49
50 #ifdef STATIC_LIB
51
52 // A version of OnLoad called when the Conscrypt library has been statically linked to the JVM (For
53 // Java >= 1.8). The manner in which the library is statically linked is implementation specific.
54 //
55 // See http://openjdk.java.net/jeps/178
JNI_OnLoad_conscrypt(JavaVM * vm,void * reserved)56 CONSCRYPT_PUBLIC jint JNI_OnLoad_conscrypt(JavaVM* vm, void* reserved) {
57 return libconscrypt_JNI_OnLoad(vm, reserved);
58 }
59
60 #else // !STATIC_LIB
61
62 // Method called by the JVM when the Conscrypt shared library is loaded.
JNI_OnLoad(JavaVM * vm,void * reserved)63 CONSCRYPT_PUBLIC jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
64 return libconscrypt_JNI_OnLoad(vm, reserved);
65 }
66
67 #endif // !STATIC_LIB
68