main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "lib_rats_wrapper.h"
  10. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  11. /**
  12. * hex_dump
  13. *
  14. * @brief dump data in hex format
  15. *
  16. * @param title: Title
  17. * @param buf: User buffer
  18. * @param size: Dump data size
  19. * @param number: The number of outputs per line
  20. *
  21. * @return void
  22. */
  23. void
  24. hex_dump(const char *title, const uint8_t *buf, uint32_t size, uint32_t number)
  25. {
  26. int i, j;
  27. if (title) {
  28. printf("\n\t%s:\n\n", title);
  29. }
  30. for (i = 0; i < size; i += number) {
  31. printf("%08X: ", i);
  32. for (j = 0; j < number; j++) {
  33. if (j % 8 == 0) {
  34. printf(" ");
  35. }
  36. if (i + j < size)
  37. printf("%02X ", buf[i + j]);
  38. else
  39. printf(" ");
  40. }
  41. printf(" ");
  42. for (j = 0; j < number; j++) {
  43. if (i + j < size) {
  44. printf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  45. }
  46. }
  47. printf("\n");
  48. }
  49. }
  50. int
  51. main(int argc, char **argv)
  52. {
  53. int ret_code = -1;
  54. char *evidence_json = NULL;
  55. // Generate user_data by SHA256 buffer and the wasm module.
  56. // user_data = SHA256(sha256_wasm_module || buffer)
  57. const char *buffer = "This is a sample.";
  58. // If you want to declare the evidence of type rats_sgx_evidence_t on the
  59. // stack, you should modify the stack size of the CMAKE_EXE_LINKER_FLAGS in
  60. // CMakeLists.txt to 51200 at least.
  61. rats_sgx_evidence_t *evidence =
  62. (rats_sgx_evidence_t *)malloc(sizeof(rats_sgx_evidence_t));
  63. if (!evidence) {
  64. printf("ERROR: No memory to allocate.\n");
  65. goto err;
  66. }
  67. int rats_err = librats_collect(&evidence_json, buffer);
  68. if (rats_err != 0) {
  69. printf("ERROR: Collect evidence failed, error code: %#x\n", rats_err);
  70. goto err;
  71. }
  72. if (librats_parse_evidence(evidence_json, evidence) != 0) {
  73. printf("ERROR: Parse evidence failed.\n");
  74. goto err;
  75. }
  76. // You could use these parameters for further verification.
  77. hex_dump("Quote", evidence->quote, evidence->quote_size, 32);
  78. hex_dump("User Data", evidence->user_data, SGX_USER_DATA_SIZE, 32);
  79. hex_dump("MRENCLAVE", evidence->mr_enclave, SGX_MEASUREMENT_SIZE, 32);
  80. hex_dump("MRSIGNER", evidence->mr_signer, SGX_MEASUREMENT_SIZE, 32);
  81. printf("\n\tProduct ID:\t\t\t\t%u\n", evidence->product_id);
  82. printf("\tSecurity Version:\t\t\t%u\n", evidence->security_version);
  83. printf("\tAttributes.flags:\t\t\t%llu\n", evidence->att_flags);
  84. printf("\tAttributes.flags[INITTED]:\t\t%d\n",
  85. (evidence->att_flags & SGX_FLAGS_INITTED) != 0);
  86. printf("\tAttributes.flags[DEBUG]:\t\t%d\n",
  87. (evidence->att_flags & SGX_FLAGS_DEBUG) != 0);
  88. printf("\tAttributes.flags[MODE64BIT]:\t\t%d\n",
  89. (evidence->att_flags & SGX_FLAGS_MODE64BIT) != 0);
  90. printf("\tAttributes.flags[PROVISION_KEY]:\t%d\n",
  91. (evidence->att_flags & SGX_FLAGS_PROVISION_KEY) != 0);
  92. printf("\tAttributes.flags[EINITTOKEN_KEY]:\t%d\n",
  93. (evidence->att_flags & SGX_FLAGS_EINITTOKEN_KEY) != 0);
  94. printf("\tAttributes.flags[KSS]:\t\t\t%d\n",
  95. (evidence->att_flags & SGX_FLAGS_KSS) != 0);
  96. printf("\tAttributes.flags[AEX_NOTIFY]:\t\t%d\n",
  97. (evidence->att_flags & SGX_FLAGS_AEX_NOTIFY) != 0);
  98. printf("\tAttribute.xfrm:\t\t\t\t%llu\n", evidence->att_xfrm);
  99. rats_err = librats_verify((const char *)evidence_json, evidence->user_data);
  100. if (rats_err != 0) {
  101. printf("ERROR: Evidence is not trusted, error code: %#x.\n", rats_err);
  102. goto err;
  103. }
  104. ret_code = 0;
  105. printf("Evidence is trusted.\n");
  106. err:
  107. if (evidence_json) {
  108. librats_dispose_evidence_json(evidence_json);
  109. }
  110. if (evidence) {
  111. free(evidence);
  112. }
  113. return ret_code;
  114. }