Skip to content

Commit f7a6365

Browse files
committed
SWS-307
1 parent 6ff4a22 commit f7a6365

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

security/src/main/java/org/springframework/ws/soap/security/wss4j/callback/KeyStoreCallbackHandler.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.security.GeneralSecurityException;
21+
import java.security.Key;
2122
import java.security.KeyStore;
2223
import javax.crypto.SecretKey;
2324
import javax.security.auth.callback.UnsupportedCallbackException;
@@ -51,7 +52,7 @@ public void setKeyStore(KeyStore keyStore) {
5152
}
5253

5354
/**
54-
* Sets the password used to retrieve private keys from the keystore. This property is required for decription based
55+
* Sets the password used to retrieve private keys from the keystore. This property is required for decryption based
5556
* on private keys, and signing.
5657
*/
5758
public void setPrivateKeyPassword(String privateKeyPassword) {
@@ -61,7 +62,7 @@ public void setPrivateKeyPassword(String privateKeyPassword) {
6162
}
6263

6364
/**
64-
* Sets the password used to retrieve keys from the symmetric keystore. If this property is not set, it default to
65+
* Sets the password used to retrieve keys from the symmetric keystore. If this property is not set, it defaults to
6566
* the private key password.
6667
*
6768
* @see #setPrivateKeyPassword(String)
@@ -88,15 +89,12 @@ protected void handleDecrypt(WSPasswordCallback callback) throws IOException, Un
8889
protected void handleKeyName(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
8990
try {
9091
String identifier = callback.getIdentifer();
91-
KeyStore.PasswordProtection protection = new KeyStore.PasswordProtection(symmetricKeyPassword);
92-
KeyStore.Entry entry = keyStore.getEntry(identifier, protection);
93-
if (entry instanceof KeyStore.SecretKeyEntry) {
94-
KeyStore.SecretKeyEntry secretKeyEntry = (KeyStore.SecretKeyEntry) entry;
95-
SecretKey secretKey = secretKeyEntry.getSecretKey();
96-
callback.setKey(secretKey.getEncoded());
92+
Key key = keyStore.getKey(identifier, symmetricKeyPassword);
93+
if (key instanceof SecretKey) {
94+
callback.setKey(key.getEncoded());
9795
}
9896
else {
99-
throw new WSSecurityException("Key entry [" + entry + "] is not a javax.crypto.SecretKey");
97+
throw new WSSecurityException("Key [" + key + "] is not a javax.crypto.SecretKey");
10098
}
10199
}
102100
catch (GeneralSecurityException ex) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright ${YEAR} the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.soap.security.wss4j.callback;
18+
19+
import java.security.KeyStore;
20+
21+
import junit.framework.TestCase;
22+
import org.apache.ws.security.WSPasswordCallback;
23+
24+
import org.springframework.core.io.ClassPathResource;
25+
import org.springframework.ws.soap.security.support.KeyStoreFactoryBean;
26+
27+
public class KeyStoreCallbackHandlerTest extends TestCase {
28+
29+
private KeyStoreCallbackHandler callbackHandler;
30+
31+
private WSPasswordCallback callback;
32+
33+
protected void setUp() throws Exception {
34+
callbackHandler = new KeyStoreCallbackHandler();
35+
callback = new WSPasswordCallback("secretkey", WSPasswordCallback.KEY_NAME);
36+
37+
KeyStoreFactoryBean factory = new KeyStoreFactoryBean();
38+
factory.setLocation(new ClassPathResource("private.jks"));
39+
factory.setPassword("123456");
40+
factory.setType("JCEKS");
41+
factory.afterPropertiesSet();
42+
KeyStore keyStore = (KeyStore) factory.getObject();
43+
callbackHandler.setKeyStore(keyStore);
44+
callbackHandler.setSymmetricKeyPassword("123456");
45+
}
46+
47+
public void testHandleKeyName() throws Exception {
48+
callbackHandler.handleInternal(callback);
49+
assertNotNull("symmetric key must not be null", callback.getKey());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)