1 31 package org.pdfbox.examples.signature; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.IOException ; 35 36 import java.security.cert.CertificateFactory ; 37 38 import java.util.Collection ; 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 56 public class ShowSignature 57 { 58 59 66 public static void main( String [] args ) throws Exception 67 { 68 ShowSignature show = new ShowSignature(); 69 show.showSignature( args ); 70 } 71 72 private void showSignature( String [] args ) throws Exception 73 { 74 if( args.length != 2 ) 75 { 76 usage(); 77 } 78 else 79 { 80 String password = args[0]; 81 String 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 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 factory = CertificateFactory.getInstance( "X.509" ); 121 ByteArrayInputStream certStream = new ByteArrayInputStream ( certData ); 122 Collection 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 factory = CertificateFactory.getInstance( "X.509" ); 131 ByteArrayInputStream certStream = new ByteArrayInputStream ( certData ); 132 Collection 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 ( "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 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 |