Skip to content
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

Fix possible infinite loop when loading cert chains from Java P11KeyStore #216

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pkcs11/src/backend/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cryptoki_sys::{CKA_CLASS, CKA_ID, CKA_LABEL, CK_OBJECT_CLASS, CK_SESSION_HANDLE};
use cryptoki_sys::{CKA_CLASS, CKA_ID, CKA_LABEL, CKA_SUBJECT, CK_OBJECT_CLASS, CK_SESSION_HANDLE};
use log::{debug, trace};

use super::{
Expand All @@ -22,6 +22,7 @@ pub struct KeyRequirements {
pub kind: Option<ObjectKind>,
pub id: Option<String>,
pub raw_id: Option<Vec<u8>>,
pub cka_subject: Option<Vec<u8>>,
}

fn parse_key_requirements(template: Option<CkRawAttrTemplate>) -> Result<KeyRequirements, Error> {
Expand All @@ -30,6 +31,7 @@ fn parse_key_requirements(template: Option<CkRawAttrTemplate>) -> Result<KeyRequ
let mut key_id = None;
let mut kind = None;
let mut raw_id = None;
let mut cka_subject = None;
for attr in template.iter() {
debug!("attr {:?}: {:?}", attr.type_(), attr.val_bytes());

Expand Down Expand Up @@ -59,17 +61,26 @@ fn parse_key_requirements(template: Option<CkRawAttrTemplate>) -> Result<KeyRequ
if attr.type_() == CKA_LABEL && key_id.is_none() {
key_id = Some(parse_str_from_attr(&attr)?);
}

if attr.type_() == CKA_SUBJECT {
let bytes = attr
.val_bytes()
.ok_or(Error::InvalidAttribute(attr.type_()))?;
cka_subject = Some(bytes.to_vec());
}
}
Ok(KeyRequirements {
kind,
id: key_id,
raw_id,
cka_subject,
})
}
None => Ok(KeyRequirements {
kind: None,
id: None,
raw_id: None,
cka_subject: None,
}),
}
}
Expand Down
17 changes: 15 additions & 2 deletions pkcs11/src/backend/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use cryptoki_sys::{
CKR_OK, CK_FLAGS, CK_OBJECT_HANDLE, CK_RV, CK_SESSION_HANDLE, CK_SESSION_INFO, CK_SLOT_ID,
CK_USER_TYPE,
CK_USER_TYPE, CKA_SUBJECT,
};
use log::{debug, error, trace};
use nethsm_sdk_rs::apis::default_api;
Expand Down Expand Up @@ -479,7 +479,20 @@ impl Session {
.into_iter()
.filter(|(_, obj)| {
if let Some(kind) = requirements.kind {
kind == obj.kind
// kind must match
if kind != obj.kind {
false
// extra checks if kind is Cerificate
} else if kind == ObjectKind::Certificate {
// When Subject is provided as requirement, it must match
requirements.cka_subject.is_none() ||
obj.attr(CKA_SUBJECT)
.map(|attr| attr.as_bytes())
== requirements.cka_subject.as_deref()
// On other kinds, no need for extra checks
} else {
true
}
} else {
true
}
Expand Down
Loading