Exemplo n.º 1
0
  /** @param args */
  @SuppressWarnings("unchecked")
  public static void main(String[] args) {

    try {
      // String BASE = "/opt/projects-eclipse-3.2/ujiCrypto/etc/";
      String BASE = "/tmp/";

      // Leemos el fichero de configuracion
      ConfigManager.init(BASE + "jdigidoc.cfg");

      // TODO Auto-generated method stub
      SignedDoc sdoc;
      DigiDocFactory digFac = ConfigManager.instance().getDigiDocFactory();
      sdoc = digFac.readSignedDoc("/home/paul/x2.ddoc");

      System.out.println(
          "\nContenido firmado: " + new String(Base64.decode(sdoc.getLastDataFile().getBody())));
      Signature sig;

      for (int i = 0; i < sdoc.countSignatures(); i++) {
        System.out.println("Firma " + i);
        sig = sdoc.getSignature(i);

        System.out.println("  Firmado por: " + sig.getKeyInfo().getSubjectDN());
        System.out.println("  Información de timestamp: ");
        System.out.println("  Fecha: " + sig.getTimestampInfo(0).getTime());

        System.out.print("  Resultado de la verificación: ");

        ArrayList errs = sig.verify(sdoc, false, false);
        if (errs.size() == 0) {
          System.out.println("OK!");
        }

        for (int j = 0; j < errs.size(); j++) {
          System.out.println("Errores: " + errs.size());
          System.out.println("Que es: " + errs.get(j));
          System.out.println("ERROR!");
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * End Element handler
  *
  * @param namespaceURI namespace URI
  * @param lName local name
  * @param qName qualified name
  */
 public void endElement(String namespaceURI, String sName, String qName) throws SAXException {
   String tName = qName;
   if (tName.indexOf(":") != -1) tName = qName.substring(tName.indexOf(":") + 1);
   if (m_logger.isDebugEnabled()) m_logger.debug("End Element: " + tName);
   // remove last tag from stack
   String currTag = (String) m_tags.pop();
   //	<KeyName>
   if (tName.equals("KeyName")) {
     checkEncryptedData();
     EncryptedKey ekey = m_doc.getLastEncryptedKey();
     checkEncryptedKey(ekey);
     ekey.setKeyName(m_sbCollectChars.toString());
     m_sbCollectChars = null; // stop collecting
   }
   //	<CarriedKeyName>
   if (tName.equals("CarriedKeyName")) {
     checkEncryptedData();
     EncryptedKey ekey = m_doc.getLastEncryptedKey();
     checkEncryptedKey(ekey);
     ekey.setCarriedKeyName(m_sbCollectChars.toString());
     m_sbCollectChars = null; // stop collecting
   }
   //	<X509Certificate>
   if (tName.equals("X509Certificate")) {
     checkEncryptedData();
     EncryptedKey ekey = m_doc.getLastEncryptedKey();
     checkEncryptedKey(ekey);
     try {
       X509Certificate cert =
           SignedDoc.readCertificate(Base64Util.decode(m_sbCollectChars.toString().getBytes()));
       ekey.setRecipientsCertificate(cert);
     } catch (DigiDocException ex) {
       SAXDigiDocException.handleException(ex);
     }
     m_sbCollectChars = null; // stop collecting
   }
   //	<CipherValue>
   if (tName.equals("CipherValue")) {
     checkEncryptedData();
     if (m_tags.search("EncryptedKey") != -1) { // child of <EncryptedKey>
       EncryptedKey ekey = m_doc.getLastEncryptedKey();
       checkEncryptedKey(ekey);
       ekey.setTransportKeyData(Base64Util.decode(m_sbCollectChars.toString().getBytes()));
     } else { // child of <EncryptedData>
       m_doc.setData(Base64Util.decode(m_sbCollectChars.toString().getBytes()));
       if (m_doc.getMimeType() != null
           && m_doc.getMimeType().equals(EncryptedData.DENC_ENCDATA_MIME_ZLIB))
         m_doc.setDataStatus(EncryptedData.DENC_DATA_STATUS_ENCRYPTED_AND_COMPRESSED);
       else
         m_doc.setDataStatus(EncryptedData.DENC_DATA_STATUS_ENCRYPTED_AND_NOT_COMPRESSED); // ???		
     }
     m_sbCollectChars = null; // stop collecting
   }
   // <EncryptionProperty>
   if (tName.equals("EncryptionProperty")) {
     checkEncryptedData();
     EncryptionProperty eprop = m_doc.getLastProperty();
     try {
       eprop.setContent(m_sbCollectChars.toString());
     } catch (DigiDocException ex) {
       SAXDigiDocException.handleException(ex);
     }
     m_sbCollectChars = null; // stop collecting
   }
 }