1/*
2* Copyright (c) 2022 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
16export interface AsyncCallback<T, E = void> {
17    (err: BusinessError<E>, data: T): void;
18}
19
20export interface BusinessError<T = void> extends Error {
21    code: number;
22    data?: T;
23}
24
25declare namespace CertManagerFunc {
26    function getSystemTrustedCertificateList(callback: AsyncCallback<CMResult>) : void;
27    function getSystemTrustedCertificateList() : Promise<CMResult>;
28
29    function getSystemTrustedCertificate(certUri: string, callback: AsyncCallback<CMResult>) : void;
30    function getSystemTrustedCertificate(certUri: string) : Promise<CMResult>;
31
32    function setCertificateStatus(certUri: string, store: number, status: boolean, callback: AsyncCallback<boolean>) : void;
33    function setCertificateStatus(certUri: string, store: number, status: boolean) : Promise<boolean>;
34
35    function installUserTrustedCertificate(certificate: CertBlob, callback: AsyncCallback<CMResult>) : void;
36    function installUserTrustedCertificate(certificate: CertBlob,) : Promise<CMResult>;
37
38    function uninstallAllUserTrustedCertificate(callback: AsyncCallback<boolean>) : void;
39    function uninstallAllUserTrustedCertificate() : Promise<boolean>;
40
41    function uninstallUserTrustedCertificate(certUri: string, callback: AsyncCallback<boolean>) : void;
42    function uninstallUserTrustedCertificate(certUri: string) : Promise<boolean>;
43
44    function getAllUserTrustedCertificates(callback: AsyncCallback<CMResult>) : void;
45    function getAllUserTrustedCertificates() : Promise<CMResult>;
46
47    function getUserTrustedCertificate(certUri: string, callback: AsyncCallback<CMResult>) : void;
48    function getUserTrustedCertificate(certUri: string) : Promise<CMResult>;
49
50    function installPublicCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, callback: AsyncCallback<CMResult>) : void;
51    function installPublicCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) : Promise<CMResult>;
52
53    function installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, callback: AsyncCallback<CMResult>) : void;
54    function installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) : Promise<CMResult>;
55
56    function generatePrivateCertificate(keyAlias: string, keyProperties: CMKeyProperties, callback: AsyncCallback<CMResult>) : void;
57    function generatePrivateCertificate(keyAlias: string, keyProperties: CMKeyProperties) : Promise<CMResult>;
58
59    function updatePrivateCertificate(type: string, keyUri: string, certificate: CertBlob, callback: AsyncCallback<boolean>) : void;
60    function updatePrivateCertificate(type: string, keyUri: string, certificate: CertBlob) : Promise<boolean>;
61
62    function uninstallAllAppCertificate(callback: AsyncCallback<boolean>) : void;
63    function uninstallAllAppCertificate() : Promise<boolean>;
64
65    function uninstallPublicCertificate(keyUri: string, callback: AsyncCallback<boolean>) : void;
66    function uninstallPublicCertificate(keyUri: string) : Promise<boolean>;
67
68    function uninstallPrivateCertificate(keyUri: string, callback: AsyncCallback<boolean>) : void;
69    function uninstallPrivateCertificate(keyUri: string) : Promise<boolean>;
70
71    function getAllPublicCertificates(callback: AsyncCallback<CMResult>) : void;
72    function getAllPublicCertificates() : Promise<CMResult>;
73
74    function getAllAppPrivateCertificates(callback: AsyncCallback<CMResult>) : void;
75    function getAllAppPrivateCertificates() : Promise<CMResult>;
76
77    function getPublicCertificate(keyUri: string, callback: AsyncCallback<CMResult>) : void;
78    function getPublicCertificate(keyUri: string, ) : Promise<CMResult>;
79
80    function getPrivateCertificate(keyUri: string, callback: AsyncCallback<CMResult>) : void;
81    function getPrivateCertificate(keyUri: string) : Promise<CMResult>;
82
83    function grantPublicCertificate(keyUri: string, clientAppUid: string, callback: AsyncCallback<CMResult>) : void;
84    function grantPublicCertificate(keyUri: string, clientAppUid: string) : Promise<CMResult>;
85
86    function isAuthorizedApp(keyUri: string, callback: AsyncCallback<boolean>) : void;
87    function isAuthorizedApp(keyUri: string) : Promise<boolean>;
88
89    function getAuthorizedAppList(keyUri: string, callback: AsyncCallback<CMResult>) : void;
90    function getAuthorizedAppList(keyUri: string) : Promise<CMResult>;
91
92    function removeGrantedPublicCertificate(keyUri: string, clientAppUid: string, callback: AsyncCallback<boolean>) : void;
93    function removeGrantedPublicCertificate(keyUri: string, clientAppUid: string) : Promise<boolean>;
94
95    function init(authUri: string, spec: CMSignatureSpec, callback: AsyncCallback<CMHandle>) : void;
96    function init(authUri: string, spec: CMSignatureSpec) : Promise<CMHandle>;
97
98    function update(handle: Uint8Array, data: Uint8Array, callback: AsyncCallback<boolean>) : void;
99    function update(handle: Uint8Array, data: Uint8Array) : Promise<boolean>;
100
101    function finish(handle: Uint8Array, callback: AsyncCallback<CMResult>) : void;
102    function finish(handle: Uint8Array, signature: Uint8Array, callback: AsyncCallback<CMResult>) : void;
103    function finish(handle: Uint8Array, signature?: Uint8Array) : Promise<CMResult>;
104
105    function abort(handle: Uint8Array, callback: AsyncCallback<boolean>) : void;
106    function abort(handle: Uint8Array) : Promise<boolean>;
107
108    function installSystemAppCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string): Promise<CMResult>;
109
110    function getAllSystemAppCertificates(): Promise<CMResult>;
111
112    function getSystemAppCertificate(keyUri: string) : Promise<CMResult>;
113
114    function uninstallSystemAppCertificate(keyUri: string) : Promise<void>;
115
116    export interface CertInfo {
117        uri: string;
118        certAlias: string;
119        status: boolean;
120        issuerName: string;
121        subjectName: string;
122        serial: string;
123        notBefore: string;
124        notAfter: string;
125        fingerprintSha256: string;
126        cert: Uint8Array;
127    }
128
129    export interface CertAbstract {
130        uri: string;
131        certAlias: string;
132        status: boolean;
133        subjectName: string;
134    }
135
136    export interface Credential {
137        type: string;
138        alias: string;
139        keyUri: string;
140        certNum: number;
141        keyNum: number;
142        credData:Uint8Array;
143    }
144
145    export interface CredentialAbstract {
146        type: string;
147        alias: string;
148        keyUri: string;
149    }
150
151    export interface CertBlob {
152        inData: Uint8Array;
153        alias: string;
154    }
155
156    export interface CMResult {
157        certList?: Array<CertAbstract>;
158        certInfo?: CertInfo;
159        credentialList?: Array<CredentialAbstract>;
160        credential?: Credential;
161        appUidList?: Array<string>;
162        uri?: string;
163        outData?: Uint8Array;
164        isAuth?: boolean;
165    }
166
167    export interface CMKeyProperties {
168        type: string;
169        alg: string;
170        size: number;
171        padding: string;
172        purpose: string;
173        digest: string;
174        authType: string;
175        authTimeout: string;
176    }
177
178    export enum CmKeyPurpose {
179        CM_KEY_PURPOSE_SIGN = 4,
180        CM_KEY_PURPOSE_VERIFY = 8,
181    }
182
183    export interface CMSignatureSpec {
184        purpose: CmKeyPurpose;
185    }
186
187    export interface CMHandle {
188        handle: Uint8Array;
189    }
190
191    export enum CMErrorCode {
192        CM_SUCCESS = 0,
193        CM_ERROR_GENERIC = 17500001,
194        CM_ERROR_NO_FOUND = 17500002,
195        CM_ERROR_INCORRECT_FORMAT = 17500003,
196        CM_ERROR_MAX_CERT_COUNT_REACHED = 17500004,
197        CM_ERROR_NO_AUTHORIZATION = 17500005,
198        CM_ERROR_ALIAS_LENGTH_REACHED_LIMIT = 17500006,
199        CM_ERROR_PASSWORD_IS_ERR = 17500008
200    }
201}
202
203export default CertManagerFunc;
204