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