|
| 1 | +// Copyright 2023 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use cryptoki::object::{Attribute, AttributeType, CertificateType, ObjectClass, ObjectHandle}; |
| 5 | +use thiserror::Error; |
| 6 | +use x509_cert::{ |
| 7 | + certificate::{CertificateInner, Profile}, |
| 8 | + der::{Decode, Encode}, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::SessionLike; |
| 12 | + |
| 13 | +#[derive(Debug, Error)] |
| 14 | +pub enum Error { |
| 15 | + #[error("Cryptoki error: {0}")] |
| 16 | + Cryptoki(#[from] cryptoki::error::Error), |
| 17 | + |
| 18 | + #[error("Missing attribute: {0}")] |
| 19 | + MissingAttribute(AttributeType), |
| 20 | + |
| 21 | + #[error(transparent)] |
| 22 | + Der(#[from] x509_cert::der::Error), |
| 23 | + |
| 24 | + #[error("No such certificate found")] |
| 25 | + MissingCert, |
| 26 | +} |
| 27 | + |
| 28 | +pub trait CertPkcs11 { |
| 29 | + fn pkcs11_store<S: SessionLike, T: Into<Vec<Attribute>>>( |
| 30 | + &self, |
| 31 | + session: &S, |
| 32 | + base_template: T, |
| 33 | + ) -> Result<ObjectHandle, Error>; |
| 34 | + |
| 35 | + fn pkcs11_load<S: SessionLike, T: Into<Vec<Attribute>>>( |
| 36 | + session: &S, |
| 37 | + template: T, |
| 38 | + ) -> Result<Self, Error> |
| 39 | + where |
| 40 | + Self: Sized; |
| 41 | +} |
| 42 | + |
| 43 | +impl<P> CertPkcs11 for CertificateInner<P> |
| 44 | +where |
| 45 | + P: Profile, |
| 46 | +{ |
| 47 | + fn pkcs11_store<S: SessionLike, T: Into<Vec<Attribute>>>( |
| 48 | + &self, |
| 49 | + session: &S, |
| 50 | + base_template: T, |
| 51 | + ) -> Result<ObjectHandle, Error> { |
| 52 | + let mut template = base_template.into(); |
| 53 | + template.push(Attribute::Class(ObjectClass::CERTIFICATE)); |
| 54 | + template.push(Attribute::CertificateType(CertificateType::X_509)); |
| 55 | + template.push(Attribute::Token(true)); |
| 56 | + template.push(Attribute::Value(self.to_der()?)); |
| 57 | + if !self.tbs_certificate.subject.is_empty() { |
| 58 | + template.push(Attribute::Subject(self.tbs_certificate.subject.to_der()?)); |
| 59 | + } |
| 60 | + |
| 61 | + Ok(session.create_object(&template)?) |
| 62 | + } |
| 63 | + |
| 64 | + fn pkcs11_load<S: SessionLike, T: Into<Vec<Attribute>>>( |
| 65 | + session: &S, |
| 66 | + template: T, |
| 67 | + ) -> Result<Self, Error> { |
| 68 | + let mut template = template.into(); |
| 69 | + template.push(Attribute::Class(ObjectClass::CERTIFICATE)); |
| 70 | + template.push(Attribute::CertificateType(CertificateType::X_509)); |
| 71 | + |
| 72 | + let certs = session.find_objects(&template)?; |
| 73 | + if let Some(cert) = certs.first() { |
| 74 | + let attributes = session.get_attributes(*cert, &[AttributeType::Value])?; |
| 75 | + |
| 76 | + let mut value = None; |
| 77 | + for attribute in attributes { |
| 78 | + match attribute { |
| 79 | + Attribute::Value(v) if value.is_none() => { |
| 80 | + value = Some(v); |
| 81 | + } |
| 82 | + _ => {} |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + let value = value.ok_or(Error::MissingAttribute(AttributeType::Value))?; |
| 87 | + |
| 88 | + let cert = Self::from_der(&value)?; |
| 89 | + |
| 90 | + Ok(cert) |
| 91 | + } else { |
| 92 | + Err(Error::MissingCert) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments