-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource: add PKCS11 resource back-end #533
Draft
fitzthum
wants to merge
1
commit into
confidential-containers:main
Choose a base branch
from
fitzthum:pkcs11
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,106 @@ | ||
// Copyright (c) 2024 by IBM. | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::{bail, Result}; | ||
use cryptoki::context::{CInitializeArgs, Pkcs11}; | ||
use cryptoki::object::{Attribute, AttributeInfo, AttributeType, KeyType, ObjectClass}; | ||
use cryptoki::session::{Session, UserType}; | ||
use cryptoki::types::AuthPin; | ||
use serde::Deserialize; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
use super::{Repository, ResourceDesc}; | ||
|
||
#[derive(Debug, Deserialize, Clone)] | ||
pub struct Pkcs11Config { | ||
/// Path to the Pkcs11 module | ||
module: String, | ||
|
||
/// The index of the slot to be used | ||
/// If not provided, the first slot will be used. | ||
slot_index: Option<u8>, | ||
|
||
/// The user pin for authenticating the session | ||
pin: String, | ||
} | ||
|
||
pub struct Pkcs11Backend { | ||
session: Arc<Mutex<Session>>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Repository for Pkcs11Backend { | ||
async fn read_secret_resource(&self, resource_desc: ResourceDesc) -> Result<Vec<u8>> { | ||
let session = self.session.lock().await; | ||
|
||
// find object with matching label | ||
let attributes = vec![Attribute::Label(Vec::from(resource_desc.to_string()))]; | ||
let objects = session.find_objects(&attributes)?; | ||
|
||
if objects.is_empty() { | ||
bail!( | ||
"Could not find object with label {}", | ||
resource_desc.to_string() | ||
); | ||
} | ||
let object = objects[0]; | ||
|
||
// check that object has a readable value attribute | ||
let value_attribute = vec![AttributeType::Value]; | ||
let attribute_map = session.get_attribute_info_map(object, value_attribute.clone())?; | ||
let Some(AttributeInfo::Available(_size)) = attribute_map.get(&AttributeType::Value) else { | ||
bail!("Key does not have value attribute available."); | ||
}; | ||
|
||
// get the value | ||
let value = &session.get_attributes(object, &value_attribute)?[0]; | ||
let Attribute::Value(resource_bytes) = value else { | ||
bail!("Failed to get value."); | ||
}; | ||
|
||
Ok(resource_bytes.clone()) | ||
} | ||
|
||
async fn write_secret_resource( | ||
&mut self, | ||
resource_desc: ResourceDesc, | ||
data: &[u8], | ||
) -> Result<()> { | ||
let mut attributes = vec![]; | ||
attributes.push(Attribute::Class(ObjectClass::SECRET_KEY)); | ||
attributes.push(Attribute::KeyType(KeyType::GENERIC_SECRET)); | ||
attributes.push(Attribute::Extractable(true)); | ||
attributes.push(Attribute::Private(true)); | ||
|
||
attributes.push(Attribute::Value(data.to_vec())); | ||
attributes.push(Attribute::Label(Vec::from(resource_desc.to_string()))); | ||
|
||
let _object = self.session.lock().await.create_object(&attributes)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Pkcs11Backend { | ||
pub fn new(config: &Pkcs11Config) -> Result<Self> { | ||
// setup global context | ||
let pkcs11 = Pkcs11::new(config.module.clone())?; | ||
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap(); | ||
|
||
// create session | ||
let slots = pkcs11.get_slots_with_token()?; | ||
let slot_index = usize::from(config.slot_index.unwrap_or(0)); | ||
if slot_index >= slots.len() { | ||
bail!("Slot index out of range"); | ||
} | ||
|
||
let session = pkcs11.open_rw_session(slots[slot_index])?; | ||
session.login(UserType::User, Some(&AuthPin::new(config.pin.clone())))?; | ||
|
||
Ok(Self { | ||
session: Arc::new(Mutex::new(session)), | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want this to be a config item,
String
ori64
might be better?