KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > encryption > PublicKeyDecryptionMaterial


1 /**
2  * Copyright (c) 2003-2005, 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
32 package org.pdfbox.pdmodel.encryption;
33
34 import java.security.Key JavaDoc;
35 import java.security.KeyStore JavaDoc;
36 import java.security.KeyStoreException JavaDoc;
37 import java.security.NoSuchAlgorithmException JavaDoc;
38 import java.security.UnrecoverableKeyException JavaDoc;
39 import java.security.cert.X509Certificate JavaDoc;
40 import java.util.Enumeration JavaDoc;
41
42 /**
43  * This class holds necessary information to decrypt a PDF document
44  * protected by the public key security handler.
45  *
46  * To decrypt such a document, we need:
47  * <ul>
48  * <li>a valid X509 certificate which correspond to one of the recipient of the document</li>
49  * <li>the private key corresponding to this certificate
50  * <li>the password to decrypt the private key if necessary</li>
51  * </ul>
52  *
53  * Objects of this class can be used with the <code>openProtection</code> method of <code>PDDocument</code>.
54  *
55  * The following example shows how to decrypt a document using a PKCS#12 certificate
56  * (typically files with a pfx extension).
57  *
58  * <pre>
59  * PDDocument doc = PDDocument.load(document_path);
60  * KeyStore ks = KeyStore.getInstance("PKCS12");
61  * ks.load(new FileInputStream(certificate_path), password.toCharArray());
62  * PublicKeyDecryptionMaterial dm = new PublicKeyDecryptionMaterial(ks, null, password);
63  * doc.openProtection(dm);
64  * </pre>
65  *
66  * In this code sample certificate_path contains the path to the PKCS#12 certificate.
67  *
68  * @see org.pdfbox.pdmodel.PDDocument#openProtection(DecryptionMaterial)
69  *
70  * @author Benoit Guillon (benoit.guillon@snv.jussieu.fr)
71  * @version $Revision: 1.2 $
72  */

73
74 public class PublicKeyDecryptionMaterial extends DecryptionMaterial
75 {
76     private String JavaDoc password = null;
77     private KeyStore JavaDoc keyStore = null;
78     private String JavaDoc alias = null;
79         
80     /**
81      * Create a new public key decryption material.
82      *
83      * @param keystore The keystore were the private key and the certificate are
84      * @param a The alias of the private key and the certificate.
85      * If the keystore contains only 1 entry, this parameter can be left null.
86      * @param pwd The password to extract the private key from the keystore.
87      */

88     
89     public PublicKeyDecryptionMaterial(KeyStore JavaDoc keystore, String JavaDoc a, String JavaDoc pwd)
90     {
91         keyStore = keystore;
92         alias = a;
93         password = pwd;
94     }
95         
96     
97     /**
98      * Returns the certificate contained in the keystore.
99      *
100      * @return The certificate that will be used to try to open the document.
101      *
102      * @throws KeyStoreException If there is an error accessing the certificate.
103      */

104     
105     public X509Certificate JavaDoc getCertificate() throws KeyStoreException JavaDoc
106     {
107         if(keyStore.size() == 1)
108         {
109             Enumeration JavaDoc aliases = keyStore.aliases();
110             String JavaDoc keyStoreAlias = (String JavaDoc)aliases.nextElement();
111             return (X509Certificate JavaDoc)keyStore.getCertificate(keyStoreAlias);
112         }
113         else
114         {
115             if(keyStore.containsAlias(alias))
116             {
117                 return (X509Certificate JavaDoc)keyStore.getCertificate(alias);
118             }
119             throw new KeyStoreException JavaDoc("the keystore does not contain the given alias");
120         }
121     }
122     
123     /**
124      * Returns the password given by the user and that will be used
125      * to open the private key.
126      *
127      * @return The password.
128      */

129     public String JavaDoc getPassword()
130     {
131         return password;
132     }
133     
134     /**
135      * returns The private key that will be used to open the document protection.
136      * @return The private key.
137      * @throws KeyStoreException If there is an error accessing the key.
138      */

139     public Key JavaDoc getPrivateKey() throws KeyStoreException JavaDoc
140     {
141         try
142         {
143             if(keyStore.size() == 1)
144             {
145                 Enumeration JavaDoc aliases = keyStore.aliases();
146                 String JavaDoc keyStoreAlias = (String JavaDoc)aliases.nextElement();
147                 return keyStore.getKey(keyStoreAlias, password.toCharArray());
148             }
149             else
150             {
151                 if(keyStore.containsAlias(alias))
152                 {
153                     return keyStore.getKey(alias, password.toCharArray());
154                 }
155                 throw new KeyStoreException JavaDoc("the keystore does not contain the given alias");
156             }
157         }
158         catch(UnrecoverableKeyException JavaDoc ex)
159         {
160             throw new KeyStoreException JavaDoc("the private key is not recoverable");
161         }
162         catch(NoSuchAlgorithmException JavaDoc ex)
163         {
164             throw new KeyStoreException JavaDoc("the algorithm necessary to recover the key is not available");
165         }
166     }
167 }
Popular Tags