luisgoncalves / xades4j

A Java library for XAdES signature services
GNU Lesser General Public License v3.0
111 stars 66 forks source link

I have problems to sign electronic invoice with IRS DIAN COLOMBIA, I got from DIAN "Incorrecta: firma inconsistente o revocada." #191

Closed warami92rez closed 5 years ago

warami92rez commented 5 years ago

I have implemented this process with java JDK 6 and the library xades4j, this is the fragment code that I used to sign the document

` public Document signEpes(String document, Document docFactura) throws Exception { Document docRetorna = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        dbf.setNamespaceAware(true);

        docRetorna = docFactura;

        NodeList tag= docRetorna.getElementsByTagName("ext:ExtensionContent");
        Node elemToSign1= tag.item(1);
        DataObjectDesc dataObjRef = new DataObjectReference("").withTransform(new EnvelopedSignatureTransform());//crea un dataobject del xml para firmar

          KeyStore keyStore = getKeyStore();

          if(keyStore==null){
              System.err.println("No se pudo obtener almacen de firma.");
              throw new Exception("No se pudo obtener almacen de firma");
          }

          String alias=getAlias(keyStore);

          // Obtencion del certificado para firmar. Utilizaremos el primer
          // certificado del almacen.   
          X509Certificate certificate = null;
          try {
              certificate = (X509Certificate)keyStore.getCertificate(alias);
              System.out.println(certificate);
              if (certificate == null) {
                 System.out.println("No existe ningún certificado para firmar.");
                  throw new Exception("No existe ningún certificado para firmar");
              }
          } catch (KeyStoreException e1) {
              e1.printStackTrace();
          }

          // Obtención de la clave privada asociada al certificado
          PrivateKey privateKey = null;
          KeyStore tmpKs = keyStore;
          try {
              privateKey = (PrivateKey) tmpKs.getKey(alias, PASS.toCharArray()); 
          } catch (UnrecoverableKeyException e) {
              System.err.println("No existe clave privada para firmar.");
              e.printStackTrace();
          } catch (KeyStoreException e) {
              System.err.println("No existe clave privada para firmar.");
              e.printStackTrace();
          } catch (NoSuchAlgorithmException e) {
              System.err.println("No existe clave privada para firmar.");
              e.printStackTrace();
          }

        KeyingDataProvider kdp = new DirectKeyingDataProvider(certificate,privateKey);

        // politica
        SignaturePolicyInfoProvider policyInfoProvider = new SignaturePolicyInfoProvider() {
            @Override
            public SignaturePolicyBase getSignaturePolicy() {
                return new SignaturePolicyIdentifierProperty(
                        new ObjectIdentifier("https://facturaelectronica.dian.gov.co/politicadefirma/v2/politicadefirmav2.pdf", 
                                             IdentifierType.URI, "Política de firma para facturas electrónicas de la República de Colombia"),
                        new ByteArrayInputStream("https://facturaelectronica.dian.gov.co/politicadefirma/v2/politicadefirmav2.pdf".getBytes())
                );
            }
        };

        // Role

        SignerEPES signer = (SignerEPES) new XadesEpesSigningProfile(kdp, policyInfoProvider)
        .withSignaturePropertiesProvider(new SignaturePropertiesProvider() {
            @Override
            public void provideProperties(SignaturePropertiesCollector arg0) {
                SigningTimeProperty sigTime = new SigningTimeProperty();
                arg0.setSignerRole(new SignerRoleProperty().withClaimedRole("supplier"));
                arg0.setSigningTime(sigTime );
            }
        })
   .newSigner();

    signer.sign(new SignedDataObjects(new DataObjectDesc[] { dataObjRef }), elemToSign1,SignatureAppendingStrategies.AsFirstChild);
    return docRetorna;

}`
luisgoncalves commented 5 years ago
rolandopalermo commented 5 years ago

I just published a java project for implementing digital signature using xades4j according to DIAN specifications andrea-xades

luisgoncalves commented 5 years ago

Thanks for sharing @rolandopalermo .

@warami92rez I'm closing this as there hasn't been any feedback.

gdalvarezr commented 5 years ago

Hello, As I'm from Colombia and I saw many people ask for same to @luisgoncalves , I've created and api for DIAN signature Colombia that it's working fine with 2.1 "Validación previa". Please don't open more issues, the api it's free as part of my contribution. https://app.swaggerhub.com/apis-docs/tyrlabx/api-test/1.0.0