network_encoding_wrapper.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "network_encoding_wrapper.h"
  32. #include "oal/oal.h"
  33. #include "aesm_encode.h"
  34. ae_error_t AESMNetworkEncoding::aesm_send_recv_msg(const char *url, const uint8_t * msg, uint32_t msg_size, uint8_t* &resp_msg, uint32_t& resp_size, http_methods_t type, bool is_ocsp)
  35. {
  36. return ::aesm_network_send_receive(url, msg, msg_size, &resp_msg, &resp_size, type, is_ocsp);
  37. }
  38. ae_error_t AESMNetworkEncoding::aesm_send_recv_msg_encoding_internal(const char *url, const uint8_t * msg, uint32_t msg_size, uint8_t* &resp, uint32_t& resp_size)
  39. {
  40. resp = NULL;
  41. resp_size = 0;
  42. ae_error_t ae_ret = AE_SUCCESS;
  43. uint8_t *encode_msg = NULL;
  44. uint32_t encoding_size = get_request_encoding_length(msg);
  45. uint8_t *recv_msg = NULL;
  46. uint32_t decode_buffer_size = 0;
  47. uint32_t recv_size = 0;
  48. if(encoding_size == 0){
  49. AESM_DBG_WARN("invalid msg_size 0 to send to url:%s", url);
  50. ae_ret = AE_FAILURE;
  51. goto ret_point;
  52. }
  53. encode_msg = reinterpret_cast<uint8_t *>(malloc(encoding_size));
  54. if(encode_msg == NULL){
  55. AESM_DBG_ERROR("malloc failed");
  56. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  57. goto ret_point;
  58. }
  59. #ifdef DBG_LOG
  60. char dbg_hex_string[256];
  61. aesm_dbg_format_hex(msg, msg_size, dbg_hex_string, 256);
  62. AESM_DBG_TRACE("send msg \"%s\" to server:%s",dbg_hex_string, url);
  63. #endif
  64. memset(encode_msg, 0, encoding_size);
  65. if(!encode_request(msg, msg_size, encode_msg, &encoding_size)){
  66. AESM_DBG_ERROR("message encoding error, msg size %d",msg_size);
  67. ae_ret = PVE_UNEXPECTED_ERROR;
  68. goto ret_point;
  69. }
  70. AESM_DBG_TRACE("encoded msg %.*s",encoding_size, encode_msg);
  71. ae_ret = aesm_network_send_receive(url, encode_msg, encoding_size,
  72. &recv_msg, &recv_size, POST, false);
  73. if(ae_ret != AE_SUCCESS ){
  74. AESM_DBG_ERROR("fail to send encoded msg (size=%d) to url:%s",encoding_size, url);
  75. goto ret_point;
  76. }
  77. if(recv_msg == NULL){
  78. AESM_DBG_ERROR("recv NULL message from backend server");
  79. ae_ret = PVE_UNEXPECTED_ERROR;
  80. goto ret_point;
  81. }
  82. AESM_DBG_TRACE("response msg %.*s", recv_size, recv_msg);
  83. decode_buffer_size= get_response_decoding_length(recv_size);
  84. if(decode_buffer_size == 0){
  85. AESM_DBG_ERROR("response 0 length message from backend server:%s",url);
  86. ae_ret = PVE_UNEXPECTED_ERROR;
  87. goto ret_point;
  88. }
  89. AESM_DBG_TRACE("Succ recv msg:%.*s", recv_size, recv_msg);
  90. resp = reinterpret_cast<uint8_t *>(malloc(decode_buffer_size));
  91. if(resp == NULL){
  92. AESM_DBG_ERROR("malloc error");
  93. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  94. goto ret_point;
  95. }
  96. memset(resp, 0, decode_buffer_size);
  97. if(!decode_response(recv_msg,recv_size, resp, &decode_buffer_size)){
  98. AESM_DBG_WARN("fail to decode message from server");
  99. ae_ret = PVE_MSG_ERROR;
  100. goto ret_point;
  101. }
  102. #ifdef DBG_LOG
  103. aesm_dbg_format_hex(resp, decode_buffer_size, dbg_hex_string, 256);
  104. AESM_DBG_TRACE("succ decode msg \"%s\" ",dbg_hex_string);
  105. #endif
  106. resp_size = decode_buffer_size;
  107. ret_point:
  108. aesm_free_network_response_buffer(recv_msg);
  109. if(ae_ret != AE_SUCCESS){
  110. if(resp!=NULL)free(resp);
  111. resp = NULL;
  112. }
  113. if(encode_msg!=NULL)free(encode_msg);
  114. return ae_ret;
  115. }
  116. void AESMNetworkEncoding::aesm_free_response_msg(uint8_t *resp)
  117. {
  118. if(resp!=NULL){
  119. free(resp);
  120. }
  121. }
  122. ae_error_t AESMNetworkEncoding::aesm_send_recv_msg_encoding(const char *url, const uint8_t * msg, uint32_t msg_size, uint8_t* &resp, uint32_t& resp_size)
  123. {
  124. resp = NULL;
  125. resp_size = 0;
  126. AESM_DBG_TRACE("send msg to url %s",url);
  127. //call global function to encoding message, send/receive message via HTTP POST and decode response message
  128. return aesm_send_recv_msg_encoding_internal(url, msg, msg_size, resp, resp_size);
  129. }