KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Sign


1 // $Id: Sign.java,v 1.4 2004/05/16 21:49:52 mike Exp $
2

3 import java.util.*;
4 import java.io.*;
5 import org.faceless.pdf2.*;
6 import java.security.KeyStore JavaDoc;
7 import java.security.GeneralSecurityException JavaDoc;
8
9 /**
10  * An example showing how to batch sign a PDF document read
11  * from standard-in and written to standard-out.
12  */

13 public class Sign
14 {
15     private static String JavaDoc pdfpass, keystorename, reason, location, file;
16     private static char[] storepass, keypass;
17
18     private static String JavaDoc keystoretype="JKS", keystoreprovider=null, keyalias="mykey";
19     private static SignatureHandlerFactory handler = FormSignature.HANDLER_SELFSIGN;
20
21     public static void main(String JavaDoc[] args)
22         throws IOException, GeneralSecurityException JavaDoc
23     {
24     // 1. Parse the arguments
25
//
26
getargs(args);
27     if (keystorename==null) usage("No keystore specified");
28     if (keypass==null && storepass!=null) keypass=storepass;
29
30     // 2. Load the keystore. If a provider was specified, use it.
31
//
32
KeyStore JavaDoc keystore;
33     if (keystoreprovider==null) {
34         keystore = KeyStore.getInstance(keystoretype);
35     } else {
36         keystore = KeyStore.getInstance(keystoretype, keystoreprovider);
37     }
38     keystore.load(new FileInputStream(keystorename), storepass);
39
40     // 3. Create the new signature
41
//
42
FormSignature sig = new FormSignature(keystore, keyalias, keypass, handler);
43     sig.setReason(reason);
44     sig.setLocation(location);
45     PKCS7SignatureHandler pkcs7 = (PKCS7SignatureHandler)sig.getSignatureHandler();
46     sig.setName(FormSignature.getSubjectField(pkcs7.getCertificates()[0], "CN"));
47
48
49     // 4. Add a visible appearance for this signature to the first page
50
//
51

52     // 4. Load the PDF from the specified file, apply the signature, give it
53
// a visible appearance on the first page and write the PDF to "Sign.pdf"
54
//
55
InputStream in = new FileInputStream(file);
56     PDF pdf = new PDF(new PDFReader(in, pdfpass));
57     in.close();
58     sig.addAnnotation(pdf.getPage(0), 100, 100, 300, 200);
59     pdf.getForm().addElement("Test Signature", sig);
60     OutputStream out = new FileOutputStream("Sign.pdf");
61     pdf.render(out);
62     out.close();
63     }
64
65     private static void getargs(String JavaDoc[] args)
66     {
67         for (int i=0;i<args.length;i++) {
68         if (args[i].startsWith("--pdfpassword=")) {
69             pdfpass = args[i].substring(14);
70         } else if (args[i].startsWith("--keypassword=")) {
71             keypass = args[i].substring(14).toCharArray();
72         } else if (args[i].startsWith("--storepassword=")) {
73             storepass = args[i].substring(16).toCharArray();
74         } else if (args[i].startsWith("--reason=")) {
75             reason = args[i].substring(9);
76         } else if (args[i].startsWith("--location=")) {
77             location = args[i].substring(11);
78         } else if (args[i].startsWith("--keyalias=")) {
79             keyalias = args[i].substring(11);
80         } else if (args[i].startsWith("--keystore=")) {
81             keystorename = args[i].substring(11);
82         } else if (args[i].startsWith("--keystoretype=")) {
83             keystoretype = args[i].substring(15);
84         } else if (args[i].startsWith("--keystoreprovider=")) {
85             keystoreprovider = args[i].substring(19);
86         } else if (args[i].equals("--handler=verisign")) {
87             handler = FormSignature.HANDLER_VERISIGN;
88         } else if (args[i].equals("--handler=selfsign")) {
89             handler = FormSignature.HANDLER_SELFSIGN;
90         } else if (args[i].equals("--handler=acrobat6")) {
91             handler = FormSignature.HANDLER_ACROBATSIX;
92         } else if (file==null) {
93             file = args[i];
94         } else {
95             usage("Unknown argument \""+args[i]+"\"");
96         }
97     }
98     if (file==null) usage("No filename specified");
99     }
100
101     private static void usage(String JavaDoc errorstring)
102     {
103     System.err.println("ERROR: "+errorstring);
104     System.err.println("Usage: java Sign --keystore=<keystore file name>");
105     System.err.println(" --storepassword=<keystore password>");
106     System.err.println(" [--keyalias=<private key alias>]");
107     System.err.println(" [--keypassword=<private key password>]");
108     System.err.println(" [--pdfpassword=<PDF decryption password>]");
109     System.err.println(" [--keystoretype=<KeyStore type>]");
110     System.err.println(" [--keystoreprovider=<KeyStore provider>]");
111     System.err.println(" [--handler=verisign|selfsign]");
112     System.err.println(" [--location=<location comment>]");
113     System.err.println(" [--reason=<reason comment>] Input.pdf");
114     System.err.println();
115     System.err.println("--keystore The filename of the KeyStore containing the private");
116     System.err.println(" key to sign the PDF document with");
117     System.err.println("--storepassword The password required to open the keystore.");
118     System.err.println("--keyalias (optional) The name of the private key in the keystore.");
119     System.err.println(" Defaults to \"mykey\"");
120     System.err.println("--keypassword (optional) The password used to access the private key.");
121     System.err.println(" defaults to the value of the \"storepassword\" option");
122     System.err.println("--pdfpassword (optional) The password required to decrypt the incoming");
123     System.err.println(" PDF, if required");
124     System.err.println("--handler (optional) The type of signature handler to use. Valid");
125     System.err.println(" values are \"selfsign\", to use the Adobe Self-Sign handler");
126     System.err.println(" or \"verisign\" to use the Verisign Document Signer.");
127     System.err.println(" The default is \"selfsign\"");
128     System.err.println("--keystoretype (optional) The type of KeyStore. Defaults to \"JKS\", for");
129     System.err.println(" \"Java KeyStore\". Other types like \"pkcs12\" may be");
130     System.err.println(" used provided an appropriate JCE provider is available");
131     System.err.println("--keystoreprovider (optional) The name of the provider supplying the KeyStore");
132     System.err.println(" implementation");
133     System.err.println("--reason (optional) The reason the document is being signed");
134     System.err.println("--location (optional) The location the document is being signed at");
135     System.err.println();
136     System.err.println("Signs a PDF document read from standard-in, and writes it to \"Sign.pdf\".");
137     System.err.println("To create a self-signed JKS keystore called \"testkeystore\" suitable for use");
138     System.err.println("with the Adobe self-sign handler, run the following command:");
139     System.err.println();
140     System.err.println(" keytool -genkey -keyalg RSA -sigalg MD5withRSA -keystore testkeystore");
141     System.err.println();
142     System.err.println("Be sure to specify a 2 letter code for the country, otherwise Acrobat will");
143     System.err.println("be unable to validate the signature.");
144     System.err.println();
145     System.exit(0);
146     }
147 }
148
Popular Tags