Access Preview: Digital Signatures For PDF Documents
Access Preview: Digital Signatures For PDF Documents
Introduction
The main rationale for PDF used to be viewing and printing documents in a reliable way. The technology was conceived with the goal to provide a collection of utilities, applications, and system software so that a corporation can effectively capture documents from any application, send electronic versions of these documents anywhere, and view and print these documents on any machines. (Warnock, 1991)
In the years that followed, an abundance of new tools from Adobe as well as from third party software vendors emerged, and the PDF specification was and still is very much alive. Plenty of functionality has been added to the PDF format over the years. Because of this, PDF has become the preferred document format of choice in many professional sectors and industries. In this paper well focus on one specific aspect of PDF files that makes the choice for PDF over any other document format a no-brainer: digital signatures.
Imagine a document that has legal value. Such a document may contain important information about rights and obligations, in which case you need to ensure its authenticity. You dont want people to deny the commitments theyve written down. Furthermore, this document probably has to be mailed to, viewed and stored by different parties. On different places in the workflow, at different moments in time, the document can be altered, be it involuntary, for instance due to a transmission error, or deliberately, if somebody wants to create a forgery from the original document. For centuries, weve tried to solve this problem by putting a so -called wet ink signature on paper. Nowadays, we can use digital signatures to ensure: The integrity of the document we want assurance that the document hasnt been changed somewhere in the workflow. The authenticity of the document we want assurance that the author of the document is who we think it is (and not somebody else) Non-repudiation we want assurance that the author cant deny his or her authorship.
In this paper, well focus on documents in the portable document format (PDF).
This mission was set forth in the Camelot paper, and it was accomplished with the first publication of the Portable Document Format Reference (Adobe, 1993) and the availability of the first PDF software products created by Adobe. PDF became renowned as the format that could be trusted to ensure a consistent output, be it on screen or in print.
1.2.1 How to check a password .................................................................................................... 10 1.2.2 What is a digest algorithm? ................................................................................................. 11 1.2.3 Java SDKs default MessageDigest implementation ............................................................ 13 1.2.4 The BouncyCastle library ..................................................................................................... 13 1.3 Encrypting a message using a public-key encryption ................................................................. 14 1.3.1 Creating a key store ............................................................................................................. 15 1.3.2 Encrypting and decrypting messages .................................................................................. 15 1.3.3 Inspecting our self-signed certificate ................................................................................... 17 1.3.4 Using a public-key algorithm for authentication and non-repudiation................................ 18 1.4 Overview of cryptography Acronyms and Standards ................................................................. 19 1.4.1 Acronyms ............................................................................................................................. 19 1.4.2 Public-Key Cryptography Standards (PKCS) ......................................................................... 20 1.4.3 The PDF ISO Standard .......................................................................................................... 21 1.4.4 CAdES, XAdES and PAdES..................................................................................................... 21 1.5 Summary .................................................................................................................................... 22 2. PDF and digital signatures ................................................................................................................ 23 2.1 Digital signatures in PDF ............................................................................................................. 23 2.1.1 The signature handler and the sub filters ............................................................................ 23 2.1.2 The byte range covered by the digital signature ................................................................. 24 2.1.3 How to compose a signature ............................................................................................... 25 2.1.4 Algorithms supported in PDF ............................................................................................... 26 2.2 The Hello World of digital signing using iText ......................................................................... 27
2.4.4 Ordinary and Certifying signatures ...................................................................................... 46 2.4.5 Adding content after a document was signed ..................................................................... 48 2.5 Signatures in PDF and workflow ................................................................................................. 51 2.5.1 Sequential signatures in PDF ............................................................................................... 51 2.5.2 Creating a form with placeholders for multiple signatures ................................................. 52 2.5.3 Signing a document multiple times ..................................................................................... 53 2.5.4 Signing and filling out fields multiple times ......................................................................... 56 2.5.5 Locking fields and documents after signing ......................................................................... 60 2.6 Summary .................................................................................................................................... 63 3 Certificate Authorities, certificate revocation and time stamping..................................................... 64 3.1 Certificate authorities ................................................................................................................. 64 3.1.1 Signing a document with a p12 file from a Certificate Authority ......................................... 65 3.1.2 Trusting the root certificate of the Certificate Authority ..................................................... 66 3.1.3 Best practices in signing ...................................................................................................... 68 3.2 Adding Certificate Revocation information ................................................................................ 68 3.2.1 Finding the URL of a Certificate Revocation List .................................................................. 68 3.2.2 Getting the CRL online ......................................................................................................... 69 3.2.3 Creating a CrlClient using an offline copy of the CRL ........................................................... 74 3.2.4 Using the Online Certificate Status Protocol (OCSP) ............................................................ 75 3.2.5 Which is better: embedding CRLs or an OCSP response? .................................................... 77 3.3 Adding a timestamp ................................................................................................................... 78 3.3.1 Dealing with expiration and revocation dates ..................................................................... 78
4.3 Client/server architectures for signing ....................................................................................... 94 5. Validation of signed documents ....................................................................................................... 94 5.1 Validating signed PDFs................................................................................................................ 94 5.2 PAdES 4: Long-Term Validation .................................................................................................. 94 Appendix .............................................................................................................................................. 94 A. How to protect the integrity of a paper document? .................................................................... 94 B. Electronic signatures and digital signatures: are they the same thing?........................................ 94 C. Digital signatures on Android example ......................................................................................... 94
If we look inside the PDF file shown in figure 1, we see the following PDF syntax: Code sample 1: A PDF file inside-out
%PDF-1.4 % 2 0 obj <</Length 73 >>stream BT 36 806 Td 0 -18 Td /F1 12 Tf (Hello World)Tj 0 0 Td ET Q endstream endobj 4 0 obj <</Parent 3 0 R/Contents 2 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 1 0 R>>>>/MediaBox[0 0 595 842]>> endobj 1 0 obj <</BaseFont/Helvetica/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>> endobj 3 0 obj <</ITXT(5.3.0)/Type/Pages/Count 1/Kids[4 0 R]>> endobj 5 0 obj <</Type/Catalog/Pages 3 0 R>> endobj 6 0 obj <</Producer(iText 5.3.0 2000-2012 1T3XT BVBA)/ModDate(D:20120613102725+02'00')/CreationDate(D:20120613102725+02'00')>> endobj
1.1.1 How to forge the content of a PDF document Suppose we know how to apply some small changes to the file. For instance: lets change the content, the dimensions, and the metadata of the document. See the parts marked in red in Code sample 2. Code sample 2: A manually altered PDF file
%PDF-1.4 % 2 0 obj <</Length 73 >>stream BT 36 806 Td 0 -18 Td /F1 12 Tf (Hello Bruno)Tj 0 0 Td ET Q endstream endobj 4 0 obj <</Parent 3 0 R/Contents 2 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 1 0 R>>>>/MediaBox[0 0 120 806]>> endobj 1 0 obj <</BaseFont/Helvetica/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>> endobj 3 0 obj <</ITXT(5.3.0)/Type/Pages/Count 1/Kids[4 0 R]>> endobj 5 0 obj <</Type/Catalog/Pages 3 0 R>> endobj 6 0 obj
Every PDF file starts with %PDF- followed by a version number, and it ends with %%EOF. Inbetween, you can find different PDF objects that somehow define the document. Explaining the meaning of these objects is outside the scope of this paper.
I manually replaced the word World with Bruno, I changed the dimensions of the page from 595 x 842 to 120 x 806, and I changed the version number of iText in the producer line. Figure 2 shows the result.
Figure 2: an altered Hello World file Dont ever do this yourself! Changing a PDF manually will corrupt your file in 99.9% of the cases. I m only doing this to prove that, although PDF isnt a word processing format, although PDF isnt meant for editing a document, and although its not recommended to do so, you can change the content of a document. This is exactly what we try to avoid by introducing a digital signature. 1.1.2 A digitally signed PDF document Figure 3 shows a Hello World document that has been digitally signed. The blue banner tells us the document is Signed and all signatures are valid. The Signature panel informs us that the file was Signed by Bruno Specimen, and it gives you more signature details.
The green check mark means that the Document hasnt been modified since the signature was applied and that the Signers identity is valid. 1.1.3 Inspecting the syntax of the digital signature Now lets take a look inside this PDF file. Code sample 3: snippets of a signed PDF file
%PDF-1.4 % 3 0 obj <</F 132/Type/Annot/Subtype/Widget/Rect[0 0 0 0]/FT/Sig /DR<<>>/T(signature)/V 1 0 R/P 4 0 R/AP<</N 2 0 R>>>> endobj 1 0 obj <</Contents <0481801e6d931d561563fb254e27c846e08325570847ed63d6f9e35 ... b2c8788a5> /Type/Sig/SubFilter/adbe.x509.rsa_sha1/Location(Ghent) /Name(Bruno Specimen)/M(D:20120613133437+02'00')/ByteRange [0 159 423 2408 ] /Cert(0...)/Filter/Adobe.PPKLite/Reason(Demo purposes.)/R 65541>> endobj ... 9 0 obj <</Length 73>>stream BT 36 806 Td 0 -18 Td /F1 12 Tf (Hello World)Tj 0 0 Td ET Q endstream endobj ... 11 0 obj <</Type/Catalog/AcroForm<</Fields[3 0 R]/DR<</Font<</Helv 5 0 R /ZaDb 6 0 R>>>>/DA(/Helv 0 Tf 0 g )/SigFlags 3>>/Pages 10 0 R>>
Note that Ive slightly altered the file, removing bytes that arent relevant when explai ning the concept of a digital signature. First lets inspect the root object of the PDF (aka the Catalog object). Its marked in green in code sample 3 (the object with number 11). The Catalog is always represented as a PDF dictionary. Dictionaries can easily be recognized in a PDF file. They start with << and end with >>. In-between youll find a series of key-value pairs. The key is always a name object. Note that names always start with a /. For example: if the PDF contains a form, youll find an /AcroForm key in the catalog dictionary. Its value will be (a reference to) a dictionary. In turn, this dictionary will contain a /SigFlags value if the form contains a digital signature. Theres one field in the form. Its referred to from the /Fields array: see object 3 (marked in red). The field named signature (/T(signature)) is a field of type signature (/FT/Sig). We didnt see any visual representation of the signature in figure 3. Thats because Bruno Specimen decided to use an invisible signature. The rectangle (/Rect) defining the widget annotation (/Type/Annot /SubType/Widget) has a zero width and zero height ([0 0 0 0]). The actual signature can be found in the signature dictionary (marked in blue). This dictionary is referred to from the value (/V) of the signature field. The signature is the value of the /Contents entry. This signature covers all the bytes of the PDF file, except for the signature bytes itself. See the /ByteRange entry: the signature covers bytes 0 to 159 and bytes 423 to 2408. The signature itself takes bytes 160 to 422. 1.1.4 Making the signature invalid Now when I change one of the bytes inside the byte range covered by the signature, Adobe Reader will show a red cross instead of a green check mark. Figure 4 shows what happens if I manually replace World by Bruno.
In this case, the Signers identity is valid, but the Document has been altered or corrupted since it was signed. How does Adobe Reader know that the document has been altered? To understand this, we need to understand the concept of hashing and we need to know how encryption works.
I have a confession to make: I cant remember all the passwords Im using to log in into different sites such as Twitter, Facebook, LinkedIn, and so on. I am one of the frequent users of the Lost your password? functionality. Usually, I get a link that allows me to reset my password, but once in a while I get a mail containing my original password in clear text. You cant image how much this worries me! The fact that a service can provide me with my password, means that my actual password can be found somewhere in a database, on a server. Thats a dangerous situation: it means that whoever hacks the system can obtain the passwo rds of all the users. 1.2.1 How to check a password A simple way to check a password is to store a digest of the password instead of the actual password. Lets create a simple class that demonstrates how its done: Code sample 4: an example showing how to use the MessageDigest class
public class DigestDefault { protected byte[] digest; protected MessageDigest md; protected DigestDefault(String password, String algorithm, String provider) throws GeneralSecurityException {
DigestDefault app = getInstance("password", algorithm); System.out.println("Digest using " + algorithm + ": " + app.getDigestSize());
Take a look at the showTest() method in code sample 4. We create a DigestDefault object named app, passing a password password and an algorithm as parameters. An instance of the java.security.MessageDigest object is created and stored as a member variable. The password isnt stored in the DigestDefault class. Instead we store the result of the digest() method. Out of curiosity, we send some info about this result to the System.out: its length and its value as a hexadecimal String. When we test the password with the checkPassword() method, we use the same digest() method, and we compare the result with the digest that was stored. If the result is identical, the password was correct; otherwise it was wrong. How does this work? What is a digest algorithm? 1.2.2 What is a digest algorithm? When we create a digest, were using a cryptographic hash function to turn an arbitrary block of data into a fixed-size bit string. The block of data is often called the message, and the hash value is referred to as the message digest. In the previous example, the message was a password. In the context of PDF documents, the block of data could be the byte range of a PDF file.
MD5, SHA, and RIPEMD are implementations of different cryptographic hash algorithms. MD5 one in a series of message digest algorithms designed by Professor Ron Rivest of MIT. It was designed in 1991. Its still widely used, but its no longer considered secure, because it allows different messages resulting in the same 128-bit (16-byte) digest. This is known as a hash collision. SHA stands for Secure Hash Algorithm. o SHA-1 was designed by the US National Security Agency (NSA). The 160-bit (20 bytes) hash was considered safer than MD5, but in 2005 a number of security flaws were identified. o SHA-2 fixes these flaws. SHA-2 is a set of hash functions: SHA-224, SHA-256, SHA384, and SHA-512. The length of the message digest in SHA-2 can be 224 bits (28 bytes), 256 bits (32 bytes), 384 bits (48 bytes), or 512 bits (64 bytes). RIPEMD stands for RACE Integrity Primitives Evaluation Message Digest. It was developed at the Katholieke Universiteit Leuven1. In our example, we use a 128, 160 and 256 bit version (16, 20 and 32 bytes).
In section 2.1.1, well see that not all of these digest algorithms can be used in the context of PDF, but lets not get ahead of ourselves.
1 2
public static void testAll() { showTest("MD5"); showTest("SHA-1"); showTest("SHA-224"); showTest("SHA-256"); showTest("SHA-384"); showTest("SHA-512"); showTest("RIPEMD128"); showTest("RIPEMD160"); showTest("RIPEMD256"); }
But the output for SHA-224 and the RIPEMD algorithms looks like this:
1.2.4 The BouncyCastle library Bouncy Castle is a collection of APIs used in cryptography2. Its available in both Java and C#. We could extend the class in code sample 4 by defining a BouncyCastleProvider instance (named BC) as security provider. This is done in code sample 6. Code sample 6: using a different crypto provider
public class DigestBC extends DigestDefault { public static final BouncyCastleProvider PROVIDER = new BouncyCastleProvider(); static { Security.addProvider(PROVIDER); } protected DigestBC(String password, String algorithm) throws GeneralSecurityException { super(password, algorithm, PROVIDER.getName()); }
public static DigestBC getInstance( String password, String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
Note that we create a BouncyCastleProvider instance, and we add it to the Security class (in the java.security package). This class centralizes all security properties. One of its primary uses is to manage security providers. Now the output for SHA-224 and RIPEMD128 looks like this:
http://www.bouncycastle.org/
Weve already mentioned that the message could be any block of bytes, including the bytes of a specific byte range of a PDF file. To detect whether or not a PDF file has been altered, we could create a message digest of those bytes and store it inside the PDF. Then when somebody changes the PDF, the message digest taken from the changed bytes in the byte range will no longer correspond with the message digest in the PDF. Does this solve our problems of data integrity, authenticity and non-repudiation? Not yet. One could easily change the bytes of the PDF file, guess the digest algorithm that was used, and store a new message digest inside the PDF. To avoid this, we need to introduce the concept of asymmetric key algorithms.
http://en.wikipedia.org/wiki/Bob_and_Alice
A key store can contain more than one private key, and we could define a password that is different from the key store password for every key, but for the sake of simplicity, we used only one password password. Now we can use this file from our Java program to encrypt and decrypt messages. 1.3.2 Encrypting and decrypting messages Code sample 8 shows a simple class containing an encrypt() and a decrypt() method: Code sample 8: A simple class to encrypt and decrypt messages
public class EncryptDecrypt { protected KeyStore ks;
initKeyStore(keystore, ks_pass); }
public void initKeyStore(String keystore, String ks_pass) throws GeneralSecurityException, IOException { ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream(keystore), ks_pass.toCharArray()); } public X509Certificate getCertificate(String alias) throws KeyStoreException { return (X509Certificate) ks.getCertificate(alias); } public Key getPublicKey(String alias) throws GeneralSecurityException, IOException {
public static void main(String[] args) throws GeneralSecurityException, IOException { EncryptDecrypt app = new EncryptDecrypt("src/main/resources/ks", "password"); Key publicKey = app.getPublicKey("demo"); Key privateKey = app.getPrivateKey("demo", "password"); System.out.println("Let's encrypt 'secret message' with a public key"); byte[] encrypted = app.encrypt(publicKey, "secret message"); System.out.println("Encrypted message: " + new BigInteger(1, encrypted).toString(16)); System.out.println("Let's decrypt it with the corresponding private key"); String decrypted = app.decrypt(privateKey, encrypted); System.out.println(decrypted); System.out.println("You can also encrypt the message with a private key"); encrypted = app.encrypt(privateKey, "secret message"); System.out.println("Encrypted message: " + new BigInteger(1, encrypted).toString(16)); System.out.println("Now you need the public key to decrypt it"); decrypted = app.decrypt(publicKey, encrypted); System.out.println(decrypted); }
Feel free to experiment with this example, but dont panic if you get an InvalidKeyException saying that the key size is invalid. Due to import control restrictions by the governments of a few countries, the encryption libraries shipped by default with the Java SDK restrict the length, and as a result the strength, of encryption keys.
Version: V3 Subject: CN=Bruno Specimen, OU=IT, O=iText Software, L=Ghent, ST=OVL, C=BE Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5 Key: Sun RSA public key, 2048 bits modulus: 27706646249437583578501322921252037659324960984454438650274096621513733947318221232 90092536075175589409888251417041849614639606544370595805501222639942552792696182924 19557917502293557528812483868420880765808333319067679184013346901221838396913865166 99015383461952441725262486245434952426855074038516834028858534816117097190264270919 71970499616689684012198665415564791592761123642686002605100319784405598279465396131 52730660815729426764990600604032553721917074418187300648866487699179740248069790221 86704383972995455717886346330217224211116969013795163606127880980836981725138593346 185822803712134120722258642329810193 public exponent: 65537 Validity: [From: Sat Aug 04 15:40:30 CEST 2012, To: Tue Nov 17 14:40:30 CET 2015] Issuer: CN=Bruno Specimen, OU=IT, O=iText Software, L=Ghent, ST=OVL, C=BE SerialNumber: [ 501d264e] ] Algorithm: [SHA1withRSA] Signature: 0000: 12 ED EA 66 FE 6C 2C 0010: CF 9E 66 D3 DC 62 85 0020: E8 F6 61 42 02 F9 36 0030: 78 2F CA F0 53 76 41 0040: 8E 44 55 31 14 D3 AB 0050: DE 2C 1E F5 F9 E3 19 0060: EC AF CB 86 22 B8 F0 0070: 98 8E 37 34 7E AD 76 0080: 5D 21 0A A6 CD 57 70 0090: A1 BF 61 02 12 D1 3D 00A0: E1 0B 3C 1F 22 CF 11 00B0: 40 C2 59 C1 19 8B 86 00C0: D3 CF FD 8C 3B F4 6D
17
w
...f.l,...Y.D@.. ..f..b...bv...g. ..aB..6...o.K... x/..SvA.G.Z+...s .DU1...?YlS....6 .,........gfVs". ....".....7..#.C ..74..vx.c!....G ]!...Wp..#^..... ..a...=_...\..m. ..<."....-.u.... @.Y....ay.O.>.a. ....;.m..%..i...
In code sample 8, we obtain the public key from the Certificate object and we get the private key from the KeyStore object. The public and the private key objects can be used as parameters for the encrypt() and decrypt() method. These methods are almost identical. The only difference is the Cipher mode: were using either ENCRYPT_MODE or DECRYPT_MODE. 1.3.4 Using a public-key algorithm for authentication and non-repudiation When we run this class, we get the following output:
Let's encrypt 'secret message' with a public key Encrypted message: 66e7a06a40e5092aa0e70b4d57f2dd139e1902ddc012aa6d75deb6ecc9727fb9219d9aae98c054ebd53 8bfb002d31314c9fa6990d25cb528cf7516bbe3c8923c6670ba1b3673eb40a908a12146369f98fba36c 6beee04479411af7a6226e122a04d119dd9648a5d1be5cb3c52584f42fdffc6719f4d4b1e593a85c377 97d025b60f88d1ad19ad985911768bbb511c732761adace642dc8634925a8e197265ccadf07dd3a6185 50ffaf8dff13b44c91a3f9063d51f33ebd6d896321a795c34c6905f0e227253f3f86da4ca1d5f0da818 010621e1a88d4dc6cc4567d0fbc5461dbd696799f89da26b8eebcb4b4ac72115f021c2519e7036a5902 74f52ddde8d57d Let's decrypt it with the corresponding private key secret message You can also encrypt the message with a private key Encrypted message: 14d5a453baeb584a5c979ebcc142535684ce9503d1db01aa0bed0a3991dd5c6818ddf6f89bca94e24d8 dd02038564c42168c764d4d1f18fcf5e98f8da92b721add388e1971b63db841c8ddec1ae27b2f100f43 6e1f0a5294a9d2fb641b03a9aed412a4257c46f8b71700255b98a8d406a6daeb65bf64b28f85c786a67 2c56c95fa7dd539c8bfffa960a4d75ec16166088362dc1dbf0cb11ca6e7eb9d2730d885cc28abcd7d9b 56b179350d1975de16ec6c2c9ac2978cf2baa5da4b7d5b650ad5195a7fc5437072c05af55ab106f1231 c75c1a498ed83c113e4d008ee5710f32120641ea9ce7895e9f8c304ef6e8d169a9b0e0b155c91ae5f90 7978cd88e6e0f8 Now you need the public key to decrypt it secret message
Do you see what happens here? First you take a public key and use it to encrypt a message so that only somebody with the corresponding key can decrypt it. Thats what encryption is about. But in the next few lines, you do the inverse: you encrypted a message using your private key. Now when you share your public key which is usually the intention the whole world can decrypt your message. In this case, youre not using the encryption mechanism to make sure a third party cant read your message, but to make it absolutely clear that youre the author. If I can decrypt a message using your public key, Im 100% sure that it was encrypted using your private key. As only you have access to your private key, Im 100% sure that youre the author. Thats why the concept of encryption using an asymmetric key algorithm can be used for digital signing. Figure 5 shows the two different concepts. For encryption the public key (the green key) is used to encrypt, the private key (the red key) to decrypt. For digital signatures, its the other way round.
Figure 5: asymmetric key algorithms Does this solve our problems of data integrity, authenticity and non-repudiation? Yes, it does, because as soon as anybody tampers an encrypted message sent from me to you, you wont be able to decrypt it using my public key. On the other hand: if you succeed in decrypting it, youre certain that I was the author. I cant claim that I didnt sign the message while also claiming that my private key isnt compromised. However, thats not how its done in practice: encrypting and decrypting large messages requires plenty resources. If my goal is not to protect the message from being read by anybody but you, we both could save valuable time and CPU on our machines, if I encrypt only a digest of the message, and attach this signed digest to the actual message. When you receive my message, you can decrypt the digest and hash the message for comparison. If theres a match, the message is genuine. If there isnt, the message was tampered with. We havent solved all possible problems, though. Many questions remain. For instance: how can you be sure that the public key youre using is mine, and not a public key from somebody pretending hes me? What happens if my private key is compromised? Well answer these questions in the next chapters. First, we need to get acquainted with some acronyms and standards.
4 5
http://www.ietf.org/ http://www.itl.nist.gov/fipspubs/
6 7
Well discuss PAdES in more detail in the following chapters of this white paper. Note that the code used in this paper will only work with iText version 5.3.2 and later.
1.5 Summary
In this chapter, weve explored the different aspects of digital signing. First we looked at a PDF file and discovered how we could forge an existing document. Weve learned about hashing algorithms and encryption, and we found out how to combine these concepts to protect a PDF file. We ended with an overview of terms and standards. Now were ready to take a closer look at digital signatures inside a PDF file. In the next chapter, well put the theory into practice with a first set of examples.
The sub filter refers to an encoding or a format that was used to create the signature. For instance: does it use PKCS#1, PKCS#7, or CAdES? Is part of the information (such as the public certificate) stored outside the signature, or is it embedded in the signature? NOTE: In PDF, we sometimes refer to a detached signature. According to Wikipedia, a detached signature is a type of digital signature that is kept separate from its signed data, as opposed to bundled together into a single file. This definition isnt entirely correct in the context of PDF: the signature is enclosed in the PDF file, but the attributes of the signature are part of the signature, as opposed to stored in the signature dictionary. In versions predating iText 5.3.0, youd sign a PDF document choosing one of the following parameters for the setCrypto() method: PdfSignatureAppearance.WINCER_SIGNED: this created a signature with the sub filter /adbe.pkcs7.sha1. PdfSignatureAppearance.SELF_SIGNED: this created a signature with the sub filter /adbe.x509.rsa_sha1.
These options have been removed in 5.3.0 for very specific reasons. The /adbe.pkcs7.sha1 sub filter will be deprecated in PDF 2.0. ISO-32000-2 recommends: To support backward compatibility, PDF readers should process this value for the /SubFilter key but PDF writers shall not use this value for that key. iText is a PDF writer, and since iText 5.3.0, we no longer allow the creation of this type of signatures. Please dont sign any documents using this sub filter anymore.
2.1.1 The signature handler and the sub filters When creating a digital signature for a PDF, you need to define a preferred signature handler (a /Filter entry). In iText, well always use the Adobe.PPKLite filter. Its possible to adapt iText to use another filter, but theres very little need to do so: an interactive PDF processor can use any handler it prefers as long as the handler supports the specified /SubFilter format.
11
To the left, we have the elements of the digital identity of a person: his private key and a certificate containing his public key and identity info. Note that most of the times, there will be a chain of certificates. This will be explained in more detail in chapter 3. For now, were working with a single self-signed certificate. NOTE: if you look closely at figures 6 and 7, you see that the complete document is covered by the digital signature. Its not possible to sign specific pages. Some cou ntries demand that every page is initialized for the signature to be valid so that theres proof that every page has been seen, but the concept to initial the pages of a document doesnt exist in PDF. In code sample 3 (section 1.1.3), we see a PKCS#1 signature (which is no longer supported), and the certificate was an entry in the signature dictionary. It wasnt part of the actual signature. F or CMS and CAdES based signatures, the certificate (or certificate chain) is embedded in the digital signature. The signature also contains a digest of the original document that was signed using the private key. Additionally, the signature can contain a timestamp.
12
See the article about Collisions in PDF Signatures by Florian Zumbiehl http://pdfsig-collision.florz.de/
Lets take a look at an overview of the digest and encryption algorithms that are supported based on the sub filter:
Note that the use of this sub filter for signature creation will be deprecated in PDF 2.0 (ISO-32000-2). Its no longer supported in iText since version 5.3.0. adbe.x509.rsa_sha1 Supported message digests: SHA1 (since PDF 1.3), SHA256 (since PDF 1.6), and SHA384, SHA512, RIPEMD160 (since 1.7). RSA Algorithm: up to 1024 bits (since PDF 1.3), 2048 bits (since PDF 1.5), 4096 bits (since PDF 1.7). DSA Algorithm: not supported. Note that despite the reference to SHA1 in the name, other digest algorithms are supported. As pure PKCS#1 is forbidden in the PAdES standard, we no longer support this sub filter in iText (since 5.3.0). adbe.pkcs7.detached, ETSI.CAdES.detached and ETSI.RFC3161 Supported message digests: SHA1 (since PDF 1.3), SHA256 (since PDF 1.6), and SHA384, SHA512, RIPEMD160 (since 1.7). RSA Algorithm: up to 1024 bits (since PDF 1.3), 2048 bits (since PDF 1.5), 4096 bits (since PDF 1.7). DSA Algorithm: up to 4096 bits (since PDF 1.6). ECDSA: the Elliptic Curve Digital Signature Algorithm will be supported in PDF 2.0. Detached signatures are supported by default in iText since version 5.3.0. All message digests are supported. As for encryption algorithms: theres support for RSA and DSA, but not yet for ECDSA. WARNING: The use of SHA-1 is being phased out in some countries, hence the use of other hashing algorithms is recommended. NIST (the US National Institute of Standards and Technology) advises that 1024-bit RSA keys are no longer viable (since 2010) and advises moving to 2048-bit RSA keys. So do other agencies in other countries, for instance in Germany.
adbe.pkcs7.sha1 Supported message digests: SHA1 (other digests may be used to digest the signed-data field, but SHA1 is required to digest the PDF document data that is being signed). RSA Algorithm: up to 1024 bits (since PDF 1.3), 2048 bits (since PDF 1.5), 4096 bits (since PDF 1.7). DSA Algorithm: up to 4096 bits (since PDF 1.6).
With code sample 10, we can create a very simple digital signature using iText. Code sample 10: the Hello World of signing with iText
public void sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location) throws GeneralSecurityException, IOException, DocumentException { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); // Creating the signature ExternalDigest digest = new BouncyCastleDigest(); ExternalSignature signature = new PrivateKeySignature(pk, digestAlgorithm, provider); MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter); }
Please be aware that this is far from the definite code sample, but its a start. Lets examine the example step by step.
Figure 9 shows the different icons that indicate whether or not a signature is valid. As you can see, these icons have changed over the years depending on the version of Adobe Acrobat or Reader youre using.
A red cross always means that your signature is broken: the content has been altered or corrupted, or one of the certificates isnt valid, and so on. In any case, you shouldnt trust the signature. WARNING: support for CAdES is very new. Dont expect versions older than Acrobat/Reader X to be able to validate CAdES signatures! Acrobat 9 only supports signatures as described in the specification for PDF 1.7, and CAdES is new in PDF 2.0. In figure 8, we get a yellow triangle with a message At least one signature has problems. A yellow triangle (or a question mark in older versions of Adobe Reader) means that the signature cant be validated because some information is missing. In our case, Adobe Reader says: The signature validity is unknown. Theres no problem with the integrity because we see that the Document has not been modified since this signature was applied. So whats missing? In section 1.3.1, we created our own key store for Bruno Specimen. Anyone can create such a key store and pretend that he or she is Bruno Specimen. Adobe Reader is aware of this problem and tells us: Signers identity is unknown because it has not been included in your list of trusted identities and none of its parent certificates are trusted identities. Lets ignore the second part about the parent certificates for now, and focus on the list of trusted identities first.
Theres a tab named Trust saying This is a self-signed certificate. The selected certificate path is valid. However: The certificate is not trusted. In the trust settings, theres a button with caption Add to trusted identities. If you click this button, you get another dialog, as shown in figure 11.
You could decide to use this certificate as a trusted root. Lets try this and see what happens. Go to Edit > Protection > Manage Trusted Identities as shown in figure 12:
From now on, youll see a green check mark when opening the signed document. See figure 14.
Figure 15: Import a Certificate into the Trusted Identities list But wait! What if you dont really dont know Bruno Specimen? What if somebody else sends you a public key pretending hes Bruno Specimen? How can you know that the person who is sending you his public key is the person he or she says he or she is?
The result is a file name that can be imported into Adobe Reader by clicking Add Contacts. Just browse for the file bruno.crt and click Import (see figure 15). The file will show up in the list of Trusted Identities, but dont forget to edit the trust settings! You need to explicitly trust the certificate (as is shown in figure 11) before youll be able to validate Bruno Specimens signature.
Theres only one difference with code sample 10, we added a File object as an extra parameter to the createSignature() method. The tmp variable in this code sample can be a path to a specific file or to a directory. In case a directory is chosen, iText will create a file with a unique name in that directory. NOTE: if you use the createSignature() method with a temporary file, you can use an OutputStream that is null, in that case, the temporary file will serve as the actual destination file. This is good practice if your goal is to store a signed file on your file system. If the OutputStream is not null, iText will always try to delete the temporary file after the signing is done. Make sure you use a path with sufficient writing permissions, and make sure you dont try to overwrite existing files if youre working in a multithreaded environment. As were working with very simple PDF files in these examples, well continue using the method that tells iText to keep the bytes in memory.
public void sign(String src, String tmp, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location) throws GeneralSecurityException, IOException, DocumentException { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', new File(tmp)); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); // Creating the signature ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, subfilter);
Figure 16: adding an empty signature field using Adobe Acrobat Lets save this document, and fill it out using code sample 14. Code sample 14: Signing a signature field
public void sign(String src, String name, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location) throws GeneralSecurityException, IOException, DocumentException { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location);
Again, theres only one difference with code sample 10. We no longer need to pass a rectangle and a page number; the name of the field is sufficient. This way, a developer no longer has to worry about finding the best coordinates to position the signature. Now lets see if we can create an empty signature field using iText. 2.3.2 Creating a signature field programmatically using iText If you know how to create AcroForm fields using iText, you know how to create an empty signature field as shown in figure 16. If you dont know anything about AcroForm fields, take a look at code sample 15. Code sample 15: Creating a signature field
public void createPdf(String filename) throws IOException, DocumentException { // step 1: Create a Document Document document = new Document(); // step 2: Create a PdfWriter PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream(filename)); // step 3: Open the Document document.open(); // step 4: Add content document.add(new Paragraph("Hello World!")); // create a signature form field PdfFormField field = PdfFormField.createSignature(writer); field.setFieldName(SIGNAME); // set the widget properties field.setPage(); field.setWidget( new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_INVERT); field.setFlags(PdfAnnotation.FLAGS_PRINT); // add it as an annotation writer.addAnnotation(field); // maybe you want to define an appearance PdfAppearance tp = PdfAppearance.createAppearance(writer, 72, 48); tp.setColorStroke(BaseColor.BLUE); tp.setColorFill(BaseColor.LIGHT_GRAY); tp.rectangle(0.5f, 0.5f, 71.5f, 47.5f); tp.fillStroke(); tp.setColorFill(BaseColor.BLUE); ColumnText.showTextAligned(tp, Element.ALIGN_CENTER, new Phrase("SIGN HERE"), 36, 24, 25); field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); // step 5: Close the Document document.close(); }
If a field corresponds with a single widget annotation (as is the case here), the field properties and the annotation properties are usually merged into a single dictionary object. We can add the field and its visual representation to a document by adding the PdfFormField object to the PdfWriter using the addAnnotation() method. NOTE: Is it possible to have one signature correspond with more than one widget? Im sorry, but thats not a good question. See the spec about digital signature appearances by Adobe: The location of a signature within a document can have a bearing on its legal meaning. For this reason, signatures never refer to more than one annotation. If more than one location is associated with a signature the meaning may become ambiguous. In code sample 15, we also create an appearance for the empty signature using the class PdfAppearance. This class extends the PdfTemplate class used in iText to create small patches of reusable content. Its not to be mistaken with the PdfSignatureAppearance class. With PdfAppearance, you define what the field looks like before its signed, whereas PdfSignatureAppearance defines what the field looks like after signing. Creating an appearance for an empty signature field is optional: if you omit this code, a valid signature field will be added, but the end user might not really notice it. Hell only see a small orange ribbon added by Adobe Reader marking the location of the field. The original, unsigned document and the resulting, signed document for code sample 15 are shown next to each other in figure 17.
Figure 17: iText created document with empty signature field and the same document signed If you want to add a signature field to an existing document, you can invoke the same method on a PdfStamper object. The only difference is that you dont define the page number on the level of the widget annotation. 2.3.3 Adding an empty signature field to an existing document using iText In code sample 16, we pass the page number as a second parameter for the addAnnotation() method in PdfStamper. Thats the main difference compared to what we saw in code sample 15. Code sample 16: adding a signature field to an existing PDF
PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); // create a signature form field PdfFormField field = PdfFormField.createSignature(stamper.getWriter()); field.setFieldName(SIGNAME); // set the widget properties field.setWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_OUTLINE); field.setFlags(PdfAnnotation.FLAGS_PRINT); // add the annotation stamper.addAnnotation(field, 1); // close the stamper stamper.close();
We can now reuse the method from code sample 14 to sign the document weve created from scratch or the document to which weve added a signature field. Code sample 17 shouldnt have any secrets for you anymore. Code sample 17: Creating a document with a signature field, then sign it
CreateEmptyField appCreate = new CreateEmptyField(); appCreate.createPdf(UNSIGNED); BouncyCastleProvider provider = new BouncyCastleProvider(); Security.addProvider(provider); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream(KEYSTORE), PASSWORD);
The signature appearance as shown in the signed PDF of figure 17 is what a signature created by iText looks like by default. It contains the following information: Who has signed the document? iText extracts this information from the certificate. When was the document signed? If you provide a connection to a timestamp server, iText will use the date provided by the TSA (see chapter 3). Otherwise, iText will use the date and time of the computer that is used for signing, or a Calendar object provided in your code. What was the reason for signing? The reason is provided by you or your program. Where was the document signed? The location is provided by you or your program.
Suppose your customer isnt used to digital signatures . Suppose he doesnt realize that the field marked with the text Digitally signed by is the visual representation of a valid digital signature. Suppose that he wants to see an image of a wet ink signature instead of some plain text. That image as such wouldnt have any legal value whatsoever, but it can be reassuring on a psychological level. Thats more or less what the recommendations in PAdES part 6 are about, and why iText provides different methods to create custom appearances for signatures. 2.4.1 Defining a custom PdfSignatureAppearance In this section, Im going to start by explaining something, and then I want you to completely forget all about it: in early versions of the PDF specification, a signature appearance consisted of five different layers that are drawn on top of each other. These layers were numbered from n0 to n4: n0Background layer. n1Validity layer, used for the unknown and valid state. n2Signature appearance, containing information about the signature. n3Validity layer, used for the invalid state. n4Text layer, for a text representation of the state of the signature
In old Acrobat versions, one would for instance create a graphic of a yellow question mark, and put that into layer n1. On top of this yellow question mark, in layer n2, youd put the information about the signature. If the signature was made invalid, youd see the content of layer n3, usually a red cross. Layers would be made visible or not, depending on the status of the signature.
This information was chosen by the iText developers. It may not correspond with the way you want to present a signature, so lets find out how we can change this appearance.
In code sample 15, we created a PdfAppearance for the signature field before signing, but this appearance was lost and replaced with a default appearance chosen by iText. Code sample 18 now creates a custom appearance instead of the default one. The code is more complex than sample 10 and 14 because we use low-level methods to create a custom PdfSignatureAppearance. We use the getLayer() method to obtain specific layers, and we draw custom content for the background to layer 0, and information about the signature to layer 2. NOTE: If youve followed the advice I gave in the first line of this section, this is the point where youre supposed to ask: Why are you only using layer 0 and layer 2? What happened to layer 1? The answer is: there used to be a layer 1, 3 and 4 (and youll find refe rences to them in iText), but you should no longer use them. PdfSignatureAppearance will ignore all changes applied to these layers, unless you add the following line to your code: appearance.setAcro6Layers(false); (Again: this is not recommended!)
Creating your own appearance is fun, but it demands more work. iText provides some convenience methods that allow you to create custom appearances in a more programmer-friendly way. 2.4.2 Creating signature appearances using convenience methods Suppose you want to change the signature information, but you dont want to have to draw the text using PdfContentByte or ColumnText methods. Custom text Adding text is made easy using the setLayer2Text() and setLayer2Font() methods. Theres even a setRunDirection() method if you want to add text that is written from right to left. Adding an image in the background of the text, can be done with the setImage() method. These methods are used to create the signature widgets shown in figure 19.
Code sample 19 shows the relevant snippets of the complete source code for this example. Code sample 19: Custom signature info and background image.
public void sign1() throws GeneralSecurityException, IOException, DocumentException { // Custom text and custom font appearance.setLayer2Text("This document was signed by Bruno Specimen"); appearance.setLayer2Font(new Font(FontFamily.TIMES_ROMAN)); } public void sign2() throws GeneralSecurityException, IOException, DocumentException { // Custom text, custom font, and right-to-left writing appearance.setLayer2Text( "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"); appearance.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); appearance.setLayer2Font( new Font(BaseFont.createFont("C:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 12)); } public void sign3() throws GeneralSecurityException, IOException, DocumentException { // Custom text and background image appearance.setLayer2Text("This document was signed by Bruno Specimen"); appearance.setImage(Image.getInstance(IMG));
Observe that we use the run direction in the sign2() method to create the signature of Lawrence of Arabia in Arabic. Custom images In sign3(), we add an image using its original dimensions: the image scale is 100%. Change the scale value to 0.5f if you want the image to be scaled to 50%, to 2f if you want it scaled 200%. If you omit the setImageScale() method, iText will scale the image to the absolute dimensions of the signature field, possibly changing the aspect ratio of the image. If you want to avoid the distortion of the background image, you can pass a negative value as the image scale parameter as is done in the sign4() method. Custom rendering mode Apart from changing the signature info and the background, iText also allows you to choose from four different rendering modes: RenderingMode.DESCRIPTIONthis is the default, it just shows whatever description was defined for layer 2. RenderingMode.NAME_AND_DESCRIPTIONthis will split the signature field in two and add the name of the signer on one side, the description on the other side. RenderingMode.GRAPHIC_AND_DESCRIPTIONthis will split the signature field in two and add an image on one side, the description on the other side. RenderingMode.GRAPHICthe signature field will consist of an image only; no description will be shown.
The rendering mode is set using the setRenderingMode() method. Make sure that you also define an image using the setSignatureGraphic() method if you choose the rendering mode GRAPHIC_AND_DESCRIPTION or GRAPHIC. See code sample 20. Code sample 20: Changing the rendering mode and adding a signature graphic
PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature(name); appearance.setLayer2Text("Signed on " + new Date().toString()); appearance.setRenderingMode(renderingMode); appearance.setSignatureGraphic(image);
So far, weve always added a reason and a location to the signature appearance. This information is present in the signature dictionary, but this isnt the only metadata we can add. 2.4.3 Adding metadata to the signature dictionary The PDF specification allows you to add the following metadata to the signature dictionary: Name the name of the person or authority signing the document. This value should be used only when its not possible to extract the name from the signature. M the time of signing. Depending on the signature handler, this may be a normal unverified computer time or a time generated in a verifiable way from a secure server. This should only be used when the time of signing isnt available in the signature. Location the CPU host name or the physical location of the signing. Reason the reason for signing, such as I agree.
The name and the signature time are set by iText automatically, but you can override them. Lets take a look at code sample 21 to find out how this is done. Code sample 21: Setting the metadata for the signature dictionary
PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature(name); appearance.setContact(contact); appearance.setSignDate(signDate); appearance.setSignatureEvent( new SignatureEvent(){ public void getSignatureDictionary(PdfDictionary sig) { sig.put(PdfName.NAME, new PdfString(fullName)); } } );
We change the signature time using the setSignDate() method. This is a dangerous method: if you choose a date that predates the certificate, youll get a warning (a yellow triangle message) saying Signers identity is invalid because it has expired or is not yet valid. If you choose a date in the future, the signature will be broken (a red-cross icon) with the message There are errors in the formatting or information contained in this signature (Signature seems to be signed in future). NOTE: Its NOT a good idea to use the setSignDate() method. Apart from risking error messages in Adobe Reader, using the wrong date when signing a document may also be considered as fraud. Moreover, many instances wont accept a document that is signed without an official timestamp from a TSA. See chapter 3 to find out why a document that is signed without a time stamp could be considered as a useless document. If you take a closer look to code sample 21, you see that there are methods to set the reason, location, and contact info, but suppose that you want to add a key for which there s no method available in iText. In that case, you can use the SignatureEvent interface. Its getSignatureDictionary() method will be called right before the dictionary is written to the file, so you can use it to add extra keys or to override existing ones. In the code sample, I added a name Bruno L. Specimen which is slightly different from the name on the certificate. NOTE: This example is meant to explain a concept. Dont use it in your applications. Many of these metadata values arent visible in Adobe Reader. For example: Adobe Reader will prefer the name obtained from the certificate instead of the name added in the signature dictionary (which should only be used in case the name cant be retrieved from the certificate). Youll need to look inside the PDF document (for instance using iText RUPS) to see the information we added in code sample 21. Figure 21 shows such a look inside.
Figure 21: A look inside a signed PDF showing the keys of the Signature dictionary Theres one more method, actually a very important one, we havent discussed yet: a method to set the certification level. 2.4.4 Ordinary and Certifying signatures There are three different types of signatures. One of them isnt relevant in the context of this paper. Two of them are shown in figure 22.
Lets recycle code sample 10 once more, and add one line:
public void sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, int certificationLevel, String reason, String location) throws GeneralSecurityException, IOException, DocumentException { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); appearance.setCertificationLevel(certificationLevel); // Creating the signature ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, subfilter); }
In code sample 22, we pass a parameter named certificationLevel. This parameter can have one of the following values available as constants in the PdfSignatureAppearance class: NOT_CERTIFIED creates an ordinary signature aka an approval or a recipient signature. A document can be signed for approval by one or more recipients. CERTIFIED_NO_CHANGES_ALLOWED creates a certification signature aka an author signature. After the signature is applied, no changes to the document will be allowed. CERTIFIED_FORM_FILLING creates a certification signature for the author of the document. Other people can still fill out form fields or add approval signatures without invalidating the signature. CERTIFIED_FORM_FILLING_AND_ANNOTATIONS creates a certification signature. Other people can still fill out form fields- or add approval signatures as well as annotations without invalidating the signature.
This example will work perfectly for PDF documents without any signature. PdfStamper will take the objects that are read by PdfReader, add new objects that represent a text annotation, and reorganize the objects creating a new document. The internal structure of this new PDF file can be quite different when compared to the original file. It goes without saying that this will break the signature. Code sample 24 shows how to preserve the original bytes that were signed: Code Sample 24: how to add an annotation to a signed document
public void addAnnotation(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), '\0', true); PdfAnnotation comment = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(200, 800, 250, 820), "Finally Signed!", "Bruno Specimen has finally signed the document", true, "Comment"); stamper.addAnnotation(comment, 1); stamper.close(); }
Did you spot the difference? The code is almost identical. We only used a different constructor for PdfStamper. The zero byte means we dont want to change the version number of the PDF file. The Boolean value indicates whether or not we want to manipulate the file in append mode. This value is false by default. The original bytes arent preserved. By changing this value to true, we tell iText not to change any of the original bytes. This is often referred to as using PdfStamper in append mode. The extra objects defining the annotation will be added after the original %%EOF statement. Some other objects will be overridden, but the original file structure will be kept intact. As a result, an ordinary signature will not be broken when using code sample 24.
Figure 23: Preserving or breaking a signature Its important to understand that adding or removing any other content will break any type of signature. For instance: its not possible to add some extra words to a page. Code sample 25: Breaking a signature by adding text
public void addText(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), '\0', true); ColumnText.showTextAligned(stamper.getOverContent(1), Element.ALIGN_LEFT, new Phrase("TOP SECRET"), 36, 820, 0); stamper.close();
Code sample 25 works for most ordinary PDFs, but it will always break the signature of a signed PDF, even if we create the PdfStamper object in append mode. The code to add a second signature (and a third, and a fourth) is similar to the code adding an annotation. Code sample 26 wont break existing signatures unless the modification detection and protection level of one of the signatures is no changes allowed. Code sample 26: adding an extra signature
public void signAgain(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location) throws GeneralSecurityException, IOException, DocumentException { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature( new Rectangle(36, 700, 144, 732), 1, "Signature2"); // Creating the signature ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, subfilter); }
Figure 24 shows two examples, in the upper window, we have a document that is signed twice by Bruno Specimen, once with a certification signature that allowed adding form fields and once with an approval signature. In the lower window, the first signature was a certification signature that doesnt allow a second signature.
Figure 25: Schematic view of a PDF file that is signed trice The part above the line marked with Rev1 is revision 1 of the document. Its identical to what we had in figure 6. When signing the document with a second signature, we dont change any of the bytes of revision 1. We add additional content (provided that the first signature allows this content), and we create a new signature. This new signature is based on a message digest that includes the entire byte array of revision 1. The result is revision 2. When signing the document with a third signature, the bytes of revision 2 are preserved. NOTE: Looking at figure 25, you see that the signatures have to be applied sequentially. Its not possible to sign in parallel. For instance: a publisher cant send his contract to two authors at the same time for approval, and then merge the signed documents afterwards if the signatures need to be in the same document (as opposed to bundled in a portfolio). One author always has to sign the contract first, then followed by the other author.
Figure 26: This form needs to be signed by Alice, Bob, and Carol
In code sample 27, well let iText define the position. Well create a table with one column, and well use cell events to add the signature fields at the correct position in the table. Code sample 27: Creating a form with empty fields
public void createForm() throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FORM)); document.open(); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); table.addCell("Signer 1: Alice"); table.addCell(createSignatureFieldCell(writer, "sig1")); table.addCell("Signer 2: Bob"); table.addCell(createSignatureFieldCell(writer, "sig2")); table.addCell("Signer 3: Carol"); table.addCell(createSignatureFieldCell(writer, "sig3")); document.add(table); document.close(); } protected PdfPCell createSignatureFieldCell(PdfWriter writer, String name) { PdfPCell cell = new PdfPCell(); cell.setMinimumHeight(50); PdfFormField field = PdfFormField.createSignature(writer); field.setFieldName(name);
The createForm() method is pretty straightforward. We create a document from scratch in five steps. In step four, we create a table with one column; we define its width percentage. Then we add six cells, and we add the table to the document. Three cells are created in a separate method. The createSignatureFieldCell() method creates a PdfPCell instance, we define a minimum width, and then we create a signature field. We dont define a widget yet. Instead we pass the field object to a PdfPCellEvent and we add an instance of this event to the cell. What does this event do? After the cell is rendered to the page by iText, the cellLayout() method is invoked. In this method, we set the page and the position of the signature field, and we add the signature field to the PdfWriter. Now lets sign this form three times.
2.5.3 Signing a document multiple times Please read figure 27 from top to bottom. Theres revision 1 signed by Alice using a certification signature. Two blank signature fields remain. Revision two is signed by Bob. Finally revision 3 is signed by Carol.
They all signed their revision using code sample 28. Code sample 28: Signing in append mode
public void sign(String keystore, int level, String src, String name, String dest) throws GeneralSecurityException, IOException, DocumentException { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream(keystore), PASSWORD); String alias = (String)ks.aliases().nextElement(); PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD); Certificate[] chain = ks.getCertificateChain(alias); // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setVisibleSignature(name); appearance.setCertificationLevel(level); // Creating the signature ExternalSignature pks = new PrivateKeySignature(pk, "SHA-256", "BC"); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS); }
Alice, Bob and Carol signed the form in the order as defined in code sample 29. Note that the value of DEST in this example isnt a path (as was the case in previous examples), but a pattern for a path. Code sample 29: Signing a document multiple times
app.sign(ALICE, PdfSignatureAppearance.CERTIFIED_FORM_FILLING, FORM, "sig1", String.format(DEST, "alice"));
That is: first Alice added a certification signature, and then Bob and Carol added an approval signature. Suppose we switched the order. Suppose that Alice and Bob first approve the document, and that Carol certifies the document afterwards. Or suppose that both Alice and Carol try adding a certification signature, what will happen? I never questioned this before I wrote this paper, because when discussing modification detection and prevention using certification signatures, ISO-32000-1 mentions the author of a document as the person applying the first signature'13. Out of curiosity, I tried three different setups and I was surprised by the result. Apparently, the certification signature doesnt have to be the first signature in the document. I dont know if it makes sense, but Adobe Reader validates documents that are signed by approval signatures first, followed by a certification signature as correct, provided that the certification level allows form filling. I was also surprised by the output if the final signature is a certification signature that doesnt allow form filling. If you look closely at figure 28, you can see that Caro ls certification signature is valid, but the original signatures by Alice and Bob are broken. Somehow this doesnt make sense, does it?
Figure 28: Strange results for strange signature operations As expected, Alices certification signature is invalid after Carol tried to sign the document using a second certification signature, but the output is kind of strange. In the signature panel, we see Certified by %s instead of Certified by Carol, and theres a looking glass instead of a red cross.
13
Again, Alice will be the author of the document. Shell sign first using a certification signature that allows form filling. Secondly, Bob will have to fill in a field, writing Read and approved by Bob, after which he signs the document with an approval signature. Thirdly, Alice needs to do the same thing. Finally, Dave approves the document as the fourth party in the workflow. Figures 30 to 35 show different steps in the process.
In figure 31, Bob has filled out the field named approved_bob. The signature panel says that Changes have been made to this document that are permitted by the certifying party . Code sample 30 shows how you could fill out this field using iText. Code sample 30: filling out a form using iText
public void fillOut(String src, String dest, String name, String value) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), '\0', true); AcroFields form = stamper.getAcroFields(); form.setField(name, value); form.setFieldProperty(name, "setfflags", PdfFormField.FF_READ_ONLY, null); stamper.close(); }
As you can see, were using PdfStamper in append mode to avoid breaking Alices signature. The rest of the code is pretty standard in iText. You get an AcroFields object from the stamper, and you fill out fields using the setField() method, passing a key (the name as it was defined in the form) and a value. We also set the field to read only so that somebody further down the workflow doesnt accidentally change the field. The method setFieldProperty() expects four parameters: field the name of the field of which you want to change a property action the action you want to perform (fflags will replace all field flags, setfflags will add field flags, clrfflags will remove all field flags). value one or more properties set using field flags you want to change. See the constants starting with FF_ in the PdfFormField class). Inst[] an array of indexes to select specific field items. In this case, each field corresponds with a single item, so its safe to use null (which will apply the property to all items).
Figure 31: step 2, Bob says he has read and approved the document
Figure 32: step 3, Bob has signed the document for approval Now Carol repeats these two steps.
Figure 33: step 4, Alice says she has read and approved the document Although filling out a field after signing for approval is allowed, we see a yellow question mark in figure 33. Theres nothing to worry about: none of the signatures i s broken. Filling out fields after a document was signed for approval is allowed, but we get a warning when the value of fields (in this case a field named approved_carol) has changed. If we take a look at figure 34, we see that the yellow triangle disappears as soon as Carol signs the document.
We still see an extra line in the signatures panel saying the field approved_carol was filled out, but the yellow triangle previously shown in the upper blue bar (partly covered by the blue certification ribbon) has now been replaced with blue circle with an i inside. The exclamation point informs us that a change was applied, but the change was allowed by the MDP settings of the signatures. Its probably a good idea to skip the form filling step, and to fill out the form and sign it simultaneously. Thats what we do in code sample 31. Code sample 31: Filling and signing in one go
public void fillOutAndSign(String keystore, String src, String name, String fname, String value, String dest) throws GeneralSecurityException, IOException, DocumentException { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream(keystore), PASSWORD); String alias = (String)ks.aliases().nextElement(); PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD); Certificate[] chain = ks.getCertificateChain(alias); // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true); AcroFields form = stamper.getAcroFields(); form.setField(fname, value); form.setFieldProperty(fname, "setfflags", PdfFormField.FF_READ_ONLY, null); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setVisibleSignature(name); // Creating the signature ExternalSignature pks = new PrivateKeySignature(pk, "SHA-256", "BC"); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS); }
Figure 35: step 6, Dave said he approves the document and signed it
We have protected the fields against accidental change by other people down the workflow, but one could easily change the read-only status, and replace the value of the field in a later revision. Wouldnt it be nice if an approval signature could lock a field or even the whole document the way you can lock a document using a certification signature? Lets take a look at the different options. 2.5.5 Locking fields and documents after signing Since PDF 1.5, its possible to lock s pecific fields when signing a document. In PDF 2.0, there will be a new feature that allows you to define the same permissions that are already available for certification signatures, but now also for approval signatures. In both cases, this be achieved by adding a /Lock entry to the signature field with a PdfSigLockDictionary as value. Code sample 32 shows different flavors of this dictionary. Code sample 32: creating PdfSigLockDictionary objects
table.addCell("For approval by Bob"); table.addCell(createTextFieldCell("approved_bob")); PdfSigLockDictionary lock = new PdfSigLockDictionary(LockAction.INCLUDE, "sig1", "approved_bob", "sig2"); table.addCell(createSignatureFieldCell(writer, "sig2", lock)); table.addCell("For approval by Carol"); table.addCell(createTextFieldCell("approved_carol")); lock = new PdfSigLockDictionary(LockAction.EXCLUDE, "approved_dave", "sig4"); table.addCell(createSignatureFieldCell(writer, "sig3", lock)); table.addCell("For approval by Dave"); table.addCell(createTextFieldCell("approved_dave")); lock = new PdfSigLockDictionary(LockPermissions.NO_CHANGES_ALLOWED); table.addCell(createSignatureFieldCell(writer, "sig4", lock));
If you want to define a lock that covers the whole document, use the PdfSigLockDictionary constructor with one LockPermissions argument. iText will automatically select LockAction.ALL, and youll need to choose one of the following LockPermissions: NO_CHANGES_ALLOWED after approval, nobody can change the document without breaking the signature FORM_FILLING after approval, form filling is allowed. FORM_FILLING_AND_ANNOTATION after approval, form filling and adding annotations is allowed.
If a document already has specific permissions in place, you need to apply a more restrictive action. You cant use this functionality to remove restrictions. Lets take a look at code sample 33 and examine the createSignatureFieldCell() snippet that adds the lock to the signature field. Code sample 33: locking fields
We dont add the PdfSigLockDictionary entry straight to the field object. Instead we add the dictionary to the PdfWriter instance first using the addToBody() method. This writes the object to the OutputStream creating a reference number. We use that number to create an indirect reference object with the getIndirectReference() method, and add it as value for the /Lock entry. Why? Because ISO-32000-1 demands that the entry shall be an indirect reference. Lets take a look at figure 36 to see the form with all fields filled in and all signatures applied. The final line in the signature panel informs us that the document was locked by sig4.
Figure 37 shows what happens if Chuck changes Bobs text field after Bob signed the document.
Figure 37: Bobs signature is broken by Chuck Bobs signature is broken because the approved_bob field was locked by sig2. Figure 38 shows what happens if Chuck changes approved_carol after Dave has signed.
Carols signature is broken because sig3 restricted the permissions on Carols text field. Daves signature is also broken because sig4 doesnt allow any further changes on the complete document. Lets conclude this section with one final note about the LockPermissions functionality: this extra permission that can be set on the document level will only be introduced in ISO-32000-2 (PDF 2.0), but you can already use it in Adobe Acrobat and Adobe Reader X.
2.6 Summary
In this chapter, we started with a simple Hello World example showing how easy it is to sign a document using iText. In the examples that followed, we focused mainly on the cosmetic aspects of a signature. We learned how to create a signature field, and we found out how to create a variety of appearances. We noticed that there are different types of signatures: the blue ribbon and the green check mark signatures. The former represent certification or author signa tures, the latter are approval signatures. We ended this chapter by applying one certification and multiple approval signatures in a workflow. All these examples were meant to get acquainted with using iText for signing PDF documents. The signatures weve created so far arent created using the best practices. In the next chapter, well explain whats missing.
In the real world, we want documents to be signed by real human beings or real organizations, not by people weve made up. Thats what this chapter is about. We want to make sure that we can trust the document, its signature and its signer.
Imagine two real-world people who have never met: Alice and Bob. Alice needs to send a signed PDF document to Bob. For Bob to be able to validate the signature in the document, he needs Alices public certificate. That certificate is embedded in the signed PDF document, so he could add it to his list of Trusted Identities (2.2.2), but how does Bob know he can trust that certificate? Anyone can make a self-signed certificate using Alices name. This problem can be solved by involving a third person who knows both Alice and Bob. This person needs to be a trusted entity, so lets call him Trent. Trent knows Alice, and Trent is known by Bob. If somehow Trent marks Alices public key to confirm its authenticity, Bob knows that Alice is who she claims she to be. In the context of signatures, Trent does this by signing Alices certificate. Bob can decrypt Alices signed certificate using Trents certificate, and as Bob trusts Trents public certificate, he can now also trust Alices certificate. See figure 39:
Figure 39: Bob trusts Alice, because he knows Trent and Trent trusts Alice
Did we reach those three goals? Not yet! By using the concepts of hashing and encryption, we can assure that a document hasnt been modified. Using the public/private key mechanism, we can authenticate the author and he cant deny the authorship, but so far, weve only been using selfsigned certificates (see section 1.3.3). Weve signed documents in the name of fictive personae named Alice, Bob, Carol, and Dave.
public static final String SRC = "src/main/resources/hello.pdf"; public static final String DEST = "results/chapter3/hello_cacert.pdf"; public static void main(String[] args) throws IOException, GeneralSecurityException, DocumentException { Properties properties = new Properties(); properties.load(new FileInputStream("c:/home/blowagie/key.properties")); String path = properties.getProperty("PRIVATE"); char[] pass = properties.getProperty("PASSWORD").toCharArray(); BouncyCastleProvider provider = new BouncyCastleProvider(); Security.addProvider(provider); KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName()); ks.load(new FileInputStream(path), pass); String alias = (String)ks.aliases().nextElement(); PrivateKey pk = (PrivateKey) ks.getKey(alias, pass); Certificate[] chain = ks.getCertificateChain(alias); SignWithCAcert app = new SignWithCAcert(); app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "Test", "Ghent", null, null, null, 0); }
I dont want to publish my password in a white paper that can be read by anyone. So Ive stored it in a properties file that can be found in my home directory. If you want to run this example, youll have to change the path c:/home/blowagie/key.properties to a path that points to a file containing at least two properties: a path to a p12 file and a password. Observe that the sample code assumes that the key store password and the password for the private key are identical.
14 15
https://www.globalsign.com/ http://www.cacert.org/
If youd inspect the Certificate array named chain in code sample 34, youd discover that it contains two certificates instead of just one as was the case in code sample 11. You can check this by taking a look at Certificate Viewer for the signed PDF document as is done in figure 40.
Theres an important difference with what we saw in figure 10. The certificate is no longer selfsigned! There are two certificates shown in the Certificate viewer. Theres my own certificate (Bruno Lowagie) which is the signing certificate (always the first element in the chain), and theres the certificate of the CA Cert Signing Authority, the root certificate (the final element in the chain). The CA Cert Signing Authority is the Issuer of my certificate. 3.1.2 Trusting the root certificate of the Certificate Authority CA Certs root certificate corresponds with the private key that was used to sign all the public certificates that are issued to the members of the CA Cert Community. You can trust CA Certs root certificate in the PDF the way we did in section 2.2.2, or you can download that root certificate from the cacert.org site and add it as a trusted identity as we did in section 2.2.3. From now on, all the people who have a certificate issued by this CA will be trusted. You no longer have to trust each individual separately. This is shown in figures 41 and 42.
Figure 41: Individual signature not trusted, but Signers identity is valid
public void sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location, Collection<CrlClient> crlList, OcspClient ocspClient, TSAClient tsaClient, int estimatedSize) throws GeneralSecurityException, IOException, DocumentException { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setReason(reason); appearance.setLocation(location); appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); // Creating the signature ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter); }
In code sample 35, the hardcoded null values and the 0, are replaced by different objects: a Collection of CrlClients, an OcspClient and a TSAClient. Finally, theres also an estimatedSize value. Lets take a look at these objects one by one, starting with the ones regarding certificate revocation.
In this case, my signing certificate doesnt contain any CRL, but the root certificate does. iText offers different ways to use this information. 3.2.2 Getting the CRL online If you want to embed CRLs in a PDF, you need a Collection of CrlClient elements. Creating a CrlClientOnline instance is the easiest way to achieve this. See code sample 37. Code sample 37: Using the default CrlClient implementation
LoggerFactory.getInstance().setLogger(new SysoLogger()); List<CrlClient> crlList = new ArrayList<CrlClient>(); crlList.add(new CrlClientOnline()); SignWithCRLDefaultImp app = new SignWithCRLDefaultImp(); app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "Test", "Ghent", crlList, null, null, 0);
The MakeSignature class will pass a single certificate to CrlClientOnline, starting with the signing certificate. As soon as the CrlClient finds one, iText will stop looking, and use that CRL. While this solution is easy and while it will work in many cases, it wont work for my CAcert certificate. Moreover, well soon discover its not the best solution in general. Trusting the root certificate in your Java Runtime Environment (JRE) Lets start by explaining why its doesnt work for a CAcert certificate. As you can see, the URL of the Certificate Revocation List uses https. Suppose that youve installed a JRE or JDK from scratch, and you want to run this example, youll hit the following error as soon as iText tries to fetch the C RL:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException
When seeing this message, you should immediately understand that this is not an iText problem. It should also be clear that this isnt a programming error either. Its a matter of configuration. iText uses the java.net.URL class to create a connection to the https-server at cacert.org. Https means that SSL is used, and that the Java classes try to do an SSL Handshake. For this handshake to
The MakeSignature class processes CrlClientOnline a first time for the first certificate. As it doesnt find an URL for the CRL, it moves to the next certificate and processes CrlClientOnline a second time. Now it finds this URL: https://www.cacert.org/revoke.crl which should be a valid URL, but when trying to fetch the CRL, the cacert.org server is returning status code 302, which means youre being redirected. iText doesnt trust this, and wont embed the CRL. How can we fix this? Creating a CrlClient using a specific URL If you go to the CAcert site, you see that there are two URLs to fetch the Certificate Revocation List, either http://crl.cacert.org/revoke.crl or https://crl.cacert.org/revoke.crl. You can create an instance of CrlClientOnline using either one. The former solves the SSL handshake problem; the latter solves the redirect problem if youve added CAcerts root certificate to your JREs cacerts file. Code sample 39: Creating a CRL list for a CAcert certificate
CrlClient crlClient = new CrlClientOnline("https://crl.cacert.org/revoke.crl"); List<CrlClient> crlList = new ArrayList<CrlClient>(); crlList.add(crlClient);
Code sample 39 example is added to show how to work around some problems inherent to certificates obtained from CAcert. Creating a CrlClient using the Certificate chain A better way to embed CRLs that are fetched online is shown in code sample 40.
c.i.t.p.s.MakeSignature INFO Processing com.itextpdf.text.pdf.security.CrlClientOnline c.i.t.p.s.CrlClientOnline INFO Looking for CRL for certificate CN=Bruno Lowagie,E=bruno@_.com c.i.t.p.s.CrlClientOnline INFO Skipped CRL url: null c.i.t.p.s.MakeSignature INFO Processing com.itextpdf.text.pdf.security.CrlClientOnline c.i.t.p.s.CrlClientOnline INFO Looking for CRL for certificate O=Root CA, OU=http://www.cacert.org,CN=CA Cert Signing Authority,[email protected] c.i.t.p.s.CrlClientOnline INFO Found CRL url: https://www.cacert.org/revoke.crl c.i.t.p.s.CrlClientOnline INFO Checking CRL: https://www.cacert.org/revoke.crl c.i.t.p.s.CrlClientOnline INFO Skipped CRL: Invalid HTTP response: 302 for https://www.cacert.org/revoke.crl
This example is much better in general because you dont need to hardcode the URLs. iText will loop over every certificate in the chain as we did in sample 36, and will fetch every CRL it encounters, not just the first one. This is important because if you buy a certificate from an established CA, there will be more certificates in the chain, and youll want to embed the CRLs for each of those certificates. Now that we finally succeeded in embedding a CRL into a PDF, lets take a look at the certificate details in the Certificate Viewer. See figure 43.
Figure 45: A CAcert Certificate Revocation List Instead of downloading the list from our code, we can now create a CrlClientOffline object. This object has two constructors. One that accepts a byte[] and one that accepts a CRL object. Code sample 41: Creating an offline CrlClient
FileInputStream is = new FileInputStream(CRL); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; while (is.read(buf) != -1) baos.write(buf); CrlClient crlClient = new CrlClientOffline(baos.toByteArray()); List<CrlClient> crlList = new ArrayList<CrlClient>(); crlList.add(crlClient);
In code sample 41, we read a FileInputStream into a ByteArrayOutputStream and we use those bytes to create a CrlClientOffline object. In case youre curious how to create a CRL object from a file, see code sample 42.
You can use this CRL object instead of the byte[] to create a CrlClientOffline. WARNING: A downloaded CRL is valid for only a limited period. It usually expires after seven days. If it didnt expire, one could first download a CRL, then steal a cer tificate, then sign a document using the stolen certificate. As a matter of fact, this is always possible if the signature is applied within the expiration period. Thats why its good practice to cache a CRL only for a limited period. A CA normally updates its CRLs every 30 minutes. If possible, you should create a caching system that downloads a new CRL every half hour. Finally, you may have noticed one of the major downsides of embedding CRLs: the file size can get really big if the Certificate Authority doesnt really bother about the size of the CRL ( which is the case with CAcert). Later on in this chapter, well see how GlobalSign solves this problem. We can solve the problem ourselves by using the Online Certificate Status Protocol (OCSP) instead of CRLs. 3.2.4 Using the Online Certificate Status Protocol (OCSP) OCSP is an internet protocol for obtaining the revocation status of a certificate online. You can post a request to check the status of a certificate over http, and the CAs OCSP server will se nd you a response. You no longer need to parse and embed long CRLs. An OCSP response is small and constant in size, and it can be easily embedded in a digital signature. Use code sample 43 if you want to know if your certificate supports OCSP: Code sample 43: fetching the OCSP URL from a certificate
BouncyCastleProvider provider = new BouncyCastleProvider(); Security.addProvider(provider); KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName()); ks.load(new FileInputStream(path), pass.toCharArray()); String alias = (String)ks.aliases().nextElement(); Certificate[] chain = ks.getCertificateChain(alias); for (int i = 0; i < chain.length; i++) { X509Certificate cert = (X509Certificate)chain[i]; System.out.println(String.format("[%s] %s", i, cert.getSubjectDN())); System.out.println(CertificateUtil.getOCSPURL(cert)); }
When I use this code on my CAcert certificate, I get the following output:
[0] CN=Bruno Lowagie,E=bruno@______.com http://ocsp.cacert.org [1] O=Root CA,OU=http://www.cacert.org,CN=CA Cert Signing Authority,[email protected] null
When signing a document, iText needs a way to send a post to the url http://ocsp.cacert.org, and to receive a response regarding the validity of my certificate. This is done by an implementation of the OcspClient interface, for instance iTexts OcspClientBouncyCastle class that uses the Bouncy Castle library. Code sample 44: Signing with OCSP
OcspClient ocspClient = new OcspClientBouncyCastle(); SignWithOCSP app = new SignWithOCSP();
If you look at code sample 44, youll see that we dont need to pass any URL or certificate; iText will pass the certificate with the OCSP URL to the OcspClient implementation. Lets take a look at the Certificate Viewer of the resulting PDF. Figure 46 demonstrates that the selected certificate is considered valid because it has not been revoked as verified using the Online Certificate Status Protocol (OCSP) response that was embedded in the signature.
Figure 46: A digital signature with an embedded OCSP response Its important to understand that the choice between using CRLs and using OCSP isnt an or this, or that question. For the sake of the consumer of the signed document, its always best to use both because that offers more certainty than using only one type of certificate revocation checking.
Figure 47: Alice signs a document without CRL and without timestamp
It gets even worse if the certificate is revoked. This doesnt always mean that Alices key was stolen. Its also possible that Alice has left the company she used to work for. She had a key she could use to sign company documents until 2014, but if she leaves the company in 2013, the company will most probably want to revoke that key. Figure 48 shows the consequences if that ever happens:
Figure 48: Alice leaves the company in 2013, her key is revoked
Figure 49: Alice leaves the company; all documents she signed in the past remain valid Can we achieve this merely by adding a CRL or an OCSP response? No, because Alice could have cached the information about the certificate revocation before leaving the company. The only way we can assure that a document remains valid, is by also adding a timestamp. A document that was signed by Alice in 2012 will contain revocation information dating from 2012 saying that her certificate wasnt revoked at that time; the timestamp will assure that the document was certainly signed in 2012. This signature will survive the revocation and expiration date. But how do we add a timestamp to a digital signature in a PDF document? 3.3.2 Connecting to a timestamp server To solve this problem, we need to involve another third party: a Time Stamping Authority (TSA). A TSA provides an online service, accepting a message digest and concatenating a timestamp to it. This is done on a timestamp server that is contacted during the signing process. The timestamp server will return a hash that is signed using the private key of the TSA. NOTE: You need to be online to create a signature with a timestamp. Connecting to a timestamp server sending and receiving the hash takes time. If you need to sign thousands of documents in batch, you could ask the TSA to provide a time stamping certificate. This certificate will usually be stored on a Hardware Security Module. With code sample 45, we try to find out if CAcert also offers timestamping services. Code sample 45: Extracting the TSA URL from a certificate
BouncyCastleProvider provider = new BouncyCastleProvider(); Security.addProvider(provider); KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName()); ks.load(new FileInputStream(path), pass.toCharArray()); String alias = (String)ks.aliases().nextElement();
This code returns null for both certificates, meaning CAcert probably doesnt offer any TSA services. Checking their web site confirms this. Fortunately, I received an account for a timestamp server at GlobalSign. For some accounts you need an URL, a username and a password. In that case, you can create a TSAClient instance as is done in code sample 46. Observe that were again using an implementation that uses the Bouncy Castle library, just like we did for the OcspClient. Code sample 46: Creating a TSAClient Sometimes you only need an URL containing a token identifying the user. In this case you can drop the parameters tsaUser and tsaPass. Theres also a constructor that accepts an estimated size and a digest algorithm. Well learn more about the estimated size in the final section of this chapter. How to recognize a document that has a timestamp Lets take a look at the result. In figure 50, weve opened the document, and were looking at the Date/Time information in the Signature properties. Weve also opened the Certificate viewer for the timestamp certificate.
In this example, we implement the TSAInfoBouncyCastle interface. As soon as iText receives a response from a TSA, it will trigger the inspectTimeStampTokenInfo() method. In this case, we write the generation time (a Date object) to the System.out. Check out the API of Bouncy Castles TimeStampTokenInfo class, and youll discover that theres much more information you can retrieve. Instead of just writing this info to a console, you could easily pass it to other objects to create your own logs in the form of a file or records in a database. Weve finally created a PDF document that complies with the best practices. Theres only one thing that still bothers us: why do we have to add the root certificate of the CA to the Trusted Identities manually? Why cant Adobe Reader show a green check mark now that weve followed the rules by the book? Arent there easier ways to trust root certificates?
Figure 51: Changing the Security settings regarding certificates Of course, this will only work for specific root certificates that are known on the level of the OS. You can also see another option in figure 51: you can also load the Security settings from a server. In this case, a companys Sysadmin will have a tool to manage root certificates, and he can distribute a list of trusted certificates among the employees of the company.
16 17
A key store on a hardware device can be used to create a signature, but the private key cant be extracted. This means that we can no longer create a KeyStore object by passing an InputStream as we did in all the previous examples. What do we need to do instead? Lets start with an example that uses the Windows Certificate Store to get access to an USB token. 3.4.3 Signing a document using a USB token (part 1: MSCAPI) For this example, well use an iKey 4000 from SafeNet 21 (see figure 53).
20 21
http://csrc.nist.gov/publications/fips/fips1401.htm#sec1.2 http://www.safenet-inc.com/
22
http://www.safenet-inc.com/data-protection/authentication/pki-authentication/
This is the first example where the actual signing is nt done on the JVM, but externally, on a separate device. Well see some more examples in chapter 4 dedicated entirely to this type of signatures. Inspecting the Certificate chain When I open the resulting PDF, it is validated automatically. I immediately have a green check mark; I dont need to trust a certificate from a CA manually. Lets take a look at the Certificate Viewer shown in fi gure 56 to find out why.
Figure 56: The Certificate Viewer showing the Adobe Root CA Certificate
Figure 57: The Adobe Root CA, the intermediate GlobalSign and the end-user certificates Also using its Primary certificate, GlobalSign issued another intermediate certificate GlobalSign SHA256 CA for Adobe. With this certificate, they issue end user certificates for USB tokens. We already saw these certificate in figure 56 when we looked at the certificate chain in the document we signed with the iKey 4000.
Do you remember when we first looked at the concept of a digitally signed PDF document? In figure 6, I drew a schema of a document with content in blue and a signature in light red. I explained that iText either keeps the blue bytes in memory, or stores them in a temporary file (section 2.2.4) reserving a hole that can only be filled once the blue bytes are hashed and signed. How does iText know in advance which size the signature will have? How does it determine the size of the hole? The answer is: iText doesnt know. iText makes an educated guess. Theres a parameter named estimatedSize in code sample 35. Up until now, weve always passed 0 as its value. In this case, iText will start with a value of 8192 bytes. It will add the number of bytes of the CRLs, and an extra 10 bytes for every CRL. 4192 bytes will be added if you defined an OcspClient, another 4192 if you created a TSAClient (unless youve defined another size in the class that implements the time stamping interface). Normally, this educated guess creates a hole that is too big. If you look inside the PDF, youll see plenty of zeros at the end of the sig nature bytes. So be it. Its better to provide too much space, than not enough. Suppose you have an algorithm can make a better estimation that is lower than iTexts guess. In that case, you can pass your estimated size to the MakeSignature class. In the case of code sample 49, iText would reserve 16,579 bytes: 8,192 (the minimum guess) + 4,192 (for the OCSP) + 4,192 (for the TSA). But lets start with an estimated size of 13,000, and see what happens. If 13,000 bytes arent enough, well add another 50, and well keep doing so until the signature fits. Code sample 49: Making a hit-and-miss estimation of the signature size
boolean succeeded = false; int estimatedSize = 10300; while (!succeeded) { try { System.out.println("Attempt: " + estimatedSize + " bytes"); app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "Test", "Ghent", null, ocspClient, tsaClient, estimatedSize); succeeded = true; System.out.println("Succeeded!");
3.6 Summary
In this chapter, weve learned about the best practices for signing PDF documents. First we were introduced to certificate authorities. We found out that they act as trusted entities that issue certificates to end users. One could argue that all the signatures we created in the previous chapter were useless and of no value: they didnt contain any certificate revocation information, nor did they have a timestamp. We experimented with different ways to embed certificate revocation information into a PDF: we worked with online and offline Certificate Revocation Lists and we used the Online Certificate Status Protocol. Then we discovered the need for a Time Stamping Authority, and we connected to a timestamp server. All of this didnt automatically result in a PDF that showed us a green check mark automatically, but then we created a document that was signed using an USB token and a signing certificate that was chained up to the Adobe root. Finally, we created a small example that explained why documents that are signed using iText are slightly bigger than strictly necessary. In the next chapter, well start by rewriting the example with the iKey 4000, only now, we wont depend on the Microsoft CryptoAPI, but well use PKCS#11.
The actual size of the signature was somewhere between 10,350 and 10,400 bytes. This means that iText reserved at least 6,176 bytes that werent necessary for the signature. Is that a h igh price to avoid an IOException? Maybe, but I dont think so. In some situations, you have to enter a PIN for each signing operation. You dont want to repeat that action for each try. Some smart cards take more than a second to create a signature. In our hit-and-miss example, weve made three connections to a timestamp server. Is that acceptable? Does that cost more than having a file with 6 extra Kbytes? Maybe, maybe not; its up to you to make a choice.
LoggerFactory.getInstance().setLogger(new SysoLogger());
Properties properties = new Properties(); properties.load(new FileInputStream(PROPS)); char[] pass = properties.getProperty("PASSWORD").toCharArray(); String pkcs11cfg = properties.getProperty("PKCS11CFG"); BouncyCastleProvider providerBC = new BouncyCastleProvider(); Security.addProvider(providerBC); FileInputStream fis = new FileInputStream(pkcs11cfg); Provider providerPKCS11 = new SunPKCS11(fis); Security.addProvider(providerPKCS11); KeyStore ks = KeyStore.getInstance("PKCS11"); ks.load(null, pass); String alias = (String)ks.aliases().nextElement(); PrivateKey pk = (PrivateKey)ks.getKey(alias, pass); Certificate[] chain = ks.getCertificateChain(alias); OcspClient ocspClient = new OcspClientBouncyCastle(); TSAClient tsaClient = null;
Its not really necessary to create a Logger, but it helps you to see what happens during the signing process. The first difference is that we dont load a key store file and a password, but a configuration file and a password. In my case, the content of the configuration file looks like this:
Name = Luna library = /usr/lunasa/lib/libCryptoki2_64.so slot = 1
Im working on a Luna SA from SafeNet23. Its connected to a Linux machine, hence the name and the reference to an Executable and Linkable Format (ELF) file. The HSM itself is installed in a slot inside the Luna SA. In my case, I use slot 1. Again well add the Bouncy Castle provider because we rely on it for the time stamp, and we reuse the sign() method that uses Bouncy Castle to create the digest of the PDF bytes, but now we also create an instance of a SunPKCS11 provider by passing an InputStream that allows it to read the configuration file. In our case, the name of the provider will be SunPKCS11-Luna. Now we create a PKCS11 KeyStore using the parameter PKCS11, and we load it using the password for the HSM. From that moment on, all the code is almost identical to what we had before. The only change you need to make is the provider used for signing. You can still use the PrivateKeySignature class, but dont forget that it now expects SunPKCS11-Luna instead of BC. Now that was easy, wasnt it? Theres only one difficulty about it: What to put in the configuration file? That depends. Ask the vendor of the device and hell help you out. Or lets try another example ourselves. 4.1.2 Signing a document using a USB token (part 2: PKCS#11) We could easily reuse the source from code sample 50 and use it to sign a document on Windows with our iKey 4000. The only difference would be the configuration file. I can pick any name; I now need a DLL instead of an ELF; and then we face the tricky part: which slot do I use? Lets create the configuration file dynamically as is done in code sample 51. Code sample 51: creating a configuration file dynamically
String config = "name=ikey4000\n" + "library=c:/windows/system32/dkck201.dll\n" +
23
http://www.safenet-inc.com/products/data-protection/hardware-security-modules/luna-sa/
Ive chosen the name ikey4000 (but you can use another name if that pleases you more). Ive added the path to the CRYPTOKI (PKCS#11) DLL on my Windows machine, and then I call a method I named getSlotsWithTokens(). You can find the code for this method in code sample 52. Code sample 52: getting the slot used for the iKey 4000
public static long[] getSlotsWithTokens() throws IOException{ String libraryPath = "c:/windows/system32/dkck201.dll"; CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); String functionList = "C_GetFunctionList"; initArgs.flags = 0; PKCS11 tmpPKCS11 = null; long[] slotList = null; try { try { tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, false); } catch (IOException ex) { ex.printStackTrace(); throw ex; } } catch (PKCS11Exception e) { try { initArgs = null; tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, true); } catch (IOException ex) { ex.printStackTrace(); } catch (PKCS11Exception ex) { ex.printStackTrace(); } } try { slotList = tmpPKCS11.C_GetSlotList(true); for (long slot : slotList){ CK_TOKEN_INFO tokenInfo = tmpPKCS11.C_GetTokenInfo(slot); System.out.println("slot: "+slot+"\nmanufacturerID: " + String.valueOf(tokenInfo.manufacturerID) + "\nmodel: " + String.valueOf(tokenInfo.model)); } } catch (PKCS11Exception ex) { ex.printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } return slotList; }
In my case, Im using only one USB token, and the method writes the following info to the console:
slot: 2 manufacturerID: SafeNet, Inc. model: Model 400
TODO
Appendix
TODO
B. Electronic signatures and digital signatures: are they the same thing?
TODO
If you want to be kept up to date regarding updates to this document, please register for our newsletter at http://itextpdf.com/newsletter The completed document should be ready by the end of September. Please send your comments, corrections and suggestions to the iText mailing-list: http://itextpdf.com/contact.php