Skip to content

Commit 598fc0f

Browse files
authored
Merge pull request #618 from sfackler/update-mac
Update hmac and sha2
2 parents 2b59b7e + c845a36 commit 598fc0f

File tree

7 files changed

+58
-74
lines changed

7 files changed

+58
-74
lines changed

codegen/src/type_gen.rs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -319,46 +319,32 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
319319
.unwrap();
320320

321321
for (oid, type_) in types {
322-
write!(
323-
w,
324-
" {} => Some(Inner::{}),
325-
",
326-
oid, type_.variant
327-
)
328-
.unwrap();
322+
writeln!(w, " {} => Some(Inner::{}),", oid, type_.variant).unwrap();
329323
}
330324

331-
write!(
325+
writeln!(
332326
w,
333327
" _ => None,
334328
}}
335329
}}
336330
337331
pub fn oid(&self) -> Oid {{
338-
match *self {{
339-
",
332+
match *self {{",
340333
)
341334
.unwrap();
342335

343336
for (oid, type_) in types {
344-
write!(
345-
w,
346-
" Inner::{} => {},
347-
",
348-
type_.variant, oid
349-
)
350-
.unwrap();
337+
writeln!(w, " Inner::{} => {},", type_.variant, oid).unwrap();
351338
}
352339

353-
write!(
340+
writeln!(
354341
w,
355342
" Inner::Other(ref u) => u.oid,
356343
}}
357344
}}
358345
359346
pub fn kind(&self) -> &Kind {{
360-
match *self {{
361-
",
347+
match *self {{",
362348
)
363349
.unwrap();
364350

@@ -370,59 +356,54 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
370356
_ => "Simple".to_owned(),
371357
};
372358

373-
write!(
359+
writeln!(
374360
w,
375361
" Inner::{} => {{
376362
&Kind::{}
377-
}}
378-
",
363+
}}",
379364
type_.variant, kind
380365
)
381366
.unwrap();
382367
}
383368

384-
write!(
369+
writeln!(
385370
w,
386371
r#" Inner::Other(ref u) => &u.kind,
387372
}}
388373
}}
389374
390375
pub fn name(&self) -> &str {{
391-
match *self {{
392-
"#,
376+
match *self {{"#,
393377
)
394378
.unwrap();
395379

396380
for type_ in types.values() {
397-
write!(
381+
writeln!(
398382
w,
399-
r#" Inner::{} => "{}",
400-
"#,
383+
r#" Inner::{} => "{}","#,
401384
type_.variant, type_.name
402385
)
403386
.unwrap();
404387
}
405388

406-
write!(
389+
writeln!(
407390
w,
408391
" Inner::Other(ref u) => &u.name,
409392
}}
410393
}}
411-
}}
412-
"
394+
}}"
413395
)
414396
.unwrap();
415397
}
416398

417399
fn make_consts(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
418400
write!(w, "impl Type {{").unwrap();
419401
for type_ in types.values() {
420-
write!(
402+
writeln!(
421403
w,
422404
"
423405
/// {docs}
424-
pub const {ident}: Type = Type(Inner::{variant});
425-
",
406+
pub const {ident}: Type = Type(Inner::{variant});",
426407
docs = type_.doc,
427408
ident = type_.ident,
428409
variant = type_.variant

postgres-protocol/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ base64 = "0.12"
1313
byteorder = "1.0"
1414
bytes = "0.5"
1515
fallible-iterator = "0.2"
16-
hmac = "0.7"
16+
hmac = "0.8"
1717
md5 = "0.7"
1818
memchr = "2.0"
1919
rand = "0.7"
20-
sha2 = "0.8"
20+
sha2 = "0.9"
2121
stringprep = "0.1"

postgres-protocol/src/authentication/sasl.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! SASL-based authentication support.
22
3-
use hmac::{Hmac, Mac};
3+
use hmac::{Hmac, Mac, NewMac};
44
use rand::{self, Rng};
5+
use sha2::digest::FixedOutput;
56
use sha2::{Digest, Sha256};
67
use std::fmt::Write;
78
use std::io;
@@ -33,16 +34,16 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
3334

3435
fn hi(str: &[u8], salt: &[u8], i: u32) -> [u8; 32] {
3536
let mut hmac = Hmac::<Sha256>::new_varkey(str).expect("HMAC is able to accept all key sizes");
36-
hmac.input(salt);
37-
hmac.input(&[0, 0, 0, 1]);
38-
let mut prev = hmac.result().code();
37+
hmac.update(salt);
38+
hmac.update(&[0, 0, 0, 1]);
39+
let mut prev = hmac.finalize().into_bytes();
3940

4041
let mut hi = prev;
4142

4243
for _ in 1..i {
4344
let mut hmac = Hmac::<Sha256>::new_varkey(str).expect("already checked above");
44-
hmac.input(prev.as_slice());
45-
prev = hmac.result().code();
45+
hmac.update(&prev);
46+
prev = hmac.finalize().into_bytes();
4647

4748
for (hi, prev) in hi.iter_mut().zip(prev) {
4849
*hi ^= prev;
@@ -196,12 +197,12 @@ impl ScramSha256 {
196197

197198
let mut hmac = Hmac::<Sha256>::new_varkey(&salted_password)
198199
.expect("HMAC is able to accept all key sizes");
199-
hmac.input(b"Client Key");
200-
let client_key = hmac.result().code();
200+
hmac.update(b"Client Key");
201+
let client_key = hmac.finalize().into_bytes();
201202

202203
let mut hash = Sha256::default();
203-
hash.input(client_key.as_slice());
204-
let stored_key = hash.result();
204+
hash.update(client_key.as_slice());
205+
let stored_key = hash.finalize_fixed();
205206

206207
let mut cbind_input = vec![];
207208
cbind_input.extend(channel_binding.gs2_header().as_bytes());
@@ -215,11 +216,11 @@ impl ScramSha256 {
215216

216217
let mut hmac =
217218
Hmac::<Sha256>::new_varkey(&stored_key).expect("HMAC is able to accept all key sizes");
218-
hmac.input(auth_message.as_bytes());
219-
let client_signature = hmac.result();
219+
hmac.update(auth_message.as_bytes());
220+
let client_signature = hmac.finalize().into_bytes();
220221

221222
let mut client_proof = client_key;
222-
for (proof, signature) in client_proof.iter_mut().zip(client_signature.code()) {
223+
for (proof, signature) in client_proof.iter_mut().zip(client_signature) {
223224
*proof ^= signature;
224225
}
225226

@@ -267,12 +268,12 @@ impl ScramSha256 {
267268

268269
let mut hmac = Hmac::<Sha256>::new_varkey(&salted_password)
269270
.expect("HMAC is able to accept all key sizes");
270-
hmac.input(b"Server Key");
271-
let server_key = hmac.result();
271+
hmac.update(b"Server Key");
272+
let server_key = hmac.finalize().into_bytes();
272273

273-
let mut hmac = Hmac::<Sha256>::new_varkey(&server_key.code())
274-
.expect("HMAC is able to accept all key sizes");
275-
hmac.input(auth_message.as_bytes());
274+
let mut hmac =
275+
Hmac::<Sha256>::new_varkey(&server_key).expect("HMAC is able to accept all key sizes");
276+
hmac.update(auth_message.as_bytes());
276277
hmac.verify(&verifier)
277278
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "SCRAM verification error"))
278279
}

postgres-types/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,17 @@ macro_rules! accepts {
158158
#[macro_export]
159159
macro_rules! to_sql_checked {
160160
() => {
161-
fn to_sql_checked(&self,
162-
ty: &$crate::Type,
163-
out: &mut $crate::private::BytesMut)
164-
-> ::std::result::Result<$crate::IsNull,
165-
Box<dyn ::std::error::Error +
166-
::std::marker::Sync +
167-
::std::marker::Send>> {
161+
fn to_sql_checked(
162+
&self,
163+
ty: &$crate::Type,
164+
out: &mut $crate::private::BytesMut,
165+
) -> ::std::result::Result<
166+
$crate::IsNull,
167+
Box<dyn ::std::error::Error + ::std::marker::Sync + ::std::marker::Send>,
168+
> {
168169
$crate::__to_sql_checked(self, ty, out)
169170
}
170-
}
171+
};
171172
}
172173

173174
// WARNING: this function is not considered part of this crate's public API.

postgres/src/copy_out_reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl BufRead for CopyOutReader<'_> {
3838
let mut stream = self.stream.pinned();
3939
match self
4040
.connection
41-
.block_on({ async { stream.next().await.transpose() } })
41+
.block_on(async { stream.next().await.transpose() })
4242
{
4343
Ok(Some(cur)) => self.cur = cur,
4444
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),

tokio-postgres/src/error/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl DbError {
224224
///
225225
/// Might run to multiple lines.
226226
pub fn detail(&self) -> Option<&str> {
227-
self.detail.as_ref().map(|s| &**s)
227+
self.detail.as_deref()
228228
}
229229

230230
/// An optional suggestion what to do about the problem.
@@ -233,7 +233,7 @@ impl DbError {
233233
/// (potentially inappropriate) rather than hard facts. Might run to
234234
/// multiple lines.
235235
pub fn hint(&self) -> Option<&str> {
236-
self.hint.as_ref().map(|s| &**s)
236+
self.hint.as_deref()
237237
}
238238

239239
/// An optional error cursor position into either the original query string
@@ -248,20 +248,20 @@ impl DbError {
248248
/// language functions and internally-generated queries. The trace is one
249249
/// entry per line, most recent first.
250250
pub fn where_(&self) -> Option<&str> {
251-
self.where_.as_ref().map(|s| &**s)
251+
self.where_.as_deref()
252252
}
253253

254254
/// If the error was associated with a specific database object, the name
255255
/// of the schema containing that object, if any. (PostgreSQL 9.3+)
256256
pub fn schema(&self) -> Option<&str> {
257-
self.schema.as_ref().map(|s| &**s)
257+
self.schema.as_deref()
258258
}
259259

260260
/// If the error was associated with a specific table, the name of the
261261
/// table. (Refer to the schema name field for the name of the table's
262262
/// schema.) (PostgreSQL 9.3+)
263263
pub fn table(&self) -> Option<&str> {
264-
self.table.as_ref().map(|s| &**s)
264+
self.table.as_deref()
265265
}
266266

267267
/// If the error was associated with a specific table column, the name of
@@ -270,14 +270,14 @@ impl DbError {
270270
/// (Refer to the schema and table name fields to identify the table.)
271271
/// (PostgreSQL 9.3+)
272272
pub fn column(&self) -> Option<&str> {
273-
self.column.as_ref().map(|s| &**s)
273+
self.column.as_deref()
274274
}
275275

276276
/// If the error was associated with a specific data type, the name of the
277277
/// data type. (Refer to the schema name field for the name of the data
278278
/// type's schema.) (PostgreSQL 9.3+)
279279
pub fn datatype(&self) -> Option<&str> {
280-
self.datatype.as_ref().map(|s| &**s)
280+
self.datatype.as_deref()
281281
}
282282

283283
/// If the error was associated with a specific constraint, the name of the
@@ -287,12 +287,12 @@ impl DbError {
287287
/// (For this purpose, indexes are treated as constraints, even if they
288288
/// weren't created with constraint syntax.) (PostgreSQL 9.3+)
289289
pub fn constraint(&self) -> Option<&str> {
290-
self.constraint.as_ref().map(|s| &**s)
290+
self.constraint.as_deref()
291291
}
292292

293293
/// The file name of the source-code location where the error was reported.
294294
pub fn file(&self) -> Option<&str> {
295-
self.file.as_ref().map(|s| &**s)
295+
self.file.as_deref()
296296
}
297297

298298
/// The line number of the source-code location where the error was
@@ -303,7 +303,7 @@ impl DbError {
303303

304304
/// The name of the source-code routine reporting the error.
305305
pub fn routine(&self) -> Option<&str> {
306-
self.routine.as_ref().map(|s| &**s)
306+
self.routine.as_deref()
307307
}
308308
}
309309

tokio-postgres/src/generic_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl GenericClient for Client {
146146
impl private::Sealed for Transaction<'_> {}
147147

148148
#[async_trait]
149+
#[allow(clippy::needless_lifetimes)]
149150
impl GenericClient for Transaction<'_> {
150151
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
151152
where

0 commit comments

Comments
 (0)