You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFEncryptionManager.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import java.security.Provider;
  22. import java.security.Security;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. /**
  26. * This class acts as a factory for PDF encryption support. It enables the
  27. * feature to be optional to FOP depending on the availability of JCE.
  28. */
  29. public final class PDFEncryptionManager {
  30. private PDFEncryptionManager() {
  31. }
  32. /** logging instance */
  33. private static final Log LOG = LogFactory.getLog(PDFEncryptionManager.class);
  34. /**
  35. * Indicates whether JCE is available.
  36. * @return boolean true if JCE is present
  37. */
  38. public static boolean isJCEAvailable() {
  39. try {
  40. /*Class clazz =*/ Class.forName("javax.crypto.Cipher");
  41. return true;
  42. } catch (ClassNotFoundException e) {
  43. return false;
  44. }
  45. }
  46. /**
  47. * Checks whether the necessary algorithms are available.
  48. * @return boolean True if all necessary algorithms are present
  49. */
  50. public static boolean checkAvailableAlgorithms() {
  51. if (!isJCEAvailable()) {
  52. return false;
  53. } else {
  54. Provider[] providers;
  55. providers = Security.getProviders("Cipher.RC4");
  56. if (providers == null) {
  57. LOG.warn("Cipher provider for RC4 not available.");
  58. return false;
  59. }
  60. providers = Security.getProviders("MessageDigest.MD5");
  61. if (providers == null) {
  62. LOG.warn("MessageDigest provider for MD5 not available.");
  63. return false;
  64. }
  65. return true;
  66. }
  67. }
  68. /**
  69. * Sets up PDF encryption if PDF encryption is requested by registering
  70. * a <code>PDFEncryptionParams</code> object with the user agent and if
  71. * the necessary cryptographic support is available.
  72. * @param params the PDF encryption params or null to disable encryption
  73. * @param pdf the PDF document to setup encryption for
  74. */
  75. public static void setupPDFEncryption(PDFEncryptionParams params,
  76. PDFDocument pdf) {
  77. if (pdf == null) {
  78. throw new NullPointerException("PDF document must not be null");
  79. }
  80. if (params != null) {
  81. if (!checkAvailableAlgorithms()) {
  82. if (isJCEAvailable()) {
  83. LOG.warn("PDF encryption has been requested, JCE is "
  84. + "available but there's no "
  85. + "JCE provider available that provides the "
  86. + "necessary algorithms. The PDF won't be "
  87. + "encrypted.");
  88. } else {
  89. LOG.warn("PDF encryption has been requested but JCE is "
  90. + "unavailable! The PDF won't be encrypted.");
  91. }
  92. }
  93. pdf.setEncryption(params);
  94. }
  95. }
  96. /**
  97. * Creates a new PDFEncryption instance if PDF encryption is available.
  98. * @param objnum PDF object number
  99. * @param params PDF encryption parameters
  100. * @param pdf the PDF document to encrypt
  101. * @return PDFEncryption the newly created instance, null if PDF encryption
  102. * is unavailable.
  103. */
  104. public static PDFEncryption newInstance(int objnum, PDFEncryptionParams params,
  105. PDFDocument pdf) {
  106. try {
  107. Class<?> clazz = Class.forName("org.apache.fop.pdf.PDFEncryptionJCE");
  108. Method makeMethod = clazz.getMethod("make",
  109. new Class[] {int.class, PDFEncryptionParams.class, PDFDocument.class});
  110. Object obj = makeMethod.invoke(null,
  111. new Object[] {new Integer(objnum), params, pdf});
  112. return (PDFEncryption)obj;
  113. } catch (ClassNotFoundException e) {
  114. if (checkAvailableAlgorithms()) {
  115. LOG.warn("JCE and algorithms available, but the "
  116. + "implementation class unavailable. Please do a full "
  117. + "rebuild.");
  118. }
  119. return null;
  120. } catch (NoSuchMethodException e) {
  121. LOG.error(e);
  122. return null;
  123. } catch (IllegalAccessException e) {
  124. LOG.error(e);
  125. return null;
  126. } catch (InvocationTargetException e) {
  127. LOG.error(e);
  128. return null;
  129. }
  130. }
  131. }