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