Security.java
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/J is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
this software, see the FOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
Floor, Boston, MA 02110-1301 USA
*/
package com.mysql.jdbc;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Methods for doing secure authentication with MySQL-4.1 and newer.
*/
public class Security {
private static final char PVERSION41_CHAR = '*';
private static final int SHA1_HASH_SIZE = 20;
/**
* Returns hex value for given char
*/
private static int charVal(char c) {
return ((c >= '0') && (c <= '9')) ? (c - '0') : (((c >= 'A') && (c <= 'Z')) ? (c - 'A' + 10) : (c - 'a' + 10));
}
/*
* Convert password in salted form to binary string password and hash-salt
* For old password this involes one more hashing
*
* SYNOPSIS get_hash_and_password() salt IN Salt to convert from pversion IN
* Password version to use hash OUT Store zero ended hash here bin_password
* OUT Store binary password here (no zero at the end)
*
* RETURN 0 for pre 4.1 passwords !0 password version char for newer
* passwords
*/
/**
* Creates key from old password to decode scramble Used in 4.1
* authentication with passwords stored pre-4.1 hashing.
*
* @param passwd
* the password to create the key from
*
* @return 20 byte generated key
*
* @throws NoSuchAlgorithmException
* if the message digest 'SHA-1' is not available.
*/
static byte[] createKeyFromOldPassword(String passwd) throws NoSuchAlgorithmException {
/* At first hash password to the string stored in password */
passwd = makeScrambledPassword(passwd);
/* Now convert it to the salt form */
int[] salt = getSaltFromPassword(passwd);
/* Finally get hash and bin password from salt */
return getBinaryPassword(salt, false);
}
/**
* @param salt
* @param usingNewPasswords
*
* @throws NoSuchAlgorithmException
* if the message digest 'SHA-1' is not available.
*/
static byte[] getBinaryPassword(int[] salt, boolean usingNewPasswords) throws NoSuchAlgorithmException {
int val = 0;
byte[] binaryPassword = new byte[SHA1_HASH_SIZE]; /* Binary password loop pointer */
if (usingNewPasswords) /* New password version assumed */ {
int pos = 0;
for (int i = 0; i < 4; i++) /* Iterate over these elements */ {
val = salt[i];
for (int t = 3; t >= 0; t--) {
binaryPassword[pos++] = (byte) (val & 255);
val >>= 8; /* Scroll 8 bits to get next part */
}
}
return binaryPassword;
}
int offset = 0;
for (int i = 0; i < 2; i++) /* Iterate over these elements */ {
val = salt[i];
for (int t = 3; t >= 0; t--) {
binaryPassword[t + offset] = (byte) (val % 256);
val >>= 8; /* Scroll 8 bits to get next part */
}
offset += 4;
}
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(binaryPassword, 0, 8);
return md.digest();
}
private static int[] getSaltFromPassword(String password) {
int[] result = new int[6];
if ((password == null) || (password.length() == 0)) {
return result;
}
if (password.charAt(0) == PVERSION41_CHAR) {
// new password
String saltInHex = password.substring(1, 5);
int val = 0;
for (int i = 0; i < 4; i++) {
val = (val << 4) + charVal(saltInHex.charAt(i));
}
return result;
}
int resultPos = 0;
int pos = 0;
int length = password.length();
while (pos < length) {
int val = 0;
for (int i = 0; i < 8; i++) {
val = (val << 4) + charVal(password.charAt(pos++));
}
result[resultPos++] = val;
}
return result;
}
private static String longToHex(long val) {
String longHex = Long.toHexString(val);
int length = longHex.length();
if (length < 8) {
int padding = 8 - length;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < padding; i++) {
buf.append("0");
}
buf.append(longHex);
return buf.toString();
}
return longHex.substring(0, 8);
}
/**
* Creates password to be stored in user database from raw string.
*
* Handles Pre-MySQL 4.1 passwords.
*
* @param password
* plaintext password
*
* @return scrambled password
*
* @throws NoSuchAlgorithmException
* if the message digest 'SHA-1' is not available.
*/
static String makeScrambledPassword(String password) throws NoSuchAlgorithmException {
long[] passwordHash = Util.hashPre41Password(password);
StringBuilder scramble = new StringBuilder();
scramble.append(longToHex(passwordHash[0]));
scramble.append(longToHex(passwordHash[1]));
return scramble.toString();
}
/**
* Encrypt/Decrypt function used for password encryption in authentication
*
* Simple XOR is used here but it is OK as we crypt random strings
*
* @param from
* IN Data for encryption
* @param to
* OUT Encrypt data to the buffer (may be the same)
* @param scramble
* IN Scramble used for encryption
* @param length
* IN Length of data to encrypt
*/
public static void xorString(byte[] from, byte[] to, byte[] scramble, int length) {
int pos = 0;
int scrambleLength = scramble.length;
while (pos < length) {
to[pos] = (byte) (from[pos] ^ scramble[pos % scrambleLength]);
pos++;
}
}
/**
* Stage one password hashing, used in MySQL 4.1 password handling
*
* @param password
* plaintext password
*
* @return stage one hash of password
*
* @throws NoSuchAlgorithmException
* if the message digest 'SHA-1' is not available.
*/
static byte[] passwordHashStage1(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
StringBuilder cleansedPassword = new StringBuilder();
int passwordLength = password.length();
for (int i = 0; i < passwordLength; i++) {
char c = password.charAt(i);
if ((c == ' ') || (c == '\t')) {
continue; /* skip space in password */
}
cleansedPassword.append(c);
}
return md.digest(StringUtils.getBytes(cleansedPassword.toString()));
}
/**
* Stage two password hashing used in MySQL 4.1 password handling
*
* @param hash
* from passwordHashStage1
* @param salt
* salt used for stage two hashing
*
* @return result of stage two password hash
*
* @throws NoSuchAlgorithmException
* if the message digest 'SHA-1' is not available.
*/
static byte[] passwordHashStage2(byte[] hashedPassword, byte[] salt) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
// hash 4 bytes of salt
md.update(salt, 0, 4);
md.update(hashedPassword, 0, SHA1_HASH_SIZE);
return md.digest();
}
// SERVER: public_seed=create_random_string()
// send(public_seed)
//
// CLIENT: recv(public_seed)
// hash_stage1=sha1("password")
// hash_stage2=sha1(hash_stage1)
// reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
//
// // this three steps are done in scramble()
//
// send(reply)
//
//
// SERVER: recv(reply)
// hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
// candidate_hash2=sha1(hash_stage1)
// check(candidate_hash2==hash_stage2)
public static byte[] scramble411(String password, String seed, String passwordEncoding) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] passwordHashStage1 = md.digest((passwordEncoding == null || passwordEncoding.length() == 0) ? StringUtils.getBytes(password)
: StringUtils.getBytes(password, passwordEncoding));
md.reset();
byte[] passwordHashStage2 = md.digest(passwordHashStage1);
md.reset();
byte[] seedAsBytes = StringUtils.getBytes(seed, "ASCII"); // for debugging
md.update(seedAsBytes);
md.update(passwordHashStage2);
byte[] toBeXord = md.digest();
int numToXor = toBeXord.length;
for (int i = 0; i < numToXor; i++) {
toBeXord[i] = (byte) (toBeXord[i] ^ passwordHashStage1[i]);
}
return toBeXord;
}
/**
* Prevent construction.
*/
private Security() {
super();
}
}