Skip to content

Commit

Permalink
picow flash config, ed25519 key as plain bytes
Browse files Browse the repository at this point in the history
- ServBehaviour::hostkeys() now returns a slice of references
  since we can't clone SignKeys and don't want to move
  them out of Config. May need rethinking.

- embassy demos common code is now a proper crate

- Updated various embassy deps
  • Loading branch information
mkj committed Apr 28, 2023
1 parent a9f533b commit 553b5e3
Show file tree
Hide file tree
Showing 23 changed files with 614 additions and 177 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,3 @@ simplelog = { version = "0.12", features = ["test"] }


[patch.crates-io]
# needed for Default WakerRegistration, https://github.com/embassy-rs/embassy/commit/14a2d1524080593f7795fe14950a3f0ee6e2b409
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "e1eac15c429f88b1176109d6ce42185e2774ac86" }
2 changes: 1 addition & 1 deletion async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ argh = "0.1"

ssh-key = { version = "0.5", default-features = false, features = [ "std"] }

embassy-sync = { version = "0.1.0" }
embassy-sync = { version = "0.2.0" }
embassy-futures = { version = "0.1.0" }

# async feature requires nightly for async fn in trait
Expand Down
2 changes: 1 addition & 1 deletion async/src/known_hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn check_known_hosts_file(
let known_key = match OpenSSHKey::from_openssh(&lk) {
Ok(k) => k,
Err(e) => {
warn!("Unparsed key for {} on line {}:{}", host, p.display(), line);
warn!("Unparsed key for \"{}\" on line {}:{}", host, p.display(), line);
trace!("{e:?}");
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.2.0-alpha"
edition = "2021"

[dependencies]
embassy-sync = { version = "0.1.0" }
embassy-sync = { version = "0.2.0" }
embassy-futures = { version = "0.1.0" }
embedded-io = { version = "0.4", features = ["async"] }
atomic-polyfill = "1.0"
Expand Down
29 changes: 29 additions & 0 deletions embassy/demos/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "sunset-demo-embassy-common"
description = "Shared code for Sunset demos"
version = "0.1.0"
edition = "2021"

[workspace]
# blank

[dependencies]
sunset-embassy = { path = "../../" }
sunset = { path = "../../.." }
sunset-sshwire-derive = { version = "0.1", path = "../../../sshwire-derive" }

embassy-sync = { version = "0.2.0" }
embassy-net = { version = "0.1.0", features = ["tcp", "dhcpv4", "medium-ethernet", "nightly"] }
embassy-net-driver = { version = "0.1.0" }
embassy-futures = { version = "0.1.0" }

heapless = "0.7.15"
menu = "0.3"
embedded-io = { version = "0.4", features = ["async"] }

defmt = { version = "0.3", optional = true }
log = { version = "0.4", optional = true }

[features]
defmt = ["dep:defmt", "embassy-net/defmt", "embedded-io/defmt"]
log = ["dep:log", "embassy-net/log"]
3 changes: 1 addition & 2 deletions embassy/demos/common/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Embassy demos common

The [picow](../picow) and [std](../std) demos share this common code.
Currently not a full crate, just source modules included.
The [picow](../picow) and [std](../std) demos share this common code.
108 changes: 108 additions & 0 deletions embassy/demos/common/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#[allow(unused_imports)]
use {
sunset::error::{Error, Result, TrapBug},
};

#[allow(unused_imports)]
#[cfg(not(feature = "defmt"))]
use {
log::{debug, error, info, log, trace, warn},
};

#[allow(unused)]
#[cfg(feature = "defmt")]
use defmt::{debug, info, warn, panic, error, trace};

use heapless::String;

use sunset_sshwire_derive::*;
use sunset::sshwire;
use sunset::sshwire::{BinString, SSHEncode, SSHDecode, WireResult, SSHSource, SSHSink, WireError};

use sunset::{SignKey, KeyType};

// Be sure to bump picow flash_config::CURRENT_VERSION
// if this struct changes (or encode/decode impls).
#[derive(Debug)]
pub struct SSHConfig {
pub hostkey: SignKey,
/// login password
pub pw_hash: Option<[u8; 32]>,
/// SSID
pub wifi_net: String<32>,
/// WPA2 passphrase. None is Open network.
pub wifi_pw: Option<String<63>>,
}

impl SSHConfig {
/// Creates a new config with default parameters.
///
/// Will only fail on RNG failure.
pub fn new() -> Result<Self> {
let hostkey = SignKey::generate(KeyType::Ed25519, None)?;

let wifi_net = option_env!("WIFI_NETWORK").unwrap_or("guest").into();
let wifi_pw = option_env!("WIFI_PASSWORD").map(|p| p.into());
Ok(SSHConfig {
hostkey,
pw_hash: None,
wifi_net,
wifi_pw,
})
}
}

// a private encoding specific to demo config, not SSH defined.
fn enc_signkey(k: &SignKey, s: &mut dyn SSHSink) -> WireResult<()> {
// need to add a variant field if we support more key types.
match k {
SignKey::Ed25519(seed) => seed.enc(s),
_ => Err(WireError::UnknownVariant),
}
}

fn dec_signkey<'de, S>(s: &mut S) -> WireResult<SignKey> where S: SSHSource<'de> {
Ok(SignKey::Ed25519(SSHDecode::dec(s)?))
}

impl SSHEncode for SSHConfig {
fn enc(&self, s: &mut dyn SSHSink) -> WireResult<()> {
enc_signkey(&self.hostkey, s)?;
self.pw_hash.is_some().enc(s)?;
self.pw_hash.enc(s)?;

self.wifi_net.as_str().enc(s)?;

self.wifi_pw.is_some().enc(s)?;
if let Some(ref p) = self.wifi_pw {
p.as_str().enc(s)?;
}
Ok(())
}
}

impl<'de> SSHDecode<'de> for SSHConfig {
fn dec<S>(s: &mut S) -> WireResult<Self> where S: SSHSource<'de> {
let hostkey = dec_signkey(s)?;

let have_pw_hash = bool::dec(s)?;
let pw_hash = have_pw_hash.then(|| SSHDecode::dec(s)).transpose()?;

let wifi_net = <&str>::dec(s)?.into();
let have_wifi_pw = bool::dec(s)?;

let wifi_pw = have_wifi_pw.then(|| {
let p: &str = SSHDecode::dec(s)?;
Ok(p.into())
})
.transpose()?;
Ok(Self {
hostkey,
pw_hash,
wifi_net,
wifi_pw,
})
}
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use menu::*;
pub use super::BufOutput;
pub use crate::server::BufOutput;
use core::fmt::Write;

// from menu crate examples/simple.rs
Expand Down
13 changes: 13 additions & 0 deletions embassy/demos/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]

#![feature(type_alias_impl_trait)]
#![feature(async_fn_in_trait)]
// #![allow(incomplete_features)]

mod config;
mod server;

pub mod demo_menu;

pub use server::{Shell, listener};
pub use config::SSHConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,26 @@ use embedded_io::asynch;
use sunset::*;
use sunset_embassy::SSHServer;

// TODO move
pub mod demo_menu;

use crate::SSHConfig;

// #[macro_export]
// macro_rules! singleton {
// ($val:expr) => {{
// type T = impl Sized;
// static STATIC_CELL: StaticCell<T> = StaticCell::new();
// STATIC_CELL.init($val)
// }};
// }
#[macro_export]
macro_rules! singleton {
($val:expr) => {{
type T = impl Sized;
static STATIC_CELL: StaticCell<T> = StaticCell::new();
STATIC_CELL.init($val)
let (x,) = STATIC_CELL.init(($val,));
x
}};
}

pub struct SSHConfig {
keys: [SignKey; 1],
}

impl SSHConfig {
pub fn new() -> Result<Self> {
let keys = [SignKey::generate(KeyType::Ed25519, None)?];
Ok(Self {
keys
})
}
}

// common entry point
pub async fn listener<D: Driver, S: Shell>(stack: &'static Stack<D>, config: &SSHConfig) -> ! {
Expand Down Expand Up @@ -106,6 +102,9 @@ async fn session<S: Shell>(socket: &mut TcpSocket<'_>, config: &SSHConfig) -> su
struct DemoServer<'a, S: Shell> {
config: &'a SSHConfig,

// references config
hostkeys: [&'a SignKey; 1],

handle: Option<ChanHandle>,
sess: Option<ChanNum>,

Expand All @@ -120,13 +119,14 @@ impl<'a, S: Shell> DemoServer<'a, S> {
sess: None,
config,
shell,
hostkeys: [&config.hostkey],
})
}
}

impl<'a, S: Shell> ServBehaviour for DemoServer<'a, S> {
fn hostkeys(&mut self) -> BhResult<&[SignKey]> {
Ok(&self.config.keys)
fn hostkeys(&mut self) -> BhResult<&[&SignKey]> {
Ok(&self.hostkeys)
}

fn auth_unchallenged(&mut self, username: TextString) -> bool {
Expand Down
Loading

0 comments on commit 553b5e3

Please sign in to comment.