E-Invoice is an XML document having a special format. The signing of E-Invoices is very similar to enveloped signatures explained in Signature Structures. The signature must be placed in the particular field defined in E-Invoice standard.
// get the element that signature will be added Element exts = (Element)faturaDoc.getDocumentElement().getElementsByTagName("ext:UBLExtensions").item(0); Element ext = (Element)exts.getElementsByTagName("ext:UBLExtension").item(0); Element extContent = (Element)exts.getElementsByTagName("ext:ExtensionContent").item(0); // generate signature Context context = createContext(); context.setDocument(faturaDoc); XMLSignature signature = new XMLSignature(context, false); // attach signature to envelope structure extContent.appendChild(signature.getElement());
Then, the method transform
,in which the whole document is included and only the signature is omitted,
is added.
// use enveloped signature transform Transforms transforms = new Transforms(context); transforms.addTransform(new Transform(context, TransformType.ENVELOPED.getUrl())); // add whole document(="") with envelope transform, with SHA256 // and don't include it into signature(false) signature.addDocument("", "text/xml", transforms, DigestMethod.SHA_256, false);
Finally, the signer role ,verification public key and signing time attributes are added as stated in E-Invoice standard.
// add signer role information SignerRole rol = new SignerRole(context, new ClaimedRole[]{new ClaimedRole(context, "Supplier")}); signature.getQualifyingProperties().getSignedSignatureProperties().setSignerRole(rol); // e-fatura standard wants public key info in the signature PublicKey pk = KeyUtil.decodePublicKey(new ECertificate(cert.getEncoded()).getSubjectPublicKeyInfo()); signature.getKeyInfo().add(new KeyValue(context, pk)); // add signing time signature.getQualifyingProperties().getSignedSignatureProperties().setSigningTime(getTime());
The code is in tr.gov.tubitak.uekae.esya.api.xades.example.efatura.EFatura
.