1 /*
2 * Copyright (c) 2024, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //! JNI bindings to call into `hwtrust` from Java.
18
19 use anyhow::Result;
20 use hwtrust::{dice, session::Session};
21 use jni::objects::{JByteArray, JClass};
22 use jni::sys::jboolean;
23 use jni::JNIEnv;
24 use log::{debug, error, info};
25
26 /// Validates the given DICE chain.
27 #[no_mangle]
Java_com_android_microdroid_test_HwTrustJni_validateDiceChain( env: JNIEnv, _class: JClass, dice_chain: JByteArray, allow_any_mode: jboolean, ) -> jboolean28 pub extern "system" fn Java_com_android_microdroid_test_HwTrustJni_validateDiceChain(
29 env: JNIEnv,
30 _class: JClass,
31 dice_chain: JByteArray,
32 allow_any_mode: jboolean,
33 ) -> jboolean {
34 android_logger::init_once(
35 android_logger::Config::default()
36 .with_tag("hwtrust_jni")
37 .with_max_level(log::LevelFilter::Debug),
38 );
39 debug!("Starting the DICE chain validation ...");
40 match validate_dice_chain(env, dice_chain, allow_any_mode) {
41 Ok(_) => {
42 info!("DICE chain validated successfully");
43 true
44 }
45 Err(e) => {
46 error!("Failed to validate DICE chain: {:?}", e);
47 false
48 }
49 }
50 .into()
51 }
52
validate_dice_chain( env: JNIEnv, jdice_chain: JByteArray, allow_any_mode: jboolean, ) -> Result<()>53 fn validate_dice_chain(
54 env: JNIEnv,
55 jdice_chain: JByteArray,
56 allow_any_mode: jboolean,
57 ) -> Result<()> {
58 let dice_chain = env.convert_byte_array(jdice_chain)?;
59 let mut session = Session::default();
60 session.set_allow_any_mode(allow_any_mode == jboolean::from(true));
61 let _chain = dice::Chain::from_cbor(&session, &dice_chain)?;
62 Ok(())
63 }
64