001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.security.xwss;
018:
019: import java.security.cert.X509Certificate;
020: import javax.security.auth.callback.Callback;
021: import javax.security.auth.callback.CallbackHandler;
022: import javax.xml.soap.SOAPMessage;
023:
024: import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
025: import com.sun.xml.wss.impl.callback.SignatureKeyCallback;
026: import org.springframework.core.io.ClassPathResource;
027: import org.springframework.ws.soap.saaj.SaajSoapMessage;
028: import org.springframework.ws.soap.security.xwss.callback.AbstractCallbackHandler;
029:
030: public class XwssMessageInterceptorSignTest extends
031: XwssMessageInterceptorKeyStoreTestCase {
032:
033: public void testSignDefaultCertificate() throws Exception {
034: interceptor.setPolicyConfiguration(new ClassPathResource(
035: "sign-config.xml", getClass()));
036: CallbackHandler handler = new AbstractCallbackHandler() {
037:
038: protected void handleInternal(Callback callback) {
039: if (callback instanceof SignatureKeyCallback) {
040: SignatureKeyCallback keyCallback = (SignatureKeyCallback) callback;
041: if (keyCallback.getRequest() instanceof SignatureKeyCallback.DefaultPrivKeyCertRequest) {
042: SignatureKeyCallback.DefaultPrivKeyCertRequest request = (SignatureKeyCallback.DefaultPrivKeyCertRequest) keyCallback
043: .getRequest();
044: request.setX509Certificate(certificate);
045: request.setPrivateKey(privateKey);
046: } else {
047: fail("Unexpected request");
048: }
049: } else {
050: fail("Unexpected callback");
051: }
052: }
053: };
054: interceptor.setCallbackHandler(handler);
055: interceptor.afterPropertiesSet();
056: SaajSoapMessage message = loadSaajMessage("empty-soap.xml");
057: interceptor.secureMessage(message);
058: SOAPMessage result = message.getSaajMessage();
059: assertNotNull("No result returned", result);
060: assertXpathExists(
061: "BinarySecurityToken does not exist",
062: "SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsse:BinarySecurityToken",
063: result);
064: assertXpathExists(
065: "Signature does not exist",
066: "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/ds:Signature",
067: result);
068: }
069:
070: public void testSignAlias() throws Exception {
071: interceptor.setPolicyConfiguration(new ClassPathResource(
072: "sign-alias-config.xml", getClass()));
073: CallbackHandler handler = new AbstractCallbackHandler() {
074:
075: protected void handleInternal(Callback callback) {
076: if (callback instanceof SignatureKeyCallback) {
077: SignatureKeyCallback keyCallback = (SignatureKeyCallback) callback;
078: if (keyCallback.getRequest() instanceof SignatureKeyCallback.AliasPrivKeyCertRequest) {
079: SignatureKeyCallback.AliasPrivKeyCertRequest request = (SignatureKeyCallback.AliasPrivKeyCertRequest) keyCallback
080: .getRequest();
081: assertEquals("Invalid alias", "alias", request
082: .getAlias());
083: request.setX509Certificate(certificate);
084: request.setPrivateKey(privateKey);
085: } else {
086: fail("Unexpected request");
087: }
088: } else {
089: fail("Unexpected callback");
090: }
091: }
092: };
093: interceptor.setCallbackHandler(handler);
094: interceptor.afterPropertiesSet();
095: SaajSoapMessage message = loadSaajMessage("empty-soap.xml");
096: interceptor.secureMessage(message);
097: SOAPMessage result = message.getSaajMessage();
098: assertNotNull("No result returned", result);
099: assertXpathExists(
100: "BinarySecurityToken does not exist",
101: "SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsse:BinarySecurityToken",
102: result);
103: assertXpathExists(
104: "Signature does not exist",
105: "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/ds:Signature",
106: result);
107: }
108:
109: public void testValidateCertificate() throws Exception {
110: interceptor.setPolicyConfiguration(new ClassPathResource(
111: "requireSignature-config.xml", getClass()));
112: CallbackHandler handler = new AbstractCallbackHandler() {
113:
114: protected void handleInternal(Callback callback) {
115: if (callback instanceof CertificateValidationCallback) {
116: CertificateValidationCallback validationCallback = (CertificateValidationCallback) callback;
117: validationCallback
118: .setValidator(new CertificateValidationCallback.CertificateValidator() {
119: public boolean validate(
120: X509Certificate passedCertificate) {
121: assertEquals("Invalid certificate",
122: certificate,
123: passedCertificate);
124: return true;
125: }
126: });
127: } else {
128: fail("Unexpected callback");
129: }
130: }
131: };
132: interceptor.setCallbackHandler(handler);
133: interceptor.afterPropertiesSet();
134: SaajSoapMessage message = loadSaajMessage("signed-soap.xml");
135: interceptor.validateMessage(message);
136: SOAPMessage result = message.getSaajMessage();
137: assertNotNull("No result returned", result);
138: assertXpathNotExists("Security Header not removed",
139: "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security",
140: result);
141: }
142:
143: }
|