public abstract class SecurityHandler
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static byte[] |
AES_SALT |
private AccessPermission |
currentAccessPermission
The access permission granted to the current user for the document.
|
private boolean |
decryptMetadata
indicates if the Metadata have to be decrypted of not.
|
private static int |
DEFAULT_KEY_LENGTH |
protected byte[] |
encryptionKey
The encryption key that will used to encrypt / decrypt.
|
protected int |
keyLength
The length in bits of the secret key used to encrypt the document.
|
private static org.apache.commons.logging.Log |
LOG |
private java.util.Set<COSBase> |
objects |
private RC4Cipher |
rc4
The RC4 implementation used for cryptographic functions.
|
private COSName |
streamFilterName
The stream filter name.
|
private COSName |
stringFilterName
The string filter name.
|
private boolean |
useAES |
Constructor and Description |
---|
SecurityHandler() |
Modifier and Type | Method and Description |
---|---|
private byte[] |
calcFinalKey(long objectNumber,
long genNumber)
Calculate the key to be used for RC4 and AES-128.
|
private javax.crypto.Cipher |
createCipher(byte[] key,
byte[] iv,
boolean decrypt) |
void |
decrypt(COSBase obj,
long objNum,
long genNum)
This will dispatch to the correct method.
|
private void |
decryptArray(COSArray array,
long objNum,
long genNum)
This will decrypt an array.
|
private void |
decryptDictionary(COSDictionary dictionary,
long objNum,
long genNum)
This will decrypt a dictionary.
|
void |
decryptStream(COSStream stream,
long objNum,
long genNum)
This will decrypt a stream.
|
private void |
decryptString(COSString string,
long objNum,
long genNum)
This will decrypt a string.
|
private void |
encryptData(long objectNumber,
long genNumber,
java.io.InputStream data,
java.io.OutputStream output,
boolean decrypt)
Encrypt or decrypt a set of data.
|
private void |
encryptDataAES256(java.io.InputStream data,
java.io.OutputStream output,
boolean decrypt)
Encrypt or decrypt data with AES256.
|
private void |
encryptDataAESother(byte[] finalKey,
java.io.InputStream data,
java.io.OutputStream output,
boolean decrypt)
Encrypt or decrypt data with AES with key length other than 256 bits.
|
protected void |
encryptDataRC4(byte[] finalKey,
byte[] input,
java.io.OutputStream output)
Encrypt or decrypt data with RC4.
|
protected void |
encryptDataRC4(byte[] finalKey,
java.io.InputStream input,
java.io.OutputStream output)
Encrypt or decrypt data with RC4.
|
void |
encryptStream(COSStream stream,
long objNum,
int genNum)
This will encrypt a stream, but not the dictionary as the dictionary is
encrypted by visitFromString() in COSWriter and we don't want to encrypt
it twice.
|
void |
encryptString(COSString string,
long objNum,
int genNum)
This will encrypt a string.
|
AccessPermission |
getCurrentAccessPermission()
Returns the access permissions that were computed during document decryption.
|
int |
getKeyLength()
Getter of the property keyLength.
|
abstract boolean |
hasProtectionPolicy()
Returns whether a protection policy has been set.
|
boolean |
isAES()
True if AES is used for encryption and decryption.
|
private boolean |
prepareAESInitializationVector(boolean decrypt,
byte[] iv,
java.io.InputStream data,
java.io.OutputStream output) |
abstract void |
prepareDocumentForEncryption(PDDocument doc)
Prepare the document for encryption.
|
abstract void |
prepareForDecryption(PDEncryption encryption,
COSArray documentIDArray,
DecryptionMaterial decryptionMaterial)
Prepares everything to decrypt the document.
|
void |
setAES(boolean aesValue)
Set to true if AES for encryption and decryption should be used.
|
void |
setCurrentAccessPermission(AccessPermission currentAccessPermission)
Sets the access permissions.
|
protected void |
setDecryptMetadata(boolean decryptMetadata)
Set whether to decrypt meta data.
|
void |
setKeyLength(int keyLen)
Setter of the property keyLength.
|
protected void |
setStreamFilterName(COSName streamFilterName)
Set the stream filter name.
|
protected void |
setStringFilterName(COSName stringFilterName)
Set the string filter name.
|
private static final org.apache.commons.logging.Log LOG
private static final int DEFAULT_KEY_LENGTH
private static final byte[] AES_SALT
protected int keyLength
protected byte[] encryptionKey
private final RC4Cipher rc4
private boolean decryptMetadata
private final java.util.Set<COSBase> objects
private boolean useAES
private AccessPermission currentAccessPermission
private COSName streamFilterName
private COSName stringFilterName
protected void setDecryptMetadata(boolean decryptMetadata)
decryptMetadata
- true if meta data has to be decrypted.protected void setStringFilterName(COSName stringFilterName)
stringFilterName
- the string filter name.protected void setStreamFilterName(COSName streamFilterName)
streamFilterName
- the stream filter name.public abstract void prepareDocumentForEncryption(PDDocument doc) throws java.io.IOException
doc
- The document that will be encrypted.java.io.IOException
- If there is an error with the document.public abstract void prepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial) throws java.io.IOException
encryption
- encryption dictionary, can be retrieved via PDDocument.getEncryption()
documentIDArray
- document id which is returned via COSDocument.getDocumentID()
decryptionMaterial
- Information used to decrypt the document.InvalidPasswordException
- If the password is incorrect.java.io.IOException
- If there is an error accessing data.private void encryptData(long objectNumber, long genNumber, java.io.InputStream data, java.io.OutputStream output, boolean decrypt) throws java.io.IOException
objectNumber
- The data object number.genNumber
- The data generation number.data
- The data to encrypt.output
- The output to write the encrypted data to.decrypt
- true to decrypt the data, false to encrypt it.java.io.IOException
- If there is an error reading the data.private byte[] calcFinalKey(long objectNumber, long genNumber)
objectNumber
- The data object number.genNumber
- The data generation number.protected void encryptDataRC4(byte[] finalKey, java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
finalKey
- The final key obtained with via calcFinalKey(long, long)
.input
- The data to encrypt.output
- The output to write the encrypted data to.java.io.IOException
- If there is an error reading the data.protected void encryptDataRC4(byte[] finalKey, byte[] input, java.io.OutputStream output) throws java.io.IOException
finalKey
- The final key obtained with via calcFinalKey(long, long)
.input
- The data to encrypt.output
- The output to write the encrypted data to.java.io.IOException
- If there is an error reading the data.private void encryptDataAESother(byte[] finalKey, java.io.InputStream data, java.io.OutputStream output, boolean decrypt) throws java.io.IOException
finalKey
- The final key obtained with via calcFinalKey(long, long)
.data
- The data to encrypt.output
- The output to write the encrypted data to.decrypt
- true to decrypt the data, false to encrypt it.java.io.IOException
- If there is an error reading the data.private void encryptDataAES256(java.io.InputStream data, java.io.OutputStream output, boolean decrypt) throws java.io.IOException
data
- The data to encrypt.output
- The output to write the encrypted data to.decrypt
- true to decrypt the data, false to encrypt it.java.io.IOException
- If there is an error reading the data.private javax.crypto.Cipher createCipher(byte[] key, byte[] iv, boolean decrypt) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private boolean prepareAESInitializationVector(boolean decrypt, byte[] iv, java.io.InputStream data, java.io.OutputStream output) throws java.io.IOException
java.io.IOException
public void decrypt(COSBase obj, long objNum, long genNum) throws java.io.IOException
obj
- The object to decrypt.objNum
- The object number.genNum
- The object generation Number.java.io.IOException
- If there is an error getting the stream data.public void decryptStream(COSStream stream, long objNum, long genNum) throws java.io.IOException
stream
- The stream to decrypt.objNum
- The object number.genNum
- The object generation number.java.io.IOException
- If there is an error getting the stream data.public void encryptStream(COSStream stream, long objNum, int genNum) throws java.io.IOException
stream
- The stream to decrypt.objNum
- The object number.genNum
- The object generation number.java.io.IOException
- If there is an error getting the stream data.private void decryptDictionary(COSDictionary dictionary, long objNum, long genNum) throws java.io.IOException
dictionary
- The dictionary to decrypt.objNum
- The object number.genNum
- The object generation number.java.io.IOException
- If there is an error creating a new string.private void decryptString(COSString string, long objNum, long genNum) throws java.io.IOException
string
- the string to decrypt.objNum
- The object number.genNum
- The object generation number.java.io.IOException
- If an error occurs writing the new string.public void encryptString(COSString string, long objNum, int genNum) throws java.io.IOException
string
- the string to encrypt.objNum
- The object number.genNum
- The object generation number.java.io.IOException
- If an error occurs writing the new string.private void decryptArray(COSArray array, long objNum, long genNum) throws java.io.IOException
array
- The array to decrypt.objNum
- The object number.genNum
- The object generation number.java.io.IOException
- If there is an error accessing the data.public int getKeyLength()
public void setKeyLength(int keyLen)
keyLen
- The keyLength to set.public void setCurrentAccessPermission(AccessPermission currentAccessPermission)
currentAccessPermission
- The access permissions to be set.public AccessPermission getCurrentAccessPermission()
public boolean isAES()
public void setAES(boolean aesValue)
aesValue
- if true AES will be usedpublic abstract boolean hasProtectionPolicy()