1 23 24 package com.sun.enterprise.iiop.security; 25 26 import org.omg.CORBA.*; 27 import org.omg.PortableInterceptor.*; 28 import org.omg.IOP.*; 29 import java.util.*; 30 import java.security.AccessController ; 31 import java.security.PrivilegedAction ; 32 import java.security.cert.X509Certificate ; 33 34 35 import com.sun.corba.ee.org.omg.CSI.*; 36 import com.sun.corba.ee.org.omg.GSSUP.*; 37 38 39 import sun.security.util.DerInputStream; 40 import sun.security.util.DerOutputStream; 41 import sun.security.util.DerValue; 42 43 import sun.security.x509.*; import javax.security.auth.*; 46 import com.sun.enterprise.security.auth.login.PasswordCredential; 47 import com.sun.enterprise.security.auth.login.X509CertificateCredential; 48 import com.sun.enterprise.util.ORBManager; 49 import com.sun.enterprise.util.LocalStringManagerImpl; 50 import com.sun.enterprise.iiop.CSIV2TaggedComponentInfo; 51 import java.util.logging.*; 52 import com.sun.logging.*; 53 54 61 62 public class SecClientRequestInterceptor 63 extends org.omg.CORBA.LocalObject 64 implements ClientRequestInterceptor { 65 private static java.util.logging.Logger _logger=null; 66 static{ 67 _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER); 68 } 69 70 private static LocalStringManagerImpl localStrings = 71 new LocalStringManagerImpl(SecClientRequestInterceptor.class); 72 73 private String name; 75 80 private String prname; 81 private Codec codec; private ORB orb; 83 private SecurityService secsvc; 84 85 90 protected static final int SECURITY_ATTRIBUTE_SERVICE_ID = 15; 91 92 public SecClientRequestInterceptor(String name, Codec codec) { 93 this.name = name; 94 this.codec = codec; 95 this.prname = name + "::"; 96 } 97 98 public String name() { 99 return name; 100 } 101 102 108 private java.lang.Object getCred(Set credset, Class c) { 109 110 java.lang.Object cred = null ; String clsname = c.getName() ; 112 113 114 if(_logger.isLoggable(Level.FINE)) { 115 _logger.log(Level.FINE,"Checking for a single instance of class in subject"); 116 _logger.log(Level.FINE," Classname = " + clsname); 117 } 118 if (credset.size() != 1) { 119 if(_logger.isLoggable(Level.SEVERE)) 120 _logger.log(Level.SEVERE,"iiop.multiple_credset",new java.lang.Object []{new Integer (credset.size()),clsname}); 121 throw new SecurityException ( 122 localStrings.getLocalString("secclientreqinterceptor.inv_credlist_size", 123 "Credential list size is not 1.")); 124 } 125 126 Iterator iter = credset.iterator(); 127 while (iter.hasNext()) 128 cred = iter.next(); 129 if(_logger.isLoggable(Level.FINE)) 130 _logger.log(Level.FINE,"Verified single instance of class ( " +clsname + " )"); 131 return cred; 132 } 133 134 139 140 private byte[] createAuthToken(java.lang.Object cred, Class cls) 141 throws Exception 142 { 143 byte[] gsstoken = {}; 145 if (PasswordCredential.class.isAssignableFrom(cls)) { 146 147 _logger.log(Level.FINE,"Constructing a PasswordCredential client auth token"); 148 149 150 PasswordCredential pwdcred = (PasswordCredential) cred; 151 GSSUPToken tok = GSSUPToken.getClientSideInstance(orb, codec, pwdcred); 152 gsstoken = tok.getGSSToken(); 153 } 154 return gsstoken; 155 } 156 157 161 private IdentityToken createIdToken(java.lang.Object cred, Class cls) 162 throws Exception { 163 164 IdentityToken idtok = null; 165 166 DerOutputStream dos = new DerOutputStream(); 167 DerValue[] derval = {} ; byte[] cdrval ; Any any = orb.create_any(); 170 idtok = new IdentityToken(); 171 172 if (X500Name.class.isAssignableFrom(cls)) { 173 _logger.log(Level.FINE,"Constructing an X500 DN Identity Token"); 174 X500Name name = (X500Name) cred; 175 name.encode(dos); X501DistinguishedNameHelper.insert(any, dos.toByteArray()); 177 178 179 idtok.dn(codec.encode_value(any)); 180 } else if (X509CertificateCredential.class.isAssignableFrom(cls)) { 181 _logger.log(Level.FINE,"Constructing an X509 Certificate Chain Identity Token"); 182 183 X509CertificateCredential certcred = (X509CertificateCredential) cred; 184 X509Certificate [] certchain = certcred.getX509CertificateChain(); 185 _logger.log(Level.FINE,"Certchain length = " + certchain.length); 186 derval = new DerValue[certchain.length]; 187 for (int i = 0; i < certchain.length ; i++) 188 derval[i] = new DerValue(certchain[i].getEncoded()); 189 dos.putSequence(derval); 190 X509CertificateChainHelper.insert(any, dos.toByteArray()); 191 192 193 idtok.certificate_chain(codec.encode_value(any)); 194 } else if (AnonCredential.class.isAssignableFrom(cls)) { 195 _logger.log(Level.FINE,"Constructing an Anonymous Identity Token"); 196 idtok.anonymous(true); 197 198 } else if (GSSUPName.class.isAssignableFrom(cls)) { 199 200 _logger.log(Level.FINE,"Constructing a GSS Exported name Identity Token"); 201 202 GSSUPName gssname = (GSSUPName) cred; 203 204 byte[] expname = gssname.getExportedName(); 205 206 GSS_NT_ExportedNameHelper.insert(any, expname); 207 208 209 idtok.principal_name(codec.encode_value(any)); 210 } 211 return (idtok); 212 } 213 214 218 public void send_request(ClientRequestInfo ri) throws ForwardRequest 219 { 220 224 long cContextId = 0; 226 230 AuthorizationElement[] cAuthzElem = {} ; 231 232 233 IdentityToken cIdentityToken = null; 234 235 236 byte[] cAuthenticationToken = {} ; 237 238 239 byte[] cdr_encoded_saselm = {} ; 240 241 java.lang.Object cred = null ; 243 if(_logger.isLoggable(Level.FINE)) 244 _logger.log(Level.FINE,"++++ Entered " + prname + "send_request" + "()"); 245 SecurityContext secctxt = null; 247 secsvc = Csiv2Manager.getSecurityService(); 248 orb = ORBManager.getORB(); 249 if (secsvc == null) { 250 _logger.log(Level.WARNING,"iiop.no_security_service"); 251 return; 252 } 253 org.omg.CORBA.Object effective_target = ri.effective_target(); 254 try{ 255 secctxt = secsvc.getSecurityContext(effective_target); 256 }catch(InvalidMechanismException ime){ 257 _logger.log(Level.SEVERE,"iiop.sec_context_exception",ime); 258 throw new RuntimeException (ime.getMessage()); 259 }catch(InvalidIdentityTokenException iite){ 260 _logger.log(Level.SEVERE,"iiop.runtime_exception",iite); 261 throw new RuntimeException (iite.getMessage()); 262 } 263 264 268 if (secctxt == null) { 269 if(_logger.isLoggable(Level.FINE)){ 270 _logger.log(Level.FINE,"Security context is null (nothing to add to service context)"); 271 } 272 return; 273 } 274 275 final SecurityContext sCtx = secctxt; 276 277 if (secctxt.authcls != null) { 278 cred = AccessController.doPrivileged(new PrivilegedAction () { 279 public java.lang.Object run() { 280 return getCred(sCtx.subject.getPrivateCredentials(sCtx.authcls), sCtx.authcls); 281 } 282 }); 283 284 try { 285 cAuthenticationToken = createAuthToken(cred, secctxt.authcls); 286 } catch (Exception e) { 287 _logger.log(Level.SEVERE,"iiop.createauthtoken_exception",e); 288 throw new SecurityException ( 289 localStrings.getLocalString("secclientreqinterceptor.err_authtok_create", 290 "Error while constructing an authentication token.")); 291 } 292 } 293 294 295 296 if (secctxt.identcls != null) { 297 cred = getCred(secctxt.subject.getPublicCredentials(secctxt.identcls), 298 secctxt.identcls); 299 try { 300 cIdentityToken = createIdToken(cred, secctxt.identcls); 301 } catch (Exception e) { 302 _logger.log(Level.SEVERE,"iiop.createidtoken_exception",e); 303 throw new SecurityException ( 304 localStrings.getLocalString("secclientreqinterceptor.err_idtok_create", 305 "Error while constructing an identity token.")); 306 } 307 } else { 308 if(_logger.isLoggable(Level.FINE)){ 309 _logger.log(Level.FINE,"Constructing an Absent Identity Token"); 310 } 311 cIdentityToken = new IdentityToken(); 312 cIdentityToken.absent(true); 313 } 314 315 if(_logger.isLoggable(Level.FINE)){ 316 _logger.log(Level.FINE,"Creating an EstablishContext message"); 317 } 318 EstablishContext ec = new EstablishContext(cContextId, 319 cAuthzElem, 320 cIdentityToken, 321 cAuthenticationToken); 322 323 SASContextBody sasctxbody = new SASContextBody(); 324 sasctxbody.establish_msg(ec); 325 326 327 Any SasAny = orb.create_any(); 328 SASContextBodyHelper.insert(SasAny, sasctxbody); 329 330 try { 331 cdr_encoded_saselm = codec.encode_value(SasAny); 332 } catch (Exception e) { 333 _logger.log(Level.SEVERE,"iiop.encode_exception",e); 334 throw new SecurityException ( 335 localStrings.getLocalString("secclientreqinterceptor.err_cdr_encode", 336 "CDR Encoding error for a SAS context element.")); 337 } 338 339 340 ServiceContext sc = new ServiceContext(); 341 sc.context_id = SECURITY_ATTRIBUTE_SERVICE_ID; 342 sc.context_data = cdr_encoded_saselm; 343 if(_logger.isLoggable(Level.FINE)){ 344 _logger.log(Level.FINE,"Adding EstablishContext message to service context list"); 345 } 346 boolean no_replace = false; 347 ri.add_request_service_context(sc, no_replace); 348 if(_logger.isLoggable(Level.FINE)){ 349 _logger.log(Level.FINE,"Added EstablishContext message to service context list"); 350 } 351 } 352 353 public void send_poll(ClientRequestInfo ri) { 354 } 355 356 357 360 private void setreplyStatus(int status, org.omg.CORBA.Object target) { 361 if(_logger.isLoggable(Level.FINE)){ 362 _logger.log(Level.FINE,"Status to be set : " + status); 363 } 364 secsvc = Csiv2Manager.getSecurityService(); 365 if (secsvc == null) { 366 _logger.log(Level.WARNING,"iiop.no_security_service"); 367 return; 368 } 369 secsvc.receivedReply(status, target); 370 if(_logger.isLoggable(Level.FINE)){ 371 _logger.log(Level.FINE,"Invoked receivedReply()"); 372 } 373 } 374 375 382 private int mapreplyStatus(int repst) 383 { 384 int status; 385 386 if(_logger.isLoggable(Level.FINE)){ 387 _logger.log(Level.FINE,"Reply status to be mapped = " + repst); 388 } 389 390 switch (repst) { 391 392 case SUCCESSFUL.value: 393 case USER_EXCEPTION.value: 394 status = SecurityService.STATUS_PASSED; 395 break; 396 397 case LOCATION_FORWARD.value: 398 case TRANSPORT_RETRY.value: 399 status = SecurityService.STATUS_RETRY; 400 break; 401 402 case SYSTEM_EXCEPTION.value: 403 status = SecurityService.STATUS_FAILED; 404 break; 405 406 default: 407 status = repst; 408 412 break; 413 } 414 if(_logger.isLoggable(Level.FINE)){ 415 _logger.log(Level.FINE,"Mapped reply status = " + status); 416 } 417 return status; 418 } 419 420 public void receive_reply(ClientRequestInfo ri) 421 { 422 ServiceContext sc = null; 423 int status = -1; 424 425 if(_logger.isLoggable(Level.FINE)){ 426 _logger.log(Level.FINE,"++++ Entered " + prname + "receive_reply"); 427 } 428 orb = ORBManager.getORB(); 429 430 434 try { 435 sc = ri.get_reply_service_context(SECURITY_ATTRIBUTE_SERVICE_ID); 436 } catch(org.omg.CORBA.BAD_PARAM e) { 437 if(_logger.isLoggable(Level.FINE)){ 438 _logger.log(Level.FINE,"No SAS context element found in service context list"); 439 } 440 setreplyStatus(SecurityService.STATUS_PASSED, ri.effective_target()); 441 return; 442 } catch(Exception ex) { 443 _logger.log(Level.SEVERE,"iiop.service_context_exception",ex); 444 return; 445 } 446 447 Any a = orb.create_any(); 448 try { 449 a = codec.decode_value(sc.context_data, SASContextBodyHelper.type()); } catch (Exception e) { 451 _logger.log(Level.SEVERE,"iiop.decode_exception",e); 452 throw new SecurityException ( 453 localStrings.getLocalString("secclientreqinterceptor.err_cdr_decode", 454 "CDR Decoding error for SAS context element.")); 455 } 456 457 SASContextBody sasctxbody = SASContextBodyHelper.extract(a); 458 short sasdiscr = sasctxbody.discriminator(); 459 if(_logger.isLoggable(Level.FINE)){ 460 _logger.log(Level.FINE,"Received " + SvcContextUtils.getMsgname(sasdiscr) + " message"); 461 } 462 463 467 if(_logger.isLoggable(Level.FINE)){ 468 _logger.log(Level.FINE,"Verifying the SAS protocol reply message"); 469 } 470 471 472 473 if ((sasdiscr != MTCompleteEstablishContext.value) 474 && (sasdiscr != MTContextError.value)) { 475 _logger.log(Level.SEVERE,"iiop.invalid_reply_message"); 476 throw new SecurityException ( 477 localStrings.getLocalString("secclientreqinterceptor.err_not_cecec_msg", 478 "Reply message not one of CompleteEstablishContext or ContextError.")); 479 } 480 481 482 int st = mapreplyStatus(ri.reply_status()); 483 484 setreplyStatus(st, ri.effective_target()); 485 } 486 487 public void receive_exception(ClientRequestInfo ri) throws ForwardRequest 488 { 489 if(_logger.isLoggable(Level.FINE)){ 490 _logger.log(Level.FINE,"++++ Entered " + prname + "receive_exception"); 491 } 492 } 493 494 public void receive_other(ClientRequestInfo ri) throws ForwardRequest 495 { 496 } 497 498 public void destroy() 499 { 500 } 501 } 502 | Popular Tags |