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 16import cert from '@ohos.security.cert'; 17import cryptoFrameWork from '@ohos.security.cryptoFramework'; 18import util from '@ohos.util'; 19import Logger from './Logger'; 20 21const TAG: string = '[CertFramework]'; 22 23let CERT_DATA = new Uint8Array([ 24 0x30, 0x82, 0x01, 0xb6, 0x30, 0x82, 0x01, 0x1f, 0x02, 0x14, 0x53, 0x19, 0x8b, 0x14, 0x84, 0xb7, 25 0xab, 0xca, 0xe2, 0x13, 0x05, 0x16, 0xcb, 0xd6, 0x92, 0x4f, 0x9d, 0x34, 0xa6, 0x76, 0x30, 0x0d, 26 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x1a, 0x31, 27 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 28 0x65, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x33, 0x30, 29 0x31, 0x31, 0x37, 0x31, 0x33, 0x32, 0x35, 0x35, 0x33, 0x5a, 0x17, 0x0d, 0x32, 0x35, 0x31, 0x30, 30 0x31, 0x32, 0x31, 0x33, 0x32, 0x35, 0x35, 0x33, 0x5a, 0x30, 0x1a, 0x31, 0x18, 0x30, 0x16, 0x06, 31 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x52, 0x6f, 32 0x6f, 0x74, 0x20, 0x43, 0x41, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 33 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 34 0x81, 0x00, 0xe3, 0x65, 0x54, 0x13, 0xc6, 0x07, 0x31, 0x1d, 0x7e, 0xbc, 0xfc, 0x2c, 0x3d, 0xcd, 35 0x54, 0x7f, 0x28, 0x05, 0xaa, 0xd6, 0xc1, 0xd0, 0x71, 0xc9, 0x33, 0x46, 0x07, 0x80, 0x6e, 0x5d, 36 0xae, 0x9a, 0xcd, 0x29, 0x6b, 0xfb, 0x9b, 0x3a, 0x92, 0xee, 0x2f, 0x96, 0x29, 0x26, 0x12, 0x95, 37 0x84, 0x1b, 0x5a, 0x33, 0x8a, 0x28, 0x3c, 0xa3, 0x25, 0xe7, 0x14, 0x00, 0x9f, 0xbb, 0xc2, 0x85, 38 0xe6, 0x7f, 0x4d, 0x5c, 0x5e, 0xae, 0x09, 0xb4, 0x9f, 0x8a, 0x23, 0xc0, 0xb4, 0x85, 0xc3, 0x45, 39 0xea, 0x82, 0x60, 0x4e, 0x90, 0xbe, 0x6c, 0x0a, 0x0b, 0xb3, 0xfb, 0x04, 0x0d, 0xcc, 0x1d, 0xea, 40 0x07, 0x98, 0xa3, 0xf6, 0xdd, 0x4c, 0x16, 0x36, 0x68, 0xf9, 0x10, 0xa4, 0xf3, 0xd1, 0x5f, 0x0c, 41 0x59, 0x7b, 0xe6, 0x2e, 0x94, 0xe2, 0x70, 0xbf, 0x23, 0x9d, 0xda, 0x59, 0x05, 0x04, 0xe0, 0x8e, 42 0x51, 0x81, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 43 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0xb0, 0x3d, 0xe1, 0xd7, 0x71, 0x9a, 44 0x78, 0x85, 0x6e, 0xeb, 0xd5, 0xa7, 0xdf, 0x54, 0x57, 0x55, 0x3a, 0xb9, 0x6c, 0x3a, 0x3a, 0x2c, 45 0x03, 0x94, 0x6b, 0x14, 0xc2, 0x55, 0xaa, 0xbb, 0x8f, 0xc8, 0x02, 0x7a, 0x2a, 0x92, 0x5c, 0xca, 46 0xf0, 0x57, 0x7b, 0x7a, 0x8b, 0x78, 0xc6, 0x3b, 0x7a, 0x95, 0x27, 0x13, 0x83, 0xb5, 0x5a, 0x84, 47 0x55, 0x8d, 0xd8, 0x09, 0x0f, 0x9e, 0x88, 0x69, 0x64, 0x26, 0x5f, 0x46, 0x77, 0xff, 0x67, 0xaa, 48 0x71, 0xab, 0x6a, 0x9a, 0x01, 0x16, 0xc0, 0x84, 0x13, 0x90, 0xce, 0x07, 0x30, 0xc5, 0x34, 0x3d, 49 0x86, 0x8d, 0xe7, 0x3f, 0xc9, 0x2e, 0x69, 0xac, 0x83, 0x8f, 0xf0, 0x1d, 0x5a, 0x9f, 0x0e, 0x64, 50 0x14, 0xd9, 0xb1, 0xa6, 0x19, 0xcf, 0xcc, 0x92, 0x08, 0xbd, 0x94, 0x18, 0xd2, 0x83, 0x6c, 0x13, 51 0xb9, 0x45, 0xd8, 0x03, 0x58, 0x2d, 0xf1, 0xd6, 0xb9, 0x66, 52]); 53 54let SIGNATURE_TEXT = new Uint8Array([ 55 0x77, 0xb9, 0xa2, 0xfd, 0x57, 0x97, 0x66, 0xa2, 0x92, 0x73, 0x67, 0x72, 0x9d, 0x38, 0xa4, 0x72, 56 0x76, 0x02, 0x54, 0x6e, 0xeb, 0x83, 0xfd, 0x1d, 0x0c, 0xf1, 0xd7, 0x55, 0x06, 0x69, 0x51, 0xb1, 57 0x2f, 0xfb, 0x03, 0x60, 0x68, 0x50, 0x9b, 0x20, 0xef, 0xd4, 0xe9, 0xa2, 0xe8, 0x12, 0xb8, 0x55, 58 0x59, 0x8c, 0xfe, 0xfb, 0x00, 0x1d, 0x35, 0x80, 0x0f, 0xa7, 0x1c, 0xea, 0xde, 0xe2, 0x68, 0xe6, 59 0x12, 0xd3, 0x3e, 0xcb, 0xe8, 0x29, 0x99, 0xe5, 0x64, 0x2d, 0x99, 0xf5, 0x1b, 0x48, 0xf6, 0x0d, 60 0x89, 0x48, 0xeb, 0xe6, 0x12, 0x0a, 0x8d, 0x2d, 0x48, 0xe8, 0xd6, 0x52, 0xb5, 0x5f, 0xc5, 0xe8, 61 0xc1, 0x0c, 0x88, 0x5d, 0xa4, 0x57, 0x10, 0xfc, 0x50, 0x94, 0xbb, 0xb6, 0x2a, 0x17, 0xb8, 0xee, 62 0xa1, 0xda, 0x0d, 0xf0, 0x63, 0x56, 0xf4, 0xc8, 0x91, 0xb2, 0xbd, 0x47, 0xfe, 0xf1, 0x34, 0x5d 63]); 64 65let STAIN_SIGNATURE_TEXT = new Uint8Array([ 66 0x00, 0x00, 0x11, 0x12, 0x57, 0x97, 0x61, 0xa2, 0x92, 0x73, 0x67, 0x72, 0x9d, 0x38, 0xa4, 0x72, 67 0x76, 0x02, 0x54, 0x6e, 0xeb, 0x83, 0x00, 0x1d, 0x0c, 0xf1, 0xd7, 0x55, 0x06, 0x69, 0x51, 0xb1, 68 0x2f, 0xfb, 0x03, 0x60, 0x68, 0x50, 0x9b, 0x20, 0xef, 0xd4, 0xe9, 0x00, 0xe8, 0x12, 0xb0, 0x55, 69 0x00, 0x8c, 0xfe, 0xfb, 0x00, 0x11, 0x35, 0x80, 0x0f, 0x00, 0x00, 0x00, 0xde, 0xe6, 0x68, 0x00, 70 0x00, 0xd3, 0x36, 0xcb, 0xe8, 0x29, 0x99, 0xe5, 0x64, 0x2d, 0x99, 0xf5, 0x1b, 0x48, 0xf6, 0x0d, 71 0x89, 0x40, 0xeb, 0xe6, 0x12, 0x00, 0x8d, 0x2d, 0x40, 0xe8, 0xd6, 0x52, 0xb5, 0x5f, 0xc5, 0xe8, 72 0xc1, 0x0c, 0x88, 0x5d, 0xa4, 0x57, 0x10, 0xfc, 0x50, 0x94, 0xbb, 0xb6, 0x2a, 0x17, 0xb8, 0xee, 73 0xa1, 0xda, 0x0d, 0xf0, 0x63, 0x56, 0xf4, 0xc8, 0x91, 0xb2, 0x00, 0x47, 0x11, 0xf1, 0x44, 0x00 74]); 75 76function stringToUint8Array(str: String): Uint8Array { 77 if (str.length === 0) { 78 Logger.error(TAG, 'str length is 0'); 79 return null; 80 } 81 82 let len = str.length; 83 let tempArray = []; 84 for (let i = 0; i < len; ++i) { 85 tempArray.push(str.charCodeAt(i)); 86 } 87 let array = new Uint8Array(tempArray); 88 return array; 89} 90 91/** 92 * 功能模型 93 */ 94export class CertFrameworkModel { 95 // cert data 96 private certEncodingBlob = { 97 data: CERT_DATA, 98 encodingFormat: cert.EncodingFormat.FORMAT_DER 99 }; 100 101 // origin data 102 private originText: String = 'Dear Tom, can we go for a spring outing on Sunday at the Central Garden?'; 103 104 private originData = { data: stringToUint8Array(this.originText) }; 105 106 private stainOriginText: String = 'Dear Tom, can we go for a spring outing together on Monday at the lakeside garden?'; 107 108 // signature data 109 private signature: cryptoFrameWork.DataBlob = { data: SIGNATURE_TEXT }; 110 111 112 async dataDisplay(callback): Promise<void> { 113 this.originData.data = stringToUint8Array(this.originText); 114 this.signature.data = SIGNATURE_TEXT; 115 116 let utilBase = new util.Base64Helper(); 117 let data = { 118 certInfo: utilBase.encodeToStringSync(CERT_DATA), 119 originInfo: this.originText, 120 signatureInfo: utilBase.encodeToStringSync(SIGNATURE_TEXT) 121 }; 122 callback(data); 123 } 124 125 async modifyOriginData(callback): Promise<void> { 126 this.originData.data = stringToUint8Array(this.stainOriginText); 127 this.signature.data = SIGNATURE_TEXT; 128 129 let utilBase = new util.Base64Helper(); 130 let data = { 131 originInfo: this.stainOriginText, 132 signatureInfo: utilBase.encodeToStringSync(SIGNATURE_TEXT) 133 }; 134 callback(data); 135 } 136 137 async modifySignatureData(callback): Promise<void> { 138 this.originData.data = stringToUint8Array(this.originText); 139 this.signature.data = STAIN_SIGNATURE_TEXT; 140 141 let utilBase = new util.Base64Helper(); 142 let data = { 143 originInfo: this.originText, 144 signatureInfo: utilBase.encodeToStringSync(STAIN_SIGNATURE_TEXT) 145 }; 146 callback(data); 147 } 148 149 private async getPubKey(callback): Promise<void> { 150 let certObject: cert.X509Cert; 151 let pubKey: cryptoFrameWork.PubKey; 152 153 // create x509 cert object 154 await cert.createX509Cert(this.certEncodingBlob).then((x509Cert) => { 155 Logger.info(TAG, 'create x509 cert object success.'); 156 certObject = x509Cert; 157 }).catch((err) => { 158 Logger.error(TAG, `create x509 cert object failed, ${JSON.stringify(err.code)}: ${JSON.stringify(err.message)}`); 159 }); 160 161 // get pubKey from cert 162 try { 163 let pubKeyObject: cryptoFrameWork.PubKey = certObject.getPublicKey(); 164 let pubKeyBlob: cryptoFrameWork.DataBlob = pubKeyObject.getEncoded(); 165 let keyGenerator: cryptoFrameWork.AsyKeyGenerator = cryptoFrameWork.createAsyKeyGenerator('RSA1024'); 166 167 await keyGenerator.convertKey(pubKeyBlob, null).then((keyPair) => { 168 Logger.info(TAG, 'get keyPair success.'); 169 pubKey = keyPair.pubKey; 170 callback(pubKey); 171 }).catch((err) => { 172 Logger.error(TAG, `get keyPair failed, ${JSON.stringify(err.code)}: ${JSON.stringify(err.message)}`); 173 }); 174 Logger.info(TAG, 'get pubKey success.'); 175 } catch (err) { 176 Logger.error(TAG, `get pubKey failed, ${JSON.stringify(err)}`); 177 } 178 } 179 180 async verify(callback): Promise<void> { 181 // get pubkey 182 let pubKey: cryptoFrameWork.PubKey; 183 await this.getPubKey((result) => { 184 pubKey = result; 185 }); 186 187 // create verifier 188 let verifier: cryptoFrameWork.Verify; 189 try { 190 verifier = cryptoFrameWork.createVerify('RSA1024|PKCS1|SHA256'); 191 Logger.info(TAG, 'create verifier success.'); 192 } catch (err) { 193 Logger.error(TAG, `create verifier, ${JSON.stringify(err)}`); 194 } 195 196 // verify init 197 await verifier.init(pubKey).then(() => { 198 Logger.info(TAG, 'verify init success.'); 199 }).catch((err) => { 200 Logger.error(TAG, `verify init failed, ${JSON.stringify(err.code)}: ${JSON.stringify(err.message)}`); 201 }); 202 203 // verify operation 204 await verifier.verify(this.originData, this.signature).then((res) => { 205 Logger.info(TAG, 'verify operation success.'); 206 let result = res; 207 callback(result); 208 }).catch((err) => { 209 Logger.error(TAG, `verify operation failed, ${JSON.stringify(err.code)}: ${JSON.stringify(err.message)}`); 210 }); 211 } 212}