se_sig_rl.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "stdint.h"
  32. #include "se_sig_rl.h"
  33. #include "byte_order.h"
  34. #include "sgx_quote.h"
  35. #include "se_quote_internal.h"
  36. uint64_t se_get_sig_rl_size(const se_sig_rl_t *p_sig_rl)
  37. {
  38. uint64_t n2 = (p_sig_rl) ? lv_ntohl(p_sig_rl->sig_rl.n2) : 0;
  39. return(sizeof(se_sig_rl_t) - sizeof(p_sig_rl->sig_rl.bk[0])
  40. + n2 * sizeof(p_sig_rl->sig_rl.bk[0]) + 2 * SE_ECDSA_SIGN_SIZE);
  41. }
  42. sgx_status_t sgx_calc_quote_size(const uint8_t *sig_rl, uint32_t sig_rl_size, uint32_t* p_quote_size)
  43. {
  44. if (!p_quote_size)
  45. return SGX_ERROR_INVALID_PARAMETER;
  46. uint64_t quote_size = 0;
  47. uint64_t sign_size = 0;
  48. uint64_t n2 = 0;
  49. const se_sig_rl_t *p_sig_rl = reinterpret_cast<const se_sig_rl_t *>(sig_rl);
  50. if (sig_rl)
  51. {
  52. if (sig_rl_size < sizeof(se_sig_rl_t) ||
  53. se_get_sig_rl_size(p_sig_rl) != sig_rl_size)
  54. {
  55. return SGX_ERROR_INVALID_PARAMETER;
  56. }
  57. if (p_sig_rl->protocol_version != SE_EPID_SIG_RL_VERSION
  58. || p_sig_rl->epid_identifier != SE_EPID_SIG_RL_ID)
  59. {
  60. return SGX_ERROR_INVALID_PARAMETER;
  61. }
  62. }
  63. else if (sig_rl_size != 0)
  64. return SGX_ERROR_INVALID_PARAMETER;
  65. n2 = (sig_rl) ? lv_ntohl(p_sig_rl->sig_rl.n2) : 0;
  66. sign_size = sizeof(EpidSignature) - sizeof(NrProof) + n2 * sizeof(NrProof);
  67. quote_size = SE_QUOTE_LENGTH_WITHOUT_SIG + sign_size;
  68. if (quote_size >= 1ull << 32)
  69. {
  70. return SGX_ERROR_INVALID_PARAMETER;
  71. }
  72. *p_quote_size = static_cast<uint32_t>(quote_size);
  73. return SGX_SUCCESS;
  74. }
  75. sgx_status_t sgx_get_quote_size(const uint8_t *sig_rl, uint32_t* p_quote_size)
  76. {
  77. if(!p_quote_size)
  78. return SGX_ERROR_INVALID_PARAMETER;
  79. uint64_t quote_size = 0;
  80. uint64_t sign_size = 0;
  81. uint64_t n2 = 0;
  82. const se_sig_rl_t *p_sig_rl = reinterpret_cast<const se_sig_rl_t *>(sig_rl);
  83. if(sig_rl)
  84. {
  85. if(p_sig_rl->protocol_version != SE_EPID_SIG_RL_VERSION
  86. || p_sig_rl->epid_identifier != SE_EPID_SIG_RL_ID)
  87. {
  88. return SGX_ERROR_INVALID_PARAMETER;
  89. }
  90. }
  91. n2 = (sig_rl) ? lv_ntohl(p_sig_rl->sig_rl.n2) : 0;
  92. sign_size = sizeof(EpidSignature) - sizeof(NrProof) + n2*sizeof(NrProof);
  93. quote_size = SE_QUOTE_LENGTH_WITHOUT_SIG + sign_size;
  94. if (quote_size >= 1ull<<32)
  95. {
  96. return SGX_ERROR_INVALID_PARAMETER;
  97. }
  98. *p_quote_size = static_cast<uint32_t>(quote_size);
  99. return SGX_SUCCESS;
  100. }