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 
16 #ifndef CERT_MANAGER_URI_H
17 #define CERT_MANAGER_URI_H
18 
19 #include "cert_manager_mem.h"
20 #include "cm_type.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define DEC_LEN 10
27 
28 #define CM_URI_TYPE_CERTIFICATE ((uint32_t)0)
29 #define CM_URI_TYPE_MAC_KEY ((uint32_t)1)
30 #define CM_URI_TYPE_APP_KEY ((uint32_t)2)
31 #define CM_URI_TYPE_WLAN_KEY ((uint32_t)3)
32 #define CM_URI_TYPE_SYS_KEY ((uint32_t)4)
33 #define CM_URI_TYPE_MAX CM_URI_TYPE_SYS_KEY
34 #define CM_URI_TYPE_INVALID (CM_URI_TYPE_MAX + 1)
35 
36 #define MALLOC CMMalloc
37 #define FREE CMFree
38 
39 #define ASSERT_MALLOC(p, sz) do { (p) = MALLOC(sz); if ( (p) == NULL) { \
40     CM_LOG_E("Failed to allocate memory of size: %u\n", (uint32_t) (sz)); return CMR_ERROR_MALLOC_FAIL; } } while (0)
41 
42 // object types: certificate, mac-key, app-key, WLAN-key
43 static const char *g_types[] = { "c", "m", "ak", "wk", "sk" };
44 static const uint32_t TYPE_COUNT = 5;
45 
46 struct CMUri {
47     // path components
48     char *object;
49     uint32_t type;
50     char *user;
51     char *app;
52 
53     // query components
54     char *clientUser;
55     char *clientApp;
56     char *mac;
57 };
58 
59 // check if object type is a normal key type (APP_KEY or WLAN_KEY)
60 bool CertManagerIsKeyObjectType(uint32_t type);
61 
62 // Encode a URI to a char string. If (encoded) is NULL, only the required length is returned.
63 int32_t CertManagerUriEncode(char *encoded, uint32_t *encodedLen, const struct CMUri *uri);
64 
65 // Free memory allocated during CertManagerUriDecode.
66 int32_t CertManagerFreeUri(struct CMUri *uri);
67 
68 int32_t CertManagerUriDecode(struct CMUri *uri, const char *encoded);
69 
70 int32_t CertManagerGetUidFromUri(const struct CmBlob *uri, uint32_t *uid);
71 
72 int32_t CmConstructUri(const struct CMUri *uriObj, struct CmBlob *outUri);
73 
74 int32_t CmConstructCommonUri(const struct CmContext *context, const uint32_t type,
75     const struct CmBlob *certAlias, struct CmBlob *outUri);
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
81 #endif // CERT_MANAGER_URI_H