<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate base64;
extern crate crypto;
extern crate u2fhid;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use std::io;
use std::sync::mpsc::channel;
use u2fhid::{AuthenticatorTransports, KeyHandle, RegisterFlags, SignFlags, U2FManager};

extern crate log;
extern crate env_logger;

fn u2f_get_key_handle_from_register_response(register_response: &Vec<u8>) -> io::Result<Vec<u8>> {
    if register_response[0] != 0x05 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "Reserved byte not set correctly",
        ));
    }

    let key_handle_len = register_response[66] as usize;
    let mut public_key = register_response.clone();
    let mut key_handle = public_key.split_off(67);
    let _attestation = key_handle.split_off(key_handle_len);

    Ok(key_handle)
}

fn main() {
    env_logger::init().expect("Cannot start logger");

    println!("Asking a security key to register now...");
    let challenge_str =
        format!("{}{}",
        r#"{"challenge": "1vQ9mxionq0ngCnjD-wTsv1zUSrGRtFqG2xP09SbZ70","#,
        r#" "version": "U2F_V2", "appId": "http://demo.yubico.com"}"#);
    let mut challenge = Sha256::new();
    challenge.input_str(&challenge_str);
    let mut chall_bytes: Vec<u8> = vec![0; challenge.output_bytes()];
    challenge.result(&mut chall_bytes);

    let mut application = Sha256::new();
    application.input_str("http://demo.yubico.com");
    let mut app_bytes: Vec<u8> = vec![0; application.output_bytes()];
    application.result(&mut app_bytes);

    let manager = U2FManager::new().unwrap();
    let flags = RegisterFlags::empty();

    let (tx, rx) = channel();
    manager
        .register(
            flags,
            15_000,
            chall_bytes.clone(),
            app_bytes.clone(),
            vec![],
            move |rv| { tx.send(rv.unwrap()).unwrap(); },
        )
        .unwrap();

    let register_data = rx.recv().unwrap();
    println!("Register result: {}", base64::encode(&register_data));
    println!("Asking a security key to sign now, with the data from the register...");
    let credential = u2f_get_key_handle_from_register_response(&register_data).unwrap();
    let key_handle = KeyHandle {
        credential,
        transports: AuthenticatorTransports::empty(),
    };

    let flags = SignFlags::empty();
    let (tx, rx) = channel();
    manager
        .sign(
            flags,
            15_000,
            chall_bytes,
            app_bytes,
            vec![key_handle],
            move |rv| { tx.send(rv.unwrap()).unwrap(); },
        )
        .unwrap();

    let (_, sign_data) = rx.recv().unwrap();
    println!("Sign result: {}", base64::encode(&sign_data));
    println!("Done.");
}<commit_msg>Be a bit safer with the example tool<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate base64;
extern crate crypto;
extern crate u2fhid;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use std::io;
use std::sync::mpsc::channel;
use u2fhid::{AuthenticatorTransports, KeyHandle, RegisterFlags, SignFlags, U2FManager};

extern crate log;
extern crate env_logger;

fn u2f_get_key_handle_from_register_response(register_response: &Vec<u8>) -> io::Result<Vec<u8>> {
    if register_response[0] != 0x05 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "Reserved byte not set correctly",
        ));
    }

    let key_handle_len = register_response[66] as usize;
    let mut public_key = register_response.clone();
    let mut key_handle = public_key.split_off(67);
    let _attestation = key_handle.split_off(key_handle_len);

    Ok(key_handle)
}

fn main() {
    env_logger::init().expect("Cannot start logger");

    println!("Asking a security key to register now...");
    let challenge_str =
        format!("{}{}",
        r#"{"challenge": "1vQ9mxionq0ngCnjD-wTsv1zUSrGRtFqG2xP09SbZ70","#,
        r#" "version": "U2F_V2", "appId": "http://demo.yubico.com"}"#);
    let mut challenge = Sha256::new();
    challenge.input_str(&challenge_str);
    let mut chall_bytes: Vec<u8> = vec![0; challenge.output_bytes()];
    challenge.result(&mut chall_bytes);

    let mut application = Sha256::new();
    application.input_str("http://demo.yubico.com");
    let mut app_bytes: Vec<u8> = vec![0; application.output_bytes()];
    application.result(&mut app_bytes);

    let manager = U2FManager::new().unwrap();
    let flags = RegisterFlags::empty();

    let (tx, rx) = channel();
    manager
        .register(
            flags,
            15_000,
            chall_bytes.clone(),
            app_bytes.clone(),
            vec![],
            move |rv| { tx.send(rv.unwrap()).unwrap(); },
        )
        .unwrap();

    let register_data = try_or!(rx.recv(), |_| {
        panic!("Problem receiving, unable to continue");
        return;
    });
    println!("Register result: {}", base64::encode(&register_data));
    println!("Asking a security key to sign now, with the data from the register...");
    let credential = u2f_get_key_handle_from_register_response(&register_data).unwrap();
    let key_handle = KeyHandle {
        credential,
        transports: AuthenticatorTransports::empty(),
    };

    let flags = SignFlags::empty();
    let (tx, rx) = channel();
    manager
        .sign(
            flags,
            15_000,
            chall_bytes,
            app_bytes,
            vec![key_handle],
            move |rv| { tx.send(rv.unwrap()).unwrap(); },
        )
        .unwrap();

    let (_, sign_data) = try_or!(rx.recv(), |_| {
        println!("Problem receiving");
    });
    println!("Sign result: {}", base64::encode(&sign_data));
    println!("Done.");
}
{'fraction_non_alphanumeric': 0.14692801844836106, 'fraction_numerical': 0.02009553615549333, 'mean_word_length': 2.9686274509803923, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6670536', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1930, 'n_words': 451}