Changeset 5024 for branches/2.3/security


Ignore:
Timestamp:
09/02/11 13:40:07 (13 years ago)
Author:
rafaelraymundo
Message:

Ticket #2256 - Token iKey 2032 não consegue acessar certificado do usuário.

Location:
branches/2.3/security
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/2.3/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java

    r4198 r5024  
    22 
    33import br.gov.serpro.setup.Setup; 
    4 import br.gov.serpro.cert.Token; 
    54import java.awt.Frame; 
    65import java.io.ByteArrayInputStream; 
     
    5958import java.io.OutputStreamWriter; 
    6059import java.security.AlgorithmParameters; 
    61 import java.security.NoSuchProviderException; 
    6260import java.security.cert.CertificateEncodingException; 
     61import java.util.HashMap; 
    6362import java.util.regex.Matcher; 
    6463import java.util.regex.Pattern; 
     
    787786            } 
    788787 
    789             List<String> aliases = new ArrayList<String>(); 
    790             for (Enumeration<String> certificateList = keyStore.aliases(); certificateList.hasMoreElements();){ 
    791                 aliases.add(certificateList.nextElement()); 
     788            Map<String, String> aliases = new HashMap<String, String>(); 
     789            for (Token token : tokens.getRegisteredTokens()){ 
     790                aliases.putAll(token.getAliases()); 
    792791            } 
    793792 
  • branches/2.3/security/ExpressoCert/src/br/gov/serpro/cert/Token.java

    r4198 r5024  
    99import java.io.ByteArrayInputStream; 
    1010import java.io.File; 
     11import java.io.IOException; 
    1112import java.security.Provider; 
    1213import java.security.ProviderException; 
    1314import java.security.Security; 
     15import java.security.cert.CertificateFactory; 
     16import java.util.logging.Level; 
     17import java.util.logging.Logger; 
     18import java.security.cert.X509Certificate; 
     19import java.util.HashMap; 
     20import java.util.Map; 
     21import sun.security.pkcs11.wrapper.CK_ATTRIBUTE; 
     22import sun.security.pkcs11.wrapper.CK_C_INITIALIZE_ARGS; 
     23import sun.security.pkcs11.wrapper.Functions; 
     24import sun.security.pkcs11.wrapper.PKCS11; 
     25import sun.security.pkcs11.wrapper.PKCS11Exception; 
     26import static sun.security.pkcs11.wrapper.PKCS11Constants.*; 
    1427 
    1528//TODO: Deal with wildcards for environments variables. 
     
    2639    private Provider tokenProvider; 
    2740    private boolean registered = false; 
     41    private long slot; 
     42 
     43    static long CK_OBJECT_CLASS; 
     44    static long CK_OBJECT_HANDLE; 
    2845 
    2946    private Token(final Setup setup) { 
     
    5774    } 
    5875 
    59     protected void registerToken(long slot){ 
    60  
     76    protected void registerToken(long slot) throws IOException{ 
     77 
     78        this.slot = slot; 
    6179        String tokenConfiguration = new String("name = " + name + "_" + slot + "\n" + 
    6280            "library = " + libraryPath + "\nslot = " + slot + 
    63             "\ndisabledMechanisms = {\n" + "CKM_SHA1_RSA_PKCS\n}"); 
     81            "\ndisabledMechanisms = {\n" + "CKM_SHA1_RSA_PKCS\n}" + 
     82            "\n"); 
    6483 
    6584        try{ 
     
    7190                    System.out.println("Adding provider: "+pkcs11Provider.getName()); 
    7291                    System.out.println("Provider info: " + pkcs11Provider.getInfo()); 
    73                     System.out.println("Provider services:"); 
    74                     for (Provider.Service service : pkcs11Provider.getServices()){ 
    75                         System.out.println("\t"+service.toString()); 
    76                     } 
    7792                } 
    7893                Security.addProvider(pkcs11Provider); 
     
    91106    protected void unregisterToken(){ 
    92107        Security.removeProvider(this.tokenProvider.getName()); 
     108        this.registered = false; 
     109    } 
     110 
     111    Map<String, String> getAliases() throws IOException{ 
     112 
     113        if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     114            System.out.println("Getting Aliases"); 
     115        } 
     116 
     117        Map<String, String> aliases = new HashMap<String, String>(); 
     118 
     119        CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); 
     120        String functionList = "C_GetFunctionList"; 
     121 
     122        initArgs.flags = CKF_OS_LOCKING_OK; 
     123 
     124        PKCS11 tmpPKCS11 = null; 
     125        try { 
     126            try { 
     127                tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, false); 
     128            } catch (IOException ex) { 
     129                if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     130                    Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
     131                } 
     132                throw ex; 
     133            } 
     134        } catch (PKCS11Exception e) { 
     135            try { 
     136                initArgs = null; 
     137                tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, true); 
     138            } catch (IOException ex) { 
     139                if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     140                    Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
     141                } 
     142            } catch (PKCS11Exception ex) { 
     143                if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     144                    Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
     145                } 
     146            } 
     147        } 
     148 
     149        try { 
     150            // cria sessão pública rw. com flag CKF_SERIAL_SESSION 
     151            long session = tmpPKCS11.C_OpenSession(this.slot, CKF_SERIAL_SESSION, null, null); 
     152 
     153            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     154                System.out.println("session number:  "+session); 
     155            } 
     156 
     157            // TODO: Verifica se está logado, senão loga usuário. Pede pin? ou recebe pin? 
     158 
     159            CK_ATTRIBUTE[] TEMPLATE_CERTIFICATE = {new CK_ATTRIBUTE(CKA_CLASS, CKO_CERTIFICATE)}; 
     160            CK_ATTRIBUTE[] TEMPLATE_PKEY = {new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY)}; 
     161            CK_ATTRIBUTE[] TEMPLATE_KEY_LABEL_ID = {new CK_ATTRIBUTE(CKA_LABEL), new CK_ATTRIBUTE(CKA_ID)}; 
     162            CK_ATTRIBUTE[] TEMPLATE_CERT_LABEL_ID = { 
     163                new CK_ATTRIBUTE(CKA_LABEL), 
     164                new CK_ATTRIBUTE(CKA_ID), 
     165                new CK_ATTRIBUTE(CKA_VALUE) 
     166            }; 
     167 
     168            tmpPKCS11.C_FindObjectsInit(session, TEMPLATE_CERTIFICATE); 
     169            long[] certs = tmpPKCS11.C_FindObjects(session, 20); 
     170 
     171            tmpPKCS11.C_FindObjectsFinal(session); 
     172 
     173            tmpPKCS11.C_FindObjectsInit(session, TEMPLATE_PKEY); 
     174            long[] keys = tmpPKCS11.C_FindObjects(session, 20); 
     175 
     176            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     177                System.out.println("Private Keys: "+keys.length); 
     178            } 
     179 
     180            for (long key : keys){ 
     181                tmpPKCS11.C_GetAttributeValue(session, key, TEMPLATE_KEY_LABEL_ID); 
     182 
     183                if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     184                    System.out.print("Private key ID: "); 
     185                    for (byte b : (byte [])TEMPLATE_KEY_LABEL_ID[1].pValue){ 
     186                        System.out.print(b); 
     187                    } 
     188                    System.out.println(); 
     189                    if (TEMPLATE_KEY_LABEL_ID[0].pValue != null) 
     190                    { 
     191                        System.out.println("Private key LABEL: "+new String((char [])TEMPLATE_KEY_LABEL_ID[0].pValue)); 
     192                    } 
     193                    System.out.println("\nCerts:"); 
     194                } 
     195 
     196                for (long cert : certs){ 
     197                    tmpPKCS11.C_GetAttributeValue(session, cert, TEMPLATE_CERT_LABEL_ID); 
     198 
     199                    if (Functions.equals((byte [])TEMPLATE_KEY_LABEL_ID[1].pValue,  
     200                            (byte [])TEMPLATE_CERT_LABEL_ID[1].pValue)){ 
     201                        if (TEMPLATE_CERT_LABEL_ID[0].pValue != null) 
     202                        { 
     203                            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     204                                System.out.println("Certificate LABEL: "+new String((char [])TEMPLATE_CERT_LABEL_ID[0].pValue)); 
     205                            } 
     206                            ByteArrayInputStream in = new ByteArrayInputStream((byte [])TEMPLATE_CERT_LABEL_ID[2].pValue); 
     207                            CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
     208                            X509Certificate certObj = (X509Certificate)cf.generateCertificate(in); 
     209                            if (certObj.getBasicConstraints() == -1 ){ 
     210                                aliases.put(new String((char [])TEMPLATE_CERT_LABEL_ID[0].pValue), 
     211                                        certObj.getSubjectX500Principal().getName()); 
     212                            } 
     213                        } 
     214                    } 
     215 
     216                } 
     217                if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     218                    System.out.println(); 
     219                } 
     220            } 
     221 
     222            tmpPKCS11.C_CloseSession(session); 
     223 
     224        } catch (PKCS11Exception ex) { 
     225            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     226                Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
     227            } 
     228        } catch (Throwable t) { 
     229            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     230                Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, t); 
     231            } 
     232        } 
     233 
     234        return aliases; 
     235 
    93236    } 
    94237 
  • branches/2.3/security/ExpressoCert/src/br/gov/serpro/cert/TokenCollection.java

    r4198 r5024  
    99import java.io.IOException; 
    1010import java.util.HashMap; 
     11import java.util.HashSet; 
     12import java.util.Set; 
    1113import java.util.logging.Level; 
    1214import java.util.logging.Logger; 
     
    125127 
    126128    } 
     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 
    127143} 
  • branches/2.3/security/ExpressoCert/src/br/gov/serpro/ui/DialogBuilder.java

    r3633 r5024  
    33import br.gov.serpro.setup.Setup; 
    44import java.awt.BorderLayout; 
    5 import java.awt.Color; 
    65import java.awt.Dimension; 
    76import java.awt.FlowLayout; 
     
    1817import java.lang.reflect.InvocationTargetException; 
    1918 
     19import java.util.ArrayList; 
    2020import java.util.List; 
     21import java.util.Map; 
     22import java.util.Set; 
    2123import javax.swing.BorderFactory; 
    2224import javax.swing.JButton; 
     
    332334    } 
    333335 
    334     static public String showCertificateSelector(Frame parent, Setup setup, List<String> certificateList) { 
     336    static public String showCertificateSelector(Frame parent, Setup setup, Map<String, String> certificateMap) { 
    335337        DialogBuilder certificateSelectorDialog = new DialogBuilder(parent, setup); 
     338 
     339        Set<Map.Entry<String, String>> entries = certificateMap.entrySet(); 
     340        List<String> certificateList = new ArrayList<String>(); 
     341 
     342        if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     343            System.out.println("Entradas:"); 
     344        } 
     345 
     346        for (Map.Entry<String, String> entry : entries){ 
     347            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     348                System.out.println("[" + entry.getKey() + " : " + entry.getValue() + "]"); 
     349            } 
     350            certificateList.add(entry.getValue()); 
     351        } 
    336352 
    337353        try { 
     
    356372 
    357373            String subject = certificateSelectorDialog.getCertificateSubject(); 
     374            String label = ""; 
     375 
     376            for (Map.Entry<String, String> entry : entries){ 
     377                if (entry.getValue().equals(subject)){ 
     378                    label = entry.getKey(); 
     379                    break; 
     380                } 
     381            } 
     382 
    358383            certificateSelectorDialog.dispose(); 
    359384            certificateSelectorDialog = null; 
    360385 
    361             return subject; 
     386            return label; 
    362387        } 
    363388    } 
Note: See TracChangeset for help on using the changeset viewer.