forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Frantz
committed
Nov 21, 2024
1 parent
6f386ba
commit f00b8d2
Showing
12 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright lowRISC contributors (OpenTitan project). | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::Result; | ||
use cryptoki::session::Session; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_annotate::Annotate; | ||
use std::any::Any; | ||
use std::collections::HashSet; | ||
use std::path::PathBuf; | ||
|
||
use crate::commands::{BasicResult, Dispatch}; | ||
use crate::error::HsmError; | ||
use crate::module::Module; | ||
use crate::util::attribute::{AttributeMap, AttributeType, AttributeError, AttrData}; | ||
use crate::util::helper; | ||
|
||
#[derive(clap::Args, Debug, Serialize, Deserialize)] | ||
pub struct Read { | ||
#[arg(long)] | ||
id: Option<String>, | ||
#[arg(short, long)] | ||
label: Option<String>, | ||
/// Search spec | ||
#[arg(short, long)] | ||
spec: Option<AttributeMap>, | ||
#[arg()] | ||
output: PathBuf, | ||
} | ||
|
||
#[typetag::serde(name = "object-read")] | ||
impl Dispatch for Read { | ||
fn run( | ||
&self, | ||
_context: &dyn Any, | ||
_hsm: &Module, | ||
session: Option<&Session>, | ||
) -> Result<Box<dyn Annotate>> { | ||
let session = session.ok_or(HsmError::SessionRequired)?; | ||
let mut attr = AttributeMap::default(); | ||
if let Some(id) = &self.id { | ||
attr.insert(AttributeType::Id, AttrData::Str(id.into())); | ||
} | ||
if let Some(label) = &self.label { | ||
attr.insert(AttributeType::Label, AttrData::Str(label.into())); | ||
} | ||
if attr.is_empty() { | ||
return Err(HsmError::NoSearchCriteria.into()); | ||
} | ||
if let Some(spec) = &self.spec { | ||
attr.merge(spec.clone()); | ||
} | ||
let attr = attr.to_vec()?; | ||
let object = helper::find_one_object(session, &attr)?; | ||
let map = AttributeMap::from_object(session, object)?; | ||
let value = map.get(&AttributeType::Value).ok_or(AttributeError::AttributeNotFound(AttributeType::Value))?; | ||
let value = Vec::<u8>::try_from(value)?; | ||
let mut result = Box::<BasicResult>::default(); | ||
if self.output.to_str() == Some("-") { | ||
result.value = Some(String::from_utf8(value)?); | ||
} else { | ||
std::fs::write(&self.output, value)?; | ||
} | ||
Ok(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright lowRISC contributors (OpenTitan project). | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::Result; | ||
use cryptoki::session::Session; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_annotate::Annotate; | ||
use std::any::Any; | ||
use std::collections::HashSet; | ||
use std::path::PathBuf; | ||
|
||
use crate::commands::{BasicResult, Dispatch}; | ||
use crate::error::HsmError; | ||
use crate::module::Module; | ||
use crate::util::attribute::{AttributeMap, AttributeType, AttributeError, AttrData, ObjectClass}; | ||
use crate::util::helper; | ||
|
||
use cryptoki_sys::*; | ||
|
||
#[derive(clap::Args, Debug, Serialize, Deserialize)] | ||
pub struct Write { | ||
#[arg(long)] | ||
id: Option<String>, | ||
#[arg(short, long)] | ||
label: Option<String>, | ||
#[arg(short, long, default_value="false")] | ||
private: bool, | ||
#[arg(short, long)] | ||
application: Option<String>, | ||
#[arg(short, long)] | ||
template: Option<AttributeMap>, | ||
#[arg()] | ||
input: PathBuf, | ||
} | ||
|
||
#[typetag::serde(name = "object-write")] | ||
impl Dispatch for Write { | ||
fn run( | ||
&self, | ||
_context: &dyn Any, | ||
_hsm: &Module, | ||
session: Option<&Session>, | ||
) -> Result<Box<dyn Annotate>> { | ||
let session = session.ok_or(HsmError::SessionRequired)?; | ||
|
||
let mut attr = AttributeMap::default(); | ||
let id = self.id.as_ref().map_or(AttrData::None, |id| AttrData::Str(id.into())); | ||
let label = self.label.as_ref().map_or(AttrData::None, |label| AttrData::Str(label.into())); | ||
if !id.is_none() { | ||
attr.insert(AttributeType::Id, id.clone()); | ||
} | ||
if !label.is_none() { | ||
attr.insert(AttributeType::Label, label.clone()); | ||
} | ||
if id.is_none() && label.is_none() { | ||
return Err(HsmError::NoSearchCriteria.into()); | ||
} | ||
|
||
let result = Box::new(BasicResult { | ||
success: true, | ||
id, | ||
label, | ||
value: None, | ||
error: None, | ||
}); | ||
|
||
attr.insert(AttributeType::Class, AttrData::ObjectClass(ObjectClass::Data)); | ||
attr.insert(AttributeType::Token, AttrData::from(true)); | ||
attr.insert(AttributeType::Private, AttrData::from(self.private)); | ||
if let Some(application) = &self.application { | ||
attr.insert(AttributeType::Application, AttrData::Str(application.into())); | ||
} | ||
if let Some(template) = &self.template { | ||
attr.merge(template.clone()); | ||
} | ||
let value = std::fs::read(&self.input)?; | ||
attr.insert(AttributeType::Value, AttrData::from(value.as_slice())); | ||
let attr = attr.to_vec()?; | ||
for a in attr.iter() { | ||
println!("a = {a:?}"); | ||
} | ||
let mut template: Vec<CK_ATTRIBUTE> = attr.iter().map(|attr| attr.into()).collect(); | ||
for t in template.iter() { | ||
println!("t = {:?} {:?} {:?}", t.type_, t.pValue, t.ulValueLen); | ||
} | ||
|
||
session.create_object(&attr)?; | ||
Ok(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters