\r
try {\r
// Ensure the JCE policies files allow for this sized key\r
- if (Cipher.getMaxAllowedKeyLength(key.getAlgorithm()) < keySizeInBytes*8) {\r
+ if (Cipher.getMaxAllowedKeyLength(cipherAlgorithm.jceId) < keySizeInBytes*8) {\r
throw new EncryptedDocumentException("Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files");\r
}\r
\r
Cipher cipher;\r
- if (cipherAlgorithm.needsBouncyCastle) {\r
+ if (cipherAlgorithm == CipherAlgorithm.rc4) {\r
+ cipher = Cipher.getInstance(cipherAlgorithm.jceId);\r
+ } else if (cipherAlgorithm.needsBouncyCastle) {\r
registerBouncyCastle();\r
- cipher = Cipher.getInstance(key.getAlgorithm() + "/" + chain.jceId + "/" + padding, "BC");\r
+ cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding, "BC");\r
} else {\r
- cipher = Cipher.getInstance(key.getAlgorithm() + "/" + chain.jceId + "/" + padding);\r
+ cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding);\r
}\r
\r
if (vec == null) {\r
}\r
}\r
\r
-\r
private static final int InitialCodeArray[] = { \r
0xE1F0, 0x1D0F, 0xCC9C, 0x84C0, 0x110C, 0x0E10, 0xF1CE, \r
0x313E, 0x1872, 0xE139, 0xD40F, 0x84F9, 0x280C, 0xA96A, \r
==================================================================== */\r
package org.apache.poi.poifs.crypt.agile;\r
\r
-import java.io.IOException;\r
-\r
import org.apache.poi.EncryptedDocumentException;\r
import org.apache.poi.poifs.crypt.ChainingMode;\r
import org.apache.poi.poifs.crypt.CipherAlgorithm;\r
import org.apache.poi.poifs.crypt.EncryptionHeader;\r
import org.apache.poi.poifs.crypt.HashAlgorithm;\r
-import org.apache.xmlbeans.XmlException;\r
\r
import com.microsoft.schemas.office.x2006.encryption.CTDataIntegrity;\r
import com.microsoft.schemas.office.x2006.encryption.CTKeyData;\r
public class AgileEncryptionHeader extends EncryptionHeader {\r
private byte encryptedHmacKey[], encryptedHmacValue[];\r
\r
- public AgileEncryptionHeader(String descriptor) throws IOException {\r
- EncryptionDocument ed;\r
- try {\r
- ed = EncryptionDocument.Factory.parse(descriptor);\r
- } catch (XmlException e) {\r
- throw new EncryptedDocumentException("Unable to parse encryption descriptor", e);\r
- }\r
- \r
+ public AgileEncryptionHeader(String descriptor) {\r
+ this(AgileEncryptionInfoBuilder.parseDescriptor(descriptor));\r
+ }\r
+ \r
+ protected AgileEncryptionHeader(EncryptionDocument ed) {\r
CTKeyData keyData;\r
try {\r
keyData = ed.getEncryption().getKeyData();\r
package org.apache.poi.poifs.crypt.agile;\r
\r
import java.io.IOException;\r
+import java.io.InputStream;\r
\r
import org.apache.poi.EncryptedDocumentException;\r
import org.apache.poi.poifs.crypt.ChainingMode;\r
import org.apache.poi.poifs.crypt.CipherAlgorithm;\r
import org.apache.poi.poifs.crypt.EncryptionInfo;\r
import org.apache.poi.poifs.crypt.EncryptionInfoBuilder;\r
+import org.apache.poi.poifs.crypt.EncryptionMode;\r
import org.apache.poi.poifs.crypt.HashAlgorithm;\r
import org.apache.poi.poifs.filesystem.DocumentInputStream;\r
+import org.apache.xmlbeans.XmlException;\r
+\r
+import com.microsoft.schemas.office.x2006.encryption.EncryptionDocument;\r
\r
public class AgileEncryptionInfoBuilder implements EncryptionInfoBuilder {\r
\r
public void initialize(EncryptionInfo info, DocumentInputStream dis) throws IOException {\r
this.info = info;\r
\r
- StringBuilder builder = new StringBuilder();\r
- byte[] xmlDescriptor = new byte[dis.available()];\r
- dis.read(xmlDescriptor);\r
- for (byte b : xmlDescriptor)\r
- builder.append((char)b);\r
- String descriptor = builder.toString();\r
- header = new AgileEncryptionHeader(descriptor);\r
- verifier = new AgileEncryptionVerifier(descriptor);\r
- if (info.getVersionMajor() == 4 && info.getVersionMinor() == 4) {\r
+ EncryptionDocument ed = parseDescriptor(dis);\r
+ header = new AgileEncryptionHeader(ed);\r
+ verifier = new AgileEncryptionVerifier(ed);\r
+ if (info.getVersionMajor() == EncryptionMode.agile.versionMajor\r
+ && info.getVersionMinor() == EncryptionMode.agile.versionMinor) {\r
decryptor = new AgileDecryptor(this);\r
}\r
}\r
return info;\r
}\r
\r
- \r
+ protected static EncryptionDocument parseDescriptor(String descriptor) {\r
+ try {\r
+ return EncryptionDocument.Factory.parse(descriptor);\r
+ } catch (XmlException e) {\r
+ throw new EncryptedDocumentException("Unable to parse encryption descriptor", e);\r
+ }\r
+ }\r
+\r
+ protected static EncryptionDocument parseDescriptor(InputStream descriptor) {\r
+ try {\r
+ return EncryptionDocument.Factory.parse(descriptor);\r
+ } catch (Exception e) {\r
+ throw new EncryptedDocumentException("Unable to parse encryption descriptor", e);\r
+ }\r
+ }\r
}\r
import org.apache.poi.poifs.crypt.CipherAlgorithm;
import org.apache.poi.poifs.crypt.EncryptionVerifier;
import org.apache.poi.poifs.crypt.HashAlgorithm;
-import org.apache.xmlbeans.XmlException;
import com.microsoft.schemas.office.x2006.encryption.CTKeyEncryptor;
import com.microsoft.schemas.office.x2006.encryption.EncryptionDocument;
private List<AgileCertificateEntry> certList = new ArrayList<AgileCertificateEntry>();
-
public AgileEncryptionVerifier(String descriptor) {
- EncryptionDocument ed;
- try {
- ed = EncryptionDocument.Factory.parse(descriptor);
- } catch (XmlException e) {
- throw new EncryptedDocumentException("Unable to parse encryption descriptor", e);
- }
-
+ this(AgileEncryptionInfoBuilder.parseDescriptor(descriptor));
+ }
+
+ protected AgileEncryptionVerifier(EncryptionDocument ed) {
Iterator<CTKeyEncryptor> encList = ed.getEncryption().getKeyEncryptors().getKeyEncryptorList().iterator();
CTPasswordKeyEncryptor keyData;
try {
}\r
\r
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {\r
+ final CTKeyEncryptor.Uri.Enum passwordUri = \r
+ CTKeyEncryptor.Uri.HTTP_SCHEMAS_MICROSOFT_COM_OFFICE_2006_KEY_ENCRYPTOR_PASSWORD;\r
+ final CTKeyEncryptor.Uri.Enum certificateUri = \r
+ CTKeyEncryptor.Uri.HTTP_SCHEMAS_MICROSOFT_COM_OFFICE_2006_KEY_ENCRYPTOR_CERTIFICATE;\r
+ \r
AgileEncryptionVerifier ver = builder.getVerifier();\r
AgileEncryptionHeader header = builder.getHeader();\r
\r
CTKeyData keyData = edRoot.addNewKeyData();\r
CTKeyEncryptors keyEncList = edRoot.addNewKeyEncryptors();\r
CTKeyEncryptor keyEnc = keyEncList.addNewKeyEncryptor();\r
- keyEnc.setUri(CTKeyEncryptor.Uri.HTTP_SCHEMAS_MICROSOFT_COM_OFFICE_2006_KEY_ENCRYPTOR_PASSWORD);\r
+ keyEnc.setUri(passwordUri);\r
CTPasswordKeyEncryptor keyPass = keyEnc.addNewEncryptedPasswordKey();\r
\r
keyPass.setSpinCount(ver.getSpinCount());\r
\r
for (AgileCertificateEntry ace : ver.getCertificates()) {\r
keyEnc = keyEncList.addNewKeyEncryptor();\r
- keyEnc.setUri(CTKeyEncryptor.Uri.HTTP_SCHEMAS_MICROSOFT_COM_OFFICE_2006_KEY_ENCRYPTOR_CERTIFICATE);\r
+ keyEnc.setUri(certificateUri);\r
CTCertificateKeyEncryptor certData = keyEnc.addNewEncryptedCertificateKey();\r
try {\r
certData.setX509Certificate(ace.x509.getEncoded());\r
certData.setEncryptedKeyValue(ace.encryptedKey);\r
certData.setCertVerifier(ace.certVerifier);\r
}\r
-\r
+ \r
XmlOptions xo = new XmlOptions();\r
xo.setCharacterEncoding("UTF-8");\r
Map<String,String> nsMap = new HashMap<String,String>();\r
- nsMap.put("http://schemas.microsoft.com/office/2006/keyEncryptor/password","p");\r
- nsMap.put("http://schemas.microsoft.com/office/2006/keyEncryptor/certificate", "c");\r
- nsMap.put("http://schemas.microsoft.com/office/2006/encryption","");\r
+ nsMap.put(passwordUri.toString(),"p");\r
+ nsMap.put(certificateUri.toString(), "c");\r
+ xo.setUseDefaultNamespace();\r
xo.setSaveSuggestedPrefixes(nsMap);\r
xo.setSaveNamespacesFirst();\r
xo.setSaveAggressiveNamespaces();\r
leos.writeShort(info.getVersionMajor());\r
leos.writeShort(info.getVersionMinor());\r
// Reserved (4 bytes): A value that MUST be 0x00000040\r
- leos.writeInt(0x40);\r
+ leos.writeInt(info.getEncryptionFlags());\r
leos.write(bos.toByteArray());\r
\r
dir.createDocument("EncryptionInfo", leos.getWriteIndex(), new POIFSWriterListener() {\r
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
import org.apache.poi.hslf.blip.BitmapPainter;
import org.apache.poi.hslf.blip.DIB;
import org.apache.poi.hslf.blip.PICT;
import org.apache.poi.hslf.blip.PNG;
import org.apache.poi.hslf.blip.WMF;
-import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.model.Picture;
+import org.apache.poi.poifs.crypt.CryptoFunctions;
+import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
* Compute 16-byte checksum of this picture using MD5 algorithm.
*/
public static byte[] getChecksum(byte[] data) {
- MessageDigest sha;
- try {
- sha = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e){
- throw new HSLFException(e.getMessage());
- }
- sha.update(data);
- return sha.digest();
+ MessageDigest md5 = CryptoFunctions.getMessageDigest(HashAlgorithm.md5);
+ md5.update(data);
+ return md5.digest();
}
/**
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
+import org.apache.poi.poifs.crypt.CryptoFunctions;
+import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
* Helps identify the source file
*/
private static String getFileMD5(File f) {
- MessageDigest m;
- try {
- m = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
+ MessageDigest m = CryptoFunctions.getMessageDigest(HashAlgorithm.md5);
byte[]buf = new byte[2048];
try {