1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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 */
15
16 //! This module implements the capability of processing the identity information of the Asset caller.
17
18 use ipc_rust::get_calling_uid;
19
20 use asset_definition::{log_throw_error, ErrCode, Result};
21
22 use crate::{transfer_error_code, SUCCESS};
23
24 use super::OwnerType;
25
26 /// The identity of calling process.
27 #[derive(Clone)]
28 #[derive(PartialEq, Eq)]
29 pub struct CallingInfo {
30 user_id: i32,
31 owner_type: OwnerType,
32 owner_info: Vec<u8>,
33 }
34
35 #[allow(dead_code)]
36 #[repr(C)]
37 enum ResultCode {
38 Success = 0,
39 InvalidArgument = 1,
40 BmsError = 2,
41 AccessTokenError = 3,
42 }
43
44 extern "C" {
GetUserIdByUid(uid: u64, userId: &mut i32) -> bool45 fn GetUserIdByUid(uid: u64, userId: &mut i32) -> bool;
GetOwnerInfo(userId: i32, uid: u64, ownerType: *mut OwnerType, ownerInfo: *mut u8, infoLen: *mut u32) -> i3246 fn GetOwnerInfo(userId: i32, uid: u64, ownerType: *mut OwnerType, ownerInfo: *mut u8, infoLen: *mut u32) -> i32;
47 }
48
get_user_id(uid: u64) -> Result<i32>49 pub(crate) fn get_user_id(uid: u64) -> Result<i32> {
50 unsafe {
51 let mut user_id = 0;
52 if GetUserIdByUid(uid, &mut user_id) {
53 Ok(user_id)
54 } else {
55 log_throw_error!(ErrCode::AccountError, "[FATAL]Get user id failed.")
56 }
57 }
58 }
59
60 impl CallingInfo {
61 /// Build identity of current process.
new_self() -> Self62 pub fn new_self() -> Self {
63 Self::new(0, OwnerType::Native, "huks_service_3510".as_bytes().to_vec())
64 }
65
66 /// Build identity of the specified owner.
new(user_id: i32, owner_type: OwnerType, owner_info: Vec<u8>) -> Self67 pub fn new(user_id: i32, owner_type: OwnerType, owner_info: Vec<u8>) -> Self {
68 Self { user_id, owner_type, owner_info }
69 }
70
71 /// Build a instance of CallingInfo.
build() -> Result<Self>72 pub fn build() -> Result<Self> {
73 let uid = get_calling_uid();
74 let user_id: i32 = get_user_id(uid)?;
75 let mut owner_info = vec![0u8; 256];
76 let mut len = 256u32;
77 let mut owner_type = OwnerType::Hap;
78 let err = unsafe { GetOwnerInfo(user_id, uid, &mut owner_type, owner_info.as_mut_ptr(), &mut len) };
79 match err {
80 SUCCESS => {
81 owner_info.truncate(len as usize);
82 Ok(CallingInfo { user_id, owner_type, owner_info })
83 },
84 _ => Err(transfer_error_code(ErrCode::try_from(err as u32)?)),
85 }
86 }
87
88 /// Get owner type of calling.
owner_type(&self) -> u3289 pub fn owner_type(&self) -> u32 {
90 self.owner_type as u32
91 }
92
93 /// Get owner info of calling.
owner_info(&self) -> &Vec<u8>94 pub fn owner_info(&self) -> &Vec<u8> {
95 &self.owner_info
96 }
97
98 /// Get user id of calling.
user_id(&self) -> i3299 pub fn user_id(&self) -> i32 {
100 self.user_id
101 }
102 }
103