aesm_encode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. BIO_METHOD *bm = BIO_f_base64();
  137. if(bm == NULL)
  138. goto ret_point;
  139. bio64 = BIO_new(bm);
  140. if(bio64 == NULL)
  141. goto ret_point;
  142. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  143. bm = BIO_s_mem();
  144. if(bm == NULL)
  145. goto ret_point;
  146. bioMem = BIO_new(bm);
  147. if(bioMem == NULL)
  148. goto ret_point;
  149. (void)BIO_push(bio64, bioMem);
  150. if(BIO_write(bio64, in_buf, in_size) != (int)in_size){
  151. goto ret_point;
  152. }
  153. (void)BIO_flush(bio64);
  154. BUF_MEM *bptr;
  155. BIO_get_mem_ptr(bio64, &bptr);
  156. if(bptr==NULL){
  157. goto ret_point;
  158. }
  159. if(*out_size < bptr->length){
  160. goto ret_point;
  161. }
  162. if(memcpy_s(out_buf, *out_size,bptr->data, bptr->length)!=0)
  163. goto ret_point;
  164. *out_size = static_cast<uint32_t>(bptr->length);
  165. ret = true;
  166. ret_point:
  167. BIO_free_all(bio64);//we need not free bioMem too because the free_all has free it.
  168. return ret;
  169. }
  170. //Function to use openssl to do BASE64 decoding
  171. static bool base_64_decode(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t *out_size)
  172. {
  173. BIO *b64 =NULL, *bmem = NULL;
  174. bool ret = false;
  175. int read=0;
  176. memset(out_buf, 0, *out_size);
  177. BIO_METHOD *bm = BIO_f_base64();
  178. if(bm == NULL)
  179. goto ret_point;
  180. b64 = BIO_new(bm);
  181. if(b64 == NULL)
  182. goto ret_point;
  183. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  184. bmem = BIO_new_mem_buf(const_cast<uint8_t *>(in_buf), in_size);
  185. if(bmem == NULL){
  186. goto ret_point;
  187. }
  188. bmem = BIO_push(b64, bmem);
  189. read = BIO_read(bmem, out_buf, *out_size);
  190. if(read < 0)
  191. goto ret_point;
  192. *out_size = read;
  193. ret = true;
  194. ret_point:
  195. BIO_free_all(bmem);
  196. return ret;
  197. }
  198. //Function to give an upper bound of size of data after BASE64 decoding
  199. //@param length: the length in bytes of BASE64 encoded data
  200. //@return an upper bound of length in bytes of decoded data
  201. static uint32_t get_unbase_64_length(uint32_t length)
  202. {
  203. return (length * 3 / 4) + ((length * 3 % 4 > 0) ? 1 : 0 );
  204. }
  205. //Function to give an upper bound of size of data after BASR64 encoding
  206. //@param length: the length in bytes of data to be encoded
  207. //@return an upper bound of length in bytes of data after encoding
  208. static uint32_t get_base_64_length_upbound(uint32_t length)
  209. {
  210. uint32_t extra = (length+9)/10+50;//using enough extra memory
  211. return extra+(length*4+2)/3;
  212. }
  213. uint32_t get_request_encoding_length(const uint8_t *req)
  214. {
  215. //adding 1 extra byte to hold '\0'
  216. return static_cast<uint32_t>(2*PROVISION_REQUEST_HEADER_SIZE+get_base_64_length_upbound(GET_BODY_SIZE_FROM_PROVISION_REQUEST(req))+1);
  217. }
  218. uint32_t get_response_decoding_length(uint32_t buf_len)
  219. {
  220. if(buf_len<2*PROVISION_RESPONSE_HEADER_SIZE)
  221. return 0;
  222. return static_cast<uint32_t>(get_unbase_64_length(buf_len-2*static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE)) + PROVISION_RESPONSE_HEADER_SIZE);
  223. }
  224. bool encode_request(const uint8_t *req, uint32_t req_len, uint8_t *out_buf, uint32_t *out_len)
  225. {
  226. uint32_t encoded_header_size = static_cast<uint32_t>(2*PROVISION_REQUEST_HEADER_SIZE);
  227. if(*out_len<encoded_header_size)
  228. return false;
  229. if(req_len<PROVISION_REQUEST_HEADER_SIZE)
  230. return false;
  231. if(!byte_array_to_hex_string(req, PROVISION_REQUEST_HEADER_SIZE, out_buf, encoded_header_size))
  232. return false;
  233. uint32_t left_size = *out_len - encoded_header_size;
  234. if(req_len != GET_SIZE_FROM_PROVISION_REQUEST(req))
  235. return false;//error in input message
  236. if(!base_64_encode(req+PROVISION_REQUEST_HEADER_SIZE, GET_BODY_SIZE_FROM_PROVISION_REQUEST(req), out_buf + encoded_header_size, &left_size))
  237. return false;
  238. *out_len = left_size + encoded_header_size;
  239. return true;
  240. }
  241. bool decode_response(const uint8_t *input_buf, uint32_t input_len, uint8_t *resp, uint32_t *out_len)
  242. {
  243. if(input_len < 2*PROVISION_RESPONSE_HEADER_SIZE)
  244. return false;
  245. if(*out_len < PROVISION_RESPONSE_HEADER_SIZE)
  246. return false;
  247. 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)))
  248. return false;
  249. if(*out_len<GET_SIZE_FROM_PROVISION_RESPONSE(resp))
  250. return false;
  251. *out_len -= static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE);
  252. 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),
  253. resp+static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE), out_len))
  254. return false;
  255. *out_len += static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE);
  256. if(*out_len != GET_SIZE_FROM_PROVISION_RESPONSE(resp))
  257. return false;
  258. return true;
  259. }
  260. //
  261. // certPseSvn
  262. //
  263. // return ISVSVN of PSE stored in PSE certificate or 0, if error
  264. //
  265. // remarks
  266. // the ISVSVN in the cert could be old since it only updates when we execute
  267. // pse provisioning
  268. //
  269. uint32_t certPseSvn()
  270. {
  271. uint32_t pseSvn = 0;
  272. bool pseSvnFound = false;
  273. X509* cert = NULL;
  274. std::list<upse::Buffer> certChain;
  275. X509_NAME* subj2 = NULL;
  276. X509_NAME* issuer2 = NULL;
  277. X509_NAME_ENTRY *entry = NULL;
  278. ASN1_STRING *entryData = NULL;
  279. char *str = NULL;
  280. //
  281. // load cert chain from disk
  282. //
  283. ae_error_t loadCertError = Helper::LoadCertificateChain(certChain);
  284. //
  285. // create openssl bio to temporarily hold cert data
  286. //
  287. BIO* certBio = BIO_new(BIO_s_mem());
  288. if ((AE_SUCCESS == loadCertError) && (NULL != certBio)) {
  289. for (std::list<upse::Buffer>::const_iterator iterator = certChain.begin(), end = certChain.end(); iterator != end; ++iterator) {
  290. //
  291. // go from binary (tempCert) to mem bio (certBio) to internal OpenSSL representation of x509 cert (cert)
  292. //
  293. const upse::Buffer& tempCert = *iterator;
  294. int retVal = certBio->method->bwrite(certBio, (const char*) tempCert.getData(), tempCert.getSize());
  295. if (retVal <= 0) break;
  296. cert = d2i_X509_bio(certBio, NULL);
  297. if (NULL == cert) {
  298. break;
  299. }
  300. //
  301. // PSE ISVSVN is in parent of the leaf cert, in name
  302. // we'll look for a cert with "Intel PSE" at the beginning of the subject name and
  303. // "Intel PSE TCB CA" at the beginning of the issuer name and then we'll get
  304. // the ISVSVN value from (later in) the issuer name
  305. //
  306. subj2 = X509_get_subject_name(cert);
  307. issuer2 = X509_get_issuer_name(cert);
  308. for (int si = 0; si < X509_NAME_entry_count(subj2); si++) {
  309. //
  310. // boilerplate openssl stuff
  311. //
  312. entry = X509_NAME_get_entry(subj2, si);
  313. entryData = X509_NAME_ENTRY_get_data(entry);
  314. if (NULL == entryData) {
  315. continue;
  316. }
  317. str = (char*) ASN1_STRING_data(entryData);
  318. const char* tempName = "Intel PSE";
  319. if (strncmp(str, tempName, strlen(tempName))==0) {//starting with tempName
  320. for (int ii = 0; ii < X509_NAME_entry_count(issuer2); ii++) {
  321. entry = X509_NAME_get_entry(issuer2, ii);
  322. entryData = X509_NAME_ENTRY_get_data(entry);
  323. if (NULL == entryData) {
  324. continue;
  325. }
  326. str = (char*) ASN1_STRING_data(entryData);
  327. tempName = "Intel PSE TCB CA";
  328. if (strncmp(str, tempName, strlen(tempName) )==0) {//string start with tempName
  329. pseSvnFound = true;
  330. //
  331. // assume rest of issuer name, after "Intel PSE TCB CA" converts to PSE ISVSVN
  332. pseSvn = static_cast<uint32_t>(strtol(&str[strlen(tempName)], NULL, 10));
  333. break;
  334. }
  335. }
  336. if (pseSvnFound) {
  337. break;
  338. }
  339. }
  340. }
  341. if (NULL != cert) {
  342. X509_free(cert);
  343. }
  344. if (pseSvnFound) {
  345. break;
  346. }
  347. }
  348. }
  349. if (certBio != NULL) BIO_free(certBio);
  350. return pseSvn;
  351. }