001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package com.mysql.jdbc;
026:
027: import java.security.MessageDigest;
028: import java.security.NoSuchAlgorithmException;
029:
030: /**
031: * Methods for doing secure authentication with MySQL-4.1 and newer.
032: *
033: * @author Mark Matthews
034: *
035: * @version $Id: Security.java 3726 2005-05-19 15:52:24Z mmatthews $
036: */
037: class Security {
038: private static final char PVERSION41_CHAR = '*';
039:
040: private static final int SHA1_HASH_SIZE = 20;
041:
042: /**
043: * Returns hex value for given char
044: */
045: private static int charVal(char c) {
046: return ((c >= '0') && (c <= '9')) ? (c - '0')
047: : (((c >= 'A') && (c <= 'Z')) ? (c - 'A' + 10)
048: : (c - 'a' + 10));
049: }
050:
051: /*
052: * Convert password in salted form to binary string password and hash-salt
053: * For old password this involes one more hashing
054: *
055: * SYNOPSIS get_hash_and_password() salt IN Salt to convert from pversion IN
056: * Password version to use hash OUT Store zero ended hash here bin_password
057: * OUT Store binary password here (no zero at the end)
058: *
059: * RETURN 0 for pre 4.1 passwords !0 password version char for newer
060: * passwords
061: */
062:
063: /**
064: * Creates key from old password to decode scramble Used in 4.1
065: * authentication with passwords stored pre-4.1 hashing.
066: *
067: * @param passwd
068: * the password to create the key from
069: *
070: * @return 20 byte generated key
071: *
072: * @throws NoSuchAlgorithmException
073: * if the message digest 'SHA-1' is not available.
074: */
075: static byte[] createKeyFromOldPassword(String passwd)
076: throws NoSuchAlgorithmException {
077: /* At first hash password to the string stored in password */
078: passwd = makeScrambledPassword(passwd);
079:
080: /* Now convert it to the salt form */
081: int[] salt = getSaltFromPassword(passwd);
082:
083: /* Finally get hash and bin password from salt */
084: return getBinaryPassword(salt, false);
085: }
086:
087: /**
088: * DOCUMENT ME!
089: *
090: * @param salt
091: * DOCUMENT ME!
092: * @param usingNewPasswords
093: * DOCUMENT ME!
094: *
095: * @return DOCUMENT ME!
096: *
097: * @throws NoSuchAlgorithmException
098: * if the message digest 'SHA-1' is not available.
099: */
100: static byte[] getBinaryPassword(int[] salt,
101: boolean usingNewPasswords) throws NoSuchAlgorithmException {
102: int val = 0;
103:
104: byte[] binaryPassword = new byte[SHA1_HASH_SIZE]; /*
105: * Binary password
106: * loop pointer
107: */
108:
109: if (usingNewPasswords) /* New password version assumed */{
110: int pos = 0;
111:
112: for (int i = 0; i < 4; i++) /* Iterate over these elements */{
113: val = salt[i];
114:
115: for (int t = 3; t >= 0; t--) {
116: binaryPassword[pos++] = (byte) (val & 255);
117: val >>= 8; /* Scroll 8 bits to get next part */
118: }
119: }
120:
121: return binaryPassword;
122: }
123:
124: int offset = 0;
125:
126: for (int i = 0; i < 2; i++) /* Iterate over these elements */{
127: val = salt[i];
128:
129: for (int t = 3; t >= 0; t--) {
130: binaryPassword[t + offset] = (byte) (val % 256);
131: val >>= 8; /* Scroll 8 bits to get next part */
132: }
133:
134: offset += 4;
135: }
136:
137: MessageDigest md = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
138:
139: md.update(binaryPassword, 0, 8);
140:
141: return md.digest();
142: }
143:
144: private static int[] getSaltFromPassword(String password) {
145: int[] result = new int[6];
146:
147: if ((password == null) || (password.length() == 0)) {
148: return result;
149: }
150:
151: if (password.charAt(0) == PVERSION41_CHAR) {
152: // new password
153: String saltInHex = password.substring(1, 5);
154:
155: int val = 0;
156:
157: for (int i = 0; i < 4; i++) {
158: val = (val << 4) + charVal(saltInHex.charAt(i));
159: }
160:
161: return result;
162: }
163:
164: int resultPos = 0;
165: int pos = 0;
166: int length = password.length();
167:
168: while (pos < length) {
169: int val = 0;
170:
171: for (int i = 0; i < 8; i++) {
172: val = (val << 4) + charVal(password.charAt(pos++));
173: }
174:
175: result[resultPos++] = val;
176: }
177:
178: return result;
179: }
180:
181: private static String longToHex(long val) {
182: String longHex = Long.toHexString(val);
183:
184: int length = longHex.length();
185:
186: if (length < 8) {
187: int padding = 8 - length;
188: StringBuffer buf = new StringBuffer();
189:
190: for (int i = 0; i < padding; i++) {
191: buf.append("0"); //$NON-NLS-1$
192: }
193:
194: buf.append(longHex);
195:
196: return buf.toString();
197: }
198:
199: return longHex.substring(0, 8);
200: }
201:
202: /**
203: * Creates password to be stored in user database from raw string.
204: *
205: * Handles Pre-MySQL 4.1 passwords.
206: *
207: * @param password
208: * plaintext password
209: *
210: * @return scrambled password
211: *
212: * @throws NoSuchAlgorithmException
213: * if the message digest 'SHA-1' is not available.
214: */
215: static String makeScrambledPassword(String password)
216: throws NoSuchAlgorithmException {
217: long[] passwordHash = Util.newHash(password);
218: StringBuffer scramble = new StringBuffer();
219:
220: scramble.append(longToHex(passwordHash[0]));
221: scramble.append(longToHex(passwordHash[1]));
222:
223: return scramble.toString();
224: }
225:
226: /**
227: * Encrypt/Decrypt function used for password encryption in authentication
228: *
229: * Simple XOR is used here but it is OK as we crypt random strings
230: *
231: * @param from
232: * IN Data for encryption
233: * @param to
234: * OUT Encrypt data to the buffer (may be the same)
235: * @param password
236: * IN Password used for encryption (same length)
237: * @param length
238: * IN Length of data to encrypt
239: */
240: static void passwordCrypt(byte[] from, byte[] to, byte[] password,
241: int length) {
242: int pos = 0;
243:
244: while ((pos < from.length) && (pos < length)) {
245: to[pos] = (byte) (from[pos] ^ password[pos]);
246: pos++;
247: }
248: }
249:
250: /**
251: * Stage one password hashing, used in MySQL 4.1 password handling
252: *
253: * @param password
254: * plaintext password
255: *
256: * @return stage one hash of password
257: *
258: * @throws NoSuchAlgorithmException
259: * if the message digest 'SHA-1' is not available.
260: */
261: static byte[] passwordHashStage1(String password)
262: throws NoSuchAlgorithmException {
263: MessageDigest md = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
264: StringBuffer cleansedPassword = new StringBuffer();
265:
266: int passwordLength = password.length();
267:
268: for (int i = 0; i < passwordLength; i++) {
269: char c = password.charAt(i);
270:
271: if ((c == ' ') || (c == '\t')) {
272: continue; /* skip space in password */
273: }
274:
275: cleansedPassword.append(c);
276: }
277:
278: return md.digest(cleansedPassword.toString().getBytes());
279: }
280:
281: /**
282: * Stage two password hashing used in MySQL 4.1 password handling
283: *
284: * @param hash
285: * from passwordHashStage1
286: * @param salt
287: * salt used for stage two hashing
288: *
289: * @return result of stage two password hash
290: *
291: * @throws NoSuchAlgorithmException
292: * if the message digest 'SHA-1' is not available.
293: */
294: static byte[] passwordHashStage2(byte[] hashedPassword, byte[] salt)
295: throws NoSuchAlgorithmException {
296: MessageDigest md = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
297:
298: // hash 4 bytes of salt
299: md.update(salt, 0, 4);
300:
301: md.update(hashedPassword, 0, SHA1_HASH_SIZE);
302:
303: return md.digest();
304: }
305:
306: // SERVER: public_seed=create_random_string()
307: // send(public_seed)
308: //
309: // CLIENT: recv(public_seed)
310: // hash_stage1=sha1("password")
311: // hash_stage2=sha1(hash_stage1)
312: // reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
313: //
314: // // this three steps are done in scramble()
315: //
316: // send(reply)
317: //
318: //
319: // SERVER: recv(reply)
320: // hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
321: // candidate_hash2=sha1(hash_stage1)
322: // check(candidate_hash2==hash_stage2)
323: static byte[] scramble411(String password, String seed)
324: throws NoSuchAlgorithmException {
325: MessageDigest md = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
326:
327: byte[] passwordHashStage1 = md.digest(password.getBytes());
328: md.reset();
329:
330: byte[] passwordHashStage2 = md.digest(passwordHashStage1);
331: md.reset();
332:
333: byte[] seedAsBytes = seed.getBytes(); // for debugging
334: md.update(seedAsBytes);
335: md.update(passwordHashStage2);
336:
337: byte[] toBeXord = md.digest();
338:
339: int numToXor = toBeXord.length;
340:
341: for (int i = 0; i < numToXor; i++) {
342: toBeXord[i] = (byte) (toBeXord[i] ^ passwordHashStage1[i]);
343: }
344:
345: return toBeXord;
346: }
347:
348: /**
349: * Prevent construction.
350: */
351: private Security() {
352: super();
353: }
354: }
|