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
//! Put an opaque object into the `YubiHSM2`
//!
//! <https://developers.yubico.com/YubiHSM2/Commands/Put_Opaque.html>

use super::put_object::PutObjectParams;
use super::{Command, Response};
use {
    Adapter, Capability, CommandType, Domain, ObjectId, ObjectLabel, OpaqueAlg, Session,
    SessionError,
};

/// Put an opaque object (X.509 certificate or other bytestring) into the `YubiHSM2`
pub fn put_opaque<A: Adapter, T: Into<Vec<u8>>>(
    session: &mut Session<A>,
    object_id: ObjectId,
    label: ObjectLabel,
    domains: Domain,
    capabilities: Capability,
    algorithm: OpaqueAlg,
    bytes: T,
) -> Result<ObjectId, SessionError> {
    session
        .send_command(PutOpaqueCommand {
            params: PutObjectParams {
                id: object_id,
                label,
                domains,
                capabilities,
                algorithm: algorithm.into(),
            },
            data: bytes.into(),
        }).map(|response| response.object_id)
}

/// Request parameters for `command::put_opaque`
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct PutOpaqueCommand {
    /// Common parameters to all put object commands
    pub params: PutObjectParams,

    /// Serialized object
    pub data: Vec<u8>,
}

impl Command for PutOpaqueCommand {
    type ResponseType = PutOpaqueResponse;
}

/// Response from `command::put_opaque`
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct PutOpaqueResponse {
    /// ID of the opaque data object
    pub object_id: ObjectId,
}

impl Response for PutOpaqueResponse {
    const COMMAND_TYPE: CommandType = CommandType::PutOpaqueObject;
}