1 2 24 25 package com.sun.enterprise.iiop.security; 26 27 import java.lang.InheritableThreadLocal ; 28 import java.io.IOException ; 29 import java.net.Socket ; 30 import java.util.Set ; 31 import java.util.HashSet ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 import java.util.Properties ; 35 import java.net.InetAddress ; 36 import java.util.List ; 37 import java.util.ArrayList ; 38 39 import java.security.PrivilegedAction ; 40 import java.security.AccessController ; 41 import javax.security.auth.Subject ; 42 import javax.security.auth.login.LoginContext ; 43 import java.security.cert.X509Certificate ; 44 import javax.net.ssl.SSLSession; 45 import javax.net.ssl.SSLSocket; 46 import org.ietf.jgss.*; 48 49 import com.sun.enterprise.Switch; 50 import com.sun.enterprise.ExecutionContext; 51 import com.sun.enterprise.InvocationManager; 52 import com.sun.enterprise.InvocationException; 53 import com.sun.enterprise.ComponentInvocation; 54 import com.sun.enterprise.appclient.AppContainer; 55 import com.sun.enterprise.deployment.JndiNameEnvironment; 56 import com.sun.enterprise.deployment.EjbReferenceDescriptor; 57 import com.sun.enterprise.deployment.EjbDescriptor; 58 import com.sun.enterprise.deployment.EjbIORConfigurationDescriptor; 59 import com.sun.enterprise.iiop.CSIV2TaggedComponentInfo; 60 import org.omg.CORBA.ORB ; 61 import com.sun.enterprise.iiop.IORToSocketInfoImpl; 62 import com.sun.enterprise.iiop.POAProtocolMgr; 63 import com.sun.enterprise.security.SSLUtils; 64 import com.sun.enterprise.security.auth.login.PasswordCredential; 65 import com.sun.enterprise.security.auth.login.X509CertificateCredential; 66 import com.sun.enterprise.security.auth.LoginContextDriver; 67 import com.sun.enterprise.util.Utility; 68 import com.sun.enterprise.util.ORBManager; 69 import com.sun.enterprise.util.TypeUtil; 70 71 import com.sun.corba.ee.spi.ior.IOR; 72 import com.sun.corba.ee.spi.ior.iiop.IIOPAddress; 73 import com.sun.corba.ee.spi.ior.iiop.IIOPProfileTemplate; 74 import com.sun.corba.ee.spi.transport.SocketInfo; 75 import com.sun.corba.ee.org.omg.CSI.*; 76 import com.sun.corba.ee.org.omg.CSIIOP.*; 77 78 import sun.security.x509.X500Name; 79 import com.sun.enterprise.log.Log; 80 import com.sun.enterprise.util.LocalStringManagerImpl; 81 import java.util.logging.*; 82 import com.sun.logging.*; 83 84 96 97 public final class SecurityMechanismSelector { 98 99 private static java.util.logging.Logger _logger=null; 100 static{ 101 _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER); 102 } 103 104 public static String CLIENT_CONNECTION_CONTEXT = "ClientConnContext"; 105 public static String SERVER_CONNECTION_CONTEXT = "ServerConnContext"; 106 107 private static Set corbaIORDescSet = null; 108 private static boolean sslRequired = false; 109 110 private static String [] serverTrustedHosts = null; 114 115 private static LocalStringManagerImpl localStrings = 116 new LocalStringManagerImpl(SecServerRequestInterceptor.class); 117 118 private static POAProtocolMgr protocolMgr = null; 121 122 125 static { 126 try { 127 String s = 129 (String )(ORBManager.getCSIv2Props()).get(ORBManager.ORB_SSL_CLIENT_REQUIRED); 130 if ( s != null && s.equals("true") ) { 131 sslRequired = true; 132 } 133 134 corbaIORDescSet = new HashSet (); 136 EjbIORConfigurationDescriptor iorDesc = 137 new EjbIORConfigurationDescriptor(); 138 EjbIORConfigurationDescriptor iorDesc2 = 139 new EjbIORConfigurationDescriptor(); 140 String serverSslReqd = (String )(ORBManager.getCSIv2Props()).get(ORBManager.ORB_SSL_SERVER_REQUIRED); 141 if ( serverSslReqd != null && serverSslReqd.equals("true") ) { 142 iorDesc.setIntegrity(EjbIORConfigurationDescriptor.REQUIRED); 143 iorDesc.setConfidentiality( 144 EjbIORConfigurationDescriptor.REQUIRED); 145 iorDesc2.setIntegrity(EjbIORConfigurationDescriptor.REQUIRED); 146 iorDesc2.setConfidentiality( 147 EjbIORConfigurationDescriptor.REQUIRED); 148 } 149 String clientAuthReq = 150 (String )(ORBManager.getCSIv2Props()).get(ORBManager.ORB_CLIENT_AUTH_REQUIRED); 151 if ( clientAuthReq != null && clientAuthReq.equals("true") ) { 152 iorDesc.setEstablishTrustInClient( 155 EjbIORConfigurationDescriptor.REQUIRED); 156 iorDesc2.setAuthMethodRequired(true); 158 corbaIORDescSet.add(iorDesc2); 159 } 160 corbaIORDescSet.add(iorDesc); 161 162 } catch(Exception e) { 163 _logger.log(Level.SEVERE,"iiop.Exception",e); 164 } 165 } 166 167 private CompoundSecMech mechanism = null; 168 private ORB orb = null; 169 private CSIV2TaggedComponentInfo ctc = null; 170 171 174 public SecurityMechanismSelector() { 175 this.orb = ORBManager.getORB(); 176 this.ctc = new CSIV2TaggedComponentInfo(orb); 177 } 178 179 public static ServerConnectionContext getServerConnectionContext() { 180 Hashtable h = ConnectionExecutionContext.getContext(); 181 ServerConnectionContext scc = 182 (ServerConnectionContext) h.get(SERVER_CONNECTION_CONTEXT); 183 return scc; 184 } 185 186 public static void setServerConnectionContext(ServerConnectionContext scc) { 187 Hashtable h = ConnectionExecutionContext.getContext(); 188 h.put(SERVER_CONNECTION_CONTEXT, scc); 189 } 190 191 public ConnectionContext getClientConnectionContext() { 192 Hashtable h = ConnectionExecutionContext.getContext(); 193 ConnectionContext scc = 194 (ConnectionContext) h.get(CLIENT_CONNECTION_CONTEXT); 195 return scc; 196 } 197 198 public void setClientConnectionContext(ConnectionContext scc) { 199 Hashtable h = ConnectionExecutionContext.getContext(); 200 h.put(CLIENT_CONNECTION_CONTEXT, scc); 201 } 202 203 209 public SocketInfo getSSLPort(IOR ior, ConnectionContext ctx) 210 { 211 SocketInfo info = null; 212 try { 213 mechanism = selectSecurityMechanism(ior); 214 } catch(SecurityMechanismException sme) { 215 throw new RuntimeException (sme.getMessage()); 216 } 217 ctx.setIOR(ior); 218 ctx.setMechanism(mechanism); 219 220 TLS_SEC_TRANS ssl = null; 221 if ( mechanism != null ) { 222 ssl = ctc.getSSLInformation(mechanism); 223 } 224 225 if (ssl == null) { 226 if (sslRequired) { 227 IIOPProfileTemplate templ = (IIOPProfileTemplate) 229 ior.getProfile().getTaggedProfileTemplate(); 230 IIOPAddress addr = templ.getPrimaryAddress(); 231 info = IORToSocketInfoImpl.createSocketInfo( 232 "SecurityMechanismSelector1", 233 "SSL", addr.getHost(), ORBManager.getORBInitialPort()); 234 return info; 235 } else { 236 return null; 237 } 238 } 239 240 int targetRequires = ssl.target_requires; 241 int targetSupports = ssl.target_supports; 242 243 247 if (isSet(targetRequires, Integrity.value) || 248 isSet(targetRequires, Confidentiality.value) || 249 isSet(targetRequires, EstablishTrustInClient.value)) { 250 if (_logger.isLoggable(Level.FINE)) { 251 _logger.log(Level.FINE, "Target requires SSL"); 252 } 253 ctx.setSSLUsed(true); 254 String type = "SSL"; 255 if(isSet(targetRequires, EstablishTrustInClient.value)) { 256 type = "SSL_MUTUALAUTH"; 257 ctx.setSSLClientAuthenticationOccurred(true); 258 } 259 short sslport = ssl.addresses[0].port; 260 int ssl_port = Utility.shortToInt(sslport); 261 String host_name = ssl.addresses[0].host_name; 262 263 info = IORToSocketInfoImpl.createSocketInfo( 264 "SecurityMechanismSelector2", 265 type, host_name, ssl_port); 266 267 return info; 268 } else if (isSet(targetSupports, Integrity.value) || 269 isSet(targetSupports, Confidentiality.value) || 270 isSet(targetSupports, EstablishTrustInClient.value)) { 271 if (_logger.isLoggable(Level.FINE)) { 272 _logger.log(Level.FINE, "Target supports SSL"); 273 } 274 275 if ( sslRequired ) { 276 if (_logger.isLoggable(Level.FINE)) { 277 _logger.log(Level.FINE, "Client is configured to require SSL for the target"); 278 } 279 280 ctx.setSSLUsed(true); 281 short sslport = ssl.addresses[0].port; 282 String host_name = ssl.addresses[0].host_name; 283 int ssl_port = Utility.shortToInt(sslport); 284 info = IORToSocketInfoImpl.createSocketInfo( 285 "SecurityMechanismSelector3", 286 "SSL", host_name, ssl_port); 287 return info; 288 } else { 289 return null; 290 } 291 } else if ( sslRequired ) { 292 throw new RuntimeException ("SSL required by client but not supported by server."); 293 } else { 294 return null; 295 } 296 } 297 298 public Logger get_logger() { 299 return _logger; 300 } 301 302 public void set_logger(Logger val) { 303 this._logger = val; 304 } 305 306 public String getCLIENT_CONNECTION_CONTEXT() { 307 return CLIENT_CONNECTION_CONTEXT; 308 } 309 310 public void setCLIENT_CONNECTION_CONTEXT(String val) { 311 this.CLIENT_CONNECTION_CONTEXT = val; 312 } 313 314 public String getSERVER_CONNECTION_CONTEXT() { 315 return SERVER_CONNECTION_CONTEXT; 316 } 317 318 public void setSERVER_CONNECTION_CONTEXT(String val) { 319 this.SERVER_CONNECTION_CONTEXT = val; 320 } 321 322 public Set getCorbaIORDescSet() { 323 return corbaIORDescSet; 324 } 325 326 public void setCorbaIORDescSet(Set val) { 327 this.corbaIORDescSet = val; 328 } 329 330 public boolean getSslRequired() { 331 return sslRequired; 332 } 333 334 public void setSslRequired(boolean val) { 335 this.sslRequired = val; 336 } 337 338 public String [] getServerTrustedHosts() { 339 return serverTrustedHosts; 340 } 341 342 public void setServerTrustedHosts(String [] val) { 343 this.serverTrustedHosts = val; 344 } 345 346 public LocalStringManagerImpl getLocalStrings() { 347 return localStrings; 348 } 349 350 public void setLocalStrings(LocalStringManagerImpl val) { 351 this.localStrings = val; 352 } 353 354 public POAProtocolMgr getProtocolMgr() { 355 return protocolMgr; 356 } 357 358 public void setProtocolMgr(POAProtocolMgr val) { 359 this.protocolMgr = val; 360 } 361 362 public CompoundSecMech getMechanism() { 363 return mechanism; 364 } 365 366 public void setMechanism(CompoundSecMech val) { 367 this.mechanism = val; 368 } 369 370 public ORB getOrb() { 371 return orb; 372 } 373 374 public void setOrb(ORB val) { 375 this.orb = val; 376 } 377 378 public CSIV2TaggedComponentInfo getCtc() { 379 return ctc; 380 } 381 382 public void setCtc(CSIV2TaggedComponentInfo val) { 383 this.ctc = val; 384 } 385 386 public java.util.List <SocketInfo> getSSLPorts(IOR ior, ConnectionContext ctx) 387 { 388 try { 389 mechanism = selectSecurityMechanism(ior); 390 } catch(SecurityMechanismException sme) { 391 throw new RuntimeException (sme.getMessage()); 392 } 393 ctx.setIOR(ior); 394 ctx.setMechanism(mechanism); 395 396 TLS_SEC_TRANS ssl = null; 397 if ( mechanism != null ) { 398 ssl = ctc.getSSLInformation(mechanism); 399 } 400 401 if (ssl == null) { 402 if (sslRequired) { 403 IIOPProfileTemplate templ = (IIOPProfileTemplate) 405 ior.getProfile().getTaggedProfileTemplate(); 406 IIOPAddress addr = templ.getPrimaryAddress(); 407 SocketInfo info = IORToSocketInfoImpl.createSocketInfo( 408 "SecurityMechanismSelector1", 409 "SSL", addr.getHost(), ORBManager.getORBInitialPort()); 410 List <SocketInfo> sInfos = new ArrayList <SocketInfo>(); 412 sInfos.add(info); 413 return sInfos; 414 } else { 415 return null; 416 } 417 } 418 419 int targetRequires = ssl.target_requires; 420 int targetSupports = ssl.target_supports; 421 422 426 if (isSet(targetRequires, Integrity.value) || 427 isSet(targetRequires, Confidentiality.value) || 428 isSet(targetRequires, EstablishTrustInClient.value)) { 429 if (_logger.isLoggable(Level.FINE)) { 430 _logger.log(Level.FINE, "Target requires SSL"); 431 } 432 ctx.setSSLUsed(true); 433 String type = "SSL"; 434 if(isSet(targetRequires, EstablishTrustInClient.value)) { 435 type = "SSL_MUTUALAUTH"; 436 ctx.setSSLClientAuthenticationOccurred(true); 437 } 438 List <SocketInfo> socketInfos = new ArrayList <SocketInfo>(); 440 for(int addressIndex =0; addressIndex < ssl.addresses.length; addressIndex++){ 441 short sslport = ssl.addresses[addressIndex].port; 442 int ssl_port = Utility.shortToInt(sslport); 443 String host_name = ssl.addresses[addressIndex].host_name; 444 445 SocketInfo sInfo = IORToSocketInfoImpl.createSocketInfo( 446 "SecurityMechanismSelector2", 447 type, host_name, ssl_port); 448 socketInfos.add(sInfo); 449 } 450 return socketInfos; 451 } else if (isSet(targetSupports, Integrity.value) || 452 isSet(targetSupports, Confidentiality.value) || 453 isSet(targetSupports, EstablishTrustInClient.value)) { 454 if (_logger.isLoggable(Level.FINE)) { 455 _logger.log(Level.FINE, "Target supports SSL"); 456 } 457 458 if ( sslRequired ) { 459 if (_logger.isLoggable(Level.FINE)) { 460 _logger.log(Level.FINE, "Client is configured to require SSL for the target"); 461 } 462 463 ctx.setSSLUsed(true); 464 List <SocketInfo> socketInfos = new ArrayList <SocketInfo>(); 466 for(int addressIndex =0; addressIndex < ssl.addresses.length; addressIndex++){ 467 short sslport = ssl.addresses[addressIndex].port; 468 int ssl_port = Utility.shortToInt(sslport); 469 String host_name = ssl.addresses[addressIndex].host_name; 470 471 SocketInfo sInfo = IORToSocketInfoImpl.createSocketInfo( 472 "SecurityMechanismSelector3", 473 "SSL", host_name, ssl_port); 474 socketInfos.add(sInfo); 475 } 476 return socketInfos; 477 } else { 478 return null; 479 } 480 } else if ( sslRequired ) { 481 throw new RuntimeException ("SSL required by client but not supported by server."); 482 } else { 483 return null; 484 } 485 } 486 487 488 493 494 public SecurityContext selectSecurityContext(IOR ior) 495 throws InvalidIdentityTokenException, 496 InvalidMechanismException, SecurityMechanismException 497 { 498 SecurityContext context = null; 499 ConnectionContext cc = getClientConnectionContext(); 500 if(cc == null) { 501 return null; 502 } 503 mechanism = cc.getMechanism(); 504 if(mechanism == null) { 505 return null; 506 } 507 boolean sslUsed = cc.getSSLUsed(); 508 boolean clientAuthOccurred = cc.getSSLClientAuthenticationOccurred(); 509 InvocationManager im = Switch.getSwitch().getInvocationManager(); 510 if(im == null) { 511 context = getSecurityContextForAppClient(null, sslUsed, 513 clientAuthOccurred); 514 return context; 515 } 516 517 if (_logger.isLoggable(Level.FINE)) { 518 _logger.log(Level.FINE, "SSL used:" + sslUsed + " SSL Mutual auth:" + clientAuthOccurred); 519 } 520 521 ComponentInvocation ci = null; 522 ci = im.getCurrentInvocation(); 524 if (ci == null) { 525 return null; 527 } 528 Object obj = ci.getContainerContext(); 529 if(obj instanceof AppContainer) { 530 context = getSecurityContextForAppClient(ci, sslUsed, clientAuthOccurred); 531 } else { 532 context = getSecurityContextForWebOrEJB(ci, sslUsed, clientAuthOccurred); 533 } 534 return context; 535 } 536 537 543 public SecurityContext getSecurityContextForAppClient( 544 ComponentInvocation ci, 545 boolean sslUsed, 546 boolean clientAuthOccurred) 547 throws InvalidMechanismException, InvalidIdentityTokenException, 548 SecurityMechanismException { 549 550 return sendUsernameAndPassword(ci, sslUsed, clientAuthOccurred); 551 } 552 553 559 public SecurityContext getSecurityContextForWebOrEJB( 560 ComponentInvocation ci, boolean sslUsed, 561 boolean clientAuthOccurred) 562 throws InvalidMechanismException, InvalidIdentityTokenException, 563 SecurityMechanismException { 564 565 SecurityContext ctx = null; 566 if(!sslUsed) { 567 ctx = propagateIdentity(false, ci); 568 } else { 569 ctx = propagateIdentity(clientAuthOccurred, ci); 570 } 571 return ctx; 572 } 573 574 private boolean isMechanismSupported(SAS_ContextSec sas){ 575 byte[][] mechanisms = sas.supported_naming_mechanisms; 576 byte[] mechSupported = {}; 577 try{ 578 mechSupported = GSSUtils.getDER(GSSUtils.GSSUP_MECH_OID); 579 } catch (Exception e){ 580 return false; 583 } 584 if (mechanisms[0].length != mechSupported.length) 585 return false; 586 for (int i=0; i<mechanisms[0].length; i++){ 587 if (mechanisms[0][i] != mechSupported[i]) 588 return false; 589 } 590 return true; 591 } 592 public boolean isIdentityTypeSupported(SAS_ContextSec sas){ 593 int ident_token = sas.supported_identity_types; 594 int value = ident_token & 596 CSIV2TaggedComponentInfo.SUPPORTED_IDENTITY_TOKEN_TYPES; 597 if (value != 0) 598 return true; 599 else 600 return false; 601 } 602 603 611 private SecurityContext sendUsernameAndPassword(ComponentInvocation ci, 612 boolean sslUsed, 613 boolean clientAuthOccurred) 614 throws SecurityMechanismException { 615 SecurityContext ctx = null; 616 if(mechanism == null) { 617 return null; 618 } 619 AS_ContextSec asContext = mechanism.as_context_mech; 620 if( isSet(asContext.target_requires, EstablishTrustInClient.value) 621 || ( isSet(mechanism.target_requires, EstablishTrustInClient.value) 622 && !clientAuthOccurred ) ) { 623 624 ctx = getUsernameAndPassword(ci); 625 626 if (_logger.isLoggable(Level.FINE)) { 627 _logger.log(Level.FINE, "Sending Username/Password"); 628 } 629 } else { 630 return null; 631 } 632 return ctx; 633 } 634 635 642 private SecurityContext propagateIdentity(boolean clientAuth, 643 ComponentInvocation ci) 644 throws InvalidIdentityTokenException, InvalidMechanismException, 645 SecurityMechanismException { 646 647 SecurityContext ctx = null; 648 if(mechanism == null) { 649 return null; 650 } 651 AS_ContextSec asContext = mechanism.as_context_mech; 652 SAS_ContextSec sasContext = mechanism.sas_context_mech; 653 if (_logger.isLoggable(Level.FINE)) { 654 _logger.log(Level.FINE, "SAS CONTEXT's target_requires=" + sasContext.target_requires); 655 _logger.log(Level.FINE, "SAS CONTEXT's target_supports=" + sasContext.target_supports); 656 } 657 658 if(isSet(asContext.target_requires, EstablishTrustInClient.value)) { 659 ctx = getUsernameAndPassword(ci); 660 if (ctx.authcls == null){ String errmsg = 662 localStrings.getLocalString("securitymechansimselector.runas_cannot_propagate_username_password", 663 "Cannot propagate username/password required by target when using run as identity"); 664 665 _logger.log(Level.SEVERE,"iiop.runas_error",errmsg); 666 throw new SecurityMechanismException (errmsg); 667 } 668 } else if(isSet(sasContext.target_supports, IdentityAssertion.value) || 669 isSet(sasContext.target_requires, IdentityAssertion.value)) { 670 if(!isIdentityTypeSupported(sasContext)){ 673 String errmsg = 674 localStrings.getLocalString("securitymechanismselector.invalid_identity_type", 675 "The given identity token is unsupported."); 676 throw new InvalidIdentityTokenException(errmsg); 677 } 678 if (sasContext.target_supports == IdentityAssertion.value){ 679 if(!isMechanismSupported(sasContext)){ 680 String errmsg = 681 localStrings.getLocalString("securitymechanismselector.invalid_mechanism", 682 "The given mechanism type is unsupported."); 683 _logger.log(Level.SEVERE,"iiop.unsupported_type_error",errmsg); 684 throw new InvalidMechanismException(errmsg); 685 } 686 } 687 688 ctx = getIdentity(); 690 } else if(isSet(asContext.target_supports, 691 EstablishTrustInClient.value)) { 692 if (clientAuth) { ctx = getUsernameAndPassword(ci); 694 if (ctx.authcls == null) { 695 return null; } 698 } else { return null; 700 } 701 } else{ 702 return null; } 704 return ctx; 705 } 706 707 708 713 private boolean isServerTrusted() { 714 String star = "*"; 715 for (int i = 0; i < serverTrustedHosts.length; i++){ 719 if (serverTrustedHosts[i].length () == 1) { 720 if (serverTrustedHosts[i].equals (star)) 721 return true; 722 } 723 } 724 ConnectionContext scc = getClientConnectionContext (); 725 if (scc != null){ 726 Socket skt = scc.getSocket (); 727 InetAddress adr = skt.getInetAddress (); 728 return isDomainInTrustedList (adr, serverTrustedHosts); 731 } 732 return false; 733 734 } 735 736 745 private boolean isDomainInTrustedList (InetAddress inetAddress, 746 String [] trusted) 747 throws SecurityException 748 { 749 boolean isTrusted = false; 750 String domain = null; 751 String star = "*"; 752 String dot = "."; 753 try{ 755 domain = inetAddress.getHostName (); 756 } catch (Exception e){ 757 _logger.log(Level.SEVERE,"iiop.domain_lookup_failed",inetAddress.getHostAddress ()); 758 return false; 759 } 760 if (_logger.isLoggable(Level.FINE)) { 761 _logger.log(Level.FINE, " Verifying if domain address ="+ 762 inetAddress.toString () + " is in the Trusted list "); 763 _logger.log(Level.FINE, " the domain name is = "+ domain); 764 } 765 766 String [] domainTok = TypeUtil.stringToArray (domain, dot); 767 for (int i=0; i< trusted.length; i++){ 770 String [] toksList = 772 TypeUtil.stringToArray (trusted[i], dot); 773 if (toksList.length != domainTok.length){ 775 isTrusted = false; 776 continue; 777 } else{ 778 for (int j=toksList.length-1; j>=0 ; j--){ 779 if (toksList[j].equals (domainTok[j])){ 782 isTrusted = true; 783 } else { 784 if (toksList[j].equals (star)){ 786 isTrusted = true; 787 } 788 else { 789 isTrusted = false; 791 break; 792 } 793 } 794 } 795 if (isTrusted) 798 return isTrusted; 799 } 800 } 801 return isTrusted; 802 } 803 804 805 812 private SecurityContext getUsernameAndPassword(ComponentInvocation ci) 813 throws SecurityMechanismException { 814 try { 815 Subject s = null; 816 if(ci == null) { 817 com.sun.enterprise.security.ClientSecurityContext sc = 820 com.sun.enterprise.security.ClientSecurityContext.getCurrent(); 821 if(sc == null) { 822 return null; 823 } 824 s = sc.getSubject(); 825 if (_logger.isLoggable(Level.FINE)) { 826 _logger.log(Level.FINE, "SUBJECT:" + s); 827 } 828 } else { 829 Object obj = ci.getContainerContext(); 830 if(obj instanceof AppContainer) { 831 com.sun.enterprise.security.ClientSecurityContext sc = 833 com.sun.enterprise.security.ClientSecurityContext.getCurrent(); 834 if(sc == null) { 835 s = LoginContextDriver.doClientLogin( 836 AppContainer.USERNAME_PASSWORD, 837 AppContainer.getCallbackHandler()); 838 } else { 839 s = sc.getSubject(); 840 } 841 } else { 842 s = getSubjectFromSecurityCurrent(); 844 } 847 } 848 SecurityContext ctx = new SecurityContext(); 849 final Subject sub = s; 850 ctx.subject = s; 851 Set privateCredSet = 853 (Set ) AccessController.doPrivileged(new PrivilegedAction () { 854 public java.lang.Object run() { 855 return sub.getPrivateCredentials(); 856 } 857 }); 858 if (privateCredSet.isEmpty()) { if (_logger.isLoggable(Level.FINE)) { 860 _logger.log(Level.FINE, "no private credential run as mode"); 861 } 862 ctx.authcls = null; ctx.identcls = GSSUPName.class; 864 865 } else{ 866 869 AS_ContextSec asContext = mechanism.as_context_mech; 870 final byte[] target_name = asContext.target_name; 871 byte[] _realm = GSSUtils.importName(GSSUtils.GSSUP_MECH_OID, target_name); 872 final String realm_name = new String (_realm); 873 final Iterator it = privateCredSet.iterator(); 874 for(;it.hasNext();){ 875 AccessController.doPrivileged(new PrivilegedAction (){ 876 public java.lang.Object run(){ 877 PasswordCredential pc = (PasswordCredential) it.next(); 878 pc.setRealm(realm_name); 879 return null; 880 } 881 }); 882 } 883 ctx.authcls = PasswordCredential.class; 884 } 885 return ctx; 886 } catch(Exception e) { 887 _logger.log(Level.SEVERE,"iiop.user_password_exception",e); 888 return null; 889 } 890 } 891 892 896 private SecurityContext getIdentity() 897 throws SecurityMechanismException { 898 if (_logger.isLoggable(Level.FINE)) { 899 _logger.log(Level.FINE, "Getting PRINCIPAL/DN from TLS"); 900 } 901 902 SecurityContext ctx = new SecurityContext(); 903 final SecurityContext sCtx = ctx; 904 com.sun.enterprise.security.SecurityContext scontext = 906 com.sun.enterprise.security.SecurityContext.getCurrent(); 907 if ((scontext == null) || 908 scontext.didServerGenerateCredentials()){ 909 910 sCtx.identcls = AnonCredential.class; 912 913 AccessController.doPrivileged(new PrivilegedAction () { 914 public java.lang.Object run(){ 915 Subject sub = new Subject (); 917 sCtx.subject = sub; 918 sCtx.subject.getPublicCredentials().add(new AnonCredential()); 919 return null; 920 } 921 }); 922 return sCtx; 923 } 924 925 Subject s = getSubjectFromSecurityCurrent(); 926 ctx.subject = s; 927 928 final Subject sub = s; 930 Set credSet = 931 (Set ) AccessController.doPrivileged(new PrivilegedAction () { 932 public java.lang.Object run() { 933 return sub.getPrivateCredentials(PasswordCredential.class); 934 } 935 }); 936 if(credSet.size() == 1) { 937 ctx.identcls = GSSUPName.class; 938 final Set cs = credSet; 939 Subject subj = (Subject ) AccessController.doPrivileged(new 940 PrivilegedAction () { 941 public java.lang.Object run() { 942 Subject ss = new Subject (); 943 Iterator iter = cs.iterator(); 944 PasswordCredential pc = (PasswordCredential) iter.next(); 945 GSSUPName gssname = new GSSUPName(pc.getUser(), pc.getRealm()); 946 ss.getPublicCredentials().add(gssname); 947 return ss; 948 } 949 }); 950 ctx.subject = subj; 951 return ctx; 952 } 953 954 credSet = s.getPublicCredentials(); 955 if(credSet.size() != 1) { 956 _logger.log(Level.SEVERE,"iiop.principal_error"); 957 return null; 958 } else { 959 Iterator credIter = credSet.iterator(); 960 if(credIter.hasNext()) { 961 Object o = credIter.next(); 962 if(o instanceof GSSUPName) { 963 ctx.identcls = GSSUPName.class; 964 } else if(o instanceof X500Name) { 965 ctx.identcls = X500Name.class; 966 } else { 967 ctx.identcls = X509CertificateCredential.class; 968 } 969 } else { 970 _logger.log(Level.SEVERE,"iiop.credential_error"); 971 return null; 972 } 973 } 974 return ctx; 975 } 976 977 private Subject getSubjectFromSecurityCurrent() 978 throws SecurityMechanismException { 979 com.sun.enterprise.security.SecurityContext sc = null; 980 sc = com.sun.enterprise.security.SecurityContext.getCurrent(); 981 if(sc == null) { 982 if(_logger.isLoggable(Level.FINE)) { 983 _logger.log(Level.FINE," SETTING GUEST ---"); 984 } 985 sc = com.sun.enterprise.security.SecurityContext.init(); 986 } 987 if(sc == null) { 988 throw new SecurityMechanismException("Could not find " + 989 " security information"); 990 } 991 Subject s = sc.getSubject(); 992 if(s == null) { 993 throw new SecurityMechanismException("Could not find " + 994 " subject information in the security context."); 995 } 996 if (_logger.isLoggable(Level.FINE)) { 997 _logger.log(Level.FINE, "Subject in security current:" + s); 998 } 999 return s; 1000 } 1001 1002 public CompoundSecMech selectSecurityMechanism(IOR ior) 1003 throws SecurityMechanismException { 1004 CompoundSecMech[] mechList = ctc.getSecurityMechanisms(ior); 1005 CompoundSecMech mech = selectSecurityMechanism(mechList); 1006 return mech; 1007 } 1008 1009 1013 private CompoundSecMech selectSecurityMechanism( 1014 CompoundSecMech[] mechList) throws SecurityMechanismException { 1015 if(mechList == null || mechList.length == 0) { 1019 return null; 1020 } 1021 CompoundSecMech mech = null; 1022 for(int i = 0; i < mechList.length; i++) { 1023 mech = mechList[i]; 1024 boolean useMech = useMechanism(mech); 1025 if(useMech) { 1026 return mech; 1027 } 1028 } 1029 throw new SecurityMechanismException("Cannot use any of the " + 1030 " target's supported mechanisms"); 1031 } 1032 1033 private boolean useMechanism(CompoundSecMech mech) { 1034 boolean val = true; 1035 TLS_SEC_TRANS tls = ctc.getSSLInformation(mech); 1036 1037 if(tls == null) { 1038 return true; 1039 } 1040 int targetRequires = tls.target_requires; 1041 if(isSet(targetRequires, EstablishTrustInClient.value)) { 1042 if(! SSLUtils.isKeyAvailable()) { 1043 val = false; 1044 } 1045 } 1046 return val; 1047 } 1048 1049 1052 private byte[] getTargetName(Subject subj) 1053 { 1054 1055 byte[] tgt_name = {} ; 1056 1057 final Subject sub = subj; 1058 final Set credset = 1059 (Set ) AccessController.doPrivileged(new PrivilegedAction () { 1060 public java.lang.Object run() { 1061 return sub.getPrivateCredentials(PasswordCredential.class); 1062 } 1063 }); 1064 if(credset.size() == 1) { 1065 tgt_name = (byte[]) AccessController.doPrivileged(new PrivilegedAction () { 1066 public java.lang.Object run() { 1067 Iterator iter = credset.iterator(); 1068 PasswordCredential pc = (PasswordCredential) iter.next(); 1069 return pc.getTargetName(); 1070 } 1071 }); 1072 } 1073 return tgt_name; 1074 } 1075 1076 private boolean evaluate_client_conformance_ssl( 1077 EjbIORConfigurationDescriptor iordesc, 1078 boolean ssl_used, 1079 X509Certificate [] certchain) 1080 { 1081 try { 1082 if(_logger.isLoggable(Level.FINE)) { 1083 _logger.log(Level.FINE, 1084 "SecurityMechanismSelector.evaluate_client_conformance_ssl->:"); 1085 } 1086 1087 boolean ssl_required = false; 1088 boolean ssl_supported = false; 1089 int ssl_target_requires = 0; 1090 int ssl_target_supports = 0; 1091 1092 1106 1107 1109 ssl_target_requires = this.ctc.getTargetRequires(iordesc); 1110 ssl_target_supports = this.ctc.getTargetSupports(iordesc); 1111 1112 if ( isSet(ssl_target_requires, Integrity.value) 1113 || isSet(ssl_target_requires, Confidentiality.value) 1114 || isSet(ssl_target_requires, EstablishTrustInClient.value)) 1115 ssl_required = true; 1116 else 1117 ssl_required = false; 1118 1119 if ( ssl_target_supports != 0) 1120 ssl_supported = true; 1121 else 1122 ssl_supported = false; 1123 1124 1132 if(_logger.isLoggable(Level.FINE)) { 1133 _logger.log(Level.FINE, 1134 "SecurityMechanismSelector.evaluate_client_conformance_ssl:" 1135 + " " + isSet(ssl_target_requires, Integrity.value) 1136 + " " + isSet(ssl_target_requires, Confidentiality.value) 1137 + " " + isSet(ssl_target_requires, EstablishTrustInClient.value) 1138 + " " + ssl_required 1139 + " " + ssl_supported 1140 + " " + ssl_used); 1141 } 1142 1143 if (ssl_used) { 1144 if (! (ssl_required || ssl_supported)) 1145 return false; } else { 1147 if (ssl_required) 1148 return false; } 1150 1151 1162 1163 if(_logger.isLoggable(Level.FINE)) { 1164 _logger.log(Level.FINE, 1165 "SecurityMechanismSelector.evaluate_client_conformance_ssl:" 1166 + " " + certchain 1167 + " " + isSet(ssl_target_requires, EstablishTrustInClient.value) 1168 + " " + isSet(ssl_target_supports, EstablishTrustInClient.value)); 1169 } 1170 1171 if (certchain != null) { 1172 if ( ! ( isSet(ssl_target_requires, EstablishTrustInClient.value) 1173 || isSet(ssl_target_supports, EstablishTrustInClient.value))) 1174 return false; } else { 1176 if (isSet(ssl_target_requires, EstablishTrustInClient.value)) 1177 return false; } 1179 1180 if(_logger.isLoggable(Level.FINE)) { 1181 _logger.log(Level.FINE, 1182 "SecurityMechanismSelector.evaluate_client_conformance_ssl: true"); 1183 } 1184 1185 return true ; } finally { 1187 if(_logger.isLoggable(Level.FINE)) { 1188 _logger.log(Level.FINE, 1189 "SecurityMechanismSelector.evaluate_client_conformance_ssl<-:"); 1190 } 1191 } 1192 } 1193 1194 1195 1200 private boolean evaluate_client_conformance_ascontext( 1201 SecurityContext ctx, 1202 EjbIORConfigurationDescriptor iordesc) 1203 { 1204 1205 boolean client_authenticated = false; 1206 1207 AS_ContextSec ascontext = null; 1209 try { 1210 ascontext = this.ctc.createASContextSec(iordesc); 1211 } catch (Exception e) { 1212 _logger.log(Level.SEVERE, "iiop.createcontextsec_exception",e); 1213 1214 return false; 1215 } 1216 1217 if (ascontext == null) 1220 return false; 1221 1222 1238 1239 if ( (ctx != null) && (ctx.authcls != null) && (ctx.subject != null)) 1240 client_authenticated = true; 1241 else 1242 client_authenticated = false; 1243 1244 if (client_authenticated) { 1245 if ( ! ( isSet(ascontext.target_requires, EstablishTrustInClient.value) 1246 || isSet(ascontext.target_supports, EstablishTrustInClient.value))){ 1247 return false; } 1249 1251 byte [] client_tgtname = getTargetName(ctx.subject); 1252 1253 if (ascontext.target_name.length != client_tgtname.length){ 1254 return false; } 1256 for (int i=0; i < ascontext.target_name.length ; i ++) 1257 if (ascontext.target_name[i] != client_tgtname[i]){ 1258 return false; } 1260 } else { 1261 if ( isSet(ascontext.target_requires, EstablishTrustInClient.value)){ 1262 return false; } 1264 } 1265 return true; 1266 } 1267 1268 1274 private boolean evaluate_client_conformance_sascontext( 1275 SecurityContext ctx, 1276 EjbIORConfigurationDescriptor iordesc) 1277 { 1278 1279 boolean caller_propagated = false; 1280 1281 SAS_ContextSec sascontext = null; 1283 try { 1284 sascontext = this.ctc.createSASContextSec(iordesc); 1285 } catch (Exception e) { 1286 _logger.log(Level.SEVERE,"iiop.createcontextsec_exception",e); 1287 return false; 1288 } 1289 1290 if (sascontext == null) return false; 1292 1293 if ( (ctx != null) && (ctx.identcls != null) && (ctx.subject != null)) 1294 caller_propagated = true; 1295 else 1296 caller_propagated = false; 1297 1298 if (caller_propagated) { 1299 if ( ! isSet(sascontext.target_supports, IdentityAssertion.value)) 1300 return false; 1302 1311 return true; 1312 } 1313 return true; } 1315 1316 1317 1318 1334 private boolean evaluate_client_conformance(SecurityContext ctx, 1335 byte[] object_id, 1336 boolean ssl_used, 1337 X509Certificate [] certchain) 1338 { 1339 1342 1345 if (object_id == null) 1346 return true; 1347 1348 if (protocolMgr == null) 1349 protocolMgr = (POAProtocolMgr)Switch.getSwitch().getProtocolManager(); 1350 1351 if (protocolMgr == null) 1355 return true; 1356 1357 EjbDescriptor ejbDesc = protocolMgr.getEjbDescriptor(object_id); 1358 1359 Set iorDescSet = null; 1360 if (ejbDesc != null) { 1361 iorDescSet = ejbDesc.getIORConfigurationDescriptors(); 1362 } 1363 else { 1364 iorDescSet = corbaIORDescSet; 1367 } 1368 1369 if(_logger.isLoggable(Level.FINE)) { 1370 _logger.log(Level.FINE, 1371 "SecurityMechanismSelector.evaluate_client_conformance: iorDescSet: " + iorDescSet); 1372 } 1373 1374 1378 if (iorDescSet.isEmpty()) 1379 return true; 1380 1381 boolean checkSkipped = false; 1384 for (Iterator itr = iorDescSet.iterator(); itr.hasNext();) { 1385 EjbIORConfigurationDescriptor iorDesc = 1386 (EjbIORConfigurationDescriptor) itr.next(); 1387 if(skip_client_conformance(iorDesc)){ 1388 if(_logger.isLoggable(Level.FINE)) { 1389 _logger.log(Level.FINE, 1390 "SecurityMechanismSelector.evaluate_client_conformance: skip_client_conformance"); 1391 } 1392 checkSkipped = true; 1393 continue; 1394 } 1395 if (! evaluate_client_conformance_ssl(iorDesc, ssl_used, certchain)){ 1396 if(_logger.isLoggable(Level.FINE)) { 1397 _logger.log(Level.FINE, 1398 "SecurityMechanismSelector.evaluate_client_conformance: evaluate_client_conformance_ssl"); 1399 } 1400 checkSkipped = false; 1401 continue; 1402 } 1403 if ( ! evaluate_client_conformance_ascontext(ctx, iorDesc)){ 1404 if(_logger.isLoggable(Level.FINE)) { 1405 _logger.log(Level.FINE, 1406 "SecurityMechanismSelector.evaluate_client_conformance: evaluate_client_conformance_ascontext"); 1407 } 1408 checkSkipped = false; 1409 continue; 1410 } 1411 if ( ! evaluate_client_conformance_sascontext(ctx, iorDesc)){ 1412 if(_logger.isLoggable(Level.FINE)) { 1413 _logger.log(Level.FINE, 1414 "SecurityMechanismSelector.evaluate_client_conformance: evaluate_client_conformance_sascontext"); 1415 } 1416 checkSkipped = false; 1417 continue; 1418 } 1419 return true; } 1421 if(checkSkipped) 1422 return true; 1423 return false; } 1425 1426 1428 private boolean skip_client_conformance (EjbIORConfigurationDescriptor ior){ 1429 String none = ior.NONE; 1430 if(ior == null){ 1432 return false; 1433 } 1434 if(!none.equalsIgnoreCase(ior.getIntegrity())){ 1436 return false; 1437 } 1438 if(!none.equalsIgnoreCase(ior.getConfidentiality())){ 1439 return false; 1440 } 1441 if(!none.equalsIgnoreCase(ior.getEstablishTrustInClient())){ 1442 return false; 1443 } 1444 if(ior.isAuthMethodRequired()){ 1446 return false; 1447 } 1448 if(!none.equalsIgnoreCase(ior.getCallerPropagation())){ 1450 return false; 1451 } 1452 return true; 1453 } 1454 1457 public SecurityContext evaluateTrust(SecurityContext ctx, byte[] object_id) 1458 throws SecurityMechanismException 1459 { 1460 SecurityContext ssc = null; 1461 Socket socket = null; 1462 1463 boolean ssl_used = false ; 1465 1466 1469 X509Certificate [] certChain = null ; 1470 1471 1476 ServerConnectionContext scc = getServerConnectionContext(); 1477 1478 if(scc != null) { 1479 socket = scc.getSocket(); 1480 if ((socket != null) && (socket instanceof SSLSocket)) { 1481 ssl_used = true; SSLSocket sslSock = (SSLSocket) socket; 1484 SSLSession sslSession = sslSock.getSession(); 1485 try { 1486 certChain = (X509Certificate [])sslSession.getPeerCertificates(); 1487 } catch(Exception e) { 1488 if (_logger.isLoggable(Level.FINE)) { 1489 _logger.log(Level.FINE, "iiop.cannot_get_peercert", e); 1490 } 1491 } 1492 } 1493 } 1494 1495 if (socket == null && ctx == null) 1499 return null; 1500 1501 if ( evaluate_client_conformance(ctx, object_id, ssl_used, certChain) 1502 == false) { 1503 String msg = "Trust evaluation failed because "; 1504 msg = msg + "client does not conform to configured security policies"; 1505 throw new SecurityMechanismException(msg); 1506 } 1507 1508 if ( ctx == null ) { 1509 if ( socket == null || !ssl_used || certChain == null ) { 1510 return null; 1517 } else { 1518 ssc = new SecurityContext(); 1521 X500Name x500Name = (X500Name) certChain[0].getSubjectDN(); 1522 ssc.subject = new Subject (); 1523 ssc.subject.getPublicCredentials().add(x500Name); 1524 ssc.identcls = X500Name.class; 1525 ssc.authcls = null; 1526 return ssc; 1527 } 1528 } else { 1529 ssc = ctx; 1530 } 1531 1532 Class authCls = ctx.authcls; 1533 Class identCls = ctx.identcls; 1534 ssc.subject = ctx.subject; 1535 1536 ssc.authcls = null; 1537 ssc.identcls = null; 1538 1539 if (identCls != null) 1540 ssc.identcls = identCls; 1541 else if (authCls != null) 1542 ssc.authcls = authCls; 1543 else 1544 ssc.identcls = AnonCredential.class; 1545 1546 return ssc; 1547 } 1548 1549 private boolean isSet(int val1, int val2) { 1550 if((val1 & val2) == val2) { 1551 return true; 1552 } 1553 return false; 1554 } 1555} 1556 1557 1558 1574class ConnectionExecutionContext { 1575 private static InheritableThreadLocal connCurrent= new InheritableThreadLocal (); 1576 1577 1584 public static void setContext(Hashtable ctxTable) { 1585 if (ctxTable != null) { 1586 connCurrent.set(ctxTable); 1587 } else { 1588 connCurrent.set(new Hashtable ()); 1589 } 1590 } 1591 1592 1598 public static Hashtable getContext() { 1599 if (connCurrent.get() == null) { 1600 setContext(null); } 1602 return (Hashtable ) connCurrent.get(); 1603 } 1604 1605 public InheritableThreadLocal getConnCurrent() { 1606 return connCurrent; 1607 } 1608 1609 public void setConnCurrent(InheritableThreadLocal val) { 1610 this.connCurrent = val; 1611 } 1612 1613} 1614 1615 | Popular Tags |