View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * This is a test for WSS-300 - "SubjectKeyIdentifier (SKI) incorrectly calculated for 2048-bit RSA key".
36   * The SKI value WSS4J generates for various key sizes is tested against the output from openssl, e.g.:
37   *
38   * openssl x509 -inform der -ocspid -in wss40_server.crt | grep 'Public key OCSP hash'
39   * | perl -ne 'split; print pack("H*",$_[4])' | base64
40   */
41  public class SKITest {
42  
43      @Test
44      public void testRSA1024() throws Exception {
45          // Load the keystore
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          // Load the keystore
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              // Load the keystore
91              Crypto crypto = CryptoFactory.getInstance("alice_bouncycastle.properties");
92              assertNotNull(crypto);
93          } finally {
94              Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
95          }
96      }
97  }