fota_verify.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <mbedtls/md5.h>
  8. #include <mbedtls/sha1.h>
  9. #include <mbedtls/sha256.h>
  10. #include <aos/kernel.h>
  11. #include "yoc/fota.h"
  12. #include <ulog/ulog.h>
  13. #include "imagef.h"
  14. #define TAG "fotav"
  15. static uint8_t g_pubkey_rsa[] = {
  16. 0xeb, 0xe3, 0xa4, 0x41, 0x10, 0xd6, 0x25, 0x98, 0xc2, 0x27, 0x8b, 0x36, 0xac, 0xfa, 0xc3, 0x86,
  17. 0x01, 0x21, 0x16, 0x64, 0x6f, 0xf2, 0x37, 0x72, 0xf4, 0xc0, 0x60, 0xab, 0x38, 0x60, 0x47, 0x7a,
  18. 0x94, 0x7f, 0x80, 0x48, 0xa8, 0xeb, 0xa5, 0xf7, 0x1b, 0x9f, 0xf2, 0xc2, 0xf9, 0x39, 0x08, 0xbc,
  19. 0xa9, 0x3e, 0x3c, 0x0c, 0x52, 0x15, 0xb3, 0x57, 0x81, 0x5e, 0x02, 0xf2, 0xd7, 0x7e, 0x04, 0x4c,
  20. 0x6d, 0x93, 0xc4, 0x5d, 0xa3, 0x97, 0x17, 0xa6, 0x83, 0xa5, 0x9c, 0xc4, 0x91, 0xcc, 0x2d, 0x78,
  21. 0xeb, 0x64, 0xbf, 0x05, 0x4d, 0x0b, 0xed, 0x09, 0x4f, 0xd4, 0x2d, 0x46, 0x2d, 0xf0, 0xfc, 0xd0,
  22. 0x03, 0xc0, 0xb7, 0x5c, 0x19, 0x6a, 0x87, 0x11, 0xff, 0xf8, 0xa4, 0x6a, 0x55, 0xa2, 0xf6, 0x25,
  23. 0xb9, 0x00, 0x96, 0xaf, 0x2c, 0x7f, 0x15, 0xb9, 0xe3, 0xe3, 0x24, 0x3d, 0xc3, 0xa7, 0xa4, 0x37,
  24. };
  25. // static uint8_t g_pubkey_rsa[] = {
  26. // 0xe5, 0x3f, 0x3e, 0xa4, 0x54, 0xab, 0x56, 0xea, 0x96, 0x6c, 0x8c, 0x00, 0xc8, 0x6c, 0x67, 0x1c,
  27. // 0xa8, 0x7d, 0x7e, 0xd6, 0x43, 0xaf, 0x38, 0x59, 0xbb, 0xc0, 0x10, 0x6b, 0x90, 0xf9, 0xf6, 0x96,
  28. // 0x3b, 0xcb, 0x46, 0xdf, 0xce, 0xad, 0xbd, 0x81, 0x00, 0x81, 0x6b, 0xa6, 0x50, 0xae, 0xa7, 0x2b,
  29. // 0xd5, 0x0f, 0x11, 0x81, 0xbd, 0x6b, 0xff, 0xcb, 0x10, 0xd2, 0x73, 0xc5, 0xaa, 0x09, 0x5f, 0x13,
  30. // 0xb5, 0xde, 0x89, 0x1a, 0xa8, 0x83, 0x2a, 0x79, 0x18, 0x1a, 0x5a, 0xa3, 0x1c, 0x69, 0xe7, 0x76,
  31. // 0x3f, 0x9f, 0x78, 0x02, 0x66, 0x3e, 0x26, 0x01, 0x99, 0x1c, 0xb9, 0x75, 0x24, 0x45, 0x45, 0xc5,
  32. // 0x1e, 0x2f, 0x8e, 0xdc, 0x4b, 0xfe, 0x70, 0x67, 0x72, 0xbc, 0xaa, 0x6d, 0xb7, 0x6a, 0x48, 0xf6,
  33. // 0xb1, 0xa4, 0x91, 0x04, 0xbf, 0xa6, 0xe8, 0xa1, 0x4f, 0xf2, 0x4a, 0x3a, 0x14, 0x6f, 0x38, 0x0f,
  34. // 0xb1, 0x69, 0x13, 0xbb, 0xd3, 0xc4, 0xc4, 0x4a, 0x0d, 0x4d, 0x1f, 0x8c, 0x41, 0x11, 0xd7, 0xfc,
  35. // 0xff, 0x85, 0x0a, 0x7c, 0xfb, 0x74, 0x8f, 0x01, 0x37, 0x40, 0xec, 0x5d, 0x31, 0x6b, 0x9d, 0x6c,
  36. // 0xd4, 0xfd, 0xd2, 0xcc, 0xd5, 0x34, 0xd4, 0xb9, 0x93, 0xf3, 0xc7, 0xba, 0x3a, 0xfb, 0xb6, 0x2b,
  37. // 0x5e, 0xc1, 0x51, 0xce, 0xb6, 0xec, 0x5c, 0x80, 0x59, 0x89, 0x3c, 0x32, 0x2a, 0x51, 0xce, 0xc5,
  38. // 0x10, 0x2e, 0x71, 0xd7, 0x18, 0x86, 0x58, 0xfd, 0xc0, 0xc7, 0xaa, 0x03, 0x25, 0x7d, 0x5e, 0x98,
  39. // 0x4b, 0x24, 0x9a, 0x50, 0x83, 0x77, 0x59, 0x25, 0xb0, 0x4d, 0x28, 0xe4, 0x0a, 0x12, 0xed, 0x5b,
  40. // 0x16, 0xac, 0x17, 0x33, 0xb6, 0xe6, 0x82, 0x5c, 0x0f, 0xb4, 0x51, 0x14, 0x7d, 0xff, 0x98, 0xaf,
  41. // 0x04, 0x70, 0xd5, 0xd4, 0x64, 0xb9, 0x33, 0x81, 0x55, 0xc3, 0xad, 0x0d, 0xbb, 0x3d, 0x72, 0xb1,
  42. // };
  43. static int _file_read(int idx, download_img_info_t *dl_img_info, void *buffer, int length)
  44. {
  45. int ret = -1;
  46. FILE *fp;
  47. int fd;
  48. fp = dl_img_info->img_info[idx].fp;
  49. fd = dl_img_info->img_info[idx].fd;
  50. if (fp) {
  51. ret = fread(buffer, sizeof(uint8_t), length, fp);
  52. }
  53. if (fd > 0) {
  54. ret = read(fd, buffer, length);
  55. }
  56. if (ret < 0 || ret != length) {
  57. LOGE(TAG, "[%s, %d]fp:0x%08x, fd:%d, length:%d, ret:%d, errno:%d", __func__, __LINE__, fp, fd, length, ret, errno);
  58. return -1;
  59. }
  60. return ret;
  61. }
  62. int fota_data_verify(void)
  63. {
  64. int len;
  65. FILE *fp = NULL;
  66. download_img_info_t dl_img_info;
  67. uint8_t temp_buffer[4096] __attribute__((aligned(4)));
  68. LOGD(TAG, "come to image verify.");
  69. fp = fopen(IMGINFOFILE, "rb+");
  70. if (fp != NULL) {
  71. len = get_file_size(fp, -1);
  72. if (len < sizeof(download_img_info_t)) {
  73. LOGE(TAG, "The %s file length is wrong.", IMGINFOFILE);
  74. goto errout;
  75. }
  76. if (fread(&dl_img_info, 1, sizeof(download_img_info_t), fp) < sizeof(download_img_info_t)) {
  77. goto errout;
  78. }
  79. LOGD(TAG, "dl_img_info.image_count:%d", dl_img_info.image_count);
  80. for (int i = 0; i < dl_img_info.image_count; i++) {
  81. LOGD(TAG, "%s, size: %d", dl_img_info.img_info[i].img_name, dl_img_info.img_info[i].img_size);
  82. }
  83. LOGD(TAG, "dl_img_info.digest_type:%d", dl_img_info.digest_type);
  84. if (dl_img_info.digest_type > 0) {
  85. uint8_t hash_out[128];
  86. LOGD(TAG, "come to verify signature.");
  87. if (dl_img_info.digest_type >= DIGEST_HASH_TYPE_END) {
  88. LOGE(TAG, "the digest type %d is error", dl_img_info.digest_type);
  89. goto errout;
  90. }
  91. FILE *headerfp = fopen(IMGHEADERPATH, "rb+");
  92. if (!headerfp) {
  93. LOGE(TAG, "can't find %s.", IMGHEADERPATH);
  94. goto errout;
  95. }
  96. if (fread(temp_buffer, 1, sizeof(pack_header_v2_t), headerfp) < sizeof(pack_header_v2_t)) {
  97. LOGE(TAG, "read %s error.", IMGHEADERPATH);
  98. fclose(headerfp);
  99. goto errout;
  100. }
  101. fclose(headerfp);
  102. if (dl_img_info.digest_type == DIGEST_HASH_SHA1) {
  103. mbedtls_sha1_context ctx;
  104. mbedtls_sha1_init(&ctx);
  105. mbedtls_sha1_starts(&ctx);
  106. // SHA header first
  107. memset(((pack_header_v2_t *)temp_buffer)->signature, 0, sizeof(((pack_header_v2_t *)temp_buffer)->signature));
  108. mbedtls_sha1_update(&ctx, temp_buffer, sizeof(pack_header_v2_t));
  109. for (int i = 0; i < dl_img_info.image_count; i++) {
  110. int image_size = dl_img_info.img_info[i].img_size;
  111. int fpsize = get_file_size(dl_img_info.img_info[i].fp, dl_img_info.img_info[i].fd);
  112. LOGD(TAG, "### [fpsize:%d, image_size:%d]", fpsize, image_size);
  113. if (dl_img_info.img_info[i].fp) {
  114. if (fpsize != image_size) {
  115. LOGE(TAG, "the imagesize is not matched.[fpsize:%d, image_size:%d]", fpsize, image_size);
  116. goto errout;
  117. }
  118. } else if (dl_img_info.img_info[i].fd > 0) {
  119. // UBI Volume
  120. }
  121. if (image_size > sizeof(temp_buffer)) {
  122. if (_file_read(i, &dl_img_info, temp_buffer, sizeof(temp_buffer)) < 0) {
  123. goto errout;
  124. }
  125. mbedtls_sha1_update(&ctx, temp_buffer, sizeof(temp_buffer));
  126. image_size -= sizeof(temp_buffer);
  127. while (image_size > sizeof(temp_buffer)) {
  128. if (_file_read(i, &dl_img_info, temp_buffer, sizeof(temp_buffer)) < 0) {
  129. goto errout;
  130. }
  131. mbedtls_sha1_update(&ctx, temp_buffer, sizeof(temp_buffer));
  132. image_size -= sizeof(temp_buffer);
  133. }
  134. if (_file_read(i, &dl_img_info, temp_buffer, image_size) < 0) {
  135. goto errout;
  136. }
  137. mbedtls_sha1_update(&ctx, temp_buffer, image_size);
  138. } else {
  139. if (_file_read(i, &dl_img_info, temp_buffer, image_size) < 0) {
  140. goto errout;
  141. }
  142. mbedtls_sha1_update(&ctx, temp_buffer, image_size);
  143. }
  144. }
  145. mbedtls_sha1_finish(&ctx, hash_out);
  146. mbedtls_sha1_free(&ctx);
  147. if (mbed_sha1_rsa_verify(g_pubkey_rsa, sizeof(g_pubkey_rsa), hash_out, dl_img_info.signature) != 0) {
  148. LOGE(TAG, "sha1 rsa verify failed.");
  149. goto errout;
  150. }
  151. } else if (dl_img_info.digest_type == DIGEST_HASH_SHA256) {
  152. mbedtls_sha256_context ctx;
  153. mbedtls_sha256_init(&ctx);
  154. mbedtls_sha256_starts(&ctx, 0);
  155. // SHA header first
  156. memset(((pack_header_v2_t *)temp_buffer)->signature, 0, sizeof(((pack_header_v2_t *)temp_buffer)->signature));
  157. mbedtls_sha256_update(&ctx, temp_buffer, sizeof(pack_header_v2_t));
  158. for (int i = 0; i < dl_img_info.image_count; i++) {
  159. int image_size = dl_img_info.img_info[i].img_size;
  160. int fpsize = get_file_size(dl_img_info.img_info[i].fp, dl_img_info.img_info[i].fd);
  161. LOGD(TAG, "### [fpsize:%d, image_size:%d]", fpsize, image_size);
  162. if (dl_img_info.img_info[i].fp) {
  163. if (fpsize != image_size) {
  164. LOGE(TAG, "the imagesize is not matched.[fpsize:%d, image_size:%d]", fpsize, image_size);
  165. goto errout;
  166. }
  167. } else if (dl_img_info.img_info[i].fd > 0) {
  168. // UBI Volume
  169. }
  170. if (image_size > sizeof(temp_buffer)) {
  171. if (_file_read(i, &dl_img_info, temp_buffer, sizeof(temp_buffer)) < 0) {
  172. goto errout;
  173. }
  174. mbedtls_sha256_update(&ctx, temp_buffer, sizeof(temp_buffer));
  175. image_size -= sizeof(temp_buffer);
  176. while (image_size > sizeof(temp_buffer)) {
  177. if (_file_read(i, &dl_img_info, temp_buffer, sizeof(temp_buffer)) < 0) {
  178. goto errout;
  179. }
  180. mbedtls_sha256_update(&ctx, temp_buffer, sizeof(temp_buffer));
  181. image_size -= sizeof(temp_buffer);
  182. }
  183. if (_file_read(i, &dl_img_info, temp_buffer, image_size) < 0) {
  184. goto errout;
  185. }
  186. mbedtls_sha256_update(&ctx, temp_buffer, image_size);
  187. } else {
  188. if (_file_read(i, &dl_img_info, temp_buffer, image_size) < 0) {
  189. goto errout;
  190. }
  191. mbedtls_sha256_update(&ctx, temp_buffer, image_size);
  192. }
  193. }
  194. mbedtls_sha256_finish(&ctx, hash_out);
  195. mbedtls_sha256_free(&ctx);
  196. if (mbed_sha256_rsa_verify(g_pubkey_rsa, sizeof(g_pubkey_rsa), hash_out, dl_img_info.signature) != 0) {
  197. LOGE(TAG, "sha256 rsa verify failed.");
  198. goto errout;
  199. }
  200. } else {
  201. LOGE(TAG, "digest type e[%d]", dl_img_info.digest_type);
  202. goto errout;
  203. }
  204. } else {
  205. uint8_t md5_out[16];
  206. mbedtls_md5_context md5;
  207. LOGD(TAG, "come to MD5 verify.");
  208. mbedtls_md5_init(&md5);
  209. mbedtls_md5_starts(&md5);
  210. for (int i = 0; i < dl_img_info.image_count; i++) {
  211. int image_size = dl_img_info.img_info[i].img_size;
  212. int fpsize = get_file_size(dl_img_info.img_info[i].fp, dl_img_info.img_info[i].fd);
  213. LOGD(TAG, "### [fpsize:%d, image_size:%d]", fpsize, image_size);
  214. if (dl_img_info.img_info[i].fp) {
  215. if (fpsize != image_size) {
  216. LOGE(TAG, "the imagesize is not matched.[fpsize:%d, image_size:%d]", fpsize, image_size);
  217. goto errout;
  218. }
  219. } else if (dl_img_info.img_info[i].fd > 0) {
  220. // UBI Volume
  221. }
  222. if (image_size > sizeof(temp_buffer)) {
  223. if (_file_read(i, &dl_img_info, temp_buffer, sizeof(temp_buffer)) < 0) {
  224. goto errout;
  225. }
  226. mbedtls_md5_update(&md5, temp_buffer, sizeof(temp_buffer));
  227. image_size -= sizeof(temp_buffer);
  228. while (image_size > sizeof(temp_buffer)) {
  229. if (_file_read(i, &dl_img_info, temp_buffer, sizeof(temp_buffer)) < 0) {
  230. goto errout;
  231. }
  232. mbedtls_md5_update(&md5, temp_buffer, sizeof(temp_buffer));
  233. image_size -= sizeof(temp_buffer);
  234. }
  235. if (_file_read(i, &dl_img_info, temp_buffer, image_size) < 0) {
  236. goto errout;
  237. }
  238. mbedtls_md5_update(&md5, temp_buffer, image_size);
  239. } else {
  240. if (_file_read(i, &dl_img_info, temp_buffer, image_size) < 0) {
  241. goto errout;
  242. }
  243. mbedtls_md5_update(&md5, temp_buffer, image_size);
  244. }
  245. }
  246. mbedtls_md5_finish(&md5, md5_out);
  247. mbedtls_md5_free(&md5);
  248. if (memcmp(dl_img_info.md5sum, md5_out, 16) != 0) {
  249. printf("origin md5sum:\n");
  250. for (int kk = 0; kk < 16; kk++) {
  251. printf("0x%02x ", dl_img_info.md5sum[kk]);
  252. }
  253. printf("\r\n");
  254. printf("calculate md5sum:\n");
  255. for (int kk = 0; kk < 16; kk++) {
  256. printf("0x%02x ", md5_out[kk]);
  257. }
  258. printf("\r\n");
  259. LOGE(TAG, "image md5sum verify failed.");
  260. goto errout;
  261. }
  262. }
  263. LOGD(TAG, "image verify ok.");
  264. fclose(fp);
  265. return 0;
  266. }
  267. errout:
  268. LOGD(TAG, "image verify error.");
  269. if (fp) {
  270. fclose(fp);
  271. for (int i = 0; i < dl_img_info.image_count; i++) {
  272. if (dl_img_info.img_info[i].fp) {
  273. fclose(dl_img_info.img_info[i].fp);
  274. LOGD(TAG, "close 0x%08x", dl_img_info.img_info[i].fp);
  275. }
  276. if (dl_img_info.img_info[i].fd > 0) {
  277. close(dl_img_info.img_info[i].fd);
  278. LOGD(TAG, "close %d",dl_img_info.img_info[i].fd);
  279. }
  280. }
  281. }
  282. return -1;
  283. }