util_st.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _UTIL_ST_H_
  32. #define _UTIL_ST_H_
  33. #include "se_cdefs.h"
  34. #include "se_memcpy.h"
  35. #include <stdio.h>
  36. #define USAGE_STRING \
  37. "\nUsage: sgx_sign <commands> [options] file...\n"\
  38. "Commands:\n"\
  39. " sign Sign the enclave using the private key\n"\
  40. " gendata Generate enclave signing material to be signed\n"\
  41. " catsig Generate the signed enclave with the input signature file, the\n"\
  42. " public key and the enclave signing material\n"\
  43. " dump Dump metadata information for a signed enclave file\n"\
  44. "Options:\n"\
  45. " -enclave Specify the enclave file to be signed or already signed\n"\
  46. " It is a required option for the four commands\n"\
  47. " -key Specify the key file\n"\
  48. " It is a required option for \"sign\" and \"catsig\"\n"\
  49. " -config Specify the configuration for the enclave\n"\
  50. " -out Specify the output file\n"\
  51. " It is a required option for \"sign\", \"gendata\" and \"catsig\"\n"\
  52. " -sig Specify the signature file for the enclave signing material\n" \
  53. " It is a required option for \"catsig\"\n"\
  54. " -unsigned Specify the enclave signing material generated by \"gendata\"\n" \
  55. " It is a required option for \"catsig\"\n" \
  56. " -dumpfile Specify a file to dump metadata information (text format)\n" \
  57. " It is a required option for \"dump\"\n" \
  58. " -cssfile Specify a file to dump the enclave SIGSTRUCT information (binary format)\n" \
  59. " -ignore-rel-error By default, sgx_sign provides an error for enclaves with\n" \
  60. " text relocations. You can ignore the error and continue signing\n" \
  61. " by providing this option. But it is recommended you eliminate the\n" \
  62. " text relocations instead of bypassing the error with this option.\n" \
  63. " -ignore-init-sec-error By default, sgx_sign provides an error for enclaves with .init section.\n" \
  64. " You can ignore the error and continue signing by providing this option.\n" \
  65. " But it is recommended you eliminate the section instead of bypassing\n" \
  66. " the error with this option.\n\n" \
  67. "Run \"sgx_sign -help\" to get this help and exit.\n" \
  68. "Run \"sgx_sign -version\" to output version information and exit.\n\n"
  69. #define VERSION_STRING \
  70. "\nThis is sgx_sign from Intel(R) Software Guard Extensions, version %s for Linux.\n\n%s\n\n" \
  71. "BSD License. The License file could be found under the Intel(R) SGX SDK package.\n\n"
  72. // General error message
  73. #define OVERALL_ERROR "Error happened while signing the enclave.\n"
  74. #define KEY_FORMAT_ERROR "Key file format is not correct.\n"
  75. #define XML_FORMAT_ERROR "Configuration file format is not correct.\n"
  76. #define XML_NOT_FOUND_ERROR "Configuration file \"%s\" is not found.\n"
  77. #define NO_MEMORY_ERROR "Out of memory.\n"
  78. #define OPEN_FILE_ERROR "Failed to open file \"%s\".\n"
  79. #define READ_FILE_ERROR "Failed to read file \"%s\".\n"
  80. #define WRITE_FILE_ERROR "Failed to write file \"%s\".\n"
  81. // error message for measure_enclave()
  82. #define OUT_OF_EPC_ERROR "The required memory is too large. Please check TCSNum/HeapMaxSize/StackMaxSize.\n"
  83. #define META_VERSION_ERROR "Metadata version is mismatched between uRTS and sgx_sign.\n"
  84. #define INVALID_ENCLAVE_ERROR "The input enclave file is not correct.\n"
  85. #define REQUIRED_ENCLAVE_SIZE "The required memory is %lluB.\n"
  86. #define TEXT_REL_ERROR "The enclave image has text relocations.\n"
  87. #define INIT_SEC_ERROR "The enclave image has .init section.\n"
  88. // error message for fill_enclave_css()
  89. #define UNSIGNED_FILE_ERROR "The unsigned file \"%s\" is not correct.\n"
  90. #define UNSIGNED_FILE_XML_MISMATCH "The unsigned file content doesn't match the configuration file.\n"
  91. // error message for create_signature()
  92. #define SIG_FILE_ERROR "The signature file \"%s\" is not correct.\n"
  93. // error message for cmdline_parse()
  94. #define LACK_PARA_ERROR "Lack of parameters.\n"
  95. #define UNREC_CMD_ERROR "Cannot recognize the command \"%s\".\nCommand \"sign/gendata/catsig\" is required.\n"
  96. #define REPEAT_OPTION_ERROR "Repeatly specified \"%s\" option.\n"
  97. #define INVALID_FILE_NAME_ERROR "The File name is not correct for \"%s\" option.\n"
  98. #define LACK_REQUIRED_OPTION_ERROR "Option \"%s\" is required for the command \"%s\".\n"
  99. #define GIVE_INVALID_OPTION_ERROR "Option \"%s\" is invalid for the command \"%s\".\n"
  100. #define UNREC_OPTION_ERROR "Cannot recognize the option \"%s\".\n"
  101. // error message for generate_output()
  102. #define LACK_PRI_KEY_ERROR "Private key is required for the \"sign\" command.\n"
  103. #define LACK_PUB_KEY_ERROR "Public key is required for the \"catsig\" command.\n"
  104. // error message for main()
  105. #define ENCLAVE_ALREADY_SIGNED_ERROR "The enclave has been signed already.\n"
  106. #define DUMP_METADATA_ERROR "Failed to dump metadata info to file \"%s\".\n."
  107. #define SUCCESS_EXIT "Succeed.\n"
  108. // error message for traverser_parameter()
  109. #define LACK_VALUE_FOR_ELEMENT_ERROR "No value for the element \"%s\".\n"
  110. #define INVALID_VALUE_FOR_ELEMENT_ERROR "Invalid value for the element\"%s\".\n"
  111. #define UNREC_ELEMENT_ERROR "Element is not recognized - \"%s\".\n"
  112. #define REPEATED_DEFINE_ERROR "Defined \"%s\" too many times.\n"
  113. #define VALUE_OUT_OF_RANGE_ERROR "The value of \"%s\" is out of range.\n"
  114. // error message for modify_metadata()
  115. #define SET_STACK_SIZE_ERROR "Stack size setting is not correct.\n"
  116. #define SET_HEAP_SIZE_ALIGN_ERROR "Heap size setting is not correct: size is not page aligned.\n"
  117. #define SET_HEAP_SIZE_INIT_MAX_ERROR "Heap size setting is not correct: init value should not be larger than max value.\n"
  118. #define SET_HEAP_SIZE_INIT_MIN_ERROR "Heap size setting is not correct: min value should not be larger than init value.\n"
  119. #define SET_HEAP_SIZE_MAX_MIN_ERROR "Heap size setting is not correct: max value should not be smaller than min value.\n"
  120. #define SET_HW_LE_ERROR "Conflicting setting between the 'HW' and 'LaunchKey'.\n"
  121. #define SET_TCS_MAX_NUM_ERROR "Maximum number of TCS is not correct.\n"
  122. #define SET_TCS_MIN_POOL_ERROR "Minimum number of TCS Pool is not correct.\n"
  123. // error message for parse_key_file()
  124. #define INVALID_EXPONENT_ERROR "The exponent of the input key is invalid. Only '3' is accepted as the exponent.\n"
  125. #define INVALID_KEYSIZE_ERROR "The required key size must be 3072 bits.\n"
  126. #include <stdint.h>
  127. #include <iostream>
  128. typedef enum _command_mode_t
  129. {
  130. SIGN = 0,
  131. GENDATA,
  132. CATSIG,
  133. DUMP
  134. } command_mode_t;
  135. #ifdef __cplusplus
  136. extern "C" {
  137. #endif
  138. size_t get_file_size(const char *filename);
  139. bool read_file_to_buf(const char *filename, uint8_t *buffer, size_t bsize);
  140. bool write_data_to_file(const char *filename, std::ios_base::openmode mode, uint8_t *buf, size_t bsize, long offset = 0);
  141. bool copy_file(const char *source_path, const char *dest_path);
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif