keystore_utils.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2016 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. #define LOG_TAG "keystore"
  17. #include "keystore_utils.h"
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <log/log.h>
  22. #include <private/android_filesystem_config.h>
  23. #include <private/android_logger.h>
  24. #include <log/log_event_list.h>
  25. #include <keystore/keymaster_types.h>
  26. #include <keystore/keystore_client.h>
  27. #include "blob.h"
  28. size_t readFully(int fd, uint8_t* data, size_t size) {
  29. size_t remaining = size;
  30. while (remaining > 0) {
  31. ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining));
  32. if (n <= 0) {
  33. return size - remaining;
  34. }
  35. data += n;
  36. remaining -= n;
  37. }
  38. return size;
  39. }
  40. size_t writeFully(int fd, uint8_t* data, size_t size) {
  41. size_t remaining = size;
  42. while (remaining > 0) {
  43. ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining));
  44. if (n < 0) {
  45. ALOGW("write failed: %s", strerror(errno));
  46. return size - remaining;
  47. }
  48. data += n;
  49. remaining -= n;
  50. }
  51. if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) {
  52. ALOGW("fsync failed: %s", strerror(errno));
  53. return -1;
  54. }
  55. return size;
  56. }
  57. void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params) {
  58. using namespace keystore;
  59. params->push_back(TAG_PURPOSE, KeyPurpose::SIGN);
  60. params->push_back(TAG_PURPOSE, KeyPurpose::VERIFY);
  61. params->push_back(TAG_PURPOSE, KeyPurpose::ENCRYPT);
  62. params->push_back(TAG_PURPOSE, KeyPurpose::DECRYPT);
  63. params->push_back(TAG_PADDING, PaddingMode::NONE);
  64. if (keyType == EVP_PKEY_RSA) {
  65. params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_SIGN);
  66. params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_ENCRYPT);
  67. params->push_back(TAG_PADDING, PaddingMode::RSA_PSS);
  68. params->push_back(TAG_PADDING, PaddingMode::RSA_OAEP);
  69. }
  70. params->push_back(TAG_DIGEST, Digest::NONE);
  71. params->push_back(TAG_DIGEST, Digest::MD5);
  72. params->push_back(TAG_DIGEST, Digest::SHA1);
  73. params->push_back(TAG_DIGEST, Digest::SHA_2_224);
  74. params->push_back(TAG_DIGEST, Digest::SHA_2_256);
  75. params->push_back(TAG_DIGEST, Digest::SHA_2_384);
  76. params->push_back(TAG_DIGEST, Digest::SHA_2_512);
  77. params->push_back(TAG_NO_AUTH_REQUIRED);
  78. params->push_back(TAG_ORIGINATION_EXPIRE_DATETIME, LLONG_MAX);
  79. params->push_back(TAG_USAGE_EXPIRE_DATETIME, LLONG_MAX);
  80. params->push_back(TAG_ACTIVE_DATETIME, 0);
  81. }
  82. uid_t get_app_id(uid_t uid) {
  83. return uid % AID_USER;
  84. }
  85. uid_t get_user_id(uid_t uid) {
  86. return uid / AID_USER;
  87. }
  88. void log_key_integrity_violation(const char* name, uid_t uid) {
  89. if (!__android_log_security()) return;
  90. android_log_event_list(SEC_TAG_KEY_INTEGRITY_VIOLATION)
  91. << name << int32_t(uid) << LOG_ID_SECURITY;
  92. }
  93. namespace keystore {
  94. hidl_vec<uint8_t> blob2hidlVec(const Blob& blob) {
  95. hidl_vec<uint8_t> result(blob.getValue(), blob.getValue() + blob.getLength());
  96. return result;
  97. }
  98. SecurityLevel flagsToSecurityLevel(int32_t flags) {
  99. switch (flags & (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX)) {
  100. case KEYSTORE_FLAG_FALLBACK:
  101. // treating Strongbox flag as "don't care" if Fallback is set
  102. case (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX):
  103. return SecurityLevel::SOFTWARE;
  104. case KEYSTORE_FLAG_STRONGBOX:
  105. return SecurityLevel::STRONGBOX;
  106. default:
  107. return SecurityLevel::TRUSTED_ENVIRONMENT;
  108. }
  109. }
  110. uint32_t securityLevelToFlags(SecurityLevel secLevel) {
  111. switch (secLevel) {
  112. case SecurityLevel::SOFTWARE:
  113. return KEYSTORE_FLAG_FALLBACK;
  114. case SecurityLevel::STRONGBOX:
  115. return KEYSTORE_FLAG_STRONGBOX;
  116. default:
  117. return 0;
  118. }
  119. }
  120. } // namespace keystore