<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Berthou.com &#187; pdf</title>
	<atom:link href="http://www.berthou.com/us/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.berthou.com/us</link>
	<description>A french developper blog</description>
	<lastBuildDate>Mon, 09 Aug 2010 08:55:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Verify PDF signature with iText</title>
		<link>http://www.berthou.com/us/2009/07/01/verify-pdf-signature-with-itext/</link>
		<comments>http://www.berthou.com/us/2009/07/01/verify-pdf-signature-with-itext/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 17:56:39 +0000</pubDate>
		<dc:creator>rberthou</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[certificat]]></category>
		<category><![CDATA[pdf]]></category>

		<guid isPermaLink="false">http://www.berthou.com/us/?p=54</guid>
		<description><![CDATA[Description After the article &#8220;How to sign a PDF in Java&#8221; I had various demands concerning the verification of this signature. You will find in this article an exemple of code which permit to check this. Tools used http://itextpdf.sourceforge.net/howtosign.html#howtoverify : Origins of the informations and of the code used below Acrobat reader 8.x ou sup [...]]]></description>
				<content:encoded><![CDATA[<h2 class="titre2">Description</h2>
<p><img src="/_img/pdf_sign.png" class="left"/><br />
After the article &#8220;How to sign a PDF in Java&#8221; I had various demands concerning the verification of this signature.<br />
You will find in this article an exemple of code which permit to check this.</p>
<h2 class="titre2">Tools used</h2>
<ul>
<li><a href="http://itextpdf.sourceforge.net/howtosign.html" target="_new">http://itextpdf.sourceforge.net/howtosign.html#howtoverify</a> : Origins of the informations and of the code used below</li>
<li><a href="http://www.adobe.com/fr/products/acrobat/" target="_blank">Acrobat reader 8.x ou sup</a> To read the PDF documents</li>
<li><a href="http://www.lowagie.com/iText/" target="_blank">iText 2.x</a>Java librarie tools (the reference according to me) to create, sign and handle PDF documents</li>
<li><a href="http://www.bouncycastle.org/fr/index.html" target="_blank">bouncycastle.org</a> used in itext (bcprov)</li>
</ul>
<p><span id="more-54"></span></p>
<h2 class="titre2">Exemple (source code)</h2>
<p>As you can see the java code to do this is very simple :</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.berthou.test.pdf</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.cert.Certificate</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Calendar</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.lowagie.text.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.lowagie.text.pdf.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> test_pdf  <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Name of the PDF document
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> dirname  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;D:<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span> <span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// &quot;D:\\&quot;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> fname  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HelloWorld_sign&quot;</span> <span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			test_pdf.<span style="color: #006633;">verifyPdf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">boolean</span> verifyPdf<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, DocumentException, <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">KeyStore</span> kall <span style="color: #339933;">=</span> PdfPKCS7.<span style="color: #006633;">loadCacertsKeyStore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		PdfReader reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PdfReader<span style="color: #009900;">&#40;</span>dirname <span style="color: #339933;">+</span> fname <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		AcroFields af <span style="color: #339933;">=</span> reader.<span style="color: #006633;">getAcroFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Search of the whole signature</span>
		<span style="color: #003399;">ArrayList</span> names <span style="color: #339933;">=</span> af.<span style="color: #006633;">getSignatureNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// For every signature :</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> k <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> names.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		   <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>names.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  		   <span style="color: #666666; font-style: italic;">// Affichage du nom</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Signature name: &quot;</span> <span style="color: #339933;">+</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Signature covers whole document: &quot;</span>
                                <span style="color: #339933;">+</span> af.<span style="color: #006633;">signatureCoversWholeDocument</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  		   <span style="color: #666666; font-style: italic;">// Affichage sur les revision - version</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Document revision: &quot;</span> <span style="color: #339933;">+</span> af.<span style="color: #006633;">getRevision</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; of &quot;</span>
                                <span style="color: #339933;">+</span> af.<span style="color: #006633;">getTotalRevisions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #666666; font-style: italic;">// Debut de l'extraction de la &quot;revision&quot;</span>
		   <span style="color: #003399;">FileOutputStream</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;d:<span style="color: #000099; font-weight: bold;">\\</span>revision_&quot;</span>
                                <span style="color: #339933;">+</span> af.<span style="color: #006633;">getRevision</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #000066; font-weight: bold;">byte</span> bb<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8192</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		   <span style="color: #003399;">InputStream</span> ip <span style="color: #339933;">=</span> af.<span style="color: #006633;">extractRevision</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #000066; font-weight: bold;">int</span> n <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		   <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">=</span> ip.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>bb<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>bb, <span style="color: #cc66cc;">0</span>, n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   out.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   ip.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #666666; font-style: italic;">// Fin extraction revision</span>
&nbsp;
		   PdfPKCS7 pk <span style="color: #339933;">=</span> af.<span style="color: #006633;">verifySignature</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> pk.<span style="color: #006633;">getSignDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #003399;">Certificate</span> pkc<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pk.<span style="color: #006633;">getCertificates</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #666666; font-style: italic;">// Information about the certificat, le signataire</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Subject: &quot;</span>
                                <span style="color: #339933;">+</span> PdfPKCS7.<span style="color: #006633;">getSubjectFields</span><span style="color: #009900;">&#40;</span>pk.<span style="color: #006633;">getSigningCertificate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #666666; font-style: italic;">// Le document à t'il ete modifié ?</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Document modified: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #339933;">!</span>pk.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		   <span style="color: #666666; font-style: italic;">// Is the certificate avaible ? Be carefull we search the chain of certificat</span>
		   <span style="color: #003399;">Object</span> fails<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> PdfPKCS7.<span style="color: #006633;">verifyCertificates</span><span style="color: #009900;">&#40;</span>pkc, kall, <span style="color: #000066; font-weight: bold;">null</span>, cal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fails <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		       <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Certificates verified against the KeyStore&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #000000; font-weight: bold;">else</span>
		       <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Certificate failed: &quot;</span> <span style="color: #339933;">+</span> fails<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2 class="titre2">Sample PDF generate</h2>
<p class="alertDownload">
<a href="/download.php?type=wp&#038;file=HelloWorld_sign.pdf" target="_blank">HelloWorld_sign.pdf</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.berthou.com/us/2009/07/01/verify-pdf-signature-with-itext/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
