source: trunk/security/ExpressoCert/src/br/gov/serpro/cert/Token.java @ 1174

Revision 1174, 2.8 KB checked in by rafaelraymundo, 15 years ago (diff)

Ticket #558 - Altera nome de diretório seguranca para security.

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.security.Provider;
12import java.security.ProviderException;
13import java.security.Security;
14
15//TODO: Deal with wildcards for environments variables.
16
17/**
18 *
19 * @author esa
20 */
21class Token{
22
23    private final Setup setup;
24    private String name;
25    private String libraryPath;
26    private Provider tokenProvider;
27    private boolean registered = false;
28
29    private Token(final Setup setup) {
30        this.setup = setup;
31    }
32
33    Token(String name, String libraryPath, final Setup setup){
34        this(setup);
35        this.setName(name);
36        this.setLibraryPath(libraryPath);
37    }
38
39    public boolean isRegistered() {
40        return this.registered;
41    }
42
43    public void setLibraryPath(String libraryPath) {
44        this.libraryPath = libraryPath;
45    }
46
47    public void setName(String name) {
48        this.name = name;
49    }
50
51    public String getName() {
52        return this.name;
53    }
54
55    protected void registerToken(){
56        String tokenConfiguration = new String("name = " + name + "\n" +
57            "library = " + libraryPath + "\ndisabledMechanisms = {\n" +
58            "CKM_SHA1_RSA_PKCS\n}");
59
60        try{
61            this.registered = false;
62            if (libraryExists()){
63                Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(new ByteArrayInputStream(tokenConfiguration.getBytes()));
64                this.tokenProvider = pkcs11Provider;
65                Security.addProvider(pkcs11Provider);
66                this.registered = true;
67            }
68        }
69        catch (ProviderException e){
70            if (setup.getParameter("debug").equalsIgnoreCase("true")) {
71                e.printStackTrace();
72                System.out.println("Não foi possível inicializar o seguinte token: " + tokenConfiguration);
73            }
74        }
75    }
76
77    protected void unregisterToken(){
78        Security.removeProvider(this.tokenProvider.getName());
79    }
80
81/*    public KeyStore getKeystore() throws KeyStoreException {
82
83        return (this.keyStore = KeyStore.getInstance("PKCS11"));
84
85    }
86*/
87    public boolean libraryExists(){
88
89        File libraryFile = new File(libraryPath);
90        if (libraryFile.exists()){
91            if (setup.getParameter("debug").equalsIgnoreCase("true")) {
92                System.out.println("Arquivo " + libraryPath + " existe.");
93            }
94            return true;
95        }
96       
97        if (setup.getParameter("debug").equalsIgnoreCase("true")) {
98            System.out.println("Biblioteca do Token/SmartCard " + name + " não foi encontrada: " + libraryPath);
99        }
100       
101        return false;
102    }
103   
104}
Note: See TracBrowser for help on using the repository browser.