sec/
args.rs

1use std::num::ParseIntError;
2
3use clap::{Args, Parser, Subcommand};
4use twizzler::object::ObjID;
5
6#[derive(Parser, Debug)]
7#[command(version, about, long_about = None)]
8pub struct CliArgs {
9    #[command(subcommand)]
10    pub command: Commands,
11}
12
13#[derive(Subcommand, Debug)]
14pub enum Commands {
15    /// Commands pertaining to security contexts
16    #[command(subcommand)]
17    Ctx(CtxCommands),
18    /// Commands pertaining to singing/verifying keys
19    #[command(subcommand)]
20    Key(KeyCommands),
21
22    /// Commands pertaining to objects.
23    #[command(subcommand)]
24    Obj(ObjCommands),
25}
26
27#[derive(Subcommand, Debug)]
28pub enum CtxCommands {
29    /// New security context
30    New(NewCtxArgs),
31
32    /// Inspect a Security Context
33    Inspect(CtxInspectArgs),
34
35    /// Commands pertaining to adding security primitives to Security Contexts.
36    #[command(subcommand)]
37    Add(CtxAddCommands),
38}
39
40#[derive(Subcommand, Debug)]
41pub enum KeyCommands {
42    #[command(short_flag = 'n')]
43    NewPair,
44}
45
46#[derive(Subcommand, Debug)]
47pub enum CtxAddCommands {
48    Cap(CapAddArgs),
49}
50
51#[derive(Args, Debug)]
52pub struct CapAddArgs {
53    /// The signing key of the object
54    #[arg(short = 's', long, value_parser=parse_obj_id)]
55    pub signing_key_id: ObjID,
56
57    /// The Security Context that will be modified.
58    #[arg(short = 'm', long, value_parser=parse_obj_id)]
59    pub modifying_ctx: ObjID,
60
61    /// The target object this capability will be usable for.
62    #[arg(short = 't', long, value_parser=parse_obj_id)]
63    pub target_obj: ObjID,
64
65    /// Optionally specify if this operation needs to be specified in a
66    /// specific security context
67    #[arg(short = 'e', long, value_parser=parse_obj_id)]
68    pub executing_ctx: Option<ObjID>,
69}
70
71#[derive(Subcommand, Debug)]
72pub enum ObjCommands {
73    /// Create a new object.
74    New(NewObjectArgs),
75
76    /// Creates a new _sealed_ object. Sealed here means an object with
77    /// no default permissions, meaning a capability is required to access it
78    Sealed(SealedObjectArgs),
79
80    /// Inspect an existing object.
81    Inspect(ObjInspectArgs),
82}
83
84#[derive(Args, Debug)]
85pub struct NewObjectArgs {
86    /// The verifyign key to use when creating the object
87    #[arg(short = 'v', long, value_parser=parse_obj_id)]
88    pub verifying_key_id: ObjID,
89
90    /// Simple string message to store inside the object
91    #[arg(short, long)]
92    pub message: String,
93}
94
95#[derive(Args, Debug)]
96pub struct SealedObjectArgs {
97    /// The verifying key to use when creating the object
98    #[arg(short = 'v', long, value_parser=parse_obj_id)]
99    pub verifying_key_id: ObjID,
100
101    #[arg(short = 's', long, value_parser=parse_obj_id)]
102    /// The signing key to use when creating the capability for the
103    /// sealed object.
104    pub signing_key_id: ObjID,
105
106    /// Optionally a security context to use when creating this object.
107    #[arg(short = 'c', long, value_parser=parse_obj_id)]
108    pub sec_ctx_id: Option<ObjID>,
109
110    /// Simple string message to store inside the object
111    #[arg(short, long)]
112    pub message: String,
113}
114
115#[derive(Args, Debug)]
116pub struct ObjInspectArgs {
117    /// The security context to use when inspecting this object.
118    #[arg(short = 's', long, value_parser=parse_obj_id)]
119    pub sec_ctx_id: Option<ObjID>,
120
121    /// The object to be inspected.
122    #[arg(short = 'o', long, value_parser=parse_obj_id)]
123    pub obj_id: ObjID,
124}
125
126#[derive(Args, Debug)]
127pub struct CtxInspectArgs {
128    /// The security context to be inspected.
129    #[arg(short = 's', long, value_parser=parse_obj_id)]
130    pub sec_ctx_id: ObjID,
131}
132
133fn parse_obj_id(arg: &str) -> Result<ObjID, ParseIntError> {
134    let as_num = u128::from_str_radix(arg, 16)?;
135    Ok(ObjID::from(as_num))
136}
137
138#[derive(Args, Debug)]
139pub struct NewCtxArgs {
140    /// Makes this security context undetachable once attached to.
141    #[arg(short, long, default_value = "false")]
142    pub undetachable: bool,
143}
144
145#[derive(Debug, Args)]
146pub struct AccessArgs {
147    #[arg(short, long)]
148    pub obj_id: String,
149    #[arg(short, long)]
150    pub sec_ctx_id: String,
151}