• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "hvb_cmdline.h"
16 #include "hvb_util.h"
17 #include "hvb_cert.h"
18 #include "hvb_rvt.h"
19 #include "hvb_ops.h"
20 #include "hvb.h"
21 #include "hvb_sysdeps.h"
22 #include "hvb_crypto.h"
23 
cmdline_append_option(struct hvb_verified_data * vd,const char * key,const char * value)24 static int cmdline_append_option(struct hvb_verified_data *vd, const char *key, const char *value)
25 {
26     uint64_t option_len = 0;
27     uint64_t key_len, value_len;
28     struct hvb_cmdline_data *cmdline = NULL;
29 
30     if (vd == NULL || vd->cmdline.buf == NULL)
31         return -1;
32 
33     cmdline = &vd->cmdline;
34 
35     key_len = hvb_strlen(key);
36     value_len = hvb_strlen(value);
37     /* 2 for blank space and = */
38     option_len = key_len + value_len + 2;
39     if (option_len > cmdline->max_size - cmdline->cur_pos - 1)
40         return -1;
41 
42     /* append blank space */
43     cmdline->buf[cmdline->cur_pos] = ' ';
44     cmdline->cur_pos++;
45     /* append key */
46     hvb_memcpy(cmdline->buf + cmdline->cur_pos, key, key_len);
47     cmdline->cur_pos += key_len;
48     /* append = */
49     cmdline->buf[cmdline->cur_pos] = '=';
50     cmdline->cur_pos++;
51     /* append value */
52     hvb_memcpy(cmdline->buf + cmdline->cur_pos, value, value_len);
53     cmdline->cur_pos += value_len;
54 
55     return 1;
56 }
57 
hvb_append_version_cmdline(struct hvb_verified_data * vd,const char * key_value,uint64_t version_major,uint64_t version_minor)58 static int hvb_append_version_cmdline(struct hvb_verified_data *vd, const char *key_value,
59                                       uint64_t version_major, uint64_t version_minor)
60 {
61     char major_digits[HVB_MAX_DIGITS_UINT64];
62     char minor_digits[HVB_MAX_DIGITS_UINT64];
63     char combined[HVB_MAX_DIGITS_UINT64 * 2 + 1];
64     uint64_t num_major_digits, num_minor_digits;
65 
66     num_major_digits = hvb_uint64_to_base10(version_major, major_digits);
67     num_minor_digits = hvb_uint64_to_base10(version_minor, minor_digits);
68 
69     hvb_memcpy(combined, major_digits, num_major_digits);
70     combined[num_major_digits] = '.';
71     hvb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
72     combined[num_major_digits + 1 + num_minor_digits] = '\0';
73 
74     return cmdline_append_option(vd, key_value, combined);
75 }
76 
cmdline_append_uint64_base10(struct hvb_verified_data * vd,const char * key,uint64_t value)77 static int cmdline_append_uint64_base10(struct hvb_verified_data *vd, const char *key, uint64_t value)
78 {
79     char digits[HVB_MAX_DIGITS_UINT64];
80 
81     hvb_uint64_to_base10(value, digits);
82 
83     return cmdline_append_option(vd, key, digits);
84 }
85 
cmdline_append_hex(struct hvb_verified_data * vd,const char * key,const uint8_t * data,uint64_t data_len)86 static int cmdline_append_hex(struct hvb_verified_data *vd, const char* key,
87                               const uint8_t *data, uint64_t data_len)
88 {
89     int ret;
90     char *hex_data = hvb_bin2hex(data, data_len);
91 
92     if (hex_data == NULL) {
93         return 0;
94     }
95 
96     ret = cmdline_append_option(vd, key, hex_data);
97     hvb_free(hex_data);
98 
99     return ret;
100 }
101 
hvb_creat_cmdline(struct hvb_ops * ops,struct hvb_verified_data * vd)102 enum hvb_errno hvb_creat_cmdline(struct hvb_ops *ops, struct hvb_verified_data *vd)
103 {
104     enum hvb_errno ret = HVB_OK;
105     enum hvb_io_errno io_ret = HVB_IO_OK;
106     bool device_locked = false;
107 
108     /* set ohos.boot.hvb.version. */
109     if (!hvb_append_version_cmdline(vd, HVB_CMDLINE_VERSION,
110                                     HVB_VERSION_MAJOR, HVB_VERSION_MINOR)) {
111         ret = HVB_ERROR_OOM;
112         goto fail;
113     }
114 
115     /* set ohos.boot.device_state to "locked" or "unlocked". */
116     io_ret = ops->read_lock_state(ops, &device_locked);
117     if (io_ret == HVB_IO_ERROR_OOM) {
118         ret = HVB_ERROR_OOM;
119         goto fail;
120     } else if (io_ret != HVB_IO_OK) {
121         hvb_print("Error getting device state.\n");
122         ret = HVB_ERROR_IO;
123         goto fail;
124     }
125 
126     if (!cmdline_append_option(vd, HVB_CMDLINE_DEV_STATE,
127                                device_locked ? "locked" : "unlocked")) {
128         ret = HVB_ERROR_OOM;
129         goto fail;
130     }
131 
132     /*
133      * set ohos.boot.hvb.{hash_algo, size, digest} - use same hash
134      * function as is used to sign rvt.
135      */
136     uint8_t rvt_digest[HVB_SHA256_DIGEST_BYTES] = {0};
137     uint64_t rvt_size = 0;
138     for (uint64_t n = 0; n < vd->num_loaded_certs; n++) {
139         rvt_size += vd->certs[n].data.size;
140     }
141 
142     if (hvb_calculate_certs_digest(vd, rvt_digest) != HVB_OK) {
143         hvb_print("Error calculate rvt digest.\n");
144         ret = HVB_ERROR_OOM;
145         goto fail;
146     }
147 
148     if (!cmdline_append_option(vd, HVB_CMDLINE_HASH_ALG, "sha256") ||
149         !cmdline_append_uint64_base10(vd, HVB_CMDLINE_RVT_SIZE, rvt_size) ||
150         !cmdline_append_hex(vd, HVB_CMDLINE_CERT_DIGEST, rvt_digest, HVB_SHA256_DIGEST_BYTES)) {
151         ret = HVB_ERROR_OOM;
152         goto fail;
153     }
154 
155 fail:
156     return ret;
157 }
158