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
use bytes::buf::Chain;
use bytes::Bytes;
use digest::{Digest, FixedOutput};
use generic_array::GenericArray;
use rand::thread_rng;
use rsa::{pkcs8::FromPublicKey, PaddingScheme, PublicKey, RsaPublicKey};
use sha1::Sha1;
use sha2::Sha256;
use crate::error::Error;
use crate::mysql::connection::stream::MySqlStream;
use crate::mysql::protocol::auth::AuthPlugin;
use crate::mysql::protocol::Packet;
impl AuthPlugin {
pub(super) async fn scramble(
self,
stream: &mut MySqlStream,
password: &str,
nonce: &Chain<Bytes, Bytes>,
) -> Result<Vec<u8>, Error> {
match self {
AuthPlugin::CachingSha2Password => Ok(scramble_sha256(password, nonce).to_vec()),
AuthPlugin::MySqlNativePassword => Ok(scramble_sha1(password, nonce).to_vec()),
AuthPlugin::Sha256Password => encrypt_rsa(stream, 0x01, password, nonce).await,
}
}
pub(super) async fn handle(
self,
stream: &mut MySqlStream,
packet: Packet<Bytes>,
password: &str,
nonce: &Chain<Bytes, Bytes>,
) -> Result<bool, Error> {
match self {
AuthPlugin::CachingSha2Password if packet[0] == 0x01 => {
match packet[1] {
0x03 => Ok(true),
0x04 => {
let payload = encrypt_rsa(stream, 0x02, password, nonce).await?;
stream.write_packet(&*payload);
stream.flush().await?;
Ok(false)
}
v => {
Err(err_protocol!("unexpected result from fast authentication 0x{:x} when expecting 0x03 (AUTH_OK) or 0x04 (AUTH_CONTINUE)", v))
}
}
}
_ => Err(err_protocol!(
"unexpected packet 0x{:02x} for auth plugin '{}' during authentication",
packet[0],
self.name()
)),
}
}
}
fn scramble_sha1(
password: &str,
nonce: &Chain<Bytes, Bytes>,
) -> GenericArray<u8, <Sha1 as FixedOutput>::OutputSize> {
let mut ctx = Sha1::new();
ctx.update(password);
let mut pw_hash = ctx.finalize_reset();
ctx.update(&pw_hash);
let pw_hash_hash = ctx.finalize_reset();
ctx.update(nonce.first_ref());
ctx.update(nonce.last_ref());
ctx.update(pw_hash_hash);
let pw_seed_hash_hash = ctx.finalize();
xor_eq(&mut pw_hash, &pw_seed_hash_hash);
pw_hash
}
fn scramble_sha256(
password: &str,
nonce: &Chain<Bytes, Bytes>,
) -> GenericArray<u8, <Sha256 as FixedOutput>::OutputSize> {
let mut ctx = Sha256::new();
ctx.update(password);
let mut pw_hash = ctx.finalize_reset();
ctx.update(&pw_hash);
let pw_hash_hash = ctx.finalize_reset();
ctx.update(nonce.first_ref());
ctx.update(nonce.last_ref());
ctx.update(pw_hash_hash);
let pw_seed_hash_hash = ctx.finalize();
xor_eq(&mut pw_hash, &pw_seed_hash_hash);
pw_hash
}
async fn encrypt_rsa<'s>(
stream: &'s mut MySqlStream,
public_key_request_id: u8,
password: &'s str,
nonce: &'s Chain<Bytes, Bytes>,
) -> Result<Vec<u8>, Error> {
if stream.is_tls() {
return Ok(to_asciz(password));
}
stream.write_packet(&[public_key_request_id][..]);
stream.flush().await?;
let packet = stream.recv_packet().await?;
let rsa_pub_key = &packet[1..];
let mut pass = to_asciz(password);
let (a, b) = (nonce.first_ref(), nonce.last_ref());
let mut nonce = Vec::with_capacity(a.len() + b.len());
nonce.extend_from_slice(&*a);
nonce.extend_from_slice(&*b);
xor_eq(&mut pass, &*nonce);
let pkey = parse_rsa_pub_key(rsa_pub_key)?;
let padding = PaddingScheme::new_oaep::<sha1::Sha1>();
pkey.encrypt(&mut thread_rng(), padding, &pass[..])
.map_err(Error::protocol)
}
fn xor_eq(x: &mut [u8], y: &[u8]) {
let y_len = y.len();
for i in 0..x.len() {
x[i] ^= y[i % y_len];
}
}
fn to_asciz(s: &str) -> Vec<u8> {
let mut z = String::with_capacity(s.len() + 1);
z.push_str(s);
z.push('\0');
z.into_bytes()
}
fn parse_rsa_pub_key(key: &[u8]) -> Result<RsaPublicKey, Error> {
let pem = std::str::from_utf8(key).map_err(Error::protocol)?;
RsaPublicKey::from_public_key_pem(&pem).map_err(Error::protocol)
}