1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | |
---|
6 | package br.gov.serpro.cert; |
---|
7 | |
---|
8 | import br.gov.serpro.setup.Setup; |
---|
9 | import java.io.IOException; |
---|
10 | import java.util.HashMap; |
---|
11 | import java.util.HashSet; |
---|
12 | import java.util.Set; |
---|
13 | import java.util.logging.Level; |
---|
14 | import java.util.logging.Logger; |
---|
15 | import sun.security.pkcs11.wrapper.CK_C_INITIALIZE_ARGS; |
---|
16 | import sun.security.pkcs11.wrapper.CK_TOKEN_INFO; |
---|
17 | import sun.security.pkcs11.wrapper.PKCS11; |
---|
18 | import sun.security.pkcs11.wrapper.PKCS11Exception; |
---|
19 | import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
---|
20 | |
---|
21 | /** |
---|
22 | * |
---|
23 | * @author esa |
---|
24 | */ |
---|
25 | class TokenCollection extends HashMap<String, Token>{ |
---|
26 | |
---|
27 | private String preferedTokenKey; |
---|
28 | private final Setup setup; |
---|
29 | |
---|
30 | public TokenCollection(Setup setup){ |
---|
31 | |
---|
32 | this.setup = setup; |
---|
33 | this.addTokens(setup.getParameter("token")); |
---|
34 | |
---|
35 | } |
---|
36 | |
---|
37 | public void setPreferedToken(java.lang.String preferedTokenKey){ |
---|
38 | |
---|
39 | } |
---|
40 | |
---|
41 | public String getPreferedToken(){ |
---|
42 | return preferedTokenKey; |
---|
43 | } |
---|
44 | |
---|
45 | private void addTokens(String tokens){ |
---|
46 | |
---|
47 | String[] tokensArray = tokens.split(","); |
---|
48 | for (int i = 0; i < tokensArray.length; i++){ |
---|
49 | if (tokensArray[i] != null && tokensArray[i].length() > 0){ |
---|
50 | String[] tokenArray = tokensArray[i].split(";"); |
---|
51 | Token token = new Token(tokenArray[0], tokenArray[1], this.setup); |
---|
52 | |
---|
53 | // Aqui testar se existe token inserido em algum slot para determinado driver. |
---|
54 | // Pega sempre o primeiro slot registrado com a lib tokenArray[1] |
---|
55 | // TODO: Deixar o usuário escolher o token que vai usar; |
---|
56 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
57 | System.out.println("Getting slots from " + tokenArray[1]); |
---|
58 | } |
---|
59 | try { |
---|
60 | long[] slots = getSlotsWithTokens(tokenArray[1]); |
---|
61 | if (slots.length > 0){ |
---|
62 | token.registerToken(slots[0]); |
---|
63 | if (token.isRegistered()){ |
---|
64 | this.put(token.getName(), token); |
---|
65 | } |
---|
66 | } |
---|
67 | } catch (IOException iex){ |
---|
68 | continue; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | public long[] getSlotsWithTokens(String libraryPath) throws IOException{ |
---|
75 | CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); |
---|
76 | String functionList = "C_GetFunctionList"; |
---|
77 | |
---|
78 | initArgs.flags = CKF_OS_LOCKING_OK; |
---|
79 | PKCS11 tmpPKCS11 = null; |
---|
80 | long[] slotList = null; |
---|
81 | try { |
---|
82 | try { |
---|
83 | tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, false); |
---|
84 | } catch (IOException ex) { |
---|
85 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
86 | Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); |
---|
87 | } |
---|
88 | throw ex; |
---|
89 | } |
---|
90 | } catch (PKCS11Exception e) { |
---|
91 | try { |
---|
92 | initArgs = null; |
---|
93 | tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, true); |
---|
94 | } catch (IOException ex) { |
---|
95 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
96 | Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); |
---|
97 | } |
---|
98 | } catch (PKCS11Exception ex) { |
---|
99 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
100 | Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | try { |
---|
106 | slotList = tmpPKCS11.C_GetSlotList(true); |
---|
107 | |
---|
108 | for (long slot : slotList){ |
---|
109 | CK_TOKEN_INFO tokenInfo = tmpPKCS11.C_GetTokenInfo(slot); |
---|
110 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
111 | System.out.println("slot: "+slot+"\nmanufacturerID: " |
---|
112 | + String.valueOf(tokenInfo.manufacturerID) + "\nmodel: " |
---|
113 | + String.valueOf(tokenInfo.model)); |
---|
114 | } |
---|
115 | } |
---|
116 | } catch (PKCS11Exception ex) { |
---|
117 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
118 | Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); |
---|
119 | } |
---|
120 | } catch (Throwable t) { |
---|
121 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
122 | Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, t); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | return slotList; |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | public Set<Token> getRegisteredTokens(){ |
---|
131 | Set<Token> tokens = new HashSet<Token>(); |
---|
132 | |
---|
133 | for (String id : this.keySet()){ |
---|
134 | Token token = this.get(id); |
---|
135 | if (token.isRegistered()){ |
---|
136 | tokens.add(token); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | return tokens; |
---|
141 | } |
---|
142 | |
---|
143 | } |
---|