1 package org.jacorb.security.sas; 2 3 22 23 import java.security.Provider ; 24 25 import org.ietf.jgss.GSSException ; 26 import org.ietf.jgss.Oid ; 27 import org.omg.CORBA.Any ; 28 import org.omg.CORBA.ORB ; 29 import org.omg.GSSUP.InitialContextToken; 30 import org.omg.GSSUP.InitialContextTokenHelper; 31 import org.omg.IOP.Codec ; 32 33 import sun.security.jgss.spi.GSSNameSpi; 34 35 41 42 public final class GSSUPNameSpi 43 implements GSSNameSpi 44 { 45 private static Oid mechOid; 46 47 private Provider provider; 48 private Oid nameTypeOid; 49 50 private InitialContextToken subject = null; 51 52 static 53 { 54 try 55 { 56 mechOid = new Oid ("2.23.130.1.1.1"); 57 } 58 catch (GSSException e) 59 { 60 } 61 } 62 63 public GSSUPNameSpi(Provider provider, Oid mechOid, byte[] name ,Oid nameTypeOid) 64 { 65 this.provider = provider; 66 this.nameTypeOid = nameTypeOid; 67 69 91 } 92 93 public static byte[] encode(ORB orb, Codec codec, String username, String password, byte[] target_name) 94 { 95 InitialContextToken subject = null; 96 try 97 { 98 subject = new InitialContextToken( username.getBytes("UTF-8"), 99 password.getBytes("UTF-8"), 100 target_name); 101 } 102 catch(java.io.UnsupportedEncodingException e) 103 { 104 return new byte[0]; 107 } 108 byte[] out = null; 109 Any any = orb.create_any(); 110 InitialContextTokenHelper.insert( any, subject ); 111 try 112 { 113 out = codec.encode_value( any ); 114 } 115 catch (Exception e) 116 { 117 return new byte[0]; 119 } 120 121 byte[] mechOidArray = null; 122 try 123 { 124 mechOidArray = mechOid.getDER(); 125 } 126 catch(org.ietf.jgss.GSSException e) 127 { 128 return new byte[0]; 130 } 131 132 int length = out.length + mechOidArray.length; 133 byte[] encodedLength = null; 134 135 if((length >> 7) == 0) 136 { 137 encodedLength = new byte[]{(byte) 0x60, 139 (byte) length}; 140 } 141 else if((length >> 14) == 0) 142 { 143 encodedLength = new byte[]{(byte) 0x60, 145 (byte) ((length >> 7) | 0x80), 146 (byte) (length & 0x7F)}; 147 } 148 else if((length >> 21) == 0) 149 { 150 encodedLength = new byte[]{(byte) 0x60, 152 (byte) ((length >> 14) | 0x80), 153 (byte) (((length >> 7) & 0x7F) | 0x80), 154 (byte) (length & 0x7F)}; 155 } 156 else if((length >> 28) == 0) 157 { 158 encodedLength = new byte[]{(byte) 0x60, 160 (byte) ((length >> 21) | 0x80), 161 (byte) (((length >> 14) & 0x7F) | 0x80), 162 (byte) (((length >> 7) & 0x7F) | 0x80), 163 (byte) (length & 0x7F)}; 164 } 165 else 166 { 167 encodedLength = new byte[]{(byte) 0x60, 169 (byte) ((length >> 28) | 0x80), 170 (byte) (((length >> 21) & 0x7F) | 0x80), 171 (byte) (((length >> 14) & 0x7F) | 0x80), 172 (byte) (((length >> 7) & 0x7F) | 0x80), 173 (byte) (length & 0x7F)}; 174 } 175 176 byte[] completeContext = new byte[length + encodedLength.length]; 177 System.arraycopy(encodedLength, 0, 178 completeContext, 0, 179 encodedLength.length); 180 System.arraycopy(mechOidArray, 0, 181 completeContext, encodedLength.length, 182 mechOidArray.length); 183 System.arraycopy(out, 0, 184 completeContext, encodedLength.length + mechOidArray.length, 185 out.length); 186 187 return completeContext; 188 } 189 190 public static byte[] encode(ORB orb, Codec codec, String username, char[] password, String target_name) 191 { 192 return encode(orb, codec, username, new String (password), target_name.getBytes()); 193 } 194 195 public static InitialContextToken decode(ORB orb, Codec codec, byte[] gssToken) 196 { 197 if(gssToken[0] != 0x60) 198 { 199 return null; 201 } 202 203 205 int index = 1; 207 while(index < gssToken.length && 208 (gssToken[index] & 0x80) == 1) 209 { 210 ++index; 211 } 212 213 if(index == gssToken.length) 214 { 215 return null; 218 } 219 220 byte[] mechOidArray = null; 221 try 222 { 223 mechOidArray = mechOid.getDER(); 224 } 225 catch(org.ietf.jgss.GSSException e) 226 { 227 return null; 229 } 230 231 ++index; 233 234 if((index + mechOidArray.length) >= gssToken.length) 235 { 236 return null; 238 } 239 240 for(int i = 0; i < mechOidArray.length; ++i) 241 { 242 if(mechOidArray[i] != gssToken[index + i]) 243 { 244 return null; 246 } 247 } 248 249 index += mechOidArray.length; 251 252 byte[] icToken = new byte[gssToken.length - index]; 253 System.arraycopy(gssToken, index, icToken, 0, icToken.length); 254 255 try 256 { 257 Any any = 258 codec.decode_value( 259 icToken, 260 InitialContextTokenHelper.type()); 261 return InitialContextTokenHelper.extract(any); 262 } 263 catch (Exception e) 264 { 265 } 267 return null; 269 } 270 271 public Provider getProvider() 272 { 273 return provider; 274 } 275 276 public boolean equals(GSSNameSpi name) throws GSSException 277 { 278 return subject.equals(((GSSUPNameSpi)name).subject); 279 } 280 281 public byte[] export() throws GSSException 282 { 283 throw new GSSException (GSSException.FAILURE, GSSException.FAILURE, "Not Implemented"); 284 299 } 300 301 public Oid getMechanism() 302 { 303 return mechOid; 304 } 305 306 public String toString() 307 { 308 return null; 309 323 } 324 325 public Oid getStringNameType() 326 { 327 return nameTypeOid; 328 } 329 330 public boolean isAnonymousName() 331 { 332 System.out.println("GSSUPNameSpi.isAnonymousName"); 333 return false; 334 } 335 } 336 | Popular Tags |