KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > signature > ShowSignature


1 /**
2  * Copyright (c) 2003, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.signature;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import java.security.cert.CertificateFactory JavaDoc;
37
38 import java.util.Collection JavaDoc;
39
40 import org.pdfbox.cos.COSArray;
41 import org.pdfbox.cos.COSDictionary;
42 import org.pdfbox.cos.COSName;
43 import org.pdfbox.cos.COSString;
44
45 import org.pdfbox.pdmodel.PDDocument;
46
47 /**
48  * This will read a document from the filesystem, decrypt it and do something with the signature.
49  *
50  * usage: java org.pdfbox.examples.signature.ShowSignature <password> <inputfile>
51  *
52  *
53  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
54  * @version $Revision: 1.9 $
55  */

56 public class ShowSignature
57 {
58
59     /**
60      * This is the entry point for the application.
61      *
62      * @param args The command-line arguments.
63      *
64      * @throws Exception If there is an error reading the file.
65      */

66     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
67     {
68         ShowSignature show = new ShowSignature();
69         show.showSignature( args );
70     }
71
72     private void showSignature( String JavaDoc[] args ) throws Exception JavaDoc
73     {
74         if( args.length != 2 )
75         {
76             usage();
77         }
78         else
79         {
80             String JavaDoc password = args[0];
81             String JavaDoc infile = args[1];
82             PDDocument document = null;
83             try
84             {
85                 document = PDDocument.load( infile );
86
87                 if( document.isEncrypted() )
88                 {
89                     document.decrypt( password );
90                 }
91                 else
92                 {
93                     System.err.println( "Warning: Document is not encrypted." );
94                 }
95
96                 COSDictionary trailer = document.getDocument().getTrailer();
97                 COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
98                 COSDictionary acroForm = (COSDictionary)root.getDictionaryObject( COSName.getPDFName( "AcroForm" ) );
99                 COSArray fields = (COSArray)acroForm.getDictionaryObject( COSName.getPDFName( "Fields" ) );
100                 for( int i=0; i<fields.size(); i++ )
101                 {
102                     COSDictionary field = (COSDictionary)fields.getObject( i );
103                     String JavaDoc type = field.getNameAsString( "FT" );
104                     if( "Sig".equals( type ) )
105                     {
106                         COSDictionary cert = (COSDictionary)field.getDictionaryObject( COSName.getPDFName( "V" ) );
107                         if( cert != null )
108                         {
109                             System.out.println( "Certificate found" );
110                             System.out.println( "Name=" + cert.getDictionaryObject( COSName.getPDFName( "Name" ) ) );
111                             System.out.println( "Modified=" + cert.getDictionaryObject( COSName.getPDFName( "M" ) ) );
112                             COSName subFilter = (COSName)cert.getDictionaryObject( COSName.getPDFName( "SubFilter" ) );
113                             if( subFilter != null )
114                             {
115                                 if( subFilter.getName().equals( "adbe.x509.rsa_sha1" ) )
116                                 {
117                                     COSString certString = (COSString)cert.getDictionaryObject(
118                                         COSName.getPDFName( "Cert" ) );
119                                     byte[] certData = certString.getBytes();
120                                     CertificateFactory JavaDoc factory = CertificateFactory.getInstance( "X.509" );
121                                     ByteArrayInputStream JavaDoc certStream = new ByteArrayInputStream JavaDoc( certData );
122                                     Collection JavaDoc certs = factory.generateCertificates( certStream );
123                                     System.out.println( "certs=" + certs );
124                                 }
125                                 else if( subFilter.getName().equals( "adbe.pkcs7.sha1" ) )
126                                 {
127                                     COSString certString = (COSString)cert.getDictionaryObject(
128                                         COSName.getPDFName( "Contents" ) );
129                                     byte[] certData = certString.getBytes();
130                                     CertificateFactory JavaDoc factory = CertificateFactory.getInstance( "X.509" );
131                                     ByteArrayInputStream JavaDoc certStream = new ByteArrayInputStream JavaDoc( certData );
132                                     Collection JavaDoc certs = factory.generateCertificates( certStream );
133                                     System.out.println( "certs=" + certs );
134                                 }
135                                 else
136                                 {
137                                     System.err.println( "Unknown certificate type:" + subFilter );
138                                 }
139                             }
140                             else
141                             {
142                                 throw new IOException JavaDoc( "Missing subfilter for cert dictionary" );
143                             }
144                         }
145                         else
146                         {
147                             System.out.println( "Signature found, but no certificate" );
148                         }
149                     }
150                 }
151             }
152             finally
153             {
154                 if( document != null )
155                 {
156                     document.close();
157                 }
158             }
159         }
160     }
161
162     /**
163      * This will print a usage message.
164      */

165     private static void usage()
166     {
167         System.err.println( "usage: java org.pdfbox.examples.signature.ShowSignature " +
168                             "<password> <inputfile>" );
169     }
170
171 }
Popular Tags