Skip to content

Commit 3b47870

Browse files
author
Flavio Oliveira
committed
Compare the signatures before base64 encoding
1 parent 624614d commit 3b47870

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/wisespace-io/yubico-rs/blob/master/LICENSE)
2-
[![](https://meritbadge.herokuapp.com/yubico)](https://crates.io/crates/yubico)
1+
[![Build Status](https://travis-ci.org/wisespace-io/yubico-rs.png?branch=master)](https://travis-ci.org/wisespace-io/yubico-rs)
2+
[![Crates.io](https://img.shields.io/crates/v/yubico.svg)](https://crates.io/crates/yubico)
33

44
# Yubico
55
Yubikey client API library, [validation protocol version 2.0](https://developers.yubico.com/yubikey-val/Validation_Protocol_V2.0.html).

src/.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: rust
2+
rust:
3+
- nightly
4+
- stable
5+
sudo: false

src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use hyper::Client;
1212
use hyper::header::{Headers};
1313
use std::io::prelude::*;
1414
use base64::{encode, decode};
15-
use crypto::mac::{Mac};
15+
use crypto::mac::{Mac, MacResult};
1616
use crypto::hmac::Hmac;
1717
use crypto::sha1::Sha1;
1818
use rand::{thread_rng, Rng};
@@ -74,13 +74,15 @@ impl Yubico {
7474
let mut query = format!("id={}&nonce={}&otp={}&sl=secure", self.client_id, nonce, otp);
7575

7676
let signature = self.build_signature(query.clone());
77+
// Base 64 encode the resulting value according to RFC 4648
78+
let encoded_signature = encode(signature.code());
7779

7880
// Append the value under key h to the message.
79-
let signature_param = format!("&h={}", signature);
81+
let signature_param = format!("&h={}", encoded_signature);
8082
let encoded = utf8_percent_encode(signature_param.as_ref(), QUERY_ENCODE_SET).collect::<String>();
8183
query.push_str(encoded.as_ref());
8284

83-
let request = Request {otp: otp, nonce: nonce, signature: signature, query: query};
85+
let request = Request {otp: otp, nonce: nonce, signature: encoded_signature, query: query};
8486

8587
let pool = ThreadPool::new(3);
8688
let (tx, rx) = channel();
@@ -122,12 +124,10 @@ impl Yubico {
122124
}
123125

124126
// 1. Apply the HMAC-SHA-1 algorithm on the line as an octet string using the API key as key
125-
// 2. Base 64 encode the resulting value according to RFC 4648
126-
fn build_signature(&self, query: String) -> String {
127+
fn build_signature(&self, query: String) -> MacResult {
127128
let mut hmac = Hmac::new(Sha1::new(), &self.key[..]);
128129
hmac.input(query.as_bytes());
129-
let signature = encode(hmac.result().code());
130-
format!("{}", signature)
130+
hmac.result()
131131
}
132132

133133
// Recommendation is that clients only check that the input consists of 32-48 printable characters
@@ -195,7 +195,10 @@ impl Yubico {
195195

196196
let signature = self.build_signature(query.clone());
197197

198-
if signature == signature_response { true } else { false }
198+
199+
let decoded_signature = &decode(signature_response).unwrap()[..];
200+
201+
crypto::util::fixed_time_eq(signature.code(), decoded_signature)
199202
}
200203

201204
fn build_response_map(&self, result: String) -> BTreeMap<String, String> {

0 commit comments

Comments
 (0)