blob: 29d72956af5c7ffa314cf1e72122e837fcb2c260 [file] [log] [blame]
Janis Danisevskisda092a12021-11-17 22:58:16 -08001// 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
15use android_hardware_security_dice::aidl::android::hardware::security::dice::{
16 Config::Config as BinderConfig, InputValues::InputValues as BinderInputValues,
17 Mode::Mode as BinderMode,
18};
19use android_security_dice::aidl::android::security::dice::IDiceMaintenance::IDiceMaintenance;
20use android_security_dice::aidl::android::security::dice::IDiceNode::IDiceNode;
Janis Danisevskisda092a12021-11-17 22:58:16 -080021use binder::Strong;
Alice Wang4d3059a2023-02-15 10:24:33 +000022use diced_open_dice::DiceArtifacts;
Janis Danisevskisda092a12021-11-17 22:58:16 -080023use diced_open_dice_cbor as dice;
24use nix::libc::uid_t;
25use std::convert::TryInto;
Alice Wang9c40eca2023-02-03 13:10:24 +000026use std::ffi::CString;
Janis Danisevskisda092a12021-11-17 22:58:16 -080027
28static DICE_NODE_SERVICE_NAME: &str = "android.security.dice.IDiceNode";
29static DICE_MAINTENANCE_SERVICE_NAME: &str = "android.security.dice.IDiceMaintenance";
30
31fn get_dice_node() -> Strong<dyn IDiceNode> {
32 binder::get_interface(DICE_NODE_SERVICE_NAME).unwrap()
33}
34
35fn get_dice_maintenance() -> Strong<dyn IDiceMaintenance> {
36 binder::get_interface(DICE_MAINTENANCE_SERVICE_NAME).unwrap()
37}
38
39static TEST_MESSAGE: &[u8] = &[
40 // "My test message!"
41 0x4d, 0x79, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x21,
42 0x0a,
43];
44
45// This test calls derive with an empty argument vector and with a set of three input values.
46// It then performs the same three derivation steps on the result of the former and compares
47// the result to the result of the latter.
48fn equivalence_test() {
49 let node = get_dice_node();
50 let input_values = diced_sample_inputs::get_input_values_vector();
51 let former = node.derive(&[]).expect("Trying to call derive.");
52 let latter = node.derive(&input_values).expect("Trying to call derive with input values.");
Janis Danisevskisd9513f42021-12-16 17:15:13 -080053 let artifacts =
54 diced_utils::ResidentArtifacts::new(&former.cdiAttest, &former.cdiSeal, &former.bcc.data)
55 .unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -080056
Alice Wang93e6e372023-02-02 14:36:17 +000057 let artifacts = artifacts.execute_steps(input_values.iter()).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -080058 let from_former = diced_utils::make_bcc_handover(
Alice Wang4d3059a2023-02-15 10:24:33 +000059 artifacts.cdi_attest(),
60 artifacts.cdi_seal(),
61 artifacts.bcc().expect("bcc is none"),
Janis Danisevskisda092a12021-11-17 22:58:16 -080062 )
63 .unwrap();
64 // TODO when we have a parser/verifier, check equivalence rather
65 // than bit by bit equality.
66 assert_eq!(latter, from_former);
67}
68
69fn sign_and_verify() {
70 let node = get_dice_node();
71 let _signature = node.sign(&[], TEST_MESSAGE).expect("Trying to call sign.");
72
73 let _bcc = node.getAttestationChain(&[]).expect("Trying to call getAttestationChain.");
74 // TODO b/204938506 check the signature with the bcc when the verifier is available.
75}
76
77// This test calls derive with an empty argument vector, then demotes the itself using
78// a set of three input values, and then calls derive with empty argument vector again.
79// It then performs the same three derivation steps on the result of the former and compares
80// the result to the result of the latter.
81fn demote_test() {
82 let node = get_dice_node();
83 let input_values = diced_sample_inputs::get_input_values_vector();
84 let former = node.derive(&[]).expect("Trying to call derive.");
85 node.demote(&input_values).expect("Trying to call demote with input values.");
86
87 let latter = node.derive(&[]).expect("Trying to call derive after demote.");
88
89 let artifacts = diced_utils::ResidentArtifacts::new(
90 former.cdiAttest[..].try_into().unwrap(),
91 former.cdiSeal[..].try_into().unwrap(),
92 &former.bcc.data,
93 )
94 .unwrap();
95
Alice Wang93e6e372023-02-02 14:36:17 +000096 let artifacts = artifacts.execute_steps(input_values.iter()).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -080097 let from_former = diced_utils::make_bcc_handover(
Alice Wang4d3059a2023-02-15 10:24:33 +000098 artifacts.cdi_attest(),
99 artifacts.cdi_seal(),
100 artifacts.bcc().expect("bcc is none"),
Janis Danisevskisda092a12021-11-17 22:58:16 -0800101 )
102 .unwrap();
103 // TODO b/204938506 when we have a parser/verifier, check equivalence rather
104 // than bit by bit equality.
105 assert_eq!(latter, from_former);
106}
107
108fn client_input_values(uid: uid_t) -> BinderInputValues {
Alice Wang9c40eca2023-02-03 13:10:24 +0000109 let desc = CString::new(format!("{}", uid)).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -0800110 BinderInputValues {
Janis Danisevskisd9513f42021-12-16 17:15:13 -0800111 codeHash: [0; dice::HASH_SIZE],
Janis Danisevskisda092a12021-11-17 22:58:16 -0800112 config: BinderConfig {
Alice Wang9c40eca2023-02-03 13:10:24 +0000113 desc: dice::retry_bcc_format_config_descriptor(Some(desc.as_c_str()), None, true)
Janis Danisevskisda092a12021-11-17 22:58:16 -0800114 .unwrap(),
115 },
Janis Danisevskisd9513f42021-12-16 17:15:13 -0800116 authorityHash: [0; dice::HASH_SIZE],
Janis Danisevskisda092a12021-11-17 22:58:16 -0800117 authorityDescriptor: None,
118 mode: BinderMode::NORMAL,
Janis Danisevskisd9513f42021-12-16 17:15:13 -0800119 hidden: [0; dice::HIDDEN_SIZE],
Janis Danisevskisda092a12021-11-17 22:58:16 -0800120 }
121}
122
123// This test calls derive with an empty argument vector `former` which look like this:
124// <common root> | <caller>
125// It then demotes diced using a set of three input values prefixed with the uid based input
126// values that diced would add to any call. It then calls derive with empty argument vector
127// again which will add another step using the identity of the caller. If diced was demoted
128// correctly the chain of `latter` will
129// look as follows:
130// <common root> | <caller> | <the three sample inputs> | <caller>
131//
132// It then performs the same three derivation steps followed by a set of caller input values
133// on `former` and compares it to `latter`.
134fn demote_self_test() {
135 let maintenance = get_dice_maintenance();
136 let node = get_dice_node();
137 let input_values = diced_sample_inputs::get_input_values_vector();
138 let former = node.derive(&[]).expect("Trying to call derive.");
139
140 let client = client_input_values(nix::unistd::getuid().into());
141
142 let mut demote_vector = vec![client.clone()];
143 demote_vector.append(&mut input_values.clone());
144 maintenance.demoteSelf(&demote_vector).expect("Trying to call demote_self with input values.");
145
146 let latter = node.derive(&[]).expect("Trying to call derive after demote.");
147
148 let artifacts = diced_utils::ResidentArtifacts::new(
149 former.cdiAttest[..].try_into().unwrap(),
150 former.cdiSeal[..].try_into().unwrap(),
151 &former.bcc.data,
152 )
153 .unwrap();
154
155 let client = [client];
Janis Danisevskisda092a12021-11-17 22:58:16 -0800156
Alice Wang93e6e372023-02-02 14:36:17 +0000157 let artifacts = artifacts.execute_steps(input_values.iter().chain(client.iter())).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -0800158 let from_former = diced_utils::make_bcc_handover(
Alice Wang4d3059a2023-02-15 10:24:33 +0000159 artifacts.cdi_attest(),
160 artifacts.cdi_seal(),
161 artifacts.bcc().expect("bcc is none"),
Janis Danisevskisda092a12021-11-17 22:58:16 -0800162 )
163 .unwrap();
164 // TODO b/204938506 when we have a parser/verifier, check equivalence rather
165 // than bit by bit equality.
166 assert_eq!(latter, from_former);
167}
168
169#[test]
170fn run_serialized_test() {
171 equivalence_test();
172 sign_and_verify();
173 // The demote self test must run before the demote test or the test fails.
174 // And since demotion is not reversible the test can only pass once per boot.
175 demote_self_test();
176 demote_test();
177}