PAdES signatures in itext and the road ahead Paulo Soares
About the speaker Paulo Soares M.Sc. Electronics and Telecomunications Hardware background in military comunication systems Works for www.glintt.com Healthcare software (pharmacies, hospitals, clinics, laboratories) Industrial control, automation Software architect itext co-developer since 2000 2012-03-29 itext Summit 2
Some acronyms OSI - Open Systems Interconnection ASN.1 - Abstract Syntax Notation One BER - Basic Encoding Rules DER - Distinguished Encoding Rules IETF - Internet Engineering Task Force RFC Request For Comments CMS Cryptographic Message Syntax CAdES - CMS Advanced Electronic Signatures PAdES - PDF Advanced Electronic Signature Profiles 2012-03-29 itext Summit 3
What is PAdES In July 2009, ETSI (European Telecommunications Standard Institute) has published a new standard that will facilitate secure paperless transactions throughout Europe, in conformance with European legislation. The standard defines a series of profiles for PAdES Advanced Electronic Signatures for PDF documents that meet the requirements of the European Directive on a Community framework for electronic signatures (Directive 1999/93/EC). The new standard was developed by ETSI's Electronic Signatures and Infrastructure (ESI) Technical Committee in collaboration with PDF experts. PDF is defined in a standard ISO 32000-1, so the ETSI activity included reviewing and documenting how ISO 32000-1 can satisfy the European Directive. The resulting PAdES standard, ETSI Technical Specification (TS) 102 778, also introduces a number of adaptations and extensions to PDF to satisfy the Directive's requirements. 2012-03-29 itext Summit 4
PAdES summary Part 1: "PAdES Overview - a framework document for PAdES"; Part 2: "PAdES Basic - Profile based on ISO 32000-1"; Part 3: "PAdES Enhanced - PAdES-BES and PAdES-EPES Profiles"; Part 4: "PAdES Long Term - PAdES-LTV Profile"; Part 5: "PAdES for XML Content - Profiles for XAdES signatures". 2012-03-29 itext Summit 5
PAdES Part 1 Provides a general description of support for signatures in PDF documents including use of XML signatures to protect XML data in PDF documents; Lists the features of the PDF profiles specified in other parts of the document; Describes how the profiles may be used in combination. 2012-03-29 itext Summit 6
PAdES Part 2 Profiles the use of PDF signatures, as described in ISO 32000-1 and based on CMS, for its use in any application areas where PDF is the appropriate technology for exchange of digital documents including interactive forms. 2012-03-29 itext Summit 7
PAdES Part 2 SubFilter value adbe.pkcs7.detached adbe.pkcs7.sha1 adbe.x509.rsa.sha1a Message Digest SHA1 (PDF 1.3) SHA256 (PDF 1.6) SHA384 (PDF 1.7) SHA512 (PDF 1.7) RIPEMD160 (PDF 1.7) SHA1 (PDF 1.3)b SHA1 (PDF 1.3) SHA256 (PDF 1.6) SHA384 (PDF 1.7) SHA512 (PDF 1.7) RIPEMD160 (PDF 1.7) RSA Algorithm Support Up to 1024-bit (PDF 1.3) Up to 2048-bit (PDF 1.5) Up to 4096-bit (PDF 1.5) See adbe.pkcs7.detached See adbe.pkcs7.detached DSA Algorithm Support Up to 4096-bits (PDF 1.6) See adbe.pkcs7.detached No a Despite the appearance of sha1 in the name of this SubFilter value, supported encodings shall not be limited to the SHA1 algorithm. The PKCS#1 object contains an identifier that indicates which algorithm shall be used. b Other digest algorithms may be used to digest the signed-data field; however, SHA1 shall be used to digest the data that is being signed. 2012-03-29 itext Summit 8
PAdES Part 3 Profiles the use of PDF Signatures specified in ISO 32000-1 with an alternative signature encoding to support signature formats equivalent to the signature forms CAdES-BES, CAdES-EPES and CAdES-T as Specified in TS 101 733. The PAdES-BES profile supports basic CMS (RFC 3852) signature features as specified TS 102 778-2 with the additional protection against signing certificate substitution. The PAdES-EPES profile extends the PAdES-BES profile to include signature policies. 2012-03-29 itext Summit 9
PAdES Part 4 Profiles the electronic signature formats found in ISO 32000-1 [1] to support Long Term Validation (LTV) of PDF Signatures Specifies how to include validation information in a PDF Document and to further protect the document using time-stamps so that it is possible to subsequently verify a PDF Signature long after it was signed. This profile may be used to support long term validation of: PDF Signatures to profiles specified in TS 102 778-2 PDF Signatures to profiles specified in TS 102 778-3 PDF Signatures to profiles specified in TS 102 778-5 2012-03-29 itext Summit 10
PAdES Part 4 2012-03-29 itext Summit 11
PAdES Part 5 Defines four profiles that together profile the usage of XAdES signatures, as defined in TS 101 903, for signing XML content within the PDF containers. 2012-03-29 itext Summit 12
PAdES support in itext Full support of PAdES Part 2 Support of PAdES Part 3 with an external signature Full support of PAdES Part 4 No support of PAdES Part 5 2012-03-29 itext Summit 13
PAdES Part 2 in itext Direct support of adbe.x509.rsa.sha1 Direct support of adbe.pkcs7.sha1 Support of adbe.pkcs7.detached as an external signature but itext does the signing 2012-03-29 itext Summit 14
adbe.x509.rsa.sha1 signing KeyStore ks = KeyStore.getInstance("pkcs12"); ks.load(new FileInputStream("my_private_key.pfx"), "my_password".tochararray()); String alias = (String)ks.aliases().nextElement(); PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".tochararray()); Certificate[] chain = ks.getcertificatechain(alias); PdfReader reader = new PdfReader("original.pdf"); FileOutputStream fout = new FileOutputStream("signed.pdf"); PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0'); PdfSignatureAppearance sap = stp.getsignatureappearance(); sap.setcrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED); sap.setreason("i'm the author"); sap.setlocation("lisbon"); sap.setvisiblesignature(new Rectangle(100, 100, 200, 200), 1, null); stp.close(); 2012-03-29 itext Summit 15
adbe.pkcs7.sha1 signing KeyStore ks = KeyStore.getInstance("pkcs12"); ks.load(new FileInputStream("my_private_key.pfx"), "my_password".tochararray()); String alias = (String)ks.aliases().nextElement(); PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".tochararray()); Certificate[] chain = ks.getcertificatechain(alias); PdfReader reader = new PdfReader("original.pdf"); FileOutputStream fout = new FileOutputStream("signed.pdf"); PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0'); PdfSignatureAppearance sap = stp.getsignatureappearance(); sap.setcrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED); sap.setreason("i'm the author"); sap.setlocation("lisbon"); sap.setvisiblesignature(new Rectangle(100, 100, 200, 200), 1, null); stp.close(); 2012-03-29 itext Summit 16
adbe.pkcs7.detached signing PdfSignatureAppearance sap = stp.getsignatureappearance(); sap.setcrypto(null, chain, null, PdfSignatureAppearance.SELF_SIGNED); PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached")); dic.setdate(new PdfDate(sap.getSignDate())); sap.setcryptodictionary(dic); HashMap<PdfName,Integer> exc = new HashMap<PdfName,Integer>(); exc.put(pdfname.contents, new Integer(contentEstimated * 2 + 2)); sap.preclose(exc); PdfPKCS7 sgn = new PdfPKCS7(pk, chain, null, "SHA1", null, false); InputStream data = sap.getrangestream(); MessageDigest messagedigest = MessageDigest.getInstance("SHA1"); byte buf[] = new byte[8192]; int n; while ((n = data.read(buf)) > 0) messagedigest.update(buf, 0, n); byte hash[] = messagedigest.digest(); Calendar cal = Calendar.getInstance(); TSAClient tsc = new TSAClientBouncyCastle(TSA_URL, TSA_ACCNT, TSA_PASSW); byte[] ocsp = new OcspClientBouncyCastle((X509Certificate)chain[0], (X509Certificate)chain[1], url).getencoded(); byte sh[] = sgn.getauthenticatedattributebytes(hash, cal, ocsp); sgn.update(sh, 0, sh.length); byte[] encodedsig = sgn.getencodedpkcs7(hash, cal, tsc, ocsp); byte[] paddedsig = new byte[contentestimated]; System.arraycopy(encodedSig, 0, paddedsig, 0, encodedsig.length); PdfDictionary dic2 = new PdfDictionary(); dic2.put(pdfname.contents, new PdfString(paddedSig).setHexWriting(true)); sap.close(dic2); 2012-03-29 itext Summit 17
PAdES Part 3 in itext No direct support but possible to use an external signature itext currently has no capabilities to assist in the signing 2012-03-29 itext Summit 18
PAdES Part 4 in itext Full LTV support both in Document security Store and Timestamp 2012-03-29 itext Summit 19
PAdES Part 4 in itext PdfReader r = new PdfReader(IN_FILE); FileOutputStream fout = new FileOutputStream(OUT_FILE); PdfStamper stp = PdfStamper.createSignature(r, fout, '\0', null, true); LtvVerification v = stp.getltvverification(); AcroFields af = stp.getacrofields(); for (String signame : af.getsignaturenames()) { v.addverification(signame, new OcspClientBouncyCastle(), new CrlClientImp(), LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO); } PdfSignatureAppearance sap = stp.getsignatureappearance(); TSAClientBouncyCastle tsa = new TSAClientBouncyCastle(TSA_URL, TSA_ACCNT, TSA_PASSW, 6500, "sha256"); LtvTimestamp.timestamp(sap, tsa, null); 2012-03-29 itext Summit 20
itext sign architecture Certificate encryption Validation AcroFields.verifySignature() Signing PdfSignatureAppearance, PdfPKCS7 Open Source Modify at will 2012-03-29 itext Summit 21
Shortcomings of itext signing architecture Monolithic Impossible to open a certificate encrypted PDF with a smartcard with C# Impossible to plug-in another validation engine/logic Arbitrary signing is possible with an external signature but itext won t assist much in the process Open Source no urgency in providing a decent interface 2012-03-29 itext Summit 22
New itext signing architecture Based on the factory pattern Plug-in for certificate encryption, validation and signing Standard plug-in for detached signing and CAdES signing 2012-03-29 itext Summit 23