@@ -1081,6 +1081,29 @@ public static Document copyDocument(Document source) throws ParserConfigurationE
10811081 * @throws XPathExpressionException
10821082 */
10831083 public static String addSign (Document document , PrivateKey key , X509Certificate certificate , String signAlgorithm ) throws XMLSecurityException , XPathExpressionException {
1084+ return addSign (document , key , certificate , signAlgorithm , Constants .C14N_WC );
1085+ }
1086+
1087+ /**
1088+ * Signs the Document using the specified signature algorithm with the private key and the public certificate.
1089+ *
1090+ * @param document
1091+ * The document to be signed
1092+ * @param key
1093+ * The private key
1094+ * @param certificate
1095+ * The public certificate
1096+ * @param signAlgorithm
1097+ * Signature Algorithm
1098+ * @param c14nMethod
1099+ * Canonicalization method
1100+ *
1101+ * @return the signed document in string format
1102+ *
1103+ * @throws XMLSecurityException
1104+ * @throws XPathExpressionException
1105+ */
1106+ public static String addSign (Document document , PrivateKey key , X509Certificate certificate , String signAlgorithm , String c14nMethod ) throws XMLSecurityException , XPathExpressionException {
10841107 org .apache .xml .security .Init .init ();
10851108
10861109 // Check arguments.
@@ -1095,7 +1118,7 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
10951118 if (key == null ) {
10961119 throw new IllegalArgumentException ("Provided key was null" );
10971120 }
1098-
1121+
10991122 if (certificate == null ) {
11001123 throw new IllegalArgumentException ("Provided certificate was null" );
11011124 }
@@ -1104,17 +1127,13 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
11041127 signAlgorithm = Constants .RSA_SHA1 ;
11051128 }
11061129
1107- // document.normalizeDocument();
1108-
1109- String c14nMethod = Constants .C14N_WC ;
1110-
11111130 // Signature object
11121131 XMLSignature sig = new XMLSignature (document , null , signAlgorithm , c14nMethod );
11131132
11141133 // Including the signature into the document before sign, because
11151134 // this is an envelop signature
11161135 Element root = document .getDocumentElement ();
1117- document .setXmlStandalone (false );
1136+ document .setXmlStandalone (false );
11181137
11191138 // If Issuer, locate Signature after Issuer, Otherwise as first child.
11201139 NodeList issuerNodes = Util .query (document , "//saml:Issuer" , null );
@@ -1141,7 +1160,7 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
11411160 sig .addDocument (reference , transforms , Constants .SHA1 );
11421161
11431162 // Add the certification info
1144- sig .addKeyInfo (certificate );
1163+ sig .addKeyInfo (certificate );
11451164
11461165 // Sign the document
11471166 sig .sign (key );
@@ -1553,5 +1572,5 @@ private static byte[] toBytesUtf8(String str) {
15531572 }
15541573 }
15551574
1556-
1575+
15571576}
0 commit comments