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
//! `LAPersistedRight`, `LAPrivateKey`, and `LASecret` wrappers.
use crate::ffi;
use crate::la_error::{LAError, Result};
use crate::la_public_key::{LAPublicKey, SecKeyAlgorithm, SecKeyExchangeParameters};
use crate::la_right::LARightState;
use crate::private::{
bridge_bool, bridge_bytes, bridge_i32, bridge_i64, bridge_ptr, bridge_unit, cstring,
OwnedHandle,
};
/// Managed wrapper around Apple's `LAPersistedRight`.
#[derive(Debug)]
pub struct LAPersistedRight {
handle: OwnedHandle,
}
impl LAPersistedRight {
pub(crate) fn from_raw(raw: std::ptr::NonNull<core::ffi::c_void>) -> Self {
Self {
handle: OwnedHandle::new(raw, ffi::la_persisted_right::la_persisted_right_release),
}
}
pub(crate) const fn as_ptr(&self) -> *mut core::ffi::c_void {
self.handle.as_ptr()
}
/// The current authorization state.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn state(&self) -> Result<LARightState> {
let raw = bridge_i32(|out, error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_get_state(
self.handle.as_ptr(),
out,
error_out,
)
})?;
Ok(LARightState::from_ffi(raw))
}
/// Application-controlled integer tag.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn tag(&self) -> Result<i64> {
bridge_i64(|out, error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_get_tag(
self.handle.as_ptr(),
out,
error_out,
)
})
}
/// Update the application-controlled integer tag.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn set_tag(&self, tag: i64) -> Result<()> {
bridge_unit(|error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_set_tag(
self.handle.as_ptr(),
tag,
error_out,
)
})
}
/// Attempt to authorize the persisted right.
///
/// # Errors
///
/// Returns a mapped framework or bridge error when authorization fails.
pub fn authorize(&self, localized_reason: &str) -> Result<()> {
if localized_reason.is_empty() {
return Err(LAError::InvalidArgument(
"localized reason must not be empty".to_owned(),
));
}
let localized_reason = cstring(localized_reason)?;
bridge_unit(|error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_authorize(
self.handle.as_ptr(),
localized_reason.as_ptr(),
error_out,
)
})
}
/// Preflight whether the persisted right can eventually be authorized.
///
/// # Errors
///
/// Returns a mapped framework or bridge error when authorization is not possible.
pub fn check_can_authorize(&self) -> Result<()> {
bridge_unit(|error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_check_can_authorize(
self.handle.as_ptr(),
error_out,
)
})
}
/// Deauthorize the persisted right.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn deauthorize(&self) -> Result<()> {
bridge_unit(|error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_deauthorize(self.handle.as_ptr(), error_out)
})
}
/// Borrow the managed private key associated with this persisted right.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn key(&self) -> Result<LAPrivateKey> {
Ok(LAPrivateKey::from_raw(bridge_ptr(
|out, error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_get_key(
self.handle.as_ptr(),
out,
error_out,
)
},
)?))
}
/// Borrow the generic secret associated with this persisted right.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn secret(&self) -> Result<LASecret> {
Ok(LASecret::from_raw(bridge_ptr(|out, error_out| unsafe {
ffi::la_persisted_right::la_persisted_right_get_secret(
self.handle.as_ptr(),
out,
error_out,
)
})?))
}
/// Convenience helper returning `self.key()?.public_key()`.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn public_key(&self) -> Result<LAPublicKey> {
self.key()?.public_key()
}
}
/// Managed wrapper around Apple's `LASecret`.
#[derive(Debug)]
pub struct LASecret {
handle: OwnedHandle,
}
impl LASecret {
pub(crate) fn from_raw(raw: std::ptr::NonNull<core::ffi::c_void>) -> Self {
Self {
handle: OwnedHandle::new(raw, ffi::la_persisted_right::la_secret_release),
}
}
/// Load the secret bytes.
///
/// # Errors
///
/// Returns a mapped framework or bridge error if loading fails.
pub fn load_data(&self) -> Result<Vec<u8>> {
bridge_bytes(|out, out_len, error_out| unsafe {
ffi::la_persisted_right::la_secret_load_data(
self.handle.as_ptr(),
out,
out_len,
error_out,
)
})
}
}
/// Managed wrapper around Apple's `LAPrivateKey`.
#[derive(Debug)]
pub struct LAPrivateKey {
handle: OwnedHandle,
}
impl LAPrivateKey {
pub(crate) fn from_raw(raw: std::ptr::NonNull<core::ffi::c_void>) -> Self {
Self {
handle: OwnedHandle::new(raw, ffi::la_persisted_right::la_private_key_release),
}
}
/// Borrow the public-key counterpart of this private key.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn public_key(&self) -> Result<LAPublicKey> {
Ok(LAPublicKey::from_raw(bridge_ptr(
|out, error_out| unsafe {
ffi::la_persisted_right::la_private_key_get_public_key(
self.handle.as_ptr(),
out,
error_out,
)
},
)?))
}
/// Check whether an algorithm can sign with this key.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn can_sign_using(&self, algorithm: &SecKeyAlgorithm) -> Result<bool> {
let algorithm = cstring(algorithm.raw_name())?;
bridge_bool(|out, error_out| unsafe {
ffi::la_persisted_right::la_private_key_can_sign_using_algorithm(
self.handle.as_ptr(),
algorithm.as_ptr(),
out,
error_out,
)
})
}
/// Sign data with this key.
///
/// # Errors
///
/// Returns a mapped framework or bridge error if signing fails.
pub fn sign(&self, data: &[u8], algorithm: &SecKeyAlgorithm) -> Result<Vec<u8>> {
let algorithm = cstring(algorithm.raw_name())?;
bridge_bytes(|out, out_len, error_out| unsafe {
ffi::la_persisted_right::la_private_key_sign_data(
self.handle.as_ptr(),
data.as_ptr(),
data.len(),
algorithm.as_ptr(),
out,
out_len,
error_out,
)
})
}
/// Check whether an algorithm can decrypt with this key.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn can_decrypt_using(&self, algorithm: &SecKeyAlgorithm) -> Result<bool> {
let algorithm = cstring(algorithm.raw_name())?;
bridge_bool(|out, error_out| unsafe {
ffi::la_persisted_right::la_private_key_can_decrypt_using_algorithm(
self.handle.as_ptr(),
algorithm.as_ptr(),
out,
error_out,
)
})
}
/// Decrypt data with this key.
///
/// # Errors
///
/// Returns a mapped framework or bridge error if decryption fails.
pub fn decrypt(&self, data: &[u8], algorithm: &SecKeyAlgorithm) -> Result<Vec<u8>> {
let algorithm = cstring(algorithm.raw_name())?;
bridge_bytes(|out, out_len, error_out| unsafe {
ffi::la_persisted_right::la_private_key_decrypt_data(
self.handle.as_ptr(),
data.as_ptr(),
data.len(),
algorithm.as_ptr(),
out,
out_len,
error_out,
)
})
}
/// Check whether an algorithm can be used for key exchange.
///
/// # Errors
///
/// Returns an error if the Swift bridge rejects the request.
pub fn can_exchange_keys_using(&self, algorithm: &SecKeyAlgorithm) -> Result<bool> {
let algorithm = cstring(algorithm.raw_name())?;
bridge_bool(|out, error_out| unsafe {
ffi::la_persisted_right::la_private_key_can_exchange_keys_using_algorithm(
self.handle.as_ptr(),
algorithm.as_ptr(),
out,
error_out,
)
})
}
/// Perform a Diffie-Hellman-style key exchange with a remote public key.
///
/// # Errors
///
/// Returns a mapped framework or bridge error if key exchange fails.
pub fn exchange_keys_with_public_key(
&self,
public_key: &[u8],
algorithm: &SecKeyAlgorithm,
parameters: &SecKeyExchangeParameters,
) -> Result<Vec<u8>> {
let algorithm = cstring(algorithm.raw_name())?;
let requested_size = parameters
.requested_size_value()
.map(i64::try_from)
.transpose()
.map_err(|_| {
LAError::InvalidArgument(
"requested key-exchange size must fit in a signed 64-bit integer".to_owned(),
)
})?
.unwrap_or(-1);
let shared_info = parameters.shared_info_value();
let shared_info_ptr = shared_info.map_or(std::ptr::null(), <[u8]>::as_ptr);
let shared_info_len = shared_info.map_or(0, <[u8]>::len);
let has_shared_info = u8::from(shared_info.is_some());
bridge_bytes(|out, out_len, error_out| unsafe {
ffi::la_persisted_right::la_private_key_exchange_keys_with_public_key(
self.handle.as_ptr(),
public_key.as_ptr(),
public_key.len(),
algorithm.as_ptr(),
requested_size,
shared_info_ptr,
shared_info_len,
has_shared_info,
out,
out_len,
error_out,
)
})
}
}