1 25 package org.objectweb.carol.util.csiv2.gss; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.io.UnsupportedEncodingException ; 31 32 import org.ietf.jgss.GSSException ; 33 import org.ietf.jgss.Oid ; 34 import org.omg.GSSUP.GSSUPMechOID; 35 36 import org.objectweb.carol.util.configuration.TraceCarol; 37 38 39 40 44 public class GSSHelper { 45 46 49 private static final byte[] EXPORTED_NAME_TOK_ID = new byte[]{0x04, 0x01}; 50 51 54 private static final int[] TWO_BYTES = {0xFF00, 0xFF}; 55 56 59 private static final int[] FOUR_BYTES = {0xFF000000, 0xFF0000, 0xFF00, 0xFF}; 60 61 64 private static final int BYTES = 0xFF; 65 66 69 private static final int SEQUENCE = 0x60; 70 71 74 private static final int OBJECT_IDENTIFIER = 0x06; 75 76 77 80 private GSSHelper() { 81 82 } 83 84 85 146 147 153 public static byte[] encodeExported(String name) { 154 155 byte[] mechOidDer = GSSHelper.getMechOidDer(); 156 byte[] nameBytes = null; 157 try { 158 nameBytes = name.getBytes("UTF-8"); 160 } catch (UnsupportedEncodingException uee) { 161 throw new IllegalStateException ("Cannot get utf-8 encoding" + uee.getMessage()); 162 } 163 int nameLength = name.length(); 164 165 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 168 bos.write(EXPORTED_NAME_TOK_ID[0]); 169 bos.write(EXPORTED_NAME_TOK_ID[1]); 170 171 int mechOidLength = mechOidDer.length; 173 bos.write(mechOidLength & TWO_BYTES[0]); 174 bos.write(mechOidLength & TWO_BYTES[1]); 175 176 bos.write(mechOidDer, 0, mechOidDer.length); 178 179 bos.write(nameLength & FOUR_BYTES[0]); 181 bos.write(nameLength & FOUR_BYTES[1]); 182 bos.write(nameLength & FOUR_BYTES[2]); 183 bos.write(nameLength & FOUR_BYTES[3]); 184 185 bos.write(nameBytes, 0, nameBytes.length); 187 return bos.toByteArray(); 188 189 } 190 191 196 public static String decodeExported(byte[] toDecode) throws Exception { 197 198 ByteArrayInputStream bis = new ByteArrayInputStream (toDecode); 199 200 if (bis.read() != EXPORTED_NAME_TOK_ID[0] || bis.read() != EXPORTED_NAME_TOK_ID[1]) { 202 throw new IllegalArgumentException ("Invalid header, this is not an exported name"); 203 } 204 205 int mechOidLength = bis.read() * 8 + bis.read(); 207 208 byte[] mechOidDerTemplate = GSSHelper.getMechOidDer(); 210 byte[] mechOidDer = new byte[mechOidLength]; 211 int success = bis.read(mechOidDer); 212 if (success == -1 || success != mechOidDerTemplate.length) { 213 throw new IllegalArgumentException ("Not able to decode name, length is incorrect"); 214 } else { 215 for (int b = 0; b < mechOidDerTemplate.length; b++) { 217 if (mechOidDer[b] != mechOidDerTemplate[b]) { 218 throw new IllegalArgumentException ("Not a valid MechoID"); 219 } 220 } 221 } 222 223 int nameLength = bis.read() * 24 + bis.read() * 16 + bis.read() * 8 + bis.read(); 225 226 byte[] name = new byte[nameLength]; 227 success = bis.read(name); 228 if (success == -1 || success != nameLength) { 229 throw new IllegalArgumentException ("Not able to decode name, length is incorrect"); 230 } 231 return new String (name); 232 } 233 234 238 private static String getMechOID() { 239 return GSSUPMechOID.value.substring(4); 240 } 241 242 243 325 public static byte[] decodeToken(byte[] toExtract) { 326 int b = 0; 327 328 if (toExtract[b++] != SEQUENCE) { 330 throw new IllegalArgumentException ("Invalid token"); 331 } 332 333 int tokenLegnth = 0; 336 int lengthTmp = toExtract[b++]; 337 338 if ((lengthTmp & 128) == 128) { int additionalOctets = lengthTmp & 0x7f; 344 for (int i = 0; i < additionalOctets; i++) { 345 tokenLegnth = (tokenLegnth << 8) + (toExtract[b++] & BYTES); 346 } 347 } else { 348 tokenLegnth = lengthTmp; 350 } 351 352 353 if (toExtract[b] != OBJECT_IDENTIFIER) { throw new IllegalArgumentException ("Invalid object identifier"); 356 } 357 358 byte[] mechOidDerTemplate = GSSHelper.getMechOidDer(); 360 for (int i = 0; i < mechOidDerTemplate.length; i++) { 362 if (toExtract[b++] != mechOidDerTemplate[i]) { 363 throw new IllegalArgumentException ("Not a valid MechoID"); 364 } 365 } 366 367 int objLength = toExtract.length - b; 370 byte[] objId = new byte[objLength]; 371 System.arraycopy(toExtract, b, objId, 0, objLength); 372 373 return objId; 374 375 } 376 377 383 public static byte[] encodeToken(byte[] contextData) throws IOException { 384 385 byte[] mechOidDer = GSSHelper.getMechOidDer(); 386 int mechOidLength = mechOidDer.length; 387 int contextDataLength = contextData.length; 388 389 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 390 391 bos.write(SEQUENCE); 393 394 395 bos.write(mechOidLength + contextDataLength); 397 398 bos.write(mechOidDer); 400 401 bos.write(contextData); 403 404 return bos.toByteArray(); 405 } 406 407 408 411 public static byte[] getMechOidDer() { 412 Oid oid = null; 413 byte[] gssupDerEncoding = null; 414 try { 415 oid = new Oid (getMechOID()); 416 gssupDerEncoding = oid.getDER(); 417 } catch (GSSException gsse) { 418 TraceCarol.error("Error while getting MechOID"); 419 return null; 420 } 421 return gssupDerEncoding; 422 } 423 424 } 425 | Popular Tags |