1 /* 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 30 // #define SECMOD_DEBUG 31 32 #include "j2secmod.h" 33 34 35 JNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssVersionCheck 36 (JNIEnv *env, jclass thisClass, jlong jHandle, jstring jVersion) 37 { 38 const char *requiredVersion = (*env)->GetStringUTFChars(env, jVersion, NULL); 39 int res; 40 FPTR_VersionCheck versionCheck = 41 (FPTR_VersionCheck)findFunction(env, jHandle, "NSS_VersionCheck"); 42 43 if (versionCheck == NULL) { 44 return JNI_FALSE; 45 } 46 47 res = versionCheck(requiredVersion); 48 dprintf2("-version >=%s: %d\n", requiredVersion, res); 49 (*env)->ReleaseStringUTFChars(env, jVersion, requiredVersion); 50 51 return (res == 0) ? JNI_FALSE : JNI_TRUE; 52 } 53 54 JNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssInit 55 (JNIEnv *env, jclass thisClass, jstring jFunctionName, jlong jHandle, jstring jConfigDir) 56 { 57 const char *functionName = (*env)->GetStringUTFChars(env, jFunctionName, NULL); 58 const char *configDir = (jConfigDir == NULL) ? NULL : (*env)->GetStringUTFChars(env, jConfigDir, NULL); 59 FPTR_Init init = (FPTR_Init)findFunction(env, jHandle, functionName); 60 int res; 61 62 (*env)->ReleaseStringUTFChars(env, jFunctionName, functionName); 63 if (init == NULL) { 64 return JNI_FALSE; 65 } 66 67 res = init(configDir); 68 if (configDir != NULL) { 69 (*env)->ReleaseStringUTFChars(env, jConfigDir, configDir); 70 } 71 dprintf1("-res: %d\n", res); 72 73 return (res == 0) ? JNI_TRUE : JNI_FALSE; 74 } 75 76 JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_Secmod_nssGetModuleList 77 (JNIEnv *env, jclass thisClass, jlong jHandle, jstring jLibDir) 78 { 79 FPTR_GetDBModuleList getModuleList = 80 (FPTR_GetDBModuleList)findFunction(env, jHandle, "SECMOD_GetDefaultModuleList"); 81 82 SECMODModuleList *list; 83 SECMODModule *module; 84 jclass jListClass, jModuleClass; 85 jobject jList, jModule; 86 jmethodID jListConstructor, jAdd, jModuleConstructor; 87 jstring jCommonName, jDllName; 88 jboolean jFIPS; 89 jint i; 90 91 if (getModuleList == NULL) { 92 dprintf("-getmodulelist function not found\n"); 93 return NULL; 94 } 95 list = getModuleList(); 96 if (list == NULL) { 97 dprintf("-module list is null\n"); 98 return NULL; 99 } 100 101 jListClass = (*env)->FindClass(env, "java/util/ArrayList"); 102 jListConstructor = (*env)->GetMethodID(env, jListClass, "<init>", "()V"); 103 jAdd = (*env)->GetMethodID(env, jListClass, "add", "(Ljava/lang/Object;)Z"); 104 jList = (*env)->NewObject(env, jListClass, jListConstructor); 105 106 jModuleClass = (*env)->FindClass(env, "sun/security/pkcs11/Secmod$Module"); 107 jModuleConstructor = (*env)->GetMethodID(env, jModuleClass, "<init>", 108 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZI)V"); 109 110 while (list != NULL) { 111 module = list->module; 112 // assert module != null 113 dprintf1("-commonname: %s\n", module->commonName); 114 dprintf1("-dllname: %s\n", (module->dllName != NULL) ? module->dllName : "NULL"); 115 dprintf1("-slots: %d\n", module->slotCount); 116 dprintf1("-loaded: %d\n", module->loaded); 117 dprintf1("-internal: %d\n", module->internal); 118 dprintf1("-fips: %d\n", module->isFIPS); 119 jCommonName = (*env)->NewStringUTF(env, module->commonName); 120 if (module->dllName == NULL) { 121 jDllName = NULL; 122 } else { 123 jDllName = (*env)->NewStringUTF(env, module->dllName); 124 } 125 jFIPS = module->isFIPS; 126 for (i = 0; i < module->slotCount; i++ ) { 127 jModule = (*env)->NewObject(env, jModuleClass, jModuleConstructor, 128 jLibDir, jDllName, jCommonName, jFIPS, i); 129 (*env)->CallVoidMethod(env, jList, jAdd, jModule); 130 } 131 list = list->next; 132 } 133 dprintf("-ok\n"); 134 135 return jList; 136 }