• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ebourg / jsign / #371

20 May 2025 09:49AM UTC coverage: 83.194% (-0.1%) from 83.31%
#371

push

ebourg
Allow the keystore parameter to be specified with the ETOKEN storetype to distinguish between multiple connected devices (#300)

0 of 6 new or added lines in 2 files covered. (0.0%)

36 existing lines in 3 files now uncovered.

4861 of 5843 relevant lines covered (83.19%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jsign-crypto/src/main/java/net/jsign/SafeNetEToken.java
1
/*
2
 * Copyright 2023 Emmanuel Bourg
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package net.jsign;
18

19
import java.io.File;
20
import java.io.IOException;
21
import java.security.Provider;
22
import java.security.ProviderException;
23
import java.util.ArrayList;
24
import java.util.List;
25

26
import sun.security.pkcs11.wrapper.PKCS11;
27
import sun.security.pkcs11.wrapper.PKCS11Exception;
28

29
/**
30
 * Helper class for working with SafeNet eTokens.
31
 *
32
 * @since 6.0
33
 */
34
class SafeNetEToken {
×
35

36
    /**
37
     * Returns the security provider for the SafeNet eToken.
38
     *
39
     * @return the SafeNet eTokens security provider
40
     * @throws ProviderException thrown if the provider can't be initialized
41
     */
42
    static Provider getProvider(String name) {
NEW
43
        return ProviderUtils.createSunPKCS11Provider(getSunPKCS11Configuration(name));
×
44
    }
45

46
    /**
47
     * Returns the SunPKCS11 configuration of the SafeNet eToken.
48
     *
49
     * @throws ProviderException thrown if the PKCS11 modules cannot be found
50
     */
51
    static String getSunPKCS11Configuration(String name) {
52
        File library = getPKCS11Library();
×
53
        if (!library.exists()) {
×
54
            throw new ProviderException("SafeNet eToken PKCS11 module is not installed (" + library + " is missing)");
×
55
        }
56

57
        long slot;
58
        try {
59
            try {
NEW
60
                slot = Integer.parseInt(name);
×
NEW
61
            } catch (Exception e) {
×
NEW
62
                slot = getTokenSlot(library);
×
NEW
63
            }
×
UNCOV
64
        } catch (Exception e) {
×
UNCOV
65
            throw new ProviderException(e);
×
66
        }
×
67

68
        return new PKCS11Configuration().name("SafeNet eToken").library(library).slot(slot).toString();
×
69
    }
70

71
    /**
72
     * Returns the slot index associated to the token.
73
     */
74
    static long getTokenSlot(File libraryPath) throws PKCS11Exception, IOException {
UNCOV
75
        PKCS11 pkcs11 = PKCS11.getInstance(libraryPath.getAbsolutePath(), "C_GetFunctionList", null, false);
×
76
        long[] slots = pkcs11.C_GetSlotList(true);
×
77
        return slots.length > 0 ? slots[0] : -1;
×
78
    }
79

80
    /**
81
     * Attempts to locate the SafeNet eToken PKCS11 library on the system.
82
     */
83
    static File getPKCS11Library() {
UNCOV
84
        String osname = System.getProperty("os.name");
×
85
        String arch = System.getProperty("sun.arch.data.model");
×
86

UNCOV
87
        if (osname.contains("Windows")) {
×
88
            return new File(System.getenv("windir") + "/system32/eTPKCS11.dll");
×
89

UNCOV
90
        } else if (osname.contains("Mac")) {
×
91
            return new File("/usr/local/lib/libeTPkcs11.dylib");
×
92

93
        } else {
94
            // Linux
UNCOV
95
            List<String> paths = new ArrayList<>();
×
96
            if ("64".equals(arch)) {
×
97
                paths.add("/usr/lib64/pkcs11/libeTPkcs11.so");
×
98
                paths.add("/usr/lib64/libeTPkcs11.so");
×
99
                paths.add("/usr/lib64/libeToken.so");
×
100
            }
UNCOV
101
            paths.add("/usr/lib/pkcs11/libeTPkcs11.so");
×
102
            paths.add("/usr/lib/pkcs11/libeToken.so");
×
103
            paths.add("/usr/lib/libeTPkcs11.so");
×
104
            paths.add("/usr/lib/libeToken.so");
×
105

UNCOV
106
            for (String path : paths) {
×
107
                File library = new File(path);
×
108
                if (library.exists()) {
×
109
                    return library;
×
110
                }
UNCOV
111
            }
×
112

UNCOV
113
            return new File("/usr/local/lib/libeToken.so");
×
114
        }
115
    }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc