security-rs 0.8.0

Safe Rust bindings for Apple's Security framework — keychain, identity, certificates, trust, authorization, CMS, SecureTransport, and cryptographic primitives on macOS
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::ffi::c_void;

use crate::bridge::{self, CoreFoundationHandle, Handle};
use crate::error::{Result, SecurityError};
use crate::secret::SecretBytes;
use crate::Value;

mod internet_password;
pub use internet_password::{InternetCredential, InternetPassword};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
#[must_use]
/// Mirrors `SecAccessControlCreateFlags`.
pub struct AccessControlFlags(u64);

impl AccessControlFlags {
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const DEFAULTS: Self = Self(0);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const USER_PRESENCE: Self = Self(1 << 0);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const BIOMETRY_ANY: Self = Self(1 << 1);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const BIOMETRY_CURRENT_SET: Self = Self(1 << 3);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const DEVICE_PASSCODE: Self = Self(1 << 4);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const COMPANION: Self = Self(1 << 5);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const OR: Self = Self(1 << 14);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const AND: Self = Self(1 << 15);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const PRIVATE_KEY_USAGE: Self = Self(1 << 30);
    /// Mirrors a `SecAccessControlCreateFlags` bit.
    pub const APPLICATION_PASSWORD: Self = Self(1 << 31);

    pub const fn empty() -> Self {
        Self(0)
    }
    pub const fn all() -> Self {
        Self(0xc000_c03b)
    }
    pub const fn bits(self) -> u64 {
        self.0
    }
    pub const fn from_bits(bits: u64) -> Option<Self> {
        if bits & !Self::all().0 == 0 {
            Some(Self(bits))
        } else {
            None
        }
    }
    pub const fn from_bits_retain(bits: u64) -> Self {
        Self(bits)
    }
    pub const fn from_bits_truncate(bits: u64) -> Self {
        Self(bits & Self::all().0)
    }
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }
    pub const fn is_all(self) -> bool {
        self.0 & Self::all().0 == Self::all().0
    }
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }
    pub const fn intersects(self, other: Self) -> bool {
        self.0 & other.0 != 0
    }
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
    pub const fn intersection(self, other: Self) -> Self {
        Self(self.0 & other.0)
    }
    pub const fn difference(self, other: Self) -> Self {
        Self(self.0 & !other.0)
    }
    pub const fn symmetric_difference(self, other: Self) -> Self {
        Self(self.0 ^ other.0)
    }
    pub const fn complement(self) -> Self {
        Self(!self.0 & Self::all().0)
    }
    pub fn insert(&mut self, other: Self) {
        self.0 |= other.0;
    }
    pub fn remove(&mut self, other: Self) {
        self.0 &= !other.0;
    }
    pub fn toggle(&mut self, other: Self) {
        self.0 ^= other.0;
    }
    pub fn set(&mut self, other: Self, value: bool) {
        if value {
            self.0 |= other.0;
        } else {
            self.0 &= !other.0;
        }
    }
}

impl std::ops::BitOr for AccessControlFlags {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}
impl std::ops::BitAnd for AccessControlFlags {
    type Output = Self;
    fn bitand(self, rhs: Self) -> Self {
        Self(self.0 & rhs.0)
    }
}
impl std::ops::BitXor for AccessControlFlags {
    type Output = Self;
    fn bitxor(self, rhs: Self) -> Self {
        Self(self.0 ^ rhs.0)
    }
}
impl std::ops::Sub for AccessControlFlags {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        Self(self.0 & !rhs.0)
    }
}
impl std::ops::Not for AccessControlFlags {
    type Output = Self;
    fn not(self) -> Self {
        Self(!self.0 & Self::all().0)
    }
}
impl std::ops::BitOrAssign for AccessControlFlags {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}
impl std::ops::BitAndAssign for AccessControlFlags {
    fn bitand_assign(&mut self, rhs: Self) {
        self.0 &= rhs.0;
    }
}
impl std::ops::BitXorAssign for AccessControlFlags {
    fn bitxor_assign(&mut self, rhs: Self) {
        self.0 ^= rhs.0;
    }
}
impl std::ops::SubAssign for AccessControlFlags {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 &= !rhs.0;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
/// Mirrors protection classes used by `SecAccessControlCreateWithFlags`.
pub enum AccessControlProtection {
    /// Mirrors a `SecAccessControl` protection-class constant.
    #[default]
    WhenUnlocked,
    /// Mirrors a `SecAccessControl` protection-class constant.
    AfterFirstUnlock,
    /// Mirrors a `SecAccessControl` protection-class constant.
    WhenPasscodeSetThisDeviceOnly,
    /// Mirrors a `SecAccessControl` protection-class constant.
    WhenUnlockedThisDeviceOnly,
    /// Mirrors a `SecAccessControl` protection-class constant.
    AfterFirstUnlockThisDeviceOnly,
}

impl AccessControlProtection {
    const fn as_bridge_name(self) -> &'static str {
        match self {
            Self::WhenUnlocked => "when_unlocked",
            Self::AfterFirstUnlock => "after_first_unlock",
            Self::WhenPasscodeSetThisDeviceOnly => "when_passcode_set_this_device_only",
            Self::WhenUnlockedThisDeviceOnly => "when_unlocked_this_device_only",
            Self::AfterFirstUnlockThisDeviceOnly => "after_first_unlock_this_device_only",
        }
    }
}

#[derive(Debug, Clone)]
/// Wraps `SecAccessControlRef`.
pub struct AccessControl {
    object: CoreFoundationHandle,
    protection: AccessControlProtection,
    flags: AccessControlFlags,
}

impl AccessControl {
    /// Wraps the corresponding `SecAccessControlRef` operation.
    pub fn type_id() -> usize {
        unsafe { bridge::security_access_control_get_type_id() }
    }

    /// Wraps the corresponding `SecAccessControlRef` operation.
    pub fn create(protection: AccessControlProtection, flags: AccessControlFlags) -> Result<Self> {
        let protection_name = bridge::cstring(protection.as_bridge_name())?;
        let mut status = 0;
        let mut error = std::ptr::null_mut();
        let raw = unsafe {
            bridge::security_access_control_create(
                protection_name.as_ptr(),
                flags.bits(),
                &raw mut status,
                &raw mut error,
            )
        };
        let Some(object) = CoreFoundationHandle::from_raw(raw) else {
            return Err(bridge::status_error(
                "security_access_control_create",
                status,
                error,
            )?);
        };
        Ok(Self {
            object,
            protection,
            flags,
        })
    }

    /// Wraps the corresponding `SecAccessControlRef` operation.
    pub fn is_valid(&self) -> bool {
        !self.object.as_ptr().is_null()
    }

    pub const fn protection(&self) -> AccessControlProtection {
        self.protection
    }

    pub const fn flags(&self) -> AccessControlFlags {
        self.flags
    }

    pub const fn as_ptr(&self) -> *mut c_void {
        self.object.as_ptr()
    }
}

#[derive(Debug, Default)]
pub struct KeychainOptions {
    accessibility: AccessControlProtection,
    access_control: Option<AccessControl>,
    access_group: Option<String>,
    synchronizable: bool,
    data_protection_keychain: bool,
    authentication_context: Option<Handle>,
}

impl KeychainOptions {
    #[must_use]
    pub fn accessibility(mut self, accessibility: AccessControlProtection) -> Self {
        self.accessibility = accessibility;
        self
    }

    #[must_use]
    pub fn access_control(mut self, access_control: AccessControl) -> Self {
        self.access_control = Some(access_control);
        self
    }

    #[must_use]
    pub fn access_group(mut self, access_group: impl Into<String>) -> Self {
        self.access_group = Some(access_group.into());
        self
    }

    #[must_use]
    pub fn synchronizable(mut self, synchronizable: bool) -> Self {
        self.synchronizable = synchronizable;
        self
    }

    #[must_use]
    pub fn data_protection_keychain(mut self, data_protection_keychain: bool) -> Self {
        self.data_protection_keychain = data_protection_keychain;
        self
    }

    #[allow(clippy::missing_safety_doc)]
    pub unsafe fn authentication_context(mut self, la_context: *mut c_void) -> Result<Self> {
        let raw = unsafe { bridge::security_authentication_context_retain(la_context) };
        let handle = Handle::from_raw(raw).ok_or_else(|| {
            SecurityError::InvalidArgument("authentication context must be an LAContext".to_owned())
        })?;
        self.authentication_context = Some(handle);
        Ok(self)
    }

    fn bridge_value(&self) -> Value {
        let mut options = std::collections::BTreeMap::from([
            (
                "accessibility".to_owned(),
                Value::String(self.accessibility.as_bridge_name().to_owned()),
            ),
            (
                "synchronizable".to_owned(),
                Value::Bool(self.synchronizable),
            ),
            (
                "data_protection_keychain".to_owned(),
                Value::Bool(self.data_protection_keychain),
            ),
        ]);
        if let Some(access_group) = &self.access_group {
            options.insert(
                "access_group".to_owned(),
                Value::String(access_group.clone()),
            );
        }
        Value::Object(options)
    }

    fn access_control_ptr(&self) -> *mut c_void {
        self.access_control
            .as_ref()
            .map_or(std::ptr::null_mut(), AccessControl::as_ptr)
    }

    fn authentication_context_ptr(&self) -> *mut c_void {
        self.authentication_context
            .as_ref()
            .map_or(std::ptr::null_mut(), Handle::as_ptr)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Wraps a generic-password identity used with `SecItem` queries.
pub struct KeychainEntry {
    account: String,
    service: String,
}

impl KeychainEntry {
    /// Wraps the corresponding generic-password operation built on `SecItem`.
    pub fn new(account: impl Into<String>, service: impl Into<String>) -> Self {
        Self {
            account: account.into(),
            service: service.into(),
        }
    }

    /// Wraps the corresponding generic-password operation built on `SecItem`.
    pub fn account(&self) -> &str {
        &self.account
    }

    /// Wraps the corresponding generic-password operation built on `SecItem`.
    pub fn service(&self) -> &str {
        &self.service
    }

    /// Wraps the corresponding generic-password operation built on `SecItem`.
    pub fn set(&self, secret: impl AsRef<[u8]>) -> Result<()> {
        Keychain::set(&self.account, &self.service, secret)
    }

    /// Wraps the corresponding generic-password operation built on `SecItem`.
    pub fn get(&self) -> Result<SecretBytes> {
        Keychain::get(&self.account, &self.service)
    }

    /// Wraps the corresponding generic-password operation built on `SecItem`.
    pub fn delete(&self) -> Result<()> {
        Keychain::delete(&self.account, &self.service)
    }
}

/// Wraps generic-password operations built on `SecItem` APIs.
pub struct Keychain;

impl Keychain {
    /// Wraps the corresponding generic-password `SecItem` operation.
    pub fn entry(account: impl Into<String>, service: impl Into<String>) -> KeychainEntry {
        KeychainEntry::new(account, service)
    }

    /// Wraps the corresponding generic-password `SecItem` operation.
    pub fn set(account: &str, service: &str, secret: impl AsRef<[u8]>) -> Result<()> {
        Self::set_with_options(account, service, secret, &KeychainOptions::default())
    }

    /// Wraps the corresponding generic-password `SecItem` operation.
    pub fn get(account: &str, service: &str) -> Result<SecretBytes> {
        Self::get_with_options(account, service, &KeychainOptions::default())
    }

    /// Wraps the corresponding generic-password `SecItem` operation.
    pub fn delete(account: &str, service: &str) -> Result<()> {
        Self::delete_with_options(account, service, &KeychainOptions::default())
    }

    /// Wraps the corresponding generic-password `SecItem` operation.
    pub fn list_accounts(service: &str) -> Result<Vec<String>> {
        Self::list_accounts_with_options(service, &KeychainOptions::default())
    }

    pub fn set_with_options(
        account: &str,
        service: &str,
        secret: impl AsRef<[u8]>,
        options: &KeychainOptions,
    ) -> Result<()> {
        let secret = secret.as_ref();
        let account = bridge::cstring(account)?;
        let service = bridge::cstring(service)?;
        let options_value = bridge::InputValue::new(&options.bridge_value())?;
        let mut error = std::ptr::null_mut();
        let status = unsafe {
            bridge::security_keychain_set_item(
                account.as_ptr(),
                service.as_ptr(),
                secret.as_ptr().cast(),
                bridge::len_to_isize(secret.len())?,
                options_value.as_ptr(),
                options.access_control_ptr(),
                options.authentication_context_ptr(),
                &raw mut error,
            )
        };
        bridge::status_result("security_keychain_set_item", status, error)
    }

    pub fn get_with_options(
        account: &str,
        service: &str,
        options: &KeychainOptions,
    ) -> Result<SecretBytes> {
        let account = bridge::cstring(account)?;
        let service = bridge::cstring(service)?;
        let options_value = bridge::InputValue::new(&options.bridge_value())?;
        let mut status = 0;
        let mut error = std::ptr::null_mut();
        let raw = unsafe {
            bridge::security_keychain_copy_item(
                account.as_ptr(),
                service.as_ptr(),
                options_value.as_ptr(),
                options.authentication_context_ptr(),
                &raw mut status,
                &raw mut error,
            )
        };
        bridge::required_secret("security_keychain_copy_item", raw, status, error)
    }

    pub fn delete_with_options(
        account: &str,
        service: &str,
        options: &KeychainOptions,
    ) -> Result<()> {
        let account = bridge::cstring(account)?;
        let service = bridge::cstring(service)?;
        let options_value = bridge::InputValue::new(&options.bridge_value())?;
        let mut error = std::ptr::null_mut();
        let status = unsafe {
            bridge::security_keychain_delete_item(
                account.as_ptr(),
                service.as_ptr(),
                options_value.as_ptr(),
                options.authentication_context_ptr(),
                &raw mut error,
            )
        };
        bridge::status_result("security_keychain_delete_item", status, error)
    }

    pub fn list_accounts_with_options(
        service: &str,
        options: &KeychainOptions,
    ) -> Result<Vec<String>> {
        let service = bridge::cstring(service)?;
        let options_value = bridge::InputValue::new(&options.bridge_value())?;
        let mut status = 0;
        let mut error = std::ptr::null_mut();
        let raw = unsafe {
            bridge::security_keychain_list_accounts(
                service.as_ptr(),
                options_value.as_ptr(),
                options.authentication_context_ptr(),
                &raw mut status,
                &raw mut error,
            )
        };
        bridge::required_value("security_keychain_list_accounts", raw, status, error)?
            .into_strings("security_keychain_list_accounts")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn access_control_protection_base_bridge_names_are_stable() {
        assert_eq!(
            AccessControlProtection::WhenUnlocked.as_bridge_name(),
            "when_unlocked"
        );
        assert_eq!(
            AccessControlProtection::AfterFirstUnlock.as_bridge_name(),
            "after_first_unlock"
        );
    }

    #[test]
    fn access_control_protection_device_only_bridge_names_are_stable() {
        assert_eq!(
            AccessControlProtection::WhenPasscodeSetThisDeviceOnly.as_bridge_name(),
            "when_passcode_set_this_device_only"
        );
        assert_eq!(
            AccessControlProtection::WhenUnlockedThisDeviceOnly.as_bridge_name(),
            "when_unlocked_this_device_only"
        );
        assert_eq!(
            AccessControlProtection::AfterFirstUnlockThisDeviceOnly.as_bridge_name(),
            "after_first_unlock_this_device_only"
        );
    }

    #[test]
    fn access_control_flags_round_trip_through_bits() {
        let flags = AccessControlFlags::USER_PRESENCE
            | AccessControlFlags::PRIVATE_KEY_USAGE
            | AccessControlFlags::APPLICATION_PASSWORD;

        assert_eq!(AccessControlFlags::from_bits(flags.bits()), Some(flags));
    }

    #[test]
    fn default_options_protect_items_when_unlocked() {
        let value = KeychainOptions::default().bridge_value();
        assert_eq!(
            value.get("accessibility").and_then(Value::as_str),
            Some("when_unlocked")
        );
        assert_eq!(
            value.get("synchronizable").and_then(Value::as_bool),
            Some(false)
        );
        assert_eq!(
            value
                .get("data_protection_keychain")
                .and_then(Value::as_bool),
            Some(false)
        );
        assert!(value.get("access_group").is_none());
        assert_eq!(
            AccessControlProtection::default(),
            AccessControlProtection::WhenUnlocked
        );
    }

    #[test]
    fn options_serialize_every_selected_control() {
        let options = KeychainOptions::default()
            .accessibility(AccessControlProtection::WhenUnlockedThisDeviceOnly)
            .access_group("TEAMID.group")
            .synchronizable(true)
            .data_protection_keychain(true);
        let value = options.bridge_value();
        assert_eq!(
            value.get("accessibility").and_then(Value::as_str),
            Some("when_unlocked_this_device_only")
        );
        assert_eq!(
            value.get("access_group").and_then(Value::as_str),
            Some("TEAMID.group")
        );
        assert_eq!(
            value.get("synchronizable").and_then(Value::as_bool),
            Some(true)
        );
        assert_eq!(
            value
                .get("data_protection_keychain")
                .and_then(Value::as_bool),
            Some(true)
        );
        assert!(options.access_control_ptr().is_null());
        assert!(options.authentication_context_ptr().is_null());
    }

    #[test]
    fn keychain_entry_new_preserves_account_and_service() {
        let entry = KeychainEntry::new("alice", "security-rs.tests");

        assert_eq!(entry.account(), "alice");
        assert_eq!(entry.service(), "security-rs.tests");
    }

    #[test]
    fn keychain_entry_convenience_constructor_matches_new() {
        let entry = Keychain::entry("alice", "security-rs.tests");

        assert_eq!(entry, KeychainEntry::new("alice", "security-rs.tests"));
    }
}