0N/A/*
4320N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A
0N/A// #define SECMOD_DEBUG
0N/A
0N/A#include "j2secmod.h"
0N/A
0N/A
0N/AJNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssVersionCheck
0N/A (JNIEnv *env, jclass thisClass, jlong jHandle, jstring jVersion)
0N/A{
0N/A const char *requiredVersion = (*env)->GetStringUTFChars(env, jVersion, NULL);
0N/A int res;
0N/A FPTR_VersionCheck versionCheck =
0N/A (FPTR_VersionCheck)findFunction(env, jHandle, "NSS_VersionCheck");
0N/A
0N/A if (versionCheck == NULL) {
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A res = versionCheck(requiredVersion);
0N/A dprintf2("-version >=%s: %d\n", requiredVersion, res);
0N/A (*env)->ReleaseStringUTFChars(env, jVersion, requiredVersion);
0N/A
0N/A return (res == 0) ? JNI_FALSE : JNI_TRUE;
0N/A}
0N/A
0N/AJNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssInit
0N/A (JNIEnv *env, jclass thisClass, jstring jFunctionName, jlong jHandle, jstring jConfigDir)
0N/A{
0N/A const char *functionName = (*env)->GetStringUTFChars(env, jFunctionName, NULL);
0N/A const char *configDir = (jConfigDir == NULL) ? NULL : (*env)->GetStringUTFChars(env, jConfigDir, NULL);
0N/A FPTR_Init init = (FPTR_Init)findFunction(env, jHandle, functionName);
0N/A int res;
0N/A
0N/A (*env)->ReleaseStringUTFChars(env, jFunctionName, functionName);
0N/A if (init == NULL) {
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A res = init(configDir);
0N/A if (configDir != NULL) {
0N/A (*env)->ReleaseStringUTFChars(env, jConfigDir, configDir);
0N/A }
0N/A dprintf1("-res: %d\n", res);
0N/A
0N/A return (res == 0) ? JNI_TRUE : JNI_FALSE;
0N/A}
0N/A
0N/AJNIEXPORT jobject JNICALL Java_sun_security_pkcs11_Secmod_nssGetModuleList
4320N/A (JNIEnv *env, jclass thisClass, jlong jHandle, jstring jLibDir)
0N/A{
0N/A FPTR_GetDBModuleList getModuleList =
0N/A (FPTR_GetDBModuleList)findFunction(env, jHandle, "SECMOD_GetDefaultModuleList");
0N/A
0N/A SECMODModuleList *list;
0N/A SECMODModule *module;
0N/A jclass jListClass, jModuleClass;
0N/A jobject jList, jModule;
0N/A jmethodID jListConstructor, jAdd, jModuleConstructor;
0N/A jstring jCommonName, jDllName;
0N/A jboolean jFIPS;
0N/A jint i;
0N/A
0N/A if (getModuleList == NULL) {
0N/A dprintf("-getmodulelist function not found\n");
0N/A return NULL;
0N/A }
0N/A list = getModuleList();
0N/A if (list == NULL) {
0N/A dprintf("-module list is null\n");
0N/A return NULL;
0N/A }
0N/A
0N/A jListClass = (*env)->FindClass(env, "java/util/ArrayList");
0N/A jListConstructor = (*env)->GetMethodID(env, jListClass, "<init>", "()V");
0N/A jAdd = (*env)->GetMethodID(env, jListClass, "add", "(Ljava/lang/Object;)Z");
0N/A jList = (*env)->NewObject(env, jListClass, jListConstructor);
0N/A
0N/A jModuleClass = (*env)->FindClass(env, "sun/security/pkcs11/Secmod$Module");
4320N/A jModuleConstructor = (*env)->GetMethodID(env, jModuleClass, "<init>",
4320N/A "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZI)V");
0N/A
0N/A while (list != NULL) {
0N/A module = list->module;
0N/A // assert module != null
0N/A dprintf1("-commonname: %s\n", module->commonName);
0N/A dprintf1("-dllname: %s\n", (module->dllName != NULL) ? module->dllName : "NULL");
0N/A dprintf1("-slots: %d\n", module->slotCount);
0N/A dprintf1("-loaded: %d\n", module->loaded);
0N/A dprintf1("-internal: %d\n", module->internal);
0N/A dprintf1("-fips: %d\n", module->isFIPS);
0N/A jCommonName = (*env)->NewStringUTF(env, module->commonName);
0N/A if (module->dllName == NULL) {
0N/A jDllName = NULL;
0N/A } else {
0N/A jDllName = (*env)->NewStringUTF(env, module->dllName);
0N/A }
0N/A jFIPS = module->isFIPS;
0N/A for (i = 0; i < module->slotCount; i++ ) {
4320N/A jModule = (*env)->NewObject(env, jModuleClass, jModuleConstructor,
4320N/A jLibDir, jDllName, jCommonName, jFIPS, i);
0N/A (*env)->CallVoidMethod(env, jList, jAdd, jModule);
0N/A }
0N/A list = list->next;
0N/A }
0N/A dprintf("-ok\n");
0N/A
0N/A return jList;
0N/A}