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