| 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 | pub struct SecurityCtx {
|
| 21 | ctx: bindings::lsm_context,
|
| 22 | }
|
| 23 |
|
| 24 | impl SecurityCtx {
|
| 25 | |
| 26 | #[inline]
|
| 27 | pub fn from_secid(secid: u32) -> Result<Self> {
|
| 28 | |
| 29 | let mut ctx: bindings::lsm_context = unsafe { core::mem::zeroed() };
|
| 30 |
|
| 31 | |
| 32 | to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut ctx) })?;
|
| 33 |
|
| 34 | |
| 35 | Ok(Self { ctx })
|
| 36 | }
|
| 37 |
|
| 38 | |
| 39 | #[inline]
|
| 40 | pub fn is_empty(&self) -> bool {
|
| 41 | self.ctx.len == 0
|
| 42 | }
|
| 43 |
|
| 44 | |
| 45 | #[inline]
|
| 46 | pub fn len(&self) -> usize {
|
| 47 | self.ctx.len as usize
|
| 48 | }
|
| 49 |
|
| 50 | |
| 51 | #[inline]
|
| 52 | pub fn as_bytes(&self) -> &[u8] {
|
| 53 | let ptr = self.ctx.context;
|
| 54 | if ptr.is_null() {
|
| 55 | debug_assert_eq!(self.len(), 0);
|
| 56 | |
| 57 | return &[];
|
| 58 | }
|
| 59 |
|
| 60 | |
| 61 | |
| 62 | |
| 63 | unsafe { core::slice::from_raw_parts(ptr.cast(), self.len()) }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | impl Drop for SecurityCtx {
|
| 68 | #[inline]
|
| 69 | fn drop(&mut self) {
|
| 70 | |
| 71 | |
| 72 | unsafe { bindings::security_release_secctx(&mut self.ctx) };
|
| 73 | }
|
| 74 | }
|