rsa_keymaster1_key.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2015 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/contexts/soft_keymaster_context.h>
  17. #include <keymaster/legacy_support/rsa_keymaster1_key.h>
  18. #include <keymaster/km_openssl/openssl_utils.h>
  19. #include <keymaster/logger.h>
  20. #include "rsa_keymaster1_operation.h"
  21. namespace keymaster {
  22. RsaKeymaster1KeyFactory::RsaKeymaster1KeyFactory(const SoftwareKeyBlobMaker* blob_maker,
  23. const Keymaster1Engine* engine)
  24. : RsaKeyFactory(blob_maker), engine_(engine),
  25. sign_factory_(new RsaKeymaster1OperationFactory(KM_PURPOSE_SIGN, engine)),
  26. decrypt_factory_(new RsaKeymaster1OperationFactory(KM_PURPOSE_DECRYPT, engine)),
  27. // For pubkey ops we can use the normal operation factories.
  28. verify_factory_(new RsaVerificationOperationFactory),
  29. encrypt_factory_(new RsaEncryptionOperationFactory) {}
  30. static bool is_supported(uint32_t digest) {
  31. return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
  32. }
  33. static void UpdateToWorkAroundUnsupportedDigests(const AuthorizationSet& key_description,
  34. AuthorizationSet* new_description) {
  35. bool have_unsupported_digests = false;
  36. bool have_digest_none = false;
  37. bool have_pad_none = false;
  38. bool have_padding_requiring_digest = false;
  39. for (const keymaster_key_param_t& entry : key_description) {
  40. new_description->push_back(entry);
  41. if (entry.tag == TAG_DIGEST) {
  42. if (entry.enumerated == KM_DIGEST_NONE) {
  43. have_digest_none = true;
  44. } else if (!is_supported(entry.enumerated)) {
  45. LOG_D("Found request for unsupported digest %u", entry.enumerated);
  46. have_unsupported_digests = true;
  47. }
  48. }
  49. if (entry.tag == TAG_PADDING) {
  50. switch (entry.enumerated) {
  51. case KM_PAD_RSA_PSS:
  52. case KM_PAD_RSA_OAEP:
  53. have_padding_requiring_digest = true;
  54. break;
  55. case KM_PAD_NONE:
  56. have_pad_none = true;
  57. break;
  58. }
  59. }
  60. }
  61. if (have_unsupported_digests && !have_digest_none) {
  62. LOG_I("Adding KM_DIGEST_NONE to key authorization, to enable software digesting", 0);
  63. new_description->push_back(TAG_DIGEST, KM_DIGEST_NONE);
  64. }
  65. if (have_unsupported_digests && have_padding_requiring_digest && !have_pad_none) {
  66. LOG_I("Adding KM_PAD_NONE to key authorization, to enable PSS or OAEP software padding", 0);
  67. new_description->push_back(TAG_PADDING, KM_PAD_NONE);
  68. }
  69. }
  70. keymaster_error_t RsaKeymaster1KeyFactory::GenerateKey(const AuthorizationSet& key_description,
  71. KeymasterKeyBlob* key_blob,
  72. AuthorizationSet* hw_enforced,
  73. AuthorizationSet* sw_enforced) const {
  74. AuthorizationSet key_params_copy;
  75. UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
  76. return engine_->GenerateKey(key_params_copy, key_blob, hw_enforced, sw_enforced);
  77. }
  78. keymaster_error_t RsaKeymaster1KeyFactory::ImportKey(
  79. const AuthorizationSet& key_description, keymaster_key_format_t input_key_material_format,
  80. const KeymasterKeyBlob& input_key_material, KeymasterKeyBlob* output_key_blob,
  81. AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced) const {
  82. AuthorizationSet key_params_copy;
  83. UpdateToWorkAroundUnsupportedDigests(key_description, &key_params_copy);
  84. return engine_->ImportKey(key_params_copy, input_key_material_format, input_key_material,
  85. output_key_blob, hw_enforced, sw_enforced);
  86. }
  87. keymaster_error_t RsaKeymaster1KeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
  88. const AuthorizationSet& additional_params,
  89. AuthorizationSet&& hw_enforced,
  90. AuthorizationSet&& sw_enforced,
  91. UniquePtr<Key>* key) const {
  92. if (!key)
  93. return KM_ERROR_OUTPUT_PARAMETER_NULL;
  94. keymaster_error_t error;
  95. RSA_Ptr rsa(engine_->BuildRsaKey(key_material, additional_params, &error));
  96. if (!rsa.get())
  97. return error;
  98. key->reset(new (std::nothrow)
  99. RsaKeymaster1Key(rsa.release(), move(hw_enforced), move(sw_enforced), this));
  100. if (!(*key))
  101. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  102. (*key)->key_material() = move(key_material);
  103. return KM_ERROR_OK;
  104. }
  105. OperationFactory* RsaKeymaster1KeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
  106. switch (purpose) {
  107. case KM_PURPOSE_SIGN:
  108. return sign_factory_.get();
  109. case KM_PURPOSE_VERIFY:
  110. return verify_factory_.get();
  111. case KM_PURPOSE_ENCRYPT:
  112. return encrypt_factory_.get();
  113. case KM_PURPOSE_DECRYPT:
  114. return decrypt_factory_.get();
  115. case KM_PURPOSE_DERIVE_KEY:
  116. case KM_PURPOSE_WRAP:
  117. break;
  118. }
  119. return nullptr;
  120. }
  121. } // namespace keymaster