1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.message;
21
22 import javax.security.auth.callback.CallbackHandler;
23
24 import org.w3c.dom.Document;
25
26 import org.apache.ws.security.WSConstants;
27 import org.apache.ws.security.WSSConfig;
28 import org.apache.ws.security.WSSecurityEngine;
29 import org.apache.ws.security.common.CustomHandler;
30 import org.apache.ws.security.common.KeystoreCallbackHandler;
31 import org.apache.ws.security.common.SOAPUtil;
32 import org.apache.ws.security.components.crypto.Crypto;
33 import org.apache.ws.security.components.crypto.CryptoFactory;
34 import org.apache.ws.security.handler.RequestData;
35 import org.apache.ws.security.handler.WSHandlerConstants;
36
37
38
39
40
41
42
43
44 public class EncryptionCRLTest extends org.junit.Assert {
45 private static final org.apache.commons.logging.Log LOG =
46 org.apache.commons.logging.LogFactory.getLog(EncryptionCRLTest.class);
47
48 private WSSecurityEngine secEngine = new WSSecurityEngine();
49 private CallbackHandler keystoreCallbackHandler = new KeystoreCallbackHandler();
50 private Crypto crypto = null;
51
52 public EncryptionCRLTest() throws Exception {
53 crypto = CryptoFactory.getInstance("wss40All.properties");
54 }
55
56
57
58
59
60
61 @org.junit.Before
62 public void setUp() throws Exception {
63 WSSConfig wssConfig = WSSConfig.getNewInstance();
64 wssConfig.setWsiBSPCompliant(true);
65 secEngine.setWssConfig(wssConfig);
66 }
67
68
69
70
71
72
73
74 @org.junit.Test
75 public void testEncryptionWithOutRevocationCheck() throws Exception {
76 final WSSConfig cfg = WSSConfig.getNewInstance();
77 final RequestData reqData = new RequestData();
78 reqData.setWssConfig(cfg);
79 reqData.setEncUser("wss40rev");
80 reqData.setEncKeyId(WSConstants.BST_DIRECT_REFERENCE);
81 reqData.setEncSymmAlgo(WSConstants.TRIPLE_DES);
82 reqData.setEncCrypto(crypto);
83 java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
84 messageContext.put(WSHandlerConstants.PW_CALLBACK_REF, keystoreCallbackHandler);
85 reqData.setMsgContext(messageContext);
86 reqData.setUsername("wss40rev");
87
88 final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
89 actions.add(Integer.valueOf(WSConstants.ENCR));
90 final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
91 CustomHandler handler = new CustomHandler();
92 handler.send(
93 WSConstants.ENCR,
94 doc,
95 reqData,
96 actions,
97 true
98 );
99
100 String outputString =
101 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
102 if (LOG.isDebugEnabled()) {
103 LOG.debug(outputString);
104 }
105
106 verify(doc, crypto, keystoreCallbackHandler);
107 }
108
109
110
111
112
113
114
115 @org.junit.Test
116 public void testEncryptionWithRevocationCheck() throws Exception {
117 final WSSConfig cfg = WSSConfig.getNewInstance();
118 final RequestData reqData = new RequestData();
119 reqData.setWssConfig(cfg);
120 reqData.setEncUser("wss40rev");
121 reqData.setEncKeyId(WSConstants.BST_DIRECT_REFERENCE);
122 reqData.setEncSymmAlgo(WSConstants.TRIPLE_DES);
123 reqData.setEncCrypto(crypto);
124 java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
125 messageContext.put(WSHandlerConstants.PW_CALLBACK_REF, keystoreCallbackHandler);
126 reqData.setMsgContext(messageContext);
127 reqData.setUsername("wss40rev");
128
129 final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
130 actions.add(Integer.valueOf(WSConstants.ENCR));
131 final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
132 CustomHandler handler = new CustomHandler();
133 handler.setOption(WSHandlerConstants.ENABLE_REVOCATION, "true");
134 try {
135 handler.send(
136 WSConstants.ENCR,
137 doc,
138 reqData,
139 actions,
140 true
141 );
142 fail ("Failure expected on a revoked certificate");
143 } catch (Exception ex) {
144 String errorMessage = ex.getMessage();
145
146 assertTrue(errorMessage.contains("Certificate has been revoked")
147 || errorMessage.contains("Certificate revocation")
148 || errorMessage.contains("Error during certificate path validation"));
149 }
150
151 }
152
153
154
155
156
157
158
159
160 private void verify(
161 Document doc, Crypto decCrypto, CallbackHandler handler
162 ) throws Exception {
163 secEngine.processSecurityHeader(doc, null, handler, decCrypto);
164 if (LOG.isDebugEnabled()) {
165 String outputString =
166 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
167 LOG.debug(outputString);
168 }
169 }
170 }