\r
import java.lang.reflect.Field;\r
import java.lang.reflect.Method;\r
+import java.security.AccessController;\r
import java.security.GeneralSecurityException;\r
import java.security.MessageDigest;\r
+import java.security.PrivilegedAction;\r
import java.security.Provider;\r
import java.security.Security;\r
import java.util.List;\r
import org.apache.poi.poifs.crypt.dsig.SignatureConfig.SignatureConfigurable;\r
import org.apache.poi.util.POILogFactory;\r
import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.SuppressForbidden;\r
import org.w3c.dom.Document;\r
\r
/**\r
}\r
\r
// helper method ... will be removed soon\r
- public static void brokenJvmWorkaround(Reference reference) {\r
- DigestMethod digestMethod = reference.getDigestMethod();\r
- String digestMethodUri = digestMethod.getAlgorithm();\r
+ public static void brokenJvmWorkaround(final Reference reference) {\r
+ final DigestMethod digestMethod = reference.getDigestMethod();\r
+ final String digestMethodUri = digestMethod.getAlgorithm();\r
\r
- // workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1155012\r
- // overwrite standard message digest, if a digest <> SHA1 is used\r
- Provider bcProv = Security.getProvider("BC");\r
+ final Provider bcProv = Security.getProvider("BC");\r
if (bcProv != null && !DigestMethod.SHA1.equals(digestMethodUri)) {\r
- try {\r
- Method m = DOMDigestMethod.class.getDeclaredMethod("getMessageDigestAlgorithm");\r
- m.setAccessible(true);\r
- String mdAlgo = (String)m.invoke(digestMethod);\r
- MessageDigest md = MessageDigest.getInstance(mdAlgo, bcProv);\r
- Field f = DOMReference.class.getDeclaredField("md");\r
- f.setAccessible(true);\r
- f.set(reference, md);\r
- } catch (Exception e) {\r
- LOG.log(POILogger.WARN, "Can't overwrite message digest (workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1155012)", e);\r
- }\r
+ // workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1155012\r
+ // overwrite standard message digest, if a digest <> SHA1 is used\r
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {\r
+ @Override\r
+ @SuppressForbidden("Workaround for a bug, needs access to private JDK members (may fail in Java 9): https://bugzilla.redhat.com/show_bug.cgi?id=1155012")\r
+ public Void run() {\r
+ try {\r
+ Method m = DOMDigestMethod.class.getDeclaredMethod("getMessageDigestAlgorithm");\r
+ m.setAccessible(true);\r
+ String mdAlgo = (String)m.invoke(digestMethod);\r
+ MessageDigest md = MessageDigest.getInstance(mdAlgo, bcProv);\r
+ Field f = DOMReference.class.getDeclaredField("md");\r
+ f.setAccessible(true);\r
+ f.set(reference, md);\r
+ } catch (Exception e) {\r
+ LOG.log(POILogger.WARN, "Can't overwrite message digest (workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1155012)", e);\r
+ }\r
+ return null; // Void\r
+ }\r
+ });\r
}\r
}\r
}
\ No newline at end of file