Changeset 5898


Ignore:
Timestamp:
04/09/12 15:21:57 (12 years ago)
Author:
rafaelraymundo
Message:

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

Location:
branches/2.3
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/2.3/expressoMail1_2/index.php

    r5319 r5898  
    182182    var sentfolder = "'.$_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder'].'"; 
    183183    var spamfolder = "'.$_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSpamFolder'].'"; 
     184    var locale = "'.$GLOBALS['phpgw']->translation->userlang.'"; 
    184185    var token_param = "'.$var_tokens.'"; 
    185     var locale = "'.$GLOBALS['phpgw']->common->getPreferredLanguage().'"; 
     186 
    186187    </script>'; 
    187  
     188     
    188189    // End Set Imap Folder names options 
    189190        //User info 
     
    221222        echo '<script type="text/javascript">init();</script>'; 
    222223 
    223         // Maybe it's better to put locale in another place. 
    224         $locale = $GLOBALS['phpgw']->translation->userlang; 
    225         echo '<input type="hidden" id="locale" name="locale" value="'.$locale.'" />'; 
    226  
    227224        if ($GLOBALS['phpgw_info']['user']['preferences']['expressoMail']['use_shortcuts']) 
    228225        { 
  • branches/2.3/expressoMail1_2/js/main.js

    r5319 r5898  
    137137        var search_div = Element('search_div'); 
    138138        var applet = null; 
    139         var locale = Element('locale').value; 
    140139        if (navigator.userAgent.match('MSIE')){ 
    141140                applet = document.createElement('<object style="display:yes;width:0;height:0;vertical-align:bottom;" id="cert_applet" ' + 
     
    158157                                'commons-httpclient-3.1.jar,' + 
    159158                                'commons-logging-1.1.1.jar' 
    160                         //debug:'true' 
     159                        //debug:'false' 
    161160                } 
    162161 
  • branches/2.3/phpgwapi/templates/default/login_default.php

    r5371 r5898  
    505505                $cod_applet = 
    506506 
    507 /*    // com debug ativado 
    508             '<script type="text/javascript"> 
     507    // com debug ativado 
     508/*            '<script type="text/javascript"> 
    509509                                        if (navigator.userAgent.match(\'MSIE\')){ 
    510510                                                document.write(\'<object style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" \' + 
     
    513513                                                \'<param name="codebase" value="/security/">\' + 
    514514                                                \'<param name="code" value="LoginApplet.class"> \' + 
    515                                                 \'<param name="locale" value="' . $lang . '"> \' + 
     515                                                \'<param name="locale" value="' . $GLOBALS['phpgw']->translation->userlang . '"> \' + 
    516516                                                \'<param name="mayscript" value="true"> \' + ' 
    517517                                                . $param1 
     
    521521                                        } 
    522522                                        else { 
    523                                                 document.write(\'<embed style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" codebase="/security/" code="LoginApplet.class" locale="' . $lang . '"\' + 
     523                                                document.write(\'<embed style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" codebase="/security/" code="LoginApplet.class" locale="' . $GLOBALS['phpgw']->translation->userlang . '"\' + 
    524524                                                \'archive="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar" \' + ' 
    525525                                                . $param2 
  • branches/2.3/preferences/handlecertificate.php

    r5010 r5898  
    156156                                                                                       "; 
    157157                    $cod_applet = 
    158         /*    // com debug ativado 
    159                 '<script type="text/javascript"> 
    160                                             if (navigator.userAgent.match(\'MSIE\')){ 
    161                                                     document.write(\'<object style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" \' + 
    162                                                     \'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> \' + 
    163                                                     \'<param name="type" value="application/x-java-applet;version=1.5"> \' + 
    164                                                     \'<param name="code" value="LoginApplet.class"> \' + 
    165                                                     \'<param name="locale" value="' . $lang . '"> \' + 
    166                                                     \'<param name="mayscript" value="true"> \' + ' 
    167                                                     . $param1 
    168                                                     . ' \'<param name="archive" value="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar"> \' + 
    169                             \'<param name="debug" value="true"> \' + 
    170                                                     \'</object>\'); 
    171                                             } 
    172                                             else { 
    173                                                     document.write(\'<embed style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" code="LoginApplet.class" locale="' . $lang . '"\' + 
    174                                                     \'archive="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar" \' + ' 
    175                                                     . $param2 
    176                                                     . ' \'type="application/x-java-applet;version=1.5" debug= "true" mayscript > \' + 
    177                                                     \'<noembed> \' + 
    178                                                     \'No Java Support. \' + 
    179                                                     \'</noembed> \' + 
    180                                                     \'</embed> \'); 
    181                                             } 
    182                                     </script>'; 
    183         */ 
    184                 // sem debug ativado 
    185                 '<script type="text/javascript"> 
    186                                             if (navigator.userAgent.match(\'MSIE\')){ 
    187                                                     document.write(\'<object style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" \' + 
    188                                                     \'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> \' + 
    189                                                     \'<param name="type" value="application/x-java-applet;version=1.5"> \' + 
    190                                                     \'<param name="codebase" value="/security/">\' + 
    191                                                     \'<param name="code" value="LoginApplet.class"> \' + 
    192                                                     \'<param name="locale" value="' . $lang . '"> \' + 
    193                                                     \'<param name="mayscript" value="true"> \' + ' 
    194                                                     . $param1 
    195                                                     . ' \'<param name="archive" value="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar"> \' + 
    196                                                     \'</object>\'); 
    197                                             } 
    198                                             else { 
    199                                                     document.write(\'<embed style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" codebase="/security/" code="LoginApplet.class" locale="' . $lang . '"\' + 
    200                                                     \'archive="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar" \' + ' 
    201                                                     . $param2 
    202                                                     . ' \'type="application/x-java-applet;version=1.5" mayscript > \' + 
    203                                                     \'<noembed> \' + 
    204                                                     \'No Java Support. \' + 
    205                                                     \'</noembed> \' + 
    206                                                     \'</embed> \'); 
    207                                             } 
    208                                     </script>'; 
     158/* 
     159// com debug ativado 
     160            '<script type="text/javascript"> 
     161                                        if (navigator.userAgent.match(\'MSIE\')){ 
     162                                                document.write(\'<object style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" \' + 
     163                                                \'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> \' + 
     164                                                \'<param name="type" value="application/x-java-applet;version=1.5"> \' + 
     165                                                \'<param name="codebase" value="/security/">\' + 
     166                                                \'<param name="code" value="LoginApplet.class"> \' + 
     167                                                \'<param name="locale" value="' . $GLOBALS['phpgw']->translation->userlang . '"> \' + 
     168                                                \'<param name="mayscript" value="true"> \' + ' 
     169                                                . $param1 
     170                                                . ' \'<param name="archive" value="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar"> \' + 
     171                        \'<param name="debug" value="true"> \' + 
     172                                                \'</object>\'); 
     173                                        } 
     174                                        else { 
     175                                                document.write(\'<embed style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" codebase="/security/" code="LoginApplet.class" locale="' . $GLOBALS['phpgw']->translation->userlang . '"\' + 
     176                                                \'archive="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar" \' + ' 
     177                                                . $param2 
     178                                                . ' \'type="application/x-java-applet;version=1.5" debug= "true" mayscript > \' + 
     179                                                \'<noembed> \' + 
     180                                                \'No Java Support. \' + 
     181                                                \'</noembed> \' + 
     182                                                \'</embed> \'); 
     183                                        } 
     184                                </script>'; 
     185*/ 
     186            // sem debug ativado 
     187            '<script type="text/javascript"> 
     188                                        if (navigator.userAgent.match(\'MSIE\')){ 
     189                                                document.write(\'<object style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" \' + 
     190                                                \'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> \' + 
     191                                                \'<param name="type" value="application/x-java-applet;version=1.5"> \' + 
     192                                                \'<param name="codebase" value="/security/">\' + 
     193                                                \'<param name="code" value="LoginApplet.class"> \' + 
     194                                                \'<param name="locale" value="' . $GLOBALS['phpgw']->translation->userlang . '"> \' + 
     195                                                \'<param name="mayscript" value="true"> \' + ' 
     196                                                . $param1 
     197                                                . ' \'<param name="archive" value="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar"> \' + 
     198                                                \'</object>\'); 
     199                                        } 
     200                                        else { 
     201                                                document.write(\'<embed style="display:yes;width:0;height:0;vertical-align:bottom;" id="login_applet" codebase="/security/" code="LoginApplet.class" locale="' . $GLOBALS['phpgw']->translation->userlang . '"\' + 
     202                                                \'archive="ExpressoCertLogin.jar,ExpressoCert.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,commons-codec-1.3.jar,bcmail-jdk15-142.jar,mail.jar,activation.jar,bcprov-jdk15-142.jar" \' + ' 
     203                                                . $param2 
     204                                                . ' \'type="application/x-java-applet;version=1.5" mayscript > \' + 
     205                                                \'<noembed> \' + 
     206                                                \'No Java Support. \' + 
     207                                                \'</noembed> \' + 
     208                                                \'</embed> \'); 
     209                                        } 
     210                                </script>'; 
     211 
    209212                  echo $cod_applet; 
    210213                  echo '<form id="certificateForm" name="certificateForm" method="POST" action="handlecertificate.php" >'; 
  • branches/2.3/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java

    r5386 r5898  
    6565import java.security.AlgorithmParameters; 
    6666import java.security.cert.CertificateEncodingException; 
     67import java.text.DateFormat; 
    6768import java.util.HashMap; 
     69import java.util.Locale; 
    6870import java.util.regex.Matcher; 
    6971import java.util.regex.Pattern; 
     
    109111    // caso seja necessário. 
    110112    private int keystoreStatus; 
     113    private static boolean useMSCapi = false; 
    111114    public static final int KEYSTORE_DETECTED = 0; 
    112115    public static final int KEYSTORE_NOT_DETECTED = 1; 
     
    133136            HOME_SUBDIR = "\\dados de aplicativos\\sun\\java\\deployment\\security"; 
    134137            EPASS_2000 = System.getenv("SystemRoot") + "\\system32\\ngp11v211.dll"; 
    135             //EPASS_2000 = System.getenv("ProgramFiles")+"\\Gemplus\\GemSafe Libraries\\BIN\\gclib.dll"; 
     138            DigitalCertificate.useMSCapi = true; 
    136139        } 
    137140 
     
    201204    } 
    202205 
     206    public static boolean isUseMSCapi() { 
     207        return useMSCapi; 
     208    } 
     209 
    203210    public KeyStore getKeyStore() { 
    204211        return keyStore; 
     
    219226    public void destroy() { 
    220227 
    221         AuthProvider ap = null; 
    222  
    223         if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
    224             System.out.println("logout no provider"); 
    225         } 
    226         if (keyStore != null) { 
    227             ap = (AuthProvider) this.keyStore.getProvider(); 
    228         } 
    229  
    230         if (ap != null) { 
    231             try { 
    232                 ap.logout(); 
    233             } catch (LoginException e) { 
    234                 if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
    235                     e.printStackTrace(); 
    236                 } 
    237             } 
    238         } 
    239  
    240         if (providerName != null) { 
    241             Security.removeProvider(providerName); 
    242         } 
    243  
    244         this.cert = null; 
     228        AuthProvider ap = null; 
     229 
     230        if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     231            System.out.println("logout no provider"); 
     232        } 
     233         
     234        if (this.keyStore != null && this.keyStore.getProvider() instanceof AuthProvider) { 
     235            ap = (AuthProvider) this.keyStore.getProvider(); 
     236         
     237            try { 
     238                ap.logout(); 
     239            } catch (LoginException e) { 
     240                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     241                    e.printStackTrace(); 
     242                } 
     243            } 
     244 
     245            if (this.providerName != null) { 
     246                Security.removeProvider(providerName); 
     247            } 
     248        } 
     249         
     250        this.cert = null; 
    245251        this.selectedCertificateAlias = null; 
    246         this.keyStore = null; 
    247         this.pkcs12Input = null; 
    248         this.providerName = null; 
    249  
     252        this.keyStore = null; 
     253        this.pkcs12Input = null; 
     254        this.providerName = null; 
    250255    } 
    251256 
     
    262267    public int init() { 
    263268 
    264         // TODO: Usar dentro de um "loop" para testar outros modelos de tokens. 
    265         this.tokens = new TokenCollection(setup); 
     269        if (!DigitalCertificate.useMSCapi){ 
     270            this.tokens = new TokenCollection(setup); 
     271        } 
    266272 
    267273        Provider[] providers = Security.getProviders(); 
     
    272278        } 
    273279 
    274         int interfaceType = DigitalCertificate.KEYSTORE_DETECTED; 
     280        int interfaceType = DigitalCertificate.KEYSTORE_NOT_DETECTED; 
    275281 
    276282        try { 
     
    312318            try { 
    313319                // Testa se uma keystore já foi carregada previamente 
    314                 if (keyStore.getType().equalsIgnoreCase("pkcs11")) { 
     320                if (keyStore.getType().equalsIgnoreCase("pkcs11")  
     321                        || keyStore.getType().equalsIgnoreCase("windows-my")) { 
    315322                    keyStore.load(null, null); 
    316323                } else { 
     
    319326 
    320327                // Se chegou aqui KeyStore está liberada, mostrar tela de login sem pedir o pin. 
    321                 this.keystoreStatus = DigitalCertificate.KEYSTORE_ALREADY_LOADED; 
     328                this.keystoreStatus = DigitalCertificate.useMSCapi ? DigitalCertificate.KEYSTORE_DETECTED : DigitalCertificate.KEYSTORE_ALREADY_LOADED ; 
    322329 
    323330            } catch (ProviderException e) { 
     
    402409                        while (headers.hasMoreElements()){ 
    403410                            String header = (String) headers.nextElement(); 
    404                             System.out.println("header: "+header); 
    405411                            headersString += header+"\r\n"; 
    406412                        } 
     
    455461    public String signMail(Map<String, String> data) throws IOException, GeneralSecurityException, SMIMEException, MessagingException { 
    456462 
    457         Key privateKey = null; 
     463        Key privateKey = null; 
    458464        if (this.keystoreStatus == DigitalCertificate.KEYSTORE_DETECTED) { 
    459             String pin = DialogBuilder.showPinDialog(this.parentFrame, this.setup); 
    460             if (pin != null) { 
    461                 openKeyStore(pin.toCharArray()); 
    462                 if (this.selectedCertificateAlias == null){ 
     465            char[] pin = null; 
     466            if (!DigitalCertificate.useMSCapi) { 
     467                String sPin = DialogBuilder.showPinDialog(this.parentFrame, this.setup); 
     468                if (sPin != null) { 
     469                    pin = sPin.toCharArray(); 
     470                } 
     471                else { 
    463472                    return null; 
    464473                } 
    465                 privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin.toCharArray()); 
    466             } else { 
    467                 return null; 
    468             } 
     474            } 
     475 
     476            try { 
     477                openKeyStore(pin); 
     478            } 
     479            catch (Exception e) 
     480            { 
     481                if (e instanceof IOException){ 
     482                    throw new IOException(e); 
     483                } 
     484                else if (e instanceof GeneralSecurityException){ 
     485                    throw new GeneralSecurityException(e); 
     486                } 
     487            } 
     488             
     489            if (this.selectedCertificateAlias == null){ 
     490                return null; 
     491            } 
     492            privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin); 
     493             
    469494        } /* 
    470495        else if (this.keystoreStatus == DigitalCertificate.KEYSTORE_ALREADY_LOADED){ 
     
    671696        Key privateKey = null; 
    672697        if (this.keystoreStatus == DigitalCertificate.KEYSTORE_DETECTED) { 
    673             String pin = DialogBuilder.showPinDialog(this.parentFrame, this.setup); 
    674             if (pin != null) { 
    675                 openKeyStore(pin.toCharArray()); 
    676                 if (this.selectedCertificateAlias == null){ 
    677                     return null; 
    678                 } 
    679                 privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin.toCharArray()); 
    680             } else { 
    681                 return null; 
    682             } 
     698            char[] pin = null; 
     699            if (!DigitalCertificate.useMSCapi) { 
     700               String sPin = DialogBuilder.showPinDialog(this.parentFrame, this.setup); 
     701               if (sPin != null) { 
     702                   pin = sPin.toCharArray(); 
     703               } 
     704               else { 
     705                   return null; 
     706               } 
     707            } 
     708 
     709            openKeyStore(pin); 
     710            if (this.selectedCertificateAlias == null){ 
     711                return null; 
     712            } 
     713            privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin); 
     714             
    683715        } /* 
    684716        else if (this.keystoreStatus == DigitalCertificate.KEYSTORE_ALREADY_LOADED){ 
     
    847879                if (Integer.parseInt(resposta[0].trim()) == 0) { 
    848880                    // Se código da resposta for zero, decripta a senha criptografada do usuário 
    849                     resposta[2] = decriptPassword(resposta[2].trim(), pin); 
     881                    resposta[2] = decryptPassword(resposta[2].trim(), pin); 
    850882                } 
    851883            } 
     
    875907     * @throws GeneralSecurityException se algum problema ocorrer na decriptação da senha. 
    876908     */ 
    877     public String decriptPassword(String encodedPassword, String pin) throws GeneralSecurityException { 
    878  
    879         String decodedPassword = new String(); 
    880  
    881         // Pega a chave privada do primeiro certificado armazenado na KeyStore 
    882         Key privateKey = this.keyStore.getKey(selectedCertificateAlias, pin.toCharArray()); 
     909    public String decryptPassword(String encodedPassword, String pin) throws GeneralSecurityException, IOException { 
     910 
     911        String decodedPassword = new String(); 
     912 
     913        // Pega a chave privada do primeiro certificado armazenado na KeyStore 
     914        Key privateKey = pin == null ? this.keyStore.getKey(selectedCertificateAlias, null) : 
     915            this.keyStore.getKey(selectedCertificateAlias, pin.toCharArray()); 
     916 
     917        Cipher dcipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
     918        dcipher.init(Cipher.DECRYPT_MODE, privateKey); 
    883919 
    884920        // Inicializa os cipher com os parâmetros corretos para realizar a decriptação 
    885         Cipher dcipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
    886         dcipher.init(Cipher.DECRYPT_MODE, privateKey); 
     921//      Cipher dcipher = DigitalCertificate.useMSCapi ? 
     922//            Cipher.getInstance("RSA/ECB/PKCS1Padding", Security.getProvider(this.providerName)) 
     923//            : Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
     924//      dcipher.init(Cipher.DECRYPT_MODE, privateKey); 
    887925 
    888926        // Decodifica a senha em base64 e a decripta 
    889         decodedPassword = new String(dcipher.doFinal(Base64Utils.base64Decode(encodedPassword))); 
    890  
     927        decodedPassword = new String(dcipher.doFinal(Base64Utils.base64Decode(encodedPassword))); 
     928        
    891929        return decodedPassword.trim(); 
    892930 
     
    902940 
    903941        try{ 
    904             if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
    905                 System.out.println("Carregando provider: PKCS11"); 
    906             } 
    907             this.keyStore = KeyStore.getInstance("PKCS11"); 
    908             this.providerName = keyStore.getProvider().getName(); 
     942            if (!DigitalCertificate.useMSCapi) { 
     943                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     944                    System.out.println("Carregando provider: PKCS11"); 
     945                } 
     946                this.keyStore = KeyStore.getInstance("PKCS11"); 
     947                this.providerName = keyStore.getProvider().getName(); 
     948            } 
     949            else { 
     950                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     951                    System.out.println("Carregando provider: SunMSCAPI"); 
     952                } 
     953                this.keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); 
     954                this.providerName = this.keyStore.getProvider().getName(); 
     955 
     956                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     957                    System.out.println(keyStore.getProvider().getName() +" carregado!"); 
     958                } 
     959            } 
    909960        } 
    910961        catch (GeneralSecurityException kex){ 
     
    921972    } 
    922973 
     974    Map<String, String> getAliasesList() throws IOException, KeyStoreException{ 
     975 
     976        if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     977            System.out.println("Getting Aliases"); 
     978        } 
     979 
     980        Map<String, String> aliases = new HashMap<String, String>(); 
     981       
     982        for (Enumeration<String> al = this.keyStore.aliases(); al.hasMoreElements();){ 
     983            String alias = al.nextElement(); 
     984            X509Certificate certObj = (X509Certificate) this.keyStore.getCertificate(alias); 
     985             
     986            StringBuilder selector = new StringBuilder(); 
     987            // get more info to generate the value 
     988            // Subject's CN / Issuer's CN / Expiration Data 
     989            String subject = certObj.getSubjectX500Principal().getName(); 
     990            int pInicial = subject.indexOf('=')+1; 
     991            int pFinal = subject.indexOf(',', pInicial); 
     992            selector.append(subject.substring(pInicial, pFinal)+" | "); 
     993 
     994            String issuer = certObj.getIssuerX500Principal().getName(); 
     995            pInicial = issuer.indexOf('=')+1; 
     996            pFinal = issuer.indexOf(',', pInicial); 
     997            selector.append(issuer.substring(pInicial, pFinal)+" | "); 
     998 
     999            // TODO: get the system locale 
     1000            Locale locale = new Locale("pt", "BR"); 
     1001            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 
     1002            selector.append(df.format(certObj.getNotAfter())+" | "); 
     1003 
     1004            selector.append("("+certObj.getSerialNumber()+")"); 
     1005 
     1006            aliases.put(alias, selector.toString()); 
     1007             
     1008        } 
     1009 
     1010        return aliases; 
     1011    } 
     1012 
     1013//    public void removeCertificate() throws IOException { 
     1014//        Token token = tokens.getRegisteredTokens().iterator().next(); 
     1015//        token.removeCertificate(); 
     1016//    } 
     1017// 
     1018//    public void writeCerts(char[] pin, LinkedHashMap<char[], Certificate> certs) throws IOException{ 
     1019//        Token token = tokens.getRegisteredTokens().iterator().next(); 
     1020//        token.getAliases(); 
     1021// 
     1022//        for (Map.Entry<char[], Certificate> entry : certs.entrySet()) { 
     1023//            token.writeCert(pin, entry.getKey(), entry.getValue()); 
     1024//        } 
     1025//    } 
     1026 
    9231027    /** 
    9241028     *  Abre a keystore passando o pin 
    9251029     *  @param pin pin para acessar o Token 
    9261030     */ 
     1031    @SuppressWarnings("empty-statement") 
    9271032    public void openKeyStore(char[] pin) throws IOException { 
    9281033        // TODO:  Verify if object DigitalCertificate was initiated 
     
    9311036            if (this.keyStore.getType().equals("PKCS11")) { 
    9321037                this.keyStore.load(null, pin); 
    933             } else { 
     1038            } else if (this.keyStore.getType().equals("Windows-MY")) { 
     1039                this.keyStore.load(null, null); 
     1040            } else { 
    9341041                this.keyStore.load(this.pkcs12Input, pin); 
    9351042            } 
    9361043 
    937             Map<String, String> aliases = new HashMap<String, String>(); 
    938             for (Token token : tokens.getRegisteredTokens()){ 
    939                 aliases.putAll(token.getAliases()); 
    940             } 
    941  
    9421044            // selecionador de certificado 
    943             this.selectedCertificateAlias = DialogBuilder.showCertificateSelector(this.parentFrame, this.setup, aliases); 
     1045            this.selectedCertificateAlias = DialogBuilder.showCertificateSelector(this.parentFrame, 
     1046                    this.setup, this.getAliasesList()); 
    9441047            if (this.selectedCertificateAlias != null){ 
    9451048                this.cert = this.keyStore.getCertificate(this.selectedCertificateAlias); 
  • branches/2.3/security/ExpressoCert/src/br/gov/serpro/cert/Token.java

    r5179 r5898  
    1313import java.security.ProviderException; 
    1414import java.security.Security; 
    15 import java.security.cert.CertificateFactory; 
    16 import java.util.logging.Level; 
    17 import java.util.logging.Logger; 
    18 import java.security.cert.X509Certificate; 
    19 import java.text.DateFormat; 
    20 import java.util.HashMap; 
    21 import java.util.Locale; 
    22 import java.util.Map; 
    23 import sun.security.pkcs11.wrapper.CK_ATTRIBUTE; 
    24 import sun.security.pkcs11.wrapper.CK_C_INITIALIZE_ARGS; 
    25 import sun.security.pkcs11.wrapper.Functions; 
    26 import sun.security.pkcs11.wrapper.PKCS11; 
    27 import sun.security.pkcs11.wrapper.PKCS11Exception; 
    28 import static sun.security.pkcs11.wrapper.PKCS11Constants.*; 
    2915 
    3016//TODO: Deal with wildcards for environments variables. 
     
    11197    } 
    11298 
    113     Map<String, String> getAliases() throws IOException{ 
    114  
    115         if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    116             System.out.println("Getting Aliases"); 
    117         } 
    118  
    119         Map<String, String> aliases = new HashMap<String, String>(); 
    120  
    121         CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); 
    122         String functionList = "C_GetFunctionList"; 
    123  
    124         initArgs.flags = CKF_OS_LOCKING_OK; 
    125  
    126         PKCS11 tmpPKCS11 = null; 
    127         try { 
    128             try { 
    129                 tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, false); 
    130             } catch (IOException ex) { 
    131                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    132                     Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
    133                 } 
    134                 throw ex; 
    135             } 
    136         } catch (PKCS11Exception e) { 
    137             try { 
    138                 initArgs = null; 
    139                 tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, true); 
    140             } catch (IOException ex) { 
    141                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    142                     Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
    143                 } 
    144             } catch (PKCS11Exception ex) { 
    145                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    146                     Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
    147                 } 
    148             } 
    149         } 
    150  
    151         try { 
    152             // cria sessão pública rw. com flag CKF_SERIAL_SESSION 
    153             long session = tmpPKCS11.C_OpenSession(this.slot, CKF_SERIAL_SESSION, null, null); 
    154  
    155             if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    156                 System.out.println("session number:  "+session); 
    157             } 
    158  
    159             // TODO: Verifica se está logado, senão loga usuário. Pede pin? ou recebe pin? 
    160  
    161             CK_ATTRIBUTE[] TEMPLATE_CERTIFICATE = {new CK_ATTRIBUTE(CKA_CLASS, CKO_CERTIFICATE)}; 
    162             CK_ATTRIBUTE[] TEMPLATE_PKEY = {new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY)}; 
    163             CK_ATTRIBUTE[] TEMPLATE_KEY_LABEL_ID = {new CK_ATTRIBUTE(CKA_LABEL), new CK_ATTRIBUTE(CKA_ID)}; 
    164             CK_ATTRIBUTE[] TEMPLATE_CERT_LABEL_ID = { 
    165                 new CK_ATTRIBUTE(CKA_LABEL), 
    166                 new CK_ATTRIBUTE(CKA_ID), 
    167                 new CK_ATTRIBUTE(CKA_VALUE) 
    168             }; 
    169  
    170             tmpPKCS11.C_FindObjectsInit(session, TEMPLATE_CERTIFICATE); 
    171             long[] certs = tmpPKCS11.C_FindObjects(session, 20); 
    172  
    173             tmpPKCS11.C_FindObjectsFinal(session); 
    174  
    175             tmpPKCS11.C_FindObjectsInit(session, TEMPLATE_PKEY); 
    176             long[] keys = tmpPKCS11.C_FindObjects(session, 20); 
    177  
    178             if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    179                 System.out.println("Private Keys: "+keys.length); 
    180             } 
    181  
    182             for (long key : keys){ 
    183                 tmpPKCS11.C_GetAttributeValue(session, key, TEMPLATE_KEY_LABEL_ID); 
    184  
    185                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    186                     System.out.print("Private key ID: "); 
    187                     for (byte b : (byte [])TEMPLATE_KEY_LABEL_ID[1].pValue){ 
    188                         System.out.print(b); 
    189                     } 
    190                     System.out.println(); 
    191                     if (TEMPLATE_KEY_LABEL_ID[0].pValue != null) 
    192                     { 
    193                         System.out.println("Private key LABEL: "+new String((char [])TEMPLATE_KEY_LABEL_ID[0].pValue)); 
    194                     } 
    195                 } 
    196  
    197                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    198                     System.out.println("Certs:"); 
    199                 } 
    200                  
    201                 for (long cert : certs){ 
    202                     tmpPKCS11.C_GetAttributeValue(session, cert, TEMPLATE_CERT_LABEL_ID); 
    203  
    204                     if (Functions.equals((byte [])TEMPLATE_KEY_LABEL_ID[1].pValue,  
    205                             (byte [])TEMPLATE_CERT_LABEL_ID[1].pValue)){ 
    206                         if (TEMPLATE_CERT_LABEL_ID[0].pValue != null) 
    207                         { 
    208                             if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    209                                 System.out.println("Certificate LABEL: "+new String((char [])TEMPLATE_CERT_LABEL_ID[0].pValue)); 
    210                             } 
    211                             ByteArrayInputStream in = new ByteArrayInputStream((byte [])TEMPLATE_CERT_LABEL_ID[2].pValue); 
    212                             CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
    213                             X509Certificate certObj = (X509Certificate)cf.generateCertificate(in); 
    214                             if (certObj.getBasicConstraints() == -1 ){ 
    215  
    216                                 StringBuilder selector = new StringBuilder(); 
    217                                 // get more info to generate the value 
    218                                 // Subject's CN / Issuer's CN / Expiration Data 
    219                                 String subject = certObj.getSubjectX500Principal().getName(); 
    220                                 int pInicial = subject.indexOf('=')+1; 
    221                                 int pFinal = subject.indexOf(',', pInicial); 
    222                                 selector.append(subject.substring(pInicial, pFinal)+" | "); 
    223  
    224                                 String issuer = certObj.getIssuerX500Principal().getName(); 
    225                                 pInicial = issuer.indexOf('=')+1; 
    226                                 pFinal = issuer.indexOf(',', pInicial); 
    227                                 selector.append(issuer.substring(pInicial, pFinal)+" | "); 
    228  
    229                                 Locale locale = new Locale("pt", "BR"); 
    230                                 DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 
    231                                 selector.append(df.format(certObj.getNotAfter())+" | "); 
    232                                  
    233                                 selector.append("("+certObj.getSerialNumber()+")"); 
    234                                  
    235                                 aliases.put(new String((char [])TEMPLATE_CERT_LABEL_ID[0].pValue), 
    236                                         selector.toString()); 
    237                             } 
    238                         } 
    239                     } 
    240  
    241                 } 
    242                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    243                     System.out.println(); 
    244                 } 
    245             } 
    246  
    247             tmpPKCS11.C_CloseSession(session); 
    248  
    249         } catch (PKCS11Exception ex) { 
    250             if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    251                 Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex); 
    252             } 
    253         } catch (Throwable t) { 
    254             if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    255                 Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, t); 
    256             } 
    257         } 
    258  
    259         return aliases; 
    260  
    261     } 
    262  
    26399    public boolean libraryExists(){ 
    264100 
  • branches/2.3/security/ExpressoCertLogin/src/LoginApplet.java

    r4124 r5898  
    176176                    case DigitalCertificate.KEYSTORE_DETECTED: 
    177177                        // Mostra PinNeedeDialog. 
    178                         String pin = DialogBuilder.showPinDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup); 
    179                          
    180                         if (pin != null) { 
    181                             dc.openKeyStore(pin.toCharArray()); 
     178                        char[] pin = null; 
     179                        if (!DigitalCertificate.isUseMSCapi()){ 
     180                            String sPin = DialogBuilder.showPinDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup); 
     181                            if (sPin != null) { 
     182                                pin = sPin.toCharArray(); 
     183                            } 
     184                        } 
     185                        if (DigitalCertificate.isUseMSCapi() || pin != null) { 
     186                            dc.openKeyStore(pin); 
    182187                            if (documentURL.getPath().matches(".*login.php$")){ 
    183                                 tryAgain = parseVercert(dc.getCredentials(pin, new URL(this.getCodeBase().getProtocol()+"://" + 
     188 
     189                                tryAgain = pin == null ? parseVercert(dc.getCredentials(null, new URL(this.getCodeBase().getProtocol()+"://" + 
     190                                    this.getCodeBase().getHost() + ":" + this.getCodeBase().getPort() + 
     191                                    "/security/vercert.php")), dc.getPEMCertificate()) : 
     192                                    parseVercert(dc.getCredentials(new String(pin), new URL(this.getCodeBase().getProtocol()+"://" + 
    184193                                    this.getCodeBase().getHost() + ":" + this.getCodeBase().getPort() + 
    185194                                    "/security/vercert.php")), dc.getPEMCertificate()); 
Note: See TracChangeset for help on using the changeset viewer.