/** * Signs the xml with XmlDSig using the enveloped mode, with optional xpath transform (see XmlSignatureAppearance). * @param sap the XmlSignatureAppearance * @param externalSignature the interface providing the actual signing * @param keyInfo KeyInfo for verification * @throws GeneralSecurityException * @throws IOException * @throws DocumentException */ public static void signXmlDSig(XmlSignatureAppearance sap, ExternalSignature externalSignature, KeyInfo keyInfo) throws GeneralSecurityException, IOException, DocumentException { verifyArguments(sap, externalSignature); XMLSignatureFactory fac = createSignatureFactory(); Reference reference = generateContentReference(fac, sap, null); String signatureMethod = null; if (externalSignature.getEncryptionAlgorithm().equals(SecurityConstants.RSA)) signatureMethod = SignatureMethod.RSA_SHA1; else if (externalSignature.getEncryptionAlgorithm().equals(SecurityConstants.DSA)) signatureMethod = SignatureMethod.DSA_SHA1; // Create the SignedInfo DOMSignedInfo signedInfo = (DOMSignedInfo)fac.newSignedInfo( fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(signatureMethod, null), Collections.singletonList(reference)); //sign and update document with XmlLocator sign(fac, externalSignature, sap.getXmlLocator(), signedInfo, null, keyInfo, null); sap.close(); }
/** * Signs the xml with XmlDSig using the enveloped mode, with optional xpath transform (see XmlSignatureAppearance). * @param sap the XmlSignatureAppearance * @param externalSignature the interface providing the actual signing * @param keyInfo KeyInfo for verification * @throws GeneralSecurityException * @throws IOException * @throws DocumentException */ public static void signXmlDSig(XmlSignatureAppearance sap, ExternalSignature externalSignature, KeyInfo keyInfo) throws GeneralSecurityException, IOException, DocumentException { verifyArguments(sap, externalSignature); XMLSignatureFactory fac = createSignatureFactory(); Reference reference = generateContentReference(fac, sap, null); String signatureMethod = null; if (externalSignature.getEncryptionAlgorithm().equals(SecurityConstants.RSA)) signatureMethod = SignatureMethod.RSA_SHA1; else if (externalSignature.getEncryptionAlgorithm().equals(SecurityConstants.DSA)) signatureMethod = SignatureMethod.DSA_SHA1; // Create the SignedInfo DOMSignedInfo signedInfo = (DOMSignedInfo)fac.newSignedInfo( fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(signatureMethod, null), Collections.singletonList(reference)); //sign and update document with XmlLocator sign(fac, externalSignature, sap.getXmlLocator(), signedInfo, null, keyInfo, null); sap.close(); }
Reference contentReference = generateContentReference(fac, sap, contentReferenceId); Reference signedPropertiesReference = generateCustomReference(fac, "#"+signedPropertiesId, SecurityConstants.SignedProperties_Type, null);
Reference contentReference = generateContentReference(fac, sap, contentReferenceId); Reference signedPropertiesReference = generateCustomReference(fac, "#"+signedPropertiesId, SecurityConstants.SignedProperties_Type, null);