android_keymaster.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright 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 <keymaster/android_keymaster.h>
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <stddef.h>
  20. #include <keymaster/UniquePtr.h>
  21. #include <keymaster/android_keymaster_utils.h>
  22. #include <keymaster/key.h>
  23. #include <keymaster/key_blob_utils/ae.h>
  24. #include <keymaster/key_factory.h>
  25. #include <keymaster/keymaster_context.h>
  26. #include <keymaster/km_openssl/openssl_err.h>
  27. #include <keymaster/operation.h>
  28. #include <keymaster/operation_table.h>
  29. namespace keymaster {
  30. namespace {
  31. const uint8_t MAJOR_VER = 2;
  32. const uint8_t MINOR_VER = 0;
  33. const uint8_t SUBMINOR_VER = 0;
  34. keymaster_error_t CheckVersionInfo(const AuthorizationSet& tee_enforced,
  35. const AuthorizationSet& sw_enforced,
  36. const KeymasterContext& context) {
  37. uint32_t os_version;
  38. uint32_t os_patchlevel;
  39. context.GetSystemVersion(&os_version, &os_patchlevel);
  40. uint32_t key_os_patchlevel;
  41. if (tee_enforced.GetTagValue(TAG_OS_PATCHLEVEL, &key_os_patchlevel) ||
  42. sw_enforced.GetTagValue(TAG_OS_PATCHLEVEL, &key_os_patchlevel)) {
  43. if (key_os_patchlevel < os_patchlevel)
  44. return KM_ERROR_KEY_REQUIRES_UPGRADE;
  45. else if (key_os_patchlevel > os_patchlevel)
  46. return KM_ERROR_INVALID_KEY_BLOB;
  47. }
  48. return KM_ERROR_OK;
  49. }
  50. } // anonymous namespace
  51. AndroidKeymaster::AndroidKeymaster(KeymasterContext* context, size_t operation_table_size)
  52. : context_(context), operation_table_(new(std::nothrow) OperationTable(operation_table_size)) {}
  53. AndroidKeymaster::~AndroidKeymaster() {}
  54. AndroidKeymaster::AndroidKeymaster(AndroidKeymaster&& other)
  55. : context_(move(other.context_)), operation_table_(move(other.operation_table_)) {}
  56. // TODO(swillden): Unify support analysis. Right now, we have per-keytype methods that determine if
  57. // specific modes, padding, etc. are supported for that key type, and AndroidKeymaster also has
  58. // methods that return the same information. They'll get out of sync. Best to put the knowledge in
  59. // the keytypes and provide some mechanism for AndroidKeymaster to query the keytypes for the
  60. // information.
  61. template <typename T>
  62. bool check_supported(const KeymasterContext& context, keymaster_algorithm_t algorithm,
  63. SupportedResponse<T>* response) {
  64. if (context.GetKeyFactory(algorithm) == nullptr) {
  65. response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
  66. return false;
  67. }
  68. return true;
  69. }
  70. void AndroidKeymaster::GetVersion(const GetVersionRequest&, GetVersionResponse* rsp) {
  71. if (rsp == nullptr)
  72. return;
  73. rsp->major_ver = MAJOR_VER;
  74. rsp->minor_ver = MINOR_VER;
  75. rsp->subminor_ver = SUBMINOR_VER;
  76. rsp->error = KM_ERROR_OK;
  77. }
  78. void AndroidKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& /* request */,
  79. SupportedAlgorithmsResponse* response) {
  80. if (response == nullptr)
  81. return;
  82. response->error = KM_ERROR_OK;
  83. size_t algorithm_count = 0;
  84. const keymaster_algorithm_t* algorithms = context_->GetSupportedAlgorithms(&algorithm_count);
  85. if (algorithm_count == 0)
  86. return;
  87. response->results_length = algorithm_count;
  88. response->results = dup_array(algorithms, algorithm_count);
  89. if (!response->results)
  90. response->error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
  91. }
  92. template <typename T>
  93. void GetSupported(const KeymasterContext& context, keymaster_algorithm_t algorithm,
  94. keymaster_purpose_t purpose,
  95. const T* (OperationFactory::*get_supported_method)(size_t* count) const,
  96. SupportedResponse<T>* response) {
  97. if (response == nullptr || !check_supported(context, algorithm, response))
  98. return;
  99. const OperationFactory* factory = context.GetOperationFactory(algorithm, purpose);
  100. if (!factory) {
  101. response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
  102. return;
  103. }
  104. size_t count;
  105. const T* supported = (factory->*get_supported_method)(&count);
  106. response->SetResults(supported, count);
  107. }
  108. void AndroidKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
  109. SupportedBlockModesResponse* response) {
  110. GetSupported(*context_, request.algorithm, request.purpose,
  111. &OperationFactory::SupportedBlockModes, response);
  112. }
  113. void AndroidKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
  114. SupportedPaddingModesResponse* response) {
  115. GetSupported(*context_, request.algorithm, request.purpose,
  116. &OperationFactory::SupportedPaddingModes, response);
  117. }
  118. void AndroidKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
  119. SupportedDigestsResponse* response) {
  120. GetSupported(*context_, request.algorithm, request.purpose, &OperationFactory::SupportedDigests,
  121. response);
  122. }
  123. void AndroidKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
  124. SupportedImportFormatsResponse* response) {
  125. if (response == nullptr || !check_supported(*context_, request.algorithm, response))
  126. return;
  127. size_t count;
  128. const keymaster_key_format_t* formats =
  129. context_->GetKeyFactory(request.algorithm)->SupportedImportFormats(&count);
  130. response->SetResults(formats, count);
  131. }
  132. void AndroidKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
  133. SupportedExportFormatsResponse* response) {
  134. if (response == nullptr || !check_supported(*context_, request.algorithm, response))
  135. return;
  136. size_t count;
  137. const keymaster_key_format_t* formats =
  138. context_->GetKeyFactory(request.algorithm)->SupportedExportFormats(&count);
  139. response->SetResults(formats, count);
  140. }
  141. GetHmacSharingParametersResponse AndroidKeymaster::GetHmacSharingParameters() {
  142. GetHmacSharingParametersResponse response;
  143. KeymasterEnforcement* policy = context_->enforcement_policy();
  144. if (!policy) {
  145. response.error = KM_ERROR_UNIMPLEMENTED;
  146. return response;
  147. }
  148. response.error = policy->GetHmacSharingParameters(&response.params);
  149. return response;
  150. }
  151. ComputeSharedHmacResponse
  152. AndroidKeymaster::ComputeSharedHmac(const ComputeSharedHmacRequest& request) {
  153. ComputeSharedHmacResponse response;
  154. KeymasterEnforcement* policy = context_->enforcement_policy();
  155. if (!policy) {
  156. response.error = KM_ERROR_UNIMPLEMENTED;
  157. return response;
  158. }
  159. response.error = policy->ComputeSharedHmac(request.params_array, &response.sharing_check);
  160. return response;
  161. }
  162. VerifyAuthorizationResponse
  163. AndroidKeymaster::VerifyAuthorization(const VerifyAuthorizationRequest& request) {
  164. KeymasterEnforcement* policy = context_->enforcement_policy();
  165. if (!policy) {
  166. VerifyAuthorizationResponse response;
  167. response.error = KM_ERROR_UNIMPLEMENTED;
  168. return response;
  169. }
  170. return policy->VerifyAuthorization(request);
  171. }
  172. void AndroidKeymaster::AddRngEntropy(const AddEntropyRequest& request,
  173. AddEntropyResponse* response) {
  174. response->error = context_->AddRngEntropy(request.random_data.peek_read(),
  175. request.random_data.available_read());
  176. }
  177. void AndroidKeymaster::GenerateKey(const GenerateKeyRequest& request,
  178. GenerateKeyResponse* response) {
  179. if (response == nullptr)
  180. return;
  181. keymaster_algorithm_t algorithm;
  182. const KeyFactory* factory = nullptr;
  183. UniquePtr<Key> key;
  184. if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
  185. !(factory = context_->GetKeyFactory(algorithm)))
  186. response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
  187. else {
  188. KeymasterKeyBlob key_blob;
  189. response->enforced.Clear();
  190. response->unenforced.Clear();
  191. response->error = factory->GenerateKey(request.key_description, &key_blob,
  192. &response->enforced, &response->unenforced);
  193. if (response->error == KM_ERROR_OK)
  194. response->key_blob = key_blob.release();
  195. }
  196. }
  197. void AndroidKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
  198. GetKeyCharacteristicsResponse* response) {
  199. if (response == nullptr)
  200. return;
  201. UniquePtr<Key> key;
  202. response->error =
  203. context_->ParseKeyBlob(KeymasterKeyBlob(request.key_blob), request.additional_params,
  204. &key);
  205. if (response->error != KM_ERROR_OK)
  206. return;
  207. // scavenge the key object for the auth lists
  208. response->enforced = move(key->hw_enforced());
  209. response->unenforced = move(key->sw_enforced());
  210. response->error = CheckVersionInfo(response->enforced, response->unenforced, *context_);
  211. }
  212. void AndroidKeymaster::BeginOperation(const BeginOperationRequest& request,
  213. BeginOperationResponse* response) {
  214. if (response == nullptr)
  215. return;
  216. response->op_handle = 0;
  217. const KeyFactory* key_factory;
  218. UniquePtr<Key> key;
  219. response->error = LoadKey(request.key_blob, request.additional_params, &key_factory, &key);
  220. if (response->error != KM_ERROR_OK)
  221. return;
  222. response->error = KM_ERROR_UNKNOWN_ERROR;
  223. keymaster_algorithm_t key_algorithm;
  224. if (!key->authorizations().GetTagValue(TAG_ALGORITHM, &key_algorithm))
  225. return;
  226. response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
  227. OperationFactory* factory = key_factory->GetOperationFactory(request.purpose);
  228. if (!factory) return;
  229. OperationPtr operation(
  230. factory->CreateOperation(move(*key), request.additional_params, &response->error));
  231. if (operation.get() == nullptr) return;
  232. if (context_->enforcement_policy()) {
  233. km_id_t key_id;
  234. response->error = KM_ERROR_UNKNOWN_ERROR;
  235. if (!context_->enforcement_policy()->CreateKeyId(request.key_blob, &key_id)) return;
  236. operation->set_key_id(key_id);
  237. response->error = context_->enforcement_policy()->AuthorizeOperation(
  238. request.purpose, key_id, operation->authorizations(), request.additional_params,
  239. 0 /* op_handle */, true /* is_begin_operation */);
  240. if (response->error != KM_ERROR_OK) return;
  241. }
  242. response->output_params.Clear();
  243. response->error = operation->Begin(request.additional_params, &response->output_params);
  244. if (response->error != KM_ERROR_OK)
  245. return;
  246. response->op_handle = operation->operation_handle();
  247. response->error = operation_table_->Add(move(operation));
  248. }
  249. void AndroidKeymaster::UpdateOperation(const UpdateOperationRequest& request,
  250. UpdateOperationResponse* response) {
  251. if (response == nullptr)
  252. return;
  253. response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
  254. Operation* operation = operation_table_->Find(request.op_handle);
  255. if (operation == nullptr)
  256. return;
  257. if (context_->enforcement_policy()) {
  258. response->error = context_->enforcement_policy()->AuthorizeOperation(
  259. operation->purpose(), operation->key_id(), operation->authorizations(),
  260. request.additional_params, request.op_handle, false /* is_begin_operation */);
  261. if (response->error != KM_ERROR_OK) {
  262. operation_table_->Delete(request.op_handle);
  263. return;
  264. }
  265. }
  266. response->error =
  267. operation->Update(request.additional_params, request.input, &response->output_params,
  268. &response->output, &response->input_consumed);
  269. if (response->error != KM_ERROR_OK) {
  270. // Any error invalidates the operation.
  271. operation_table_->Delete(request.op_handle);
  272. }
  273. }
  274. void AndroidKeymaster::FinishOperation(const FinishOperationRequest& request,
  275. FinishOperationResponse* response) {
  276. if (response == nullptr)
  277. return;
  278. response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
  279. Operation* operation = operation_table_->Find(request.op_handle);
  280. if (operation == nullptr)
  281. return;
  282. if (context_->enforcement_policy()) {
  283. response->error = context_->enforcement_policy()->AuthorizeOperation(
  284. operation->purpose(), operation->key_id(), operation->authorizations(),
  285. request.additional_params, request.op_handle, false /* is_begin_operation */);
  286. if (response->error != KM_ERROR_OK) {
  287. operation_table_->Delete(request.op_handle);
  288. return;
  289. }
  290. }
  291. response->error = operation->Finish(request.additional_params, request.input, request.signature,
  292. &response->output_params, &response->output);
  293. operation_table_->Delete(request.op_handle);
  294. }
  295. void AndroidKeymaster::AbortOperation(const AbortOperationRequest& request,
  296. AbortOperationResponse* response) {
  297. if (!response)
  298. return;
  299. Operation* operation = operation_table_->Find(request.op_handle);
  300. if (!operation) {
  301. response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
  302. return;
  303. }
  304. response->error = operation->Abort();
  305. operation_table_->Delete(request.op_handle);
  306. }
  307. void AndroidKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
  308. if (response == nullptr)
  309. return;
  310. UniquePtr<Key> key;
  311. response->error =
  312. context_->ParseKeyBlob(KeymasterKeyBlob(request.key_blob), request.additional_params, &key);
  313. if (response->error != KM_ERROR_OK)
  314. return;
  315. UniquePtr<uint8_t[]> out_key;
  316. size_t size;
  317. response->error = key->formatted_key_material(request.key_format, &out_key, &size);
  318. if (response->error == KM_ERROR_OK) {
  319. response->key_data = out_key.release();
  320. response->key_data_length = size;
  321. }
  322. }
  323. void AndroidKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
  324. if (!response)
  325. return;
  326. const KeyFactory* key_factory;
  327. UniquePtr<Key> key;
  328. response->error = LoadKey(request.key_blob, request.attest_params,
  329. &key_factory, &key);
  330. if (response->error != KM_ERROR_OK)
  331. return;
  332. keymaster_blob_t attestation_application_id;
  333. if (request.attest_params.GetTagValue(TAG_ATTESTATION_APPLICATION_ID,
  334. &attestation_application_id)) {
  335. key->sw_enforced().push_back(TAG_ATTESTATION_APPLICATION_ID, attestation_application_id);
  336. }
  337. CertChainPtr certchain;
  338. response->error = context_->GenerateAttestation(*key, request.attest_params, &certchain);
  339. if (response->error == KM_ERROR_OK) {
  340. response->certificate_chain = *certchain;
  341. // response->certificate_chain took possession of secondary resources. So we shallowly
  342. // delete the keymaster_cert_chain_t object, but nothing else.
  343. // TODO Can we switch to managed types eventually?
  344. delete certchain.release();
  345. }
  346. }
  347. void AndroidKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
  348. if (!response)
  349. return;
  350. KeymasterKeyBlob upgraded_key;
  351. response->error = context_->UpgradeKeyBlob(KeymasterKeyBlob(request.key_blob),
  352. request.upgrade_params, &upgraded_key);
  353. if (response->error != KM_ERROR_OK)
  354. return;
  355. response->upgraded_key = upgraded_key.release();
  356. }
  357. void AndroidKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
  358. if (response == nullptr)
  359. return;
  360. keymaster_algorithm_t algorithm;
  361. const KeyFactory* factory = nullptr;
  362. UniquePtr<Key> key;
  363. if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
  364. !(factory = context_->GetKeyFactory(algorithm)))
  365. response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
  366. else {
  367. keymaster_key_blob_t key_material = {request.key_data, request.key_data_length};
  368. KeymasterKeyBlob key_blob;
  369. response->error = factory->ImportKey(request.key_description, request.key_format,
  370. KeymasterKeyBlob(key_material), &key_blob,
  371. &response->enforced, &response->unenforced);
  372. if (response->error == KM_ERROR_OK)
  373. response->key_blob = key_blob.release();
  374. }
  375. }
  376. void AndroidKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
  377. if (!response)
  378. return;
  379. response->error = context_->DeleteKey(KeymasterKeyBlob(request.key_blob));
  380. }
  381. void AndroidKeymaster::DeleteAllKeys(const DeleteAllKeysRequest&, DeleteAllKeysResponse* response) {
  382. if (!response)
  383. return;
  384. response->error = context_->DeleteAllKeys();
  385. }
  386. void AndroidKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
  387. if (!response)
  388. return;
  389. response->error = context_->SetSystemVersion(request.os_version, request.os_patchlevel);
  390. }
  391. bool AndroidKeymaster::has_operation(keymaster_operation_handle_t op_handle) const {
  392. return operation_table_->Find(op_handle) != nullptr;
  393. }
  394. keymaster_error_t AndroidKeymaster::LoadKey(const keymaster_key_blob_t& key_blob,
  395. const AuthorizationSet& additional_params,
  396. const KeyFactory** factory, UniquePtr<Key>* key) {
  397. KeymasterKeyBlob key_material;
  398. keymaster_error_t error = context_->ParseKeyBlob(KeymasterKeyBlob(key_blob), additional_params,
  399. key);
  400. if (error != KM_ERROR_OK)
  401. return error;
  402. if (factory) *factory = (*key)->key_factory();
  403. return CheckVersionInfo((*key)->hw_enforced(), (*key)->sw_enforced(), *context_);
  404. }
  405. void AndroidKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
  406. ImportWrappedKeyResponse* response) {
  407. if (!response) return;
  408. KeymasterKeyBlob secret_key;
  409. AuthorizationSet key_description;
  410. keymaster_key_format_t key_format;
  411. response->error =
  412. context_->UnwrapKey(request.wrapped_key, request.wrapping_key, request.additional_params,
  413. request.masking_key, &key_description, &key_format, &secret_key);
  414. if (response->error != KM_ERROR_OK) {
  415. return;
  416. }
  417. int sid_idx = key_description.find(TAG_USER_SECURE_ID);
  418. if (sid_idx != -1) {
  419. uint8_t sids = key_description[sid_idx].long_integer;
  420. if (!key_description.erase(sid_idx)) {
  421. response->error = KM_ERROR_UNKNOWN_ERROR;
  422. return;
  423. }
  424. if (sids & HW_AUTH_PASSWORD) {
  425. key_description.push_back(TAG_USER_SECURE_ID, request.password_sid);
  426. }
  427. if (sids & HW_AUTH_FINGERPRINT) {
  428. key_description.push_back(TAG_USER_SECURE_ID, request.biometric_sid);
  429. }
  430. }
  431. keymaster_algorithm_t algorithm;
  432. const KeyFactory* factory = nullptr;
  433. if (!key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
  434. !(factory = context_->GetKeyFactory(algorithm))) {
  435. response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
  436. } else {
  437. KeymasterKeyBlob key_blob;
  438. response->error =
  439. factory->ImportKey(key_description, key_format, KeymasterKeyBlob(secret_key), &key_blob,
  440. &response->enforced, &response->unenforced);
  441. if (response->error == KM_ERROR_OK) {
  442. response->key_blob = key_blob;
  443. }
  444. }
  445. }
  446. } // namespace keymaster