source: branches/2.3/security/ExpressoCert/src/br/gov/serpro/cert/Token.java @ 5898

Revision 5898, 3.3 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2256 - Problemas com o token iKey 2032 / 4000.

Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package br.gov.serpro.cert;
7
8import br.gov.serpro.setup.Setup;
9import java.io.ByteArrayInputStream;
10import java.io.File;
11import java.io.IOException;
12import java.security.Provider;
13import java.security.ProviderException;
14import java.security.Security;
15
16//TODO: Deal with wildcards for environments variables.
17
18/**
19 *
20 * @author esa
21 */
22class Token{
23
24    private final Setup setup;
25    private String name;
26    private String libraryPath;
27    private Provider tokenProvider;
28    private boolean registered = false;
29    private long slot;
30
31    static long CK_OBJECT_CLASS;
32    static long CK_OBJECT_HANDLE;
33
34    private Token(final Setup setup) {
35        this.setup = setup;
36    }
37
38    Token(String name, String libraryPath, final Setup setup){
39        this(setup);
40        this.setName(name);
41        this.setLibraryPath(libraryPath);
42    }
43
44    public boolean isRegistered() {
45        return this.registered;
46    }
47
48    public void setLibraryPath(String libraryPath) {
49        this.libraryPath = libraryPath;
50    }
51
52    public void setName(String name) {
53        this.name = name;
54    }
55
56    public String getName() {
57        return this.name;
58    }
59
60    public String getProviderName(){
61        return this.tokenProvider.getName();
62    }
63
64    protected void registerToken(long slot) throws IOException{
65
66        this.slot = slot;
67        String tokenConfiguration = new String("name = " + name + "_" + slot + "\n" +
68            "library = " + libraryPath + "\nslot = " + slot +
69            "\ndisabledMechanisms = {\n" + "CKM_SHA1_RSA_PKCS\n}" +
70            "\n");
71
72        try{
73            this.registered = false;
74            if (libraryExists()){
75                Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(new ByteArrayInputStream(tokenConfiguration.getBytes()));
76                this.tokenProvider = pkcs11Provider;
77                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
78                    System.out.println("Adding provider: "+pkcs11Provider.getName());
79                    System.out.println("Provider info: " + pkcs11Provider.getInfo());
80                }
81                Security.addProvider(pkcs11Provider);
82                this.setName(this.tokenProvider.getName());
83                this.registered = true;
84            }
85        }
86        catch (ProviderException e){
87            if (setup.getParameter("debug").equalsIgnoreCase("true")) {
88                e.printStackTrace();
89                System.out.println("Não foi possível inicializar o seguinte token: " + tokenConfiguration);
90            }
91        }
92    }
93
94    protected void unregisterToken(){
95        Security.removeProvider(this.tokenProvider.getName());
96        this.registered = false;
97    }
98
99    public boolean libraryExists(){
100
101        File libraryFile = new File(libraryPath);
102        if (libraryFile.exists()){
103            if (setup.getParameter("debug").equalsIgnoreCase("true")) {
104                System.out.println("Arquivo " + libraryPath + " existe.");
105            }
106            return true;
107        }
108       
109        if (setup.getParameter("debug").equalsIgnoreCase("true")) {
110            System.out.println("Biblioteca do Token/SmartCard " + name + " não foi encontrada: " + libraryPath);
111        }
112       
113        return false;
114    }
115   
116}
Note: See TracBrowser for help on using the repository browser.