1 17 package org.alfresco.filesys.server.auth.ntlm; 18 19 import java.io.UnsupportedEncodingException ; 20 21 import org.alfresco.filesys.util.DataPacker; 22 23 28 public abstract class NTLMMessage 29 { 30 32 private static final int DefaultBlobSize = 256; 33 34 36 public static final int OffsetSignature = 0; 37 public static final int OffsetType = 8; 38 39 41 public static final int BufferHeaderLen = 8; 42 43 45 private byte[] m_buf; 46 private int m_offset; 47 48 private int m_len; 49 50 53 protected NTLMMessage() 54 { 55 57 m_buf = new byte[DefaultBlobSize]; 58 m_len = DefaultBlobSize; 59 } 60 61 68 protected NTLMMessage(byte[] buf, int offset, int len) 69 { 70 m_buf = buf; 71 m_offset = offset; 72 73 m_len = len; 74 } 75 76 81 public final int isMessageType() 82 { 83 return DataPacker.getIntelInt(m_buf, m_offset + OffsetType); 84 } 85 86 91 public abstract int getFlags(); 92 93 99 public final boolean hasFlag(int flag) 100 { 101 return (getFlags() & flag) != 0 ? true : false; 102 } 103 108 public int getLength() 109 { 110 return m_len; 111 } 112 113 118 public final void setMessageType(int typ) 119 { 120 DataPacker.putIntelInt(typ, m_buf, m_offset + OffsetType); 121 } 122 123 130 public final void copyFrom(byte[] buf, int offset, int len) 131 { 132 134 if ( m_buf == null || m_offset != 0 || m_buf.length < len) 135 m_buf = new byte[len]; 136 137 139 System.arraycopy(buf, offset, m_buf, 0, len); 140 } 141 142 147 public final byte[] getBytes() 148 { 149 byte[] byts = new byte[getLength()]; 150 System.arraycopy(m_buf, m_offset, byts, 0, getLength()); 151 return byts; 152 } 153 154 159 protected abstract void setFlags(int flags); 160 161 167 protected void initializeHeader(int typ, int flags) 168 { 169 171 System.arraycopy( NTLM.Signature, 0, m_buf, m_offset, NTLM.Signature.length); 172 173 setMessageType(typ); 174 setFlags(flags); 175 } 176 177 183 protected final int getShortValue(int offset) 184 { 185 return DataPacker.getIntelShort(m_buf, m_offset + offset); 186 } 187 188 194 protected final int getIntValue(int offset) 195 { 196 return DataPacker.getIntelInt(m_buf, m_offset + offset); 197 } 198 199 205 protected final int getByteOffset(int offset) 206 { 207 return getIntValue(m_offset + offset + 4); 208 } 209 210 216 protected final byte[] getByteValue(int offset) 217 { 218 220 int bLen = getShortValue(m_offset + offset); 221 if ( bLen == 0) 222 return null; 223 224 int bOff = getIntValue(m_offset + offset + 4); 225 return getRawBytes(bOff, bLen); 226 } 227 228 235 protected final byte[] getRawBytes(int offset, int len) 236 { 237 byte[] byts = new byte[len]; 238 System.arraycopy(m_buf, m_offset + offset, byts, 0, len); 239 240 return byts; 241 } 242 243 249 protected final int getStringLength(int offset) 250 { 251 int bufpos = m_offset + offset; 252 253 if ( bufpos + 2 > m_len) 254 return -1; 255 return DataPacker.getIntelShort(m_buf, bufpos); 256 } 257 258 264 protected final int getStringAllocatedLength(int offset) 265 { 266 int bufpos = m_offset + offset; 267 268 if ( bufpos + 8 > m_len) 269 return -1; 270 return DataPacker.getIntelShort(m_buf, bufpos + 2); 271 } 272 273 279 protected final int getStringOffset(int offset) 280 { 281 int bufpos = m_offset + offset; 282 283 if ( bufpos + 8 > m_len) 284 return -1; 285 return DataPacker.getIntelInt(m_buf, bufpos + 4); 286 } 287 288 295 protected final String getStringValue(int offset, boolean isuni) 296 { 297 int bufpos = m_offset + offset; 298 299 if ( offset + 8 > m_len) 300 return null; 301 302 304 int len = DataPacker.getIntelShort(m_buf, bufpos); 305 int pos = DataPacker.getIntelInt(m_buf, bufpos + 4); 306 307 309 if ( pos + len > m_len) 310 return null; 311 314 316 String str = null; 317 try 318 { 319 str = new String (m_buf, m_offset + pos, len, isuni ? "UnicodeLittle" : "US-ASCII"); 320 } 321 catch (UnsupportedEncodingException ex) 322 { 323 ex.printStackTrace(); 324 } 325 326 return str; 327 } 328 329 337 protected final String getRawString(int offset, int len, boolean isuni) 338 { 339 return DataPacker.getString(m_buf, m_offset + offset, len, isuni); 340 } 341 342 348 protected final void setShortValue(int offset, int sval) 349 { 350 DataPacker.putIntelShort(sval, m_buf, m_offset + offset); 351 } 352 353 359 protected final void setIntValue(int offset, int val) 360 { 361 DataPacker.putIntelInt(val, m_buf, m_offset + offset); 362 } 363 364 370 protected final void setRawBytes(int offset, byte[] byts) 371 { 372 System.arraycopy(byts, 0, m_buf, m_offset + offset, byts.length); 373 } 374 375 381 protected final void setRawInts(int offset, int[] ints) 382 { 383 int bufpos = m_offset + offset; 384 385 for ( int i = 0; i < ints.length; i++) 386 { 387 DataPacker.putIntelInt( ints[i], m_buf, bufpos); 388 bufpos += 4; 389 } 390 } 391 392 400 protected final int setRawString(int offset, String str, boolean isuni) 401 { 402 return DataPacker.putString(str, m_buf, m_offset + offset, false, isuni); 403 } 404 405 411 protected final void zeroBytes(int offset, int len) 412 { 413 int bufpos = m_offset + offset; 414 for ( int i = 0; i < len; i++) 415 m_buf[bufpos++] = (byte) 0; 416 } 417 418 426 protected final int setByteValue(int offset, byte[] byts, int dataOffset) 427 { 428 int bytsLen = byts != null ? byts.length : 0; 429 430 if ( m_offset + offset + 12 > m_buf.length || 431 m_offset + dataOffset + bytsLen > m_buf.length) 432 throw new ArrayIndexOutOfBoundsException (); 433 434 436 DataPacker.putIntelShort(bytsLen, m_buf, m_offset + offset); 437 DataPacker.putIntelShort(bytsLen, m_buf, m_offset + offset + 2); 438 DataPacker.putIntelInt(dataOffset, m_buf, m_offset + offset + 4); 439 440 442 if ( bytsLen > 0) 443 System.arraycopy(byts, 0, m_buf, m_offset + dataOffset, bytsLen); 444 445 447 return dataOffset + DataPacker.wordAlign(bytsLen); 448 } 449 450 459 protected final int setStringValue(int offset, String val, int strOffset, boolean isuni) 460 { 461 463 int len = val.length(); 464 if ( isuni) 465 len *= 2; 466 467 if ( m_offset + offset + 8 > m_buf.length || 468 m_offset + strOffset + len > m_buf.length) 469 throw new ArrayIndexOutOfBoundsException (); 470 471 473 474 DataPacker.putIntelShort(len, m_buf, m_offset + offset); 475 DataPacker.putIntelShort(len, m_buf, m_offset + offset + 2); 476 DataPacker.putIntelInt(strOffset, m_buf, m_offset + offset + 4); 477 478 480 return DataPacker.putString(val, m_buf, m_offset + strOffset, false, isuni) - m_offset; 481 } 482 483 488 protected final void setLength(int len) 489 { 490 m_len = len; 491 } 492 493 499 public final static int isNTLMType(byte[] buf) 500 { 501 return isNTLMType(buf, 0); 502 } 503 504 511 public final static int isNTLMType(byte[] buf, int offset) 512 { 513 515 if ( buf == null || buf.length < BufferHeaderLen) 516 return -1; 517 518 for ( int i = 0; i < NTLM.Signature.length; i++) 519 { 520 if ( buf[offset + i] != NTLM.Signature[i]) 521 return -1; 522 } 523 524 526 return DataPacker.getIntelInt(buf, offset + OffsetType); 527 } 528 } 529 | Popular Tags |