1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.examples.signature; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.IOException; 21 22 import java.security.cert.CertificateFactory; 23 24 import java.util.Collection; 25 26 import org.apache.pdfbox.cos.COSArray; 27 import org.apache.pdfbox.cos.COSDictionary; 28 import org.apache.pdfbox.cos.COSName; 29 import org.apache.pdfbox.cos.COSString; 30 31 import org.apache.pdfbox.pdmodel.PDDocument; 32 33 /** 34 * This will read a document from the filesystem, decrypt it and do something with the signature. 35 * 36 * usage: java org.apache.pdfbox.examples.signature.ShowSignature <password> <inputfile> 37 * 38 * 39 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 40 * @version $Revision: 1.9 $ 41 */ 42 public class ShowSignature 43 { 44 45 private ShowSignature() 46 { 47 } 48 /** 49 * This is the entry point for the application. 50 * 51 * @param args The command-line arguments. 52 * 53 * @throws Exception If there is an error reading the file. 54 */ 55 public static void main( String[] args ) throws Exception 56 { 57 ShowSignature show = new ShowSignature(); 58 show.showSignature( args ); 59 } 60 61 private void showSignature( String[] args ) throws Exception 62 { 63 if( args.length != 2 ) 64 { 65 usage(); 66 } 67 else 68 { 69 String password = args[0]; 70 String infile = args[1]; 71 PDDocument document = null; 72 try 73 { 74 document = PDDocument.load( infile ); 75 76 if( document.isEncrypted() ) 77 { 78 document.decrypt( password ); 79 } 80 else 81 { 82 System.err.println( "Warning: Document is not encrypted." ); 83 } 84 85 COSDictionary trailer = document.getDocument().getTrailer(); 86 COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT ); 87 COSDictionary acroForm = (COSDictionary)root.getDictionaryObject( COSName.ACRO_FORM ); 88 COSArray fields = (COSArray)acroForm.getDictionaryObject( COSName.FIELDS ); 89 for( int i=0; i<fields.size(); i++ ) 90 { 91 COSDictionary field = (COSDictionary)fields.getObject( i ); 92 String type = field.getNameAsString( "FT" ); 93 if( "Sig".equals( type ) ) 94 { 95 COSDictionary cert = (COSDictionary)field.getDictionaryObject( COSName.V ); 96 if( cert != null ) 97 { 98 System.out.println( "Certificate found" ); 99 System.out.println( "Name=" + cert.getDictionaryObject( COSName.NAME ) ); 100 System.out.println( "Modified=" + cert.getDictionaryObject( COSName.getPDFName( "M" ) ) ); 101 COSName subFilter = (COSName)cert.getDictionaryObject( COSName.getPDFName( "SubFilter" ) ); 102 if( subFilter != null ) 103 { 104 if( subFilter.getName().equals( "adbe.x509.rsa_sha1" ) ) 105 { 106 COSString certString = (COSString)cert.getDictionaryObject( 107 COSName.getPDFName( "Cert" ) ); 108 byte[] certData = certString.getBytes(); 109 CertificateFactory factory = CertificateFactory.getInstance( "X.509" ); 110 ByteArrayInputStream certStream = new ByteArrayInputStream( certData ); 111 Collection certs = factory.generateCertificates( certStream ); 112 System.out.println( "certs=" + certs ); 113 } 114 else if( subFilter.getName().equals( "adbe.pkcs7.sha1" ) ) 115 { 116 COSString certString = (COSString)cert.getDictionaryObject( 117 COSName.CONTENTS ); 118 byte[] certData = certString.getBytes(); 119 CertificateFactory factory = CertificateFactory.getInstance( "X.509" ); 120 ByteArrayInputStream certStream = new ByteArrayInputStream( certData ); 121 Collection certs = factory.generateCertificates( certStream ); 122 System.out.println( "certs=" + certs ); 123 } 124 else 125 { 126 System.err.println( "Unknown certificate type:" + subFilter ); 127 } 128 } 129 else 130 { 131 throw new IOException( "Missing subfilter for cert dictionary" ); 132 } 133 } 134 else 135 { 136 System.out.println( "Signature found, but no certificate" ); 137 } 138 } 139 } 140 } 141 finally 142 { 143 if( document != null ) 144 { 145 document.close(); 146 } 147 } 148 } 149 } 150 151 /** 152 * This will print a usage message. 153 */ 154 private static void usage() 155 { 156 System.err.println( "usage: java org.apache.pdfbox.examples.signature.ShowSignature " + 157 "<password> <inputfile>" ); 158 } 159 160 }