DRM_app.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "ReplayProtectedDRM.h"
  32. #include "TimeBasedDRM.h"
  33. #include <iostream>
  34. using namespace std;
  35. #include <string.h>
  36. #include <unistd.h>
  37. #define memcpy_s(dst,dst_size,src,max_count) memcpy(dst,src,max_count)
  38. #define Sleep(n) usleep((n)*1000)
  39. uint32_t test_replay_protected_drm_operation()
  40. {
  41. cout<<endl<<"\tReplay Protected DRM operation:"<<endl;
  42. uint32_t result = 0;
  43. ReplayProtectedDRM DRM;
  44. result = DRM.init();
  45. if(result)
  46. {
  47. cerr<<"Initialization the DRM failed."<<endl;
  48. return result;
  49. }
  50. else
  51. cout<<"Successfully initialized the DRM."<<endl;
  52. do{
  53. result = DRM.perform_function();
  54. if(result)
  55. {
  56. cerr<<"Performing the DRM functions failed."<<endl;
  57. break;
  58. }
  59. else
  60. cout<<"Successfully performed the DRM functions."<<endl;
  61. result = DRM.update_secret();
  62. if(result)
  63. {
  64. cerr<<"Updating the DRM secret failed."<<endl;
  65. break;
  66. }
  67. else
  68. cout<<"Successfully updated the DRM secret."<<endl;
  69. result = DRM.perform_function();
  70. if(result)
  71. {
  72. cerr<<"Performing the DRM functions failed."<<endl;
  73. break;
  74. }
  75. else
  76. cout<<"Successfully performed the DRM functions."<<endl;
  77. }while(0);
  78. if(DRM.delete_secret())
  79. {
  80. cerr<<"Deleting the DRM secret failed."<<endl;
  81. return result;
  82. }
  83. else
  84. cout<<"Successfully deleted the DRM secret."<<endl;
  85. return result;
  86. }
  87. uint32_t test_replay_protected_drm_update_limitation()
  88. {
  89. cout<<endl<<"\tReplay Protected DRM update limitation:"<<endl;
  90. uint32_t result = 0;
  91. ReplayProtectedDRM DRM;
  92. result = DRM.init();
  93. if(result)
  94. {
  95. cerr<<"Initialization the DRM failed."<<endl;
  96. return result;
  97. }
  98. else
  99. cout<<"Successfully initialized the DRM."<<endl;
  100. do{
  101. result = DRM.perform_function();
  102. if(result)
  103. {
  104. cerr<<"Performing the DRM functions fail."<<endl;
  105. break;
  106. }
  107. else
  108. cout<<"Successfully performed the DRM functions."<<endl;
  109. for (int i = 0; i <= REPLAY_PROTECTED_PAY_LOAD_MAX_RELEASE_VERSION; i++)
  110. {
  111. result = DRM.update_secret();
  112. if(result == MAX_RELEASE_REACHED &&
  113. i>=REPLAY_PROTECTED_PAY_LOAD_MAX_RELEASE_VERSION)
  114. {
  115. cout<<"\tExpected failure."<<endl
  116. <<"\tThe DRM secret update limitation reached."<<endl;
  117. result = 0;
  118. return result;
  119. }
  120. else if(result)
  121. {
  122. cerr<<"Updating the DRM secret failed."<<endl;
  123. break;
  124. }
  125. else
  126. cout<<"Successfully updated the DRM secret."<<endl;
  127. result = DRM.perform_function();
  128. if(result)
  129. {
  130. cerr<<"Performing the DRM functions failed."<<endl;
  131. break;
  132. }
  133. else
  134. cout<<"Successfully performed the DRM functions."<<endl;
  135. }
  136. if(!result)
  137. {
  138. result = 1;
  139. cerr<<"\tUnexpected success."<<endl
  140. <<"\tFailed to catch update limitation."<<endl;
  141. }
  142. }while(0);
  143. if(DRM.delete_secret())
  144. {
  145. cerr<<"Deleting the DRM secret failed."<<endl;
  146. return result;
  147. }
  148. else
  149. cout<<"Successfully deleted the DRM secret."<<endl;
  150. return result;
  151. }
  152. uint32_t test_replay_protected_drm_replay_attack_protection()
  153. {
  154. cout<<endl<<"\tReplay Protected DRM replay attack protection:"<<endl;
  155. uint32_t result = 0;
  156. uint8_t sealed_log[ReplayProtectedDRM::sealed_activity_log_length];
  157. ReplayProtectedDRM DRM;
  158. result = DRM.init(sealed_log);
  159. if(result)
  160. {
  161. cerr<<"Initialization the DRM failed."<<endl;
  162. return result;
  163. }
  164. else
  165. cout<<"Successfully initialized the DRM."<<endl;
  166. /* store a valid log for replay attack */
  167. uint8_t replay_log[ReplayProtectedDRM::sealed_activity_log_length];
  168. memcpy_s(replay_log,ReplayProtectedDRM::sealed_activity_log_length,
  169. sealed_log,ReplayProtectedDRM::sealed_activity_log_length);
  170. do{
  171. result = DRM.update_secret(sealed_log);
  172. if(result)
  173. {
  174. cerr<<"Updating the DRM secret functions failed."<<endl;
  175. break;
  176. }
  177. else
  178. cout<<"Successfully updated the DRM functions."<<endl;
  179. result = DRM.perform_function(replay_log);
  180. if(result == REPLAY_DETECTED)
  181. cout<<"\tExpected failure."<<endl
  182. <<"\tReplay attack to DRM functions is caught."<<endl;
  183. else
  184. {
  185. cerr<<"\tUnexpected success."<<endl
  186. <<"\tReplay attack to DRM functions is NOT caught."<<endl;
  187. result = 1;
  188. break;
  189. }
  190. result = DRM.perform_function(sealed_log);
  191. if(result)
  192. {
  193. cerr<<"Performing the DRM functions failed."<<endl;
  194. break;
  195. }
  196. else
  197. cout<<"Successfully performed the DRM functions ."<<endl;
  198. }while(0);
  199. if(DRM.delete_secret(sealed_log))
  200. {
  201. cerr<<"Deleting the DRM secret failed."<<endl;
  202. return result;
  203. }
  204. else
  205. cout<<"Successfully deleted the DRM secret."<<endl;
  206. return result;
  207. }
  208. uint32_t test_time_based_policy_operation()
  209. {
  210. cout<<endl<<"\tTime based policy operation:"<<endl;
  211. TimeBasedDRM DRM;
  212. uint32_t result = 0;
  213. result = DRM.init();
  214. if(result)
  215. {
  216. cerr<<"Initialization the time based policy failed."<<endl;
  217. return result;
  218. }
  219. else
  220. cout<<"Successfully initialized the time based policy."<<endl;
  221. result = DRM.perform_function();
  222. if(result)
  223. {
  224. cerr<<"Performing the time based policy functions failed."<<endl;
  225. return result;
  226. }
  227. else
  228. cout<<"Successfully performed the time based policy functions."<<endl;
  229. return 0;
  230. }
  231. uint32_t test_time_based_policy_expiration()
  232. {
  233. cout<<endl<<"\tTime based policy expiration:"<<endl;
  234. TimeBasedDRM DRM;
  235. uint32_t result = 0;
  236. result = DRM.init();
  237. if(result)
  238. {
  239. cerr<<"Initialization the time based policy failed."<<endl;
  240. return result;
  241. }
  242. else
  243. cout<<"Successfully initialized the time based policy."<<endl;
  244. /* wait for time based DRM expiring */
  245. Sleep((TIME_BASED_LEASE_DURATION_SECOND+1)*1000);
  246. result = DRM.perform_function();
  247. if(result== LEASE_EXPIRED)
  248. {
  249. cout<<"\tExpected failure."<<endl
  250. <<"\tTime based policy has expired."<<endl;
  251. return 0;
  252. }
  253. else
  254. {
  255. cerr<<"\tUnexpected success."<<endl
  256. <<"\tTime based policy failed to catch expiration."<<endl;
  257. return 1;
  258. }
  259. }
  260. #define _T(x) x
  261. int main(int argc, char* argv[])
  262. {
  263. argc; /* unused parameter */
  264. argv; /* unused parameter */
  265. uint32_t result;
  266. /* normal operation */
  267. result = test_replay_protected_drm_operation();
  268. /* trigger update limitation */
  269. result = test_replay_protected_drm_update_limitation();
  270. /* replay attack */
  271. result = test_replay_protected_drm_replay_attack_protection();
  272. /* normal operation */
  273. result = test_time_based_policy_operation();
  274. /* trigger expiration */
  275. result = test_time_based_policy_expiration();
  276. printf("Enter a character before exit ...\n");
  277. getchar();
  278. return 0;
  279. }