dev_sign_mqtt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "infra_defs.h"
  4. #include "infra_config.h"
  5. #include "infra_sha256.h"
  6. #include "dev_sign_api.h"
  7. #include "dev_sign_wrapper.h"
  8. #define SIGN_MQTT_PORT (1883)
  9. /* all secure mode define */
  10. #define MODE_TLS_GUIDER "-1"
  11. #define MODE_TLS_DIRECT "2"
  12. #define MODE_TCP_DIRECT_PLAIN "3"
  13. #define MODE_ITLS_DNS_ID2 "8"
  14. #ifdef MQTT_PRE_AUTH
  15. #define SECURE_MODE MODE_TLS_GUIDER
  16. #else
  17. #ifdef SUPPORT_TLS
  18. #define SECURE_MODE MODE_TLS_DIRECT
  19. #else
  20. #define SECURE_MODE MODE_TCP_DIRECT_PLAIN
  21. #endif
  22. #endif
  23. /* use fixed timestamp */
  24. #define TIMESTAMP_VALUE "2524608000000"
  25. /* clientid key value pair define */
  26. const char *clientid_kv[][2] = {
  27. {
  28. "timestamp", TIMESTAMP_VALUE
  29. },
  30. {
  31. "_v", "sdk-c-"IOTX_SDK_VERSION
  32. },
  33. {
  34. "securemode", SECURE_MODE
  35. },
  36. {
  37. "signmethod", "hmacsha256"
  38. },
  39. {
  40. "lan", "C"
  41. },
  42. #if defined(DEVICE_MODEL_ENABLED) && !defined(DEVICE_MODEL_CLASSIC)
  43. {
  44. "v", IOTX_ALINK_VERSION
  45. },
  46. #else
  47. {
  48. "gw", "0"
  49. },
  50. {
  51. "ext", "0"
  52. },
  53. #endif
  54. };
  55. static void _hex2str(uint8_t *input, uint16_t input_len, char *output)
  56. {
  57. char *zEncode = "0123456789ABCDEF";
  58. int i = 0, j = 0;
  59. for (i = 0; i < input_len; i++) {
  60. output[j++] = zEncode[(input[i] >> 4) & 0xf];
  61. output[j++] = zEncode[(input[i]) & 0xf];
  62. }
  63. }
  64. int _sign_get_clientid(char *clientid_string, const char *device_id, const char *custom_kv, uint8_t enable_itls)
  65. {
  66. uint8_t i;
  67. if (clientid_string == NULL || device_id == NULL) {
  68. return FAIL_RETURN;
  69. }
  70. memset(clientid_string, 0, DEV_SIGN_CLIENT_ID_MAXLEN);
  71. memcpy(clientid_string, device_id, strlen(device_id));
  72. memcpy(clientid_string + strlen(clientid_string), "|", 1);
  73. if (enable_itls > 0) {
  74. clientid_kv[2][1] = MODE_ITLS_DNS_ID2;
  75. }
  76. else {
  77. clientid_kv[2][1] = SECURE_MODE;
  78. }
  79. for (i = 0; i < (sizeof(clientid_kv) / (sizeof(clientid_kv[0]))); i++) {
  80. if ((strlen(clientid_string) + strlen(clientid_kv[i][0]) + strlen(clientid_kv[i][1]) + 2) >=
  81. DEV_SIGN_CLIENT_ID_MAXLEN) {
  82. return FAIL_RETURN;
  83. }
  84. memcpy(clientid_string + strlen(clientid_string), clientid_kv[i][0], strlen(clientid_kv[i][0]));
  85. memcpy(clientid_string + strlen(clientid_string), "=", 1);
  86. memcpy(clientid_string + strlen(clientid_string), clientid_kv[i][1], strlen(clientid_kv[i][1]));
  87. memcpy(clientid_string + strlen(clientid_string), ",", 1);
  88. }
  89. if (custom_kv != NULL) {
  90. if ((strlen(clientid_string) + strlen(custom_kv) + 1) >= DEV_SIGN_CLIENT_ID_MAXLEN) {
  91. return FAIL_RETURN;
  92. }
  93. memcpy(clientid_string + strlen(clientid_string), custom_kv, strlen(custom_kv));
  94. memcpy(clientid_string + strlen(clientid_string), ",", 1);
  95. }
  96. memcpy(clientid_string + strlen(clientid_string) - 1, "|", 1);
  97. return SUCCESS_RETURN;
  98. }
  99. int _iotx_generate_sign_string(const char *device_id, const char *device_name, const char *product_key, const char *device_secret, char *sign_string)
  100. {
  101. char signsource[DEV_SIGN_SOURCE_MAXLEN] = {0};
  102. uint16_t signsource_len = 0;
  103. const char sign_fmt[] = "clientId%sdeviceName%sproductKey%stimestamp%s";
  104. uint8_t sign_hex[32] = {0};
  105. signsource_len = sizeof(sign_fmt) + strlen(device_id) + strlen(device_name) + strlen(product_key) + strlen(TIMESTAMP_VALUE);
  106. if (signsource_len >= DEV_SIGN_SOURCE_MAXLEN) {
  107. return ERROR_DEV_SIGN_SOURCE_TOO_SHORT;
  108. }
  109. memset(signsource, 0, DEV_SIGN_SOURCE_MAXLEN);
  110. memcpy(signsource, "clientId", strlen("clientId"));
  111. memcpy(signsource + strlen(signsource), device_id, strlen(device_id));
  112. memcpy(signsource + strlen(signsource), "deviceName", strlen("deviceName"));
  113. memcpy(signsource + strlen(signsource), device_name, strlen(device_name));
  114. memcpy(signsource + strlen(signsource), "productKey", strlen("productKey"));
  115. memcpy(signsource + strlen(signsource), product_key, strlen(product_key));
  116. memcpy(signsource + strlen(signsource), "timestamp", strlen("timestamp"));
  117. memcpy(signsource + strlen(signsource), TIMESTAMP_VALUE, strlen(TIMESTAMP_VALUE));
  118. utils_hmac_sha256((uint8_t *)signsource, strlen(signsource), (uint8_t *)device_secret,
  119. strlen(device_secret), sign_hex);
  120. _hex2str(sign_hex, 32, sign_string);
  121. return SUCCESS_RETURN;
  122. }
  123. static char* rt_strlwr(char *str)
  124. {
  125. if(str == NULL)
  126. return NULL;
  127. char *p = str;
  128. while (*p != '\0')
  129. {
  130. if(*p >= 'A' && *p <= 'Z')
  131. *p = (*p) + 0x20;
  132. p++;
  133. }
  134. return str;
  135. }
  136. int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta, iotx_sign_mqtt_t *signout)
  137. {
  138. uint16_t length = 0;
  139. char device_id[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 1] = {0};
  140. int res;
  141. if (region >= IOTX_MQTT_DOMAIN_NUMBER || meta == NULL) {
  142. return -1;
  143. }
  144. memset(signout, 0, sizeof(iotx_sign_mqtt_t));
  145. memcpy(device_id, meta->product_key, strlen(meta->product_key));
  146. memcpy(device_id + strlen(device_id), ".", strlen("."));
  147. memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
  148. /* setup clientid */
  149. if (_sign_get_clientid(signout->clientid, device_id, NULL, 0) != SUCCESS_RETURN) {
  150. return ERROR_DEV_SIGN_CLIENT_ID_TOO_SHORT;
  151. }
  152. /* setup password */
  153. memset(signout->password, 0, DEV_SIGN_PASSWORD_MAXLEN);
  154. res = _iotx_generate_sign_string(device_id, meta->device_name, meta->product_key, meta->device_secret, signout->password);
  155. if (res < SUCCESS_RETURN) {
  156. return res;
  157. }
  158. /* setup hostname */
  159. if (IOTX_CLOUD_REGION_CUSTOM == region) {
  160. if (g_infra_mqtt_domain[region] == NULL) {
  161. return ERROR_DEV_SIGN_CUSTOM_DOMAIN_IS_NULL;
  162. }
  163. length = strlen(g_infra_mqtt_domain[region]) + 1;
  164. if (length >= DEV_SIGN_HOSTNAME_MAXLEN) {
  165. return ERROR_DEV_SIGN_HOST_NAME_TOO_SHORT;
  166. }
  167. memset(signout->hostname, 0, DEV_SIGN_HOSTNAME_MAXLEN);
  168. memcpy(signout->hostname, g_infra_mqtt_domain[region], strlen(g_infra_mqtt_domain[region]));
  169. }
  170. else {
  171. length = strlen(meta->product_key) + strlen(g_infra_mqtt_domain[region]) + 2;
  172. if (length >= DEV_SIGN_HOSTNAME_MAXLEN) {
  173. return ERROR_DEV_SIGN_HOST_NAME_TOO_SHORT;
  174. }
  175. memset(signout->hostname, 0, DEV_SIGN_HOSTNAME_MAXLEN);
  176. memcpy(signout->hostname, meta->product_key, strlen(meta->product_key));
  177. memcpy(signout->hostname + strlen(signout->hostname), ".", strlen("."));
  178. memcpy(signout->hostname + strlen(signout->hostname), g_infra_mqtt_domain[region],
  179. strlen(g_infra_mqtt_domain[region]));
  180. }
  181. rt_strlwr(signout->hostname);
  182. HAL_Printf("host name:%s\r\n", signout->hostname);
  183. /* setup username */
  184. length = strlen(meta->device_name) + strlen(meta->product_key) + 2;
  185. if (length >= DEV_SIGN_USERNAME_MAXLEN) {
  186. return ERROR_DEV_SIGN_USERNAME_TOO_SHORT;
  187. }
  188. memset(signout->username, 0, DEV_SIGN_USERNAME_MAXLEN);
  189. memcpy(signout->username, meta->device_name, strlen(meta->device_name));
  190. memcpy(signout->username + strlen(signout->username), "&", strlen("&"));
  191. memcpy(signout->username + strlen(signout->username), meta->product_key, strlen(meta->product_key));
  192. /* setup port */
  193. #ifdef SUPPORT_TLS
  194. signout->port = 443;
  195. #else
  196. signout->port = 1883;
  197. #endif /* #ifdef SUPPORT_TLS */
  198. return SUCCESS_RETURN;
  199. }