aesm_encode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "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. #include "upse/helper.h"
  42. #include "upse/Buffer.h"
  43. /**
  44. * Method converts byte containing value from 0x00-0x0F into its corresponding ASCII code,
  45. * e.g. converts 0x00 to '0', 0x0A to 'A'.
  46. * Note: This is mainly a helper method for internal use in byte_array_to_hex_string().
  47. *
  48. * @param in byte to be converted (allowed values: 0x00-0x0F)
  49. *
  50. * @return ASCII code representation of the byte or 0 if method failed (e.g input value was not in provided range).
  51. */
  52. static uint8_t convert_value_to_ascii(uint8_t in)
  53. {
  54. if(in <= 0x09)
  55. {
  56. return (uint8_t)(in + '0');
  57. }
  58. else if(in <= 0x0F)
  59. {
  60. return (uint8_t)(in - 10 + 'A');
  61. }
  62. return 0;
  63. }
  64. /**
  65. * Method converts char containing ASCII code into its corresponding value,
  66. * e.g. converts '0' to 0x00, 'A' to 0x0A.
  67. *
  68. * @param in char containing ASCII code (allowed values: '0-9', 'a-f', 'A-F')
  69. * @param val output parameter containing converted value, if method succeeds.
  70. *
  71. * @return true if conversion succeeds, false otherwise
  72. */
  73. static bool convert_ascii_to_value(uint8_t in, uint8_t& val)
  74. {
  75. if(in >= '0' && in <= '9')
  76. {
  77. val = static_cast<uint8_t>(in - '0');
  78. }
  79. else if(in >= 'A' && in <= 'F')
  80. {
  81. val = static_cast<uint8_t>(in - 'A'+10);
  82. }
  83. else if(in >= 'a' && in <= 'f')
  84. {
  85. val = static_cast<uint8_t>(in - 'a'+10);
  86. }
  87. else
  88. {
  89. return false;
  90. }
  91. return true;
  92. }
  93. //Function to do HEX encoding of array of bytes
  94. //@param in_buf, bytes array whose length is in_size
  95. // out_buf, output the HEX encoding of in_buf on success.
  96. //@return true on success and false on error
  97. //The out_size must always be 2*in_size since each byte into encoded by 2 characters
  98. static bool byte_array_to_hex_string(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size)
  99. {
  100. if(in_size>UINT32_MAX/2)return false;
  101. if(in_buf==NULL||out_buf==NULL|| out_size!=in_size*2 )return false;
  102. for(uint32_t i=0; i< in_size; i++)
  103. {
  104. *out_buf++ = convert_value_to_ascii( static_cast<uint8_t>(*in_buf >> 4));
  105. *out_buf++ = convert_value_to_ascii( static_cast<uint8_t>(*in_buf & 0xf));
  106. in_buf++;
  107. }
  108. return true;
  109. }
  110. //Function to do HEX decoding
  111. //@param in_buf, character strings which are HEX encoding of a byte array
  112. // out_buf, output the decode byte array on success
  113. //@return true on success and false on error
  114. //The in_size must be even number and equals 2*out_size
  115. static bool hex_string_to_byte_array(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size)
  116. {
  117. if(out_size>UINT32_MAX/2)return false;
  118. if(in_buf==NULL||out_buf==NULL||out_size*2!=in_size)return false;
  119. for(uint32_t i=0;i<out_size;i++)
  120. {
  121. uint8_t value_first, value_second;
  122. if(!convert_ascii_to_value(in_buf[i*2], value_first))
  123. return false;
  124. if(!convert_ascii_to_value(in_buf[i*2+1], value_second))
  125. return false;
  126. out_buf[i] = static_cast<uint8_t>(value_second+ (value_first<<4));
  127. }
  128. return true;
  129. }
  130. //Function to use openssl to do BASE64 encoding
  131. static bool base_64_encode(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t *out_size)
  132. {
  133. BIO* bioMem = NULL;
  134. bool ret = false;
  135. BIO *bio64 = NULL;
  136. bio64 = BIO_new(BIO_f_base64());
  137. if(bio64 == NULL)
  138. goto ret_point;
  139. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  140. bioMem = BIO_new(BIO_s_mem());
  141. if(bioMem == NULL)
  142. goto ret_point;
  143. (void)BIO_push(bio64, bioMem);
  144. if(BIO_write(bio64, in_buf, in_size) != (int)in_size){
  145. goto ret_point;
  146. }
  147. (void)BIO_flush(bio64);
  148. BUF_MEM *bptr;
  149. BIO_get_mem_ptr(bio64, &bptr);
  150. if(bptr==NULL){
  151. goto ret_point;
  152. }
  153. if(*out_size < bptr->length){
  154. goto ret_point;
  155. }
  156. if(memcpy_s(out_buf, *out_size,bptr->data, bptr->length)!=0)
  157. goto ret_point;
  158. *out_size = static_cast<uint32_t>(bptr->length);
  159. ret = true;
  160. ret_point:
  161. BIO_free_all(bio64);//we need not free bioMem too because the free_all has free it.
  162. return ret;
  163. }
  164. //Function to use openssl to do BASE64 decoding
  165. static bool base_64_decode(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t *out_size)
  166. {
  167. BIO *b64 =NULL, *bmem = NULL;
  168. bool ret = false;
  169. int read=0;
  170. memset(out_buf, 0, *out_size);
  171. b64 = BIO_new(BIO_f_base64());
  172. if(b64 == NULL)
  173. goto ret_point;
  174. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  175. bmem = BIO_new_mem_buf(const_cast<uint8_t *>(in_buf), in_size);
  176. if(bmem == NULL){
  177. goto ret_point;
  178. }
  179. bmem = BIO_push(b64, bmem);
  180. read = BIO_read(bmem, out_buf, *out_size);
  181. if(read < 0)
  182. goto ret_point;
  183. *out_size = read;
  184. ret = true;
  185. ret_point:
  186. BIO_free_all(bmem);
  187. return ret;
  188. }
  189. //Function to give an upper bound of size of data after BASE64 decoding
  190. //@param length: the length in bytes of BASE64 encoded data
  191. //@return an upper bound of length in bytes of decoded data
  192. static uint32_t get_unbase_64_length(uint32_t length)
  193. {
  194. return (length * 3 / 4) + ((length * 3 % 4 > 0) ? 1 : 0 );
  195. }
  196. //Function to give an upper bound of size of data after BASR64 encoding
  197. //@param length: the length in bytes of data to be encoded
  198. //@return an upper bound of length in bytes of data after encoding
  199. static uint32_t get_base_64_length_upbound(uint32_t length)
  200. {
  201. uint32_t extra = (length+9)/10+50;//using enough extra memory
  202. return extra+(length*4+2)/3;
  203. }
  204. uint32_t get_request_encoding_length(const uint8_t *req)
  205. {
  206. //adding 1 extra byte to hold '\0'
  207. return static_cast<uint32_t>(2*PROVISION_REQUEST_HEADER_SIZE+get_base_64_length_upbound(GET_BODY_SIZE_FROM_PROVISION_REQUEST(req))+1);
  208. }
  209. uint32_t get_response_decoding_length(uint32_t buf_len)
  210. {
  211. if(buf_len<2*PROVISION_RESPONSE_HEADER_SIZE)
  212. return 0;
  213. return static_cast<uint32_t>(get_unbase_64_length(buf_len-2*static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE)) + PROVISION_RESPONSE_HEADER_SIZE);
  214. }
  215. bool encode_request(const uint8_t *req, uint32_t req_len, uint8_t *out_buf, uint32_t *out_len)
  216. {
  217. uint32_t encoded_header_size = static_cast<uint32_t>(2*PROVISION_REQUEST_HEADER_SIZE);
  218. if(*out_len<encoded_header_size)
  219. return false;
  220. if(req_len<PROVISION_REQUEST_HEADER_SIZE)
  221. return false;
  222. if(!byte_array_to_hex_string(req, PROVISION_REQUEST_HEADER_SIZE, out_buf, encoded_header_size))
  223. return false;
  224. uint32_t left_size = *out_len - encoded_header_size;
  225. if(req_len != GET_SIZE_FROM_PROVISION_REQUEST(req))
  226. return false;//error in input message
  227. if(!base_64_encode(req+PROVISION_REQUEST_HEADER_SIZE, GET_BODY_SIZE_FROM_PROVISION_REQUEST(req), out_buf + encoded_header_size, &left_size))
  228. return false;
  229. *out_len = left_size + encoded_header_size;
  230. return true;
  231. }
  232. bool decode_response(const uint8_t *input_buf, uint32_t input_len, uint8_t *resp, uint32_t *out_len)
  233. {
  234. if(input_len < 2*PROVISION_RESPONSE_HEADER_SIZE)
  235. return false;
  236. if(*out_len < PROVISION_RESPONSE_HEADER_SIZE)
  237. return false;
  238. 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)))
  239. return false;
  240. if(*out_len<GET_SIZE_FROM_PROVISION_RESPONSE(resp))
  241. return false;
  242. *out_len -= static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE);
  243. 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),
  244. resp+static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE), out_len))
  245. return false;
  246. *out_len += static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE);
  247. if(*out_len != GET_SIZE_FROM_PROVISION_RESPONSE(resp))
  248. return false;
  249. return true;
  250. }
  251. //
  252. // certPseSvn
  253. //
  254. // return ISVSVN of PSE stored in PSE certificate or 0, if error
  255. //
  256. // remarks
  257. // the ISVSVN in the cert could be old since it only updates when we execute
  258. // pse provisioning
  259. //
  260. uint32_t certPseSvn()
  261. {
  262. uint32_t pseSvn = 0;
  263. bool pseSvnFound = false;
  264. X509* cert = NULL;
  265. std::list<upse::Buffer> certChain;
  266. X509_NAME* subj2 = NULL;
  267. X509_NAME* issuer2 = NULL;
  268. X509_NAME_ENTRY *entry = NULL;
  269. ASN1_STRING *entryData = NULL;
  270. char *str = NULL;
  271. //
  272. // load cert chain from disk
  273. //
  274. ae_error_t loadCertError = Helper::LoadCertificateChain(certChain);
  275. //
  276. // create openssl bio to temporarily hold cert data
  277. //
  278. BIO* certBio = BIO_new(BIO_s_mem());
  279. if ((AE_SUCCESS == loadCertError) && (NULL != certBio)) {
  280. for (std::list<upse::Buffer>::const_iterator iterator = certChain.begin(), end = certChain.end(); iterator != end; ++iterator) {
  281. //
  282. // go from binary (tempCert) to mem bio (certBio) to internal OpenSSL representation of x509 cert (cert)
  283. //
  284. const upse::Buffer& tempCert = *iterator;
  285. int retVal = BIO_write(certBio, (const char*) tempCert.getData(), tempCert.getSize());
  286. if (retVal <= 0) break;
  287. cert = d2i_X509_bio(certBio, NULL);
  288. if (NULL == cert) {
  289. break;
  290. }
  291. //
  292. // PSE ISVSVN is in parent of the leaf cert, in name
  293. // we'll look for a cert with "Intel PSE" at the beginning of the subject name and
  294. // "Intel PSE TCB CA" at the beginning of the issuer name and then we'll get
  295. // the ISVSVN value from (later in) the issuer name
  296. //
  297. subj2 = X509_get_subject_name(cert);
  298. issuer2 = X509_get_issuer_name(cert);
  299. for (int si = 0; si < X509_NAME_entry_count(subj2); si++) {
  300. //
  301. // boilerplate openssl stuff
  302. //
  303. entry = X509_NAME_get_entry(subj2, si);
  304. entryData = X509_NAME_ENTRY_get_data(entry);
  305. if (NULL == entryData) {
  306. continue;
  307. }
  308. str = (char*) ASN1_STRING_data(entryData);
  309. const char* tempName = "Intel PSE";
  310. if (strncmp(str, tempName, strlen(tempName))==0) {//starting with tempName
  311. for (int ii = 0; ii < X509_NAME_entry_count(issuer2); ii++) {
  312. entry = X509_NAME_get_entry(issuer2, ii);
  313. entryData = X509_NAME_ENTRY_get_data(entry);
  314. if (NULL == entryData) {
  315. continue;
  316. }
  317. str = (char*) ASN1_STRING_data(entryData);
  318. tempName = "Intel PSE TCB CA";
  319. if (strncmp(str, tempName, strlen(tempName) )==0) {//string start with tempName
  320. pseSvnFound = true;
  321. //
  322. // assume rest of issuer name, after "Intel PSE TCB CA" converts to PSE ISVSVN
  323. pseSvn = static_cast<uint32_t>(strtol(&str[strlen(tempName)], NULL, 10));
  324. break;
  325. }
  326. }
  327. if (pseSvnFound) {
  328. break;
  329. }
  330. }
  331. }
  332. if (NULL != cert) {
  333. X509_free(cert);
  334. }
  335. if (pseSvnFound) {
  336. break;
  337. }
  338. }
  339. }
  340. if (certBio != NULL) BIO_free(certBio);
  341. return pseSvn;
  342. }