java.lang.Object
org.sejda.sambox.pdmodel.encryption.SecurityHandler
- Direct Known Subclasses:
PublicKeySecurityHandler,StandardSecurityHandler
A security handler as described in the PDF specifications. A security handler is responsible of
documents protection.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byte[]private AccessPermissionThe access permission granted to the current user for the document.private booleanindicates if the Metadata have to be decrypted of not.private static final shortprivate byte[]The encryption key that will used to encrypt / decrypt.protected shortThe length of the secret key used to encrypt the document.private static final org.slf4j.Loggerprivate final RC4CipherThe RC4 implementation used for cryptographic functions.private COSNameThe stream filter name.private COSNameThe string filter name.private boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate byte[]calcFinalKey(long objectNumber, long genNumber) Calculate the key to be used for RC4 and AES-128.voidThis will dispatch to the correct method.private voiddecryptArray(COSArray array, long objNum, long genNum) This will decrypt an array.private voiddecryptData(long objectNumber, long genNumber, InputStream data, OutputStream output) Encrypt or decrypt a set of data.private voiddecryptDataAES256(InputStream data, OutputStream output) Encrypt or decrypt data with AES256.private voiddecryptDataAESother(byte[] finalKey, InputStream data, OutputStream output) Encrypt or decrypt data with AES with key length other than 256 bits.protected voiddecryptDataRC4(byte[] finalKey, byte[] input, OutputStream output) Encrypt or decrypt data with RC4.protected voiddecryptDataRC4(byte[] finalKey, InputStream input, OutputStream output) Encrypt or decrypt data with RC4.private voiddecryptDictionary(COSDictionary dictionary, long objNum, long genNum) This will decrypt a dictionary.voiddecryptStream(COSStream stream, long objNum, long genNum) This will decrypt a stream.private voiddecryptString(COSString string, long objNum, long genNum) This will decrypt a string.Returns the access permissions that were computed during document decryption.byte[]intbooleanisAES()True if AES is used for encryption and decryption.booleanprivate voidlogErrorOnce(String message, long key) abstract voidprepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial) Prepares everything to decrypt the document.voidsetAES(boolean aesValue) Set to true if AES for encryption and decryption should be used.voidsetCurrentAccessPermission(AccessPermission currentAccessPermission) protected voidsetDecryptMetadata(boolean decryptMetadata) Set wether to decrypt meta data.protected voidsetEncryptionKey(byte[] encryptionKey) voidsetKeyLength(int keyLen) protected voidsetStreamFilterName(COSName streamFilterName) Set the stream filter name.protected voidsetStringFilterName(COSName stringFilterName) Set the string filter name.
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
DEFAULT_KEY_LENGTH
private static final short DEFAULT_KEY_LENGTH- See Also:
-
AES_SALT
private static final byte[] AES_SALT -
keyLength
protected short keyLengthThe length of the secret key used to encrypt the document. -
encryptionKey
private byte[] encryptionKeyThe encryption key that will used to encrypt / decrypt. -
rc4
The RC4 implementation used for cryptographic functions. -
decryptMetadata
private boolean decryptMetadataindicates if the Metadata have to be decrypted of not. -
objects
-
useAES
private boolean useAES -
currentAccessPermission
The access permission granted to the current user for the document. These permissions are computed during decryption and are in read only mode. -
streamFilterName
The stream filter name. -
stringFilterName
The string filter name. -
logOnceCache
-
-
Constructor Details
-
SecurityHandler
public SecurityHandler()
-
-
Method Details
-
setDecryptMetadata
protected void setDecryptMetadata(boolean decryptMetadata) Set wether to decrypt meta data.- Parameters:
decryptMetadata- true if meta data has to be decrypted.
-
isDecryptMetadata
public boolean isDecryptMetadata()- Returns:
- True if meta data has to be decrypted.
-
setStringFilterName
Set the string filter name.- Parameters:
stringFilterName- the string filter name.
-
setStreamFilterName
Set the stream filter name.- Parameters:
streamFilterName- the stream filter name.
-
prepareForDecryption
public abstract void prepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial) throws IOException Prepares everything to decrypt the document.- Parameters:
encryption- encryption dictionary, can be retrieved viaPDDocument.getEncryption()documentIDArray- document id which is returned viaCOSDocument.getDocumentID()decryptionMaterial- Information used to decrypt the document.- Throws:
IOException- If there is an error accessing data.
-
decryptData
private void decryptData(long objectNumber, long genNumber, InputStream data, OutputStream output) throws IOException Encrypt or decrypt a set of data.- Parameters:
objectNumber- The data object number.genNumber- The data generation number.data- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
calcFinalKey
private byte[] calcFinalKey(long objectNumber, long genNumber) Calculate the key to be used for RC4 and AES-128.- Parameters:
objectNumber- The data object number.genNumber- The data generation number.- Returns:
- the calculated key.
-
decryptDataRC4
protected void decryptDataRC4(byte[] finalKey, InputStream input, OutputStream output) throws IOException Encrypt or decrypt data with RC4.- Parameters:
finalKey- The final key obtained with viacalcFinalKey(long, long).input- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
decryptDataRC4
protected void decryptDataRC4(byte[] finalKey, byte[] input, OutputStream output) throws IOException Encrypt or decrypt data with RC4.- Parameters:
finalKey- The final key obtained with viacalcFinalKey(long, long).input- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
decryptDataAESother
private void decryptDataAESother(byte[] finalKey, InputStream data, OutputStream output) throws IOException Encrypt or decrypt data with AES with key length other than 256 bits.- Parameters:
finalKey- The final key obtained with via .data- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
decryptDataAES256
Encrypt or decrypt data with AES256.- Parameters:
data- The data to encrypt.output- The output to write the encrypted data to.- Throws:
IOException- If there is an error reading the data.
-
decrypt
This will dispatch to the correct method.- Parameters:
obj- The object to decrypt.objNum- The object number.genNum- The object generation Number.- Throws:
IOException- If there is an error getting the stream data.
-
decryptStream
This will decrypt a stream.- Parameters:
stream- The stream to decrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If there is an error getting the stream data.
-
decryptDictionary
private void decryptDictionary(COSDictionary dictionary, long objNum, long genNum) throws IOException This will decrypt a dictionary.- Parameters:
dictionary- The dictionary to decrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If there is an error creating a new string.
-
decryptString
This will decrypt a string.- Parameters:
string- the string to decrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If an error occurs writing the new string.
-
logErrorOnce
-
decryptArray
This will decrypt an array.- Parameters:
array- The array to decrypt.objNum- The object number.genNum- The object generation number.- Throws:
IOException- If there is an error accessing the data.
-
getKeyLength
public int getKeyLength()- Returns:
- key length in bits
-
setKeyLength
public void setKeyLength(int keyLen) -
setCurrentAccessPermission
- Parameters:
currentAccessPermission- The access permissions to be set.
-
getCurrentAccessPermission
Returns the access permissions that were computed during document decryption. The returned object is in read only mode.- Returns:
- the access permissions or null if the document was not decrypted.
-
isAES
public boolean isAES()True if AES is used for encryption and decryption.- Returns:
- true if AEs is used
-
setAES
public void setAES(boolean aesValue) Set to true if AES for encryption and decryption should be used.- Parameters:
aesValue- if true AES will be used
-
getEncryptionKey
public byte[] getEncryptionKey() -
setEncryptionKey
protected void setEncryptionKey(byte[] encryptionKey)
-