-
Notifications
You must be signed in to change notification settings - Fork 21
/
WSITAuthContextBase.java
1854 lines (1632 loc) · 80 KB
/
WSITAuthContextBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.wss.provider.wsit;
import com.sun.xml.ws.api.addressing.AddressingVersion;
import com.sun.xml.ws.api.model.wsdl.WSDLFault;
import com.sun.xml.ws.security.impl.policyconv.XWSSPolicyGenerator;
import com.sun.xml.ws.security.policy.CertStoreConfig;
import com.sun.xml.ws.security.policy.KerberosConfig;
import com.sun.xml.ws.security.policy.SecurityPolicyVersion;
import com.sun.xml.wss.impl.policy.mls.EncryptionPolicy;
import com.sun.xml.wss.impl.policy.mls.EncryptionTarget;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Hashtable;
import javax.xml.namespace.QName;
import java.util.Set;
import com.sun.xml.ws.api.message.AddressingUtils;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.api.message.Message;
import com.sun.xml.ws.api.message.MessageHeaders;
import com.sun.xml.ws.api.SOAPVersion;
import com.sun.xml.ws.api.WSBinding;
import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
import com.sun.xml.ws.api.model.wsdl.WSDLOperation;
import com.sun.xml.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.ws.api.policy.ModelTranslator;
import com.sun.xml.ws.policy.NestedPolicy;
import com.sun.xml.ws.security.impl.policyconv.SCTokenWrapper;
import com.sun.xml.ws.security.impl.policyconv.SecurityPolicyHolder;
import com.sun.xml.ws.api.pipe.Pipe;
import com.sun.xml.ws.policy.AssertionSet;
import com.sun.xml.ws.policy.Policy;
import com.sun.xml.ws.policy.PolicyException;
import com.sun.xml.ws.policy.PolicyMap;
import com.sun.xml.ws.policy.PolicyMapKey;
import com.sun.xml.ws.policy.PolicyMerger;
import com.sun.xml.wss.jaxws.impl.Constants;
import com.sun.xml.wss.jaxws.impl.TubeConfiguration;
import com.sun.xml.ws.security.policy.AsymmetricBinding;
import com.sun.xml.ws.security.policy.AlgorithmSuite;
import com.sun.xml.ws.security.policy.SecureConversationToken;
import com.sun.xml.ws.security.policy.SupportingTokens;
import com.sun.xml.ws.security.policy.SymmetricBinding;
import com.sun.xml.ws.security.impl.policy.PolicyUtil;
import com.sun.xml.ws.security.IssuedTokenContext;
import com.sun.xml.ws.policy.sourcemodel.PolicySourceModel;
import jakarta.xml.soap.SOAPFault;
import jakarta.xml.soap.SOAPFactory;
import jakarta.xml.ws.soap.SOAPFaultException;
import jakarta.xml.soap.SOAPConstants;
import com.sun.xml.wss.XWSSecurityException;
import com.sun.xml.wss.SecurityEnvironment;
import com.sun.xml.wss.impl.policy.mls.MessagePolicy;
import com.sun.xml.ws.security.trust.WSTrustElementFactory;
import com.sun.xml.ws.policy.PolicyAssertion;
import com.sun.xml.ws.security.policy.Token;
import jakarta.xml.bind.JAXBContext;
import com.sun.xml.wss.impl.MessageConstants;
import com.sun.xml.wss.impl.misc.DefaultCallbackHandler;
import com.sun.xml.ws.security.policy.KeyStore;
import com.sun.xml.ws.security.policy.TrustStore;
import com.sun.xml.ws.security.policy.CallbackHandlerConfiguration;
import com.sun.xml.ws.security.policy.Validator;
import com.sun.xml.ws.security.policy.ValidatorConfiguration;
import com.sun.xml.ws.security.policy.WSSAssertion;
import java.util.Properties;
import com.sun.xml.ws.api.pipe.Tube;
import com.sun.xml.ws.api.policy.ModelUnmarshaller;
import com.sun.xml.ws.api.server.WSEndpoint;
import com.sun.xml.ws.runtime.dev.SessionManager;
import com.sun.xml.ws.rx.mc.api.McProtocolVersion;
import com.sun.xml.ws.rx.rm.api.RmProtocolVersion;
import com.sun.xml.ws.security.impl.policy.SessionManagerStore;
import com.sun.xml.ws.security.opt.impl.util.CertificateRetriever;
import com.sun.xml.wss.jaxws.impl.ClientTubeConfiguration;
import com.sun.xml.wss.jaxws.impl.ServerTubeConfiguration;
import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
import com.sun.xml.wss.ProcessingContext;
import com.sun.xml.wss.impl.PolicyViolationException;
import com.sun.xml.wss.impl.ProcessingContextImpl;
import com.sun.xml.wss.impl.SecurableSoapMessage;
import com.sun.xml.wss.impl.SecurityAnnotator;
import com.sun.xml.wss.impl.WssSoapFaultException;
import com.sun.xml.wss.jaxws.impl.RMPolicyResolver;
import java.util.Map;
import javax.security.auth.callback.CallbackHandler;
import jakarta.security.auth.message.MessageInfo;
import jakarta.xml.soap.SOAPMessage;
import com.sun.xml.ws.security.secconv.WSSCVersion;
import com.sun.xml.ws.security.trust.WSTrustVersion;
import com.sun.xml.wss.XWSSConstants;
import com.sun.xml.wss.impl.policy.spi.PolicyVerifier;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.xml.wss.provider.wsit.logging.LogDomainConstants;
import com.sun.xml.wss.provider.wsit.logging.LogStringsMessages;
import java.security.cert.X509Certificate;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
/**
*
* @author kumar jayanti
*/
public abstract class WSITAuthContextBase {
protected static final Logger log =
Logger.getLogger(
LogDomainConstants.WSIT_PVD_DOMAIN,
LogDomainConstants.WSIT_PVD_DOMAIN_BUNDLE);
//*************Synchronized And Modified at Runtime***********
// Per-Proxy State for SecureConversation sessions
// as well as IssuedTokenContext returned by invoking a Trust-Plugin
// This map stores IssuedTokenContext against the Policy-Id
protected Hashtable<String, IssuedTokenContext> issuedTokenContextMap = new Hashtable<>();
//*************STATIC(s)**************************************
//protected static QName bsOperationName =
// new QName("http://schemas.xmlsoap.org/ws/2005/02/trust","RequestSecurityToken");
private final QName EPREnabled = new QName("http://schemas.sun.com/2006/03/wss/server","EnableEPRIdentity");
private final QName encSCServerCancel = new QName("http://schemas.sun.com/2006/03/wss/server","EncSCCancel");
private final QName encSCClientCancel = new QName("http://schemas.sun.com/2006/03/wss/client","EncSCCancel");
private final QName optServerSecurity = new QName("http://schemas.sun.com/2006/03/wss/server","DisableStreamingSecurity");
private final QName optClientSecurity = new QName("http://schemas.sun.com/2006/03/wss/client","DisableStreamingSecurity");
protected boolean disableIncPrefix = false;
private final QName disableIncPrefixServer = new QName("http://schemas.sun.com/2006/03/wss/server","DisableInclusivePrefixList");
private final QName disableIncPrefixClient = new QName("http://schemas.sun.com/2006/03/wss/client","DisableInclusivePrefixList");
protected boolean encRMLifecycleMsg = false;
private final QName encRMLifecycleMsgServer = new QName("http://schemas.sun.com/2006/03/wss/server","EncryptRMLifecycleMessage");
private final QName encRMLifecycleMsgClient = new QName("http://schemas.sun.com/2006/03/wss/client","EncryptRMLifecycleMessage");
protected boolean encHeaderContent = false;
private final QName encHeaderContentServer = new QName("http://schemas.sun.com/2006/03/wss/server","EncryptHeaderContent");
private final QName encHeaderContentClient = new QName("http://schemas.sun.com/2006/03/wss/client","EncryptHeaderContent");
protected boolean allowMissingTimestamp = false;
private final QName allowMissingTSServer = new QName("http://schemas.sun.com/2006/03/wss/server","AllowMissingTimestamp");
private final QName allowMissingTSClient = new QName("http://schemas.sun.com/2006/03/wss/client","AllowMissingTimestamp");
protected boolean securityMUValue = true;
private final QName unsetSecurityMUValueServer = new QName("http://schemas.sun.com/2006/03/wss/server","UnsetSecurityMUValue");
private final QName unsetSecurityMUValueClient = new QName("http://schemas.sun.com/2006/03/wss/client","UnsetSecurityMUValue");
//static JAXBContext used across the Pipe
protected static final JAXBContext jaxbContext;
protected WSSCVersion wsscVer = null;
protected WSTrustVersion wsTrustVer = null;
protected RmProtocolVersion rmVer = RmProtocolVersion.WSRM200502;
protected McProtocolVersion mcVer = McProtocolVersion.WSMC200702;
protected static final ArrayList<String> securityPolicyNamespaces ;
//protected static MessagePolicy emptyMessagePolicy;
protected static final List<PolicyAssertion> EMPTY_LIST = Collections.emptyList();
// debug the Secure SOAP Messages (enable dumping)
protected static final boolean debug ;
//public static final URI ISSUE_REQUEST_URI ;
//public static final URI CANCEL_REQUEST_URI ;
//***********CTOR initialized Instance Variables**************
protected Pipe nextPipe;
protected Tube nextTube;
// TODO: Optimized flag to be set based on some conditions (no SignedElements/EncryptedElements)
protected boolean optimized = true;
protected TubeConfiguration pipeConfig = null;
// Security Environment reference initialized with a JAAS CallbackHandler
protected SecurityEnvironment secEnv = null;
// SOAP version
protected boolean isSOAP12 = false;
protected SOAPVersion soapVersion = null;
// SOAP Factory
protected SOAPFactory soapFactory = null;
//protected PolicyMap wsPolicyMap = null;
protected List<PolicyAlternativeHolder> policyAlternatives =
new ArrayList<>();
protected Policy bpMSP = null;
//protected WSDLBoundOperation cachedOperation = null;
// store as instance variable
protected Marshaller marshaller =null;
protected Unmarshaller unmarshaller =null;
//store instance variable(s): Binding has IssuedToken/RM/SC Policy
//TODO:POLALT : Comment them and move these fields into
//PolicyAlternativeHolder later
boolean hasIssuedTokens = false;
boolean hasSecureConversation = false;
boolean hasKerberosToken = false;
protected AlgorithmSuite bindingLevelAlgSuite = null;
private AlgorithmSuite bootStrapAlgoSuite;
boolean hasReliableMessaging = false;
boolean hasMakeConnection = false;
//boolean addressingEnabled = false;
AddressingVersion addVer = null;
//WSDLPort port = null;
// Security Policy version
protected SecurityPolicyVersion spVersion = SecurityPolicyVersion.SECURITYPOLICY200507;
protected static final String REQ_PACKET = "REQ_PACKET";
protected static final String RES_PACKET = "RES_PACKET";
protected static final String DEFAULT_JMAC_HANDLER = "com.sun.enterprise.security.jmac.callback.ContainerCallbackHandler";
protected static final String WSDLPORT="WSDLPort";
protected static final String WSENDPOINT="WSEndpoint";
protected X509Certificate serverCert = null;
protected boolean isCertValidityVerified = false;
protected long timestampTimeOut = 0;
protected int iterationsForPDK = 0;
protected boolean isEPREnabled = false;
private boolean encryptCancelPayload = false;
private Policy cancelMSP;
protected boolean isCertValid;
static {
try {
//TODO: system property maynot be appropriate for server side.
debug = Boolean.parseBoolean(System.getProperty("DebugSecurity"));
//ISSUE_REQUEST_URI = new URI(WSTrustConstants.REQUEST_SECURITY_TOKEN_ISSUE_ACTION);
//CANCEL_REQUEST_URI = new URI(WSTrustConstants.CANCEL_REQUEST);
jaxbContext = WSTrustElementFactory.getContext();
securityPolicyNamespaces = new ArrayList<>();
securityPolicyNamespaces.add(SecurityPolicyVersion.SECURITYPOLICY200507.namespaceUri);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/** Creates a new instance of WSITAuthContextBase */
public WSITAuthContextBase(Map<String, Object> map) {
this.nextPipe = (Pipe)map.get("NEXT_PIPE");
this.nextTube = (Tube)map.get("NEXT_TUBE");
PolicyMap wsPolicyMap = (PolicyMap)map.get("POLICY");
WSDLPort port =(WSDLPort)map.get("WSDL_MODEL");
if (this instanceof WSITClientAuthContext) {
WSBinding binding = (WSBinding)map.get("BINDING");
pipeConfig = new ClientTubeConfiguration(
wsPolicyMap, port, binding);
} else {
WSEndpoint endPoint = (WSEndpoint)map.get("ENDPOINT");
pipeConfig = new ServerTubeConfiguration(
wsPolicyMap, port, endPoint);
}
soapVersion = pipeConfig.getBinding().getSOAPVersion();
isSOAP12 = soapVersion == SOAPVersion.SOAP_12;
wsPolicyMap = pipeConfig.getPolicyMap();
soapFactory = pipeConfig.getBinding().getSOAPVersion().getSOAPFactory();
if(wsPolicyMap != null){
collectPolicies(wsPolicyMap,policyAlternatives);
}
//unmarshaller as instance variable of the pipe
try {
this.marshaller = jaxbContext.createMarshaller();
this.unmarshaller = jaxbContext.createUnmarshaller();
}catch (jakarta.xml.bind.JAXBException ex) {
throw new RuntimeException(ex);
}
// check whether Service Port has RM
hasReliableMessaging = isReliableMessagingEnabled(pipeConfig.getWSDLPort());
hasMakeConnection = isMakeConnectionEnabled(pipeConfig.getWSDLPort());
//put properties for use by AuthModule init
map.put("SOAP_VERSION", soapVersion);
}
/**
* Summary from Section 4.2, WS-Security Policy spec( version 1.1 July 2005 ).
* MessagePolicySubject : policy can be attached to
* 1) wsdl:binding/wsdl:operation/wsdl:input, ./wsdl:output, or ./wsdl:fault
*
* OperationPolicySubject : policy can be attached to
* 1)wsdl:binding/wsdl:operation
*
* EndpointPolicySubject : policy can be attached to
* 1)wsdl:port
* 2)wsdl:Binding
*/
protected void collectPolicies(PolicyMap wsPolicyMap, List<PolicyAlternativeHolder> alternatives) {
try {
if (wsPolicyMap == null) {
return;
}
//To check: Is this sufficient, any edge cases I need to take care
QName serviceName = pipeConfig.getWSDLPort().getOwner().getName();
QName portName = pipeConfig.getWSDLPort().getName();
//Review: will this take care of EndpointPolicySubject
//PolicyMerger policyMerge = PolicyMerger.getMerger();
PolicyMapKey endpointKey = PolicyMap.createWsdlEndpointScopeKey(serviceName, portName);
//createWsdlEndpointScopeKey(serviceName,portName);
//Review:Will getEffectivePolicy return null or empty policy ?.
Policy endpointPolicy = wsPolicyMap.getEndpointEffectivePolicy(endpointKey);
//TODO:POLALT these should be alternative specific as well ?.
setPolicyCredentials(endpointPolicy);
//This will be used for setting credentials like spVersion.... etc for operation level policies
for (WSDLBoundOperation operation : pipeConfig.getWSDLPort().getBinding().getBindingOperations()) {
QName operationName = new QName(operation.getBoundPortType().getName().getNamespaceURI(), operation.getName().getLocalPart());
PolicyMapKey operationKey = PolicyMap.createWsdlOperationScopeKey(serviceName, portName, operationName);
Policy operationPolicy = wsPolicyMap.getOperationEffectivePolicy(operationKey);
setPolicyCredentials(operationPolicy);
}
//This will be used for setting credentials like spVersion.... etc for binding level policies
if (endpointPolicy == null) {
ArrayList<Policy> policyList = new ArrayList<>();
PolicyAlternativeHolder ph = new PolicyAlternativeHolder(null, spVersion, bpMSP);
alternatives.add(ph);
collectOperationAndMessageLevelPolicies(wsPolicyMap, null, policyList,ph);
return;
}
Iterator<AssertionSet> policiesIter = endpointPolicy.iterator();
while (policiesIter.hasNext()) {
ArrayList<Policy> policyList = new ArrayList<>();
AssertionSet ass = policiesIter.next();
PolicyAlternativeHolder ph = new PolicyAlternativeHolder(ass, spVersion, bpMSP);
alternatives.add(ph);
Collection<AssertionSet> coll = new ArrayList<>();
coll.add(ass);
Policy singleAlternative = Policy.createPolicy(
endpointPolicy.getNamespaceVersion(), endpointPolicy.getName(), endpointPolicy.getId(), coll);
buildProtocolPolicy(singleAlternative, ph);
//if(endpointPolicy != null){
policyList.add(singleAlternative);
//}
collectOperationAndMessageLevelPolicies(wsPolicyMap, singleAlternative, policyList, ph);
}
} catch (PolicyException pe) {
throw generateInternalError(pe);
}
}
//TODO:POLALT Alternatives only at BindingLevel for Now
private void collectOperationAndMessageLevelPolicies(PolicyMap wsPolicyMap,
Policy singleAlternative, ArrayList<Policy> policyList, PolicyAlternativeHolder ph) {
if (wsPolicyMap == null) {
return;
}
try {
QName serviceName = pipeConfig.getWSDLPort().getOwner().getName();
QName portName = pipeConfig.getWSDLPort().getName();
PolicyMerger policyMerge = PolicyMerger.getMerger();
for (WSDLBoundOperation operation : pipeConfig.getWSDLPort().getBinding().getBindingOperations()) {
QName operationName = new QName(operation.getBoundPortType().getName().getNamespaceURI(),
operation.getName().getLocalPart());
PolicyMapKey messageKey = PolicyMap.createWsdlMessageScopeKey(
serviceName, portName, operationName);
PolicyMapKey operationKey = PolicyMap.createWsdlOperationScopeKey(serviceName, portName, operationName);
//Review:Not sure if this is need and what is the
//difference between operation and message level key.
//securityPolicyNamespaces
Policy operationPolicy = wsPolicyMap.getOperationEffectivePolicy(operationKey);
if (operationPolicy != null) {
policyList.add(operationPolicy);
} else {
//log fine message
//System.out.println("Operation Level Security policy is null");
}
Policy imPolicy = null;
imPolicy = wsPolicyMap.getInputMessageEffectivePolicy(messageKey);
if (imPolicy != null) {
policyList.add(imPolicy);
}
//input message effective policy to be used. Policy elements at various
//scopes merged.
Policy imEP = policyMerge.merge(policyList);
SecurityPolicyHolder outPH = null;
if (imEP != null) {
outPH = addOutgoingMP(operation, imEP, ph);
}
if (imPolicy != null) {
policyList.remove(imPolicy);
}
//one way
SecurityPolicyHolder inPH = null;
Policy omPolicy = null;
omPolicy = wsPolicyMap.getOutputMessageEffectivePolicy(messageKey);
if (omPolicy != null) {
policyList.add(omPolicy);
}
//ouput message effective policy to be used. Policy elements at various
//scopes merged.
Policy omEP = policyMerge.merge(policyList);
if (omPolicy != null) {
policyList.remove(omPolicy);
}
if (omEP != null) {
inPH = addIncomingMP(operation, omEP, ph);
}
Iterator faults = operation.getOperation().getFaults().iterator();
ArrayList<Policy> faultPL = new ArrayList<>();
if (singleAlternative != null) {
faultPL.add(singleAlternative);
}
if (operationPolicy != null) {
faultPL.add(operationPolicy);
}
while (faults.hasNext()) {
WSDLFault fault = (WSDLFault) faults.next();
PolicyMapKey fKey = null;
fKey = PolicyMap.createWsdlFaultMessageScopeKey(
serviceName, portName, operationName,
new QName(operationName.getNamespaceURI(), fault.getName()));
Policy fPolicy = wsPolicyMap.getFaultMessageEffectivePolicy(fKey);
if (fPolicy != null) {
faultPL.add(fPolicy);
} else {
//continue;
}
Policy ep = policyMerge.merge(faultPL);
if (inPH != null) {
addIncomingFaultPolicy(ep, inPH, fault);
}
if (outPH != null) {
addOutgoingFaultPolicy(ep, outPH, fault);
}
faultPL.remove(fPolicy);
}
if (operationPolicy != null) {
policyList.remove(operationPolicy);
}
}
} catch (PolicyException pe) {
throw generateInternalError(pe);
}
}
private void setPolicyCredentials(Policy policy){
if (policy != null){
if (policy.contains(AddressingVersion.W3C.policyNsUri) ||policy.contains("http://www.w3.org/2007/05/addressing/metadata")){
addVer = AddressingVersion.W3C;
} else if (policy.contains(AddressingVersion.MEMBER.policyNsUri)){
addVer = AddressingVersion.MEMBER;
}
if(policy.contains(optServerSecurity) || policy.contains(optClientSecurity)){
optimized = false;
}
if (policy.contains(EPREnabled)) {
isEPREnabled = true;
}
if (policy.contains(encSCServerCancel) || policy.contains(encSCClientCancel)) {
this.encryptCancelPayload = true;
}
if (policy.contains(disableIncPrefixServer) || policy.contains(disableIncPrefixClient)){
disableIncPrefix = true;
}
if(policy.contains(encHeaderContentServer) || policy.contains(encHeaderContentClient)){
encHeaderContent = true;
}
if(policy.contains(allowMissingTSClient) || policy.contains(allowMissingTSServer)){
allowMissingTimestamp = true;
}
if(policy.contains(unsetSecurityMUValueClient) || policy.contains(unsetSecurityMUValueServer)){
securityMUValue = false;
}
if(policy.contains(SecurityPolicyVersion.SECURITYPOLICY200507.namespaceUri)){
spVersion = SecurityPolicyVersion.SECURITYPOLICY200507;
wsscVer = WSSCVersion.WSSC_10;
wsTrustVer = WSTrustVersion.WS_TRUST_10;
} else if(policy.contains(SecurityPolicyVersion.SECURITYPOLICY12NS.namespaceUri)){
spVersion = SecurityPolicyVersion.SECURITYPOLICY12NS;
wsscVer = WSSCVersion.WSSC_13;
wsTrustVer = WSTrustVersion.WS_TRUST_13;
} else if (policy.contains(SecurityPolicyVersion.SECURITYPOLICY200512.namespaceUri)) {
spVersion = SecurityPolicyVersion.SECURITYPOLICY200512;
wsscVer = WSSCVersion.WSSC_10;
wsTrustVer = WSTrustVersion.WS_TRUST_10;
}
if (policy.contains(RmProtocolVersion.WSRM200702.protocolNamespaceUri) ||
policy.contains(RmProtocolVersion.WSRM200702.policyNamespaceUri)) {
rmVer = RmProtocolVersion.WSRM200702;
} else if (policy.contains(RmProtocolVersion.WSRM200502.protocolNamespaceUri) ||
policy.contains(RmProtocolVersion.WSRM200502.policyNamespaceUri)) {
rmVer = RmProtocolVersion.WSRM200502;
}
if (policy.contains(this.encRMLifecycleMsgServer) || policy.contains(encRMLifecycleMsgClient)) {
encRMLifecycleMsg = true;
}
}
}
protected RuntimeException generateInternalError(PolicyException ex){
SOAPFault fault = null;
try {
if (isSOAP12) {
fault = soapFactory.createFault(ex.getMessage(),SOAPConstants.SOAP_SENDER_FAULT);
fault.appendFaultSubcode(MessageConstants.WSSE_INTERNAL_SERVER_ERROR);
} else {
fault = soapFactory.createFault(ex.getMessage(), MessageConstants.WSSE_INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
log.log(Level.SEVERE, LogStringsMessages.WSITPVD_0002_INTERNAL_SERVER_ERROR(), e);
throw new RuntimeException(LogStringsMessages.WSITPVD_0002_INTERNAL_SERVER_ERROR(), e);
}
return new SOAPFaultException(fault);
}
//TODO:POLALT : should this method look over all alternatives
protected List<PolicyAssertion> getInBoundSCP(Message message){
SecurityPolicyHolder sph = null;
for (PolicyAlternativeHolder p : policyAlternatives) {
if (p.getInMessagePolicyMap() == null) {
return Collections.emptyList();
}
Collection coll = p.getInMessagePolicyMap().values();
Iterator itr = coll.iterator();
while (itr.hasNext()) {
SecurityPolicyHolder ph = (SecurityPolicyHolder) itr.next();
if (ph != null) {
sph = ph;
break;
}
}
if (sph == null) {
return EMPTY_LIST;
}
}
return sph.getSecureConversationTokens();
}
//TODO:POLALT : should this method look over all alternatives
protected List<PolicyAssertion> getOutBoundSCP(
Message message) {
SecurityPolicyHolder sph = null;
//TODO:encapsulate this explicit public member access p.x below
for (PolicyAlternativeHolder p : policyAlternatives) {
if (p.getOutMessagePolicyMap() == null) {
return Collections.emptyList();
}
Collection coll = p.getOutMessagePolicyMap().values();
Iterator itr = coll.iterator();
while (itr.hasNext()) {
SecurityPolicyHolder ph = (SecurityPolicyHolder) itr.next();
if (ph != null) {
sph = ph;
break;
}
}
if (sph == null) {
return EMPTY_LIST;
}
}
return sph.getSecureConversationTokens();
}
//TODO:POLALT : should this method look over all alternatives
protected List<PolicyAssertion> getOutBoundKTP(Packet packet, boolean isSCMessage){
if(isSCMessage){
Token scToken = (Token)packet.invocationProperties.get(Constants.SC_ASSERTION);
return ((SCTokenWrapper)scToken).getKerberosTokens();
}
SecurityPolicyHolder sph = null;
//TODO:encapsulate this explicit public member access p.x below
for (PolicyAlternativeHolder p : policyAlternatives) {
if (p.getOutMessagePolicyMap() == null) {
return Collections.emptyList();
}
Message message = packet.getMessage();
Collection coll = p.getOutMessagePolicyMap().values();
Iterator itr = coll.iterator();
while (itr.hasNext()) {
SecurityPolicyHolder ph = (SecurityPolicyHolder) itr.next();
if (ph != null) {
sph = ph;
break;
}
}
if (sph == null) {
return EMPTY_LIST;
}
}
return sph.getKerberosTokens();
}
//TODO:POLALT : should this method look over all alternatives
protected List<PolicyAssertion> getSecureConversationPolicies(
Message message, String scope) {
SecurityPolicyHolder sph = null;
//TODO:encapsulate this explicit public member access p.x below
for (PolicyAlternativeHolder p : policyAlternatives) {
if (p.getOutMessagePolicyMap() == null) {
return Collections.emptyList();
}
Collection coll = p.getOutMessagePolicyMap().values();
Iterator itr = coll.iterator();
while (itr.hasNext()) {
SecurityPolicyHolder ph = (SecurityPolicyHolder) itr.next();
if (ph != null) {
sph = ph;
break;
}
}
if (sph == null) {
return EMPTY_LIST;
}
}
return sph.getSecureConversationTokens();
}
//TODO :: Refactor
protected ArrayList<PolicyAssertion> getTokens(Policy policy){
ArrayList<PolicyAssertion> tokenList = new ArrayList<>();
for(AssertionSet assertionSet : policy){
for(PolicyAssertion assertion:assertionSet){
if(PolicyUtil.isAsymmetricBinding(assertion, spVersion)){
AsymmetricBinding sb = (AsymmetricBinding)assertion;
Token iToken = sb.getInitiatorToken();
if (iToken != null){
addToken(iToken, tokenList);
}else{
addToken(sb.getInitiatorSignatureToken(), tokenList);
addToken(sb.getInitiatorEncryptionToken(), tokenList);
}
Token rToken = sb.getRecipientToken();
if (rToken != null){
addToken(rToken, tokenList);
}else{
addToken(sb.getRecipientSignatureToken(), tokenList);
addToken(sb.getRecipientEncryptionToken(), tokenList);
}
}else if(PolicyUtil.isSymmetricBinding(assertion, spVersion)){
SymmetricBinding sb = (SymmetricBinding)assertion;
Token token = sb.getProtectionToken();
if(token != null){
addToken(token,tokenList);
}else{
addToken(sb.getEncryptionToken(),tokenList);
addToken(sb.getSignatureToken(),tokenList);
}
}else if(PolicyUtil.isSupportingTokens(assertion, spVersion)){
SupportingTokens st = (SupportingTokens)assertion;
Iterator itr = st.getTokens();
while(itr.hasNext()){
addToken((Token)itr.next(),tokenList);
}
}
}
}
return tokenList;
}
private void addConfigAssertions(Policy policy,SecurityPolicyHolder sph){
//ArrayList<PolicyAssertion> tokenList = new ArrayList<PolicyAssertion>();
for(AssertionSet assertionSet : policy){
for(PolicyAssertion assertion:assertionSet){
if(PolicyUtil.isConfigPolicyAssertion(assertion)){
sph.addConfigAssertions(assertion);
}
}
}
}
private void addToken(Token token,ArrayList<PolicyAssertion> list){
if (token == null){
return;
}
if(PolicyUtil.isSecureConversationToken((PolicyAssertion)token, spVersion) ||
PolicyUtil.isIssuedToken((PolicyAssertion)token, spVersion) ||
PolicyUtil.isKerberosToken((PolicyAssertion)token, spVersion)){
list.add((PolicyAssertion)token);
}
}
protected PolicyMapKey getOperationKey(Message message){
WSDLBoundOperation operation = message.getOperation(pipeConfig.getWSDLPort());
WSDLOperation wsdlOperation = operation.getOperation();
QName serviceName = pipeConfig.getWSDLPort().getOwner().getName();
QName portName = pipeConfig.getWSDLPort().getName();
//WSDLInput input = wsdlOperation.getInput();
//WSDLOutput output = wsdlOperation.getOutput();
//QName inputMessageName = input.getMessage().getName();
//QName outputMessageName = output.getMessage().getName();
return PolicyMap.createWsdlMessageScopeKey(
serviceName,portName,wsdlOperation.getName());
}
protected abstract SecurityPolicyHolder addOutgoingMP(WSDLBoundOperation operation,Policy policy, PolicyAlternativeHolder ph)throws PolicyException;
protected abstract SecurityPolicyHolder addIncomingMP(WSDLBoundOperation operation,Policy policy, PolicyAlternativeHolder ph)throws PolicyException;
protected AlgorithmSuite getBindingAlgorithmSuite(Packet packet) {
return bindingLevelAlgSuite;
}
protected void cacheMessage(Packet packet){
// Not required, commenting
// Message message = null;
// if(!optimized){
// try{
// message = packet.getMessage();
// message= Messages.create(message.readAsSOAPMessage());
// packet.setMessage(message);
// }catch(SOAPException se){
// // internal error
// log.log(Level.SEVERE, LogStringsMessages.WSITPVD_0005_PROBLEM_PROC_SOAP_MESSAGE(), se);
// throw new WebServiceException(LogStringsMessages.WSITPVD_0005_PROBLEM_PROC_SOAP_MESSAGE(), se);
// }
// }
}
private boolean hasTargets(NestedPolicy policy){
AssertionSet as = policy.getAssertionSet();
//Iterator<PolicyAssertion> paItr = as.iterator();
boolean foundTargets = false;
for(PolicyAssertion assertion : as){
if(PolicyUtil.isSignedParts(assertion, spVersion) || PolicyUtil.isEncryptParts(assertion, spVersion)){
foundTargets = true;
break;
}
}
return foundTargets;
}
protected Policy getEffectiveBootstrapPolicy(NestedPolicy bp)throws PolicyException{
try {
ArrayList<Policy> pl = new ArrayList<>();
pl.add(bp);
Policy mbp = getMessageBootstrapPolicy();
if ( mbp != null ) {
pl.add(mbp);
}
PolicyMerger pm = PolicyMerger.getMerger();
return pm.merge(pl);
} catch (Exception e) {
log.log(Level.SEVERE, LogStringsMessages.WSITPVD_0007_PROBLEM_GETTING_EFF_BOOT_POLICY(), e);
throw new PolicyException(LogStringsMessages.WSITPVD_0007_PROBLEM_GETTING_EFF_BOOT_POLICY(), e);
}
}
private Policy getMessageBootstrapPolicy()throws PolicyException ,IOException{
if(bpMSP == null){
String bootstrapMessagePolicy = "boot-msglevel-policy.xml";
if(SecurityPolicyVersion.SECURITYPOLICY12NS.namespaceUri.equals(spVersion.namespaceUri)){
bootstrapMessagePolicy = "boot-msglevel-policy-sx.xml";
}
PolicySourceModel model = unmarshalPolicy(
"com/sun/xml/ws/security/impl/policyconv/"+ bootstrapMessagePolicy);
bpMSP = ModelTranslator.getTranslator().translate(model);
}
return bpMSP;
}
// TODO : remove this unused method if possible
// private Policy getMessageLevelBSP() throws PolicyException {
// QName serviceName = pipeConfig.getWSDLModel().getOwner().getName();
// QName portName = pipeConfig.getWSDLModel().getName();
// PolicyMapKey operationKey = PolicyMap.createWsdlOperationScopeKey(serviceName, portName, bsOperationName);
//
// Policy operationLevelEP = wsPolicyMap.getOperationEffectivePolicy(operationKey);
// return operationLevelEP;
// }
protected PolicySourceModel unmarshalPolicy(String resource) throws PolicyException, IOException {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (is == null) {
return null;
}
Reader reader = new InputStreamReader(is);
PolicySourceModel model = ModelUnmarshaller.getUnmarshaller().unmarshalModel(reader);
reader.close();
return model;
}
protected final WSDLBoundOperation cacheOperation(Message msg, Packet packet){
WSDLBoundOperation cachedOperation = msg.getOperation(pipeConfig.getWSDLPort());
packet.invocationProperties.put("WSDL_BOUND_OPERATION", cachedOperation);
return cachedOperation;
}
protected final void resetCachedOperation(Packet packet){
packet.invocationProperties.put("WSDL_BOUND_OPERATION", null);
}
protected final void cacheOperation(WSDLBoundOperation op, Packet packet) {
packet.invocationProperties.put("WSDL_BOUND_OPERATION", op);
}
protected final WSDLBoundOperation cachedOperation(Packet packet) {
return (WSDLBoundOperation)packet.invocationProperties.get("WSDL_BOUND_OPERATION");
}
protected boolean isSCMessage(Packet packet){
if (!bindingHasSecureConversationPolicy()) {
return false;
}
if (!isAddressingEnabled()) {
return false;
}
String action = getAction(packet);
return Constants.rstSCTURI.equals(action);
}
protected boolean isSCRenew(Packet packet){
if (!bindingHasSecureConversationPolicy()) {
return false;
}
if (!isAddressingEnabled()) {
return false;
}
String action = getAction(packet);
return wsscVer.getSCTRenewResponseAction().equals(action) ||
wsscVer.getSCTRenewRequestAction().equals(action);
}
protected boolean isSCCancel(Packet packet){
if (!bindingHasSecureConversationPolicy()) {
return false;
}
if (!isAddressingEnabled()) {
return false;
}
String action = getAction(packet);
return wsscVer.getSCTCancelResponseAction().equals(action) ||
wsscVer.getSCTCancelRequestAction().equals(action);
}
protected boolean isAddressingEnabled() {
return (addVer != null);
}
protected boolean isTrustMessage(Packet packet){
if (!isAddressingEnabled()) {
return false;
}
// Issue
String action = getAction(packet);
if(wsTrustVer.getIssueRequestAction().equals(action) ||
wsTrustVer.getIssueFinalResoponseAction().equals(action)){
return true;
}
// Validate
return wsTrustVer.getValidateRequestAction().equals(action) ||
wsTrustVer.getValidateFinalResoponseAction().equals(action);
}
protected boolean isRMMessage(Packet packet){
if (!isAddressingEnabled()) {
return false;
}
if (!bindingHasRMPolicy()) {
return false;
}
return rmVer.isProtocolAction(getAction(packet));
}
protected boolean isMakeConnectionMessage(Packet packet) {
if (!this.hasMakeConnection) {
return false;
}
return mcVer.isProtocolAction(getAction(packet));
}
protected String getAction(Packet packet){
// if ("true".equals(packet.invocationProperties.get(WSTrustConstants.IS_TRUST_MESSAGE))){
// return (String)packet.invocationProperties.get(WSTrustConstants.REQUEST_SECURITY_TOKEN_ISSUE_ACTION);
//}
MessageHeaders hl = packet.getMessage().getHeaders();
//String action = hl.getAction(pipeConfig.getBinding().getAddressingVersion(), pipeConfig.getBinding().getSOAPVersion());
return AddressingUtils.getAction(hl, addVer, pipeConfig.getBinding().getSOAPVersion());
}
protected WSDLBoundOperation getWSDLOpFromAction(Packet packet ,boolean isIncomming){
String uriValue = getAction(packet);
for (PolicyAlternativeHolder p : policyAlternatives) {
Set<WSDLBoundOperation> keys = p.getOutMessagePolicyMap().keySet();
for (WSDLBoundOperation wbo : keys) {
WSDLOperation wo = wbo.getOperation();
// WsaWSDLOperationExtension extensions = wo.getExtension(WsaWSDLOperationExtension.class);
String action = getAction(wo, isIncomming);
if (action != null && action.equals(uriValue)) {
return wbo;
}
}
}
return null;
}
protected void buildProtocolPolicy(Policy endpointPolicy, PolicyAlternativeHolder ph)throws PolicyException{
if(endpointPolicy == null ){
return;
}
try{
RMPolicyResolver rr = new RMPolicyResolver(spVersion, rmVer, mcVer, encRMLifecycleMsg);
Policy msgLevelPolicy = rr.getOperationLevelPolicy();
PolicyMerger merger = PolicyMerger.getMerger();
ArrayList<Policy> pList = new ArrayList<>(2);
pList.add(endpointPolicy);
pList.add(msgLevelPolicy);
Policy effectivePolicy = merger.merge(pList);
addIncomingProtocolPolicy(effectivePolicy,"RM",ph);
addOutgoingProtocolPolicy(effectivePolicy,"RM",ph);
pList.remove(msgLevelPolicy);
pList.add(getMessageBootstrapPolicy());
PolicyMerger pm = PolicyMerger.getMerger();
//add secure conversation policy.
Policy ep = pm.merge(pList);
addIncomingProtocolPolicy(ep,"SC",ph);
addOutgoingProtocolPolicy(ep,"SC", ph);
ArrayList<Policy> pList1 = new ArrayList<>(2);
pList1.add(endpointPolicy);
pList1.add(getSCCancelPolicy(encryptCancelPayload));
PolicyMerger pm1 = PolicyMerger.getMerger();
//add secure conversation policy.
Policy ep1 = pm1.merge(pList1);
addIncomingProtocolPolicy(ep1,"SC-CANCEL",ph);
addOutgoingProtocolPolicy(ep1,"SC-CANCEL",ph);
}catch(IOException ie){
log.log(Level.SEVERE,