|
1 /* |
|
2 * Copyright (c) 2005, 2014, 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 #include "jni_util.h" |
|
34 |
|
35 |
|
36 JNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssVersionCheck |
|
37 (JNIEnv *env, jclass thisClass, jlong jHandle, jstring jVersion) |
|
38 { |
|
39 int res = 0; |
|
40 FPTR_VersionCheck versionCheck; |
|
41 const char *requiredVersion; |
|
42 |
|
43 versionCheck = (FPTR_VersionCheck)findFunction(env, jHandle, |
|
44 "NSS_VersionCheck"); |
|
45 if (versionCheck == NULL) { |
|
46 return JNI_FALSE; |
|
47 } |
|
48 |
|
49 requiredVersion = (*env)->GetStringUTFChars(env, jVersion, NULL); |
|
50 if (requiredVersion == NULL) { |
|
51 return JNI_FALSE; |
|
52 } |
|
53 |
|
54 res = versionCheck(requiredVersion); |
|
55 dprintf2("-version >=%s: %d\n", requiredVersion, res); |
|
56 (*env)->ReleaseStringUTFChars(env, jVersion, requiredVersion); |
|
57 |
|
58 return (res == 0) ? JNI_FALSE : JNI_TRUE; |
|
59 } |
|
60 |
|
61 /* |
|
62 * Initializes NSS. |
|
63 * The NSS_INIT_OPTIMIZESPACE flag is supplied by the caller. |
|
64 * The NSS_Init* functions are mapped to the NSS_Initialize function. |
|
65 */ |
|
66 JNIEXPORT jboolean JNICALL Java_sun_security_pkcs11_Secmod_nssInitialize |
|
67 (JNIEnv *env, jclass thisClass, jstring jFunctionName, jlong jHandle, jstring jConfigDir, jboolean jNssOptimizeSpace) |
|
68 { |
|
69 int res = 0; |
|
70 FPTR_Initialize initialize = |
|
71 (FPTR_Initialize)findFunction(env, jHandle, "NSS_Initialize"); |
|
72 unsigned int flags = 0x00; |
|
73 const char *configDir = NULL; |
|
74 const char *functionName = NULL; |
|
75 |
|
76 /* If we cannot initialize, exit now */ |
|
77 if (initialize == NULL) { |
|
78 res = 1; |
|
79 goto cleanup; |
|
80 } |
|
81 |
|
82 functionName = (*env)->GetStringUTFChars(env, jFunctionName, NULL); |
|
83 if (functionName == NULL) { |
|
84 res = 1; |
|
85 goto cleanup; |
|
86 } |
|
87 |
|
88 if (jConfigDir != NULL) { |
|
89 configDir = (*env)->GetStringUTFChars(env, jConfigDir, NULL); |
|
90 if (!configDir) { |
|
91 res = 1; |
|
92 goto cleanup; |
|
93 } |
|
94 } |
|
95 |
|
96 if (jNssOptimizeSpace == JNI_TRUE) { |
|
97 flags = 0x20; // NSS_INIT_OPTIMIZESPACE flag |
|
98 } |
|
99 |
|
100 /* |
|
101 * If the NSS_Init function is requested then call NSS_Initialize to |
|
102 * open the Cert, Key and Security Module databases, read only. |
|
103 */ |
|
104 if (strcmp("NSS_Init", functionName) == 0) { |
|
105 flags = flags | 0x01; // NSS_INIT_READONLY flag |
|
106 res = initialize(configDir, "", "", "secmod.db", flags); |
|
107 |
|
108 /* |
|
109 * If the NSS_InitReadWrite function is requested then call |
|
110 * NSS_Initialize to open the Cert, Key and Security Module databases, |
|
111 * read/write. |
|
112 */ |
|
113 } else if (strcmp("NSS_InitReadWrite", functionName) == 0) { |
|
114 res = initialize(configDir, "", "", "secmod.db", flags); |
|
115 |
|
116 /* |
|
117 * If the NSS_NoDB_Init function is requested then call |
|
118 * NSS_Initialize without creating Cert, Key or Security Module |
|
119 * databases. |
|
120 */ |
|
121 } else if (strcmp("NSS_NoDB_Init", functionName) == 0) { |
|
122 flags = flags | 0x02 // NSS_INIT_NOCERTDB flag |
|
123 | 0x04 // NSS_INIT_NOMODDB flag |
|
124 | 0x08 // NSS_INIT_FORCEOPEN flag |
|
125 | 0x10; // NSS_INIT_NOROOTINIT flag |
|
126 res = initialize("", "", "", "", flags); |
|
127 |
|
128 } else { |
|
129 res = 2; |
|
130 } |
|
131 |
|
132 cleanup: |
|
133 if (functionName != NULL) { |
|
134 (*env)->ReleaseStringUTFChars(env, jFunctionName, functionName); |
|
135 } |
|
136 if (configDir != NULL) { |
|
137 (*env)->ReleaseStringUTFChars(env, jConfigDir, configDir); |
|
138 } |
|
139 dprintf1("-res: %d\n", res); |
|
140 |
|
141 return (res == 0) ? JNI_TRUE : JNI_FALSE; |
|
142 } |
|
143 |
|
144 JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_Secmod_nssGetModuleList |
|
145 (JNIEnv *env, jclass thisClass, jlong jHandle, jstring jLibDir) |
|
146 { |
|
147 FPTR_GetDBModuleList getModuleList = |
|
148 (FPTR_GetDBModuleList)findFunction(env, jHandle, "SECMOD_GetDefaultModuleList"); |
|
149 |
|
150 SECMODModuleList *list; |
|
151 SECMODModule *module; |
|
152 jclass jListClass, jModuleClass; |
|
153 jobject jList, jModule; |
|
154 jmethodID jListConstructor, jAdd, jModuleConstructor; |
|
155 jstring jCommonName, jDllName; |
|
156 jboolean jFIPS; |
|
157 jint i; |
|
158 |
|
159 if (getModuleList == NULL) { |
|
160 dprintf("-getmodulelist function not found\n"); |
|
161 return NULL; |
|
162 } |
|
163 list = getModuleList(); |
|
164 if (list == NULL) { |
|
165 dprintf("-module list is null\n"); |
|
166 return NULL; |
|
167 } |
|
168 |
|
169 jListClass = (*env)->FindClass(env, "java/util/ArrayList"); |
|
170 if (jListClass == NULL) { |
|
171 return NULL; |
|
172 } |
|
173 jListConstructor = (*env)->GetMethodID(env, jListClass, "<init>", "()V"); |
|
174 if (jListConstructor == NULL) { |
|
175 return NULL; |
|
176 } |
|
177 jAdd = (*env)->GetMethodID(env, jListClass, "add", "(Ljava/lang/Object;)Z"); |
|
178 if (jAdd == NULL) { |
|
179 return NULL; |
|
180 } |
|
181 jList = (*env)->NewObject(env, jListClass, jListConstructor); |
|
182 if (jList == NULL) { |
|
183 return NULL; |
|
184 } |
|
185 jModuleClass = (*env)->FindClass(env, "sun/security/pkcs11/Secmod$Module"); |
|
186 if (jModuleClass == NULL) { |
|
187 return NULL; |
|
188 } |
|
189 jModuleConstructor = (*env)->GetMethodID(env, jModuleClass, "<init>", |
|
190 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZI)V"); |
|
191 if (jModuleConstructor == NULL) { |
|
192 return NULL; |
|
193 } |
|
194 |
|
195 while (list != NULL) { |
|
196 module = list->module; |
|
197 // assert module != null |
|
198 dprintf1("-commonname: %s\n", module->commonName); |
|
199 dprintf1("-dllname: %s\n", (module->dllName != NULL) ? module->dllName : "NULL"); |
|
200 dprintf1("-slots: %d\n", module->slotCount); |
|
201 dprintf1("-loaded: %d\n", module->loaded); |
|
202 dprintf1("-internal: %d\n", module->internal); |
|
203 dprintf1("-fips: %d\n", module->isFIPS); |
|
204 jCommonName = (*env)->NewStringUTF(env, module->commonName); |
|
205 if (jCommonName == NULL) { |
|
206 return NULL; |
|
207 } |
|
208 if (module->dllName == NULL) { |
|
209 jDllName = NULL; |
|
210 } else { |
|
211 jDllName = (*env)->NewStringUTF(env, module->dllName); |
|
212 if (jDllName == NULL) { |
|
213 return NULL; |
|
214 } |
|
215 } |
|
216 jFIPS = module->isFIPS; |
|
217 for (i = 0; i < module->slotCount; i++ ) { |
|
218 jModule = (*env)->NewObject(env, jModuleClass, jModuleConstructor, |
|
219 jLibDir, jDllName, jCommonName, jFIPS, i); |
|
220 if (jModule == NULL) { |
|
221 return NULL; |
|
222 } |
|
223 (*env)->CallVoidMethod(env, jList, jAdd, jModule); |
|
224 if ((*env)->ExceptionCheck(env)) { |
|
225 return NULL; |
|
226 } |
|
227 } |
|
228 list = list->next; |
|
229 } |
|
230 dprintf("-ok\n"); |
|
231 |
|
232 return jList; |
|
233 } |