1 | /* |
---|
2 | Tibisay Movil |
---|
3 | |
---|
4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
7 | |
---|
8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
11 | GPL versión 2.0 de la Free Software Foundation. |
---|
12 | |
---|
13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
16 | |
---|
17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
18 | llamado "gpl-2.0.txt" en ingles. |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | package ve.gob.cenditel.tibisaymovil; |
---|
23 | |
---|
24 | import java.security.cert.CertificateException; |
---|
25 | import java.security.cert.CertificateExpiredException; |
---|
26 | import java.security.cert.CertificateNotYetValidException; |
---|
27 | import java.security.cert.X509Certificate; |
---|
28 | import java.util.List; |
---|
29 | |
---|
30 | import org.spongycastle.cert.ocsp.OCSPException; |
---|
31 | |
---|
32 | import android.app.Activity; |
---|
33 | import android.content.Context; |
---|
34 | import android.content.Intent; |
---|
35 | import android.content.res.Resources; |
---|
36 | import android.graphics.Point; |
---|
37 | import android.net.ConnectivityManager; |
---|
38 | import android.net.Uri; |
---|
39 | import android.os.AsyncTask; |
---|
40 | import android.os.Bundle; |
---|
41 | import android.text.method.ScrollingMovementMethod; |
---|
42 | import android.view.Display; |
---|
43 | import android.view.LayoutInflater; |
---|
44 | import android.view.View; |
---|
45 | import android.view.Window; |
---|
46 | import android.view.View.OnClickListener; |
---|
47 | import android.view.ViewGroup; |
---|
48 | import android.view.WindowManager; |
---|
49 | import android.widget.Button; |
---|
50 | import android.widget.CheckBox; |
---|
51 | import android.widget.LinearLayout; |
---|
52 | import android.widget.TextView; |
---|
53 | import android.widget.Toast; |
---|
54 | |
---|
55 | public class CertValidatorActivity extends Activity implements OnClickListener { |
---|
56 | |
---|
57 | private final static int PENDING = -1; |
---|
58 | private final static int VALID = 0; |
---|
59 | private final static int INVALID = 1; |
---|
60 | private final static int ERROR = 2; |
---|
61 | private final static int NOT_CONNECTED = 3; |
---|
62 | |
---|
63 | |
---|
64 | //TODO: NEW |
---|
65 | private final static int EXPIRED = 4; |
---|
66 | private final static int NOTYETVALID = 5; |
---|
67 | |
---|
68 | |
---|
69 | private CertValidatorView viewHolder; |
---|
70 | private String alias; |
---|
71 | private List<Uri> files; |
---|
72 | private AsyncTask<String, Void, X509Certificate[]> certInfoTask = null; |
---|
73 | private AsyncTask<X509Certificate, Void, Integer> validateTask = null; |
---|
74 | private int result = PENDING; |
---|
75 | boolean embedSignature; |
---|
76 | |
---|
77 | /** Called when the activity is first created. */ |
---|
78 | @Override |
---|
79 | public void onCreate(Bundle savedInstanceState) { |
---|
80 | //Estilando la barra de titulo |
---|
81 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
82 | |
---|
83 | super.onCreate(savedInstanceState); |
---|
84 | this.viewHolder = new CertValidatorView(); |
---|
85 | |
---|
86 | this.alias = getIntent().getStringExtra(IntentExtraField.ALIAS); |
---|
87 | this.files = getIntent().getParcelableArrayListExtra(IntentExtraField.FILES); |
---|
88 | this.embedSignature = getIntent().getBooleanExtra(IntentExtraField.EMBED_SIGNATURE_IN_PDF, false); |
---|
89 | Toast.makeText(this, "embebed="+embedSignature, Toast.LENGTH_SHORT).show(); |
---|
90 | |
---|
91 | |
---|
92 | if (files.isEmpty()) |
---|
93 | finish(); |
---|
94 | |
---|
95 | if (savedInstanceState == null || (this.result = savedInstanceState.getInt("result")) == PENDING) { |
---|
96 | startValidation(); |
---|
97 | } else { |
---|
98 | if (isConnected() && FsUtils.containsPDF(this.files)) { |
---|
99 | // CertValidatorActivity.this.viewHolder.embedSignature.setEnabled(true); |
---|
100 | } |
---|
101 | setValidationResult(this.result); |
---|
102 | } |
---|
103 | //Estilando Barra de titulo |
---|
104 | if(customTitleSupported) |
---|
105 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
106 | } |
---|
107 | |
---|
108 | @Override |
---|
109 | protected void onDestroy() { |
---|
110 | |
---|
111 | super.onDestroy(); |
---|
112 | if (this.certInfoTask != null) { |
---|
113 | this.certInfoTask.cancel(true); |
---|
114 | } |
---|
115 | if (this.validateTask != null) { |
---|
116 | this.validateTask.cancel(true); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | @Override |
---|
121 | protected void onSaveInstanceState(Bundle outState) { |
---|
122 | |
---|
123 | super.onSaveInstanceState(outState); |
---|
124 | outState.putInt("result", this.result); |
---|
125 | } |
---|
126 | |
---|
127 | @Override |
---|
128 | public void onClick(View view) { |
---|
129 | |
---|
130 | switch (view.getId()) { |
---|
131 | |
---|
132 | case R.id.button_cancel_zone: |
---|
133 | setResult(RESULT_CANCELED); |
---|
134 | finish(); |
---|
135 | break; |
---|
136 | |
---|
137 | case R.id.button_accept_zone: |
---|
138 | |
---|
139 | Intent intent = new Intent(); |
---|
140 | intent.putExtra(IntentExtraField.EMBED_SIGNATURE_IN_PDF, embedSignature); |
---|
141 | setResult(Activity.RESULT_OK, intent); |
---|
142 | finish(); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | private boolean isConnected() { |
---|
147 | |
---|
148 | ConnectivityManager cm = |
---|
149 | (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
---|
150 | |
---|
151 | return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected(); |
---|
152 | } |
---|
153 | |
---|
154 | private void startValidation() { |
---|
155 | |
---|
156 | this.certInfoTask = new AsyncTask<String, Void, X509Certificate[]>() { |
---|
157 | |
---|
158 | @Override |
---|
159 | protected X509Certificate[] doInBackground(String... alias) { |
---|
160 | KeyChainStrategy keyChain = KeyChainStrategy.getInstance(); |
---|
161 | X509Certificate[] chain = null; |
---|
162 | try { |
---|
163 | chain = keyChain.getCertificateChain(alias[0]); |
---|
164 | } catch (Exception e) { |
---|
165 | return null; |
---|
166 | } |
---|
167 | return chain; |
---|
168 | } |
---|
169 | |
---|
170 | @Override |
---|
171 | protected void onPostExecute(X509Certificate[] chain) { |
---|
172 | |
---|
173 | if (chain == null) { |
---|
174 | setValidationResult(CertValidatorActivity.ERROR); |
---|
175 | } else { |
---|
176 | CertValidatorActivity.this.viewHolder.setData( |
---|
177 | CertificateUtils.getSubject(chain[0]), |
---|
178 | CertificateUtils.getSerial(chain[0]), |
---|
179 | CertificateUtils.getIssuer(chain[0]), |
---|
180 | CertValidatorActivity.this.files); |
---|
181 | |
---|
182 | if (isConnected()) { |
---|
183 | if (FsUtils.containsPDF(CertValidatorActivity.this.files)) { |
---|
184 | // CertValidatorActivity.this.viewHolder.embedSignature.setEnabled(true); |
---|
185 | } |
---|
186 | // validate(chain); |
---|
187 | } else { |
---|
188 | setValidationResult(NOT_CONNECTED); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | }.execute(alias); |
---|
194 | } |
---|
195 | |
---|
196 | private void validate(X509Certificate[] chain) { |
---|
197 | |
---|
198 | this.validateTask = new AsyncTask<X509Certificate, Void, Integer>() { |
---|
199 | |
---|
200 | @Override |
---|
201 | protected Integer doInBackground(X509Certificate... params) { |
---|
202 | |
---|
203 | if (params.length < 2) { |
---|
204 | return ValidationResult.ERROR_CHAIN_RESULT; |
---|
205 | } |
---|
206 | try { |
---|
207 | params[0].checkValidity(); |
---|
208 | String DEFAULT_OCSP_RESPONDER = "http://ocsp.suscerte.gob.ve/"; |
---|
209 | |
---|
210 | //Antiguo |
---|
211 | /*if (CertificateUtils.checkWithOCSP(params[0], params[2],DEFAULT_OCSP_RESPONDER)) { |
---|
212 | return VALID; |
---|
213 | } else { |
---|
214 | return INVALID; |
---|
215 | }*/ |
---|
216 | |
---|
217 | ValidationResult res; |
---|
218 | |
---|
219 | res= CertificateUtils.checkWithOCSP(params[0], params[params.length-1],DEFAULT_OCSP_RESPONDER); |
---|
220 | |
---|
221 | int result = res.getResultValue(); |
---|
222 | |
---|
223 | |
---|
224 | if (result==ValidationResult.GOOD_RESULT) |
---|
225 | return VALID; |
---|
226 | else if (result==ValidationResult.ERROR_CONNECTION_RESULT) |
---|
227 | return NOT_CONNECTED; |
---|
228 | else |
---|
229 | return result; |
---|
230 | |
---|
231 | |
---|
232 | } catch (CertificateExpiredException e) { |
---|
233 | return EXPIRED; |
---|
234 | } catch (CertificateNotYetValidException e) { |
---|
235 | return NOTYETVALID; |
---|
236 | } catch (CertificateException e) { |
---|
237 | return ValidationResult.UNKNOWN_RESULT; |
---|
238 | } catch (OCSPException e) { |
---|
239 | return ValidationResult.UNKNOWN_RESULT; |
---|
240 | } catch (Exception e) { |
---|
241 | return ERROR; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | @Override |
---|
246 | protected void onPostExecute(Integer result) { |
---|
247 | |
---|
248 | CertValidatorActivity.this.setValidationResult(result); |
---|
249 | } |
---|
250 | }.execute(chain); |
---|
251 | } |
---|
252 | |
---|
253 | private void setValidationResult(int result) { |
---|
254 | |
---|
255 | this.result = result; |
---|
256 | this.viewHolder.accept.setEnabled(true); |
---|
257 | |
---|
258 | // this.viewHolder.validationStatus.removeAllViews(); |
---|
259 | |
---|
260 | LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
---|
261 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams ( |
---|
262 | LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5F); |
---|
263 | |
---|
264 | View v = null; |
---|
265 | |
---|
266 | TextView text; |
---|
267 | // switch (result) { |
---|
268 | // |
---|
269 | // |
---|
270 | // |
---|
271 | // case VALID: |
---|
272 | // v = inflater.inflate(R.layout.certificate_valid, null); |
---|
273 | // this.viewHolder.validationStatus.addView(v, params); |
---|
274 | // break; |
---|
275 | // case INVALID: |
---|
276 | // v = inflater.inflate(R.layout.certificate_not_valid, null); |
---|
277 | // this.viewHolder.validationStatus.addView(v, params); |
---|
278 | // break; |
---|
279 | // |
---|
280 | // case ERROR: |
---|
281 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
282 | // this.viewHolder.validationStatus.addView(v, params); |
---|
283 | // break; |
---|
284 | // |
---|
285 | // |
---|
286 | // //TODO: NEW MESSAGES |
---|
287 | // case ValidationResult.ERROR_CHAIN_RESULT: |
---|
288 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
289 | // text = (TextView) v.findViewById(R.id.fail_text); |
---|
290 | // text.setText(R.string.ERROR_CHAIN_RESULT); |
---|
291 | // this.viewHolder.validationStatus.addView(v, params); |
---|
292 | // break; |
---|
293 | // case ValidationResult.GOOD_RESULT: |
---|
294 | // v = inflater.inflate(R.layout.certificate_valid, null); |
---|
295 | // this.viewHolder.validationStatus.addView(v, params); |
---|
296 | // break; |
---|
297 | // |
---|
298 | // case ValidationResult.ERROR_CONNECTION_RESULT: |
---|
299 | // v = inflater.inflate(R.layout.certificate_not_connected, null); |
---|
300 | // this.viewHolder.validationStatus.addView(v, params); |
---|
301 | // break; |
---|
302 | // case ValidationResult.GOOD_UNTRUSTED_RESULT: |
---|
303 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
304 | // text = (TextView) v.findViewById(R.id.fail_text); |
---|
305 | // text.setText(R.string.GOOD_UNTRUSTED_RESULT); |
---|
306 | // this.viewHolder.validationStatus.addView(v, params); |
---|
307 | // break; |
---|
308 | // |
---|
309 | // case ValidationResult.REVOKED_RESULT: |
---|
310 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
311 | // text = (TextView) v.findViewById(R.id.fail_text); |
---|
312 | // text.setText(R.string.REVOKED_RESULT); |
---|
313 | // this.viewHolder.validationStatus.addView(v, params); |
---|
314 | // break; |
---|
315 | // |
---|
316 | // case ValidationResult.UNKNOWN_RESULT: |
---|
317 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
318 | // text = (TextView) v.findViewById(R.id.fail_text); |
---|
319 | // text.setText(R.string.UNKNOWN_RESULT); |
---|
320 | // this.viewHolder.validationStatus.addView(v, params); |
---|
321 | // break; |
---|
322 | // |
---|
323 | // case EXPIRED: |
---|
324 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
325 | // text = (TextView) v.findViewById(R.id.fail_text); |
---|
326 | // text.setText(R.string.EXPIRED); |
---|
327 | // this.viewHolder.validationStatus.addView(v, params); |
---|
328 | // break; |
---|
329 | // |
---|
330 | // case NOTYETVALID: |
---|
331 | // v=inflater.inflate(R.layout.certificate_validation_failed, null); |
---|
332 | // text = (TextView) v.findViewById(R.id.fail_text); |
---|
333 | // text.setText(R.string.NOTYETVALID); |
---|
334 | // this.viewHolder.validationStatus.addView(v, params); |
---|
335 | // break; |
---|
336 | // |
---|
337 | // default: // NOT_CONNECTED |
---|
338 | // v = inflater.inflate(R.layout.certificate_not_connected, null); |
---|
339 | // this.viewHolder.validationStatus.addView(v, params); |
---|
340 | // } |
---|
341 | |
---|
342 | |
---|
343 | WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); |
---|
344 | Display display = wm.getDefaultDisplay(); |
---|
345 | // this.viewHolder.validationStatus.setMinimumWidth(display.getWidth()/2); |
---|
346 | // this.viewHolder.embedSignature.setMinimumWidth(display.getWidth()/2); |
---|
347 | this.viewHolder.fixing.getParent().childDrawableStateChanged(this.viewHolder.fixing); |
---|
348 | } |
---|
349 | |
---|
350 | private class CertValidatorView { |
---|
351 | |
---|
352 | // public CheckBox embedSignature; |
---|
353 | public TextView certificateData; |
---|
354 | public TextView documentsData; |
---|
355 | // public ViewGroup validationStatus; |
---|
356 | public LinearLayout accept; |
---|
357 | public LinearLayout cancel; |
---|
358 | public LinearLayout fixing; |
---|
359 | |
---|
360 | public CertValidatorView() { |
---|
361 | |
---|
362 | setContentView(R.layout.cert_validation); |
---|
363 | |
---|
364 | // this.embedSignature = (CheckBox) findViewById(R.id.embed_signature); |
---|
365 | this.certificateData = (TextView) findViewById(R.id.cert_data); |
---|
366 | this.documentsData = (TextView) findViewById(R.id.docs_data); |
---|
367 | this.certificateData.setMovementMethod(new ScrollingMovementMethod()); |
---|
368 | this.documentsData.setMovementMethod(new ScrollingMovementMethod()); |
---|
369 | // this.validationStatus = (ViewGroup) findViewById(R.id.validation_status); |
---|
370 | this.accept = (LinearLayout) findViewById(R.id.button_accept_zone); |
---|
371 | this.accept.setOnClickListener(CertValidatorActivity.this); |
---|
372 | // this.accept.setEnabled(false); |
---|
373 | this.cancel = (LinearLayout) findViewById(R.id.button_cancel_zone); |
---|
374 | this.cancel.setOnClickListener(CertValidatorActivity.this); |
---|
375 | |
---|
376 | this.fixing = (LinearLayout) findViewById(R.id.fixing); |
---|
377 | |
---|
378 | |
---|
379 | } |
---|
380 | |
---|
381 | public void setData(String subject, String serial, String issuer, List<Uri> files) { |
---|
382 | |
---|
383 | StringBuilder builder = new StringBuilder(); |
---|
384 | StringBuilder builderDocs = new StringBuilder(); |
---|
385 | Resources res = CertValidatorActivity.this.getResources(); |
---|
386 | |
---|
387 | builder.append(res.getString(R.string.subject)); |
---|
388 | builder.append(":\n"); |
---|
389 | builder.append(subject); |
---|
390 | builder.append("\n\n"); |
---|
391 | |
---|
392 | builder.append(res.getString(R.string.serial_number)); |
---|
393 | builder.append(":\n"); |
---|
394 | builder.append(serial); |
---|
395 | builder.append("\n\n"); |
---|
396 | |
---|
397 | builder.append(res.getString(R.string.issuer)); |
---|
398 | builder.append(":\n"); |
---|
399 | builder.append(issuer); |
---|
400 | builder.append("\n\n"); |
---|
401 | |
---|
402 | for (Uri file : files) { |
---|
403 | builderDocs.append("\n - "); |
---|
404 | builderDocs.append(file.getLastPathSegment()); |
---|
405 | } |
---|
406 | |
---|
407 | this.certificateData.setText(builder.toString()); |
---|
408 | this.documentsData.setText(builderDocs.toString()); |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|