1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

// This file was autogenerated by `//sw/host/hsmtool/scripts/pkcs11_consts.py`.
// Do not edit.'

use cryptoki_sys::*;
use num_enum::{FromPrimitive, IntoPrimitive};
use std::convert::TryFrom;

use crate::util::attribute::{AttrData, AttributeError};

#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    IntoPrimitive,
    FromPrimitive,
    serde::Serialize,
    serde::Deserialize,
)]
#[repr(u64)]
pub enum ObjectClass {
    #[serde(rename = "CKO_DATA")]
    Data = CKO_DATA,
    #[serde(rename = "CKO_CERTIFICATE")]
    Certificate = CKO_CERTIFICATE,
    #[serde(rename = "CKO_PUBLIC_KEY")]
    PublicKey = CKO_PUBLIC_KEY,
    #[serde(rename = "CKO_PRIVATE_KEY")]
    PrivateKey = CKO_PRIVATE_KEY,
    #[serde(rename = "CKO_SECRET_KEY")]
    SecretKey = CKO_SECRET_KEY,
    #[serde(rename = "CKO_HW_FEATURE")]
    HwFeature = CKO_HW_FEATURE,
    #[serde(rename = "CKO_DOMAIN_PARAMETERS")]
    DomainParameters = CKO_DOMAIN_PARAMETERS,
    #[serde(rename = "CKO_MECHANISM")]
    Mechanism = CKO_MECHANISM,
    #[serde(rename = "CKO_OTP_KEY")]
    OtpKey = CKO_OTP_KEY,
    #[num_enum(catch_all)]
    UnknownObjectClass(u64) = u64::MAX,
}

impl From<cryptoki::object::ObjectClass> for ObjectClass {
    fn from(val: cryptoki::object::ObjectClass) -> Self {
        let val = CK_OBJECT_CLASS::from(val);
        Self::from(val)
    }
}

impl TryFrom<ObjectClass> for cryptoki::object::ObjectClass {
    type Error = cryptoki::error::Error;
    fn try_from(val: ObjectClass) -> Result<Self, Self::Error> {
        let val = CK_OBJECT_CLASS::from(val);
        cryptoki::object::ObjectClass::try_from(val)
    }
}

impl TryFrom<&AttrData> for ObjectClass {
    type Error = AttributeError;
    fn try_from(val: &AttrData) -> Result<Self, Self::Error> {
        match val {
            AttrData::ObjectClass(x) => Ok(*x),
            _ => Err(AttributeError::EncodingError),
        }
    }
}

impl From<ObjectClass> for AttrData {
    fn from(val: ObjectClass) -> Self {
        AttrData::ObjectClass(val)
    }
}