1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __ALCS_API_INTERNAL_H__
6 #define __ALCS_API_INTERNAL_H__
7 #include "CoAPExport.h"
8 #include "linkkit/alcs_api.h"
9 #include "alcs_internal.h"
10 #include "linkkit/infra/infra_aes.h"
11 
12 #define KEY_MAXCOUNT          10
13 #define RANDOMKEY_LEN         16
14 #define KEYSEQ_LEN            3
15 #define COAP_OPTION_SESSIONID 71
16 
17 #ifdef ALCS_CLIENT_ENABLED
18 typedef struct {
19     char *accessKey;
20     char *accessToken;
21     char *deviceName;
22     char *productKey;
23     struct list_head lst;
24 } ctl_key_item;
25 #endif
26 
27 #ifdef ALCS_SERVER_ENABLED
28 
29 typedef struct {
30     char keyprefix[KEYPREFIX_LEN + 1];
31     char *secret;
32     ServerKeyPriority priority;
33 } svr_key_info;
34 
35 typedef struct {
36     svr_key_info keyInfo;
37     struct list_head lst;
38 } svr_key_item;
39 
40 typedef struct {
41     char *id;
42     char *revocation;
43     svr_key_info keyInfo;
44     struct list_head lst;
45 } svr_group_item;
46 #endif
47 
48 typedef struct {
49     char *id;
50     char *accessKey;
51     char *accessToken;
52     struct list_head lst;
53 } ctl_group_item;
54 
55 typedef struct {
56     void *list_mutex;
57 #ifdef ALCS_CLIENT_ENABLED
58     struct list_head lst_ctl;
59     unsigned char ctl_count;
60 #endif
61 #ifdef ALCS_SERVER_ENABLED
62     struct list_head lst_svr;
63     unsigned char svr_count;
64     char *revocation;
65 #endif
66     struct list_head lst_ctl_group;
67     int ctl_group_count;
68     struct list_head lst_svr_group;
69     int svr_group_count;
70 } auth_list;
71 
72 #define PK_DN_CHECKSUM_LEN 6
73 typedef struct {
74     char randomKey[RANDOMKEY_LEN + 1];
75     int sessionId;
76     char sessionKey[32];
77     int authed_time;
78     int heart_time;
79     int interval;
80     NetworkAddr addr;
81     char pk_dn[PK_DN_CHECKSUM_LEN];
82     struct list_head lst;
83 } session_item;
84 
85 #define ROLE_SERVER 2
86 #define ROLE_CLIENT 1
87 
88 typedef struct {
89     CoAPContext *context;
90     int seq;
91     auth_list lst_auth;
92 #ifdef ALCS_SERVER_ENABLED
93     struct list_head lst_svr_sessions;
94 #endif
95 #ifdef ALCS_CLIENT_ENABLED
96     struct list_head lst_ctl_sessions;
97 #endif
98     char role;
99     struct list_head lst;
100 } device_auth_list;
101 
102 #ifdef SUPPORT_MULTI_DEVICES
103 extern struct list_head device_list;
104 
105 device_auth_list *get_device(CoAPContext *context);
106 
107 auth_list *get_list(CoAPContext *context);
108 
109 #ifdef ALCS_CLIENT_ENABLED
110 struct list_head *get_ctl_session_list(CoAPContext *context);
111 #endif
112 
113 #ifdef ALCS_SERVER_ENABLED
114 struct list_head *get_svr_session_list(CoAPContext *context);
115 #endif
116 
117 #else
118 extern device_auth_list _device;
119 #define get_device(v) (&_device)
120 
121 #ifdef ALCS_SERVER_ENABLED
122 #define get_svr_session_list(v) \
123     (_device.role & ROLE_SERVER ? &_device.lst_svr_sessions : NULL)
124 #endif
125 #ifdef ALCS_CLIENT_ENABLED
126 #define get_ctl_session_list(v) \
127     (_device.role & ROLE_CLIENT ? &_device.lst_ctl_sessions : NULL)
128 #endif
129 
130 #define get_list(v) (&_device.lst_auth)
131 #endif
132 
133 void remove_session(CoAPContext *ctx, session_item *session);
134 
135 #ifdef ALCS_CLIENT_ENABLED
136 session_item *get_ctl_session(CoAPContext *ctx, AlcsDeviceKey *key);
137 #endif
138 
139 #ifdef ALCS_SERVER_ENABLED
140 session_item *get_svr_session(CoAPContext *ctx, AlcsDeviceKey *key);
141 session_item *get_session_by_checksum(struct list_head *sessions,
142                                       NetworkAddr *addr,
143                                       char ck[PK_DN_CHECKSUM_LEN]);
144 
145 #define MAX_PATH_CHECKSUM_LEN (5)
146 typedef struct {
147     char path[MAX_PATH_CHECKSUM_LEN];
148     char pk_dn[PK_DN_CHECKSUM_LEN];
149     char *filter_path;
150     path_type_t path_type;
151     CoAPRecvMsgHandler cb;
152     struct list_head lst;
153 } secure_resource_cb_item;
154 
155 extern struct list_head secure_resource_cb_head;
156 #endif
157 
158 int alcs_encrypt(const char *src, int len, const char *key, void *out);
159 int alcs_decrypt(const char *src, int len, const char *key, void *out);
160 int observe_data_encrypt(CoAPContext *ctx, const char *paths, NetworkAddr *addr,
161                          CoAPMessage *message, CoAPLenString *src,
162                          CoAPLenString *dest);
163 
164 bool is_networkadd_same(NetworkAddr *addr1, NetworkAddr *addr2);
165 void gen_random_key(unsigned char random[], int len);
166 bool req_payload_parser(const char *payload, int len, char **seq, int *seqlen,
167                         char **data, int *datalen);
168 int internal_secure_send(CoAPContext *ctx, session_item *session,
169                          NetworkAddr *addr, CoAPMessage *message, char observe,
170                          CoAPSendMsgHandler handler);
171 
172 int alcs_resource_register_secure(CoAPContext *context, const char *pk,
173                                   const char *dn, const char *path,
174                                   unsigned short permission, unsigned int ctype,
175                                   unsigned int maxage,
176                                   CoAPRecvMsgHandler callback);
177 void alcs_resource_cb_deinit(void);
178 void alcs_auth_list_deinit(void);
179 void alcs_utils_md5_hexstr(unsigned char input[16], unsigned char output[32]);
180 
181 #endif
182