public final class StandardSecurityHandler extends SecurityHandler
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_VERSION |
private static byte[] |
ENCRYPT_PADDING
Standard padding for encryption.
|
static java.lang.String |
FILTER
Type of security handler.
|
private static java.lang.String[] |
HASHES_2B |
private static org.apache.commons.logging.Log |
LOG
Log instance.
|
private StandardProtectionPolicy |
policy |
static java.lang.Class<?> |
PROTECTION_POLICY_CLASS
Protection policy class for this handler.
|
encryptionKey, keyLength
Constructor and Description |
---|
StandardSecurityHandler()
Constructor.
|
StandardSecurityHandler(StandardProtectionPolicy p)
Constructor used for encryption.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
computeEncryptedKey(byte[] password,
byte[] o,
byte[] u,
byte[] oe,
byte[] ue,
int permissions,
byte[] id,
int encRevision,
int keyLengthInBytes,
boolean encryptMetadata,
boolean isOwnerPassword)
Compute the encryption key.
|
private byte[] |
computeEncryptedKeyRev234(byte[] password,
byte[] o,
int permissions,
byte[] id,
boolean encryptMetadata,
int length,
int encRevision) |
private byte[] |
computeEncryptedKeyRev56(byte[] password,
boolean isOwnerPassword,
byte[] o,
byte[] u,
byte[] oe,
byte[] ue,
int encRevision) |
private byte[] |
computeHash2A(byte[] password,
byte[] salt,
byte[] u) |
private static byte[] |
computeHash2B(byte[] input,
byte[] password,
byte[] userKey) |
byte[] |
computeOwnerPassword(byte[] ownerPassword,
byte[] userPassword,
int encRevision,
int length)
Compute the owner entry in the encryption dictionary.
|
private byte[] |
computeRC4key(byte[] ownerPassword,
int encRevision,
int length) |
private int |
computeRevisionNumber(int version)
Computes the revision version of the StandardSecurityHandler to
use regarding the version number and the permissions bits set.
|
private static byte[] |
computeSHA256(byte[] input,
byte[] password,
byte[] userKey) |
byte[] |
computeUserPassword(byte[] password,
byte[] owner,
int permissions,
byte[] id,
int encRevision,
int keyLengthInBytes,
boolean encryptMetadata)
This will compute the user password hash.
|
private int |
computeVersionNumber()
Computes the version number of the StandardSecurityHandler
based on the encryption key length.
|
private static byte[] |
concat(byte[] a,
byte[] b) |
private static byte[] |
concat(byte[] a,
byte[] b,
byte[] c) |
private byte[] |
getDocumentIDBytes(COSArray documentIDArray) |
byte[] |
getUserPassword(byte[] ownerPassword,
byte[] owner,
int encRevision,
int length)
Get the user password based on the owner password.
|
boolean |
hasProtectionPolicy()
Returns whether a protection policy has been set.
|
boolean |
isOwnerPassword(byte[] ownerPassword,
byte[] user,
byte[] owner,
int permissions,
byte[] id,
int encRevision,
int keyLengthInBytes,
boolean encryptMetadata)
Check for owner password.
|
boolean |
isOwnerPassword(java.lang.String password,
byte[] user,
byte[] owner,
int permissions,
byte[] id,
int encRevision,
int keyLengthInBytes,
boolean encryptMetadata)
Check for owner password.
|
boolean |
isUserPassword(byte[] password,
byte[] user,
byte[] owner,
int permissions,
byte[] id,
int encRevision,
int keyLengthInBytes,
boolean encryptMetadata)
Check if a plaintext password is the user password.
|
boolean |
isUserPassword(java.lang.String password,
byte[] user,
byte[] owner,
int permissions,
byte[] id,
int encRevision,
int keyLengthInBytes,
boolean encryptMetadata)
Check if a plaintext password is the user password.
|
private boolean |
isUserPassword234(byte[] password,
byte[] user,
byte[] owner,
int permissions,
byte[] id,
int encRevision,
int length,
boolean encryptMetadata) |
private boolean |
isUserPassword56(byte[] password,
byte[] user,
int encRevision) |
private static void |
logIfStrongEncryptionMissing() |
void |
prepareDocumentForEncryption(PDDocument document)
Prepare document for encryption.
|
private void |
prepareEncryptionDictAES(PDEncryption encryptionDictionary,
COSName aesVName) |
private void |
prepareEncryptionDictRev2345(java.lang.String ownerPassword,
java.lang.String userPassword,
PDEncryption encryptionDictionary,
int permissionInt,
PDDocument document,
int revision,
int length) |
private void |
prepareEncryptionDictRev6(java.lang.String ownerPassword,
java.lang.String userPassword,
PDEncryption encryptionDictionary,
int permissionInt) |
void |
prepareForDecryption(PDEncryption encryption,
COSArray documentIDArray,
DecryptionMaterial decryptionMaterial)
Prepares everything to decrypt the document.
|
private static byte[] |
truncate127(byte[] in) |
private byte[] |
truncateOrPad(byte[] password)
This will take the password and truncate or pad it as necessary.
|
private void |
validatePerms(PDEncryption encryption,
int dicPermissions,
boolean encryptMetadata) |
decrypt, decryptStream, encryptDataRC4, encryptDataRC4, encryptStream, encryptString, getCurrentAccessPermission, getKeyLength, isAES, setAES, setCurrentAccessPermission, setDecryptMetadata, setKeyLength, setStreamFilterName, setStringFilterName
private static final org.apache.commons.logging.Log LOG
public static final java.lang.String FILTER
public static final java.lang.Class<?> PROTECTION_POLICY_CLASS
private static final byte[] ENCRYPT_PADDING
private static final java.lang.String[] HASHES_2B
private static final int DEFAULT_VERSION
private StandardProtectionPolicy policy
public StandardSecurityHandler()
public StandardSecurityHandler(StandardProtectionPolicy p)
p
- The protection policy.private int computeVersionNumber()
private int computeRevisionNumber(int version)
version
- The version number.public void prepareForDecryption(PDEncryption encryption, COSArray documentIDArray, DecryptionMaterial decryptionMaterial) throws java.io.IOException
prepareForDecryption
in class SecurityHandler
encryption
- encryption dictionarydocumentIDArray
- document iddecryptionMaterial
- Information used to decrypt the document.InvalidPasswordException
- If the password is incorrect.java.io.IOException
- If there is an error accessing data.private byte[] getDocumentIDBytes(COSArray documentIDArray)
private void validatePerms(PDEncryption encryption, int dicPermissions, boolean encryptMetadata) throws java.io.IOException
java.io.IOException
public void prepareDocumentForEncryption(PDDocument document) throws java.io.IOException
prepareDocumentForEncryption
in class SecurityHandler
document
- The document to encrypt.java.io.IOException
- If there is an error accessing data.private void prepareEncryptionDictRev6(java.lang.String ownerPassword, java.lang.String userPassword, PDEncryption encryptionDictionary, int permissionInt) throws java.io.IOException
java.io.IOException
private void prepareEncryptionDictRev2345(java.lang.String ownerPassword, java.lang.String userPassword, PDEncryption encryptionDictionary, int permissionInt, PDDocument document, int revision, int length) throws java.io.IOException
java.io.IOException
private void prepareEncryptionDictAES(PDEncryption encryptionDictionary, COSName aesVName)
public boolean isOwnerPassword(byte[] ownerPassword, byte[] user, byte[] owner, int permissions, byte[] id, int encRevision, int keyLengthInBytes, boolean encryptMetadata) throws java.io.IOException
ownerPassword
- The owner password.user
- The u entry of the encryption dictionary.owner
- The o entry of the encryption dictionary.permissions
- The set of permissions on the document.id
- The document id.encRevision
- The encryption algorithm revision.keyLengthInBytes
- The encryption key length in bytes.encryptMetadata
- The encryption metadatajava.io.IOException
- If there is an error accessing data.public byte[] getUserPassword(byte[] ownerPassword, byte[] owner, int encRevision, int length) throws java.io.IOException
ownerPassword
- The plaintext owner password.owner
- The o entry of the encryption dictionary.encRevision
- The encryption revision number.length
- The key length.java.io.IOException
- If there is an error accessing data while generating the user password.public byte[] computeEncryptedKey(byte[] password, byte[] o, byte[] u, byte[] oe, byte[] ue, int permissions, byte[] id, int encRevision, int keyLengthInBytes, boolean encryptMetadata, boolean isOwnerPassword) throws java.io.IOException
password
- The password to compute the encrypted key.o
- The O entry of the encryption dictionary.u
- The U entry of the encryption dictionary.oe
- The OE entry of the encryption dictionary.ue
- The UE entry of the encryption dictionary.permissions
- The permissions for the document.id
- The document id.encRevision
- The revision of the encryption algorithm.keyLengthInBytes
- The length of the encryption key in bytes.encryptMetadata
- The encryption metadataisOwnerPassword
- whether the password given is the owner password (for revision 6)java.io.IOException
- If there is an error with encryption.private byte[] computeEncryptedKeyRev234(byte[] password, byte[] o, int permissions, byte[] id, boolean encryptMetadata, int length, int encRevision)
private byte[] computeEncryptedKeyRev56(byte[] password, boolean isOwnerPassword, byte[] o, byte[] u, byte[] oe, byte[] ue, int encRevision) throws java.io.IOException
java.io.IOException
public byte[] computeUserPassword(byte[] password, byte[] owner, int permissions, byte[] id, int encRevision, int keyLengthInBytes, boolean encryptMetadata) throws java.io.IOException
password
- The plain text password.owner
- The owner password hash.permissions
- The document permissions.id
- The document id.encRevision
- The revision of the encryption.keyLengthInBytes
- The length of the encryption key in bytes.encryptMetadata
- The encryption metadatajava.io.IOException
- if the password could not be computedpublic byte[] computeOwnerPassword(byte[] ownerPassword, byte[] userPassword, int encRevision, int length) throws java.io.IOException
ownerPassword
- The plaintext owner password.userPassword
- The plaintext user password.encRevision
- The revision number of the encryption algorithm.length
- The length of the encryption key.java.io.IOException
- if the owner password could not be computedprivate byte[] computeRC4key(byte[] ownerPassword, int encRevision, int length)
private byte[] truncateOrPad(byte[] password)
password
- The password to pad or truncate.public boolean isUserPassword(byte[] password, byte[] user, byte[] owner, int permissions, byte[] id, int encRevision, int keyLengthInBytes, boolean encryptMetadata) throws java.io.IOException
password
- The plaintext password.user
- The u entry of the encryption dictionary.owner
- The o entry of the encryption dictionary.permissions
- The permissions set in the PDF.id
- The document id used for encryption.encRevision
- The revision of the encryption algorithm.keyLengthInBytes
- The length of the encryption key in bytes.encryptMetadata
- The encryption metadata.java.io.IOException
- If there is an error accessing data.private boolean isUserPassword234(byte[] password, byte[] user, byte[] owner, int permissions, byte[] id, int encRevision, int length, boolean encryptMetadata) throws java.io.IOException
java.io.IOException
private boolean isUserPassword56(byte[] password, byte[] user, int encRevision) throws java.io.IOException
java.io.IOException
public boolean isUserPassword(java.lang.String password, byte[] user, byte[] owner, int permissions, byte[] id, int encRevision, int keyLengthInBytes, boolean encryptMetadata) throws java.io.IOException
password
- The plaintext password.user
- The u entry of the encryption dictionary.owner
- The o entry of the encryption dictionary.permissions
- The permissions set in the PDF.id
- The document id used for encryption.encRevision
- The revision of the encryption algorithm.keyLengthInBytes
- The length of the encryption key in bytes.encryptMetadata
- The encryption metadatajava.io.IOException
- If there is an error accessing data.public boolean isOwnerPassword(java.lang.String password, byte[] user, byte[] owner, int permissions, byte[] id, int encRevision, int keyLengthInBytes, boolean encryptMetadata) throws java.io.IOException
password
- The owner password.user
- The u entry of the encryption dictionary.owner
- The o entry of the encryption dictionary.permissions
- The set of permissions on the document.id
- The document id.encRevision
- The encryption algorithm revision.keyLengthInBytes
- The encryption key length in bytes.encryptMetadata
- The encryption metadatajava.io.IOException
- If there is an error accessing data.private byte[] computeHash2A(byte[] password, byte[] salt, byte[] u) throws java.io.IOException
java.io.IOException
private static byte[] computeHash2B(byte[] input, byte[] password, byte[] userKey) throws java.io.IOException
java.io.IOException
private static byte[] computeSHA256(byte[] input, byte[] password, byte[] userKey) throws java.io.IOException
java.io.IOException
private static byte[] concat(byte[] a, byte[] b)
private static byte[] concat(byte[] a, byte[] b, byte[] c)
private static byte[] truncate127(byte[] in)
private static void logIfStrongEncryptionMissing()
public boolean hasProtectionPolicy()
hasProtectionPolicy
in class SecurityHandler