@@ -1081,6 +1081,29 @@ public static Document copyDocument(Document source) throws ParserConfigurationE
1081
1081
* @throws XPathExpressionException
1082
1082
*/
1083
1083
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 {
1084
1107
org .apache .xml .security .Init .init ();
1085
1108
1086
1109
// Check arguments.
@@ -1095,7 +1118,7 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
1095
1118
if (key == null ) {
1096
1119
throw new IllegalArgumentException ("Provided key was null" );
1097
1120
}
1098
-
1121
+
1099
1122
if (certificate == null ) {
1100
1123
throw new IllegalArgumentException ("Provided certificate was null" );
1101
1124
}
@@ -1104,17 +1127,13 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
1104
1127
signAlgorithm = Constants .RSA_SHA1 ;
1105
1128
}
1106
1129
1107
- // document.normalizeDocument();
1108
-
1109
- String c14nMethod = Constants .C14N_WC ;
1110
-
1111
1130
// Signature object
1112
1131
XMLSignature sig = new XMLSignature (document , null , signAlgorithm , c14nMethod );
1113
1132
1114
1133
// Including the signature into the document before sign, because
1115
1134
// this is an envelop signature
1116
1135
Element root = document .getDocumentElement ();
1117
- document .setXmlStandalone (false );
1136
+ document .setXmlStandalone (false );
1118
1137
1119
1138
// If Issuer, locate Signature after Issuer, Otherwise as first child.
1120
1139
NodeList issuerNodes = Util .query (document , "//saml:Issuer" , null );
@@ -1141,7 +1160,7 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
1141
1160
sig .addDocument (reference , transforms , Constants .SHA1 );
1142
1161
1143
1162
// Add the certification info
1144
- sig .addKeyInfo (certificate );
1163
+ sig .addKeyInfo (certificate );
1145
1164
1146
1165
// Sign the document
1147
1166
sig .sign (key );
@@ -1553,5 +1572,5 @@ private static byte[] toBytesUtf8(String str) {
1553
1572
}
1554
1573
}
1555
1574
1556
-
1575
+
1557
1576
}
0 commit comments