1# Development on Device Security Level Management 2 3## Overview 4 5### DSLM 6 7The OpenHarmony distributed technology can converge resources from different devices to form a Super Device. Poor security capabilities of any device may threaten the security of the Super Device. 8 9The Device Security Level Management (DSLM) module is introduced to manage the security levels of OpenHarmony devices. When different types of user data are hopped or processed in OpenHarmony distributed services, the DSLM APIs can be called to obtain the security levels of related devices for subsequent processing. 10 11### Basic Concepts 12 13- Device security level 14 15 The security level of an OpenHarmony device depends on the system security capabilities of the device. The OpenHarmony system security capabilities are based on the root of trust (RoT) for boot, RoT for storage, and RoT for compute on the hardware. Security technologies and capabilities focus on device integrity protection, data confidentiality protection, and vulnerability defense. 16 17 The following figure shows the OpenHarmony security architecture. 18 19  20 21 The above figure shows the typical security architecture for a single device. The architecture may vary depending on the risk level as well as the software and hardware resources of the device. The security capabilities of OpenHarmony devices are classified into five levels from SL1 to SL5, based on an industry standard security classification model and actual OpenHarmony service scenarios and device types. In the OpenHarmony ecosystem, higher security levels include all the capabilities of lower security levels by default. The figure below shows the security levels of OpenHarmony devices. 22 23  24 25 - SL1: SL1 is the lowest security level of OpenHarmony devices. Usually equipped with a lightweight operating system and low-end microprocessors, such devices implement simple services and do not need to process sensitive data. SL1 devices are required to support software integrity protection and eliminate common errors. Devices that cannot meet the requirements of SL1 can only be controlled by OpenHarmony devices. They cannot control OpenHarmony devices for more complex service collaboration. 26 27 - SL2: OpenHarmony devices of SL2 can label their own data and define access control rules to implement discretionary access control (DAC). These devices must have basic anti-penetration capabilities. Devices of this level support a lightweight, secure, and isolated environment for deploying a small number of necessary security services. 28 29 - SL3: OpenHarmony devices of SL3 have comprehensive security protection capabilities, and their operating systems have relatively complete security semantics and support mandatory access control (MAC). The system data can be structured as critical elements and non-critical elements. The critical elements are protected by a well-defined security policy model. Devices of this level must have certain anti-penetration capabilities to defend against common vulnerability exploits. 30 31 - SL4: OpenHarmony devices of SL4 must have simplified trusted computing base (TCB) and come with anti-tampering capabilities. The implementation of SL4 should be concise and secure enough. Adequate authentication and arbitration are required for any access to critical elements. Devices of this level have considerable anti-penetration capabilities and can defend against most software attacks. 32 33 - SL5: SL5 indicates the highest security protection capabilities for OpenHarmony devices. The system core software modules must have passed formal verification. Key hardware modules, such as the RoT and cryptographic computing engine, must be able to defend against physical attacks and attacks simulated in labs. Devices at this level must have high-security units, such as dedicated security chips, to enhance the boot, storage, and running of the root of trust (RoT). 34 35- DSLM 36 37 DSLM is a module to manage the security levels of OpenHarmony devices. It verifies and updates device security level information for OpenHarmony devices in collaboration. It also provides an interface for querying the security level of each device. 38 39### Working Principles 40 41The security level of each device in a Super Device provides the decision-making criteria for processing or hopping various user data. For example, the distributed file storage service does not allow sensitive data to be stored on devices with security level lower than SL3. 42 43### Constraints 44 45The default security level of OpenHarmony devices is SL1. Device manufacturers can customize a higher security level based on service requirements. For details, see [Customizing Device Security Levels](#customizing-device-security-levels). 46 47## Development Guidelines 48 49### When to Use 50 51When processing or hopping various user data, a subsystem can invoke the APIs provided by the DSLM module to obtain the security level information of related devices. Then, the subsystems determine the subsequent processing based on the security level and data to be processed. 52 53### Available APIs 54 55All the APIs are native C interfaces for implementing underlying capabilities and are not open to apps. The APIs are described as follows: 56| API| Description| 57| :----------------------------------------------------------- | :------------------------------------------- | 58| int32_t RequestDeviceSecurityInfo(const DeviceIdentify \*identify, const RequestOption \*option, DeviceSecurityInfo \*\*info); | Requests the security level information of a device synchronously.| 59| int32_t RequestDeviceSecurityInfoAsync(const DeviceIdentify \*identify, const RequestOption \*option, DeviceSecurityInfoCallback callback); | Requests the security level information of a device asynchronously.| 60| void FreeDeviceSecurityInfo(DeviceSecurityInfo \*info); | Releases the device security level information.| 61| int32\_t GetDeviceSecurityLevelValue(const DeviceSecurityInfo \*info, int32_t \*level); | Obtains the device security level from the security level information.| 62 63### How to Develop 64 651. Add the dependencies for compilation. 66 67 ```undefined 68 external_deps += [ "device_security_level:dslm_sdk" ] 69 ``` 70 712. Add the header files of dependencies. 72 73 ```cpp 74 #include "device_security_defines.h" // Header file for defining critical data structures. 75 #include "device_security_info.h" // Header file for defining APIs. 76 ``` 77 783. Call APIs. 79 80 ```cpp 81 // Obtain the unique device identifier (UDID) of the device of which the security level is to be queried. 82 const DeviceIdentify *device = GetDestDeviceUdid(); 83 84 // Obtain the RequestOption. 85 const RequestOption *option = DEFAULT_OPTION; 86 87 // Define a pointer to the device security level obtained. 88 DeviceSecurityInfo *info = NULL; 89 90 // Call RequestDeviceSecurityInfo to obtain the device security level information of the peer device. 91 int32_t ret = RequestDeviceSecurityInfo(device, DEFAULT_OPTION, &info); 92 93 int32_t level = 0; 94 // Obtain the device security level from the device security level information. 95 ret = GetDeviceSecurityLevelValue(info, &level); 96 if (ret == SUCCESS) { 97 // The operation is successful. 98 return; 99 } 100 // Release the memory before the processing is complete. 101 FreeDeviceSecurityInfo(info); 102 ``` 103 104### Development Example 105 106A service with the file sharing function needs to be developed. To prevent sensitive files from being shared unintentionally, the following judgments must be performed before any file is sent: 107 108- If the security level of the destination device is SL3 or higher, the service sends the file. 109- If the security level of the destination device is lower than SL3, the service denies the file transfer and display a dialog box to notify the user. 110 111**Example of synchronously obtaining the device security level** 112 113```cpp 114void CheckDestDeviceSecurityLevel(const DeviceIdentify *device, RequestOption *option) 115{ 116 // Pointer to the device security level information. 117 DeviceSecurityInfo *info = NULL; 118 // Obtain the security level information of the device. 119 int32_t ret = RequestDeviceSecurityInfo(device, option, &info); 120 if (ret != SUCCESS) { 121 // Failed to obtain the information. You can develop a retry process as required. 122 return; 123 } 124 int32_t level = 0; 125 // Obtain the device security level from the device security level information. 126 ret = GetDeviceSecurityLevelValue(info, &level); 127 if (ret != SUCCESS) { 128 // Failed to obtain the security level. You can develop a retry process as required. 129 return; 130 } 131 // After the device security level is successfully obtained, check the lowest security level required for the current operation. 132 // The lowest device security level required for the current operation is 3. 133 if (level >= 3) { 134 // The security level of the target device meets the requirements. Services are processed properly. 135 } else { 136 // The security level of the target device does not meet the requirements. An alert or dialog box is displayed as required. 137 } 138 // Release the memory before the processing is complete. 139 FreeDeviceSecurityInfo(info); 140} 141``` 142 143**Example of asynchronously obtaining the device security level** 144 145```cpp 146// Callback 147void DeviceSecurityInfoCallback(const DeviceIdentify *identify, struct DeviceSecurityInfo *info) 148{ 149 int32_t level = 0; 150 // Obtain the device security level from the device security level information. 151 int32_t ret = GetDeviceSecurityLevelValue(info, &level); 152 if (ret != SUCCESS) { 153 // Failed to obtain the information. You can develop a retry process as required. 154 return; 155 } 156 // After the device security level is successfully obtained, check the lowest security level required for the current operation. 157 // The lowest device security level required for the current operation is 3. 158 if (level >= 3) { 159 // The security level of the target device meets the requirements. Services are processed properly. 160 } else { 161 // The security level of the target device does not meet the requirements. An alert or dialog box is displayed as required. 162 } 163 // Release the memory before the processing is complete. 164 FreeDeviceSecurityInfo(info); 165} 166 167void CheckDestDeviceSecurityLevelAsync(const DeviceIdentify *device, RequestOption *option) 168{ 169 // Invoke the asynchronous callback to return the device security level obtained. 170 int ret = RequestDeviceSecurityInfoAsync(device, option, DeviceSecurityInfoCallback); 171 if (ret != SUCCESS) { 172 // Failed to obtain the security level. You can develop a retry process as required. 173 // In this case, the callback will not be invoked. 174 return; 175 } 176 // The callback is invoked. Wait for the callback to return the device security level. 177} 178``` 179 180## Customizing Device Security Levels 181 182### Device Security Level Credential 183 184To ensure its integrity and non-repudiation, the security level information must be encapsulated in a "device security level credential" (credential for short) file for transmission between devices. In addition to the security level information of the device, the credential may include device attributes, such as the device model and version. Moreover, the credential must be signed using the public key infrastructure (PKI) technology. Other basic security capabilities of OpenHarmony, such as [Device Authentication](https://gitee.com/openharmony/security_deviceauth) and [HUKS](https://gitee.com/openharmony/security_huks), are used to ensure secure transmission of credentials. 185 186### Default Implementation 187 188The DSLM module provides default implementation of security level information synchronization and verification. It is assumed that the security level of all OpenHarmony devices is SL1, and a loose verification scheme is used. For details, see the [source code](https://gitee.com/openharmony/security_device_security_level/tree/master/oem_property/ohos). 189 190You can change the device security level as required. For details about the OpenHarmony device security levels, see [Basic Concepts](#basic-concepts). You can also use more severe verification schemes, including but are not limited to using device-specific credential, periodically downloading updated credentials from a server and strictly authenticating the issuer and validity period of the credentials, and using Trusted Execution Environment (TEE) or even Secure Element (SE) to sign credential files. 191 192### Generating a Credential File 193 194The credential file consists of four Base64-encoded strings, separated by periods (.). The following is an example: 195 196```undefined 197<base64-header>.<base64-payload>.<base64-signature>.<base64-attestation> 198``` 199 200#### 1. Construct the `header`. 201 202The header is a fixed JSON string in the following format: 203 204``` json 205{ 206 "typ": "DSL" 207} 208``` 209 210Encode the header string to Base64 format to obtain `<base64-header>`. 211 212```undefined 213eyJ0eXAiOiAiRFNMIn0= 214``` 215 216#### 2. Construct the `payload`. 217 218Construct the payload in a JSON string. The following is an example: 219 220``` json 221{ 222 "type": "debug", 223 "manufacture": "ohos", 224 "brand": "rk3568", 225 "model": "rk3568", 226 "softwareVersion": "3.2.2", 227 "securityLevel": "SL1", 228 "signTime": "20220209150259", 229 "version": "1.0.1" 230} 231``` 232 233Encode the payload string to Base64 format to obtain `<base64-payload>`. 234 235```undefined 236eyJ0eXBlIjogImRlYnVnIiwgIm1hbnVmYWN0dXJlIjogIm9ob3MiLCAiYnJhbmQiOiAicmszNTY4IiwgIm1vZGVsIjogInJrMzU2OCIsICJzb2Z0d2FyZVZlcnNpb24iOiAiMy4yLjIiLCAic2VjdXJpdHlMZXZlbCI6ICJTTDEiLCAic2lnblRpbWUiOiAiMjAyMjAyMDkxNTAyNTkiLCAidmVyc2lvbiI6ICIxLjAuMSJ9 237``` 238 239The fields in the `payload` are described as follows: 240 241| Field| Description| Mandatory| Value Range| 242| :-------------- | :----------------- | :----------- | :-------------------- | 243| type | Credential type.| Yes| [debug release] | 244| manufacture | Device manufacturer information.| Yes| string [0..128] | 245| brand | Device brand.| Yes| string [0..128] | 246| model | Device model.| Yes| string [0..128] | 247| softwareVersion | Device software version.| Yes| string [0..128] | 248| securityLevel | Device security level.| Yes| [SL1 SL2 SL3 SL4 SL5] | 249| signTime | Time when the credential was signed.| Yes| string [0..128] | 250| version | Credential version.| Yes| string [0..32] | 251| sn | Device SN.| No| string [0..128] | 252| udid | Device UDID.| No| string [0..128] | 253 254#### 3. Construct the `signature`. 255 256Construct the signature of the header and payload. 257 258##### 3.1 Construct the raw data to be signed. 259 260Combine the Base64-encoded header and payload with a period (.) in between to obtain `<base64-head>.<base64-payload>`. 261Example: 262 263```undefined 264eyJ0eXAiOiAiRFNMIn0=.eyJ0eXBlIjogImRlYnVnIiwgIm1hbnVmYWN0dXJlIjogIm9ob3MiLCAiYnJhbmQiOiAicmszNTY4IiwgIm1vZGVsIjogInJrMzU2OCIsICJzb2Z0d2FyZVZlcnNpb24iOiAiMy4yLjIiLCAic2VjdXJpdHlMZXZlbCI6ICJTTDEiLCAic2lnblRpbWUiOiAiMjAyMjAyMDkxNTAyNTkiLCAidmVyc2lvbiI6ICIxLjAuMSJ9 265``` 266 267##### 3.2 Generate a private key for signature. 268 269The Elliptic Curve Digital Signature algorithm (ECDSA) is used to sign the raw data in the credential file. Generate an ECDSA key pair `<ecc-l3-pk>` and `<ecc-l3-sk>` first. 270 271> **NOTICE** 272> 273> This step must be performed in a secure and reliable environment, for example, a cryptographic machine that meets related security requirements, to ensure that the key used for signature is not disclosed. 274 275##### 3.3 Sign the raw data. 276 277Use the ECC private key `<ecc-l3-sk>` to sign `<base64-head>.<base64-payload>`, and encode the signature to Base64 format to obtain `<base64-signature>`. 278 279```undefined 280MGUCMDb9xoiFzTWVkHDU3VWSVQ59gLyw4TchZ0+eQ3vUfQsLt3Hkg0r7a/PmhkNr3X/mTgIxAIywIRE6vRTRs0xk6xKp8A0XwMMiIyjZlujPJfasCvFonpsvXLAqCAIYbe1J0k4Zfg== 281``` 282 283#### 4. Construct the `attestation`. 284 285> **NOTICE** 286> 287> This step must be performed in a secure and reliable environment, for example, a cryptographic machine that meets related security requirements, to ensure that the key used for signature is not disclosed. 288> The key pairs involved in this step do not need to be generated each time. Secure key pairs can be reused. 289 290##### 4.1 Generate verification information for an end-entity certificate signature. 291 2921. Generate an ECDSA key pair `<ecc-l2-pk>` and `<ecc-l2-sk>` for an intermediate CA certificate signature. 2932. Use `<ecc-l2-sk>` to sign `<ecc-l3-pk>` (generated in step 3.2) to obtain `<ecc-l3-pk-signature>`. 2943. Combine `<ecc-l3-pk>` and `<ecc-l3-pk-signature>` into a JSON string. The following is an example: 295 296``` json 297{ 298 "userPublicKey": "<ecc-l3-pk>", 299 "signature": "<ecc-l3-pk-signature>" 300} 301``` 302 303##### 4.2 Generate verification information for an intermediate CA certificate signature. 304 3051. Generate an ECDSA key pair `<ecc-root-pk>` and `<ecc-root-sk>` for a root certificate signature. 3062. Use `<ecc-root-sk>` to sign `<ecc-l2-pk>` (generated in step 4.1) to obtain `<ecc-l2-pk-signature>`. 3073. Combine `<ecc-l3-pk>` and `<ecc-l3-pk-signature>` into a JSON string. 308The following is an example: 309 310``` json 311{ 312 "userPublicKey": "<ecc-l2-pk>", 313 "signature": "<ecc-l2-pk-signature>" 314} 315``` 316 317##### 4.3 Generate root signature verification information. 318 3191. Use `<ecc-root-sk>` to sign the `<ecc-root-pk>` (generated in step 4.2) to obtain `<ecc-root-pk-self-signature>` (a self-signature). 3202. Combine `<ecc-root-pk>` and `<ecc-root-pk-self-signature>` into a JSON string. The following is an example: 321 322``` json 323{ 324 "userPublicKey": "<ecc-root-pk>", 325 "signature": "<ecc-root-pk-self-signature>" 326} 327``` 328 329##### 4.4 Generate the `attestation`. 330 3311. Combine the preceding three pieces of signature information into a JSON array. 332 333 ```json 334 [ 335 { 336 "userPublicKey": "<ecc-l3-pk>", 337 "signature": "<ecc-l3-pk-signature>" 338 }, 339 { 340 "userPublicKey": "<ecc-l2-pk>", 341 "signature": "<ecc-l2-pk-signature>" 342 }, 343 { 344 "userPublicKey": "<ecc-root-pk>", 345 "signature": "<ecc-root-pk-self-signature>" 346 } 347 ] 348 ``` 349 3502. Encode the JSON array to Base64 format to obtain `<base64-attestation>`. 351 352 ```undefined 353 W3sidXNlclB1YmxpY0tleSI6ICJNSG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3VlJKRENBT3FYRnhGektJbFBFTDFvcFJDUmhhWmJrRzc5Y3ZrWC9HVVhlaFVYc2V2ZGhyb2VRVERFdz09IiwgInNpZ25hdHVyZSI6ICJNR1FDTUdQRndvSDJLbHhwbVZhWXRWV1ViMHpDSUJxYXFXY2F6czFqOVp4YklLUmVkR2tJY0VJdHN0UFoxdnVTanYvNDJnSXdSeGZPcTRoQTdNMHlGV2ZPSndqRTlTc2JsYXhvRDNiRTZCYzN2QjUyMmsyQ0ZJNWJqelpkeUFTVW04d2J2TW5WIn1d 354 ``` 355 356#### 5. Construct a complete credential. 357 358Put the four pieces of data together with a period (.) in between to obtain `<base64-header>.<base64-payload>.<base64-signature>.<base64-attestation>`. The following is an example: 359 360```undefined 361eyJ0eXAiOiAiRFNMIn0=.eyJ0eXBlIjogImRlYnVnIiwgIm1hbnVmYWN0dXJlIjogIm9ob3MiLCAiYnJhbmQiOiAicmszNTY4IiwgIm1vZGVsIjogInJrMzU2OCIsICJzb2Z0d2FyZVZlcnNpb24iOiAiMy4yLjIiLCAic2VjdXJpdHlMZXZlbCI6ICJTTDEiLCAic2lnblRpbWUiOiAiMjAyMjAyMDkxNTAyNTkiLCAidmVyc2lvbiI6ICIxLjAuMSJ9.MGUCMDb9xoiFzTWVkHDU3VWSVQ59gLyw4TchZ0+eQ3vUfQsLt3Hkg0r7a/PmhkNr3X/mTgIxAIywIRE6vRTRs0xk6xKp8A0XwMMiIyjZlujPJfasCvFonpsvXLAqCAIYbe1J0k4Zfg==.W3sidXNlclB1YmxpY0tleSI6ICJNSG93RkFZSEtvWkl6ajBDQVFZSkt5UURBd0lJQVFFTEEySUFCREdOMU9xYWZrWFc2a0l1SEZrMVQ0TS84RVJUY3p0eWRDaGtramFROEkzNEc2Q3E1aTNJcnczVnRhQS9KTTF2a0lHOUZDVWRUaHZFUlJFUTFUdG9xemdxZW9SUzVwQW1EYUUyalEwYzdDem8rOHVUWTRIYW1weXZ1TENtenlYUXFnPT0iLCAic2lnbmF0dX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2JsYXhvRDNiRTZCYzN2QjUyMmsyQ0ZJNWJqelpkeUFTVW04d2J2TW5WIn1d 362``` 363 364### Credential Exchange Protocol 365 366When detecting a device goes online, the DSLM module requests the device security level credential from the device through the channel provided by [DSoftBus](https://gitee.com/openharmony/communication_dsoftbus). 367 368The packet for requesting the credential is in the following format: 369 370``` json 371{ 372 "message": 1, 373 "payload": { 374 "version": 196608, 375 "challenge": "0102030405060708", 376 "support": [ 377 300 378 ] 379 } 380} 381``` 382 383The fields in the request message are described as follows: 384 385| Field| Description| 386| :-------- | :------------------------------------ | 387| message | Message header. The value **1** indicates a request for the device security level credential.| 388| payload | Message payload, which is the specific request information.| 389| version | Version of the protocol used by the requester.| 390| challenge | Challenge value corresponding to this request.| 391| support | List of credential formats supported by the requester.| 392 393After receiving the request, the peer device returns a response in the following format: 394 395``` json 396{ 397 "message": 2, 398 "payload": { 399 "version": 196608, 400 "type": 300, 401 "challenge": "0102030405060708", 402 "info": "YWJjZAEDBQcJ..." 403 } 404} 405``` 406 407The fields in the response message are described as follows: 408| Field| Description| 409| :-------- | :--------------------------------------------------------- | 410| message | Message header. The value **2** indicates a response to the request for the device security level credential.| 411| payload | Message payload, which is the specific response information.| 412| version | Version of the protocol used by the responder. | 413| type | Format of the credential returned, which describes how to parse the **info** field.| 414| challenge | Challenge value corresponding to this response message.| 415| info | Signed credential information, which also includes the device information and challenge value verification information.| 416 417### Tool 418 419The DSLM module provides a [credential tool](https://gitee.com/openharmony/security_device_security_level/blob/master/oem_property/ohos/dslm_cred_tool.py) to help you better understand the issuing and verification of credentials. This tool is a Python script encapsulated with OpenSSL commands. 420You can use the tool as follows: 421 4221. Initialize the signature key. 423 424 ``` undefined 425 ./dslm_cred_tool.py init 426 ``` 427 4282. Generate a credential. 429 430 For example, to generate a credential file **cred.txt** with the device model of **rk3568**, device version of **3.0.0**, and device security level of **SL3**, run the following command: 431 432 ``` undefined 433 ./dslm_cred_tool.py create --field-manufacture OHOS --field-brand rk3568 --field-model rk3568 --field-software-version 3.0.0 --field-security-level SL3 --cred-file cred.txt 434 ``` 435 436 A credential file is generated as follows: 437 438 ``` undefined 439 cat cred.txt 440 eyJ0eXAiOiAiRFNMIn0=.eyJ0eXBlIjogImRlYnVnIiwgIm1hbnVmYWN0dXJlIjogIk9IT1MiLCAiYnJhbmQiOiAicmszNTY4IiwgIm1vZGVsIjogInJrMzU2OCIsICJzb2Z0d2FyZVZlcnNpb24iOiAiMy4wLjAiLCAic2VjdXJpdHlMZXZlbCI6ICJTTDMiLCAic2lnblRpbWUiOiAiMjAyMjAyMDkxNTUzMDMiLCAidmVyc2lvbiI6ICIxLjAuMSJ9.MGQCMEqZy/snsRyjMupnEvTpQfhQn+IcdCc5Q3NGxllNQVhoZX8PNyw6ATTgyx+26ghmtQIwVH5KwQ4/VejxckeHmtkBVhofhgmRapzvyVnyiB3PdsU7nvHk8A/zC7PFy1CWBG3z.W3sidXNlclB1YmxpY0tleSI6ICJNSG93RkFZSEtvWkl6ajBDQVFZSkt5UURBd0lJQVFFTEEySUFCQzFXRUxSVlU1NGp1U1ZXWlUrT29CM3hacFd5MWg3QW5uSFdKWm5QbTB3S2l0ZlJZelJKZ3FiUGQyZ3ltVXBUWVl1cmhyRDQxbFdPbUNzcmt0VWdaNTFXdGNCTmc5SG1GODkzc2ZHVFM5eUJNS0JoMGcxSHZaSVFSN1k0S3FXaWpnPT0iLCAic2lnbmF0dXJlIjogIk1HUUNNRFVicTZ2Z2R1YVF0bFVwOTR0azd4VjRJcEx2WVZWY3Y4aFNOTkw0azdPRHhmbEVGTHJFaUdPRWhwMUcweGFGYlFJd1pUbTk1cWx4OTBFZnptV3VIOGlEY2ZWYVlQS2N5SEYwR2ZFcEUzb1NESzQwZEFOZ0FJMWVQY09rTzBPOTdnTFAifSwgeyJ1c2VyUHVibGljS2V5IjogIk1Ib3dGQVlIS29aSXpqMENBUVlKS3lRREF3SUlBUUVMQTJJQUJGKzY1a0lSYTM2dkE4QVZWNXFrcUozYXpXTkdGQy9oaVdPL0tFNHR0S1pMOUsyNlhzQ2hQbjVNc3BlT2F3b1dqSU02bTVLOFZTcU1DYlZNN0svY0VRU0tYdDJTeVJGZERVZU9TaFZmQm9YVmxqaXRUU2puN0V5Q2pERVZiWjFRNEE9PSIsICJzaWduYXR1cmUiOiAiTUdRQ01HanF2cnZ5VW1YNVZLVVc1UkFkUTNkZ2hBYmNBazBRQnppQlFWMVFZUTNQMVFPSzdMckM1b0RObXh6T2Y0QUtmd0l3SzVWU2x3ZG5JSUR6Zm9PUXBEUVAycGhTVGgxSGVjbXJRK1F4VGxWelo0aHJsdnJyd2xCNnp0T3pWRFdNblRELyJ9LCB7InVzZXJQdWJsaWNLZXkiOiAiTUhvd0ZBWUhLb1pJemowQ0FRWUpLeVFEQXdJSUFRRUxBMklBQkZCa2FDNE9mc2VTREt2cW8vbU5VaUtXQ3JtK1VDNGFQcjVsODRNM2tMVCtDdkd3OWhqOGJ6d2I1MzNtVVlFZVhWWWtUdFlRYWRURkRJZXV1dGIzNU1QZDlEKytNMFRFWnZvcTY4NFhoYTVQMzBUbVRhK0ZvOG02UWliZWc3TmFQdz09IiwgInNpZ25hdHVyZSI6ICJNR1FDTURJcmpNYzhvODVPRHFZT0R4c05PcmpYdUhvWjM5endpZlhVTkdDc0lkN2xjU2FWcnhCVlNqRjRyaWg5Y1R6T3dRSXdWQXA3RUF5c1pucEI5REJWVWczQzlMeGQ3eTQxWEMwYVVPcGZUKzI3REVvWmM1WVVldDFGa1FwdmFQckduaFhVIn1d 441 ``` 442 4433. Verify a credential. 444 445 ``` undefined 446 ./dslm_cred_tool.py verify --cred-file cred.txt 447 ``` 448 449 The command output is as follows: 450 451 ``` undefined 452 head: 453 { 454 "typ": "DSL" 455 } 456 payload: 457 { 458 "type": "debug", 459 "manufacture": "OHOS", 460 "brand": "rk3568", 461 "model": "rk3568", 462 "softwareVersion": "3.0.0", 463 "securityLevel": "SL3", 464 "signTime": "20220209155303", 465 "version": "1.0.1" 466 } 467 verify success! 468 ``` 469 470## FAQs 471 472- Q: How can I use the credential tool in a production environment? 473 474 A: The credential tool cannot be directly used in the production environment. It is used to demonstrate the format and generation process of credentials. In the production environment, you are advised to generate credentials and save related keys in a cryptographic machine that meets related security requirements. 475 476- Q: How do I verify a credential in a production environment? 477 478 A: You are advised to use a properly kept private key to sign the credential and use more severe signature verification process instead of the default verification process provided by the DSLM module. For example, allow only the credentials issued by trusted certification authorities (CAs), and bind the credential and device ID to enhance the security. 479 480## References 481 482None 483