1 /*
2 * Copyright (c) 2022-2024 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
16 #ifndef CM_TYPE_H
17 #define CM_TYPE_H
18
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 #ifndef CM_API_PUBLIC
27 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(__ICCARM__) /* __ICCARM__ for iar */
28 #define CM_API_EXPORT
29 #else
30 #define CM_API_EXPORT __attribute__ ((visibility("default")))
31 #endif
32 #else
33 #define CM_API_EXPORT __attribute__ ((visibility("default")))
34 #endif
35
36 #define MAX_LEN_CERTIFICATE 8196
37
38 #define MAX_LEN_CERTIFICATE_CHAIN (3 * MAX_LEN_CERTIFICATE)
39
40 #define MAX_SUFFIX_LEN 16
41 #define MAX_COUNT_CERTIFICATE 256
42 #define MAX_LEN_URI 256
43 #define MAX_AUTH_LEN_URI 256
44 #define MAX_LEN_CERT_ALIAS 65 /* include 1 byte: the terminator('\0') */
45 #define MAX_LEN_SUBJECT_NAME 256
46 #define MAX_LEN_PACKGE_NAME 64
47 #define MAX_UINT32_LEN 16
48
49 #define MAX_LEN_ISSUER_NAME 256
50 #define MAX_LEN_SERIAL 64
51 #define MAX_LEN_NOT_BEFORE 32
52 #define MAX_LEN_NOT_AFTER 32
53 #define MAX_LEN_FINGER_PRINT_SHA256 128
54 #define MAX_LEN_APP_CERT 20480
55 #define MAX_LEN_APP_CERT_PASSWD 33 /* 32位密码 + 1位结束符 */
56
57 #define CERT_MAX_PATH_LEN 256
58 #define CM_ARRAY_SIZE(arr) ((sizeof(arr)) / (sizeof((arr)[0])))
59 #define INIT_INVALID_VALUE 0xFFFFFFFF
60
61 #define CERT_STATUS_ENABLED ((uint32_t) 0)
62 #define CERT_STATUS_DISABLED ((uint32_t) 1)
63
64 /*
65 * Align to 4-tuple
66 * Before calling this function, ensure that the size does not overflow after 3 is added.
67 */
68 #define ALIGN_SIZE(size) ((((uint32_t)(size) + 3) >> 2) << 2)
69
70 #define CM_BITS_PER_BYTE 8
71 #define CM_KEY_BYTES(keySize) (((keySize) + CM_BITS_PER_BYTE - 1) / CM_BITS_PER_BYTE)
72 #define MAX_OUT_BLOB_SIZE (5 * 1024 * 1024)
73
74 #define CM_CREDENTIAL_STORE 0
75 #define CM_SYSTEM_TRUSTED_STORE 1
76 #define CM_USER_TRUSTED_STORE 2
77 #define CM_PRI_CREDENTIAL_STORE 3
78 #define CM_SYS_CREDENTIAL_STORE 4
79 #define CM_STORE_CHECK(a) \
80 (((a) != CM_CREDENTIAL_STORE) && ((a) != CM_PRI_CREDENTIAL_STORE) && ((a) != CM_SYS_CREDENTIAL_STORE))
81
82 enum CmKeyDigest {
83 CM_DIGEST_NONE = 0,
84 CM_DIGEST_MD5 = 1,
85 CM_DIGEST_SHA1 = 10,
86 CM_DIGEST_SHA224 = 11,
87 CM_DIGEST_SHA256 = 12,
88 CM_DIGEST_SHA384 = 13,
89 CM_DIGEST_SHA512 = 14,
90 };
91
92 enum CmKeyPurpose {
93 CM_KEY_PURPOSE_ENCRYPT = 1, /* Usable with RSA, EC, AES, and SM4 keys. */
94 CM_KEY_PURPOSE_DECRYPT = 2, /* Usable with RSA, EC, AES, and SM4 keys. */
95 CM_KEY_PURPOSE_SIGN = 4, /* Usable with RSA, EC keys. */
96 CM_KEY_PURPOSE_VERIFY = 8, /* Usable with RSA, EC keys. */
97 CM_KEY_PURPOSE_DERIVE = 16, /* Usable with EC keys. */
98 CM_KEY_PURPOSE_WRAP = 32, /* Usable with wrap key. */
99 CM_KEY_PURPOSE_UNWRAP = 64, /* Usable with unwrap key. */
100 CM_KEY_PURPOSE_MAC = 128, /* Usable with mac. */
101 CM_KEY_PURPOSE_AGREE = 256, /* Usable with agree. */
102 };
103
104 enum CmKeyPadding {
105 CM_PADDING_NONE = 0,
106 CM_PADDING_OAEP = 1,
107 CM_PADDING_PSS = 2,
108 CM_PADDING_PKCS1_V1_5 = 3,
109 CM_PADDING_PKCS5 = 4,
110 CM_PADDING_PKCS7 = 5,
111 };
112
113 enum CmErrorCode {
114 CM_SUCCESS = 0,
115 CM_FAILURE = -1,
116
117 CMR_ERROR_NOT_PERMITTED = -2,
118 CMR_ERROR_NOT_SUPPORTED = -3,
119 CMR_ERROR_STORAGE = -4,
120 CMR_ERROR_NOT_FOUND = -5,
121 CMR_ERROR_NULL_POINTER = -6,
122 CMR_ERROR_INVALID_ARGUMENT = -7,
123 CMR_ERROR_MAKE_DIR_FAIL = -8,
124 CMR_ERROR_INVALID_OPERATION = -9,
125 CMR_ERROR_OPEN_FILE_FAIL = -10,
126 CMR_ERROR_READ_FILE_ERROR = -11,
127 CMR_ERROR_WRITE_FILE_FAIL = -12,
128 CMR_ERROR_REMOVE_FILE_FAIL = -13,
129 CMR_ERROR_CLOSE_FILE_FAIL = -14,
130 CMR_ERROR_MALLOC_FAIL = -15,
131 CMR_ERROR_NOT_EXIST = -16,
132 CMR_ERROR_ALREADY_EXISTS = -17,
133 CMR_ERROR_INSUFFICIENT_DATA = -18,
134 CMR_ERROR_BUFFER_TOO_SMALL = -19,
135 CMR_ERROR_INVALID_CERT_FORMAT = -20,
136 CMR_ERROR_PARAM_NOT_EXIST = -21,
137 CMR_ERROR_SESSION_REACHED_LIMIT = -22,
138 CMR_ERROR_PERMISSION_DENIED = -23,
139 CMR_ERROR_AUTH_CHECK_FAILED = -24,
140 CMR_ERROR_KEY_OPERATION_FAILED = -25,
141 CMR_ERROR_NOT_SYSTEMP_APP = -26,
142 CMR_ERROR_MAX_CERT_COUNT_REACHED = -27,
143 CMR_ERROR_ALIAS_LENGTH_REACHED_LIMIT = -28,
144 CMR_ERROR_GET_ADVSECMODE_PARAM_FAIL = -29,
145 CMR_ERROR_DEVICE_ENTER_ADVSECMODE = -30,
146 };
147
148 enum CMErrorCode { /* temp use */
149 CMR_OK = 0,
150 CMR_ERROR = -1,
151 };
152
153 enum CmTagType {
154 CM_TAG_TYPE_INVALID = 0 << 28,
155 CM_TAG_TYPE_INT = 1 << 28,
156 CM_TAG_TYPE_UINT = 2 << 28,
157 CM_TAG_TYPE_ULONG = 3 << 28,
158 CM_TAG_TYPE_BOOL = 4 << 28,
159 CM_TAG_TYPE_BYTES = 5 << 28,
160 };
161
162 enum CmTag {
163 /* Inner-use TAGS used for ipc serialization */
164 CM_TAG_PARAM0_BUFFER = CM_TAG_TYPE_BYTES | 30001,
165 CM_TAG_PARAM1_BUFFER = CM_TAG_TYPE_BYTES | 30002,
166 CM_TAG_PARAM2_BUFFER = CM_TAG_TYPE_BYTES | 30003,
167 CM_TAG_PARAM3_BUFFER = CM_TAG_TYPE_BYTES | 30004,
168 CM_TAG_PARAM4_BUFFER = CM_TAG_TYPE_BYTES | 30005,
169 CM_TAG_PARAM0_UINT32 = CM_TAG_TYPE_UINT | 30006,
170 CM_TAG_PARAM1_UINT32 = CM_TAG_TYPE_UINT | 30007,
171 CM_TAG_PARAM2_UINT32 = CM_TAG_TYPE_UINT | 30008,
172 CM_TAG_PARAM3_UINT32 = CM_TAG_TYPE_UINT | 30009,
173 CM_TAG_PARAM4_UINT32 = CM_TAG_TYPE_UINT | 30010,
174 CM_TAG_PARAM0_BOOL = CM_TAG_TYPE_BOOL | 30011,
175 CM_TAG_PARAM1_BOOL = CM_TAG_TYPE_BOOL | 30012,
176 CM_TAG_PARAM2_BOOL = CM_TAG_TYPE_BOOL | 30013,
177 CM_TAG_PARAM3_BOOL = CM_TAG_TYPE_BOOL | 30014,
178 CM_TAG_PARAM4_BOOL = CM_TAG_TYPE_BOOL | 30015,
179 CM_TAG_PARAM0_NULL = CM_TAG_TYPE_BYTES | 30016,
180 CM_TAG_PARAM1_NULL = CM_TAG_TYPE_BYTES | 30017,
181 CM_TAG_PARAM2_NULL = CM_TAG_TYPE_BYTES | 30018,
182 CM_TAG_PARAM3_NULL = CM_TAG_TYPE_BYTES | 30019,
183 CM_TAG_PARAM4_NULL = CM_TAG_TYPE_BYTES | 30020,
184 };
185
186 #define CM_PARAM_BUFFER_NULL_INTERVAL ((CM_TAG_PARAM0_NULL) - (CM_TAG_PARAM0_BUFFER))
187
188 enum CmSendType {
189 CM_SEND_TYPE_ASYNC = 0,
190 CM_SEND_TYPE_SYNC,
191 };
192
193 struct CmMutableBlob {
194 uint32_t size;
195 uint8_t *data;
196 };
197
198 struct CmContext {
199 uint32_t userId;
200 uint32_t uid;
201 char packageName[MAX_LEN_PACKGE_NAME];
202 };
203
204 struct CmBlob {
205 uint32_t size;
206 uint8_t *data;
207 };
208
209 struct CertBlob {
210 struct CmBlob uri[MAX_COUNT_CERTIFICATE];
211 struct CmBlob certAlias[MAX_COUNT_CERTIFICATE];
212 struct CmBlob subjectName[MAX_COUNT_CERTIFICATE];
213 };
214
215 struct CmAppCertInfo {
216 struct CmBlob appCert;
217 struct CmBlob appCertPwd;
218 };
219
220 struct CertListAbtInfo {
221 uint32_t uriSize;
222 char uri[MAX_LEN_URI];
223 uint32_t aliasSize;
224 char certAlias[MAX_LEN_CERT_ALIAS];
225 uint32_t status;
226 uint32_t subjectNameSize;
227 char subjectName[MAX_LEN_SUBJECT_NAME];
228 };
229
230 struct CertAbstract {
231 char uri[MAX_LEN_URI];
232 char certAlias[MAX_LEN_CERT_ALIAS];
233 bool status;
234 char subjectName[MAX_LEN_SUBJECT_NAME];
235 };
236
237 struct CertList {
238 uint32_t certsCount;
239 struct CertAbstract *certAbstract;
240 };
241
242 struct CertAbtInfo {
243 uint32_t aliasSize;
244 char certAlias[MAX_LEN_CERT_ALIAS];
245 uint32_t status;
246 uint32_t certsize;
247 uint8_t certData[MAX_LEN_CERTIFICATE];
248 };
249
250 struct CertInfo {
251 char uri[MAX_LEN_URI];
252 char certAlias[MAX_LEN_CERT_ALIAS];
253 bool status;
254 char issuerName[MAX_LEN_ISSUER_NAME];
255 char subjectName[MAX_LEN_SUBJECT_NAME];
256 char serial[MAX_LEN_SERIAL];
257 char notBefore[MAX_LEN_NOT_BEFORE];
258 char notAfter[MAX_LEN_NOT_AFTER];
259 char fingerprintSha256[MAX_LEN_FINGER_PRINT_SHA256];
260 struct CmBlob certInfo;
261 };
262
263 struct CertFile {
264 const struct CmBlob *fileName;
265 const struct CmBlob *path;
266 };
267
268 struct CertFileInfo {
269 struct CmBlob fileName;
270 struct CmBlob path;
271 };
272
273 struct CMApp {
274 uint32_t userId;
275 uint32_t uid;
276 const char *packageName;
277 struct CmBlob *appId; // for attestation
278 };
279
280 struct Credential {
281 uint32_t isExist;
282 char type[MAX_LEN_SUBJECT_NAME];
283 char alias[MAX_LEN_CERT_ALIAS];
284 char keyUri[MAX_LEN_URI];
285 uint32_t certNum;
286 uint32_t keyNum;
287 struct CmBlob credData;
288 };
289
290 struct CredentialAbstract {
291 char type[MAX_LEN_SUBJECT_NAME];
292 char alias[MAX_LEN_CERT_ALIAS];
293 char keyUri[MAX_LEN_URI];
294 };
295
296 struct CredentialList {
297 uint32_t credentialCount;
298 struct CredentialAbstract *credentialAbstract;
299 };
300
301 struct AppCert {
302 uint32_t certCount;
303 uint32_t keyCount;
304 uint32_t certSize;
305 uint8_t appCertdata[MAX_LEN_CERTIFICATE_CHAIN];
306 };
307
308 struct CmParam {
309 uint32_t tag;
310 union {
311 bool boolParam;
312 int32_t int32Param;
313 uint32_t uint32Param;
314 uint64_t uint64Param;
315 struct CmBlob blob;
316 };
317 };
318
319 struct CmParamOut {
320 uint32_t tag;
321 union {
322 bool *boolParam;
323 int32_t *int32Param;
324 uint32_t *uint32Param;
325 uint64_t *uint64Param;
326 struct CmBlob *blob;
327 };
328 };
329
330 struct CmParamSet {
331 uint32_t paramSetSize;
332 uint32_t paramsCnt;
333 struct CmParam params[];
334 };
335
336 struct CmAppUidList {
337 uint32_t appUidCount;
338 uint32_t *appUid;
339 };
340
341 struct CmSignatureSpec {
342 uint32_t purpose;
343 uint32_t padding;
344 uint32_t digest;
345 };
346
347 struct CmAppCertParam {
348 struct CmBlob *appCert;
349 struct CmBlob *appCertPwd;
350 struct CmBlob *certAlias;
351 uint32_t store;
352 uint32_t userId;
353 };
354
CmIsAdditionOverflow(uint32_t a,uint32_t b)355 static inline bool CmIsAdditionOverflow(uint32_t a, uint32_t b)
356 {
357 return (UINT32_MAX - a) < b;
358 }
359
CmIsInvalidLength(uint32_t length)360 static inline bool CmIsInvalidLength(uint32_t length)
361 {
362 return (length == 0) || (length > MAX_OUT_BLOB_SIZE);
363 }
364
CmCheckBlob(const struct CmBlob * blob)365 static inline int32_t CmCheckBlob(const struct CmBlob *blob)
366 {
367 if ((blob == NULL) || (blob->data == NULL) || (blob->size == 0)) {
368 return CMR_ERROR_INVALID_ARGUMENT;
369 }
370 return CM_SUCCESS;
371 }
372
373 #ifdef __cplusplus
374 }
375 #endif
376
377 #endif /* CM_TYPE_H */
378