xref: /aosp_15_r20/system/security/keystore2/aaid/lib.rs (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker // Copyright 2020, The Android Open Source Project
2*e1997b9aSAndroid Build Coastguard Worker //
3*e1997b9aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*e1997b9aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*e1997b9aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*e1997b9aSAndroid Build Coastguard Worker //
7*e1997b9aSAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*e1997b9aSAndroid Build Coastguard Worker //
9*e1997b9aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*e1997b9aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*e1997b9aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e1997b9aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*e1997b9aSAndroid Build Coastguard Worker // limitations under the License.
14*e1997b9aSAndroid Build Coastguard Worker 
15*e1997b9aSAndroid Build Coastguard Worker //! Rust binding for getting the attestation application id.
16*e1997b9aSAndroid Build Coastguard Worker 
17*e1997b9aSAndroid Build Coastguard Worker use keystore2_aaid_bindgen::{
18*e1997b9aSAndroid Build Coastguard Worker     aaid_keystore_attestation_id, KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE,
19*e1997b9aSAndroid Build Coastguard Worker };
20*e1997b9aSAndroid Build Coastguard Worker 
21*e1997b9aSAndroid Build Coastguard Worker /// Returns the attestation application id for the given uid or an error code
22*e1997b9aSAndroid Build Coastguard Worker /// corresponding to ::android::status_t.
get_aaid(uid: u32) -> Result<Vec<u8>, u32>23*e1997b9aSAndroid Build Coastguard Worker pub fn get_aaid(uid: u32) -> Result<Vec<u8>, u32> {
24*e1997b9aSAndroid Build Coastguard Worker     let mut buffer = [0u8; KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE];
25*e1997b9aSAndroid Build Coastguard Worker     let mut size = KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE;
26*e1997b9aSAndroid Build Coastguard Worker     // Safety:
27*e1997b9aSAndroid Build Coastguard Worker     // aaid_keystore_attestation_id expects a buffer of exactly
28*e1997b9aSAndroid Build Coastguard Worker     // KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE bytes and returns the number of bytes written
29*e1997b9aSAndroid Build Coastguard Worker     // in the second pointer argument.
30*e1997b9aSAndroid Build Coastguard Worker     let status = unsafe { aaid_keystore_attestation_id(uid, buffer.as_mut_ptr(), &mut size) };
31*e1997b9aSAndroid Build Coastguard Worker     match status {
32*e1997b9aSAndroid Build Coastguard Worker         0 => Ok(buffer[0..size].to_vec()),
33*e1997b9aSAndroid Build Coastguard Worker         status => Err(status),
34*e1997b9aSAndroid Build Coastguard Worker     }
35*e1997b9aSAndroid Build Coastguard Worker }
36