aesm_encode.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2011-2016 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 "aesm_encode.h"
  32. #include "se_memcpy.h"
  33. #include "AEClass.h"
  34. #include "PVEClass.h"
  35. #include "tlv_common.h"
  36. #include <openssl/evp.h>
  37. #include <openssl/bio.h>
  38. #include <openssl/buffer.h>
  39. #include <openssl/x509v3.h>
  40. #include <list>
  41. /**
  42. * Method converts byte containing value from 0x00-0x0F into its corresponding ASCII code,
  43. * e.g. converts 0x00 to '0', 0x0A to 'A'.
  44. * Note: This is mainly a helper method for internal use in byte_array_to_hex_string().
  45. *
  46. * @param in byte to be converted (allowed values: 0x00-0x0F)
  47. *
  48. * @return ASCII code representation of the byte or 0 if method failed (e.g input value was not in provided range).
  49. */
  50. static uint8_t convert_value_to_ascii(uint8_t in)
  51. {
  52. if(in <= 0x09)
  53. {
  54. return (uint8_t)(in + '0');
  55. }
  56. else if(in <= 0x0F)
  57. {
  58. return (uint8_t)(in - 10 + 'A');
  59. }
  60. return 0;
  61. }
  62. /**
  63. * Method converts char containing ASCII code into its corresponding value,
  64. * e.g. converts '0' to 0x00, 'A' to 0x0A.
  65. *
  66. * @param in char containing ASCII code (allowed values: '0-9', 'a-f', 'A-F')
  67. * @param val output parameter containing converted value, if method suceeds.
  68. *
  69. * @return true if conversion succeeds, false otherwise
  70. */
  71. static bool convert_ascii_to_value(uint8_t in, uint8_t& val)
  72. {
  73. if(in >= '0' && in <= '9')
  74. {
  75. val = static_cast<uint8_t>(in - '0');
  76. }
  77. else if(in >= 'A' && in <= 'F')
  78. {
  79. val = static_cast<uint8_t>(in - 'A'+10);
  80. }
  81. else if(in >= 'a' && in <= 'f')
  82. {
  83. val = static_cast<uint8_t>(in - 'a'+10);
  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. return true;
  90. }
  91. //Function to do HEX encoding of array of bytes
  92. //@param in_buf, bytes array whose length is in_size
  93. // out_buf, output the HEX encoding of in_buf on success.
  94. //@return true on success and false on error
  95. //The out_size must always be 2*in_size since each byte into encoded by 2 characters
  96. static bool byte_array_to_hex_string(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size)
  97. {
  98. if(in_buf==NULL||out_buf==NULL|| out_size!=in_size*2 )return false;
  99. for(uint32_t i=0; i< in_size; i++)
  100. {
  101. *out_buf++ = convert_value_to_ascii( static_cast<uint8_t>(*in_buf >> 4));
  102. *out_buf++ = convert_value_to_ascii( static_cast<uint8_t>(*in_buf & 0xf));
  103. in_buf++;
  104. }
  105. return true;
  106. }
  107. //Function to do HEX decoding
  108. //@param in_buf, character strings which are HEX encoding of a byte array
  109. // out_buf, output the decode byte array on success
  110. //@return true on success and false on error
  111. //The in_size must be even number and equals 2*out_size
  112. static bool hex_string_to_byte_array(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size)
  113. {
  114. if(in_buf==NULL||out_buf==NULL||out_size*2!=in_size)return false;
  115. for(uint32_t i=0;i<out_size;i++)
  116. {
  117. uint8_t value_first, value_second;
  118. if(!convert_ascii_to_value(in_buf[i*2], value_first))
  119. return false;
  120. if(!convert_ascii_to_value(in_buf[i*2+1], value_second))
  121. return false;
  122. out_buf[i] = static_cast<uint8_t>(value_second+ (value_first<<4));
  123. }
  124. return true;
  125. }
  126. //Function to use openssl to do BASE64 encoding
  127. static bool base_64_encode(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t *out_size)
  128. {
  129. BIO* bioMem = NULL;
  130. bool ret = false;
  131. BIO *bio64 = NULL;
  132. BIO_METHOD *bm = BIO_f_base64();
  133. if(bm == NULL)
  134. goto ret_point;
  135. bio64 = BIO_new(bm);
  136. if(bio64 == NULL)
  137. goto ret_point;
  138. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  139. bm = BIO_s_mem();
  140. if(bm == NULL)
  141. goto ret_point;
  142. bioMem = BIO_new(bm);
  143. if(bioMem == NULL)
  144. goto ret_point;
  145. (void)BIO_push(bio64, bioMem);
  146. if(BIO_write(bio64, in_buf, in_size) != (int)in_size){
  147. goto ret_point;
  148. }
  149. (void)BIO_flush(bio64);
  150. BUF_MEM *bptr;
  151. BIO_get_mem_ptr(bio64, &bptr);
  152. if(bptr==NULL){
  153. goto ret_point;
  154. }
  155. if(*out_size < bptr->length){
  156. goto ret_point;
  157. }
  158. if(memcpy_s(out_buf, *out_size,bptr->data, bptr->length)!=0)
  159. goto ret_point;
  160. *out_size = static_cast<uint32_t>(bptr->length);
  161. ret = true;
  162. ret_point:
  163. BIO_free_all(bio64);//we need not free bioMem too because the free_all has free it.
  164. return ret;
  165. }
  166. //Function to use openssl to do BASE64 decoding
  167. static bool base_64_decode(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t *out_size)
  168. {
  169. BIO *b64 =NULL, *bmem = NULL;
  170. bool ret = false;
  171. int read=0;
  172. memset(out_buf, 0, *out_size);
  173. BIO_METHOD *bm = BIO_f_base64();
  174. if(bm == NULL)
  175. goto ret_point;
  176. b64 = BIO_new(bm);
  177. if(b64 == NULL)
  178. goto ret_point;
  179. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  180. bmem = BIO_new_mem_buf(const_cast<uint8_t *>(in_buf), in_size);
  181. if(bmem == NULL){
  182. goto ret_point;
  183. }
  184. bmem = BIO_push(b64, bmem);
  185. read = BIO_read(bmem, out_buf, *out_size);
  186. if(read < 0)
  187. goto ret_point;
  188. *out_size = read;
  189. ret = true;
  190. ret_point:
  191. BIO_free_all(bmem);
  192. return ret;
  193. }
  194. //Function to give an upbound of size of data after BASE64 decoding
  195. //@param length: the length in bytes of BASE64 encoded data
  196. //@return an upbound of length in bytes of decoded data
  197. static uint32_t get_unbase_64_length(uint32_t length)
  198. {
  199. return (length * 3 / 4) + ((length * 3 % 4 > 0) ? 1 : 0 );
  200. }
  201. //Function to give an upbound of size of data after BASR64 encoding
  202. //@param length: the length in bytes of data to be encoded
  203. //@return an upbound of length in bytes of data after encoding
  204. static uint32_t get_base_64_length_upbound(uint32_t length)
  205. {
  206. uint32_t extra = (length+9)/10+50;//using enough extra memory
  207. return extra+(length*4+2)/3;
  208. }
  209. uint32_t get_request_encoding_length(const uint8_t *req)
  210. {
  211. //adding 1 extra byte to hold '\0'
  212. return static_cast<uint32_t>(2*PROVISION_REQUEST_HEADER_SIZE+get_base_64_length_upbound(GET_BODY_SIZE_FROM_PROVISION_REQUEST(req))+1);
  213. }
  214. uint32_t get_response_decoding_length(uint32_t buf_len)
  215. {
  216. if(buf_len<2*PROVISION_RESPONSE_HEADER_SIZE)
  217. return 0;
  218. return static_cast<uint32_t>(get_unbase_64_length(buf_len-2*static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE)) + PROVISION_RESPONSE_HEADER_SIZE);
  219. }
  220. bool encode_request(const uint8_t *req, uint32_t req_len, uint8_t *out_buf, uint32_t *out_len)
  221. {
  222. uint32_t encoded_header_size = static_cast<uint32_t>(2*PROVISION_REQUEST_HEADER_SIZE);
  223. if(*out_len<encoded_header_size)
  224. return false;
  225. if(req_len<PROVISION_REQUEST_HEADER_SIZE)
  226. return false;
  227. if(!byte_array_to_hex_string(req, PROVISION_REQUEST_HEADER_SIZE, out_buf, encoded_header_size))
  228. return false;
  229. uint32_t left_size = *out_len - encoded_header_size;
  230. if(req_len != GET_SIZE_FROM_PROVISION_REQUEST(req))
  231. return false;//error in input message
  232. if(!base_64_encode(req+PROVISION_REQUEST_HEADER_SIZE, GET_BODY_SIZE_FROM_PROVISION_REQUEST(req), out_buf + encoded_header_size, &left_size))
  233. return false;
  234. *out_len = left_size + encoded_header_size;
  235. return true;
  236. }
  237. bool decode_response(const uint8_t *input_buf, uint32_t input_len, uint8_t *resp, uint32_t *out_len)
  238. {
  239. if(input_len < 2*PROVISION_RESPONSE_HEADER_SIZE)
  240. return false;
  241. if(*out_len < PROVISION_RESPONSE_HEADER_SIZE)
  242. return false;
  243. if(!hex_string_to_byte_array(input_buf, static_cast<uint32_t>(2*PROVISION_RESPONSE_HEADER_SIZE), resp, static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE)))
  244. return false;
  245. if(*out_len<GET_SIZE_FROM_PROVISION_RESPONSE(resp))
  246. return false;
  247. *out_len -= static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE);
  248. if(!base_64_decode(input_buf+static_cast<uint32_t>(2*PROVISION_RESPONSE_HEADER_SIZE), input_len - static_cast<uint32_t>(2*PROVISION_RESPONSE_HEADER_SIZE),
  249. resp+static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE), out_len))
  250. return false;
  251. *out_len += static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE);
  252. if(*out_len != GET_SIZE_FROM_PROVISION_RESPONSE(resp))
  253. return false;
  254. return true;
  255. }