001: /**
002: * Copyright (c) 2003-2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox;
031:
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.security.KeyStore;
035:
036: import org.pdfbox.pdmodel.PDDocument;
037: import org.pdfbox.pdmodel.encryption.AccessPermission;
038: import org.pdfbox.pdmodel.encryption.DecryptionMaterial;
039: import org.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial;
040: import org.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
041:
042: /**
043: * This will read a document from the filesystem, decrypt it and and then write
044: * the results to the filesystem. <br/><br/>
045: *
046: * usage: java org.pdfbox.Decrypt <password> <inputfile> <outputfile>
047: *
048: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
049: * @version $Revision: 1.5 $
050: */
051: public class Decrypt {
052: private static final String ALIAS = "-alias";
053: private static final String PASSWORD = "-password";
054: private static final String KEYSTORE = "-keyStore";
055:
056: /**
057: * This is the entry point for the application.
058: *
059: * @param args The command-line arguments.
060: *
061: * @throws Exception If there is an error decrypting the document.
062: */
063: public static void main(String[] args) throws Exception {
064: Decrypt decrypt = new Decrypt();
065: decrypt.decrypt(args);
066: }
067:
068: private void decrypt(String[] args) throws Exception {
069: if (args.length < 2 || args.length > 3) {
070: usage();
071: } else {
072: String password = null;
073: String infile = null;
074: String outfile = null;
075: String alias = null;
076: String keyStore = null;
077: for (int i = 0; i < args.length; i++) {
078: if (args[i].equals(ALIAS)) {
079: i++;
080: if (i >= args.length) {
081: usage();
082: }
083: alias = args[i];
084: } else if (args[i].equals(KEYSTORE)) {
085: i++;
086: if (i >= args.length) {
087: usage();
088: }
089: keyStore = args[i];
090: } else if (args[i].equals(PASSWORD)) {
091: i++;
092: if (i >= args.length) {
093: usage();
094: }
095: password = args[i];
096: } else if (infile == null) {
097: infile = args[i];
098: } else if (outfile == null) {
099: outfile = args[i];
100: } else {
101: usage();
102: }
103: }
104: if (infile == null) {
105: usage();
106: }
107: if (outfile == null) {
108: outfile = infile;
109: }
110: if (password == null) {
111: password = "";
112: }
113:
114: PDDocument document = null;
115:
116: try {
117: document = PDDocument.load(infile);
118:
119: if (document.isEncrypted()) {
120: DecryptionMaterial decryptionMaterial = null;
121: if (keyStore != null) {
122: KeyStore ks = KeyStore.getInstance("PKCS12");
123: ks.load(new FileInputStream(keyStore), password
124: .toCharArray());
125:
126: decryptionMaterial = new PublicKeyDecryptionMaterial(
127: ks, alias, password);
128: } else {
129: decryptionMaterial = new StandardDecryptionMaterial(
130: password);
131: }
132: document.openProtection(decryptionMaterial);
133: AccessPermission ap = document
134: .getCurrentAccessPermission();
135: if (ap.isOwnerPermission()) {
136: document.save(outfile);
137: } else {
138: throw new IOException(
139: "Error: You are only allowed to decrypt a document with the owner password.");
140: }
141: } else {
142: System.err
143: .println("Error: Document is not encrypted.");
144: }
145: } finally {
146: if (document != null) {
147: document.close();
148: }
149: }
150: }
151: }
152:
153: /**
154: * This will print a usage message.
155: */
156: private static void usage() {
157: System.err.println("usage: java org.pdfbox.Decrypt "
158: + "[options] <inputfile> [outputfile]");
159: System.err
160: .println("-alias The alias of the key in the certificate file "
161: + "(mandatory if several keys are available)");
162: System.err
163: .println("-password The password to open the certificate and extract the private key from it.");
164: System.err
165: .println("-keyStore The KeyStore that holds the certificate.");
166: System.exit(-1);
167: }
168:
169: }
|