blob: f579a4ef5d047130500defd8eb5ecd4d3f7f7de0 [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;
22use diced_open_dice_cbor as dice;
23use nix::libc::uid_t;
24use std::convert::TryInto;
25
26static DICE_NODE_SERVICE_NAME: &str = "android.security.dice.IDiceNode";
27static DICE_MAINTENANCE_SERVICE_NAME: &str = "android.security.dice.IDiceMaintenance";
28
29fn get_dice_node() -> Strong<dyn IDiceNode> {
30 binder::get_interface(DICE_NODE_SERVICE_NAME).unwrap()
31}
32
33fn get_dice_maintenance() -> Strong<dyn IDiceMaintenance> {
34 binder::get_interface(DICE_MAINTENANCE_SERVICE_NAME).unwrap()
35}
36
37static TEST_MESSAGE: &[u8] = &[
38 // "My test message!"
39 0x4d, 0x79, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x21,
40 0x0a,
41];
42
43// This test calls derive with an empty argument vector and with a set of three input values.
44// It then performs the same three derivation steps on the result of the former and compares
45// the result to the result of the latter.
46fn equivalence_test() {
47 let node = get_dice_node();
48 let input_values = diced_sample_inputs::get_input_values_vector();
49 let former = node.derive(&[]).expect("Trying to call derive.");
50 let latter = node.derive(&input_values).expect("Trying to call derive with input values.");
Janis Danisevskisd9513f42021-12-16 17:15:13 -080051 let artifacts =
52 diced_utils::ResidentArtifacts::new(&former.cdiAttest, &former.cdiSeal, &former.bcc.data)
53 .unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -080054
Alice Wang93e6e372023-02-02 14:36:17 +000055 let artifacts = artifacts.execute_steps(input_values.iter()).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -080056 let (cdi_attest, cdi_seal, bcc) = artifacts.into_tuple();
57 let from_former = diced_utils::make_bcc_handover(
58 cdi_attest[..].try_into().unwrap(),
59 cdi_seal[..].try_into().unwrap(),
60 &bcc,
61 )
62 .unwrap();
63 // TODO when we have a parser/verifier, check equivalence rather
64 // than bit by bit equality.
65 assert_eq!(latter, from_former);
66}
67
68fn sign_and_verify() {
69 let node = get_dice_node();
70 let _signature = node.sign(&[], TEST_MESSAGE).expect("Trying to call sign.");
71
72 let _bcc = node.getAttestationChain(&[]).expect("Trying to call getAttestationChain.");
73 // TODO b/204938506 check the signature with the bcc when the verifier is available.
74}
75
76// This test calls derive with an empty argument vector, then demotes the itself using
77// a set of three input values, and then calls derive with empty argument vector again.
78// It then performs the same three derivation steps on the result of the former and compares
79// the result to the result of the latter.
80fn demote_test() {
81 let node = get_dice_node();
82 let input_values = diced_sample_inputs::get_input_values_vector();
83 let former = node.derive(&[]).expect("Trying to call derive.");
84 node.demote(&input_values).expect("Trying to call demote with input values.");
85
86 let latter = node.derive(&[]).expect("Trying to call derive after demote.");
87
88 let artifacts = diced_utils::ResidentArtifacts::new(
89 former.cdiAttest[..].try_into().unwrap(),
90 former.cdiSeal[..].try_into().unwrap(),
91 &former.bcc.data,
92 )
93 .unwrap();
94
Alice Wang93e6e372023-02-02 14:36:17 +000095 let artifacts = artifacts.execute_steps(input_values.iter()).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -080096 let (cdi_attest, cdi_seal, bcc) = artifacts.into_tuple();
97 let from_former = diced_utils::make_bcc_handover(
98 cdi_attest[..].try_into().unwrap(),
99 cdi_seal[..].try_into().unwrap(),
100 &bcc,
101 )
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 {
109 BinderInputValues {
Janis Danisevskisd9513f42021-12-16 17:15:13 -0800110 codeHash: [0; dice::HASH_SIZE],
Janis Danisevskisda092a12021-11-17 22:58:16 -0800111 config: BinderConfig {
112 desc: dice::bcc::format_config_descriptor(Some(&format!("{}", uid)), None, true)
113 .unwrap(),
114 },
Janis Danisevskisd9513f42021-12-16 17:15:13 -0800115 authorityHash: [0; dice::HASH_SIZE],
Janis Danisevskisda092a12021-11-17 22:58:16 -0800116 authorityDescriptor: None,
117 mode: BinderMode::NORMAL,
Janis Danisevskisd9513f42021-12-16 17:15:13 -0800118 hidden: [0; dice::HIDDEN_SIZE],
Janis Danisevskisda092a12021-11-17 22:58:16 -0800119 }
120}
121
122// This test calls derive with an empty argument vector `former` which look like this:
123// <common root> | <caller>
124// It then demotes diced using a set of three input values prefixed with the uid based input
125// values that diced would add to any call. It then calls derive with empty argument vector
126// again which will add another step using the identity of the caller. If diced was demoted
127// correctly the chain of `latter` will
128// look as follows:
129// <common root> | <caller> | <the three sample inputs> | <caller>
130//
131// It then performs the same three derivation steps followed by a set of caller input values
132// on `former` and compares it to `latter`.
133fn demote_self_test() {
134 let maintenance = get_dice_maintenance();
135 let node = get_dice_node();
136 let input_values = diced_sample_inputs::get_input_values_vector();
137 let former = node.derive(&[]).expect("Trying to call derive.");
138
139 let client = client_input_values(nix::unistd::getuid().into());
140
141 let mut demote_vector = vec![client.clone()];
142 demote_vector.append(&mut input_values.clone());
143 maintenance.demoteSelf(&demote_vector).expect("Trying to call demote_self with input values.");
144
145 let latter = node.derive(&[]).expect("Trying to call derive after demote.");
146
147 let artifacts = diced_utils::ResidentArtifacts::new(
148 former.cdiAttest[..].try_into().unwrap(),
149 former.cdiSeal[..].try_into().unwrap(),
150 &former.bcc.data,
151 )
152 .unwrap();
153
154 let client = [client];
Janis Danisevskisda092a12021-11-17 22:58:16 -0800155
Alice Wang93e6e372023-02-02 14:36:17 +0000156 let artifacts = artifacts.execute_steps(input_values.iter().chain(client.iter())).unwrap();
Janis Danisevskisda092a12021-11-17 22:58:16 -0800157 let (cdi_attest, cdi_seal, bcc) = artifacts.into_tuple();
158 let from_former = diced_utils::make_bcc_handover(
159 cdi_attest[..].try_into().unwrap(),
160 cdi_seal[..].try_into().unwrap(),
161 &bcc,
162 )
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}