From 445a246506a601ee8756cae8486075a556e60cfa Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 11 Mar 2021 22:15:10 -0500 Subject: [PATCH] Identity commands: new --- rust-zerotier-core/src/identity.rs | 8 ++- service/src/cli.rs | 2 +- service/src/commands/identity.rs | 78 ++++++++++++++++++++++++++++++ service/src/main.rs | 4 +- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/rust-zerotier-core/src/identity.rs b/rust-zerotier-core/src/identity.rs index 8b6c69d11..90f5a01f4 100644 --- a/rust-zerotier-core/src/identity.rs +++ b/rust-zerotier-core/src/identity.rs @@ -20,12 +20,18 @@ use num_traits::{FromPrimitive, ToPrimitive}; use crate::*; use crate::capi as ztcore; -#[derive(FromPrimitive, ToPrimitive, PartialEq, Eq)] +#[derive(FromPrimitive, ToPrimitive, PartialEq, Eq, Clone, Copy)] pub enum IdentityType { Curve25519 = ztcore::ZT_IdentityType_ZT_IDENTITY_TYPE_C25519 as isize, NistP384 = ztcore::ZT_IdentityType_ZT_IDENTITY_TYPE_P384 as isize, } +impl ToString for IdentityType { + fn to_string(&self) -> String { + String::from(if *self == IdentityType::Curve25519 { "c25519" } else { "p384" }) + } +} + pub struct Identity { pub type_: IdentityType, pub address: Address, diff --git a/service/src/cli.rs b/service/src/cli.rs index 96afe7f6b..9f748fc6d 100644 --- a/service/src/cli.rs +++ b/service/src/cli.rs @@ -199,7 +199,7 @@ pub(crate) fn parse_cli_args() -> ArgMatches<'static> { .arg(Arg::with_name("member").index(1).required(true)))) .subcommand(App::new("identity") .subcommand(App::new("new") - .arg(Arg::with_name("type").possible_value("p384").possible_value("c25519").index(1))) + .arg(Arg::with_name("type").possible_value("p384").possible_value("c25519").default_value("c25519").index(1))) .subcommand(App::new("getpublic") .arg(Arg::with_name("identity").index(1).required(true))) .subcommand(App::new("fingerprint") diff --git a/service/src/commands/identity.rs b/service/src/commands/identity.rs index e69de29bb..6a9a3cbcd 100644 --- a/service/src/commands/identity.rs +++ b/service/src/commands/identity.rs @@ -0,0 +1,78 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +use clap::ArgMatches; +use crate::store::Store; +use zerotier_core::{IdentityType, Identity}; + +/* + identity [args] + new [c25519 | p384] Create identity (default: c25519) + getpublic Extract public part of identity + fingerprint Get an identity's fingerprint + validate Locally validate an identity + sign Sign a file with an identity's key + verify Verify a signature + + */ + +fn new_<'a>(store: &Store, cli_args: &ArgMatches<'a>) -> i32 { + let id_type = cli_args.value_of("type").map_or(IdentityType::Curve25519, |idt| { + match idt { + "p384" => IdentityType::NistP384, + _ => IdentityType::Curve25519, + } + }); + let id = Identity::new_generate(id_type); + if id.is_err() { + println!("ERROR: identity generation failed: {}", id.err().unwrap().to_str()); + return 1; + } + println!("{}", id.ok().unwrap().to_secret_string()); + 0 +} + +fn getpublic<'a>(store: &Store, cli_args: &ArgMatches<'a>) -> i32 { + 0 +} + +fn fingerprint<'a>(store: &Store, cli_args: &ArgMatches<'a>) -> i32 { + 0 +} + +fn validate<'a>(store: &Store, cli_args: &ArgMatches<'a>) -> i32 { + 0 +} + +fn sign<'a>(store: &Store, cli_args: &ArgMatches<'a>) -> i32 { + 0 +} + +fn verify<'a>(store: &Store, cli_args: &ArgMatches<'a>) -> i32 { + 0 +} + +pub(crate) fn run<'a>(store: &Store, cli_args: &ArgMatches<'a>, _: &Option) -> i32 { + match cli_args.subcommand() { + ("new", Some(sub_cli_args)) => new_(store, sub_cli_args), + ("getpublic", Some(sub_cli_args)) => getpublic(store, sub_cli_args), + ("fingerprint", Some(sub_cli_args)) => fingerprint(store, sub_cli_args), + ("validate", Some(sub_cli_args)) => validate(store, sub_cli_args), + ("sign", Some(sub_cli_args)) => sign(store, sub_cli_args), + ("verify", Some(sub_cli_args)) => verify(store, sub_cli_args), + _ => { + crate::cli::print_help(); + 1 + } + } +} diff --git a/service/src/main.rs b/service/src/main.rs index 0c58c9e2a..7f9c997fb 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -101,7 +101,9 @@ fn main() { process_exit_value = service::run(&store, auth_token); } ("controller", Some(sub_cli_args)) => {} - ("identity", Some(sub_cli_args)) => {} + ("identity", Some(sub_cli_args)) => { + process_exit_value = crate::commands::identity::run(&store, sub_cli_args, &auth_token); + } ("locator", Some(sub_cli_args)) => { process_exit_value = crate::commands::locator::run(&store, sub_cli_args, &auth_token); }