| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{
|
| 10 | bindings,
|
| 11 | error::{to_result, Result},
|
| 12 | };
|
| 13 |
|
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | pub struct SecurityCtx {
|
| 22 | secdata: *mut core::ffi::c_char,
|
| 23 | seclen: usize,
|
| 24 | }
|
| 25 |
|
| 26 | impl SecurityCtx {
|
| 27 | |
| 28 | pub fn from_secid(secid: u32) -> Result<Self> {
|
| 29 | let mut secdata = core::ptr::null_mut();
|
| 30 | let mut seclen = 0u32;
|
| 31 | |
| 32 | to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut secdata, &mut seclen) })?;
|
| 33 |
|
| 34 | |
| 35 | Ok(Self {
|
| 36 | secdata,
|
| 37 | seclen: seclen as usize,
|
| 38 | })
|
| 39 | }
|
| 40 |
|
| 41 | |
| 42 | pub fn is_empty(&self) -> bool {
|
| 43 | self.seclen == 0
|
| 44 | }
|
| 45 |
|
| 46 | |
| 47 | pub fn len(&self) -> usize {
|
| 48 | self.seclen
|
| 49 | }
|
| 50 |
|
| 51 | |
| 52 | pub fn as_bytes(&self) -> &[u8] {
|
| 53 | let ptr = self.secdata;
|
| 54 | if ptr.is_null() {
|
| 55 | debug_assert_eq!(self.seclen, 0);
|
| 56 | |
| 57 | return &[];
|
| 58 | }
|
| 59 |
|
| 60 | |
| 61 | |
| 62 | |
| 63 | unsafe { core::slice::from_raw_parts(ptr.cast(), self.seclen) }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | impl Drop for SecurityCtx {
|
| 68 | fn drop(&mut self) {
|
| 69 | |
| 70 | |
| 71 | |
| 72 | unsafe { bindings::security_release_secctx(self.secdata, self.seclen as u32) };
|
| 73 | }
|
| 74 | }
|