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.

EnvelopedSignatureFacet.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. /* ====================================================================
  16. This product contains an ASLv2 licensed version of the OOXML signer
  17. package from the eID Applet project
  18. http://code.google.com/p/eid-applet/source/browse/trunk/README.txt
  19. Copyright (C) 2008-2014 FedICT.
  20. ================================================================= */
  21. package org.apache.poi.poifs.crypt.dsig.facets;
  22. import java.security.InvalidAlgorithmParameterException;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.security.cert.X509Certificate;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import javax.xml.crypto.dsig.CanonicalizationMethod;
  28. import javax.xml.crypto.dsig.DigestMethod;
  29. import javax.xml.crypto.dsig.Reference;
  30. import javax.xml.crypto.dsig.Transform;
  31. import javax.xml.crypto.dsig.XMLObject;
  32. import javax.xml.crypto.dsig.XMLSignatureFactory;
  33. import javax.xml.crypto.dsig.spec.TransformParameterSpec;
  34. import org.apache.poi.poifs.crypt.dsig.SignatureConfig;
  35. import org.w3c.dom.Document;
  36. /**
  37. * Signature Facet implementation to create enveloped signatures.
  38. *
  39. * @author Frank Cornelis
  40. *
  41. */
  42. public class EnvelopedSignatureFacet implements SignatureFacet {
  43. private SignatureConfig signatureConfig;
  44. public void setSignatureConfig(SignatureConfig signatureConfig) {
  45. this.signatureConfig = signatureConfig;
  46. }
  47. @Override
  48. public void postSign(Document document, List<X509Certificate> signingCertificateChain) {
  49. // empty
  50. }
  51. @Override
  52. public void preSign(Document document
  53. , XMLSignatureFactory signatureFactory
  54. , List<Reference> references
  55. , List<XMLObject> objects)
  56. throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
  57. DigestMethod digestMethod = signatureFactory.newDigestMethod
  58. (signatureConfig.getDigestMethodUri(), null);
  59. List<Transform> transforms = new ArrayList<Transform>();
  60. Transform envelopedTransform = signatureFactory.newTransform
  61. (CanonicalizationMethod.ENVELOPED, (TransformParameterSpec) null);
  62. transforms.add(envelopedTransform);
  63. Transform exclusiveTransform = signatureFactory.newTransform
  64. (CanonicalizationMethod.EXCLUSIVE, (TransformParameterSpec) null);
  65. transforms.add(exclusiveTransform);
  66. Reference reference = signatureFactory.newReference("", digestMethod,
  67. transforms, null, null);
  68. references.add(reference);
  69. }
  70. }