Author: lehmi Date: Sat Jan 17 15:08:34 2015 New Revision: 1652623 URL: http://svn.apache.org/r1652623 Log: PDFBOX-2600: removed no longer needed decryption methods as proposed by Tilman Hausherr
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1652623&r1=1652622&r2=1652623&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Sat Jan 17 15:08:34 2015 @@ -43,14 +43,10 @@ import org.apache.pdfbox.pdmodel.common. import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.common.PDStream; import org.apache.pdfbox.pdmodel.encryption.AccessPermission; -import org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial; import org.apache.pdfbox.pdmodel.encryption.PDEncryption; import org.apache.pdfbox.pdmodel.encryption.ProtectionPolicy; import org.apache.pdfbox.pdmodel.encryption.SecurityHandler; import org.apache.pdfbox.pdmodel.encryption.SecurityHandlerFactory; -import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial; -import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; -import org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream; @@ -608,17 +604,6 @@ public class PDDocument implements Close } /** - * @deprecated Use {@link #getEncryption()} instead. - * - * @return The encryption dictionary(most likely a PDStandardEncryption object) - */ - @Deprecated - public PDEncryption getEncryptionDictionary() - { - return getEncryption(); - } - - /** * This will get the encryption dictionary for this document. This will still return the parameters if the document * was decrypted. As the encryption architecture in PDF documents is plugable this returns an abstract class, * but the only supported subclass at this time is a @@ -706,74 +691,6 @@ public class PDDocument implements Close } /** - * This will decrypt a document. - * - * @deprecated This method is provided for compatibility reasons only. User should use the new - * security layer instead and the openProtection method especially. - * - * @param password Either the user or owner password. - * - * @throws IOException If there is an error getting the stream data. - */ - @Deprecated - public void decrypt(String password) throws IOException - { - StandardDecryptionMaterial m = new StandardDecryptionMaterial(password); - openProtection(m); - } - - /** - * This will <b>mark</b> a document to be encrypted. The actual encryption will occur when the document is saved. - * - * @deprecated This method is provided for compatibility reasons only. User should use the new security layer - * instead and the openProtection method especially. - * - * @param ownerPassword The owner password to encrypt the document. - * @param userPassword The user password to encrypt the document. - - * @throws IOException If there is an error accessing the data. - */ - @Deprecated - public void encrypt(String ownerPassword, String userPassword) throws IOException - { - if (!isEncrypted()) - { - encryption = new PDEncryption(); - } - - getEncryption().setSecurityHandler(new StandardSecurityHandler( - new StandardProtectionPolicy(ownerPassword, userPassword, new AccessPermission()))); - } - - /** - * The owner password that was passed into the encrypt method. You should never use this method. This will not - * longer be valid once encryption has occured. - * - * @return The owner password passed to the encrypt method. - * - * @deprecated Do not rely on this method anymore. - */ - @Deprecated - public String getOwnerPasswordForEncryption() - { - return null; - } - - /** - * The user password that was passed into the encrypt method. You should never use this method. This will not longer - * be valid once encryption has occured. - * - * @return The user password passed to the encrypt method. - * - * @deprecated Do not rely on this method anymore. - */ - @Deprecated - public String getUserPasswordForEncryption() - { - return null; - } - - /** * Parses PDF with non sequential parser. * * @param file file to be loaded @@ -1112,33 +1029,6 @@ public class PDDocument implements Close } /** - * Tries to decrypt the document in memory using the provided decryption material. - * - * @see org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial - * @see org.apache.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial - * - * @param decryptionMaterial The decryption material (password or certificate). - * - * @throws IOException If there is an error reading cryptographic information. - */ - public void openProtection(DecryptionMaterial decryptionMaterial) throws IOException - { - if (isEncrypted()) - { - SecurityHandler securityHandler = getEncryption().getSecurityHandler(); - securityHandler.decryptDocument(this, decryptionMaterial); - accessPermission = securityHandler.getCurrentAccessPermission(); - document.dereferenceObjectStreams(); - document.setEncryptionDictionary(null); - getDocumentCatalog(); - } - else - { - throw new IOException("Document is not encrypted"); - } - } - - /** * Returns the access permissions granted when the document was decrypted. If the document was not decrypted this * method returns the access permission for a document owner (ie can do everything). The returned object is in read * only mode so that permissions cannot be changed. Methods providing access to content should rely on this object @@ -1156,53 +1046,6 @@ public class PDDocument implements Close } /** - * Get the security handler that is used for document encryption. - * - * @deprecated Use {@link #getEncryption()}. - * {@link org.apache.pdfbox.pdmodel.encryption.PDEncryption#getSecurityHandler()} - * - * @return The handler used to encrypt/decrypt the document. - */ - @Deprecated - public SecurityHandler getSecurityHandler() - { - if (isEncrypted() && getEncryption().hasSecurityHandler()) - { - try - { - return getEncryption().getSecurityHandler(); - } - catch (IOException e) - { - // will never happen because we checked hasSecurityHandler() first - throw new RuntimeException(e); - } - } - else - { - return null; - } - } - - /** - * @deprecated Use protection policies instead. - * - * @param securityHandler security handler to be assigned to document - * @return true if security handler was set - */ - @Deprecated - public boolean setSecurityHandler(SecurityHandler securityHandler) - { - if (isEncrypted()) - { - return false; - } - encryption = new PDEncryption(); - getEncryption().setSecurityHandler(securityHandler); - return true; - } - - /** * Indicates if all security is removed or not when writing the pdf. * * @return returns true if all security shall be removed otherwise false Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java?rev=1652623&r1=1652622&r2=1652623&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java Sat Jan 17 15:08:34 2015 @@ -104,29 +104,8 @@ public final class PublicKeySecurityHand } /** - * Decrypt the document. - * - * @param doc The document to decrypt. - * @param decryptionMaterial The data used to decrypt the document. - * - * @throws IOException If there is an error accessing data. - */ - @Override - public void decryptDocument(PDDocument doc, DecryptionMaterial decryptionMaterial) throws IOException - { - this.document = doc; - PDEncryption dictionary = doc.getEncryption(); - prepareForDecryption( dictionary, doc.getDocument().getDocumentID(), decryptionMaterial ); - proceedDecryption(); - } - - /** * Prepares everything to decrypt the document. * - * If {@link #decryptDocument(PDDocument, DecryptionMaterial)} is used, this - * method is called from there. Only if decryption of single objects is - * needed this should be called instead. - * * @param encryption encryption dictionary, can be retrieved via * {@link PDDocument#getEncryption()} * @param documentIDArray document id which is returned via Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java?rev=1652623&r1=1652622&r2=1652623&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java Sat Jan 17 15:08:34 2015 @@ -29,8 +29,6 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.HashSet; -import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; @@ -46,9 +44,7 @@ import javax.crypto.spec.SecretKeySpec; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSDictionary; -import org.apache.pdfbox.cos.COSDocument; import org.apache.pdfbox.cos.COSName; -import org.apache.pdfbox.cos.COSObject; import org.apache.pdfbox.cos.COSStream; import org.apache.pdfbox.cos.COSString; import org.apache.pdfbox.io.IOUtils; @@ -84,7 +80,7 @@ public abstract class SecurityHandler /** The RC4 implementation used for cryptographic functions. */ protected RC4Cipher rc4 = new RC4Cipher(); - /** indicates if the Metadata have to be decrypted of not */ + /** indicates if the Metadata have to be decrypted of not. */ protected boolean decryptMetadata; private final Set<COSBase> objects = new HashSet<COSBase>(); @@ -110,9 +106,6 @@ public abstract class SecurityHandler /** * Prepares everything to decrypt the document. * - * If {@link #decryptDocument(PDDocument, DecryptionMaterial)} is used, this method is - * called from there. Only if decryption of single objects is needed this should be called instead. - * * @param encryption encryption dictionary, can be retrieved via {@link PDDocument#getEncryption()} * @param documentIDArray document id which is returned via {@link COSDocument#getDocumentID()} * @param decryptionMaterial Information used to decrypt the document. @@ -123,83 +116,6 @@ public abstract class SecurityHandler DecryptionMaterial decryptionMaterial) throws IOException; /** - * Prepare the document for decryption. - * - * @param doc The document to decrypt. - * @param mat Information required to decrypt the document. - * @throws IOException If there is an error with the document. - */ - public abstract void decryptDocument(PDDocument doc, DecryptionMaterial mat) throws IOException; - - /** - * This method must be called by an implementation of this class to really proceed - * to decryption. - * - * @throws IOException If there is an error in the decryption. - */ - protected void proceedDecryption() throws IOException - { - - COSDictionary trailer = document.getDocument().getTrailer(); - COSArray fields = (COSArray) trailer.getObjectFromPath("Root/AcroForm/Fields"); - - // We need to collect all the signature dictionaries, for some - // reason the 'Contents' entry of signatures is not really encrypted - if (fields != null) - { - for (int i = 0; i < fields.size(); i++) - { - COSDictionary field = (COSDictionary) fields.getObject(i); - if (field != null) - { - addDictionaryAndSubDictionary(potentialSignatures, field); - } - else - { - throw new IOException("Could not decypt document, object not found."); - } - } - } - - List<COSObject> allObjects = document.getDocument().getObjects(); - Iterator<COSObject> objectIter = allObjects.iterator(); - COSDictionary encryptionDict = document.getEncryption().getCOSDictionary(); - while (objectIter.hasNext()) - { - COSObject nextObj = objectIter.next(); - COSBase nextCOSBase = nextObj.getObject(); - boolean isSignatureDictionary = false; - if (nextCOSBase instanceof COSDictionary) - { - isSignatureDictionary = COSName.SIG.equals(((COSDictionary) nextCOSBase).getCOSName(COSName.TYPE)); - } - if (!isSignatureDictionary && nextCOSBase!= encryptionDict) - { - decryptObject(nextObj); - } - } - document.setEncryptionDictionary(null); - } - - private void addDictionaryAndSubDictionary(Set<COSDictionary> set, COSDictionary dic) - { - if (dic != null) // in case dictionary is part of object stream we have null value here - { - set.add(dic); - COSArray kids = (COSArray) dic.getDictionaryObject(COSName.KIDS); - for (int i = 0; kids != null && i < kids.size(); i++) - { - addDictionaryAndSubDictionary(set, (COSDictionary) kids.getObject(i)); - } - COSBase value = dic.getDictionaryObject(COSName.V); - if (value instanceof COSDictionary) - { - addDictionaryAndSubDictionary(set, (COSDictionary) value); - } - } - } - - /** * Encrypt or decrypt a set of data. * * @param objectNumber The data object number. @@ -210,7 +126,7 @@ public abstract class SecurityHandler * * @throws IOException If there is an error reading the data. */ - public void encryptData(long objectNumber, long genNumber, InputStream data, + private void encryptData(long objectNumber, long genNumber, InputStream data, OutputStream output, boolean decrypt) throws IOException { // Determine whether we're using Algorithm 1 (for RC4 and AES-128), or 1.A (for AES-256) @@ -394,21 +310,6 @@ public abstract class SecurityHandler } /** - * This will decrypt an object in the document. - * - * @param object The object to decrypt. - * - * @throws IOException If there is an error getting the stream data. - */ - private void decryptObject(COSObject object) throws IOException - { - long objNum = object.getObjectNumber().intValue(); - long genNum = object.getGenerationNumber().intValue(); - COSBase base = object.getObject(); - decrypt(base, objNum, genNum); - } - - /** * This will dispatch to the correct method. * * @param obj The object to decrypt. Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java?rev=1652623&r1=1652622&r2=1652623&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java Sat Jan 17 15:08:34 2015 @@ -132,7 +132,8 @@ public final class StandardSecurityHandl } if (version == 5) { - return 6; // note about revision 5: "Shall not be used. This value was used by a deprecated Adobe extension." + // note about revision 5: "Shall not be used. This value was used by a deprecated Adobe extension." + return 6; } if ( version == 2 || version == 3 || policy.getPermissions().hasAnyRevision3PermissionSet()) { @@ -142,28 +143,6 @@ public final class StandardSecurityHandl } /** - * Decrypt the document. - * - * @param doc The document to be decrypted. - * @param decryptionMaterial Information used to decrypt the document. - * - * @throws IOException If there is an error accessing data. - */ - @Override - public void decryptDocument(PDDocument doc, DecryptionMaterial decryptionMaterial) - throws IOException - { - document = doc; - - PDEncryption dictionary = document.getEncryption(); - COSArray documentIDArray = document.getDocument().getDocumentID(); - - prepareForDecryption(dictionary, documentIDArray, decryptionMaterial); - - proceedDecryption(); - } - - /** * Prepares everything to decrypt the document. * * Called from {@link #decryptDocument(PDDocument, DecryptionMaterial)}. @@ -464,8 +443,7 @@ public final class StandardSecurityHandl { COSArray idArray = document.getDocument().getDocumentID(); - //check if the document has an id yet. If it does not then - //generate one + //check if the document has an id yet. If it does not then generate one if( idArray == null || idArray.size() < 2 ) { MessageDigest md = MessageDigests.getMD5();