parsec-service 1.5.0

A language-agnostic API to secure services in a platform-agnostic way
Documentation
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use super::utils::{algorithm_to_mechanism, to_response_status};
use super::{KeyPairType, Provider, utils};
use crate::authenticators::ApplicationIdentity;
use crate::key_info_managers::KeyIdentity;
use cryptoki::mechanism::{Mechanism, MechanismType};
use cryptoki::object::{Attribute, AttributeType, KeyType, ObjectClass, ObjectHandle};
use cryptoki::session::Session;
use log::{error, info, trace};
use parsec_interface::operations::psa_key_attributes::{EccFamily, Id, Lifetime, Type};
use parsec_interface::operations::utils_deprecated_primitives::CheckDeprecated;
use parsec_interface::operations::{
    psa_destroy_key, psa_export_public_key, psa_generate_key, psa_import_key,
};
use parsec_interface::requests::{ResponseStatus, Result};
use parsec_interface::secrecy::ExposeSecret;
use picky_asn1::wrapper::{IntegerAsn1, OctetStringAsn1};
use picky_asn1_x509::RsaPublicKey;
use std::convert::TryInto;

impl Provider {
    /// Find the PKCS 11 object handle corresponding to the key ID and the key type (public,
    /// private or any key type) given as parameters for the current session.
    pub(super) fn find_key(
        &self,
        session: &Session,
        key_id: u32,
        key_type: KeyPairType,
    ) -> Result<ObjectHandle> {
        let mut template = vec![Attribute::Id(key_id.to_be_bytes().to_vec())];

        match key_type {
            KeyPairType::PublicKey => template.push(Attribute::Class(ObjectClass::PUBLIC_KEY)),
            KeyPairType::PrivateKey => template.push(Attribute::Class(ObjectClass::PRIVATE_KEY)),
            KeyPairType::Any => (),
        }

        trace!("FindObjects commands");
        let objects = session
            .find_objects(&template)
            .map_err(to_response_status)?;

        if objects.is_empty() {
            Err(ResponseStatus::PsaErrorDoesNotExist)
        } else {
            Ok(objects[0])
        }
    }

    pub(super) fn move_pub_key_to_psa_crypto(&self, key_identity: &KeyIdentity) -> Result<Id> {
        info!("Attempting to export public key");
        let export_operation = psa_export_public_key::Operation {
            key_name: key_identity.key_name().to_owned(),
        };
        let psa_export_public_key::Result { data } =
            self.psa_export_public_key_internal(key_identity.application(), export_operation)?;

        info!("Importing public key into PSA Crypto");
        let mut attributes = self.key_info_store.get_key_attributes(&key_identity)?;
        attributes.lifetime = Lifetime::Volatile;
        attributes.key_type = match attributes.key_type {
            Type::RsaKeyPair | Type::RsaPublicKey => Type::RsaPublicKey,
            Type::EccKeyPair { curve_family } | Type::EccPublicKey { curve_family } => {
                Type::EccPublicKey { curve_family }
            }
            Type::DhKeyPair { group_family } | Type::DhPublicKey { group_family } => {
                Type::DhPublicKey { group_family }
            }
            _ => return Err(ResponseStatus::PsaErrorInvalidArgument),
        };
        let id = psa_crypto::operations::key_management::import(attributes, None, &data)?;

        Ok(id)
    }

    pub(super) fn remove_psa_crypto_pub_key(&self, pub_key_id: Id) -> Result<()> {
        info!("Removing public key stored in PSA.");
        unsafe { psa_crypto::operations::key_management::destroy(pub_key_id) }.map_err(|e| {
            error!("Failed to remove public key from PSA Crypto.");
            e
        })?;
        Ok(())
    }

    pub(super) fn psa_generate_key_internal(
        &self,
        application_identity: &ApplicationIdentity,
        op: psa_generate_key::Operation,
    ) -> Result<psa_generate_key::Result> {
        return_on_deprecated!(op, "The key requested to generate is deprecated");

        if op.attributes.key_type.is_public_key() {
            error!("A public key type can not be generated.");
            return Err(ResponseStatus::PsaErrorInvalidArgument);
        }

        let key_name = op.key_name;
        let key_attributes = op.attributes;

        if key_attributes.policy.usage_flags.export() && !self.allow_export {
            error!(
                "The configuration of this provider does not allow it to generate keys that can be exported."
            );
            return Err(ResponseStatus::PsaErrorNotPermitted);
        }

        let key_identity = KeyIdentity::new(
            application_identity.clone(),
            self.provider_identity.clone(),
            key_name,
        );
        self.key_info_store.does_not_exist(&key_identity)?;

        let session = self.new_session()?;

        let key_id = self.create_key_id();

        let mut pub_template = vec![
            Attribute::Id(key_id.to_be_bytes().to_vec()),
            Attribute::Token(true.into()),
            Attribute::AllowedMechanisms(vec![
                algorithm_to_mechanism(key_attributes.policy.permitted_algorithms)
                    .map_err(to_response_status)?
                    .mechanism_type(),
            ]),
        ];
        let mut priv_template = pub_template.clone();
        priv_template.push(Attribute::Class(ObjectClass::PRIVATE_KEY));
        pub_template.push(Attribute::Class(ObjectClass::PUBLIC_KEY));
        pub_template.push(Attribute::Private(false.into()));

        utils::key_pair_usage_flags_to_pkcs11_attributes(
            key_attributes.policy.usage_flags,
            &mut pub_template,
            &mut priv_template,
        );

        let mech = match key_attributes.key_type {
            Type::RsaKeyPair => {
                pub_template.push(Attribute::PublicExponent(utils::PUBLIC_EXPONENT.to_vec()));
                pub_template.push(Attribute::ModulusBits(
                    key_attributes.bits.try_into().map_err(to_response_status)?,
                ));
                Ok(Mechanism::RsaPkcsKeyPairGen)
            }
            Type::EccKeyPair { curve_family } => {
                pub_template.push(Attribute::EcParams(
                    picky_asn1_der::to_vec(&utils::ec_params(curve_family, key_attributes.bits)?)
                        .map_err(|e| {
                        error!("Failed to generate EC parameters: {}", e);
                        ResponseStatus::PsaErrorGenericError
                    })?,
                ));
                Ok(Mechanism::EccKeyPairGen)
            }
            _ => Err(ResponseStatus::PsaErrorNotSupported),
        }?;

        match session.generate_key_pair(&mech, &pub_template, &priv_template) {
            Ok((public, private)) => {
                if let Err(e) =
                    self.key_info_store
                        .insert_key_info(key_identity, &key_id, key_attributes)
                {
                    format_error!("Failed to insert the mappings, deleting the key", e);
                    if let Err(e) = session.destroy_object(public) {
                        format_error!("Failed to destroy public part of the key", e);
                    }
                    if let Err(e) = session.destroy_object(private) {
                        format_error!("Failed to destroy private part of the key", e);
                    }
                    Err(e)
                } else {
                    Ok(psa_generate_key::Result {})
                }
            }
            Err(error) => {
                format_error!("Generate key status", error);
                Err(to_response_status(error))
            }
        }
    }

    pub(super) fn psa_import_key_internal(
        &self,
        application_identity: &ApplicationIdentity,
        op: psa_import_key::Operation,
    ) -> Result<psa_import_key::Result> {
        warn_on_deprecated!(op, "The key requested to import is deprecated");

        let key_name = op.key_name;
        let key_attributes = op.attributes;

        if key_attributes.policy.usage_flags.export() && !self.allow_export {
            error!(
                "The configuration of this provider does not allow it to generate keys that can be exported."
            );
            return Err(ResponseStatus::PsaErrorNotPermitted);
        }
        if op.data.expose_secret().is_empty() {
            error!("Key data is empty");
            return Err(ResponseStatus::PsaErrorInvalidArgument);
        }

        let key_identity = KeyIdentity::new(
            application_identity.clone(),
            self.provider_identity.clone(),
            key_name,
        );
        self.key_info_store.does_not_exist(&key_identity)?;

        let session = self.new_session()?;

        let key_id = self.create_key_id();

        let mut template: Vec<Attribute> = Vec::new();
        template.push(Attribute::Class(ObjectClass::PUBLIC_KEY));
        template.push(Attribute::Token(true.into()));
        template.push(Attribute::Verify(true.into()));
        template.push(Attribute::Id(key_id.to_be_bytes().to_vec()));

        match op.attributes.key_type {
            Type::RsaPublicKey => {
                self.handle_rsa_public_import_attrib(
                    op.data.expose_secret(),
                    key_attributes.bits,
                    &mut template,
                )?;
            }
            Type::EccPublicKey { curve_family } => {
                self.handle_ecc_public_import_attrib(
                    op.data.expose_secret(),
                    key_attributes.bits,
                    curve_family,
                    &mut template,
                )?;
            }
            _ => {
                error!(
                    "The pkcs11 provider does not support the {:?} key type.",
                    op.attributes.key_type
                );
                return Err(ResponseStatus::PsaErrorNotSupported);
            }
        }
        trace!("CreateObject command");
        match session.create_object(&template) {
            Ok(key) => {
                if let Err(e) =
                    self.key_info_store
                        .insert_key_info(key_identity, &key_id, key_attributes)
                {
                    format_error!("Failed to insert the mappings, deleting the key.", e);
                    if let Err(e) = session.destroy_object(key) {
                        format_error!("Failed to destroy public key: ", e);
                    }
                    Err(e)
                } else {
                    Ok(psa_import_key::Result {})
                }
            }
            Err(error) => {
                format_error!("Import key status: ", error);
                Err(to_response_status(error))
            }
        }
    }

    pub(super) fn handle_rsa_public_import_attrib(
        &self,
        key_data: &[u8],
        bits: usize,
        template: &mut Vec<Attribute>,
    ) -> Result<()> {
        let public_key: RsaPublicKey = picky_asn1_der::from_bytes(key_data).map_err(|e| {
            format_error!("Failed to parse RsaPublicKey data", e);
            ResponseStatus::PsaErrorInvalidArgument
        })?;

        if public_key.modulus.is_negative() || public_key.public_exponent.is_negative() {
            error!("Only positive modulus and public exponent are supported.");
            return Err(ResponseStatus::PsaErrorInvalidArgument);
        }

        let modulus_object = public_key.modulus.as_unsigned_bytes_be();
        let exponent_object = public_key.public_exponent.as_unsigned_bytes_be();
        if bits != 0 && modulus_object.len() * 8 != bits {
            if crate::utils::GlobalConfig::log_error_details() {
                error!(
                    "`bits` field of key attributes (value: {}) must be either 0 or equal to the size of the key in `data` (value: {}).",
                    bits,
                    modulus_object.len() * 8
                );
            } else {
                error!(
                    "`bits` field of key attributes must be either 0 or equal to the size of the key in `data`."
                );
            }

            return Err(ResponseStatus::PsaErrorInvalidArgument);
        }

        template.push(Attribute::Modulus(modulus_object.into()));
        template.push(Attribute::PublicExponent(exponent_object.into()));
        template.push(Attribute::Encrypt(true.into()));
        template.push(Attribute::Private(false.into()));
        template.push(Attribute::AllowedMechanisms(vec![MechanismType::RSA_PKCS]));
        template.push(Attribute::KeyType(KeyType::RSA));

        Ok(())
    }

    pub(super) fn handle_ecc_public_import_attrib(
        &self,
        key_data: &[u8],
        bits: usize,
        curve_family: EccFamily,
        template: &mut Vec<Attribute>,
    ) -> Result<()> {
        match curve_family {
            EccFamily::Montgomery => {
                // Montgomery curves aren't supported because their format differs from what
                // we need below.
                // In any case, the list of curves for which we can create `EcParams` below
                // is even shorter than that.
                error!("Importing EC keys using Montgomery curves is not currently supported.");
                return Err(ResponseStatus::PsaErrorNotSupported);
            }
            _ => (),
        }

        // For the format of ECC public keys, see:
        // https://parallaxsecond.github.io/parsec-book/parsec_client/operations/psa_export_public_key.html#description
        let key_len = ((key_data.len() - 1) / 2) * 8;
        let bits = if bits == 0 { key_len } else { bits };
        if bits != key_len {
            if crate::utils::GlobalConfig::log_error_details() {
                error!(
                    "`bits` field of key attributes (value: {}) must be either 0 or equal to half the size of the key in `data` (value: {}) for Weierstrass curves.",
                    bits, key_len
                );
            } else {
                error!(
                    "`bits` field of key attributes must be either 0 or equal to half the size of the key in `data` for Weierstrass curves."
                );
            }
            return Err(ResponseStatus::PsaErrorInvalidArgument);
        }

        // The format expected by PKCS11 is an ASN.1 OctetString containing the
        // data that the PSA Crypto interface specifies.
        // See ECPoint in [SEC1](https://www.secg.org/sec1-v2.pdf). PKCS11 mandates using
        // [ANSI X9.62 ECPoint](https://cryptsoft.com/pkcs11doc/v220/group__SEC__12__3__3__ECDSA__PUBLIC__KEY__OBJECTS.html),
        // however SEC1 is an equivalent spec.
        let key_data =
            picky_asn1_der::to_vec(&OctetStringAsn1(key_data.to_vec())).map_err(|e| {
                error!("Failed to generate EC Point OctetString: {}", e);
                ResponseStatus::PsaErrorInvalidArgument
            })?;
        template.push(Attribute::EcPoint(key_data));
        template.push(Attribute::Private(false.into()));
        template.push(Attribute::AllowedMechanisms(vec![MechanismType::ECDSA]));
        template.push(Attribute::KeyType(KeyType::EC));
        template.push(Attribute::EcParams(
            picky_asn1_der::to_vec(&utils::ec_params(curve_family, bits)?).map_err(|e| {
                error!("Failed to generate EC parameters: {}", e);
                ResponseStatus::PsaErrorGenericError
            })?,
        ));

        Ok(())
    }

    pub(super) fn psa_export_public_key_internal(
        &self,
        application_identity: &ApplicationIdentity,
        op: psa_export_public_key::Operation,
    ) -> Result<psa_export_public_key::Result> {
        let key_name = op.key_name;
        let key_identity = KeyIdentity::new(
            application_identity.clone(),
            self.provider_identity.clone(),
            key_name,
        );
        let key_attributes = self.key_info_store.get_key_attributes(&key_identity)?;
        let key_id = self.key_info_store.get_key_id(&key_identity)?;
        let session = self.new_session()?;

        let key = self.find_key(&session, key_id, KeyPairType::PublicKey)?;
        info!("Located key for export.");
        let data = match key_attributes.key_type {
            Type::RsaKeyPair | Type::RsaPublicKey => {
                self.export_public_rsa_internal(key, &session)?
            }
            Type::EccKeyPair { .. } | Type::EccPublicKey { .. } => {
                self.export_public_ec_internal(key, &session)?
            }
            _ => {
                return Err(ResponseStatus::PsaErrorNotSupported);
            }
        };

        Ok(psa_export_public_key::Result { data: data.into() })
    }

    fn export_public_rsa_internal(&self, key: ObjectHandle, session: &Session) -> Result<Vec<u8>> {
        let mut attributes = session
            .get_attributes(
                key,
                &[AttributeType::Modulus, AttributeType::PublicExponent],
            )
            .map_err(to_response_status)?;

        if attributes.len() != 2 {
            error!("Expected to find modulus and public exponent attributes in public key.");
            return Err(ResponseStatus::PsaErrorCommunicationFailure);
        }

        let modulus = if let Attribute::Modulus(vec) = attributes.remove(0) {
            IntegerAsn1::from_bytes_be_unsigned(vec)
        } else {
            error!("Expected to find modulus attribute.");
            return Err(ResponseStatus::PsaErrorCommunicationFailure);
        };
        let public_exponent = if let Attribute::PublicExponent(vec) = attributes.remove(0) {
            IntegerAsn1::from_bytes_be_unsigned(vec)
        } else {
            error!("Expected to find public exponent attribute.");
            return Err(ResponseStatus::PsaErrorCommunicationFailure);
        };

        let key = RsaPublicKey {
            modulus,
            public_exponent,
        };
        Ok(picky_asn1_der::to_vec(&key).map_err(|err| {
            format_error!("Could not serialise key elements", err);
            ResponseStatus::PsaErrorCommunicationFailure
        })?)
    }

    fn export_public_ec_internal(&self, key: ObjectHandle, session: &Session) -> Result<Vec<u8>> {
        let mut attributes = session
            .get_attributes(key, &[AttributeType::EcPoint])
            .map_err(to_response_status)?;

        if attributes.len() != 1 {
            error!("Expected to find EC point attribute in public key.");
            return Err(ResponseStatus::PsaErrorCommunicationFailure);
        }

        if let Attribute::EcPoint(data) = attributes.remove(0) {
            // The format provided by PKCS11 is an ASN.1 OctetString containing the
            // data that the PSA Crypto interface expects.
            // See ECPoint in [SEC1](https://www.secg.org/sec1-v2.pdf). PKCS11 mandates using
            // [ANSI X9.62 ECPoint](https://cryptsoft.com/pkcs11doc/v220/group__SEC__12__3__3__ECDSA__PUBLIC__KEY__OBJECTS.html),
            // however SEC1 is an equivalent spec.
            let parsed_data = picky_asn1_der::from_bytes::<OctetStringAsn1>(&data);
            match parsed_data {
                Ok(key_data) => Ok(key_data.0),
                // Some PKCS#11 implementations provide the EC_POINT as raw bytes and fail to wrap in ASN.1 OctetString as
                // mandated by the spec. If the ASN.1 parse fails, then we assume that the raw bytes are the EC_POINT data,
                // and return those instead of failing, trading off strictness for broader interoperability.
                Err(_) => Ok(data),
            }
        } else {
            error!("Expected to find modulus attribute.");
            Err(ResponseStatus::PsaErrorCommunicationFailure)
        }
    }

    pub(super) fn psa_destroy_key_internal(
        &self,
        application_identity: &ApplicationIdentity,
        op: psa_destroy_key::Operation,
    ) -> Result<psa_destroy_key::Result> {
        let key_name = op.key_name;
        let key_identity = KeyIdentity::new(
            application_identity.clone(),
            self.provider_identity.clone(),
            key_name,
        );
        let key_id = self.key_info_store.get_key_id(&key_identity)?;

        let _ = self.key_info_store.remove_key_info(&key_identity)?;

        let session = self.new_session()?;

        let first_key = self.find_key(&session, key_id, KeyPairType::Any)?;
        session
            .destroy_object(first_key)
            .map_err(to_response_status)?;

        // Second key is optional.
        match self.find_key(&session, key_id, KeyPairType::Any) {
            Ok(key) => {
                trace!("DestroyObject command");
                session.destroy_object(key).map_err(to_response_status)
            }
            // A second key is optional.
            Err(ResponseStatus::PsaErrorDoesNotExist) => Ok(()),
            Err(e) => {
                format_error!("Error destroying key", e);
                Err(e)
            }
        }?;

        Ok(psa_destroy_key::Result {})
    }
}