[e68708b] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
| 2 | |
---|
| 3 | import java.io.BufferedInputStream; |
---|
| 4 | import java.io.File; |
---|
| 5 | import java.io.InputStream; |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | import android.net.Uri; |
---|
| 9 | import android.os.Bundle; |
---|
| 10 | import android.app.Activity; |
---|
| 11 | import android.content.Context; |
---|
| 12 | import android.content.Intent; |
---|
| 13 | import android.util.Log; |
---|
| 14 | import android.view.LayoutInflater; |
---|
| 15 | import android.view.View; |
---|
| 16 | import android.view.ViewGroup; |
---|
| 17 | import android.view.Window; |
---|
| 18 | import android.view.View.OnClickListener; |
---|
| 19 | import android.widget.AdapterView; |
---|
| 20 | import android.widget.BaseAdapter; |
---|
| 21 | import android.widget.ImageView; |
---|
| 22 | import android.widget.LinearLayout; |
---|
| 23 | import android.widget.ListView; |
---|
| 24 | import android.widget.TextView; |
---|
| 25 | import android.widget.Toast; |
---|
| 26 | |
---|
| 27 | import java.security.*; |
---|
| 28 | import java.security.cert.Certificate; |
---|
| 29 | import java.security.cert.CertificateFactory; |
---|
| 30 | import java.io.*; |
---|
| 31 | import java.lang.reflect.Field; |
---|
| 32 | |
---|
| 33 | import com.lowagie.text.pdf.*; |
---|
| 34 | |
---|
| 35 | import ee.sk.digidoc.SignedDoc; |
---|
| 36 | import ee.sk.utils.ConvertUtils; |
---|
| 37 | |
---|
| 38 | import java.util.Calendar; |
---|
| 39 | import java.util.ArrayList; |
---|
| 40 | import java.util.Random; |
---|
| 41 | |
---|
| 42 | import java.security.cert.X509Certificate; |
---|
| 43 | import java.text.SimpleDateFormat; |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | public class PDFVerifyResultActivity extends Activity { |
---|
| 53 | |
---|
| 54 | private LinearLayout button_share; |
---|
| 55 | private LinearLayout button_finish; |
---|
| 56 | |
---|
[c14b8d2] | 57 | // ruta absoluta al archivo a verificar |
---|
[e68708b] | 58 | private String pdfToVerify; |
---|
| 59 | |
---|
| 60 | @Override |
---|
| 61 | protected void onCreate(Bundle savedInstanceState) { |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | //Estilando la barra de titulo |
---|
| 66 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
| 67 | |
---|
| 68 | super.onCreate(savedInstanceState); |
---|
| 69 | |
---|
| 70 | setContentView(R.layout.activity_verify_result_pdf); |
---|
| 71 | |
---|
| 72 | //Estilando Barra de titulo |
---|
| 73 | if(customTitleSupported) |
---|
| 74 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
| 75 | |
---|
| 76 | // obtener el archivo pdf a verificar |
---|
| 77 | Bundle bundle = getIntent().getExtras(); |
---|
| 78 | pdfToVerify = bundle.getString("pdfToVerify"); |
---|
| 79 | TextView pdfName = (TextView) this.findViewById(R.id.filename_text); |
---|
| 80 | pdfName.setText(pdfToVerify); |
---|
| 81 | |
---|
| 82 | // ejecutar el proceso de verificacion del pdf |
---|
| 83 | doVerification(pdfToVerify); |
---|
| 84 | |
---|
| 85 | // habilitar opciones de botones inferiores |
---|
| 86 | button_share = (LinearLayout) this.findViewById(R.id.button_remove_certificate_zone); |
---|
| 87 | button_finish = (LinearLayout) this.findViewById(R.id.button_add_certificate_zone); |
---|
| 88 | |
---|
| 89 | // habilita el click sobre la acción de finalizar la verificacion de firma y regresar |
---|
| 90 | button_finish.setOnClickListener(new OnClickListener() |
---|
| 91 | { |
---|
| 92 | public void onClick(View v) |
---|
| 93 | { |
---|
| 94 | |
---|
| 95 | Intent oIntent = new Intent(PDFVerifyResultActivity.this, TibisayMovilActivity.class); |
---|
| 96 | oIntent.setAction(Intent.ACTION_VIEW); |
---|
| 97 | oIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
---|
| 98 | startActivity(oIntent); |
---|
| 99 | |
---|
| 100 | finish(); |
---|
| 101 | } |
---|
| 102 | }); |
---|
| 103 | |
---|
| 104 | // habilita el click sobre la acción de compartir el archivo |
---|
| 105 | button_share.setOnClickListener(new OnClickListener() |
---|
| 106 | { |
---|
| 107 | public void onClick(View v) |
---|
| 108 | { |
---|
| 109 | shareIt(); |
---|
| 110 | } |
---|
| 111 | }); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | } // fin de onCreate |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Ejecuta la verificacion de la firma del pdf utilizando la biblioteca droidtext |
---|
| 120 | * |
---|
| 121 | * */ |
---|
| 122 | private void doVerification(String pdfToVerify) { |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | try { |
---|
| 126 | |
---|
| 127 | Random rnd = new Random(); |
---|
| 128 | |
---|
| 129 | PdfReader reader = new PdfReader(pdfToVerify); |
---|
| 130 | Log.d("VerificadorPDF", "new PdfReader"); |
---|
| 131 | |
---|
| 132 | AcroFields af = reader.getAcroFields(); |
---|
| 133 | Log.d("VerificadorPDF", "reader.getAcroFields()"); |
---|
| 134 | |
---|
| 135 | ArrayList names = af.getSignatureNames(); |
---|
| 136 | Log.d("VerificadorPDF", "af.getSignatureNames()"); |
---|
| 137 | |
---|
| 138 | Toast.makeText(getApplicationContext(), "names.size: "+ Integer.toString(names.size()), Toast.LENGTH_SHORT).show(); |
---|
| 139 | Log.d("VerificadorPDF", Integer.toString(names.size())); |
---|
| 140 | |
---|
| 141 | if (names.size() == 0) { |
---|
| 142 | // el archivo no posee firmas |
---|
| 143 | TextView signatureMessage = (TextView) this.findViewById(R.id.archivo_original_a_descifrar); |
---|
| 144 | signatureMessage.setText("El documento PDF no está firmado electrónicamente."); |
---|
| 145 | |
---|
| 146 | TextView labelSignature = (TextView) this.findViewById(R.id.label_archivo_original); |
---|
| 147 | labelSignature.setText("Documento:"); |
---|
| 148 | |
---|
| 149 | TextView labelSigners = (TextView) this.findViewById(R.id.label_destinatario_cifrado); |
---|
| 150 | labelSigners.setVisibility(View.GONE); |
---|
| 151 | |
---|
| 152 | ListView listViewSigners = (ListView) this.findViewById(R.id.signatures_list); |
---|
| 153 | listViewSigners.setVisibility(View.GONE); |
---|
| 154 | return; |
---|
| 155 | }else{ |
---|
| 156 | // el documento si posee firmas |
---|
| 157 | //KeyStore kall = PdfPKCS7.loadCacertsKeyStore(); |
---|
| 158 | |
---|
| 159 | // crear certificate factory |
---|
| 160 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
---|
| 161 | |
---|
| 162 | // crear un KeyStore |
---|
| 163 | String keyStoreType = KeyStore.getDefaultType(); |
---|
| 164 | KeyStore kall = KeyStore.getInstance(keyStoreType); |
---|
| 165 | kall.load(null, null); |
---|
| 166 | |
---|
| 167 | // obtener vector de campos de /res/raw |
---|
| 168 | Field[] fields = R.raw.class.getFields(); |
---|
| 169 | |
---|
| 170 | InputStream caInput = null; |
---|
| 171 | |
---|
| 172 | // iterar sobre todos los certificados incluidos en /res/raw y cargarlos |
---|
| 173 | // en el KeyStore |
---|
| 174 | for (int l=0; l < fields.length; l++) { |
---|
| 175 | try { |
---|
| 176 | Toast.makeText(getApplicationContext(), "PDFVerifyResultActivity - reading certificate: "+Integer.toString(l)+ " "+ |
---|
| 177 | Integer.toString(fields[l].getInt(null)), Toast.LENGTH_SHORT).show(); |
---|
| 178 | |
---|
| 179 | caInput = new BufferedInputStream(getResources().openRawResource(fields[l].getInt(null))); |
---|
| 180 | |
---|
| 181 | } catch (IllegalArgumentException e) { |
---|
| 182 | |
---|
| 183 | Toast.makeText(getApplicationContext(), "PDFVerifyResultActivity: IllegalArgumentException "+e.getMessage(), |
---|
| 184 | Toast.LENGTH_SHORT).show(); |
---|
| 185 | break; |
---|
| 186 | } catch (IllegalAccessException e) { |
---|
| 187 | |
---|
| 188 | Toast.makeText(getApplicationContext(), "PDFVerifyResultActivity: IllegalAccessException "+e.getMessage(), |
---|
| 189 | Toast.LENGTH_SHORT).show(); |
---|
| 190 | break; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | Certificate ca; |
---|
| 194 | try { |
---|
| 195 | ca = cf.generateCertificate(caInput); |
---|
| 196 | //Log.d("**ca:", ((X509Certificate) ca).getSubjectDN()); |
---|
| 197 | |
---|
| 198 | //Log.d("*** try: ca.toString()", ca.toString()); |
---|
| 199 | } finally { |
---|
| 200 | caInput.close(); |
---|
| 201 | } |
---|
| 202 | kall.setCertificateEntry("ca"+Integer.toString(l), ca); |
---|
| 203 | }// fin de la iteracion sobre /res/raw |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | // se crea el adaptador para mostrar los firmantes del contenedor |
---|
| 207 | ListView signerListView = (ListView) findViewById(R.id.signatures_list); |
---|
| 208 | final ArrayList<SignerFromPDF> signers = new ArrayList<SignerFromPDF>(); |
---|
| 209 | |
---|
| 210 | boolean invalidSignature = false; |
---|
| 211 | |
---|
| 212 | for (int i = 0; i < names.size(); i++){ |
---|
| 213 | |
---|
| 214 | String name = (String)names.get(i); |
---|
| 215 | PdfPKCS7 pk = af.verifySignature(name); |
---|
| 216 | Calendar cal = pk.getSignDate(); |
---|
| 217 | Certificate pkc[] = pk.getCertificates(); |
---|
| 218 | |
---|
| 219 | X509Certificate cert = (X509Certificate) pk.getSigningCertificate(); |
---|
| 220 | |
---|
| 221 | String cn = cert.getSubjectDN().toString(); |
---|
| 222 | String signerName = ConvertUtils.getCommonName(cn); |
---|
| 223 | |
---|
| 224 | SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS"); |
---|
| 225 | String date = date_format.format(pk.getSignDate().getTime()); |
---|
| 226 | |
---|
| 227 | String certificateSerial = cert.getSerialNumber().toString(); |
---|
| 228 | String validFrom = cert.getNotBefore().toString(); |
---|
| 229 | String validUntil = cert.getNotAfter().toString(); |
---|
| 230 | String certificateIssuer = cert.getIssuerDN().toString(); |
---|
| 231 | |
---|
| 232 | Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal); |
---|
| 233 | if (fails == null) { |
---|
| 234 | System.out.print(pk.getSignName()); |
---|
| 235 | Toast.makeText(getApplicationContext(), "Firma Válida", Toast.LENGTH_SHORT).show(); |
---|
| 236 | } |
---|
| 237 | else { |
---|
| 238 | System.out.print("Firma no válida"); |
---|
| 239 | Toast.makeText(getApplicationContext(), "Firma NO Válida", Toast.LENGTH_SHORT).show(); |
---|
| 240 | |
---|
| 241 | invalidSignature = true; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | // agregar el nombre comun del firmante y la fecha y la hora |
---|
| 245 | signers.add(new SignerFromPDF(signerName, cn, date, certificateSerial, validFrom, validUntil, certificateIssuer)); |
---|
| 246 | |
---|
| 247 | TextView signatureValidation = (TextView) this.findViewById(R.id.archivo_original_a_descifrar); |
---|
| 248 | |
---|
| 249 | if (invalidSignature){ |
---|
| 250 | signatureValidation.setText("Firma Inválida"); |
---|
| 251 | }else{ |
---|
| 252 | signatureValidation.setText("Firma Válida"); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | SignerFromPDFAdapter signerAdapter = new SignerFromPDFAdapter(PDFVerifyResultActivity.this, signers); |
---|
| 259 | signerListView.setAdapter(signerAdapter); |
---|
| 260 | signerListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | @Override |
---|
| 264 | public void onItemClick(AdapterView<?> parent, View view, |
---|
| 265 | int position, long id) { |
---|
| 266 | |
---|
| 267 | Toast.makeText(getApplicationContext(), "SE DEBE MOSTRAR INFORMACIÓN DEL FIRMANTE", Toast.LENGTH_SHORT).show(); |
---|
| 268 | showSignatureDetails(position, signers); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | }); |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | } // fin de else: el documento posee firmas |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | }catch(Exception e){ |
---|
| 279 | Toast.makeText(getApplicationContext(), "Error al verificar pdf:"+e.getMessage(), Toast.LENGTH_LONG).show(); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | // funcion para compartir el documento |
---|
| 286 | private void shareIt() { |
---|
| 287 | |
---|
| 288 | Intent shareIntent = new Intent(); |
---|
| 289 | shareIntent.setAction(Intent.ACTION_SEND); |
---|
| 290 | File file = new File(pdfToVerify); |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | Uri uri = Uri.fromFile(file); |
---|
| 294 | Log.i("DEBUG", file.getPath()); |
---|
| 295 | //Log.d("******", getMimeType(file.getPath())); |
---|
| 296 | //shareIntent.setDataAndType(uri, getMimeType(file.getPath())); |
---|
| 297 | shareIntent.putExtra(Intent.EXTRA_STREAM, uri); |
---|
| 298 | shareIntent.setType("application/*"); |
---|
| 299 | startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_it_using))); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | // class que abstrae un firmante incluido en un contenedor BDOC |
---|
| 304 | private class SignerFromPDF { |
---|
| 305 | |
---|
| 306 | // nombre del firmante |
---|
| 307 | private String mName; |
---|
| 308 | |
---|
| 309 | // nombre comun del firmante |
---|
| 310 | private String mCN; |
---|
| 311 | |
---|
| 312 | // fecha y hora de la firma |
---|
| 313 | private String mDate; |
---|
| 314 | |
---|
| 315 | // serial del certificado |
---|
| 316 | private String mCertificateSerial; |
---|
| 317 | |
---|
| 318 | // fecha de validez inicial |
---|
| 319 | private String mValidFrom; |
---|
| 320 | |
---|
| 321 | // fecha de validez final |
---|
| 322 | private String mValidUntil; |
---|
| 323 | |
---|
| 324 | // emisor del certificado |
---|
| 325 | private String mCertificateIssuer; |
---|
| 326 | |
---|
| 327 | public SignerFromPDF(String name, String cn, String date, String serial, String validFrom, String validUntil, String issuer){ |
---|
| 328 | mName = name; |
---|
| 329 | mCN = cn; |
---|
| 330 | mDate = date; |
---|
| 331 | mCertificateSerial = serial; |
---|
| 332 | mValidFrom = validFrom; |
---|
| 333 | mValidUntil = validUntil; |
---|
| 334 | mCertificateIssuer = issuer; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | public void setName(String name) { |
---|
| 338 | this.mName = name; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | public String getName() { |
---|
| 342 | return mName; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | public void setCN(String cn) { |
---|
| 346 | this.mCN = cn; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | public String getCN() { |
---|
| 350 | return mCN; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | public void setDate(String date) { |
---|
| 354 | this.mDate = date; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | public String getDate() { |
---|
| 358 | return mDate; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | public void setCertificateSerial(String certSerial) { |
---|
| 362 | this.mCertificateSerial = certSerial; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | public String getCertificateSerial() { |
---|
| 366 | return mCertificateSerial; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | public void setValidFrom(String validFrom) { |
---|
| 370 | this.mValidFrom = validFrom; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | public String getValidFrom() { |
---|
| 374 | return mValidFrom; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | public void setValidUntil(String validUntil) { |
---|
| 378 | this.mValidUntil = validUntil; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | public String getValidUntil() { |
---|
| 382 | return mValidUntil; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | public void setCertificateIssuer(String certIssuer) { |
---|
| 386 | this.mCertificateIssuer = certIssuer; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | public String getCertificateIssuer() { |
---|
| 390 | return mCertificateIssuer; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | } // fin de clase SignerFromPDF |
---|
| 394 | |
---|
| 395 | // clase que abstrae el adaptador para llenar el ListView de documentos que |
---|
| 396 | // se encuentran dentro del contenedor BDOC |
---|
| 397 | private class SignerFromPDFAdapter extends BaseAdapter { |
---|
| 398 | |
---|
| 399 | private ArrayList<SignerFromPDF> mSignerFromPDFList; |
---|
| 400 | private LayoutInflater lInflater; |
---|
| 401 | |
---|
| 402 | private SignerFromPDFAdapter(Context context, ArrayList<SignerFromPDF> signers) { |
---|
| 403 | this.lInflater = LayoutInflater.from(context); |
---|
| 404 | this.mSignerFromPDFList = signers; |
---|
| 405 | |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | |
---|
| 409 | @Override |
---|
| 410 | public int getCount() { |
---|
| 411 | return mSignerFromPDFList.size(); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | @Override |
---|
| 415 | public Object getItem(int position) { |
---|
| 416 | return mSignerFromPDFList.get(position); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | @Override |
---|
| 420 | public long getItemId(int position) { |
---|
| 421 | return position; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | @Override |
---|
| 425 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 426 | ViewHolder container = null; |
---|
| 427 | |
---|
| 428 | if (convertView == null){ |
---|
| 429 | container = new ViewHolder(); |
---|
| 430 | convertView = lInflater.inflate(R.layout.signer_inside_bdoc_item, null); |
---|
| 431 | |
---|
| 432 | container.signer_text = (TextView) convertView.findViewById(R.id.signer_info); |
---|
| 433 | container.signer_modified = (TextView) convertView.findViewById(R.id.signature_errors); |
---|
| 434 | container.type_image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 435 | convertView.setTag(container); |
---|
| 436 | }else{ |
---|
| 437 | container = (ViewHolder) convertView.getTag(); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | SignerFromPDF doc = (SignerFromPDF) getItem(position); |
---|
| 441 | container.signer_text.setText(doc.getName()); |
---|
| 442 | container.signer_modified.setText(doc.getName()); |
---|
| 443 | container.type_image.setImageResource(R.drawable.ic_pluma); |
---|
| 444 | |
---|
| 445 | return convertView; |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | class ViewHolder{ |
---|
| 450 | TextView signer_text; |
---|
| 451 | TextView signer_modified; |
---|
| 452 | ImageView type_image; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | /** |
---|
| 458 | * Lanza la actividad para mostrar detalles de un firmante |
---|
| 459 | * |
---|
| 460 | * */ |
---|
| 461 | private void showSignatureDetails(int position, ArrayList<SignerFromPDF> signers) { |
---|
| 462 | |
---|
| 463 | Log.d("**showSignatureDetails", Integer.toString(position)); |
---|
| 464 | Log.d("** nombre", signers.get(position).getName()); |
---|
| 465 | Intent intent = new Intent(this, ActivitySignerInfo.class); |
---|
| 466 | |
---|
| 467 | intent.putExtra("name", signers.get(position).getName()); |
---|
| 468 | intent.putExtra("cn", signers.get(position).getCN()); |
---|
| 469 | intent.putExtra("date", signers.get(position).getDate()); |
---|
| 470 | intent.putExtra("certificateSerial", signers.get(position).getCertificateSerial()); |
---|
| 471 | intent.putExtra("validFrom", signers.get(position).getValidFrom()); |
---|
| 472 | intent.putExtra("validUntil", signers.get(position).getValidUntil()); |
---|
| 473 | intent.putExtra("certificateIssuer", signers.get(position).getCertificateIssuer()); |
---|
| 474 | startActivity(intent); |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | |
---|
| 478 | } |
---|