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.str;
21
22 import org.apache.ws.security.WSConstants;
23 import org.apache.ws.security.WSSecurityException;
24 import org.apache.ws.security.message.token.BinarySecurity;
25 import org.apache.ws.security.message.token.KerberosSecurity;
26 import org.apache.ws.security.message.token.PKIPathSecurity;
27 import org.apache.ws.security.message.token.SecurityTokenReference;
28 import org.apache.ws.security.message.token.X509Security;
29 import org.apache.ws.security.saml.ext.AssertionWrapper;
30
31
32
33
34
35 public final class BSPEnforcer {
36
37 private BSPEnforcer() {
38
39 }
40
41
42
43
44
45
46
47
48 public static void checkBinarySecurityBSPCompliance(
49 SecurityTokenReference secRef,
50 BinarySecurity token
51 ) throws WSSecurityException {
52 if (secRef.containsReference()) {
53
54 String valueType = secRef.getReference().getValueType();
55 if (((token instanceof X509Security) && !X509Security.X509_V3_TYPE.equals(valueType))
56 || ((token instanceof PKIPathSecurity) && !PKIPathSecurity.PKI_TYPE.equals(valueType))
57 || ((token instanceof KerberosSecurity)
58 && !WSConstants.WSS_GSS_KRB_V5_AP_REQ.equals(valueType))) {
59 throw new WSSecurityException(
60 WSSecurityException.INVALID_SECURITY_TOKEN,
61 "invalidValueType",
62 new Object[]{valueType}
63 );
64 }
65 } else if (secRef.containsKeyIdentifier()) {
66 String valueType = secRef.getKeyIdentifierValueType();
67 if (!SecurityTokenReference.SKI_URI.equals(valueType)
68 && !SecurityTokenReference.THUMB_URI.equals(valueType)
69 && !WSConstants.WSS_KRB_KI_VALUE_TYPE.equals(valueType)) {
70 throw new WSSecurityException(
71 WSSecurityException.INVALID_SECURITY_TOKEN,
72 "invalidValueType",
73 new Object[]{valueType}
74 );
75 }
76 }
77
78
79 if (token instanceof PKIPathSecurity) {
80 String tokenType = secRef.getTokenType();
81 if (!PKIPathSecurity.PKI_TYPE.equals(tokenType)) {
82 throw new WSSecurityException(
83 WSSecurityException.INVALID_SECURITY_TOKEN,
84 "invalidTokenType",
85 new Object[]{tokenType}
86 );
87 }
88 }
89 }
90
91
92
93
94
95
96
97 public static void checkEncryptedKeyBSPCompliance(
98 SecurityTokenReference secRef
99 ) throws WSSecurityException {
100 if (secRef.containsKeyIdentifier()) {
101 String valueType = secRef.getKeyIdentifierValueType();
102 if (!SecurityTokenReference.ENC_KEY_SHA1_URI.equals(valueType)) {
103 throw new WSSecurityException(
104 WSSecurityException.INVALID_SECURITY_TOKEN,
105 "invalidValueType",
106 new Object[]{valueType}
107 );
108 }
109 }
110
111 String tokenType = secRef.getTokenType();
112 if (!WSConstants.WSS_ENC_KEY_VALUE_TYPE.equals(tokenType)) {
113 throw new WSSecurityException(
114 WSSecurityException.INVALID_SECURITY_TOKEN,
115 "invalidTokenType",
116 new Object[]{tokenType}
117 );
118 }
119 }
120
121
122
123
124
125
126
127
128 public static void checkSamlTokenBSPCompliance(
129 SecurityTokenReference secRef,
130 AssertionWrapper assertion
131 ) throws WSSecurityException {
132
133 if (secRef.containsKeyIdentifier()) {
134 String valueType = secRef.getKeyIdentifierValueType();
135 if (assertion.getSaml1() != null
136 && !WSConstants.WSS_SAML_KI_VALUE_TYPE.equals(valueType)) {
137 throw new WSSecurityException(
138 WSSecurityException.INVALID_SECURITY_TOKEN,
139 "invalidValueType",
140 new Object[]{valueType}
141 );
142 }
143 if (assertion.getSaml2() != null
144 && !WSConstants.WSS_SAML2_KI_VALUE_TYPE.equals(valueType)) {
145 throw new WSSecurityException(
146 WSSecurityException.INVALID_SECURITY_TOKEN,
147 "invalidValueType",
148 new Object[]{valueType}
149 );
150 }
151 String encoding = secRef.getKeyIdentifierEncodingType();
152 if (encoding != null && !"".equals(encoding)) {
153 throw new WSSecurityException(
154 WSSecurityException.INVALID_SECURITY_TOKEN,
155 "badEncodingType",
156 new Object[]{encoding}
157 );
158 }
159 }
160
161
162 String tokenType = secRef.getTokenType();
163 if (assertion.getSaml1() != null && !WSConstants.WSS_SAML_TOKEN_TYPE.equals(tokenType)) {
164 throw new WSSecurityException(
165 WSSecurityException.INVALID_SECURITY_TOKEN,
166 "invalidTokenType",
167 new Object[]{tokenType}
168 );
169 }
170 if (assertion.getSaml2() != null && !WSConstants.WSS_SAML2_TOKEN_TYPE.equals(tokenType)) {
171 throw new WSSecurityException(
172 WSSecurityException.INVALID_SECURITY_TOKEN,
173 "invalidTokenType",
174 new Object[]{tokenType}
175 );
176 }
177
178
179 if (assertion.getSaml2() != null && secRef.containsReference()) {
180 String valueType = secRef.getReference().getValueType();
181 if (valueType != null && !"".equals(valueType)) {
182 throw new WSSecurityException(
183 WSSecurityException.INVALID_SECURITY_TOKEN,
184 "invalidValueType",
185 new Object[]{valueType}
186 );
187 }
188 }
189 }
190
191
192
193
194
195
196
197 public static void checkUsernameTokenBSPCompliance(
198 SecurityTokenReference secRef
199 ) throws WSSecurityException {
200 if (!secRef.containsReference()) {
201
202 throw new WSSecurityException(
203 WSSecurityException.FAILED_CHECK, "unsupportedKeyId"
204 );
205 }
206
207 String valueType = secRef.getReference().getValueType();
208 if (!WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE.equals(valueType)) {
209
210 throw new WSSecurityException(
211 WSSecurityException.INVALID_SECURITY,
212 "invalidValueType",
213 new Object[]{valueType}
214 );
215 }
216 }
217
218
219 }