Skip to content

Commit be9040d

Browse files
committed
Checking decryption algorithms in some of the StAX tests
1 parent a3f3bf3 commit be9040d

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

src/test/java/org/apache/xml/security/test/stax/encryption/KeyWrapEncryptionVerificationTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
import org.apache.xml.security.stax.ext.XMLSec;
5050
import org.apache.xml.security.stax.ext.XMLSecurityConstants;
5151
import org.apache.xml.security.stax.ext.XMLSecurityProperties;
52+
import org.apache.xml.security.stax.securityEvent.AlgorithmSuiteSecurityEvent;
53+
import org.apache.xml.security.stax.securityEvent.SecurityEvent;
54+
import org.apache.xml.security.stax.securityEvent.SecurityEventConstants;
5255
import org.apache.xml.security.test.dom.DSNamespaceContext;
5356
import org.apache.xml.security.test.stax.signature.TestSecurityEventListener;
5457
import org.apache.xml.security.test.stax.utils.StAX2DOM;
@@ -63,6 +66,7 @@
6366
import org.w3c.dom.NodeList;
6467

6568
import static org.junit.jupiter.api.Assertions.assertEquals;
69+
import static org.junit.jupiter.api.Assertions.assertFalse;
6670
import static org.junit.jupiter.api.Assertions.assertNotNull;
6771
import static org.junit.jupiter.api.Assertions.assertTrue;
6872

@@ -161,6 +165,8 @@ void testAES128KW() throws Exception {
161165

162166
document = StAX2DOM.readDoc(securityStreamReader);
163167

168+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.AES_128_KeyWrap);
169+
164170
// Check the CreditCard decrypted ok
165171
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
166172
assertEquals(nodeList.getLength(), 1);
@@ -215,6 +221,8 @@ void testAES192KW() throws Exception {
215221

216222
document = StAX2DOM.readDoc(securityStreamReader);
217223

224+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.AES_192_KeyWrap);
225+
218226
// Check the CreditCard decrypted ok
219227
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
220228
assertEquals(nodeList.getLength(), 1);
@@ -269,6 +277,8 @@ void testAES256KW() throws Exception {
269277

270278
document = StAX2DOM.readDoc(securityStreamReader);
271279

280+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.AES_256_KeyWrap);
281+
272282
// Check the CreditCard decrypted ok
273283
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
274284
assertEquals(nodeList.getLength(), 1);
@@ -324,6 +334,8 @@ void testTripleDESKW() throws Exception {
324334

325335
document = StAX2DOM.readDoc(securityStreamReader);
326336

337+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.TRIPLEDES_KeyWrap);
338+
327339
// Check the CreditCard decrypted ok
328340
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
329341
assertEquals(nodeList.getLength(), 1);
@@ -378,6 +390,8 @@ void testRSAv15KW() throws Exception {
378390

379391
document = StAX2DOM.readDoc(securityStreamReader);
380392

393+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.RSA_v1dot5);
394+
381395
// Check the CreditCard decrypted ok
382396
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
383397
assertEquals(nodeList.getLength(), 1);
@@ -432,6 +446,8 @@ void testRSAOAEPKW() throws Exception {
432446

433447
document = StAX2DOM.readDoc(securityStreamReader);
434448

449+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.RSA_OAEP);
450+
435451
// Check the CreditCard decrypted ok
436452
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
437453
assertEquals(nodeList.getLength(), 1);
@@ -486,6 +502,8 @@ void testRSAOAEP11KW() throws Exception {
486502

487503
document = StAX2DOM.readDoc(securityStreamReader);
488504

505+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.RSA_OAEP_11);
506+
489507
// Check the CreditCard decrypted ok
490508
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
491509
assertEquals(nodeList.getLength(), 1);
@@ -545,6 +563,8 @@ void testCamellia128KW() throws Exception {
545563

546564
document = StAX2DOM.readDoc(securityStreamReader);
547565

566+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.CAMELLIA_128_KeyWrap);
567+
548568
// Check the CreditCard decrypted ok
549569
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
550570
assertEquals(nodeList.getLength(), 1);
@@ -604,6 +624,8 @@ void testCamellia192KW() throws Exception {
604624

605625
document = StAX2DOM.readDoc(securityStreamReader);
606626

627+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.CAMELLIA_192_KeyWrap);
628+
607629
// Check the CreditCard decrypted ok
608630
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
609631
assertEquals(nodeList.getLength(), 1);
@@ -663,6 +685,8 @@ void testCamellia256KW() throws Exception {
663685

664686
document = StAX2DOM.readDoc(securityStreamReader);
665687

688+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.CAMELLIA_256_KeyWrap);
689+
666690
// Check the CreditCard decrypted ok
667691
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
668692
assertEquals(nodeList.getLength(), 1);
@@ -722,11 +746,40 @@ void testSEED128KW() throws Exception {
722746

723747
document = StAX2DOM.readDoc(securityStreamReader);
724748

749+
checkDecryptionMethod(securityEventListener, encryptionAlgorithm, XMLCipher.SEED_128_KeyWrap);
750+
725751
// Check the CreditCard decrypted ok
726752
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
727753
assertEquals(nodeList.getLength(), 1);
728754
}
729755

756+
private void checkDecryptionMethod(
757+
TestSecurityEventListener securityEventListener,
758+
String encryptionAlgorithm,
759+
String keyWrapAlgorithm
760+
) {
761+
List<SecurityEvent> algorithmEvents =
762+
securityEventListener.getSecurityEvents(SecurityEventConstants.AlgorithmSuite);
763+
assertFalse(algorithmEvents.isEmpty());
764+
765+
boolean matchedEncryptionAlgorithm = false;
766+
boolean matchedKeyWrapAlgorithm = false;
767+
for (SecurityEvent event : algorithmEvents) {
768+
AlgorithmSuiteSecurityEvent algorithmEvent = (AlgorithmSuiteSecurityEvent) event;
769+
if (XMLSecurityConstants.Enc.equals(algorithmEvent.getAlgorithmUsage())) {
770+
assertEquals(encryptionAlgorithm, algorithmEvent.getAlgorithmURI());
771+
matchedEncryptionAlgorithm = true;
772+
} else if (XMLSecurityConstants.Sym_Key_Wrap.equals(algorithmEvent.getAlgorithmUsage())
773+
|| XMLSecurityConstants.Asym_Key_Wrap.equals(algorithmEvent.getAlgorithmUsage())) {
774+
assertEquals(keyWrapAlgorithm, algorithmEvent.getAlgorithmURI());
775+
matchedKeyWrapAlgorithm = true;
776+
}
777+
}
778+
779+
assertTrue(matchedEncryptionAlgorithm);
780+
assertTrue(matchedKeyWrapAlgorithm);
781+
}
782+
730783
private void encrypt(
731784
EncryptedKey encryptedKey,
732785
String algorithm,

src/test/java/org/apache/xml/security/test/stax/encryption/SymmetricEncryptionVerificationTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
import org.apache.xml.security.stax.ext.XMLSec;
4949
import org.apache.xml.security.stax.ext.XMLSecurityConstants;
5050
import org.apache.xml.security.stax.ext.XMLSecurityProperties;
51+
import org.apache.xml.security.stax.securityEvent.AlgorithmSuiteSecurityEvent;
52+
import org.apache.xml.security.stax.securityEvent.SecurityEvent;
53+
import org.apache.xml.security.stax.securityEvent.SecurityEventConstants;
5154
import org.apache.xml.security.test.dom.DSNamespaceContext;
5255
import org.apache.xml.security.test.stax.signature.TestSecurityEventListener;
5356
import org.apache.xml.security.test.stax.utils.StAX2DOM;
@@ -62,6 +65,7 @@
6265
import org.w3c.dom.NodeList;
6366

6467
import static org.junit.jupiter.api.Assertions.assertEquals;
68+
import static org.junit.jupiter.api.Assertions.assertFalse;
6569
import static org.junit.jupiter.api.Assertions.assertNotNull;
6670
import static org.junit.jupiter.api.Assertions.assertTrue;
6771

@@ -153,6 +157,8 @@ void testAES128() throws Exception {
153157

154158
document = StAX2DOM.readDoc(securityStreamReader);
155159

160+
checkDecryptionMethod(securityEventListener, algorithm);
161+
156162
// Check the CreditCard decrypted ok
157163
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
158164
assertEquals(nodeList.getLength(), 1);
@@ -203,6 +209,8 @@ void testAES128_GCM() throws Exception {
203209

204210
document = StAX2DOM.readDoc(securityStreamReader);
205211

212+
checkDecryptionMethod(securityEventListener, algorithm);
213+
206214
// Check the CreditCard decrypted ok
207215
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
208216
assertEquals(nodeList.getLength(), 1);
@@ -253,6 +261,8 @@ void testAES192() throws Exception {
253261

254262
document = StAX2DOM.readDoc(securityStreamReader);
255263

264+
checkDecryptionMethod(securityEventListener, algorithm);
265+
256266
// Check the CreditCard decrypted ok
257267
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
258268
assertEquals(nodeList.getLength(), 1);
@@ -303,6 +313,8 @@ void testAES192_GCM() throws Exception {
303313

304314
document = StAX2DOM.readDoc(securityStreamReader);
305315

316+
checkDecryptionMethod(securityEventListener, algorithm);
317+
306318
// Check the CreditCard decrypted ok
307319
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
308320
assertEquals(nodeList.getLength(), 1);
@@ -353,6 +365,8 @@ void testAES256() throws Exception {
353365

354366
document = StAX2DOM.readDoc(securityStreamReader);
355367

368+
checkDecryptionMethod(securityEventListener, algorithm);
369+
356370
// Check the CreditCard decrypted ok
357371
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
358372
assertEquals(nodeList.getLength(), 1);
@@ -403,6 +417,8 @@ void testAES256_GCM() throws Exception {
403417

404418
document = StAX2DOM.readDoc(securityStreamReader);
405419

420+
checkDecryptionMethod(securityEventListener, algorithm);
421+
406422
// Check the CreditCard decrypted ok
407423
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
408424
assertEquals(nodeList.getLength(), 1);
@@ -452,6 +468,8 @@ void testTRIPLE_DES() throws Exception {
452468

453469
document = StAX2DOM.readDoc(securityStreamReader);
454470

471+
checkDecryptionMethod(securityEventListener, algorithm);
472+
455473
// Check the CreditCard decrypted ok
456474
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
457475
assertEquals(nodeList.getLength(), 1);
@@ -504,6 +522,8 @@ void testSEED_128() throws Exception {
504522

505523
document = StAX2DOM.readDoc(securityStreamReader);
506524

525+
checkDecryptionMethod(securityEventListener, algorithm);
526+
507527
// Check the CreditCard decrypted ok
508528
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
509529
assertEquals(nodeList.getLength(), 1);
@@ -556,6 +576,8 @@ void testCAMELLIA_128() throws Exception {
556576

557577
document = StAX2DOM.readDoc(securityStreamReader);
558578

579+
checkDecryptionMethod(securityEventListener, algorithm);
580+
559581
// Check the CreditCard decrypted ok
560582
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
561583
assertEquals(nodeList.getLength(), 1);
@@ -608,6 +630,8 @@ void testCAMELLIA_192() throws Exception {
608630

609631
document = StAX2DOM.readDoc(securityStreamReader);
610632

633+
checkDecryptionMethod(securityEventListener, algorithm);
634+
611635
// Check the CreditCard decrypted ok
612636
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
613637
assertEquals(nodeList.getLength(), 1);
@@ -660,11 +684,30 @@ void testCAMELLIA_256() throws Exception {
660684

661685
document = StAX2DOM.readDoc(securityStreamReader);
662686

687+
checkDecryptionMethod(securityEventListener, algorithm);
688+
663689
// Check the CreditCard decrypted ok
664690
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
665691
assertEquals(nodeList.getLength(), 1);
666692
}
667693

694+
private void checkDecryptionMethod(TestSecurityEventListener securityEventListener, String encryptionAlgorithm) {
695+
List<SecurityEvent> algorithmEvents =
696+
securityEventListener.getSecurityEvents(SecurityEventConstants.AlgorithmSuite);
697+
assertFalse(algorithmEvents.isEmpty());
698+
699+
boolean matchedEncryptionAlgorithm = false;
700+
for (SecurityEvent event : algorithmEvents) {
701+
AlgorithmSuiteSecurityEvent algorithmEvent = (AlgorithmSuiteSecurityEvent) event;
702+
if (XMLSecurityConstants.Enc.equals(algorithmEvent.getAlgorithmUsage())) {
703+
assertEquals(encryptionAlgorithm, algorithmEvent.getAlgorithmURI());
704+
matchedEncryptionAlgorithm = true;
705+
}
706+
}
707+
708+
assertTrue(matchedEncryptionAlgorithm);
709+
}
710+
668711
private void encryptUsingDOM(
669712
String algorithm,
670713
SecretKey secretKey,

0 commit comments

Comments
 (0)