2016-06-01 22:57:40 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2020-01-30 22:20:07 +01:00
|
|
|
|
|
|
|
#include <keystore/keystore_attestation_id.h>
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
#define LOG_TAG "keystore_att_id"
|
|
|
|
|
2018-04-23 07:52:28 +02:00
|
|
|
#include <log/log.h>
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <binder/IServiceManager.h>
|
|
|
|
#include <binder/Parcel.h>
|
|
|
|
#include <binder/Parcelable.h>
|
|
|
|
#include <binder/PersistableBundle.h>
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
#include <android/security/keystore/BpKeyAttestationApplicationIdProvider.h>
|
|
|
|
#include <android/security/keystore/IKeyAttestationApplicationIdProvider.h>
|
|
|
|
#include <android/security/keystore/KeyAttestationApplicationId.h>
|
|
|
|
#include <android/security/keystore/KeyAttestationPackageInfo.h>
|
|
|
|
#include <android/security/keystore/Signature.h>
|
2016-06-01 22:57:40 +02:00
|
|
|
|
2017-12-06 13:01:42 +01:00
|
|
|
#include <private/android_filesystem_config.h> /* for AID_SYSTEM */
|
|
|
|
|
2016-06-01 22:57:40 +02:00
|
|
|
#include <openssl/asn1t.h>
|
2017-12-28 22:19:50 +01:00
|
|
|
#include <openssl/bn.h>
|
2016-06-01 22:57:40 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include <utils/String8.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-12-18 23:11:46 +01:00
|
|
|
constexpr const char* kAttestationSystemPackageName = "AndroidSystem";
|
2018-08-16 19:53:15 +02:00
|
|
|
constexpr const char* kUnknownPackageName = "UnknownPackage";
|
2017-12-18 23:11:46 +01:00
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
std::vector<uint8_t> signature2SHA256(const security::keystore::Signature& sig) {
|
2016-06-01 22:57:40 +02:00
|
|
|
std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH);
|
2023-03-11 02:26:48 +01:00
|
|
|
SHA256(sig.data.data(), sig.data.size(), digest_buffer.data());
|
2016-06-01 22:57:40 +02:00
|
|
|
return digest_buffer;
|
|
|
|
}
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
using ::android::security::keystore::BpKeyAttestationApplicationIdProvider;
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
class KeyAttestationApplicationIdProvider : public BpKeyAttestationApplicationIdProvider {
|
|
|
|
public:
|
|
|
|
KeyAttestationApplicationIdProvider();
|
|
|
|
|
|
|
|
static KeyAttestationApplicationIdProvider& get();
|
|
|
|
|
|
|
|
private:
|
|
|
|
android::sp<android::IServiceManager> service_manager_;
|
|
|
|
};
|
|
|
|
|
|
|
|
KeyAttestationApplicationIdProvider& KeyAttestationApplicationIdProvider::get() {
|
|
|
|
static KeyAttestationApplicationIdProvider mpm;
|
|
|
|
return mpm;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyAttestationApplicationIdProvider::KeyAttestationApplicationIdProvider()
|
2023-07-18 20:53:39 +02:00
|
|
|
: BpKeyAttestationApplicationIdProvider(android::defaultServiceManager()->waitForService(
|
|
|
|
String16("sec_key_att_app_id_provider"))) {}
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
DECLARE_STACK_OF(ASN1_OCTET_STRING);
|
|
|
|
|
|
|
|
typedef struct km_attestation_package_info {
|
|
|
|
ASN1_OCTET_STRING* package_name;
|
|
|
|
ASN1_INTEGER* version;
|
|
|
|
} KM_ATTESTATION_PACKAGE_INFO;
|
|
|
|
|
2018-08-16 19:53:15 +02:00
|
|
|
// Estimated size:
|
|
|
|
// 4 bytes for the package name + package_name length
|
|
|
|
// 11 bytes for the version (2 bytes header and up to 9 bytes of data).
|
|
|
|
constexpr size_t AAID_PKG_INFO_OVERHEAD = 15;
|
2016-06-01 22:57:40 +02:00
|
|
|
ASN1_SEQUENCE(KM_ATTESTATION_PACKAGE_INFO) = {
|
|
|
|
ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, package_name, ASN1_OCTET_STRING),
|
|
|
|
ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, version, ASN1_INTEGER),
|
|
|
|
} ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO);
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO);
|
|
|
|
|
|
|
|
DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO);
|
|
|
|
|
2018-08-16 19:53:15 +02:00
|
|
|
// Estimated size:
|
|
|
|
// See estimate above for the stack of package infos.
|
|
|
|
// 34 (32 + 2) bytes for each signature digest.
|
|
|
|
constexpr size_t AAID_SIGNATURE_SIZE = 34;
|
2016-06-01 22:57:40 +02:00
|
|
|
typedef struct km_attestation_application_id {
|
|
|
|
STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos;
|
2016-09-01 12:41:29 +02:00
|
|
|
STACK_OF(ASN1_OCTET_STRING) * signature_digests;
|
2016-06-01 22:57:40 +02:00
|
|
|
} KM_ATTESTATION_APPLICATION_ID;
|
|
|
|
|
2018-08-16 19:53:15 +02:00
|
|
|
// Estimated overhead:
|
|
|
|
// 4 for the header of the octet string containing the fully-encoded data.
|
|
|
|
// 4 for the sequence header.
|
|
|
|
// 4 for the header of the package info set.
|
|
|
|
// 4 for the header of the signature set.
|
|
|
|
constexpr size_t AAID_GENERAL_OVERHEAD = 16;
|
2016-06-01 22:57:40 +02:00
|
|
|
ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = {
|
|
|
|
ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO),
|
2016-09-01 12:41:29 +02:00
|
|
|
ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING),
|
2016-06-01 22:57:40 +02:00
|
|
|
} ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID);
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID);
|
2017-12-18 23:11:46 +01:00
|
|
|
|
|
|
|
} // namespace
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
} // namespace android
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <> struct default_delete<android::KM_ATTESTATION_PACKAGE_INFO> {
|
|
|
|
void operator()(android::KM_ATTESTATION_PACKAGE_INFO* p) {
|
|
|
|
android::KM_ATTESTATION_PACKAGE_INFO_free(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <> struct default_delete<ASN1_OCTET_STRING> {
|
|
|
|
void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); }
|
|
|
|
};
|
|
|
|
template <> struct default_delete<android::KM_ATTESTATION_APPLICATION_ID> {
|
|
|
|
void operator()(android::KM_ATTESTATION_APPLICATION_ID* p) {
|
|
|
|
android::KM_ATTESTATION_APPLICATION_ID_free(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace security {
|
|
|
|
namespace {
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
using ::android::security::keystore::KeyAttestationApplicationId;
|
|
|
|
using ::android::security::keystore::KeyAttestationPackageInfo;
|
2016-06-01 22:57:40 +02:00
|
|
|
|
2016-09-01 12:41:29 +02:00
|
|
|
status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo,
|
2016-06-01 22:57:40 +02:00
|
|
|
std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO>* attestation_package_info_ptr) {
|
|
|
|
|
|
|
|
if (!attestation_package_info_ptr) return BAD_VALUE;
|
|
|
|
auto& attestation_package_info = *attestation_package_info_ptr;
|
|
|
|
|
|
|
|
attestation_package_info.reset(KM_ATTESTATION_PACKAGE_INFO_new());
|
|
|
|
if (!attestation_package_info.get()) return NO_MEMORY;
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
if (!pinfo.packageName) {
|
2016-09-01 12:41:29 +02:00
|
|
|
ALOGE("Key attestation package info lacks package name");
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
std::string pkg_name(String8(pinfo.packageName).c_str());
|
2016-06-01 22:57:40 +02:00
|
|
|
if (!ASN1_OCTET_STRING_set(attestation_package_info->package_name,
|
|
|
|
reinterpret_cast<const unsigned char*>(pkg_name.data()),
|
|
|
|
pkg_name.size())) {
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:19:50 +01:00
|
|
|
BIGNUM* bn_version = BN_new();
|
|
|
|
if (bn_version == nullptr) {
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
2023-03-11 02:26:48 +01:00
|
|
|
if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.versionCode)) != 1) {
|
2017-12-28 22:19:50 +01:00
|
|
|
BN_free(bn_version);
|
2016-06-01 22:57:40 +02:00
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
2017-12-28 22:19:50 +01:00
|
|
|
status_t retval = NO_ERROR;
|
|
|
|
if (BN_to_ASN1_INTEGER(bn_version, attestation_package_info->version) == nullptr) {
|
|
|
|
retval = UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
BN_free(bn_version);
|
|
|
|
return retval;
|
2016-06-01 22:57:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 19:53:15 +02:00
|
|
|
/* The following function are not used. They are mentioned here to silence
|
|
|
|
* warnings about them not being used.
|
|
|
|
*/
|
|
|
|
void unused_functions_silencer() __attribute__((unused));
|
|
|
|
void unused_functions_silencer() {
|
|
|
|
i2d_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr);
|
|
|
|
d2i_KM_ATTESTATION_APPLICATION_ID(nullptr, nullptr, 0);
|
|
|
|
d2i_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-09-01 12:41:29 +02:00
|
|
|
StatusOr<std::vector<uint8_t>>
|
2016-06-01 22:57:40 +02:00
|
|
|
build_attestation_application_id(const KeyAttestationApplicationId& key_attestation_id) {
|
|
|
|
auto attestation_id =
|
|
|
|
std::unique_ptr<KM_ATTESTATION_APPLICATION_ID>(KM_ATTESTATION_APPLICATION_ID_new());
|
2018-08-16 19:53:15 +02:00
|
|
|
size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD;
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos);
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
if (key_attestation_id.packageInfos.begin() == key_attestation_id.packageInfos.end())
|
|
|
|
return BAD_VALUE;
|
2016-09-01 12:41:29 +02:00
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
for (auto pinfo = key_attestation_id.packageInfos.begin();
|
|
|
|
pinfo != key_attestation_id.packageInfos.end(); ++pinfo) {
|
|
|
|
if (!pinfo->packageName) {
|
2016-06-01 22:57:40 +02:00
|
|
|
ALOGE("Key attestation package info lacks package name");
|
2016-09-01 12:41:29 +02:00
|
|
|
return BAD_VALUE;
|
2016-06-01 22:57:40 +02:00
|
|
|
}
|
2023-03-11 02:26:48 +01:00
|
|
|
std::string package_name(String8(pinfo->packageName).c_str());
|
2016-06-01 22:57:40 +02:00
|
|
|
std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO> attestation_package_info;
|
2016-09-01 12:41:29 +02:00
|
|
|
auto rc = build_attestation_package_info(*pinfo, &attestation_package_info);
|
2016-06-01 22:57:40 +02:00
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
ALOGE("Building DER attestation package info failed %d", rc);
|
2016-09-01 12:41:29 +02:00
|
|
|
return rc;
|
2016-06-01 22:57:40 +02:00
|
|
|
}
|
2018-08-16 19:53:15 +02:00
|
|
|
estimated_encoded_size += AAID_PKG_INFO_OVERHEAD + package_name.size();
|
|
|
|
if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
2016-06-01 22:57:40 +02:00
|
|
|
if (!sk_push(attestation_pinfo_stack, attestation_package_info.get())) {
|
2016-09-01 12:41:29 +02:00
|
|
|
return NO_MEMORY;
|
2016-06-01 22:57:40 +02:00
|
|
|
}
|
|
|
|
// if push succeeded, the stack takes ownership
|
|
|
|
attestation_package_info.release();
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:41:29 +02:00
|
|
|
/** Apps can only share a uid iff they were signed with the same certificate(s). Because the
|
|
|
|
* signature field actually holds the signing certificate, rather than a signature, we can
|
|
|
|
* simply use the set of signature digests of the first package info.
|
|
|
|
*/
|
2023-03-11 02:26:48 +01:00
|
|
|
const auto& pinfo = *key_attestation_id.packageInfos.begin();
|
2016-09-01 12:41:29 +02:00
|
|
|
std::vector<std::vector<uint8_t>> signature_digests;
|
|
|
|
|
2023-03-11 02:26:48 +01:00
|
|
|
for (auto sig = pinfo.signatures.begin(); sig != pinfo.signatures.end(); ++sig) {
|
2016-09-01 12:41:29 +02:00
|
|
|
signature_digests.push_back(signature2SHA256(*sig));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests);
|
|
|
|
for (auto si : signature_digests) {
|
2018-08-16 19:53:15 +02:00
|
|
|
estimated_encoded_size += AAID_SIGNATURE_SIZE;
|
|
|
|
if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-01 12:41:29 +02:00
|
|
|
auto asn1_item = std::unique_ptr<ASN1_OCTET_STRING>(ASN1_OCTET_STRING_new());
|
|
|
|
if (!asn1_item) return NO_MEMORY;
|
|
|
|
if (!ASN1_OCTET_STRING_set(asn1_item.get(), si.data(), si.size())) {
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
if (!sk_push(signature_digest_stack, asn1_item.get())) {
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
asn1_item.release(); // if push succeeded, the stack takes ownership
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:57:40 +02:00
|
|
|
int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr);
|
2016-09-01 12:41:29 +02:00
|
|
|
if (len < 0) return UNKNOWN_ERROR;
|
|
|
|
|
|
|
|
std::vector<uint8_t> result(len);
|
|
|
|
uint8_t* p = result.data();
|
2016-06-01 22:57:40 +02:00
|
|
|
len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p);
|
2016-09-01 12:41:29 +02:00
|
|
|
if (len < 0) return UNKNOWN_ERROR;
|
2016-06-01 22:57:40 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:41:29 +02:00
|
|
|
StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) {
|
2017-12-18 23:11:46 +01:00
|
|
|
KeyAttestationApplicationId key_attestation_id;
|
2016-06-01 22:57:40 +02:00
|
|
|
|
2017-12-06 13:01:42 +01:00
|
|
|
if (uid == AID_SYSTEM) {
|
2017-12-18 23:11:46 +01:00
|
|
|
/* Use a fixed ID for system callers */
|
2023-03-11 02:26:48 +01:00
|
|
|
auto pinfo = KeyAttestationPackageInfo();
|
|
|
|
pinfo.packageName = String16(kAttestationSystemPackageName);
|
|
|
|
pinfo.versionCode = 1;
|
|
|
|
key_attestation_id.packageInfos.push_back(std::move(pinfo));
|
2017-12-06 13:01:42 +01:00
|
|
|
} else {
|
2017-12-18 23:11:46 +01:00
|
|
|
/* Get the attestation application ID from package manager */
|
|
|
|
auto& pm = KeyAttestationApplicationIdProvider::get();
|
|
|
|
auto status = pm.getKeyAttestationApplicationId(uid, &key_attestation_id);
|
2018-08-16 19:53:15 +02:00
|
|
|
// Package Manager call has failed, perform attestation but indicate that the
|
|
|
|
// caller is unknown.
|
2017-12-06 13:01:42 +01:00
|
|
|
if (!status.isOk()) {
|
2018-08-16 19:53:15 +02:00
|
|
|
ALOGW("package manager request for key attestation ID failed with: %s %d",
|
2023-08-11 18:10:37 +02:00
|
|
|
status.exceptionMessage().c_str(), status.exceptionCode());
|
2023-03-11 02:26:48 +01:00
|
|
|
|
|
|
|
auto pinfo = KeyAttestationPackageInfo();
|
|
|
|
pinfo.packageName = String16(kUnknownPackageName);
|
|
|
|
pinfo.versionCode = 1;
|
|
|
|
key_attestation_id.packageInfos.push_back(std::move(pinfo));
|
2017-12-06 13:01:42 +01:00
|
|
|
}
|
2016-06-01 22:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DER encode the attestation application ID */
|
2017-12-18 23:11:46 +01:00
|
|
|
return build_attestation_application_id(key_attestation_id);
|
2016-06-01 22:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace security
|
|
|
|
} // namespace android
|