144 }
145
146 /**
147 * Initialize this Secmod.
148 *
149 * @param configDir the directory containing the NSS configuration
150 * files such as secmod.db
151 * @param nssLibDir the directory containing the NSS libraries
152 * (libnss3.so or nss3.dll) or null if the library is on
153 * the system default shared library path
154 *
155 * @throws IOException if NSS has already been initialized,
156 * the specified directories are invalid, or initialization
157 * fails for any other reason
158 */
159 public void initialize(String configDir, String nssLibDir)
160 throws IOException {
161 initialize(DbMode.READ_WRITE, configDir, nssLibDir);
162 }
163
164 public synchronized void initialize(DbMode dbMode, String configDir, String nssLibDir)
165 throws IOException {
166 if (isInitialized()) {
167 throw new IOException("NSS is already initialized");
168 }
169
170 if (dbMode == null) {
171 throw new NullPointerException();
172 }
173 if ((dbMode != DbMode.NO_DB) && (configDir == null)) {
174 throw new NullPointerException();
175 }
176 String platformLibName = System.mapLibraryName("nss3");
177 String platformPath;
178 if (nssLibDir == null) {
179 platformPath = platformLibName;
180 } else {
181 File base = new File(nssLibDir);
182 if (base.isDirectory() == false) {
183 throw new IOException("nssLibDir must be a directory:" + nssLibDir);
184 }
185 File platformFile = new File(base, platformLibName);
194 if (configBase.isDirectory() == false ) {
195 throw new IOException("configDir must be a directory: " + configDir);
196 }
197 File secmodFile = new File(configBase, "secmod.db");
198 if (secmodFile.isFile() == false) {
199 throw new FileNotFoundException(secmodFile.getPath());
200 }
201 }
202
203 if (DEBUG) System.out.println("lib: " + platformPath);
204 nssHandle = nssLoadLibrary(platformPath);
205 if (DEBUG) System.out.println("handle: " + nssHandle);
206 fetchVersions();
207 if (supported == false) {
208 throw new IOException
209 ("The specified version of NSS is incompatible, "
210 + "3.7 or later required");
211 }
212
213 if (DEBUG) System.out.println("dir: " + configDir);
214 boolean initok = nssInit(dbMode.functionName, nssHandle, configDir);
215 if (DEBUG) System.out.println("init: " + initok);
216 if (initok == false) {
217 throw new IOException("NSS initialization failed");
218 }
219
220 this.configDir = configDir;
221 this.nssLibDir = nssLibDir;
222 }
223
224 /**
225 * Return an immutable list of all available modules.
226 *
227 * @throws IllegalStateException if this Secmod is misconfigured
228 * or not initialized
229 */
230 public synchronized List<Module> getModules() {
231 try {
232 if (isInitialized() == false) {
233 throw new IllegalStateException("NSS not initialized");
234 }
748 token.p11.C_FindObjectsFinal(session.id());
749 if (DEBUG) System.out.println("handles: " + handles.length);
750
751 for (long handle : handles) {
752 TrustAttributes trust = new TrustAttributes(token, session, handle);
753 trustMap.put(trust.getHash(), trust);
754 }
755 } finally {
756 token.releaseSession(session);
757 }
758 return trustMap;
759 }
760
761 private static native long nssGetLibraryHandle(String libraryName);
762
763 private static native long nssLoadLibrary(String name) throws IOException;
764
765 private static native boolean nssVersionCheck(long handle, String minVersion);
766
767 private static native boolean nssInit(String functionName, long handle, String configDir);
768
769 private static native Object nssGetModuleList(long handle, String libDir);
770
771 }
|
144 }
145
146 /**
147 * Initialize this Secmod.
148 *
149 * @param configDir the directory containing the NSS configuration
150 * files such as secmod.db
151 * @param nssLibDir the directory containing the NSS libraries
152 * (libnss3.so or nss3.dll) or null if the library is on
153 * the system default shared library path
154 *
155 * @throws IOException if NSS has already been initialized,
156 * the specified directories are invalid, or initialization
157 * fails for any other reason
158 */
159 public void initialize(String configDir, String nssLibDir)
160 throws IOException {
161 initialize(DbMode.READ_WRITE, configDir, nssLibDir);
162 }
163
164 public void initialize(DbMode dbMode, String configDir, String nssLibDir)
165 throws IOException {
166 initialize(dbMode, configDir, nssLibDir, false);
167 }
168
169 public synchronized void initialize(DbMode dbMode, String configDir,
170 String nssLibDir, boolean nssUseOptimizeSpace) throws IOException {
171
172
173 if (isInitialized()) {
174 throw new IOException("NSS is already initialized");
175 }
176
177 if (dbMode == null) {
178 throw new NullPointerException();
179 }
180 if ((dbMode != DbMode.NO_DB) && (configDir == null)) {
181 throw new NullPointerException();
182 }
183 String platformLibName = System.mapLibraryName("nss3");
184 String platformPath;
185 if (nssLibDir == null) {
186 platformPath = platformLibName;
187 } else {
188 File base = new File(nssLibDir);
189 if (base.isDirectory() == false) {
190 throw new IOException("nssLibDir must be a directory:" + nssLibDir);
191 }
192 File platformFile = new File(base, platformLibName);
201 if (configBase.isDirectory() == false ) {
202 throw new IOException("configDir must be a directory: " + configDir);
203 }
204 File secmodFile = new File(configBase, "secmod.db");
205 if (secmodFile.isFile() == false) {
206 throw new FileNotFoundException(secmodFile.getPath());
207 }
208 }
209
210 if (DEBUG) System.out.println("lib: " + platformPath);
211 nssHandle = nssLoadLibrary(platformPath);
212 if (DEBUG) System.out.println("handle: " + nssHandle);
213 fetchVersions();
214 if (supported == false) {
215 throw new IOException
216 ("The specified version of NSS is incompatible, "
217 + "3.7 or later required");
218 }
219
220 if (DEBUG) System.out.println("dir: " + configDir);
221 boolean initok = nssInit(dbMode.functionName, nssHandle, configDir,
222 nssUseOptimizeSpace);
223 if (DEBUG) System.out.println("init: " + initok);
224 if (initok == false) {
225 throw new IOException("NSS initialization failed");
226 }
227
228 this.configDir = configDir;
229 this.nssLibDir = nssLibDir;
230 }
231
232 /**
233 * Return an immutable list of all available modules.
234 *
235 * @throws IllegalStateException if this Secmod is misconfigured
236 * or not initialized
237 */
238 public synchronized List<Module> getModules() {
239 try {
240 if (isInitialized() == false) {
241 throw new IllegalStateException("NSS not initialized");
242 }
756 token.p11.C_FindObjectsFinal(session.id());
757 if (DEBUG) System.out.println("handles: " + handles.length);
758
759 for (long handle : handles) {
760 TrustAttributes trust = new TrustAttributes(token, session, handle);
761 trustMap.put(trust.getHash(), trust);
762 }
763 } finally {
764 token.releaseSession(session);
765 }
766 return trustMap;
767 }
768
769 private static native long nssGetLibraryHandle(String libraryName);
770
771 private static native long nssLoadLibrary(String name) throws IOException;
772
773 private static native boolean nssVersionCheck(long handle, String minVersion);
774
775 private static native boolean nssInit(String functionName, long handle, String configDir);
776
777 private static native boolean nssInit(String functionName, long handle, String configDir, boolean nssUseOptimizeSpace);
778
779 private static native Object nssGetModuleList(long handle, String libDir);
780
781 }
|