Changeset 56b06e2 in dispositivos_moviles


Ignore:
Timestamp:
Oct 15, 2013, 9:11:48 AM (11 years ago)
Author:
Antonio Araujo Brett <aaraujo@…>
Branches:
master
Children:
6b383b3
Parents:
6638d33
Message:

*- Carga de certificados de servidores confiables al momento de descargar archivos a través de https.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TibisayMovil/src/ve/gob/cenditel/tibisaymovil/DownloaderActivity.java

    r6638d33 r56b06e2  
    66import java.io.FileInputStream;
    77import java.io.FileOutputStream;
     8import java.io.IOException;
    89import java.io.InputStream;
    910import java.io.OutputStream;
     11import java.lang.reflect.Field;
    1012import java.net.HttpURLConnection;
    1113import java.net.URL;
     
    7779                if(customTitleSupported)
    7880                        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
     81               
     82                // ------------------------------------------------------------------------------------
     83                // el siguiente segmento de codigo permite obtener los nombres de los archivos
     84                // que estan en /res/raw
     85                ArrayList<String> list = new ArrayList<String>();
     86                Field[] fields = R.raw.class.getFields();
     87               
     88                for (int l=0; l < fields.length; l++) {
     89                        try {
     90                                Toast.makeText(getApplicationContext(), "DownloaderActivity: "+Integer.toString(l)+ " "+ Integer.toString(fields[l].getInt(null)), Toast.LENGTH_SHORT).show();
     91                        } catch (IllegalArgumentException e) {
     92                                // TODO Auto-generated catch block
     93                                Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalArgumentException "+e.getMessage(), Toast.LENGTH_SHORT).show();
     94                        } catch (IllegalAccessException e) {
     95                                // TODO Auto-generated catch block
     96                                Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalAccessException "+e.getMessage(), Toast.LENGTH_SHORT).show();
     97                        }
     98                }
     99               
     100                /*
     101                for(Field f : fields)
     102                try {
     103                        Toast.makeText(getApplicationContext(), "DownloaderActivity: Fields.length: "+Integer.toString(fields.length), Toast.LENGTH_SHORT).show();
     104                        Toast.makeText(getApplicationContext(), "DownloaderActivity: "+f.getName(), Toast.LENGTH_SHORT).show();
     105                        //list.add(splitString[0]);
     106                } catch (IllegalArgumentException e) {
     107                        Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalArgumentException "+e.getMessage(), Toast.LENGTH_SHORT).show();
     108                }
     109                */
     110                // ------------------------------------------------------------------------------------
    79111               
    80112               
     
    287319                               
    288320                                // descarga del certificado del servidor
     321                               
     322                                // crear certificate factory
    289323                                CertificateFactory cf = CertificateFactory.getInstance("X.509");
    290                                 //InputStream caInput = new BufferedInputStream(new FileInputStream(rootDir+"/tibisay.cenditel.gob.ve.pem"));
    291                                 //InputStream caInput = new BufferedInputStream(new FileInputStream(rootDir+"/tibisay.pem"));
     324                               
     325                                // crear un KeyStore
     326                                String keyStoreType = KeyStore.getDefaultType();
     327                        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
     328                        keyStore.load(null, null);
     329                               
     330                        // obtener vector de campos de /res/raw
     331                                Field[] fields = R.raw.class.getFields();
     332                               
     333                                InputStream caInput = null;
     334                               
     335                                // iterar sobre todos los certificados incluidos en /res/raw y cargarlos
     336                                // en el KeyStore
     337                                for (int l=0; l < fields.length; l++) {
     338                                        try {
     339                                                Toast.makeText(getApplicationContext(), "DownloaderActivity: "+Integer.toString(l)+ " "+
     340                                                        Integer.toString(fields[l].getInt(null)), Toast.LENGTH_SHORT).show();
     341
     342                                                caInput = new BufferedInputStream(getResources().openRawResource(fields[l].getInt(null)));
     343
     344                                        } catch (IllegalArgumentException e) {
     345                                                // TODO Auto-generated catch block
     346                                                Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalArgumentException "+e.getMessage(),                                         Toast.LENGTH_SHORT).show();
     347                                        } catch (IllegalAccessException e) {
     348                                                // TODO Auto-generated catch block
     349                                                Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalAccessException "+e.getMessage(),                                   Toast.LENGTH_SHORT).show();
     350                                        }
     351                                                                         
     352                            Certificate ca;
     353                            try {
     354                                ca = cf.generateCertificate(caInput);
     355                                //Log.d("**ca:", ((X509Certificate) ca).getSubjectDN());
     356                                           
     357                                //Log.d("*** try: ca.toString()", ca.toString());
     358                                } finally {
     359                                    caInput.close();
     360                                }
     361                                        keyStore.setCertificateEntry("ca"+Integer.toString(l), ca);
     362                                }// fin de la iteracion sobre /res/raw
     363
     364                                // Create a TrustManager that trusts the CAs in our KeyStore
     365                                String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
     366                                TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
     367                                tmf.init(keyStore);
     368                                Log.d("***", "despues de crear TrustManager");
     369
     370                                // Create an SSLContext that uses our TrustManager
     371                                SSLContext context = SSLContext.getInstance("TLS");
     372                                context.init(null, tmf.getTrustManagers(), null);
     373                                Log.d("***", "despues de crear SSLContext");
     374                               
     375                                HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection();
     376                                Log.d("***", "despues de crear httpsConnection");
     377                               
     378                                httpsConnection.setSSLSocketFactory(context.getSocketFactory());
     379                                Log.d("***", "despues de crear setSSLSocketFactory");
     380                               
     381                                //input = new BufferedInputStream(httpsConnection.getInputStream());
     382                                input = httpsConnection.getInputStream();
     383                               
     384                                Log.d("***", "despues de crear BufferedInputStream");
     385                               
     386                                lenghtOfFile = httpsConnection.getContentLength();
     387                                Log.d("***", "se acepto la excepcion");
     388
     389                               
     390                                /*
     391                                // descarga del certificado del servidor
     392                                CertificateFactory cf = CertificateFactory.getInstance("X.509");
    292393                                InputStream caInput = new BufferedInputStream(getResources().openRawResource(R.raw.gestion));
    293394                                Certificate ca;
     
    301402                                }
    302403                                Log.d("***", "despues de Certificate ca");
     404                               
    303405                                // Create a KeyStore containing our trusted CAs
    304406                                String keyStoreType = KeyStore.getDefaultType();
     
    332434                                lenghtOfFile = httpsConnection.getContentLength();
    333435                                Log.d("***", "se acepto la excepcion");
     436                               
     437                                */
    334438                               
    335439                        }else{                 
Note: See TracChangeset for help on using the changeset viewer.