96
96
* A class that contains several auxiliary methods related to the SAML protocol
97
97
*/
98
98
public final class Util {
99
+
99
100
/**
100
101
* Private property to construct a logger for this class.
101
102
*/
@@ -109,6 +110,11 @@ public final class Util {
109
110
/** Indicates if JAXP 1.5 support has been detected. */
110
111
private static boolean JAXP_15_SUPPORTED = isJaxp15Supported ();
111
112
113
+ static {
114
+ System .setProperty ("org.apache.xml.security.ignoreLineBreaks" , "true" );
115
+ org .apache .xml .security .Init .init ();
116
+ }
117
+
112
118
private Util () {
113
119
//not called
114
120
}
@@ -380,7 +386,6 @@ public static Document parseXML(InputSource inputSource) throws ParserConfigurat
380
386
* @return the Document object
381
387
*/
382
388
public static String convertDocumentToString (Document doc , Boolean c14n ) {
383
- org .apache .xml .security .Init .init ();
384
389
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
385
390
if (c14n ) {
386
391
XMLUtils .outputDOMc14nWithComments (doc , baos );
@@ -525,8 +530,6 @@ public static X509Certificate loadCert(String certString) throws CertificateExce
525
530
* @throws GeneralSecurityException
526
531
*/
527
532
public static PrivateKey loadPrivateKey (String keyString ) throws GeneralSecurityException {
528
- org .apache .xml .security .Init .init ();
529
-
530
533
String extractedKey = formatPrivateKey (keyString , false );
531
534
extractedKey = chunkString (extractedKey , 64 );
532
535
KeyFactory kf = KeyFactory .getInstance ("RSA" );
@@ -808,8 +811,6 @@ public static String urlDecoder(String input) {
808
811
* @throws SignatureException
809
812
*/
810
813
public static byte [] sign (String text , PrivateKey key , String signAlgorithm ) throws NoSuchAlgorithmException , InvalidKeyException , SignatureException {
811
- org .apache .xml .security .Init .init ();
812
-
813
814
if (signAlgorithm == null ) {
814
815
signAlgorithm = Constants .RSA_SHA1 ;
815
816
}
@@ -964,8 +965,6 @@ public static Boolean validateMetadataSign(Document doc, X509Certificate cert, S
964
965
public static Boolean validateSignNode (Node signNode , X509Certificate cert , String fingerprint , String alg ) {
965
966
Boolean res = false ;
966
967
try {
967
- org .apache .xml .security .Init .init ();
968
-
969
968
Element sigElement = (Element ) signNode ;
970
969
XMLSignature signature = new XMLSignature (sigElement , "" , true );
971
970
@@ -1034,8 +1033,6 @@ public static boolean isAlgorithmWhitelisted(String alg) {
1034
1033
*/
1035
1034
public static void decryptElement (Element encryptedDataElement , PrivateKey inputKey ) {
1036
1035
try {
1037
- org .apache .xml .security .Init .init ();
1038
-
1039
1036
XMLCipher xmlCipher = XMLCipher .getInstance ();
1040
1037
xmlCipher .init (XMLCipher .DECRYPT_MODE , null );
1041
1038
@@ -1111,15 +1108,36 @@ public static Document copyDocument(Document source) throws ParserConfigurationE
1111
1108
* The public certificate
1112
1109
* @param signAlgorithm
1113
1110
* Signature Algorithm
1114
- *
1111
+ *
1115
1112
* @return the signed document in string format
1116
- *
1113
+ *
1117
1114
* @throws XMLSecurityException
1118
1115
* @throws XPathExpressionException
1119
1116
*/
1120
1117
public static String addSign (Document document , PrivateKey key , X509Certificate certificate , String signAlgorithm ) throws XMLSecurityException , XPathExpressionException {
1121
- org .apache .xml .security .Init .init ();
1118
+ return addSign (document , key , certificate , signAlgorithm , Constants .SHA1 );
1119
+ }
1122
1120
1121
+ /**
1122
+ * Signs the Document using the specified signature algorithm with the private key and the public certificate.
1123
+ *
1124
+ * @param document
1125
+ * The document to be signed
1126
+ * @param key
1127
+ * The private key
1128
+ * @param certificate
1129
+ * The public certificate
1130
+ * @param signAlgorithm
1131
+ * Signature Algorithm
1132
+ * @param digestAlgorithm
1133
+ * Digest Algorithm
1134
+ *
1135
+ * @return the signed document in string format
1136
+ *
1137
+ * @throws XMLSecurityException
1138
+ * @throws XPathExpressionException
1139
+ */
1140
+ public static String addSign (Document document , PrivateKey key , X509Certificate certificate , String signAlgorithm , String digestAlgorithm ) throws XMLSecurityException , XPathExpressionException {
1123
1141
// Check arguments.
1124
1142
if (document == null ) {
1125
1143
throw new IllegalArgumentException ("Provided document was null" );
@@ -1132,18 +1150,21 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
1132
1150
if (key == null ) {
1133
1151
throw new IllegalArgumentException ("Provided key was null" );
1134
1152
}
1135
-
1153
+
1136
1154
if (certificate == null ) {
1137
1155
throw new IllegalArgumentException ("Provided certificate was null" );
1138
1156
}
1139
1157
1140
1158
if (signAlgorithm == null || signAlgorithm .isEmpty ()) {
1141
1159
signAlgorithm = Constants .RSA_SHA1 ;
1142
1160
}
1161
+ if (digestAlgorithm == null || digestAlgorithm .isEmpty ()) {
1162
+ digestAlgorithm = Constants .SHA1 ;
1163
+ }
1143
1164
1144
- // document.normalizeDocument();
1165
+ document .normalizeDocument ();
1145
1166
1146
- String c14nMethod = Constants .C14NEXC_WC ;
1167
+ String c14nMethod = Constants .C14NEXC ;
1147
1168
1148
1169
// Signature object
1149
1170
XMLSignature sig = new XMLSignature (document , null , signAlgorithm , c14nMethod );
@@ -1155,30 +1176,42 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
1155
1176
1156
1177
// If Issuer, locate Signature after Issuer, Otherwise as first child.
1157
1178
NodeList issuerNodes = Util .query (document , "//saml:Issuer" , null );
1179
+ Element elemToSign = null ;
1158
1180
if (issuerNodes .getLength () > 0 ) {
1159
1181
Node issuer = issuerNodes .item (0 );
1160
1182
root .insertBefore (sig .getElement (), issuer .getNextSibling ());
1183
+ elemToSign = (Element ) issuer .getParentNode ();
1161
1184
} else {
1162
- root .insertBefore (sig .getElement (), root .getFirstChild ());
1185
+ NodeList entitiesDescriptorNodes = Util .query (document , "//md:EntitiesDescriptor" , null );
1186
+ if (entitiesDescriptorNodes .getLength () > 0 ) {
1187
+ elemToSign = (Element )entitiesDescriptorNodes .item (0 );
1188
+ } else {
1189
+ NodeList entityDescriptorNodes = Util .query (document , "//md:EntityDescriptor" , null );
1190
+ if (entityDescriptorNodes .getLength () > 0 ) {
1191
+ elemToSign = (Element )entityDescriptorNodes .item (0 );
1192
+ } else {
1193
+ elemToSign = root ;
1194
+ }
1195
+ }
1196
+ root .insertBefore (sig .getElement (), elemToSign .getFirstChild ());
1163
1197
}
1164
1198
1165
- String id = root .getAttribute ("ID" );
1199
+ String id = elemToSign .getAttribute ("ID" );
1166
1200
1167
1201
String reference = id ;
1168
1202
if (!id .isEmpty ()) {
1169
- root .setIdAttributeNS (null , "ID" , true );
1203
+ elemToSign .setIdAttributeNS (null , "ID" , true );
1170
1204
reference = "#" + id ;
1171
1205
}
1172
1206
1173
1207
// Create the transform for the document
1174
1208
Transforms transforms = new Transforms (document );
1175
1209
transforms .addTransform (Constants .ENVSIG );
1176
- //transforms.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
1177
1210
transforms .addTransform (c14nMethod );
1178
- sig .addDocument (reference , transforms , Constants . SHA1 );
1211
+ sig .addDocument (reference , transforms , digestAlgorithm );
1179
1212
1180
1213
// Add the certification info
1181
- sig .addKeyInfo (certificate );
1214
+ sig .addKeyInfo (certificate );
1182
1215
1183
1216
// Sign the document
1184
1217
sig .sign (key );
@@ -1197,14 +1230,16 @@ public static String addSign(Document document, PrivateKey key, X509Certificate
1197
1230
* The public certificate
1198
1231
* @param signAlgorithm
1199
1232
* Signature Algorithm
1200
- *
1233
+ * @param digestAlgorithm
1234
+ * Digest Algorithm
1235
+ *
1201
1236
* @return the signed document in string format
1202
1237
*
1203
1238
* @throws ParserConfigurationException
1204
1239
* @throws XMLSecurityException
1205
1240
* @throws XPathExpressionException
1206
1241
*/
1207
- public static String addSign (Node node , PrivateKey key , X509Certificate certificate , String signAlgorithm ) throws ParserConfigurationException , XPathExpressionException , XMLSecurityException {
1242
+ public static String addSign (Node node , PrivateKey key , X509Certificate certificate , String signAlgorithm , String digestAlgorithm ) throws ParserConfigurationException , XPathExpressionException , XMLSecurityException {
1208
1243
// Check arguments.
1209
1244
if (node == null ) {
1210
1245
throw new IllegalArgumentException ("Provided node was null" );
@@ -1216,9 +1251,31 @@ public static String addSign(Node node, PrivateKey key, X509Certificate certific
1216
1251
Node newNode = doc .importNode (node , true );
1217
1252
doc .appendChild (newNode );
1218
1253
1219
- return addSign (doc , key , certificate , signAlgorithm );
1220
- }
1221
-
1254
+ return addSign (doc , key , certificate , signAlgorithm , digestAlgorithm );
1255
+ }
1256
+
1257
+ /**
1258
+ * Signs a Node using the specified signature algorithm with the private key and the public certificate.
1259
+ *
1260
+ * @param node
1261
+ * The Node to be signed
1262
+ * @param key
1263
+ * The private key
1264
+ * @param certificate
1265
+ * The public certificate
1266
+ * @param signAlgorithm
1267
+ * Signature Algorithm
1268
+ *
1269
+ * @return the signed document in string format
1270
+ *
1271
+ * @throws ParserConfigurationException
1272
+ * @throws XMLSecurityException
1273
+ * @throws XPathExpressionException
1274
+ */
1275
+ public static String addSign (Node node , PrivateKey key , X509Certificate certificate , String signAlgorithm ) throws ParserConfigurationException , XPathExpressionException , XMLSecurityException {
1276
+ return addSign (node , key , certificate , signAlgorithm , Constants .SHA1 );
1277
+ }
1278
+
1222
1279
/**
1223
1280
* Validates signed binary data (Used to validate GET Signature).
1224
1281
*
@@ -1241,8 +1298,6 @@ public static String addSign(Node node, PrivateKey key, X509Certificate certific
1241
1298
public static Boolean validateBinarySignature (String signedQuery , byte [] signature , X509Certificate cert , String signAlg ) throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeyException , SignatureException {
1242
1299
Boolean valid = false ;
1243
1300
try {
1244
- org .apache .xml .security .Init .init ();
1245
-
1246
1301
String convertedSigAlg = signatureAlgConversion (signAlg );
1247
1302
1248
1303
Signature sig = Signature .getInstance (convertedSigAlg ); //, provider);
@@ -1278,7 +1333,6 @@ public static Boolean validateBinarySignature(String signedQuery, byte[] signatu
1278
1333
public static Boolean validateBinarySignature (String signedQuery , byte [] signature , List <X509Certificate > certList , String signAlg ) throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeyException , SignatureException {
1279
1334
Boolean valid = false ;
1280
1335
1281
- org .apache .xml .security .Init .init ();
1282
1336
String convertedSigAlg = signatureAlgConversion (signAlg );
1283
1337
Signature sig = Signature .getInstance (convertedSigAlg ); //, provider);
1284
1338
0 commit comments