Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 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 | #include "keystore_attestation_id.h" |
| 17 | |
| 18 | #define LOG_TAG "keystore_att_id" |
| 19 | |
Logan Chien | cdc813f | 2018-04-23 13:52:28 +0800 | [diff] [blame] | 20 | #include <log/log.h> |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/Parcel.h> |
| 28 | #include <binder/Parcelable.h> |
| 29 | #include <binder/PersistableBundle.h> |
| 30 | |
| 31 | #include <android/security/keymaster/BpKeyAttestationApplicationIdProvider.h> |
| 32 | #include <android/security/keymaster/IKeyAttestationApplicationIdProvider.h> |
| 33 | #include <keystore/KeyAttestationApplicationId.h> |
| 34 | #include <keystore/KeyAttestationPackageInfo.h> |
| 35 | #include <keystore/Signature.h> |
| 36 | |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> /* for AID_SYSTEM */ |
| 38 | |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 39 | #include <openssl/asn1t.h> |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 40 | #include <openssl/bn.h> |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 41 | #include <openssl/sha.h> |
| 42 | |
| 43 | #include <utils/String8.h> |
| 44 | |
| 45 | namespace android { |
| 46 | |
| 47 | namespace { |
| 48 | |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 49 | constexpr const char* kAttestationSystemPackageName = "AndroidSystem"; |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 50 | constexpr const char* kUnknownPackageName = "UnknownPackage"; |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 51 | |
| 52 | std::vector<uint8_t> signature2SHA256(const content::pm::Signature& sig) { |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 53 | std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH); |
| 54 | SHA256(sig.data().data(), sig.data().size(), digest_buffer.data()); |
| 55 | return digest_buffer; |
| 56 | } |
| 57 | |
| 58 | using ::android::security::keymaster::BpKeyAttestationApplicationIdProvider; |
| 59 | |
| 60 | class KeyAttestationApplicationIdProvider : public BpKeyAttestationApplicationIdProvider { |
| 61 | public: |
| 62 | KeyAttestationApplicationIdProvider(); |
| 63 | |
| 64 | static KeyAttestationApplicationIdProvider& get(); |
| 65 | |
| 66 | private: |
| 67 | android::sp<android::IServiceManager> service_manager_; |
| 68 | }; |
| 69 | |
| 70 | KeyAttestationApplicationIdProvider& KeyAttestationApplicationIdProvider::get() { |
| 71 | static KeyAttestationApplicationIdProvider mpm; |
| 72 | return mpm; |
| 73 | } |
| 74 | |
| 75 | KeyAttestationApplicationIdProvider::KeyAttestationApplicationIdProvider() |
| 76 | : BpKeyAttestationApplicationIdProvider( |
| 77 | android::defaultServiceManager()->getService(String16("sec_key_att_app_id_provider"))) {} |
| 78 | |
| 79 | DECLARE_STACK_OF(ASN1_OCTET_STRING); |
| 80 | |
| 81 | typedef struct km_attestation_package_info { |
| 82 | ASN1_OCTET_STRING* package_name; |
| 83 | ASN1_INTEGER* version; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 84 | } KM_ATTESTATION_PACKAGE_INFO; |
| 85 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 86 | // Estimated size: |
| 87 | // 4 bytes for the package name + package_name length |
| 88 | // 11 bytes for the version (2 bytes header and up to 9 bytes of data). |
| 89 | constexpr size_t AAID_PKG_INFO_OVERHEAD = 15; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 90 | ASN1_SEQUENCE(KM_ATTESTATION_PACKAGE_INFO) = { |
| 91 | ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, package_name, ASN1_OCTET_STRING), |
| 92 | ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, version, ASN1_INTEGER), |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 93 | } ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO); |
| 94 | IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO); |
| 95 | |
| 96 | DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO); |
| 97 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 98 | // Estimated size: |
| 99 | // See estimate above for the stack of package infos. |
| 100 | // 34 (32 + 2) bytes for each signature digest. |
| 101 | constexpr size_t AAID_SIGNATURE_SIZE = 34; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 102 | typedef struct km_attestation_application_id { |
| 103 | STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos; |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 104 | STACK_OF(ASN1_OCTET_STRING) * signature_digests; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 105 | } KM_ATTESTATION_APPLICATION_ID; |
| 106 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 107 | // Estimated overhead: |
| 108 | // 4 for the header of the octet string containing the fully-encoded data. |
| 109 | // 4 for the sequence header. |
| 110 | // 4 for the header of the package info set. |
| 111 | // 4 for the header of the signature set. |
| 112 | constexpr size_t AAID_GENERAL_OVERHEAD = 16; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 113 | ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = { |
| 114 | ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO), |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 115 | ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING), |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 116 | } ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID); |
| 117 | IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID); |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 118 | |
| 119 | } // namespace |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 120 | |
| 121 | } // namespace android |
| 122 | |
| 123 | namespace std { |
| 124 | template <> struct default_delete<android::KM_ATTESTATION_PACKAGE_INFO> { |
| 125 | void operator()(android::KM_ATTESTATION_PACKAGE_INFO* p) { |
| 126 | android::KM_ATTESTATION_PACKAGE_INFO_free(p); |
| 127 | } |
| 128 | }; |
| 129 | template <> struct default_delete<ASN1_OCTET_STRING> { |
| 130 | void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); } |
| 131 | }; |
| 132 | template <> struct default_delete<android::KM_ATTESTATION_APPLICATION_ID> { |
| 133 | void operator()(android::KM_ATTESTATION_APPLICATION_ID* p) { |
| 134 | android::KM_ATTESTATION_APPLICATION_ID_free(p); |
| 135 | } |
| 136 | }; |
| 137 | } // namespace std |
| 138 | |
| 139 | namespace android { |
| 140 | namespace security { |
| 141 | namespace { |
| 142 | |
| 143 | using ::android::security::keymaster::KeyAttestationApplicationId; |
| 144 | using ::android::security::keymaster::KeyAttestationPackageInfo; |
| 145 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 146 | status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo, |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 147 | std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO>* attestation_package_info_ptr) { |
| 148 | |
| 149 | if (!attestation_package_info_ptr) return BAD_VALUE; |
| 150 | auto& attestation_package_info = *attestation_package_info_ptr; |
| 151 | |
| 152 | attestation_package_info.reset(KM_ATTESTATION_PACKAGE_INFO_new()); |
| 153 | if (!attestation_package_info.get()) return NO_MEMORY; |
| 154 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 155 | if (!pinfo.package_name()) { |
| 156 | ALOGE("Key attestation package info lacks package name"); |
| 157 | return BAD_VALUE; |
| 158 | } |
| 159 | |
| 160 | std::string pkg_name(String8(*pinfo.package_name()).string()); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 161 | if (!ASN1_OCTET_STRING_set(attestation_package_info->package_name, |
| 162 | reinterpret_cast<const unsigned char*>(pkg_name.data()), |
| 163 | pkg_name.size())) { |
| 164 | return UNKNOWN_ERROR; |
| 165 | } |
| 166 | |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 167 | BIGNUM* bn_version = BN_new(); |
| 168 | if (bn_version == nullptr) { |
| 169 | return NO_MEMORY; |
| 170 | } |
| 171 | if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.version_code())) != 1) { |
| 172 | BN_free(bn_version); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 173 | return UNKNOWN_ERROR; |
| 174 | } |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 175 | status_t retval = NO_ERROR; |
| 176 | if (BN_to_ASN1_INTEGER(bn_version, attestation_package_info->version) == nullptr) { |
| 177 | retval = UNKNOWN_ERROR; |
| 178 | } |
| 179 | BN_free(bn_version); |
| 180 | return retval; |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 181 | } |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 182 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 183 | /* The following function are not used. They are mentioned here to silence |
| 184 | * warnings about them not being used. |
| 185 | */ |
| 186 | void unused_functions_silencer() __attribute__((unused)); |
| 187 | void unused_functions_silencer() { |
| 188 | i2d_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr); |
| 189 | d2i_KM_ATTESTATION_APPLICATION_ID(nullptr, nullptr, 0); |
| 190 | d2i_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr, 0); |
| 191 | } |
| 192 | |
| 193 | } // namespace |
| 194 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 195 | StatusOr<std::vector<uint8_t>> |
| 196 | build_attestation_application_id(const KeyAttestationApplicationId& key_attestation_id) { |
| 197 | auto attestation_id = |
| 198 | std::unique_ptr<KM_ATTESTATION_APPLICATION_ID>(KM_ATTESTATION_APPLICATION_ID_new()); |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 199 | size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 200 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 201 | auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos); |
| 202 | |
| 203 | if (key_attestation_id.pinfos_begin() == key_attestation_id.pinfos_end()) return BAD_VALUE; |
| 204 | |
| 205 | for (auto pinfo = key_attestation_id.pinfos_begin(); pinfo != key_attestation_id.pinfos_end(); |
| 206 | ++pinfo) { |
| 207 | if (!pinfo->package_name()) { |
| 208 | ALOGE("Key attestation package info lacks package name"); |
| 209 | return BAD_VALUE; |
| 210 | } |
| 211 | std::string package_name(String8(*pinfo->package_name()).string()); |
| 212 | std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO> attestation_package_info; |
| 213 | auto rc = build_attestation_package_info(*pinfo, &attestation_package_info); |
| 214 | if (rc != NO_ERROR) { |
| 215 | ALOGE("Building DER attestation package info failed %d", rc); |
| 216 | return rc; |
| 217 | } |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 218 | estimated_encoded_size += AAID_PKG_INFO_OVERHEAD + package_name.size(); |
| 219 | if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) { |
| 220 | break; |
| 221 | } |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 222 | if (!sk_push(attestation_pinfo_stack, attestation_package_info.get())) { |
| 223 | return NO_MEMORY; |
| 224 | } |
| 225 | // if push succeeded, the stack takes ownership |
| 226 | attestation_package_info.release(); |
| 227 | } |
| 228 | |
| 229 | /** Apps can only share a uid iff they were signed with the same certificate(s). Because the |
| 230 | * signature field actually holds the signing certificate, rather than a signature, we can |
| 231 | * simply use the set of signature digests of the first package info. |
| 232 | */ |
| 233 | const auto& pinfo = *key_attestation_id.pinfos_begin(); |
| 234 | std::vector<std::vector<uint8_t>> signature_digests; |
| 235 | |
| 236 | for (auto sig = pinfo.sigs_begin(); sig != pinfo.sigs_end(); ++sig) { |
| 237 | signature_digests.push_back(signature2SHA256(*sig)); |
| 238 | } |
| 239 | |
| 240 | auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 241 | for (auto si : signature_digests) { |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 242 | estimated_encoded_size += AAID_SIGNATURE_SIZE; |
| 243 | if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) { |
| 244 | break; |
| 245 | } |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 246 | auto asn1_item = std::unique_ptr<ASN1_OCTET_STRING>(ASN1_OCTET_STRING_new()); |
| 247 | if (!asn1_item) return NO_MEMORY; |
| 248 | if (!ASN1_OCTET_STRING_set(asn1_item.get(), si.data(), si.size())) { |
| 249 | return UNKNOWN_ERROR; |
| 250 | } |
| 251 | if (!sk_push(signature_digest_stack, asn1_item.get())) { |
| 252 | return NO_MEMORY; |
| 253 | } |
| 254 | asn1_item.release(); // if push succeeded, the stack takes ownership |
| 255 | } |
| 256 | |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 257 | int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 258 | if (len < 0) return UNKNOWN_ERROR; |
| 259 | |
| 260 | std::vector<uint8_t> result(len); |
| 261 | uint8_t* p = result.data(); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 262 | len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 263 | if (len < 0) return UNKNOWN_ERROR; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 264 | |
| 265 | return result; |
| 266 | } |
| 267 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 268 | StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) { |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 269 | KeyAttestationApplicationId key_attestation_id; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 270 | |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 271 | if (uid == AID_SYSTEM) { |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 272 | /* Use a fixed ID for system callers */ |
| 273 | auto pinfo = std::make_unique<KeyAttestationPackageInfo>( |
| 274 | String16(kAttestationSystemPackageName), 1 /* version code */, |
| 275 | std::make_shared<KeyAttestationPackageInfo::SignaturesVector>()); |
| 276 | key_attestation_id = KeyAttestationApplicationId(std::move(pinfo)); |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 277 | } else { |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 278 | /* Get the attestation application ID from package manager */ |
| 279 | auto& pm = KeyAttestationApplicationIdProvider::get(); |
| 280 | auto status = pm.getKeyAttestationApplicationId(uid, &key_attestation_id); |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 281 | // Package Manager call has failed, perform attestation but indicate that the |
| 282 | // caller is unknown. |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 283 | if (!status.isOk()) { |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 284 | ALOGW("package manager request for key attestation ID failed with: %s %d", |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 285 | status.exceptionMessage().string(), status.exceptionCode()); |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 286 | auto pinfo = std::make_unique<KeyAttestationPackageInfo>( |
| 287 | String16(kUnknownPackageName), 1 /* version code */, |
| 288 | std::make_shared<KeyAttestationPackageInfo::SignaturesVector>()); |
| 289 | key_attestation_id = KeyAttestationApplicationId(std::move(pinfo)); |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 290 | } |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* DER encode the attestation application ID */ |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 294 | return build_attestation_application_id(key_attestation_id); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | } // namespace security |
| 298 | } // namespace android |