• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import { CMResult, BusinessError, CMErrorCode, CMBlob, CMSignatureSpec, CertManagerStore } from './certStubStruct';
17import  certStubUtil  from './certStubUtil';
18import fileio from '@ohos.fileio';
19
20var certUtil = new certStubUtil();
21const TAG = "CertManager Stub: ";
22const certNum = 10
23const keyNum = 20
24const authorListA = ["20010027", "20010026", "10003", "20010013","20010033"]
25const authorListB = ["20010033", "10003", "20010027"]
26const authorListC = ["20010032", "20010013", "20010026", "10003"]
27
28export class CertStubPromise {
29    exceptFlag = 0;
30    uriBase = "uri://stub/certFile/"
31    type = "CM_URI_TYPE_APP_KEY"
32    uriCount = 0;
33    systemCertMap = new Map();
34    userCertMap = new Map();
35    uriToAlias = new Map();
36    appCredMap = new Map();
37    privateCredMap = new Map();
38    uriToAppList = new Map();
39
40    constructor() {
41        let newCert;
42
43        console.log(TAG + "getSystemTrustedCertificateList stub constructor start");
44        newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
45            "SystemRoot_1", true, new Uint8Array([0, 1, 2, 42, 100, 101, 102, 255]));
46        this.systemCertMap.set(this.uriBase + this.uriCount.toString(), newCert);
47        this.uriCount++;
48
49        newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
50            "SystemRoot_2", false, new Uint8Array([64, 1, 2, 42, 100, 123, 321, 255]));
51        this.systemCertMap.set(this.uriBase + this.uriCount.toString(), newCert);
52        this.uriCount++;
53
54        newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
55            "SystemRoot_3", true, new Uint8Array([54, 1, 123, 42, 100, 101, 102, 23]));
56        this.systemCertMap.set(this.uriBase + this.uriCount.toString(), newCert);
57        this.uriCount++;
58
59        newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
60            "UserRoot_1", true, new Uint8Array([0, 1, 2, 42, 100, 101, 102, 255]));
61        this.userCertMap.set("UserRoot_1", newCert);
62        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "UserRoot_1")
63        this.uriCount++;
64
65        newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
66            "UserRoot_2", false, new Uint8Array([0, 1, 2, 42, 100, 101, 102, 255]));
67        this.userCertMap.set("UserRoot_2", newCert);
68        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "UserRoot_2")
69        this.uriCount++;
70
71        newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
72            "UserRoot_3", true, new Uint8Array([0, 1, 2, 42, 100, 101, 102, 255]));
73        this.userCertMap.set("UserRoot_3", newCert);
74        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "UserRoot_3")
75        this.uriCount++;
76
77        newCert = certUtil.parseCredential(this.type, "TheEncryptionForCCMPV1", this.uriBase + this.uriCount.toString(), certNum, keyNum,
78            new Uint8Array([25, 5, 34, 45, 64, 78, 7, 8]));
79        this.appCredMap.set("ApptemRoot_1", newCert);
80        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "ApptemRoot_1")
81        this.uriToAppList.set(this.uriBase + this.uriCount.toString(), authorListA)
82        this.uriCount++;
83
84        newCert = certUtil.parseCredential(this.type, "TheEncryptionForCCMPV2", this.uriBase + this.uriCount.toString(), certNum, keyNum,
85            new Uint8Array([23, 1, 43, 42, 78, 90, 56, 12]));
86        this.appCredMap.set("ApptemRoot_2", newCert);
87        this.uriToAppList.set(this.uriBase + this.uriCount.toString(), authorListB)
88        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "ApptemRoot_2")
89        this.uriCount++;
90
91        newCert = certUtil.parseCredential(this.type, "TheEncryptionForCCMPV3", this.uriBase + this.uriCount.toString(), certNum, keyNum,
92            new Uint8Array([21, 1, 32, 645, 100, 101, 102, 23]));
93        this.appCredMap.set("ApptemRoot_3", newCert);
94        this.uriToAppList.set(this.uriBase + this.uriCount.toString(), authorListC)
95        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "ApptemRoot_3")
96        this.uriCount++;
97
98        newCert = certUtil.parseCredential(this.type, "Employee_WPA_EAPIEA", this.uriBase + this.uriCount.toString(), certNum, keyNum,
99            new Uint8Array([32, 41, 54, 67, 77, 98, 91, 123]));
100        this.privateCredMap.set("PrivatetemRoot_1", newCert);
101        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "PrivatetemRoot_1")
102        this.uriCount++;
103
104        newCert = certUtil.parseCredential(this.type, "Employee_WPA_EAPIEB", this.uriBase + this.uriCount.toString(), certNum, keyNum,
105            new Uint8Array([34, 65, 67, 68, 33, 77, 14, 18]));
106        this.privateCredMap.set("PrivatetemRoot_2", newCert);
107        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "PrivatetemRoot_2")
108        this.uriCount++;
109
110        newCert = certUtil.parseCredential(this.type, "Employee_WPA_EAPIEC", this.uriBase + this.uriCount.toString(), certNum, keyNum,
111            new Uint8Array([12, 3, 54, 76, 8, 123, 43, 55]));
112        this.privateCredMap.set("PrivatetemRoot_3", newCert);
113        this.uriToAlias.set(this.uriBase + this.uriCount.toString(), "PrivatetemRoot_3")
114        this.uriCount++;
115    }
116
117    getSystemTrustedCertificateList() {
118        return new Promise<CMResult>((resolve, reject) => {
119            let error: BusinessError;
120            let data: CMResult;
121            let certList = new Array();
122
123            if (!this.exceptFlag) {
124                console.log(TAG + "getSystemTrustedCertificateList stub start");
125                this.systemCertMap.forEach((data, keyUri) => {
126                    certList.push(this.systemCertMap.get(keyUri))
127                })
128                data = {
129                    certList: certList
130                };
131                resolve(data);
132            } else {
133                error = {
134                    code: CMErrorCode.CMR_FAILURE,
135                };
136                reject(error);
137            }
138        })
139    }
140
141    getSystemTrustedCertificate(certUri: string) {
142        return new Promise<CMResult>((resolve, reject) => {
143            let data: CMResult;
144            let error: BusinessError;
145
146            if (this.systemCertMap.has(certUri)) {
147                data = {
148                    certInfo: this.systemCertMap.get(certUri)
149                };
150                resolve(data);
151            } else {
152                error = {
153                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
154                };
155                reject(error);
156            }
157        })
158    }
159
160    setCertificateStatus(certUri: string, store: number, status: boolean) {
161        return new Promise<void>((resolve, reject) => {
162            let error: BusinessError;
163            let alias;
164
165            if ((!this.systemCertMap.has(certUri)) &&
166            (!this.uriToAlias.has(certUri))) {
167                error = {
168                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
169                };
170                reject(error);
171            }
172
173            if (store == CertManagerStore.CERT_MANAGER_SYSTEM_TRUSTED_STORE) {
174                this.systemCertMap.get(certUri).status = status;
175                resolve();
176            } else if (store == CertManagerStore.CERT_MANAGER_USER_TRUSTED_STORE) {
177                alias = this.uriToAlias.get(certUri);
178                this.userCertMap.get(alias).status = status;
179                resolve();
180            } else {
181                error = {
182                    code: CMErrorCode.CMR_ERROR_NOT_SUPPORTED,
183                };
184                reject(error);
185            }
186        })
187    }
188
189    installUserTrustedCertificate(certificate: CMBlob) {
190        return new Promise<void>((resolve, reject) => {
191            let newCert = certUtil.parseCertInfo(this.uriBase + this.uriCount.toString(),
192                certificate.alias, true, certificate.inData);
193            this.userCertMap.set(certificate.alias, newCert);
194            this.uriToAlias.set(this.uriBase + this.uriCount.toString(), certificate.alias)
195            this.uriCount++;
196
197            resolve();
198        })
199    }
200
201    uninstallAllUserTrustedCertificate() {
202        return new Promise<void>((resolve, reject) => {
203            console.log(TAG + "uninstallAllUserTrustedCertificate stub in");
204            this.userCertMap.clear();
205            this.uriToAlias.clear();
206            resolve();
207        })
208    }
209
210    uninstallUserTrustedCertificate(certUri: string) {
211        return new Promise<void>((resolve, reject) => {
212            let error: BusinessError;
213            let alias;
214
215            if (!this.uriToAlias.has(certUri)) {
216                error = {
217                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
218                };
219                reject(error);
220            }
221
222            alias = this.uriToAlias.get(certUri);
223            this.uriToAlias.delete(certUri);
224            this.userCertMap.delete(alias);
225            resolve();
226        })
227    }
228
229    getUserTrustedCertificateList() {
230        return new Promise<CMResult>((resolve, reject) => {
231            let error: BusinessError;
232            let data: CMResult;
233            let certList = new Array();
234
235            if (!this.exceptFlag) {
236                this.userCertMap.forEach((data, keyAlias) => {
237                    certList.push(this.userCertMap.get(keyAlias))
238                })
239
240                data = {
241                    certList: certList
242                };
243                resolve(data);
244            } else {
245                error = {
246                    code: CMErrorCode.CMR_FAILURE,
247                };
248                reject(error);
249            }
250        })
251    }
252
253    getUserTrustedCertificate(certUri: string) {
254        return new Promise<CMResult>((resolve, reject) => {
255            let data: CMResult;
256            let error: BusinessError;
257
258            if (this.uriToAlias.has(certUri)) {
259                let alias = this.uriToAlias.get(certUri);
260                data = {
261                    certInfo: this.userCertMap.get(alias)
262                };
263                resolve(data);
264            } else {
265                error = {
266                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
267                };
268                reject(error);
269            }
270        })
271    }
272
273    installAppCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) {
274        return new Promise<void>((resolve, reject) => {
275            let newCred = certUtil.parseCredential(this.type, certAlias, this.uriBase + this.uriCount.toString(),
276                certNum, keyNum, keystore);
277            this.appCredMap.set(certAlias, newCred);
278            this.uriToAlias.set(this.uriBase + this.uriCount.toString(), certAlias)
279            this.uriCount++;
280
281            resolve();
282        })
283    }
284
285    installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) {
286        return new Promise<void>((resolve, reject) => {
287            let newCred = certUtil.parseCredential("privateCred", certAlias, this.uriBase + this.uriCount.toString(),
288                1, 1, keystore);
289            this.privateCredMap.set(certAlias, newCred);
290            this.uriToAlias.set(this.uriBase + this.uriCount.toString(), certAlias);
291            this.uriCount++;
292
293            resolve();
294        })
295    }
296
297    uninstallAllAppCertificate() {
298        return new Promise<void>((resolve, reject) => {
299            console.log(TAG + "uninstallAllAppCertificate stub in");
300            this.appCredMap.clear();
301            this.privateCredMap.clear();
302
303            resolve();
304        })
305    }
306
307    uninstallAppCertificate(keyUri: string) {
308        return new Promise<void>((resolve, reject) => {
309            let alias;
310            let error: BusinessError;
311
312            if (!this.uriToAlias.has(keyUri)) {
313                error = {
314                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
315                };
316                reject(error);
317            }
318
319            alias = this.uriToAlias.get(keyUri);
320            this.uriToAlias.delete(keyUri);
321            this.appCredMap.delete(alias);
322
323            resolve();
324        })
325    }
326
327    uninstallPrivateCertificate(keyUri) {
328        return new Promise<void>((resolve, reject) => {
329            let alias;
330            let error: BusinessError;
331
332            if (!this.uriToAlias.has(keyUri)) {
333                error = {
334                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
335                };
336                reject(error);
337            }
338
339            alias = this.uriToAlias.get(keyUri);
340            this.uriToAlias.delete(keyUri);
341            this.privateCredMap.delete(alias);
342
343            resolve();
344        })
345    }
346
347    getAppCertificateList() {
348        return new Promise<CMResult>((resolve, reject) => {
349            let error: BusinessError;
350            let data: CMResult;
351            let credList = new Array();
352
353            if (!this.exceptFlag) {
354                this.appCredMap.forEach((data, keyAlias) => {
355                    credList.push(this.appCredMap.get(keyAlias))
356                })
357
358                data = {
359                    credentialList: credList
360                };
361                resolve(data);
362            } else {
363                error = {
364                    code: CMErrorCode.CMR_FAILURE,
365                };
366                reject(error);
367            }
368        })
369    }
370
371    getPrivateCertificateList() {
372        return new Promise<CMResult>((resolve, reject) => {
373            let error: BusinessError;
374            let data: CMResult;
375            let credList = new Array();
376
377            if (!this.exceptFlag) {
378                this.privateCredMap.forEach((data, keyAlias) => {
379                    credList.push(this.privateCredMap.get(keyAlias))
380                })
381
382                data = {
383                    credentialList: credList
384                };
385                resolve(data);
386            } else {
387                error = {
388                    code: CMErrorCode.CMR_FAILURE,
389                };
390                reject(error);
391            }
392        })
393    }
394
395    getAppCertificate(keyUri: string) {
396        return new Promise<CMResult>((resolve, reject) => {
397            let data: CMResult;
398            let error: BusinessError;
399
400            if (this.uriToAlias.has(keyUri)) {
401                let alias = this.uriToAlias.get(keyUri);
402                data = {
403                    credential: this.appCredMap.get(alias)
404                };
405                resolve(data);
406            } else {
407                error = {
408                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
409                };
410                reject(error);
411            }
412        })
413    }
414
415    getPrivateCertificate(keyUri: string) {
416        return new Promise<CMResult>((resolve, reject) => {
417            let data: CMResult;
418            let error: BusinessError;
419
420            if (this.uriToAlias.has(keyUri)) {
421                let alias = this.uriToAlias.get(keyUri);
422                data = {
423                    credential: this.privateCredMap.get(alias)
424                };
425                resolve(data);
426            } else {
427                error = {
428                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
429                };
430                reject(error);
431            }
432        })
433    }
434
435    grantAppCertificate(keyUri: string, clientAppUid: string) {
436        return new Promise<CMResult>((resolve, reject) => {
437            let appList: Array<string>;
438            let data: CMResult;
439            let error: BusinessError;
440
441            console.log(TAG + "grantAppCertificate stub uri: " + keyUri + " clientAppUid: " + clientAppUid);
442            if (!this.uriToAlias.has(keyUri)) {
443                error = {
444                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
445                };
446                reject(error);
447            }
448
449            if (this.uriToAppList.has(keyUri)) {
450                appList = this.uriToAppList.get(keyUri);
451            } else {
452                appList = new Array();
453            }
454
455            appList.push(clientAppUid);
456            this.uriToAppList.set(keyUri, appList)
457            data = {
458                authUri: "authUri-value"
459            };
460            console.log(TAG + "grantAppCertificate stub uriToAppList: " + JSON.stringify(appList));
461
462            resolve(data);
463        })
464    }
465
466    isAuthorizedApp(keyUri: string) {
467        return new Promise<CMResult>((resolve, reject) => {
468            let data: CMResult;
469            data = {
470                isAuth: false
471            };
472            resolve(data);
473        })
474    }
475
476    getAuthorizedAppList(keyUri: string) {
477        return new Promise<CMResult>((resolve, reject) => {
478            let data: CMResult;
479            let appList: Array<string>;
480
481            if ((!this.uriToAlias.has(keyUri)) || (!this.uriToAppList.has(keyUri))) {
482                data = {
483                    appUidList: appList,
484                };
485                resolve(data);
486            }
487
488            appList = this.uriToAppList.get(keyUri);
489
490            data = {
491                appUidList: appList
492            };
493            resolve(data);
494        })
495    }
496
497    removeGrantedAppCertificate(keyUri: string, clientAppUid: string) {
498        return new Promise<void>((resolve, reject) => {
499            let appList: Array<string>;
500
501            if ((!this.uriToAlias.has(keyUri)) || (!this.uriToAppList.has(keyUri))) {
502                let error = {
503                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
504                };
505                reject(error);
506            }
507
508            appList = this.uriToAppList.get(keyUri);
509            if (appList.indexOf(clientAppUid) != -1) {
510                appList = appList.filter(item => item != clientAppUid);
511                this.uriToAppList.set(keyUri, appList);
512                resolve();
513            } else {
514                let error = {
515                    code: CMErrorCode.CMR_ERROR_NOT_EXIST,
516                };
517                reject(error);
518            }
519        })
520    }
521
522    init(authUri: string, spec: CMSignatureSpec) {
523        return new Promise<void>((resolve, reject) => {
524            let errorCode = CMErrorCode.CMR_SUCCESS;
525            let data: CMResult;
526
527            resolve();
528        })
529    }
530
531    update(handle: number, data: Uint8Array, token?: Uint8Array) {
532        return new Promise<void>((resolve, reject) => {
533            let errorCode = CMErrorCode.CMR_SUCCESS;
534            let data: CMResult;
535
536            resolve();
537        })
538    }
539
540    finish(handle: number, signature?: Uint8Array) {
541        return new Promise<void>((resolve, reject) => {
542            let errorCode = CMErrorCode.CMR_SUCCESS;
543            let data: CMResult;
544
545            resolve();
546        })
547    }
548
549    abort(handle: number) {
550        return new Promise<void>((resolve, reject) => {
551            let errorCode = CMErrorCode.CMR_SUCCESS;
552            let data: CMResult;
553
554            resolve();
555        })
556    }
557
558    mapToJson(map) {
559        let obj = Object.create(null);
560        for (let [k, v] of map) {
561            obj[k] = v;
562        }
563
564        return JSON.stringify(obj)
565    }
566
567    jsonToMap(jsonString) {
568        console.log(TAG + "jsonToMap start");
569        let map = new Map();
570        let obj = JSON.parse(jsonString);
571
572        for (let k of Object.keys(obj)) {
573            map.set(k, obj[k])
574        }
575        return map;
576    }
577
578    saveAllMaps() {
579        console.log(TAG + "saveAllMaps start");
580        let dataList = [
581            this.mapToJson(this.systemCertMap),
582            this.mapToJson(this.userCertMap),
583            this.mapToJson(this.uriToAlias),
584            this.mapToJson(this.appCredMap),
585            this.mapToJson(this.privateCredMap),
586            this.mapToJson(this.uriToAppList)
587        ];
588
589        let context = globalThis.certManagerAbilityContext;
590        let path = context.cacheDir;
591        path += '/certStubData'
592        try {
593            fileio.unlinkSync(path);
594        } catch (err) {
595            console.log(TAG + "saveAllMaps unlinkSync err: " + err);
596        }
597
598        try {
599            let fd = fileio.openSync(path, 0o102, 0o600);
600            fileio.writeSync(fd, JSON.stringify(dataList));
601            fileio.closeSync(fd);
602        } catch (err) {
603            console.log(TAG + "saveAllMaps File operation fail, err: " + err);
604            return;
605        }
606
607        console.log(TAG + "saveAllMaps success");
608    }
609
610    uint8ArrayToString(fileData) {
611        var dataString = "";
612        for (var i = 0; i < fileData.length; i++) {
613            dataString += String.fromCharCode(fileData[i]);
614        }
615        return dataString
616    }
617
618    restoreMapsFromJson(json) {
619        console.log(TAG + "restoreMapsFromJson start Json: " + json);
620        let dataList = JSON.parse(json);
621        this.systemCertMap = this.jsonToMap(dataList[0]);
622        this.userCertMap = this.jsonToMap(dataList[1]);
623        this.uriToAlias = this.jsonToMap(dataList[2]);
624        this.appCredMap = this.jsonToMap(dataList[3]);
625        this.privateCredMap = this.jsonToMap(dataList[4]);
626        this.uriToAppList = this.jsonToMap(dataList[5]);
627        console.log(TAG + "restoreMapsFromJson end");
628    }
629
630    restoreAllMaps() {
631        console.log(TAG + "getAllMaps start");
632        let context = globalThis.certManagerAbilityContext;
633        let path = context.cacheDir;
634        path += '/certStubData'
635
636        try {
637            let fd = fileio.openSync(path, 0o002, 0o666);
638            console.log(TAG + "getAllMaps fd: " + fd);
639            let stat = fileio.fstatSync(fd);
640            let size = stat.size;
641            let buf = new ArrayBuffer(size);
642
643            fileio.read(fd, buf, (err, data) => {
644                if (data) {
645                    this.restoreMapsFromJson(this.uint8ArrayToString(new Uint8Array(data.buffer)));
646                }
647                fileio.closeSync(fd);
648            });
649        } catch(err) {
650            console.log(TAG + "getAllMaps err: " + err);
651        }
652
653        console.log(TAG + "getAllMaps end");
654    }
655}
656
657let certStub = new CertStubPromise();
658
659export default certStub as CertStubPromise;