lib_rats_wrapper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2022 Intel Corporation
  3. * Copyright (c) 2020-2021 Alibaba Cloud
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <librats/api.h>
  10. #include <string.h>
  11. #include <openssl/sha.h>
  12. #include "sgx_quote_3.h"
  13. #include "wasm_export.h"
  14. #include "bh_common.h"
  15. #include "lib_rats_common.h"
  16. static int
  17. librats_collect_wrapper(wasm_exec_env_t exec_env, char **evidence_json,
  18. const char *buffer, uint32_t buffer_size)
  19. {
  20. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  21. wasm_module_t module = wasm_runtime_get_module(module_inst);
  22. char *wasm_module_hash = wasm_runtime_get_module_hash(module);
  23. char *json, *str_ret;
  24. uint32_t str_ret_offset;
  25. uint8_t final_hash[SHA256_DIGEST_LENGTH];
  26. SHA256_CTX sha256;
  27. SHA256_Init(&sha256);
  28. SHA256_Update(&sha256, wasm_module_hash, SHA256_DIGEST_LENGTH);
  29. if (buffer != NULL)
  30. SHA256_Update(&sha256, buffer, buffer_size);
  31. SHA256_Final(final_hash, &sha256);
  32. int ret_code = librats_collect_evidence_to_json(final_hash, &json);
  33. if (ret_code != 0) {
  34. return ret_code;
  35. }
  36. uint32_t json_size = strlen(json) + 1;
  37. str_ret_offset = module_malloc(json_size, (void **)&str_ret);
  38. if (!str_ret_offset) {
  39. free(json);
  40. return (int)RATS_ATTESTER_ERR_NO_MEM;
  41. }
  42. bh_memcpy_s(str_ret, json_size, json, json_size);
  43. *((int *)evidence_json) = str_ret_offset;
  44. free(json);
  45. return 0;
  46. }
  47. static int
  48. librats_verify_wrapper(wasm_exec_env_t exec_env, const char *evidence_json,
  49. uint32_t evidence_size, const uint8_t *hash,
  50. uint32_t hash_size)
  51. {
  52. return librats_verify_evidence_from_json(evidence_json, hash);
  53. }
  54. static int
  55. librats_parse_evidence_wrapper(wasm_exec_env_t exec_env,
  56. const char *evidence_json, uint32_t json_size,
  57. rats_sgx_evidence_t *evidence,
  58. uint32_t evidence_size)
  59. {
  60. attestation_evidence_t att_ev;
  61. if (get_evidence_from_json(evidence_json, &att_ev) != 0) {
  62. return -1;
  63. }
  64. // Only supports parsing sgx evidence currently
  65. if (strcmp(att_ev.type, "sgx_ecdsa") != 0) {
  66. return -1;
  67. }
  68. sgx_quote3_t *quote_ptr = (sgx_quote3_t *)att_ev.ecdsa.quote;
  69. bh_memcpy_s(evidence->quote, att_ev.ecdsa.quote_len, att_ev.ecdsa.quote,
  70. att_ev.ecdsa.quote_len);
  71. evidence->quote_size = att_ev.ecdsa.quote_len;
  72. bh_memcpy_s(evidence->user_data, SGX_REPORT_DATA_SIZE,
  73. quote_ptr->report_body.report_data.d, SGX_REPORT_DATA_SIZE);
  74. bh_memcpy_s(evidence->mr_enclave, sizeof(sgx_measurement_t),
  75. quote_ptr->report_body.mr_enclave.m, sizeof(sgx_measurement_t));
  76. bh_memcpy_s(evidence->mr_signer, sizeof(sgx_measurement_t),
  77. quote_ptr->report_body.mr_signer.m, sizeof(sgx_measurement_t));
  78. evidence->product_id = quote_ptr->report_body.isv_prod_id;
  79. evidence->security_version = quote_ptr->report_body.isv_svn;
  80. evidence->att_flags = quote_ptr->report_body.attributes.flags;
  81. evidence->att_xfrm = quote_ptr->report_body.attributes.flags;
  82. return 0;
  83. }
  84. /* clang-format off */
  85. #define REG_NATIVE_FUNC(func_name, signature) \
  86. { #func_name, func_name##_wrapper, signature, NULL }
  87. /* clang-format on */
  88. static NativeSymbol native_symbols_lib_rats[] = {
  89. REG_NATIVE_FUNC(librats_collect, "(**~)i"),
  90. REG_NATIVE_FUNC(librats_verify, "(*~*~)i"),
  91. REG_NATIVE_FUNC(librats_parse_evidence, "(*~*~)i")
  92. };
  93. uint32_t
  94. get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis)
  95. {
  96. *p_lib_rats_apis = native_symbols_lib_rats;
  97. return sizeof(native_symbols_lib_rats) / sizeof(NativeSymbol);
  98. }