blob: cbec7fdd3ef9c5c0df2081443e0ac95bece665ff [file] [log] [blame]
Alan Stokes14f07392021-09-27 14:03:31 +01001// Copyright 2021, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Victor Hsieh6fb8b252021-12-03 16:30:04 -080015//! Native helpers for composd.
Alan Stokes14f07392021-09-27 14:03:31 +010016
Victor Hsieh6fb8b252021-12-03 16:30:04 -080017pub use art::*;
18pub use crypto::*;
Alan Stokes14f07392021-09-27 14:03:31 +010019
20#[cxx::bridge]
Victor Hsieh6fb8b252021-12-03 16:30:04 -080021mod crypto {
Alan Stokes14f07392021-09-27 14:03:31 +010022 /// Contains either a key or a reason why the key could not be extracted.
23 struct KeyResult {
24 /// The extracted key. If empty, the attempt to extract the key failed.
25 key: Vec<u8>,
26 /// A description of what went wrong if the attempt failed.
27 error: String,
28 }
29
30 unsafe extern "C++" {
31 include!("composd_native.h");
32
33 // SAFETY: The C++ implementation manages its own memory, and does not retain or abuse
34 // the der_certificate reference. cxx handles the mapping of the return value.
35
36 /// Parse the supplied DER X.509 certificate and extract the subject's RsaPublicKey.
37 fn extract_rsa_public_key(der_certificate: &[u8]) -> KeyResult;
38 }
39}
Victor Hsieh6fb8b252021-12-03 16:30:04 -080040
41mod art {
42 use anyhow::{anyhow, Result};
43 use libc::c_char;
44 use std::ffi::{CStr, OsStr};
45 use std::io::Error;
46 use std::os::unix::ffi::OsStrExt;
47 use std::path::Path;
48 use std::ptr::null;
49
50 // From libartpalette(-system)
51 extern "C" {
52 fn PaletteCreateOdrefreshStagingDirectory(out_staging_dir: *mut *const c_char) -> i32;
53 }
54 const PALETTE_STATUS_OK: i32 = 0;
55 const PALETTE_STATUS_CHECK_ERRNO: i32 = 1;
56
57 /// Creates and returns the staging directory for odrefresh.
58 pub fn palette_create_odrefresh_staging_directory() -> Result<&'static Path> {
59 let mut staging_dir: *const c_char = null();
60 // SAFETY: The C function always returns a non-null C string (after created the directory).
61 let status = unsafe { PaletteCreateOdrefreshStagingDirectory(&mut staging_dir) };
62 match status {
63 PALETTE_STATUS_OK => {
64 // SAFETY: The previously returned `*const c_char` should point to a legitimate C
65 // string.
66 let cstr = unsafe { CStr::from_ptr(staging_dir) };
67 let path = OsStr::from_bytes(cstr.to_bytes()).as_ref();
68 Ok(path)
69 }
70 PALETTE_STATUS_CHECK_ERRNO => Err(anyhow!(Error::last_os_error().to_string())),
71 _ => Err(anyhow!("Failed with palette status {}", status)),
72 }
73 }
74}