📄 ntlmutilities.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */package org.apache.mina.proxy.handlers.http.ntlm;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.StringTokenizer;import org.apache.mina.proxy.utils.ByteUtilities;/** * NTLMUtilities.java - NTLM functions used for authentication and unit testing. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 759662 $, $Date: 2009-03-29 12:43:02 +0200 (Sun, 29 Mar 2009) $ * @since MINA 2.0.0-M3 */public class NTLMUtilities implements NTLMConstants { /** * @see #writeSecurityBuffer(short, short, int, byte[], int) */ public final static byte[] writeSecurityBuffer(short length, int bufferOffset) { byte[] b = new byte[8]; writeSecurityBuffer(length, length, bufferOffset, b, 0); return b; } /** * Writes a security buffer to the given array <code>b</code> at offset * <code>offset</code>. * * @param length the length of the security buffer * @param allocated the allocated space for the security buffer (should be * greater or equal to <code>length</code> * @param bufferOffset the offset since the beginning of the <code>b</code> * buffer where the buffer iswritten * @param b the buffer in which we write the security buffer * @param offset the offset at which to write to the buffer */ public final static void writeSecurityBuffer(short length, short allocated, int bufferOffset, byte[] b, int offset) { ByteUtilities.writeShort(length, b, offset); ByteUtilities.writeShort(allocated, b, offset + 2); ByteUtilities.writeInt(bufferOffset, b, offset + 4); } public final static void writeOSVersion(byte majorVersion, byte minorVersion, short buildNumber, byte[] b, int offset) { b[offset] = majorVersion; b[offset + 1] = minorVersion; b[offset + 2] = (byte) buildNumber; b[offset + 3] = (byte) (buildNumber >> 8); b[offset + 4] = 0; b[offset + 5] = 0; b[offset + 6] = 0; b[offset + 7] = 0x0F; } /** * Tries to return a valid Os version on windows systems. */ public final static byte[] getOsVersion() { String os = System.getProperty("os.name"); if (os == null || !os.toUpperCase().contains("WINDOWS")) { return DEFAULT_OS_VERSION; } else { byte[] osVer = new byte[8]; try { Process pr = Runtime.getRuntime().exec("cmd /C ver"); BufferedReader reader = new BufferedReader( new InputStreamReader(pr.getInputStream())); pr.waitFor(); String line; do { line = reader.readLine(); } while ((line != null) && (line.length() != 0)); int pos = line.toLowerCase().indexOf("version"); if (pos == -1) { throw new NullPointerException(); } pos += 8; line = line.substring(pos, line.indexOf(']')); StringTokenizer tk = new StringTokenizer(line, "."); if (tk.countTokens() != 3) { throw new NullPointerException(); } writeOSVersion(Byte.parseByte(tk.nextToken()), Byte .parseByte(tk.nextToken()), Short.parseShort(tk .nextToken()), osVer, 0); } catch (Exception ex) { try { String version = System.getProperty("os.version"); writeOSVersion(Byte.parseByte(version.substring(0, 1)), Byte.parseByte(version.substring(2, 3)), (short) 0, osVer, 0); } catch (Exception ex2) { return DEFAULT_OS_VERSION; } } return osVer; } } /** * see http://davenport.sourceforge.net/ntlm.html#theType1Message * * @param workStation the workstation name * @param domain the domain name * @param customFlags custom flags, if null then * <code>NTLMConstants.DEFAULT_CONSTANTS</code> is used * @param osVersion the os version of the client, if null then * <code>NTLMConstants.DEFAULT_OS_VERSION</code> is used * @return */ public final static byte[] createType1Message(String workStation, String domain, Integer customFlags, byte[] osVersion) { byte[] msg = null; if (osVersion != null && osVersion.length != 8) { throw new IllegalArgumentException( "osVersion parameter should be a 8 byte wide array"); } if (workStation == null || domain == null) { throw new NullPointerException( "workStation and domain must be non null"); } int flags = customFlags != null ? customFlags | FLAG_NEGOTIATE_WORKSTATION_SUPPLIED | FLAG_NEGOTIATE_DOMAIN_SUPPLIED : DEFAULT_FLAGS; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { baos.write(NTLM_SIGNATURE); baos.write(ByteUtilities.writeInt(MESSAGE_TYPE_1)); baos.write(ByteUtilities.writeInt(flags)); byte[] domainData = ByteUtilities.getOEMStringAsByteArray(domain); byte[] workStationData = ByteUtilities .getOEMStringAsByteArray(workStation); int pos = (osVersion != null) ? 40 : 32; baos.write(writeSecurityBuffer((short) domainData.length, pos + workStationData.length)); baos .write(writeSecurityBuffer((short) workStationData.length, pos)); if (osVersion != null) { baos.write(osVersion); } // Order is not mandatory since a pointer is given in the security buffers baos.write(workStationData); baos.write(domainData); msg = baos.toByteArray(); baos.close(); } catch (IOException e) { return null; } return msg; } /** * Write a security buffer and returns the pointer of the position * where to write the next security buffer. * * @param baos the stream where the security buffer is written * @param len the length of the security buffer * @param pointer the position where the security buffer can be written * @return the position where the next security buffer will be written * @throws IOException */ public final static int writeSecurityBufferAndUpdatePointer( ByteArrayOutputStream baos, short len, int pointer) throws IOException { baos.write(writeSecurityBuffer(len, pointer)); return pointer + len; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -