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