Skip to content

Commit

Permalink
galmon-osnma: add option to specify Merkle tree root
Browse files Browse the repository at this point in the history
The README needs to be updated, since now this tool is using
clap for command line argument parsing.
  • Loading branch information
daniestevez committed Jan 10, 2024
1 parent dc45363 commit fb38737
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 3 deletions.
114 changes: 114 additions & 0 deletions galmon-osnma/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions galmon-osnma/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ default-run = "galmon-osnma"
publish = false

[dependencies]
clap = { version = "4.4", features = ["derive"] }
ecdsa = { version = "0.16", features = ["pkcs8"] }
env_logger = "0.10"
galileo-osnma = { path = "..", features = ["galmon"] }
hex = "0.4"
log = "0.4"
p256 = { version = "0.13", features = ["ecdsa"] }
spki = { version = "0.7", features = ["pem"] }
42 changes: 39 additions & 3 deletions galmon-osnma/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clap::Parser;
use galileo_osnma::{
galmon::{navmon::nav_mon_message::GalileoInav, transport::ReadTransport},
storage::FullStorage,
Expand All @@ -8,6 +9,21 @@ use p256::ecdsa::VerifyingKey;
use spki::DecodePublicKey;
use std::io::Read;

/// Process OSNMA data reading Galmon protobuf from stdin
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Merkle tree root in hex.
#[arg(long)]
merkle_root: Option<String>,
/// Path to the public key in PEM format.
#[arg(long)]
pubkey: Option<String>,
/// Only process slow MAC data.
#[arg(long)]
slow_mac_only: bool,
}

fn load_pubkey(path: &str) -> std::io::Result<PublicKey<Validated>> {
let mut file = std::fs::File::open(path)?;
let mut pem = String::new();
Expand All @@ -18,13 +34,33 @@ fn load_pubkey(path: &str) -> std::io::Result<PublicKey<Validated>> {

fn main() -> std::io::Result<()> {
env_logger::init();
let args = Args::parse();

if args.merkle_root.is_none() && args.pubkey.is_none() {
log::error!("at least either the Merkle tree root or the public key must be specified");
// TODO: return an error exit code
return Ok(());
}

let args: Vec<_> = std::env::args().collect();
let pubkey = if let Some(pubkey_path) = &args.pubkey {
Some(load_pubkey(pubkey_path)?)
} else {
None
};

let pubkey = load_pubkey(&args[1])?;
let mut osnma: Osnma<FullStorage> = if let Some(merkle) = &args.merkle_root {
let merkle = hex::decode(merkle)
.expect("invalid Merkle tree hex data")
.try_into()
.expect("wrong length of Merkle tree hex data");
Osnma::from_merkle_tree(merkle, pubkey, args.slow_mac_only)
} else {
// Here pubkey shouldn't be None, because Merkle tree is None and we
// have checked that at least one of both is not None.
Osnma::from_pubkey(pubkey.unwrap(), args.slow_mac_only)
};

let mut read = ReadTransport::new(std::io::stdin());
let mut osnma = Osnma::<FullStorage>::from_pubkey(pubkey, false);
let mut timing_parameters: [Option<[u8; 18]>; NUM_SVNS] = [None; NUM_SVNS];
let mut ced_and_status_data: [Option<[u8; 69]>; NUM_SVNS] = [None; NUM_SVNS];
let mut current_subframe = None;
Expand Down

0 comments on commit fb38737

Please sign in to comment.