add migration from old to new version

confirmed this works for U2F and FIDO2 keys

however, the PIN is reset.
This commit is contained in:
bunnie 2023-01-25 15:34:54 +08:00
parent c083a9ca85
commit 8974a86f24
4 changed files with 245 additions and 88 deletions

View file

@ -286,6 +286,13 @@
"ja": "シークレットベースのロックを解除",
"zh": "解锁秘密基础"
},
"vault.migration_error": {
"en": "Migration from previous FIDO database encountered errors:",
"en-tts": "Migration from previous FIDO database encountered errors:",
"fr": "La migration depuis la base de données FIDO précédente a rencontré des erreurs: *MT*",
"ja": "以前の FIDO データベースからの移行でエラーが発生しました:",
"zh": "从以前的 FIDO 数据库迁移遇到错误:"
},
"vault.newitem.approve": {
"en": "Is this password okay?",
"en-tts": "Is this password okay?",
@ -461,13 +468,6 @@
"ja": "アプリ名:",
"zh": "应用名称:"
},
"vault.u2f.appinfo.newapp": {
"en": "New app: ",
"en-tts": "New app: ",
"fr": "Nouvelle application: ",
"ja": "新しいアプリ: ",
"zh": "新应用: "
},
"vault.u2f.appinfo.never": {
"en": "never",
"en-tts": "never",
@ -475,6 +475,13 @@
"ja": "使われたことがない",
"zh": "没用过"
},
"vault.u2f.appinfo.newapp": {
"en": "New app: ",
"en-tts": "New app: ",
"fr": "Nouvelle application: ",
"ja": "新しいアプリ: ",
"zh": "新应用: "
},
"vault.u2f.appinfo.seconds_ago": {
"en": " secs ago",
"en-tts": " seconds ago",

View file

@ -19,7 +19,6 @@ use pddb::Pddb;
use modals::Modals;
use locales::t;
use std::io::{Read, Write};
use std::num::ParseIntError;
use num_traits::*;
use crate::ctap::hid::{CtapHid, KeepaliveStatus, ProcessedPacket, CtapHidCommand};
@ -29,13 +28,6 @@ pub const U2F_APP_DICT: &'static str = "fido.u2fapps";
const KEEPALIVE_DELAY: Duration = Duration::from_millis(KEEPALIVE_DELAY_MS);
mod storage;
fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect()
}
pub struct XousHidConnection {
pub endpoint: usb_device_xous::UsbHid,
}
@ -176,7 +168,7 @@ impl XousEnv {
if (crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) > (now - request_start) {
let new_remaining = ((crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) - (now - request_start)) / 1000;
if new_remaining != last_remaining {
log::trace!("update timer");
log::info!("countdown: {}", new_remaining);
last_remaining = new_remaining;
let mut to_request_str = request_str.to_string();
to_request_str.push_str(
@ -483,67 +475,6 @@ impl XousEnv {
Ok(())
}
/// The point of the attestation key is to delegate the question of the authenticity of your U2F
/// key effectively to a transaction that happens between the factory and the server. If the
/// attestation private key is an impenetrable secret and you can trust the factory, then the
/// attestation key effectively proves the U2F token was made by the factory to its specifications.
///
/// In the case of Precursor's threat model, you don't trust the factory, and you assume there is
/// basically no such thing as an impenetrable secret. So an attestation key is equivalent to any
/// private key created entirely within your own device; its use proves that you're you, or at
/// least, someone has possession of your device and your unlock password.
///
/// Thus, for Precursor, there is no delegation of the question of authenticity -- you
/// choose to verify the device to the level you're satisfied with. If you're happy to
/// just take it out of the box and use it without checking anything, that's your choice,
/// but you can also check everything as much as you like.
///
/// As a result, the "attestation key" serves no role for Precursor users, other than to
/// identify them as Precursor users. A unique per-device key would furthermore allow
/// a server to uniquely identify each Precursor user. Therefore, we commit the private key
/// into this public repository so any and all Precursor users may share it. The more
/// widely it is used, the harder it becomes to de-anonymize a Precursor users.
///
/// This key was generated using the script in `tools/gen_key_materials.sh`, and then printed
/// out by calling
/// `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem`
pub fn attestation_check_init(&mut self) {
// check if the certs exist; if they don't, populate them.
// STORAGE_KEYS[0] is the private key; [1] is the public key
match self.store().find(crate::api::attestation_store::STORAGE_KEYS[0]) {
Ok(Some(_)) => {},
Ok(None) |
Err(_) => {
let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap();
self.store().insert(
crate::api::attestation_store::STORAGE_KEYS[0],
&pk
).expect("pddb error initializing OpenSK");
}
}
match self.store().find(crate::api::attestation_store::STORAGE_KEYS[1]) {
Ok(Some(_)) => {}
Ok(None) |
Err(_) => {
let der = decode_hex(
"308201423081e9021449a3e2a4e1078eae2f1a18567f0a734b09db2478300a06\
082a8648ce3d040302301f311d301b06035504030c14507265637572736f7220\
55324620444959204341301e170d3232303630353138313233395a170d333230\
3630343138313233395a30293127302506035504030c1e507265637572736f72\
2053656c662d5665726966696564204465766963653059301306072a8648ce3d\
020106082a8648ce3d030107034200042d8fad76c25851ee70ae651ab2361bee\
1b5d93769aff99d95ad112871060f2f342fb79566fee9e1bade250a29cd65012\
55e731531755284bfcbe85ada1f39f44300a06082a8648ce3d04030203480030\
45022100d27e39187da5efaa376254329499bb705f7188dd8c78e0725c7ed28d\
e5d218e1022070853e1a43707298e07ebe9b9eb7cd5839b794db4c3d22209554\
1f0bdf82d1f4").unwrap();
self.store().insert(
crate::api::attestation_store::STORAGE_KEYS[1],
&der
).expect("pddb error initializing OpenSK");
}
}
}
}
impl UserPresence for XousEnv {

View file

@ -10,6 +10,7 @@ mod totp;
mod prereqs;
mod vendor_commands;
mod storage;
mod migration_v1;
use locales::t;
@ -30,6 +31,7 @@ use crate::vendor_commands::VendorSession;
use ux::framework::{VaultUx, DEFAULT_FONT, FONT_LIST, name_to_style};
use xous_ipc::Buffer;
use xous::{send_message, Message};
use pddb::Pddb;
use usbd_human_interface_device::device::fido::*;
use num_traits::*;
@ -193,7 +195,7 @@ impl EndpointReplies {
fn main() -> ! {
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Debug);
log::info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
@ -239,12 +241,19 @@ fn main() -> ! {
// block until the PDDB is mounted
let pddb = pddb::Pddb::new();
pddb.is_mounted_blocking();
// Attempt a migration, if necessary. If none is necessary this call is fairly lightweight.
match crate::migration_v1::migrate(&pddb) {
Ok(_) => {},
Err(e) => {
log::warn!("Migration encountered errors! {:?}", e);
let modals = modals::Modals::new(&xns).unwrap();
modals.show_notification(
&format!("{}\n{:?}", t!("vault.migration_error", xous::LANG), e), None
).ok();
}
};
let mut env = XousEnv::new(conn);
{
let mutex = opensk_mutex.lock().unwrap();
env.attestation_check_init(); // ensures that our attestation keys have been created
}
let mut replies = EndpointReplies::new();
// only run the main loop if the SoC is compatible
if env.is_soc_compatible() {
@ -323,6 +332,7 @@ fn main() -> ! {
let modals = modals::Modals::new(&xns).unwrap();
let tt = ticktimer_server::Ticktimer::new().unwrap();
let mut first_time = true;
loop {
let msg = xous::receive_message(sid).unwrap();
let opcode: Option<VaultOp> = FromPrimitive::from_usize(msg.body.id());
@ -450,11 +460,14 @@ fn main() -> ! {
// HID is always selected if the vault is foregrounded
vaultux.ensure_hid();
allow_redraw = true;
// Update database & configs
send_message(actions_conn,
Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0)
).ok();
vaultux.update_mode();
if first_time {
// Populate the initial fields, just the first time
send_message(actions_conn,
Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0)
).ok();
vaultux.update_mode();
first_time = false;
}
}
}
}),
@ -561,4 +574,3 @@ fn main() -> ! {
log::trace!("quitting");
xous::terminate_process(0)
}

View file

@ -0,0 +1,207 @@
use std::io::{Read, Write};
use vault::ctap::storage::key;
use std::num::ParseIntError;
use pddb::Pddb;
const FIDO_DICT: &'static str = "fido.cfg";
const FIDO_CRED_DICT: &'static str = "fido.cred";
const FIDO_PERSISTENT_DICT: &'static str = "fido.persistent";
fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect()
}
/// The point of the attestation key is to delegate the question of the authenticity of your U2F
/// key effectively to a transaction that happens between the factory and the server. If the
/// attestation private key is an impenetrable secret and you can trust the factory, then the
/// attestation key effectively proves the U2F token was made by the factory to its specifications.
///
/// In the case of Precursor's threat model, you don't trust the factory, and you assume there is
/// basically no such thing as an impenetrable secret. So an attestation key is equivalent to any
/// private key created entirely within your own device; its use proves that you're you, or at
/// least, someone has possession of your device and your unlock password.
///
/// Thus, for Precursor, there is no delegation of the question of authenticity -- you
/// choose to verify the device to the level you're satisfied with. If you're happy to
/// just take it out of the box and use it without checking anything, that's your choice,
/// but you can also check everything as much as you like.
///
/// As a result, the "attestation key" serves no role for Precursor users, other than to
/// identify them as Precursor users. A unique per-device key would furthermore allow
/// a server to uniquely identify each Precursor user. Therefore, we commit the private key
/// into this public repository so any and all Precursor users may share it. The more
/// widely it is used, the harder it becomes to de-anonymize a Precursor users.
///
/// This key was generated using the script in `tools/gen_key_materials.sh`, and then printed
/// out by calling
/// `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem`
pub fn migrate(pddb: &Pddb) -> Result<(), xous::Error> {
if !pddb.list_dict(None).unwrap().contains(&persistent_store::store::OPENSK2_DICT.to_string()) {
// build the attestation keys
let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap();
pddb.get(
persistent_store::store::OPENSK2_DICT,
&vault::api::attestation_store::STORAGE_KEYS[0].to_string(),
None,
true, true, Some(32), None::<fn()>
).unwrap().write_all(&pk).map_err(|_| xous::Error::AccessDenied)?;
let der = decode_hex(
"308201423081e9021449a3e2a4e1078eae2f1a18567f0a734b09db2478300a06\
082a8648ce3d040302301f311d301b06035504030c14507265637572736f7220\
55324620444959204341301e170d3232303630353138313233395a170d333230\
3630343138313233395a30293127302506035504030c1e507265637572736f72\
2053656c662d5665726966696564204465766963653059301306072a8648ce3d\
020106082a8648ce3d030107034200042d8fad76c25851ee70ae651ab2361bee\
1b5d93769aff99d95ad112871060f2f342fb79566fee9e1bade250a29cd65012\
55e731531755284bfcbe85ada1f39f44300a06082a8648ce3d04030203480030\
45022100d27e39187da5efaa376254329499bb705f7188dd8c78e0725c7ed28d\
e5d218e1022070853e1a43707298e07ebe9b9eb7cd5839b794db4c3d22209554\
1f0bdf82d1f4").unwrap();
pddb.get(
persistent_store::store::OPENSK2_DICT,
&vault::api::attestation_store::STORAGE_KEYS[1].to_string(),
None,
true, true, None, None::<fn()>
).unwrap().write_all(&der).map_err(|_| xous::Error::AccessDenied)?;
// port the master secret
migrate_one(pddb, FIDO_DICT, "CRED_RANDOM_SECRET", key::CRED_RANDOM_SECRET)?;
// port AAGUID
migrate_one(pddb, FIDO_PERSISTENT_DICT, "AAGUID", key::AAGUID)?;
// port master keys
migrate_one(pddb, FIDO_DICT, "MASTER_KEYS", key::_RESERVED_KEY_STORE)?;
// port other settings
// Turns out that porting over the PIN is a mistake. Just clear it.
// migrate_one(pddb, FIDO_DICT, "MIN_PIN_LENGTH", key::MIN_PIN_LENGTH)?;
// migrate_one(pddb, FIDO_DICT, "PIN_RETRIES", key::PIN_RETRIES)?;
migrate_one(pddb, FIDO_DICT, "GLOBAL_SIGNATURE_COUNTER", key::GLOBAL_SIGNATURE_COUNTER)?;
// PIN_HASH requires appending a length to the end. This is an `advisory` field, so we...just make up a value of 8.
// This may not actually be the length used, but it's likely to be long enough to avoid forcing a re-PIN event because
// the PIN hash is too short.
// migrate_pin(pddb)?;
// migrate credentials
let creds = match pddb.list_keys(FIDO_CRED_DICT, None) {
Ok(list) => list,
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => Vec::new(),
_ => return Err(xous::Error::InternalError)
}
};
let expected_creds = creds.len();
let mut successful_migrations = 0;
let cred_start = key::CREDENTIALS.start;
for cred in creds {
let mut key = pddb.get(
FIDO_CRED_DICT,
&cred,
None, false, false, None, None::<fn()>
).map_err(|_| xous::Error::AccessDenied)?;
let mut data = Vec::<u8>::new();
key.read_to_end(&mut data).unwrap();
match vault::ctap::storage::deserialize_credential(&data) {
Some(pk) => {
log::debug!("Valid pk: {:?}", pk);
let migrated_data = vault::ctap::storage::serialize_credential(pk).map_err(|_| xous::Error::InternalError)?;
let mut new_key = pddb.get(
persistent_store::store::OPENSK2_DICT,
&(cred_start + successful_migrations).to_string(),
None, true, true, Some(migrated_data.len()), None::<fn()>
).map_err(|_| xous::Error::AccessDenied)?;
new_key.write_all(&migrated_data).map_err(|_| xous::Error::OutOfMemory)?;
successful_migrations += 1;
log::info!("Migrated credential {}:{}", FIDO_CRED_DICT, cred);
}
None => {
log::warn!("Credential {}:{} did not deserialize, cannot migrate!", FIDO_CRED_DICT, cred);
}
}
}
pddb.sync().ok();
if successful_migrations == expected_creds {
Ok(())
} else {
Err(xous::Error::InvalidString)
}
} else {
// OpenSK dict already exists; don't migrate anything.
Ok(())
}
}
/// Only migrates the data if it exists. Does not return an error if the record does not exist
fn migrate_one (
pddb: &Pddb,
legacy_dict: &str,
legacy_key: &str,
new_key: usize,
) -> Result<(), xous::Error> {
match pddb.get(
legacy_dict,
legacy_key,
None, false, false, None, None::<fn()>
) {
Ok(mut key) => {
let mut data = Vec::<u8>::new();
let len = key.read_to_end(&mut data).map_err(|_| xous::Error::AccessDenied)?;
let mut new_key = pddb.get(
persistent_store::store::OPENSK2_DICT,
&new_key.to_string(),
None, true, true, Some(len), None::<fn()>
).map_err(|_| xous::Error::AccessDenied)?;
new_key.write_all(&data).map_err(|_| xous::Error::OutOfMemory)?;
log::info!("successfully migrated {}:{}", legacy_dict, legacy_key);
Ok(())
},
Err(e) => {
match e.kind() {
std::io::ErrorKind::NotFound => {
log::info!("migration skipping {}:{}, as it does not exist", legacy_dict, legacy_key);
Ok(())
},
_ => Err(xous::Error::InternalError)
}
}
}
}
/// Handle the special case of the PIN_HASH
fn migrate_pin (
pddb: &Pddb,
) -> Result<(), xous::Error> {
let legacy_dict = FIDO_DICT;
let legacy_key = "PIN_HASH";
let new_key = key::PIN_PROPERTIES;
match pddb.get(
legacy_dict,
legacy_key,
None, false, false, None, None::<fn()>
) {
Ok(mut key) => {
let mut data = Vec::<u8>::new();
let len = key.read_to_end(&mut data).map_err(|_| xous::Error::AccessDenied)? + 1;
data.push(5); // add a bogus "length" advisory field of 8
let mut new_key = pddb.get(
persistent_store::store::OPENSK2_DICT,
&new_key.to_string(),
None, true, true, Some(len), None::<fn()>
).map_err(|_| xous::Error::AccessDenied)?;
new_key.write_all(&data).map_err(|_| xous::Error::OutOfMemory)?;
log::info!("successfully migrated {}:{}", legacy_dict, legacy_key);
Ok(())
},
Err(e) => {
match e.kind() {
std::io::ErrorKind::NotFound => {
log::info!("migration skipping {}:{}, as it does not exist", legacy_dict, legacy_key);
Ok(())
},
_ => Err(xous::Error::InternalError)
}
}
}
}