Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/Secmod.java
38919 views
1
/*
2
* Copyright (c) 2005, 2013, 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
package sun.security.pkcs11;
27
28
import java.io.*;
29
import java.util.*;
30
31
import java.security.*;
32
import java.security.KeyStore.*;
33
import java.security.cert.X509Certificate;
34
35
import sun.security.pkcs11.wrapper.*;
36
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
37
38
39
/**
40
* The Secmod class defines the interface to the native NSS
41
* library and the configuration information it stores in its
42
* secmod.db file.
43
*
44
* <p>Example code:
45
* <pre>
46
* Secmod secmod = Secmod.getInstance();
47
* if (secmod.isInitialized() == false) {
48
* secmod.initialize("/home/myself/.mozilla", "/usr/sfw/lib/mozilla");
49
* }
50
*
51
* Provider p = secmod.getModule(ModuleType.KEYSTORE).getProvider();
52
* KeyStore ks = KeyStore.getInstance("PKCS11", p);
53
* ks.load(null, password);
54
* </pre>
55
*
56
* @since 1.6
57
* @author Andreas Sterbenz
58
*/
59
public final class Secmod {
60
61
private final static boolean DEBUG = false;
62
63
private final static Secmod INSTANCE;
64
65
static {
66
sun.security.pkcs11.wrapper.PKCS11.loadNative();
67
INSTANCE = new Secmod();
68
}
69
70
private final static String NSS_LIB_NAME = "nss3";
71
72
private final static String SOFTTOKEN_LIB_NAME = "softokn3";
73
74
private final static String TRUST_LIB_NAME = "nssckbi";
75
76
// handle to be passed to the native code, 0 means not initialized
77
private long nssHandle;
78
79
// whether this is a supported version of NSS
80
private boolean supported;
81
82
// list of the modules
83
private List<Module> modules;
84
85
private String configDir;
86
87
private String nssLibDir;
88
89
private Secmod() {
90
// empty
91
}
92
93
/**
94
* Return the singleton Secmod instance.
95
*/
96
public static Secmod getInstance() {
97
return INSTANCE;
98
}
99
100
private boolean isLoaded() {
101
if (nssHandle == 0) {
102
nssHandle = nssGetLibraryHandle(System.mapLibraryName(NSS_LIB_NAME));
103
if (nssHandle != 0) {
104
fetchVersions();
105
}
106
}
107
return (nssHandle != 0);
108
}
109
110
private void fetchVersions() {
111
supported = nssVersionCheck(nssHandle, "3.7");
112
}
113
114
/**
115
* Test whether this Secmod has been initialized. Returns true
116
* if NSS has been initialized using either the initialize() method
117
* or by directly calling the native NSS APIs. The latter may be
118
* the case if the current process contains components that use
119
* NSS directly.
120
*
121
* @throws IOException if an incompatible version of NSS
122
* has been loaded
123
*/
124
public synchronized boolean isInitialized() throws IOException {
125
// NSS does not allow us to check if it is initialized already
126
// assume that if it is loaded it is also initialized
127
if (isLoaded() == false) {
128
return false;
129
}
130
if (supported == false) {
131
throw new IOException
132
("An incompatible version of NSS is already loaded, "
133
+ "3.7 or later required");
134
}
135
return true;
136
}
137
138
String getConfigDir() {
139
return configDir;
140
}
141
142
String getLibDir() {
143
return nssLibDir;
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, false);
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 nssOptimizeSpace) throws IOException {
171
172
if (isInitialized()) {
173
throw new IOException("NSS is already initialized");
174
}
175
176
if (dbMode == null) {
177
throw new NullPointerException();
178
}
179
if ((dbMode != DbMode.NO_DB) && (configDir == null)) {
180
throw new NullPointerException();
181
}
182
String platformLibName = System.mapLibraryName("nss3");
183
String platformPath;
184
if (nssLibDir == null) {
185
platformPath = platformLibName;
186
} else {
187
File base = new File(nssLibDir);
188
if (base.isDirectory() == false) {
189
throw new IOException("nssLibDir must be a directory:" + nssLibDir);
190
}
191
File platformFile = new File(base, platformLibName);
192
if (platformFile.isFile() == false) {
193
throw new FileNotFoundException(platformFile.getPath());
194
}
195
platformPath = platformFile.getPath();
196
}
197
198
if (configDir != null) {
199
String configDirPath = null;
200
String sqlPrefix = "sql:/";
201
if (!configDir.startsWith(sqlPrefix)) {
202
configDirPath = configDir;
203
} else {
204
StringBuilder configDirPathSB = new StringBuilder(configDir);
205
configDirPath = configDirPathSB.substring(sqlPrefix.length());
206
}
207
File configBase = new File(configDirPath);
208
if (configBase.isDirectory() == false ) {
209
throw new IOException("configDir must be a directory: " + configDirPath);
210
}
211
if (!configDir.startsWith(sqlPrefix)) {
212
File secmodFile = new File(configBase, "secmod.db");
213
if (secmodFile.isFile() == false) {
214
throw new FileNotFoundException(secmodFile.getPath());
215
}
216
}
217
}
218
219
if (DEBUG) System.out.println("lib: " + platformPath);
220
nssHandle = nssLoadLibrary(platformPath);
221
if (DEBUG) System.out.println("handle: " + nssHandle);
222
fetchVersions();
223
if (supported == false) {
224
throw new IOException
225
("The specified version of NSS is incompatible, "
226
+ "3.7 or later required");
227
}
228
229
if (DEBUG) System.out.println("dir: " + configDir);
230
boolean initok = nssInitialize(dbMode.functionName, nssHandle,
231
configDir, nssOptimizeSpace);
232
if (DEBUG) System.out.println("init: " + initok);
233
if (initok == false) {
234
throw new IOException("NSS initialization failed");
235
}
236
237
this.configDir = configDir;
238
this.nssLibDir = nssLibDir;
239
}
240
241
/**
242
* Return an immutable list of all available modules.
243
*
244
* @throws IllegalStateException if this Secmod is misconfigured
245
* or not initialized
246
*/
247
public synchronized List<Module> getModules() {
248
try {
249
if (isInitialized() == false) {
250
throw new IllegalStateException("NSS not initialized");
251
}
252
} catch (IOException e) {
253
// IOException if misconfigured
254
throw new IllegalStateException(e);
255
}
256
if (modules == null) {
257
@SuppressWarnings("unchecked")
258
List<Module> modules = (List<Module>)nssGetModuleList(nssHandle,
259
nssLibDir);
260
this.modules = Collections.unmodifiableList(modules);
261
}
262
return modules;
263
}
264
265
private static byte[] getDigest(X509Certificate cert, String algorithm) {
266
try {
267
MessageDigest md = MessageDigest.getInstance(algorithm);
268
return md.digest(cert.getEncoded());
269
} catch (GeneralSecurityException e) {
270
throw new ProviderException(e);
271
}
272
}
273
274
boolean isTrusted(X509Certificate cert, TrustType trustType) {
275
Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
276
TrustAttributes attr = getModuleTrust(ModuleType.KEYSTORE, bytes);
277
if (attr == null) {
278
attr = getModuleTrust(ModuleType.FIPS, bytes);
279
if (attr == null) {
280
attr = getModuleTrust(ModuleType.TRUSTANCHOR, bytes);
281
}
282
}
283
return (attr == null) ? false : attr.isTrusted(trustType);
284
}
285
286
private TrustAttributes getModuleTrust(ModuleType type, Bytes bytes) {
287
Module module = getModule(type);
288
TrustAttributes t = (module == null) ? null : module.getTrust(bytes);
289
return t;
290
}
291
292
/**
293
* Constants describing the different types of NSS modules.
294
* For this API, NSS modules are classified as either one
295
* of the internal modules delivered as part of NSS or
296
* as an external module provided by a 3rd party.
297
*/
298
public static enum ModuleType {
299
/**
300
* The NSS Softtoken crypto module. This is the first
301
* slot of the softtoken object.
302
* This module provides
303
* implementations for cryptographic algorithms but no KeyStore.
304
*/
305
CRYPTO,
306
/**
307
* The NSS Softtoken KeyStore module. This is the second
308
* slot of the softtoken object.
309
* This module provides
310
* implementations for cryptographic algorithms (after login)
311
* and the KeyStore.
312
*/
313
KEYSTORE,
314
/**
315
* The NSS Softtoken module in FIPS mode. Note that in FIPS mode the
316
* softtoken presents only one slot, not separate CRYPTO and KEYSTORE
317
* slots as in non-FIPS mode.
318
*/
319
FIPS,
320
/**
321
* The NSS builtin trust anchor module. This is the
322
* NSSCKBI object. It provides no crypto functions.
323
*/
324
TRUSTANCHOR,
325
/**
326
* An external module.
327
*/
328
EXTERNAL,
329
}
330
331
/**
332
* Returns the first module of the specified type. If no such
333
* module exists, this method returns null.
334
*
335
* @throws IllegalStateException if this Secmod is misconfigured
336
* or not initialized
337
*/
338
public Module getModule(ModuleType type) {
339
for (Module module : getModules()) {
340
if (module.getType() == type) {
341
return module;
342
}
343
}
344
return null;
345
}
346
347
static final String TEMPLATE_EXTERNAL =
348
"library = %s\n"
349
+ "name = \"%s\"\n"
350
+ "slotListIndex = %d\n";
351
352
static final String TEMPLATE_TRUSTANCHOR =
353
"library = %s\n"
354
+ "name = \"NSS Trust Anchors\"\n"
355
+ "slotListIndex = 0\n"
356
+ "enabledMechanisms = { KeyStore }\n"
357
+ "nssUseSecmodTrust = true\n";
358
359
static final String TEMPLATE_CRYPTO =
360
"library = %s\n"
361
+ "name = \"NSS SoftToken Crypto\"\n"
362
+ "slotListIndex = 0\n"
363
+ "disabledMechanisms = { KeyStore }\n";
364
365
static final String TEMPLATE_KEYSTORE =
366
"library = %s\n"
367
+ "name = \"NSS SoftToken KeyStore\"\n"
368
+ "slotListIndex = 1\n"
369
+ "nssUseSecmodTrust = true\n";
370
371
static final String TEMPLATE_FIPS =
372
"library = %s\n"
373
+ "name = \"NSS FIPS SoftToken\"\n"
374
+ "slotListIndex = 0\n"
375
+ "nssUseSecmodTrust = true\n";
376
377
/**
378
* A representation of one PKCS#11 slot in a PKCS#11 module.
379
*/
380
public static final class Module {
381
// path of the native library
382
final String libraryName;
383
// descriptive name used by NSS
384
final String commonName;
385
final int slot;
386
final ModuleType type;
387
388
private String config;
389
private SunPKCS11 provider;
390
391
// trust attributes. Used for the KEYSTORE and TRUSTANCHOR modules only
392
private Map<Bytes,TrustAttributes> trust;
393
394
Module(String libraryDir, String libraryName, String commonName,
395
boolean fips, int slot) {
396
ModuleType type;
397
398
if ((libraryName == null) || (libraryName.length() == 0)) {
399
// must be softtoken
400
libraryName = System.mapLibraryName(SOFTTOKEN_LIB_NAME);
401
if (fips == false) {
402
type = (slot == 0) ? ModuleType.CRYPTO : ModuleType.KEYSTORE;
403
} else {
404
type = ModuleType.FIPS;
405
if (slot != 0) {
406
throw new RuntimeException
407
("Slot index should be 0 for FIPS slot");
408
}
409
}
410
} else {
411
if (libraryName.endsWith(System.mapLibraryName(TRUST_LIB_NAME))
412
|| commonName.equals("Builtin Roots Module")) {
413
type = ModuleType.TRUSTANCHOR;
414
} else {
415
type = ModuleType.EXTERNAL;
416
}
417
}
418
// On Ubuntu the libsoftokn3 library is located in a subdirectory
419
// of the system libraries directory. (Since Ubuntu 11.04.)
420
File libraryFile = new File(libraryDir, libraryName);
421
if (!libraryFile.isFile()) {
422
File failover = new File(libraryDir, "nss/" + libraryName);
423
if (failover.isFile()) {
424
libraryFile = failover;
425
}
426
}
427
this.libraryName = libraryFile.getPath();
428
this.commonName = commonName;
429
this.slot = slot;
430
this.type = type;
431
initConfiguration();
432
}
433
434
private void initConfiguration() {
435
switch (type) {
436
case EXTERNAL:
437
config = String.format(TEMPLATE_EXTERNAL, libraryName,
438
commonName + " " + slot, slot);
439
break;
440
case CRYPTO:
441
config = String.format(TEMPLATE_CRYPTO, libraryName);
442
break;
443
case KEYSTORE:
444
config = String.format(TEMPLATE_KEYSTORE, libraryName);
445
break;
446
case FIPS:
447
config = String.format(TEMPLATE_FIPS, libraryName);
448
break;
449
case TRUSTANCHOR:
450
config = String.format(TEMPLATE_TRUSTANCHOR, libraryName);
451
break;
452
default:
453
throw new RuntimeException("Unknown module type: " + type);
454
}
455
}
456
457
/**
458
* Get the configuration for this module. This is a string
459
* in the SunPKCS11 configuration format. It can be
460
* customized with additional options and then made
461
* current using the setConfiguration() method.
462
*/
463
@Deprecated
464
public synchronized String getConfiguration() {
465
return config;
466
}
467
468
/**
469
* Set the configuration for this module.
470
*
471
* @throws IllegalStateException if the associated provider
472
* instance has already been created.
473
*/
474
@Deprecated
475
public synchronized void setConfiguration(String config) {
476
if (provider != null) {
477
throw new IllegalStateException("Provider instance already created");
478
}
479
this.config = config;
480
}
481
482
/**
483
* Return the pathname of the native library that implements
484
* this module. For example, /usr/lib/libpkcs11.so.
485
*/
486
public String getLibraryName() {
487
return libraryName;
488
}
489
490
/**
491
* Returns the type of this module.
492
*/
493
public ModuleType getType() {
494
return type;
495
}
496
497
/**
498
* Returns the provider instance that is associated with this
499
* module. The first call to this method creates the provider
500
* instance.
501
*/
502
@Deprecated
503
public synchronized Provider getProvider() {
504
if (provider == null) {
505
provider = newProvider();
506
}
507
return provider;
508
}
509
510
synchronized boolean hasInitializedProvider() {
511
return provider != null;
512
}
513
514
void setProvider(SunPKCS11 p) {
515
if (provider != null) {
516
throw new ProviderException("Secmod provider already initialized");
517
}
518
provider = p;
519
}
520
521
private SunPKCS11 newProvider() {
522
try {
523
InputStream in = new ByteArrayInputStream(config.getBytes("UTF8"));
524
return new SunPKCS11(in);
525
} catch (Exception e) {
526
// XXX
527
throw new ProviderException(e);
528
}
529
}
530
531
synchronized void setTrust(Token token, X509Certificate cert) {
532
Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
533
TrustAttributes attr = getTrust(bytes);
534
if (attr == null) {
535
attr = new TrustAttributes(token, cert, bytes, CKT_NETSCAPE_TRUSTED_DELEGATOR);
536
trust.put(bytes, attr);
537
} else {
538
// does it already have the correct trust settings?
539
if (attr.isTrusted(TrustType.ALL) == false) {
540
// XXX not yet implemented
541
throw new ProviderException("Cannot change existing trust attributes");
542
}
543
}
544
}
545
546
TrustAttributes getTrust(Bytes hash) {
547
if (trust == null) {
548
// If provider is not set, create a temporary provider to
549
// retrieve the trust information. This can happen if we need
550
// to get the trust information for the trustanchor module
551
// because we need to look for user customized settings in the
552
// keystore module (which may not have a provider created yet).
553
// Creating a temporary provider and then dropping it on the
554
// floor immediately is flawed, but it's the best we can do
555
// for now.
556
synchronized (this) {
557
SunPKCS11 p = provider;
558
if (p == null) {
559
p = newProvider();
560
}
561
try {
562
trust = Secmod.getTrust(p);
563
} catch (PKCS11Exception e) {
564
throw new RuntimeException(e);
565
}
566
}
567
}
568
return trust.get(hash);
569
}
570
571
public String toString() {
572
return
573
commonName + " (" + type + ", " + libraryName + ", slot " + slot + ")";
574
}
575
576
}
577
578
/**
579
* Constants representing NSS trust categories.
580
*/
581
public static enum TrustType {
582
/** Trusted for all purposes */
583
ALL,
584
/** Trusted for SSL client authentication */
585
CLIENT_AUTH,
586
/** Trusted for SSL server authentication */
587
SERVER_AUTH,
588
/** Trusted for code signing */
589
CODE_SIGNING,
590
/** Trusted for email protection */
591
EMAIL_PROTECTION,
592
}
593
594
public static enum DbMode {
595
READ_WRITE("NSS_InitReadWrite"),
596
READ_ONLY ("NSS_Init"),
597
NO_DB ("NSS_NoDB_Init");
598
599
final String functionName;
600
DbMode(String functionName) {
601
this.functionName = functionName;
602
}
603
}
604
605
/**
606
* A LoadStoreParameter for use with the NSS Softtoken or
607
* NSS TrustAnchor KeyStores.
608
* <p>
609
* It allows the set of trusted certificates that are returned by
610
* the KeyStore to be specified.
611
*/
612
public static final class KeyStoreLoadParameter implements LoadStoreParameter {
613
final TrustType trustType;
614
final ProtectionParameter protection;
615
public KeyStoreLoadParameter(TrustType trustType, char[] password) {
616
this(trustType, new PasswordProtection(password));
617
618
}
619
public KeyStoreLoadParameter(TrustType trustType, ProtectionParameter prot) {
620
if (trustType == null) {
621
throw new NullPointerException("trustType must not be null");
622
}
623
this.trustType = trustType;
624
this.protection = prot;
625
}
626
public ProtectionParameter getProtectionParameter() {
627
return protection;
628
}
629
public TrustType getTrustType() {
630
return trustType;
631
}
632
}
633
634
static class TrustAttributes {
635
final long handle;
636
final long clientAuth, serverAuth, codeSigning, emailProtection;
637
final byte[] shaHash;
638
TrustAttributes(Token token, X509Certificate cert, Bytes bytes, long trustValue) {
639
Session session = null;
640
try {
641
session = token.getOpSession();
642
// XXX use KeyStore TrustType settings to determine which
643
// attributes to set
644
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
645
new CK_ATTRIBUTE(CKA_TOKEN, true),
646
new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST),
647
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH, trustValue),
648
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING, trustValue),
649
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION, trustValue),
650
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH, trustValue),
651
new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH, bytes.b),
652
new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_MD5_HASH, getDigest(cert, "MD5")),
653
new CK_ATTRIBUTE(CKA_ISSUER, cert.getIssuerX500Principal().getEncoded()),
654
new CK_ATTRIBUTE(CKA_SERIAL_NUMBER, cert.getSerialNumber().toByteArray()),
655
// XXX per PKCS#11 spec, the serial number should be in ASN.1
656
};
657
handle = token.p11.C_CreateObject(session.id(), attrs);
658
shaHash = bytes.b;
659
clientAuth = trustValue;
660
serverAuth = trustValue;
661
codeSigning = trustValue;
662
emailProtection = trustValue;
663
} catch (PKCS11Exception e) {
664
throw new ProviderException("Could not create trust object", e);
665
} finally {
666
token.releaseSession(session);
667
}
668
}
669
TrustAttributes(Token token, Session session, long handle)
670
throws PKCS11Exception {
671
this.handle = handle;
672
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
673
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH),
674
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING),
675
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION),
676
new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH),
677
};
678
679
token.p11.C_GetAttributeValue(session.id(), handle, attrs);
680
serverAuth = attrs[0].getLong();
681
codeSigning = attrs[1].getLong();
682
emailProtection = attrs[2].getLong();
683
shaHash = attrs[3].getByteArray();
684
685
attrs = new CK_ATTRIBUTE[] {
686
new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH),
687
};
688
long c;
689
try {
690
token.p11.C_GetAttributeValue(session.id(), handle, attrs);
691
c = attrs[0].getLong();
692
} catch (PKCS11Exception e) {
693
// trust anchor module does not support this attribute
694
c = serverAuth;
695
}
696
clientAuth = c;
697
}
698
Bytes getHash() {
699
return new Bytes(shaHash);
700
}
701
boolean isTrusted(TrustType type) {
702
switch (type) {
703
case CLIENT_AUTH:
704
return isTrusted(clientAuth);
705
case SERVER_AUTH:
706
return isTrusted(serverAuth);
707
case CODE_SIGNING:
708
return isTrusted(codeSigning);
709
case EMAIL_PROTECTION:
710
return isTrusted(emailProtection);
711
case ALL:
712
return isTrusted(TrustType.CLIENT_AUTH)
713
&& isTrusted(TrustType.SERVER_AUTH)
714
&& isTrusted(TrustType.CODE_SIGNING)
715
&& isTrusted(TrustType.EMAIL_PROTECTION);
716
default:
717
return false;
718
}
719
}
720
721
private boolean isTrusted(long l) {
722
// XXX CKT_TRUSTED?
723
return (l == CKT_NETSCAPE_TRUSTED_DELEGATOR);
724
}
725
726
}
727
728
private static class Bytes {
729
final byte[] b;
730
Bytes(byte[] b) {
731
this.b = b;
732
}
733
public int hashCode() {
734
return Arrays.hashCode(b);
735
}
736
public boolean equals(Object o) {
737
if (this == o) {
738
return true;
739
}
740
if (o instanceof Bytes == false) {
741
return false;
742
}
743
Bytes other = (Bytes)o;
744
return Arrays.equals(this.b, other.b);
745
}
746
}
747
748
private static Map<Bytes,TrustAttributes> getTrust(SunPKCS11 provider)
749
throws PKCS11Exception {
750
Map<Bytes,TrustAttributes> trustMap = new HashMap<Bytes,TrustAttributes>();
751
Token token = provider.getToken();
752
Session session = null;
753
try {
754
session = token.getOpSession();
755
int MAX_NUM = 8192;
756
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
757
new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST),
758
};
759
token.p11.C_FindObjectsInit(session.id(), attrs);
760
long[] handles = token.p11.C_FindObjects(session.id(), MAX_NUM);
761
token.p11.C_FindObjectsFinal(session.id());
762
if (DEBUG) System.out.println("handles: " + handles.length);
763
764
for (long handle : handles) {
765
try {
766
TrustAttributes trust = new TrustAttributes(token, session, handle);
767
trustMap.put(trust.getHash(), trust);
768
} catch (PKCS11Exception e) {
769
// skip put on pkcs11 error
770
}
771
}
772
} finally {
773
token.releaseSession(session);
774
}
775
return trustMap;
776
}
777
778
private static native long nssGetLibraryHandle(String libraryName);
779
780
private static native long nssLoadLibrary(String name) throws IOException;
781
782
private static native boolean nssVersionCheck(long handle, String minVersion);
783
784
private static native boolean nssInitialize(String functionName, long handle, String configDir, boolean nssOptimizeSpace);
785
786
private static native Object nssGetModuleList(long handle, String libDir);
787
788
}
789
790