1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.crypto;
21
22 import java.io.InputStream;
23 import java.security.KeyStore;
24 import java.security.Security;
25 import java.security.cert.X509Certificate;
26
27 import org.apache.wss4j.common.util.Loader;
28 import org.bouncycastle.jce.provider.BouncyCastleProvider;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertNotNull;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33
34
35
36
37
38
39
40
41 public class SKITest {
42
43 @Test
44 public void testRSA1024() throws Exception {
45
46 Crypto crypto = new Merlin();
47 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
48 ClassLoader loader = Loader.getClassLoader(SKITest.class);
49 InputStream input = Merlin.loadInputStream(loader, "keys/rsa1024.jks");
50 keyStore.load(input, "security".toCharArray());
51 input.close();
52 ((Merlin)crypto).setKeyStore(keyStore);
53
54 CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
55 cryptoType.setAlias("wss40");
56 X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
57 assertTrue(certs != null && certs.length > 0);
58
59 byte[] skiBytes = crypto.getSKIBytesFromCert(certs[0]);
60 String knownBase64Encoding = "H7dt0lv9M8uYOy4SedV0kPOs22A=";
61 assertTrue(knownBase64Encoding.equals(org.apache.xml.security.utils.XMLUtils.encodeToString(skiBytes)));
62 }
63
64 @Test
65 public void testRSA2048() throws Exception {
66
67 Crypto crypto = new Merlin();
68 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
69 ClassLoader loader = Loader.getClassLoader(SKITest.class);
70 InputStream input = Merlin.loadInputStream(loader, "keys/wss40_server.jks");
71 keyStore.load(input, "security".toCharArray());
72 input.close();
73 ((Merlin)crypto).setKeyStore(keyStore);
74
75 CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
76 cryptoType.setAlias("wss40_server");
77 X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
78 assertTrue(certs != null && certs.length > 0);
79
80 byte[] skiBytes = crypto.getSKIBytesFromCert(certs[0]);
81 String knownBase64Encoding = "5LsTsLDSb7XxlaCffjNBHM5n+1A=";
82 assertTrue(knownBase64Encoding.equals(org.apache.xml.security.utils.XMLUtils.encodeToString(skiBytes)));
83 }
84
85 @Test
86 public void testBouncyCastlePKCS12() throws Exception {
87 try {
88 Security.addProvider(new BouncyCastleProvider());
89
90
91 Crypto crypto = CryptoFactory.getInstance("alice_bouncycastle.properties");
92 assertNotNull(crypto);
93 } finally {
94 Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
95 }
96 }
97 }