1 23 package com.sun.enterprise.iiop.security; 24 25 30 31 import java.io.IOException ; 32 import org.omg.CORBA.*; 33 import org.omg.PortableInterceptor.*; 34 import org.omg.IOP.*; 35 36 37 import java.util.*; 38 39 40 import com.sun.corba.ee.org.omg.CSI.*; 41 import com.sun.corba.ee.org.omg.GSSUP.*; 42 import com.sun.corba.ee.org.omg.CSIIOP.*; 43 44 import com.sun.enterprise.iiop.security.GSSUtils; 45 import com.sun.enterprise.security.auth.login.PasswordCredential; 46 47 import java.util.logging.*; 48 import com.sun.logging.*; 49 55 56 public class GSSUPToken { 57 private static java.util.logging.Logger _logger=null; 58 59 static{ 60 _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER); 61 } 62 public static final String DELIMITER_REGEXP = "\\@"; 66 public static final String ESCAPE_CHAR_REGEXP = "\\\\\\@"; 68 public static final String ESCAPE_CHAR = "\\"; 69 71 public static final String DELIMITER = "@" ; 72 public static final String DEFAULT_REALM_NAME = "default"; 73 74 79 private byte[] cdr_encoded_token = {} ; 80 81 82 PasswordCredential pwdcred = null; 83 93 public static GSSUPToken getClientSideInstance(ORB orb, Codec codec, 94 PasswordCredential pwdcred){ 95 return new GSSUPToken(orb, codec, pwdcred); 96 } 97 106 public static GSSUPToken getServerSideInstance(ORB orb, Codec codec, 107 byte[] authtok) throws SecurityMechanismException{ 108 return new GSSUPToken(orb, codec, authtok); 109 } 110 111 116 private GSSUPToken(ORB orb, Codec codec, PasswordCredential pwdcred) 117 { 118 byte[] name_utf8 = {}; byte[] password_utf8 = {}; 121 if(_logger.isLoggable(Level.FINE)){ 122 _logger.log(Level.FINE,"IIOP: Going to construct a GSSUPToken:"); 123 _logger.log(Level.FINE, pwdcred.toString()); 124 } 125 126 try { 127 String _name_ = new String (pwdcred.getUser()); 128 int index = _name_.indexOf(DELIMITER); 133 if(index == -1){ 134 }else{ int escaped_index = _name_.indexOf(ESCAPE_CHAR); 137 if(escaped_index == -1){ _name_ = _name_.replaceAll(DELIMITER_REGEXP, ESCAPE_CHAR_REGEXP); 139 }else{ _name_ = _name_.replaceAll(ESCAPE_CHAR_REGEXP, DELIMITER_REGEXP); 145 _name_ = _name_.replaceAll(DELIMITER_REGEXP, ESCAPE_CHAR_REGEXP); 146 } 147 } 148 String realm = pwdcred.getRealm(); 149 if(realm != null){ 151 _name_ = _name_ + DELIMITER + realm; 153 } 154 name_utf8 = _name_.toString().getBytes("UTF8"); 155 password_utf8 = pwdcred.getPassword().getBytes("UTF8"); 156 } catch (Exception e) { 157 _logger.log(Level.SEVERE,"iiop.password_exception",e); 158 } 159 160 163 164 SecurityMechanismSelector sms = new SecurityMechanismSelector(); 165 ConnectionContext cc = sms.getClientConnectionContext(); 166 CompoundSecMech mech = cc.getMechanism(); 167 byte[] target_name = mech.as_context_mech.target_name; 168 169 if(_logger.isLoggable(Level.FINE)){ 170 _logger.fine("Username (UTF8) " + GSSUtils.dumpHex(name_utf8)); 171 _logger.fine("Password (UTF8) " + GSSUtils.dumpHex(password_utf8)); 172 _logger.fine("Targetname " + GSSUtils.dumpHex(target_name)); 173 } 174 175 176 InitialContextToken inctxToken = 177 new InitialContextToken(name_utf8, password_utf8, target_name); 178 179 180 Any a = orb.create_any(); 181 InitialContextTokenHelper.insert(a, inctxToken); 182 183 try { 184 cdr_encoded_token = codec.encode_value(a); 185 } catch (Exception e) { 186 _logger.log(Level.SEVERE,"iiop.encode_exception",e); 187 } 188 if(_logger.isLoggable(Level.FINE)) { 189 _logger.log(Level.FINE,"IIOP:Mech specific token length (CDR encoded) = " + cdr_encoded_token.length); 190 } 191 } 192 193 197 198 private GSSUPToken(ORB orb, Codec codec, byte[] authtok) 199 throws SecurityMechanismException 200 { 201 byte[] name_utf8 = {}; byte[] password_utf8 = {}; byte[] target_name = {} ; String username = ""; 205 String userpwd = ""; 206 String realm = ""; 207 byte[] encoded_token = {} ; 208 209 if(_logger.isLoggable(Level.FINE)) { 210 _logger.log(Level.FINE, "IIOP:Going to construct a GSSUPToken:"); 211 _logger.log(Level.FINE, "IIOP:Getting CDR encoded GSSUP mechanism token from client authentication token"); 212 } 213 214 encoded_token = GSSUtils.getMechToken(GSSUtils.GSSUP_MECH_OID, authtok); 215 216 217 if(_logger.isLoggable(Level.FINE)) { 218 _logger.log(Level.FINE,"CDR encoded mech specific token length = "+ encoded_token.length); 219 } 220 221 Any a = orb.create_any(); 222 223 try { 224 a = codec.decode_value(encoded_token, InitialContextTokenHelper.type()); 225 } catch (Exception e) { 226 _logger.log(Level.SEVERE,"iiop.decode_exception",e); 227 } 228 229 InitialContextToken inctxToken = InitialContextTokenHelper.extract(a); 230 231 232 password_utf8 = inctxToken.password; 233 name_utf8 = inctxToken.username; 234 target_name = inctxToken.target_name; 235 236 if(_logger.isLoggable(Level.FINE)){ 237 _logger.fine("IIOP:Username (UTF8) " + GSSUtils.dumpHex(name_utf8)); 238 _logger.fine("IIOP:Password (UTF8) " + GSSUtils.dumpHex(password_utf8)); 239 _logger.fine("IIOP:Targetname " + GSSUtils.dumpHex(target_name)); 240 } 241 242 243 try { 244 username = new String (name_utf8, "UTF8"); 245 userpwd = new String (password_utf8, "UTF8"); 246 } catch (Exception e) { 247 _logger.log(Level.SEVERE,"iiop.user_password_exception",e); 248 } 249 250 253 String name; 254 int index = username.indexOf(DELIMITER); 255 int esc_index = username.indexOf(ESCAPE_CHAR); 256 if ( index == -1 ) { 257 name = username; 258 } 259 else if ( index == 0 || esc_index == 0) { 260 throw new SecurityMechanismException("No name_value in username"); 263 } else if (esc_index != -1){ 264 if (esc_index+2 >= username.length()){ 268 name = username.replaceAll(ESCAPE_CHAR_REGEXP, DELIMITER); 270 if(_logger.isLoggable(Level.FINE)){ 271 _logger.log(Level.FINE, "IIOP:No Realm specified, "+ 272 " creating a default realm for login"); 273 } 274 realm = DEFAULT_REALM_NAME; 275 }else { 276 int second_at_index = username.indexOf(DELIMITER, esc_index+3); 281 if (second_at_index == -1){ 282 name = username.replaceAll(ESCAPE_CHAR_REGEXP, DELIMITER); 284 285 if(_logger.isLoggable(Level.FINE)){ 286 _logger.log(Level.FINE, "IIOP:No Realm specified, "+ 287 " creating a default realm for login"); 288 } 289 realm = DEFAULT_REALM_NAME; 290 }else { 291 name = username.substring(0, second_at_index); 292 name = name.replaceAll(ESCAPE_CHAR_REGEXP, DELIMITER); 293 realm = username.substring(second_at_index+1); 294 if(realm == null){ 295 if(_logger.isLoggable(Level.FINE)){ 297 _logger.log(Level.FINE, "IIOP:No Realm specified, "+ 298 " creating a default realm for login"); 299 } 300 realm = DEFAULT_REALM_NAME; 301 } 302 } 303 } 304 } else { 306 StringTokenizer strtok = new StringTokenizer(username, DELIMITER); 308 name = strtok.nextToken(); 309 if ( strtok.hasMoreTokens() ) { 311 realm = strtok.nextToken(); 312 if(realm == null){ 316 if(_logger.isLoggable(Level.FINE)){ 317 _logger.log(Level.FINE, "IIOP:No Realm specified, "+ 318 " creating a default realm for login"); 319 } 320 realm = DEFAULT_REALM_NAME; 321 } 322 } 323 } 324 pwdcred = new PasswordCredential(name, userpwd, realm, target_name); 325 if(_logger.isLoggable(Level.FINE)){ 326 _logger.log(Level.FINE, pwdcred.toString()); 327 } 328 } 329 334 byte[] getGSSToken() throws IOException 335 { 336 if(_logger.isLoggable(Level.FINER)){ 337 _logger.log(Level.FINER, "IIOP:GSSUP mech token : " + GSSUtils.dumpHex(cdr_encoded_token)); 338 } 339 340 byte[] gsstoken = GSSUtils.createMechIndToken(GSSUtils.GSSUP_MECH_OID, cdr_encoded_token); 341 if(_logger.isLoggable(Level.FINER)){ 342 _logger.log(Level.FINER, "IIOP:GSSUP token length : " + gsstoken.length); 343 _logger.log(Level.FINER, "IIOP:GSSUP token: " + GSSUtils.dumpHex(gsstoken)); 344 } 345 return gsstoken; 346 } 347 348 352 PasswordCredential getPwdcred() 353 { 354 return pwdcred; 355 } 356 } 357 358 | Popular Tags |