TimeBasedDRM.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2011-2017 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. #include "TimeBasedDRM.h"
  32. #include "sgx_urts.h"
  33. #include "sgx_uae_service.h"
  34. #include "DRM_enclave_u.h"
  35. #include <iostream>
  36. using namespace std;
  37. #define ENCLAVE_NAME "DRM_enclave.signed.so"
  38. TimeBasedDRM::TimeBasedDRM(void): enclave_id(0)
  39. {
  40. int updated = 0;
  41. sgx_status_t sgx_ret = SGX_ERROR_UNEXPECTED;
  42. sgx_ret = sgx_create_enclave(ENCLAVE_NAME, SGX_DEBUG_FLAG,
  43. &launch_token, &updated, &enclave_id, NULL);
  44. if (sgx_ret)
  45. {
  46. cerr<<"cannot create enclave, error code = 0x"<< hex<< sgx_ret <<endl;
  47. }
  48. }
  49. TimeBasedDRM::~TimeBasedDRM(void)
  50. {
  51. if(enclave_id)
  52. sgx_destroy_enclave(enclave_id);
  53. }
  54. uint32_t TimeBasedDRM:: init(uint8_t* stored_time_based_policy)
  55. {
  56. sgx_status_t sgx_ret = SGX_ERROR_UNEXPECTED;
  57. sgx_ps_cap_t ps_cap;
  58. memset(&ps_cap, 0, sizeof(sgx_ps_cap_t));
  59. sgx_ret = sgx_get_ps_cap(&ps_cap);
  60. if (sgx_ret)
  61. {
  62. cerr<<"cannot get platform service capability, error code = 0x"<< hex<<
  63. sgx_ret <<endl;
  64. return sgx_ret;
  65. }
  66. if (!SGX_IS_TRUSTED_TIME_AVAILABLE(ps_cap))
  67. {
  68. cerr<<"trusted time is not supported"<<endl;
  69. return SGX_ERROR_SERVICE_UNAVAILABLE;
  70. }
  71. uint32_t enclave_ret = 0;
  72. sgx_ret = create_time_based_policy(enclave_id, &enclave_ret,
  73. (uint8_t *)stored_time_based_policy, time_based_policy_length);
  74. if (sgx_ret)
  75. {
  76. cerr<<"call create_time_based_policy fail, error code = 0x"<< hex<<
  77. sgx_ret <<endl;
  78. return sgx_ret;
  79. }
  80. if (enclave_ret)
  81. {
  82. cerr<<"cannot create_time_based_policy, function return fail, error code = 0x"
  83. << hex<< enclave_ret <<endl;
  84. return enclave_ret;
  85. }
  86. return 0;
  87. }
  88. uint32_t TimeBasedDRM:: init()
  89. {
  90. return init(time_based_policy);
  91. }
  92. uint32_t TimeBasedDRM::perform_function(uint8_t* stored_time_based_policy)
  93. {
  94. sgx_status_t sgx_ret = SGX_ERROR_UNEXPECTED;
  95. uint32_t enclave_ret = 0;
  96. sgx_ret = perform_time_based_policy(enclave_id, &enclave_ret,
  97. stored_time_based_policy, time_based_policy_length);
  98. if (sgx_ret)
  99. {
  100. cerr<<"call perform_time_based_policy fail, error code = 0x"<< hex<<
  101. sgx_ret <<endl;
  102. return sgx_ret;
  103. }
  104. if (enclave_ret)
  105. {
  106. cerr<<"cannot perform_time_based_policy, function return fail, error code = 0x"
  107. << hex<< enclave_ret <<endl;
  108. return enclave_ret;
  109. }
  110. return 0;
  111. }
  112. uint32_t TimeBasedDRM::perform_function()
  113. {
  114. return perform_function(time_based_policy);
  115. }