keymaster_enforcement_test.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include <keymaster/android_keymaster.h>
  20. #include <keymaster/authorization_set.h>
  21. #include <keymaster/km_openssl/soft_keymaster_enforcement.h>
  22. #include "android_keymaster_test_utils.h"
  23. namespace keymaster {
  24. namespace test {
  25. class TestKeymasterEnforcement : public SoftKeymasterEnforcement {
  26. public:
  27. TestKeymasterEnforcement()
  28. : SoftKeymasterEnforcement(3, 3), current_time_(10000), report_token_valid_(true) {}
  29. keymaster_error_t AuthorizeOperation(const keymaster_purpose_t purpose, const km_id_t keyid,
  30. const AuthProxy& auth_set) {
  31. AuthorizationSet empty_set;
  32. return KeymasterEnforcement::AuthorizeOperation(
  33. purpose, keyid, auth_set, empty_set, 0 /* op_handle */, true /* is_begin_operation */);
  34. }
  35. using KeymasterEnforcement::AuthorizeOperation;
  36. uint64_t get_current_time_ms() const override { return current_time_ * 1000; }
  37. bool activation_date_valid(uint64_t activation_date) const override {
  38. // Convert java date to time_t, non-portably.
  39. time_t activation_time = activation_date / 1000;
  40. return difftime(time(NULL), activation_time) >= 0;
  41. }
  42. bool expiration_date_passed(uint64_t expiration_date) const override {
  43. // Convert jave date to time_t, non-portably.
  44. time_t expiration_time = expiration_date / 1000;
  45. return difftime(time(NULL), expiration_time) > 0;
  46. }
  47. bool auth_token_timed_out(const hw_auth_token_t& token, uint32_t timeout) const override {
  48. return current_time_ > ntoh(token.timestamp) + timeout;
  49. }
  50. bool ValidateTokenSignature(const hw_auth_token_t&) const override {
  51. return report_token_valid_;
  52. }
  53. void tick(unsigned seconds = 1) { current_time_ += seconds; }
  54. uint32_t current_time() { return current_time_; }
  55. void set_report_token_valid(bool report_token_valid) {
  56. report_token_valid_ = report_token_valid;
  57. }
  58. private:
  59. uint32_t current_time_;
  60. bool report_token_valid_;
  61. };
  62. class KeymasterBaseTest : public ::testing::Test {
  63. protected:
  64. KeymasterBaseTest() {
  65. past_time = 0;
  66. time_t t = time(NULL);
  67. future_tm = localtime(&t);
  68. future_tm->tm_year += 1;
  69. future_time = static_cast<uint64_t>(mktime(future_tm)) * 1000;
  70. sign_param = Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN);
  71. }
  72. virtual ~KeymasterBaseTest() {}
  73. TestKeymasterEnforcement kmen;
  74. tm past_tm;
  75. tm* future_tm;
  76. uint64_t past_time;
  77. uint64_t future_time;
  78. static const km_id_t key_id = 0xa;
  79. static const uid_t uid = 0xf;
  80. keymaster_key_param_t sign_param;
  81. AuthorizationSet empty;
  82. };
  83. TEST_F(KeymasterBaseTest, TestValidKeyPeriodNoTags) {
  84. keymaster_key_param_t params[] = {
  85. sign_param,
  86. };
  87. AuthorizationSet single_auth_set(params, array_length(params));
  88. keymaster_error_t kmer =
  89. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(single_auth_set, empty));
  90. ASSERT_EQ(KM_ERROR_OK, kmer);
  91. }
  92. TEST_F(KeymasterBaseTest, TestInvalidActiveTime) {
  93. keymaster_key_param_t params[] = {
  94. Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  95. Authorization(TAG_NO_AUTH_REQUIRED), Authorization(TAG_ACTIVE_DATETIME, future_time),
  96. };
  97. AuthorizationSet auth_set(params, array_length(params));
  98. ASSERT_EQ(KM_ERROR_KEY_NOT_YET_VALID,
  99. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  100. // Pubkey ops allowed.
  101. ASSERT_EQ(KM_ERROR_OK,
  102. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  103. }
  104. TEST_F(KeymasterBaseTest, TestValidActiveTime) {
  105. keymaster_key_param_t params[] = {
  106. Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_ACTIVE_DATETIME, past_time),
  107. };
  108. AuthorizationSet auth_set(params, array_length(params));
  109. keymaster_error_t kmer_valid_time =
  110. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  111. ASSERT_EQ(KM_ERROR_OK, kmer_valid_time);
  112. }
  113. TEST_F(KeymasterBaseTest, TestInvalidOriginationExpireTime) {
  114. keymaster_key_param_t params[] = {
  115. Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  116. Authorization(TAG_ORIGINATION_EXPIRE_DATETIME, past_time),
  117. };
  118. AuthorizationSet auth_set(params, array_length(params));
  119. ASSERT_EQ(KM_ERROR_KEY_EXPIRED,
  120. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  121. // Pubkey ops allowed.
  122. ASSERT_EQ(KM_ERROR_OK,
  123. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  124. }
  125. TEST_F(KeymasterBaseTest, TestInvalidOriginationExpireTimeOnUsgae) {
  126. keymaster_key_param_t params[] = {
  127. Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
  128. Authorization(TAG_ORIGINATION_EXPIRE_DATETIME, past_time),
  129. };
  130. AuthorizationSet auth_set(params, array_length(params));
  131. keymaster_error_t kmer_invalid_origination =
  132. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty));
  133. ASSERT_EQ(KM_ERROR_OK, kmer_invalid_origination);
  134. }
  135. TEST_F(KeymasterBaseTest, TestValidOriginationExpireTime) {
  136. keymaster_key_param_t params[] = {
  137. Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  138. Authorization(TAG_ORIGINATION_EXPIRE_DATETIME, future_time),
  139. };
  140. AuthorizationSet auth_set(params, array_length(params));
  141. keymaster_error_t kmer_valid_origination =
  142. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  143. ASSERT_EQ(KM_ERROR_OK, kmer_valid_origination);
  144. }
  145. TEST_F(KeymasterBaseTest, TestInvalidUsageExpireTime) {
  146. keymaster_key_param_t params[] = {
  147. Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES),
  148. Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
  149. Authorization(TAG_USAGE_EXPIRE_DATETIME, past_time),
  150. };
  151. AuthorizationSet auth_set(params, array_length(params));
  152. keymaster_error_t kmer_invalid_origination =
  153. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty));
  154. ASSERT_EQ(KM_ERROR_KEY_EXPIRED, kmer_invalid_origination);
  155. }
  156. TEST_F(KeymasterBaseTest, TestInvalidPubkeyUsageExpireTime) {
  157. keymaster_key_param_t params[] = {
  158. Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA),
  159. Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
  160. Authorization(TAG_USAGE_EXPIRE_DATETIME, past_time),
  161. };
  162. AuthorizationSet auth_set(params, array_length(params));
  163. keymaster_error_t kmer_invalid_origination =
  164. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty));
  165. // Pubkey ops allowed.
  166. ASSERT_EQ(KM_ERROR_OK, kmer_invalid_origination);
  167. }
  168. TEST_F(KeymasterBaseTest, TestInvalidUsageExpireTimeOnOrigination) {
  169. keymaster_key_param_t params[] = {
  170. Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  171. Authorization(TAG_USAGE_EXPIRE_DATETIME, past_time),
  172. };
  173. AuthorizationSet auth_set(params, array_length(params));
  174. keymaster_error_t kmer_invalid_origination =
  175. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  176. ASSERT_EQ(KM_ERROR_OK, kmer_invalid_origination);
  177. }
  178. TEST_F(KeymasterBaseTest, TestValidUsageExpireTime) {
  179. keymaster_key_param_t params[] = {
  180. Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
  181. Authorization(TAG_USAGE_EXPIRE_DATETIME, future_time),
  182. };
  183. AuthorizationSet auth_set(params, array_length(params));
  184. keymaster_error_t kmer_valid_usage =
  185. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty));
  186. ASSERT_EQ(KM_ERROR_OK, kmer_valid_usage);
  187. }
  188. TEST_F(KeymasterBaseTest, TestValidSingleUseAccesses) {
  189. keymaster_key_param_t params[] = {
  190. Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  191. };
  192. AuthorizationSet auth_set(params, array_length(params));
  193. keymaster_error_t kmer1 =
  194. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  195. keymaster_error_t kmer2 =
  196. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  197. ASSERT_EQ(KM_ERROR_OK, kmer1);
  198. ASSERT_EQ(KM_ERROR_OK, kmer2);
  199. }
  200. TEST_F(KeymasterBaseTest, TestInvalidMaxOps) {
  201. keymaster_key_param_t params[] = {
  202. Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA),
  203. Authorization(TAG_MAX_USES_PER_BOOT, 4),
  204. };
  205. AuthorizationSet auth_set(params, array_length(params));
  206. ASSERT_EQ(KM_ERROR_OK,
  207. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  208. ASSERT_EQ(KM_ERROR_OK,
  209. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  210. ASSERT_EQ(KM_ERROR_OK,
  211. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  212. ASSERT_EQ(KM_ERROR_OK,
  213. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  214. ASSERT_EQ(KM_ERROR_KEY_MAX_OPS_EXCEEDED,
  215. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  216. // Pubkey ops allowed.
  217. ASSERT_EQ(KM_ERROR_OK,
  218. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  219. }
  220. TEST_F(KeymasterBaseTest, TestOverFlowMaxOpsTable) {
  221. keymaster_key_param_t params[] = {
  222. Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  223. Authorization(TAG_MAX_USES_PER_BOOT, 2),
  224. };
  225. AuthorizationSet auth_set(params, array_length(params));
  226. EXPECT_EQ(KM_ERROR_OK,
  227. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 1 /* key_id */, AuthProxy(auth_set, empty)));
  228. EXPECT_EQ(KM_ERROR_OK,
  229. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 2 /* key_id */, AuthProxy(auth_set, empty)));
  230. EXPECT_EQ(KM_ERROR_OK,
  231. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 3 /* key_id */, AuthProxy(auth_set, empty)));
  232. // Key 4 should fail, because table is full.
  233. EXPECT_EQ(KM_ERROR_TOO_MANY_OPERATIONS,
  234. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 4 /* key_id */, AuthProxy(auth_set, empty)));
  235. // Key 1 still works, because it's already in the table and hasn't reached max.
  236. EXPECT_EQ(KM_ERROR_OK,
  237. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 1 /* key_id */, AuthProxy(auth_set, empty)));
  238. // Key 1 no longer works, because it's reached max.
  239. EXPECT_EQ(KM_ERROR_KEY_MAX_OPS_EXCEEDED,
  240. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 1 /* key_id */, AuthProxy(auth_set, empty)));
  241. // Key 4 should fail, because table is (still and forever) full.
  242. EXPECT_EQ(KM_ERROR_TOO_MANY_OPERATIONS,
  243. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 4 /* key_id */, AuthProxy(auth_set, empty)));
  244. // Pubkey ops allowed.
  245. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */,
  246. AuthProxy(auth_set, empty)));
  247. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 4 /* key_id */,
  248. AuthProxy(auth_set, empty)));
  249. }
  250. TEST_F(KeymasterBaseTest, TestInvalidTimeBetweenOps) {
  251. keymaster_key_param_t params[] = {
  252. Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  253. Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, 10),
  254. };
  255. AuthorizationSet auth_set(params, array_length(params));
  256. keymaster_error_t kmer1 =
  257. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  258. keymaster_error_t kmer2 =
  259. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty));
  260. keymaster_error_t kmer3 =
  261. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty));
  262. ASSERT_EQ(KM_ERROR_OK, kmer1);
  263. kmen.tick(2);
  264. ASSERT_EQ(KM_ERROR_KEY_RATE_LIMIT_EXCEEDED, kmer2);
  265. // Allowed because it's a pubkey op.
  266. ASSERT_EQ(KM_ERROR_OK, kmer3);
  267. }
  268. TEST_F(KeymasterBaseTest, TestValidTimeBetweenOps) {
  269. keymaster_key_param_t params[] = {
  270. Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
  271. Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, 2),
  272. };
  273. AuthorizationSet auth_set(params, array_length(params));
  274. EXPECT_EQ(KM_ERROR_OK,
  275. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  276. kmen.tick();
  277. EXPECT_EQ(KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  278. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  279. kmen.tick();
  280. EXPECT_EQ(KM_ERROR_OK,
  281. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  282. }
  283. TEST_F(KeymasterBaseTest, TestOptTimeoutTableOverflow) {
  284. keymaster_key_param_t params[] = {
  285. Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES),
  286. Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, 4),
  287. Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
  288. };
  289. AuthorizationSet auth_set(params, array_length(params));
  290. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */,
  291. AuthProxy(auth_set, empty)));
  292. kmen.tick();
  293. // Key 1 fails because it's too soon
  294. EXPECT_EQ(
  295. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  296. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */, AuthProxy(auth_set, empty)));
  297. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 2 /* key_id */,
  298. AuthProxy(auth_set, empty)));
  299. kmen.tick();
  300. // Key 1 fails because it's too soon
  301. EXPECT_EQ(
  302. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  303. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */, AuthProxy(auth_set, empty)));
  304. // Key 2 fails because it's too soon
  305. EXPECT_EQ(
  306. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  307. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 2 /* key_id */, AuthProxy(auth_set, empty)));
  308. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 3 /* key_id */,
  309. AuthProxy(auth_set, empty)));
  310. kmen.tick();
  311. // Key 1 fails because it's too soon
  312. EXPECT_EQ(
  313. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  314. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */, AuthProxy(auth_set, empty)));
  315. // Key 2 fails because it's too soon
  316. EXPECT_EQ(
  317. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  318. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 2 /* key_id */, AuthProxy(auth_set, empty)));
  319. // Key 3 fails because it's too soon
  320. EXPECT_EQ(
  321. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  322. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 3 /* key_id */, AuthProxy(auth_set, empty)));
  323. // Key 4 fails because the table is full
  324. EXPECT_EQ(
  325. KM_ERROR_TOO_MANY_OPERATIONS,
  326. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 4 /* key_id */, AuthProxy(auth_set, empty)));
  327. kmen.tick();
  328. // Key 4 succeeds because key 1 expired.
  329. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 4 /* key_id */,
  330. AuthProxy(auth_set, empty)));
  331. // Key 1 fails because the table is full... and key 1 is no longer in it.
  332. EXPECT_EQ(
  333. KM_ERROR_TOO_MANY_OPERATIONS,
  334. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */, AuthProxy(auth_set, empty)));
  335. // Key 2 fails because it's too soon
  336. EXPECT_EQ(
  337. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  338. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 2 /* key_id */, AuthProxy(auth_set, empty)));
  339. // Key 3 fails because it's too soon
  340. EXPECT_EQ(
  341. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  342. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 3 /* key_id */, AuthProxy(auth_set, empty)));
  343. kmen.tick();
  344. // Key 1 succeeds because key 2 expired
  345. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */,
  346. AuthProxy(auth_set, empty)));
  347. // Key 2 fails because the table is full... and key 2 is no longer in it.
  348. EXPECT_EQ(
  349. KM_ERROR_TOO_MANY_OPERATIONS,
  350. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 2 /* key_id */, AuthProxy(auth_set, empty)));
  351. // Key 3 fails because it's too soon
  352. EXPECT_EQ(
  353. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  354. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 3 /* key_id */, AuthProxy(auth_set, empty)));
  355. // Key 4 fails because it's too soon
  356. EXPECT_EQ(
  357. KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  358. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 4 /* key_id */, AuthProxy(auth_set, empty)));
  359. kmen.tick(4);
  360. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */,
  361. AuthProxy(auth_set, empty)));
  362. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 2 /* key_id */,
  363. AuthProxy(auth_set, empty)));
  364. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 3 /* key_id */,
  365. AuthProxy(auth_set, empty)));
  366. }
  367. TEST_F(KeymasterBaseTest, TestPubkeyOptTimeoutTableOverflow) {
  368. keymaster_key_param_t params[] = {
  369. Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA),
  370. Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, 4), Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
  371. };
  372. AuthorizationSet auth_set(params, array_length(params));
  373. EXPECT_EQ(KM_ERROR_OK,
  374. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 1 /* key_id */, AuthProxy(auth_set, empty)));
  375. kmen.tick();
  376. // Key 1 fails because it's too soon
  377. EXPECT_EQ(KM_ERROR_KEY_RATE_LIMIT_EXCEEDED,
  378. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, 1 /* key_id */, AuthProxy(auth_set, empty)));
  379. // Too soo, but pubkey ops allowed.
  380. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, 1 /* key_id */,
  381. AuthProxy(auth_set, empty)));
  382. }
  383. TEST_F(KeymasterBaseTest, TestInvalidPurpose) {
  384. keymaster_purpose_t invalidPurpose1 = static_cast<keymaster_purpose_t>(-1);
  385. keymaster_purpose_t invalidPurpose2 = static_cast<keymaster_purpose_t>(4);
  386. AuthorizationSet auth_set(
  387. AuthorizationSetBuilder().Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY));
  388. EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE,
  389. kmen.AuthorizeOperation(invalidPurpose1, key_id, AuthProxy(auth_set, empty)));
  390. EXPECT_EQ(KM_ERROR_UNSUPPORTED_PURPOSE,
  391. kmen.AuthorizeOperation(invalidPurpose2, key_id, AuthProxy(auth_set, empty)));
  392. }
  393. TEST_F(KeymasterBaseTest, TestIncompatiblePurposeSymmetricKey) {
  394. AuthorizationSet auth_set(AuthorizationSetBuilder()
  395. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES)
  396. .Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY)
  397. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  398. EXPECT_EQ(KM_ERROR_OK,
  399. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  400. EXPECT_EQ(KM_ERROR_OK,
  401. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  402. EXPECT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE,
  403. kmen.AuthorizeOperation(KM_PURPOSE_ENCRYPT, key_id, AuthProxy(auth_set, empty)));
  404. EXPECT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE,
  405. kmen.AuthorizeOperation(KM_PURPOSE_DECRYPT, key_id, AuthProxy(auth_set, empty)));
  406. }
  407. TEST_F(KeymasterBaseTest, TestIncompatiblePurposeAssymmetricKey) {
  408. AuthorizationSet auth_set(AuthorizationSetBuilder()
  409. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  410. .Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY)
  411. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  412. EXPECT_EQ(KM_ERROR_OK,
  413. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  414. EXPECT_EQ(KM_ERROR_OK,
  415. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  416. // This one is allowed because it's a pubkey op.
  417. EXPECT_EQ(KM_ERROR_OK,
  418. kmen.AuthorizeOperation(KM_PURPOSE_ENCRYPT, key_id, AuthProxy(auth_set, empty)));
  419. EXPECT_EQ(KM_ERROR_INCOMPATIBLE_PURPOSE,
  420. kmen.AuthorizeOperation(KM_PURPOSE_DECRYPT, key_id, AuthProxy(auth_set, empty)));
  421. }
  422. TEST_F(KeymasterBaseTest, TestInvalidCallerNonce) {
  423. AuthorizationSet no_caller_nonce(AuthorizationSetBuilder()
  424. .Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT)
  425. .Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT)
  426. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES));
  427. AuthorizationSet caller_nonce(AuthorizationSetBuilder()
  428. .Authorization(TAG_PURPOSE, KM_PURPOSE_ENCRYPT)
  429. .Authorization(TAG_PURPOSE, KM_PURPOSE_DECRYPT)
  430. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_HMAC)
  431. .Authorization(TAG_CALLER_NONCE));
  432. AuthorizationSet begin_params(AuthorizationSetBuilder().Authorization(TAG_NONCE, "foo", 3));
  433. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  434. KM_PURPOSE_ENCRYPT, key_id, AuthProxy(caller_nonce, empty),
  435. begin_params, 0 /* challenge */, true /* is_begin_operation */));
  436. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  437. KM_PURPOSE_DECRYPT, key_id, AuthProxy(caller_nonce, empty),
  438. begin_params, 0 /* challenge */, true /* is_begin_operation */));
  439. EXPECT_EQ(KM_ERROR_CALLER_NONCE_PROHIBITED,
  440. kmen.AuthorizeOperation(KM_PURPOSE_ENCRYPT, key_id, AuthProxy(no_caller_nonce, empty),
  441. begin_params, 0 /* challenge */,
  442. true /* is_begin_operation */));
  443. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  444. KM_PURPOSE_DECRYPT, key_id, AuthProxy(no_caller_nonce, empty),
  445. begin_params, 0 /* challenge */, true /* is_begin_operation */));
  446. }
  447. TEST_F(KeymasterBaseTest, TestBootloaderOnly) {
  448. AuthorizationSet auth_set(AuthorizationSetBuilder()
  449. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN)
  450. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  451. .Authorization(TAG_BOOTLOADER_ONLY));
  452. EXPECT_EQ(KM_ERROR_INVALID_KEY_BLOB,
  453. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  454. // Pubkey ops allowed.
  455. EXPECT_EQ(KM_ERROR_OK,
  456. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty)));
  457. }
  458. TEST_F(KeymasterBaseTest, TestInvalidTag) {
  459. AuthorizationSet auth_set(AuthorizationSetBuilder()
  460. .Authorization(TAG_INVALID)
  461. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  462. EXPECT_EQ(KM_ERROR_INVALID_KEY_BLOB,
  463. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  464. }
  465. TEST_F(KeymasterBaseTest, TestAuthPerOpSuccess) {
  466. hw_auth_token_t token;
  467. memset(&token, 0, sizeof(token));
  468. token.version = HW_AUTH_TOKEN_VERSION;
  469. token.challenge = 99;
  470. token.user_id = 9;
  471. token.authenticator_id = 0;
  472. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  473. token.timestamp = 0;
  474. AuthorizationSet auth_set(AuthorizationSetBuilder()
  475. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  476. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  477. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  478. AuthorizationSet op_params;
  479. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  480. EXPECT_EQ(KM_ERROR_OK,
  481. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  482. op_params, token.challenge, false /* is_begin_operation */));
  483. }
  484. TEST_F(KeymasterBaseTest, TestAuthPerOpInvalidTokenSignature) {
  485. hw_auth_token_t token;
  486. memset(&token, 0, sizeof(token));
  487. token.version = HW_AUTH_TOKEN_VERSION;
  488. token.challenge = 99;
  489. token.user_id = 9;
  490. token.authenticator_id = 0;
  491. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  492. token.timestamp = 0;
  493. AuthorizationSet auth_set(AuthorizationSetBuilder()
  494. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_EC)
  495. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  496. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  497. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  498. AuthorizationSet op_params;
  499. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  500. kmen.set_report_token_valid(false);
  501. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  502. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  503. op_params, token.challenge, false /* is_begin_operation */));
  504. // Pubkey ops allowed.
  505. EXPECT_EQ(KM_ERROR_OK,
  506. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  507. op_params, token.challenge, false /* is_begin_operation */));
  508. }
  509. TEST_F(KeymasterBaseTest, TestAuthPerOpWrongChallenge) {
  510. hw_auth_token_t token;
  511. memset(&token, 0, sizeof(token));
  512. token.version = HW_AUTH_TOKEN_VERSION;
  513. token.challenge = 99;
  514. token.user_id = 9;
  515. token.authenticator_id = 0;
  516. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  517. token.timestamp = 0;
  518. AuthorizationSet auth_set(AuthorizationSetBuilder()
  519. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  520. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  521. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  522. AuthorizationSet op_params;
  523. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  524. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  525. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  526. op_params, token.challenge + 1 /* doesn't match token */,
  527. false /* is_begin_operation */));
  528. }
  529. TEST_F(KeymasterBaseTest, TestAuthPerOpNoAuthType) {
  530. hw_auth_token_t token;
  531. memset(&token, 0, sizeof(token));
  532. token.version = HW_AUTH_TOKEN_VERSION;
  533. token.challenge = 99;
  534. token.user_id = 9;
  535. token.authenticator_id = 0;
  536. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  537. token.timestamp = 0;
  538. AuthorizationSet auth_set(AuthorizationSetBuilder()
  539. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  540. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  541. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  542. AuthorizationSet op_params;
  543. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  544. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  545. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  546. op_params, token.challenge, false /* is_begin_operation */));
  547. // Pubkey ops allowed.
  548. EXPECT_EQ(KM_ERROR_OK,
  549. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  550. op_params, token.challenge, false /* is_begin_operation */));
  551. }
  552. TEST_F(KeymasterBaseTest, TestAuthPerOpWrongAuthType) {
  553. hw_auth_token_t token;
  554. memset(&token, 0, sizeof(token));
  555. token.version = HW_AUTH_TOKEN_VERSION;
  556. token.challenge = 99;
  557. token.user_id = 9;
  558. token.authenticator_id = 0;
  559. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  560. token.timestamp = 0;
  561. AuthorizationSet auth_set(
  562. AuthorizationSetBuilder()
  563. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  564. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  565. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_FINGERPRINT /* doesn't match token */)
  566. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  567. AuthorizationSet op_params;
  568. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  569. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  570. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  571. op_params, token.challenge, false /* is_begin_operation */));
  572. // Pubkey ops allowed.
  573. EXPECT_EQ(KM_ERROR_OK,
  574. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  575. op_params, token.challenge, false /* is_begin_operation */));
  576. }
  577. TEST_F(KeymasterBaseTest, TestAuthPerOpWrongSid) {
  578. hw_auth_token_t token;
  579. memset(&token, 0, sizeof(token));
  580. token.version = HW_AUTH_TOKEN_VERSION;
  581. token.challenge = 99;
  582. token.user_id = 9;
  583. token.authenticator_id = 0;
  584. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  585. token.timestamp = 0;
  586. AuthorizationSet auth_set(
  587. AuthorizationSetBuilder()
  588. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  589. .Authorization(TAG_USER_SECURE_ID, token.user_id + 1 /* doesn't match token */)
  590. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  591. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  592. AuthorizationSet op_params;
  593. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  594. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  595. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  596. op_params, token.challenge, false /* is_begin_operation */));
  597. // Pubkey op allowed.
  598. EXPECT_EQ(KM_ERROR_OK,
  599. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  600. op_params, token.challenge, false /* is_begin_operation */));
  601. }
  602. TEST_F(KeymasterBaseTest, TestAuthPerOpSuccessAlternateSid) {
  603. hw_auth_token_t token;
  604. memset(&token, 0, sizeof(token));
  605. token.version = HW_AUTH_TOKEN_VERSION;
  606. token.challenge = 99;
  607. token.user_id = 9;
  608. token.authenticator_id = 10;
  609. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  610. token.timestamp = 0;
  611. AuthorizationSet auth_set(AuthorizationSetBuilder()
  612. .Authorization(TAG_USER_SECURE_ID, token.authenticator_id)
  613. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  614. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  615. AuthorizationSet op_params;
  616. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  617. EXPECT_EQ(KM_ERROR_OK,
  618. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  619. op_params, token.challenge, false /* is_begin_operation */));
  620. }
  621. TEST_F(KeymasterBaseTest, TestAuthPerOpMissingToken) {
  622. hw_auth_token_t token;
  623. memset(&token, 0, sizeof(token));
  624. token.version = HW_AUTH_TOKEN_VERSION;
  625. token.challenge = 99;
  626. token.user_id = 9;
  627. token.authenticator_id = 0;
  628. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  629. token.timestamp = 0;
  630. AuthorizationSet auth_set(AuthorizationSetBuilder()
  631. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  632. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  633. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  634. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  635. AuthorizationSet op_params;
  636. // During begin we can skip the auth token
  637. EXPECT_EQ(KM_ERROR_OK,
  638. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  639. op_params, token.challenge, true /* is_begin_operation */));
  640. // Afterwards we must have authentication
  641. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  642. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  643. op_params, token.challenge, false /* is_begin_operation */));
  644. // Pubkey ops allowed
  645. EXPECT_EQ(KM_ERROR_OK,
  646. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  647. op_params, token.challenge, false /* is_begin_operation */));
  648. auth_set.Reinitialize(AuthorizationSetBuilder()
  649. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_AES)
  650. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  651. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  652. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN)
  653. .build());
  654. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  655. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  656. op_params, token.challenge, false /* is_begin_operation */));
  657. }
  658. TEST_F(KeymasterBaseTest, TestAuthAndNoAuth) {
  659. AuthorizationSet auth_set(AuthorizationSetBuilder()
  660. .Authorization(TAG_USER_SECURE_ID, 1)
  661. .Authorization(TAG_NO_AUTH_REQUIRED)
  662. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  663. EXPECT_EQ(KM_ERROR_INVALID_KEY_BLOB,
  664. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty)));
  665. }
  666. TEST_F(KeymasterBaseTest, TestTimedAuthSuccess) {
  667. hw_auth_token_t token;
  668. memset(&token, 0, sizeof(token));
  669. token.version = HW_AUTH_TOKEN_VERSION;
  670. token.challenge = 99;
  671. token.user_id = 9;
  672. token.authenticator_id = 0;
  673. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  674. token.timestamp = hton(kmen.current_time());
  675. AuthorizationSet auth_set(AuthorizationSetBuilder()
  676. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  677. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  678. .Authorization(TAG_AUTH_TIMEOUT, 1)
  679. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  680. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  681. AuthorizationSet op_params;
  682. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  683. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  684. KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty), op_params,
  685. 0 /* irrelevant */, false /* is_begin_operation */));
  686. }
  687. TEST_F(KeymasterBaseTest, TestTimedAuthTimedOut) {
  688. hw_auth_token_t token;
  689. memset(&token, 0, sizeof(token));
  690. token.version = HW_AUTH_TOKEN_VERSION;
  691. token.challenge = 99;
  692. token.user_id = 9;
  693. token.authenticator_id = 0;
  694. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  695. token.timestamp = hton(static_cast<uint64_t>(kmen.current_time()));
  696. AuthorizationSet auth_set(AuthorizationSetBuilder()
  697. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  698. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  699. .Authorization(TAG_AUTH_TIMEOUT, 1)
  700. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  701. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  702. AuthorizationSet op_params;
  703. op_params.push_back(Authorization(TAG_AUTH_TOKEN, &token, sizeof(token)));
  704. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  705. KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty), op_params,
  706. 0 /* irrelevant */, false /* is_begin_operation */));
  707. kmen.tick(1);
  708. // token still good
  709. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  710. KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty), op_params,
  711. 0 /* irrelevant */, false /* is_begin_operation */));
  712. kmen.tick(1);
  713. // token expired, not allowed during begin.
  714. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  715. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  716. op_params, 0 /* irrelevant */,
  717. true /* is_begin_operation */));
  718. // token expired, afterwards it's okay.
  719. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  720. KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty), op_params,
  721. 0 /* irrelevant */, false /* is_begin_operation */));
  722. // Pubkey ops allowed.
  723. EXPECT_EQ(KM_ERROR_OK, kmen.AuthorizeOperation(
  724. KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty), op_params,
  725. 0 /* irrelevant */, true /* is_begin_operation */));
  726. }
  727. TEST_F(KeymasterBaseTest, TestTimedAuthMissingToken) {
  728. hw_auth_token_t token;
  729. memset(&token, 0, sizeof(token));
  730. token.version = HW_AUTH_TOKEN_VERSION;
  731. token.challenge = 99;
  732. token.user_id = 9;
  733. token.authenticator_id = 0;
  734. token.authenticator_type = hton(static_cast<uint32_t>(HW_AUTH_PASSWORD));
  735. token.timestamp = hton(static_cast<uint64_t>(kmen.current_time()));
  736. AuthorizationSet auth_set(AuthorizationSetBuilder()
  737. .Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA)
  738. .Authorization(TAG_USER_SECURE_ID, token.user_id)
  739. .Authorization(TAG_AUTH_TIMEOUT, 1)
  740. .Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_ANY)
  741. .Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN));
  742. AuthorizationSet op_params;
  743. // Unlike auth-per-op, must have the auth token during begin.
  744. EXPECT_EQ(KM_ERROR_KEY_USER_NOT_AUTHENTICATED,
  745. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  746. op_params, token.challenge, true /* is_begin_operation */));
  747. // Later we don't check (though begin would fail, so there wouldn't be a later).
  748. EXPECT_EQ(KM_ERROR_OK,
  749. kmen.AuthorizeOperation(KM_PURPOSE_SIGN, key_id, AuthProxy(auth_set, empty),
  750. op_params, token.challenge, false /* is_begin_operation */));
  751. // Pubkey ops allowed.
  752. EXPECT_EQ(KM_ERROR_OK,
  753. kmen.AuthorizeOperation(KM_PURPOSE_VERIFY, key_id, AuthProxy(auth_set, empty),
  754. op_params, token.challenge, true /* is_begin_operation */));
  755. }
  756. TEST_F(KeymasterBaseTest, TestCreateKeyId) {
  757. keymaster_key_blob_t blob = {reinterpret_cast<const uint8_t*>("foobar"), 6};
  758. km_id_t key_id = 0;
  759. EXPECT_TRUE(kmen.CreateKeyId(blob, &key_id));
  760. EXPECT_NE(0U, key_id);
  761. }
  762. }; /* namespace test */
  763. }; /* namespace keymaster */