Package org.apache.pdfbox.exceptions

Examples of org.apache.pdfbox.exceptions.CryptographyException


            }
            digestedKey = md.digest();
        }
        catch( NoSuchAlgorithmException e )
        {
            throw new CryptographyException( e );
        }

        //step 4
        int length = Math.min( newKey.length, 16 );
        byte[] finalKey = new byte[ length ];
View Full Code Here


                idArray.add( idString );
                document.setDocumentID( idArray );
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e );
            }

        }
        COSString id = (COSString)idArray.getObject( 0 );
        encryption = new PDFEncryption();
View Full Code Here

            this.keyLength = encDictionary.getLength();
        }
 
        if(!(decryptionMaterial instanceof PublicKeyDecryptionMaterial))
        {
            throw new CryptographyException(
                "Provided decryption material is not compatible with the document");
        }
 
        PublicKeyDecryptionMaterial material = (PublicKeyDecryptionMaterial)decryptionMaterial;
 
        try
        {
            boolean foundRecipient = false;
 
            // the decrypted content of the enveloped data that match
            // the certificate in the decryption material provided
            byte[] envelopedData = null;
 
            // the bytes of each recipient in the recipients array
            byte[][] recipientFieldsBytes = new byte[encDictionary.getRecipientsLength()][];
 
            int recipientFieldsLength = 0;
 
            for(int i=0; i<encDictionary.getRecipientsLength(); i++)
            {
                COSString recipientFieldString = encDictionary.getRecipientStringAt(i);
                byte[] recipientBytes = recipientFieldString.getBytes();
                CMSEnvelopedData data = new CMSEnvelopedData(recipientBytes);
                Iterator recipCertificatesIt = data.getRecipientInfos().getRecipients().iterator();
                while(recipCertificatesIt.hasNext())
                {
                    RecipientInformation ri =
                        (RecipientInformation)recipCertificatesIt.next();
                    // Impl: if a matching certificate was previously found it is an error,
                    // here we just don't care about it
                    if(ri.getRID().match(material.getCertificate()) && !foundRecipient)
                    {
                        foundRecipient = true;
                        envelopedData = ri.getContent(material.getPrivateKey(), "BC");
                    }
                }
                recipientFieldsBytes[i] = recipientBytes;
                recipientFieldsLength += recipientBytes.length;
            }
            if(!foundRecipient || envelopedData == null)
            {
                throw new CryptographyException("The certificate matches no recipient entry");
            }
            if(envelopedData.length != 24)
            {
                throw new CryptographyException("The enveloped data does not contain 24 bytes");
            }
            // now envelopedData contains:
            // - the 20 bytes seed
            // - the 4 bytes of permission for the current user
 
            byte[] accessBytes = new byte[4];
            System.arraycopy(envelopedData, 20, accessBytes, 0, 4);
 
            currentAccessPermission = new AccessPermission(accessBytes);
            currentAccessPermission.setReadOnly();
 
             // what we will put in the SHA1 = the seed + each byte contained in the recipients array
            byte[] sha1Input = new byte[recipientFieldsLength + 20];
 
            // put the seed in the sha1 input
            System.arraycopy(envelopedData, 0, sha1Input, 0, 20);
 
            // put each bytes of the recipients array in the sha1 input
            int sha1InputOffset = 20;
            for(int i=0; i<recipientFieldsBytes.length; i++)
            {
                System.arraycopy(
                    recipientFieldsBytes[i], 0,
                    sha1Input, sha1InputOffset, recipientFieldsBytes[i].length);
                sha1InputOffset += recipientFieldsBytes[i].length;
            }
 
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] mdResult = md.digest(sha1Input);
 
            // we have the encryption key ...
            encryptionKey = new byte[this.keyLength/8];
            System.arraycopy(mdResult, 0, encryptionKey, 0, this.keyLength/8);
        }
        catch(CMSException e)
        {
            throw new CryptographyException(e);
        }
        catch(KeyStoreException e)
        {
            throw new CryptographyException(e);
        }
        catch(NoSuchProviderException e)
        {
            throw new CryptographyException(e);
        }
        catch(NoSuchAlgorithmException e)
        {
            throw new CryptographyException(e);
        }
    }
View Full Code Here

            doc.getDocument().setEncryptionDictionary(dictionary.encryptionDictionary);

        }
        catch(NoSuchAlgorithmException ex)
        {
            throw new CryptographyException(ex);
        }
        catch(NoSuchProviderException ex)
        {
            throw new CryptographyException(ex);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new CryptographyException(e);
        }

    }
View Full Code Here

        document = doc;

        PDEncryptionDictionary dictionary = document.getEncryptionDictionary();
        if(!(decryptionMaterial instanceof StandardDecryptionMaterial))
        {
            throw new CryptographyException("Provided decryption material is not compatible with the document");
        }

        StandardDecryptionMaterial material = (StandardDecryptionMaterial)decryptionMaterial;

        String password = material.getPassword();
        if(password == null)
        {
            password = "";
        }

        int dicPermissions = dictionary.getPermissions();
        int dicRevision = dictionary.getRevision();
        int dicLength = dictionary.getLength()/8;

        //some documents may have not document id, see
        //test\encryption\encrypted_doc_no_id.pdf
        COSArray documentIDArray = document.getDocument().getDocumentID();
        byte[] documentIDBytes = null;
        if( documentIDArray != null && documentIDArray.size() >= 1 )
        {
            COSString id = (COSString)documentIDArray.getObject( 0 );
            documentIDBytes = id.getBytes();
        }
        else
        {
            documentIDBytes = new byte[0];
        }

        // we need to know whether the meta data was encrypted for password calculation
        boolean encryptMetadata = dictionary.isEncryptMetaData();
       
        byte[] u = dictionary.getUserKey();
        byte[] o = dictionary.getOwnerKey();

        boolean isUserPassword =
            isUserPassword(
                password.getBytes("ISO-8859-1"),
                u,
                o,
                dicPermissions,
                documentIDBytes,
                dicRevision,
                dicLength,
                encryptMetadata);
        boolean isOwnerPassword =
            isOwnerPassword(
                password.getBytes("ISO-8859-1"),
                u,
                o,
                dicPermissions,
                documentIDBytes,
                dicRevision,
                dicLength,
                encryptMetadata);

        if( isUserPassword )
        {
            currentAccessPermission = new AccessPermission( dicPermissions );
            encryptionKey =
                computeEncryptedKey(
                    password.getBytes("ISO-8859-1"),
                    o,
                    dicPermissions,
                    documentIDBytes,
                    dicRevision,
                    dicLength,
                    encryptMetadata );
        }
        else if( isOwnerPassword )
        {
            currentAccessPermission = AccessPermission.getOwnerAccessPermission();
            byte[] computedUserPassword = getUserPassword(password.getBytes("ISO-8859-1"),o,dicRevision,dicLength );
            encryptionKey =
                computeEncryptedKey(
                    computedUserPassword,
                    o,
                    dicPermissions,
                    documentIDBytes,
                    dicRevision,
                    dicLength,
                    encryptMetadata);
        }
        else
        {
            throw new CryptographyException(
                "Error: The supplied password does not match either the owner or user password in the document." );
        }

        // detect whether AES encryption is used. This assumes that the encryption algo is
        // stored in the PDCryptFilterDictionary
View Full Code Here

                idArray.add( idString );
                document.getDocument().setDocumentID( idArray );
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e );
            }
            catch(IOException e )
            {
                throw new CryptographyException( e );
            }
        }

        COSString id = (COSString)idArray.getObject( 0 );
View Full Code Here

                    digest = md.digest();
                }
            }
            if( encRevision == 2 && length != 5 )
            {
                throw new CryptographyException(
                    "Error: Expected length=5 actual=" + length );
            }

            //3.3 STEP 4
            byte[] rc4Key = new byte[ (int)length ];
            System.arraycopy( digest, 0, rc4Key, 0, (int)length );

            //3.7 step 2
            if( encRevision == 2 )
            {
                rc4.setKey( rc4Key );
                rc4.write( o, result );
            }
            else if( encRevision == 3 || encRevision == 4)
            {
                /**
                byte[] iterationKey = new byte[ rc4Key.length ];
                byte[] dataToEncrypt = o;
                for( int i=19; i>=0; i-- )
                {
                    System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
                    for( int j=0; j< iterationKey.length; j++ )
                    {
                        iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                    }
                    rc4.setKey( iterationKey );
                    rc4.write( dataToEncrypt, result );
                    dataToEncrypt = result.toByteArray();
                    result.reset();
                }
                result.write( dataToEncrypt, 0, dataToEncrypt.length );
                */
                byte[] iterationKey = new byte[ rc4Key.length ];


                byte[] otemp = new byte[ o.length ]; //sm
                System.arraycopy( o, 0, otemp, 0, o.length ); //sm
                rc4.write( o, result);//sm

                for( int i=19; i>=0; i-- )
                {
                    System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
                    for( int j=0; j< iterationKey.length; j++ )
                    {
                        iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                    }
                    rc4.setKey( iterationKey );
                    result.reset()//sm
                    rc4.write( otemp, result ); //sm
                    otemp = result.toByteArray(); //sm
                }
            }


            return result.toByteArray();

        }
        catch( NoSuchAlgorithmException e )
        {
            throw new CryptographyException( e );
        }
    }
View Full Code Here

                }

                //step 7
                if( encRevision == 2 && length != 5 )
                {
                    throw new CryptographyException(
                        "Error: length should be 5 when revision is two actual=" + length );
                }
                System.arraycopy( digest, 0, result, 0, length );
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e );
            }
            return result;
        }
View Full Code Here

                    result.reset();
                    result.write( finalResult );
                }
                catch( NoSuchAlgorithmException e )
                {
                    throw new CryptographyException( e );
                }
            }
            return result.toByteArray();
        }
View Full Code Here

                        digest = md.digest();
                    }
                }
                if( encRevision == 2 && length != 5 )
                {
                    throw new CryptographyException(
                        "Error: Expected length=5 actual=" + length );
                }

                //STEP 4
                byte[] rc4Key = new byte[ length ];
                System.arraycopy( digest, 0, rc4Key, 0, length );

                //STEP 5
                byte[] paddedUser = truncateOrPad( userPassword );


                //STEP 6
                rc4.setKey( rc4Key );
                ByteArrayOutputStream crypted = new ByteArrayOutputStream();
                rc4.write( new ByteArrayInputStream( paddedUser ), crypted );


                //STEP 7
                if( encRevision == 3 || encRevision == 4 )
                {
                    byte[] iterationKey = new byte[ rc4Key.length ];
                    for( int i=1; i<20; i++ )
                    {
                        System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
                        for( int j=0; j< iterationKey.length; j++ )
                        {
                            iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                        }
                        rc4.setKey( iterationKey );
                        ByteArrayInputStream input = new ByteArrayInputStream( crypted.toByteArray() );
                        crypted.reset();
                        rc4.write( input, crypted );
                    }
                }

                //STEP 8
                return crypted.toByteArray();
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e.getMessage() );
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.pdfbox.exceptions.CryptographyException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.