Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/pkcs11/j2secmod.c
38918 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <stdlib.h>27#include <string.h>2829// #define SECMOD_DEBUG3031#include "j2secmod.h"32#include "jni_util.h"333435JNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssVersionCheck36(JNIEnv *env, jclass thisClass, jlong jHandle, jstring jVersion)37{38int res = 0;39FPTR_VersionCheck versionCheck;40const char *requiredVersion;4142versionCheck = (FPTR_VersionCheck)findFunction(env, jHandle,43"NSS_VersionCheck");44if (versionCheck == NULL) {45return JNI_FALSE;46}4748requiredVersion = (*env)->GetStringUTFChars(env, jVersion, NULL);49if (requiredVersion == NULL) {50return JNI_FALSE;51}5253res = versionCheck(requiredVersion);54dprintf2("-version >=%s: %d\n", requiredVersion, res);55(*env)->ReleaseStringUTFChars(env, jVersion, requiredVersion);5657return (res == 0) ? JNI_FALSE : JNI_TRUE;58}5960/*61* Initializes NSS.62* The NSS_INIT_OPTIMIZESPACE flag is supplied by the caller.63* The NSS_Init* functions are mapped to the NSS_Initialize function.64*/65JNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssInitialize66(JNIEnv *env, jclass thisClass, jstring jFunctionName, jlong jHandle, jstring jConfigDir, jboolean jNssOptimizeSpace)67{68int res = 0;69FPTR_Initialize initialize =70(FPTR_Initialize)findFunction(env, jHandle, "NSS_Initialize");71unsigned int flags = 0x00;72const char *configDir = NULL;73const char *functionName = NULL;7475/* If we cannot initialize, exit now */76if (initialize == NULL) {77res = 1;78goto cleanup;79}8081functionName = (*env)->GetStringUTFChars(env, jFunctionName, NULL);82if (functionName == NULL) {83res = 1;84goto cleanup;85}8687if (jConfigDir != NULL) {88configDir = (*env)->GetStringUTFChars(env, jConfigDir, NULL);89if (!configDir) {90res = 1;91goto cleanup;92}93}9495if (jNssOptimizeSpace == JNI_TRUE) {96flags = 0x20; // NSS_INIT_OPTIMIZESPACE flag97}9899/*100* If the NSS_Init function is requested then call NSS_Initialize to101* open the Cert, Key and Security Module databases, read only.102*/103if (strcmp("NSS_Init", functionName) == 0) {104flags = flags | 0x01; // NSS_INIT_READONLY flag105res = initialize(configDir, "", "", "secmod.db", flags);106107/*108* If the NSS_InitReadWrite function is requested then call109* NSS_Initialize to open the Cert, Key and Security Module databases,110* read/write.111*/112} else if (strcmp("NSS_InitReadWrite", functionName) == 0) {113res = initialize(configDir, "", "", "secmod.db", flags);114115/*116* If the NSS_NoDB_Init function is requested then call117* NSS_Initialize without creating Cert, Key or Security Module118* databases.119*/120} else if (strcmp("NSS_NoDB_Init", functionName) == 0) {121flags = flags | 0x02 // NSS_INIT_NOCERTDB flag122| 0x04 // NSS_INIT_NOMODDB flag123| 0x08 // NSS_INIT_FORCEOPEN flag124| 0x10; // NSS_INIT_NOROOTINIT flag125res = initialize("", "", "", "", flags);126127} else {128res = 2;129}130131cleanup:132if (functionName != NULL) {133(*env)->ReleaseStringUTFChars(env, jFunctionName, functionName);134}135if (configDir != NULL) {136(*env)->ReleaseStringUTFChars(env, jConfigDir, configDir);137}138dprintf1("-res: %d\n", res);139140return (res == 0) ? JNI_TRUE : JNI_FALSE;141}142143JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_Secmod_nssGetModuleList144(JNIEnv *env, jclass thisClass, jlong jHandle, jstring jLibDir)145{146FPTR_GetDBModuleList getModuleList =147(FPTR_GetDBModuleList)findFunction(env, jHandle, "SECMOD_GetDefaultModuleList");148149SECMODModuleList *list;150SECMODModule *module;151jclass jListClass, jModuleClass;152jobject jList, jModule;153jmethodID jListConstructor, jAdd, jModuleConstructor;154jstring jCommonName, jDllName;155jboolean jFIPS;156jint i;157158if (getModuleList == NULL) {159dprintf("-getmodulelist function not found\n");160return NULL;161}162list = getModuleList();163if (list == NULL) {164dprintf("-module list is null\n");165return NULL;166}167168jListClass = (*env)->FindClass(env, "java/util/ArrayList");169if (jListClass == NULL) {170return NULL;171}172jListConstructor = (*env)->GetMethodID(env, jListClass, "<init>", "()V");173if (jListConstructor == NULL) {174return NULL;175}176jAdd = (*env)->GetMethodID(env, jListClass, "add", "(Ljava/lang/Object;)Z");177if (jAdd == NULL) {178return NULL;179}180jList = (*env)->NewObject(env, jListClass, jListConstructor);181if (jList == NULL) {182return NULL;183}184jModuleClass = (*env)->FindClass(env, "sun/security/pkcs11/Secmod$Module");185if (jModuleClass == NULL) {186return NULL;187}188jModuleConstructor = (*env)->GetMethodID(env, jModuleClass, "<init>",189"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZI)V");190if (jModuleConstructor == NULL) {191return NULL;192}193194while (list != NULL) {195module = list->module;196// assert module != null197dprintf1("-commonname: %s\n", module->commonName);198dprintf1("-dllname: %s\n", (module->dllName != NULL) ? module->dllName : "NULL");199dprintf1("-slots: %d\n", module->slotCount);200dprintf1("-loaded: %d\n", module->loaded);201dprintf1("-internal: %d\n", module->internal);202dprintf1("-fips: %d\n", module->isFIPS);203jCommonName = (*env)->NewStringUTF(env, module->commonName);204if (jCommonName == NULL) {205return NULL;206}207if (module->dllName == NULL) {208jDllName = NULL;209} else {210jDllName = (*env)->NewStringUTF(env, module->dllName);211if (jDllName == NULL) {212return NULL;213}214}215jFIPS = module->isFIPS;216for (i = 0; i < module->slotCount; i++ ) {217jModule = (*env)->NewObject(env, jModuleClass, jModuleConstructor,218jLibDir, jDllName, jCommonName, jFIPS, i);219if (jModule == NULL) {220return NULL;221}222(*env)->CallVoidMethod(env, jList, jAdd, jModule);223if ((*env)->ExceptionCheck(env)) {224return NULL;225}226}227list = list->next;228}229dprintf("-ok\n");230231return jList;232}233234235