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.action;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.security.auth.callback.CallbackHandler;
26
27 import org.apache.ws.security.SOAPConstants;
28 import org.apache.ws.security.WSConstants;
29 import org.apache.ws.security.WSEncryptionPart;
30 import org.apache.ws.security.WSPasswordCallback;
31 import org.apache.ws.security.WSSecurityException;
32 import org.apache.ws.security.handler.RequestData;
33 import org.apache.ws.security.handler.WSHandler;
34 import org.apache.ws.security.message.WSSecUsernameToken;
35 import org.apache.ws.security.message.WSSecSignature;
36 import org.apache.ws.security.util.WSSecurityUtil;
37 import org.w3c.dom.Document;
38
39
40
41
42
43
44
45
46
47
48 public class UsernameTokenSignedAction implements Action {
49 public void execute(WSHandler handler, int actionToDo, Document doc, RequestData reqData)
50 throws WSSecurityException {
51 CallbackHandler callbackHandler =
52 handler.getPasswordCallbackHandler(reqData);
53 WSPasswordCallback passwordCallback =
54 handler.getPasswordCB(reqData.getUsername(), actionToDo, callbackHandler, reqData);
55
56 WSSecUsernameToken builder = new WSSecUsernameToken(reqData.getWssConfig());
57
58 if (reqData.isUseDerivedKey()) {
59 int iterations = reqData.getDerivedKeyIterations();
60 boolean useMac = reqData.isUseDerivedKeyForMAC();
61 builder.addDerivedKey(useMac, null, iterations);
62 } else {
63 builder.setPasswordType(reqData.getPwType());
64 builder.setSecretKeyLength(reqData.getSecretKeyLength());
65 }
66
67 builder.setUserInfo(reqData.getUsername(), passwordCallback.getPassword());
68 builder.addCreated();
69 builder.addNonce();
70 builder.prepare(doc);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 WSSecSignature sign = new WSSecSignature(reqData.getWssConfig());
89 sign.setCustomTokenValueType(WSConstants.USERNAMETOKEN_NS + "#UsernameToken");
90 sign.setCustomTokenId(builder.getId());
91 sign.setSecretKey(builder.getSecretKey());
92 sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
93 if (reqData.getSigDigestAlgorithm() != null) {
94 sign.setDigestAlgo(reqData.getSigDigestAlgorithm());
95 }
96
97 if (reqData.getSigAlgorithm() != null) {
98 sign.setSignatureAlgorithm(reqData.getSigAlgorithm());
99 } else {
100 sign.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
101 }
102
103 sign.prepare(doc, null, reqData.getSecHeader());
104
105
106
107
108
109
110
111 List<WSEncryptionPart> parts = null;
112 if (reqData.getSignatureParts().size() > 0) {
113 parts = reqData.getSignatureParts();
114 } else {
115 SOAPConstants soapConstants = reqData.getSoapConstants();
116 if (soapConstants == null) {
117 soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
118 }
119 parts = new ArrayList<WSEncryptionPart>();
120 WSEncryptionPart encP =
121 new WSEncryptionPart(WSConstants.ELEM_BODY, soapConstants.getEnvelopeURI(), "Content");
122 parts.add(encP);
123 }
124 List<javax.xml.crypto.dsig.Reference> referenceList =
125 sign.addReferencesToSign(parts, reqData.getSecHeader());
126
127 try {
128 sign.computeSignature(referenceList);
129 reqData.getSignatureValues().add(sign.getSignatureValue());
130 } catch (WSSecurityException e) {
131 throw new WSSecurityException(
132 "WSHandler: Error during UsernameTokenSignature", e
133 );
134 }
135 builder.prependToHeader(reqData.getSecHeader());
136 }
137 }