• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #define LOG_TAG "keymint_1_bootloader_test"
18 
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 #include <android-base/properties.h>
25 #include <android/binder_manager.h>
26 #include <fstab/fstab.h>
27 #include <libavb/libavb.h>
28 #include <libavb_user/avb_ops_user.h>
29 #include <remote_prov/remote_prov_utils.h>
30 
31 #include "KeyMintAidlTestBase.h"
32 
33 namespace aidl::android::hardware::security::keymint::test {
34 
35 using ::android::getAidlHalInstanceNames;
36 using ::std::string;
37 using ::std::vector;
38 
39 // Since this test needs to talk to KeyMint HAL, it can only run as root. Thus,
40 // bootloader can not be locked.
41 class BootloaderStateTest : public KeyMintAidlTestBase {
42   public:
SetUp()43     virtual void SetUp() override {
44         KeyMintAidlTestBase::SetUp();
45 
46         // Generate a key with attestation.
47         vector<uint8_t> key_blob;
48         vector<KeyCharacteristics> key_characteristics;
49         AuthorizationSet keyDesc = AuthorizationSetBuilder()
50                                            .Authorization(TAG_NO_AUTH_REQUIRED)
51                                            .EcdsaSigningKey(EcCurve::P_256)
52                                            .AttestationChallenge("foo")
53                                            .AttestationApplicationId("bar")
54                                            .Digest(Digest::NONE)
55                                            .SetDefaultValidity();
56         auto result = GenerateKey(keyDesc, &key_blob, &key_characteristics);
57         // If factory provisioned attestation key is not supported by Strongbox,
58         // then create a key with self-signed attestation and use it as the
59         // attestation key instead.
60         if (SecLevel() == SecurityLevel::STRONGBOX &&
61             result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) {
62             result = GenerateKeyWithSelfSignedAttestKey(
63                     AuthorizationSetBuilder()
64                             .EcdsaKey(EcCurve::P_256)
65                             .AttestKey()
66                             .SetDefaultValidity(), /* attest key params */
67                     keyDesc, &key_blob, &key_characteristics);
68         }
69         ASSERT_EQ(ErrorCode::OK, result);
70 
71         // Parse attested AVB values.
72         X509_Ptr cert(parse_cert_blob(cert_chain_[0].encodedCertificate));
73         ASSERT_TRUE(cert.get());
74 
75         ASN1_OCTET_STRING* attest_rec = get_attestation_record(cert.get());
76         ASSERT_TRUE(attest_rec);
77 
78         auto error = parse_root_of_trust(attest_rec->data, attest_rec->length, &attestedVbKey_,
79                                          &attestedVbState_, &attestedBootloaderState_,
80                                          &attestedVbmetaDigest_);
81         ASSERT_EQ(error, ErrorCode::OK);
82     }
83 
84     vector<uint8_t> attestedVbKey_;
85     VerifiedBoot attestedVbState_;
86     bool attestedBootloaderState_;
87     vector<uint8_t> attestedVbmetaDigest_;
88 };
89 
90 // Check that attested bootloader state is set to unlocked.
TEST_P(BootloaderStateTest,BootloaderIsUnlocked)91 TEST_P(BootloaderStateTest, BootloaderIsUnlocked) {
92     ASSERT_FALSE(attestedBootloaderState_)
93             << "This test runs as root. Bootloader must be unlocked.";
94 }
95 
96 // Check that verified boot state is set to "unverified", i.e. "orange".
TEST_P(BootloaderStateTest,VbStateIsUnverified)97 TEST_P(BootloaderStateTest, VbStateIsUnverified) {
98     // Unlocked bootloader implies that verified boot state must be "unverified".
99     ASSERT_EQ(attestedVbState_, VerifiedBoot::UNVERIFIED)
100             << "Verified boot state must be \"UNVERIFIED\" aka \"orange\".";
101 
102     // AVB spec stipulates that bootloader must set "androidboot.verifiedbootstate" parameter
103     // on the kernel command-line. This parameter is exposed to userspace as
104     // "ro.boot.verifiedbootstate" property.
105     auto vbStateProp = ::android::base::GetProperty("ro.boot.verifiedbootstate", "");
106     ASSERT_EQ(vbStateProp, "orange")
107             << "Verified boot state must be \"UNVERIFIED\" aka \"orange\".";
108 }
109 
110 // Following error codes from avb_slot_data() mean that slot data was loaded
111 // (even if verification failed).
avb_slot_data_loaded(AvbSlotVerifyResult result)112 static inline bool avb_slot_data_loaded(AvbSlotVerifyResult result) {
113     switch (result) {
114         case AVB_SLOT_VERIFY_RESULT_OK:
115         case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
116         case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
117         case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
118             return true;
119         default:
120             return false;
121     }
122 }
123 
124 // Check that attested vbmeta digest is correct.
TEST_P(BootloaderStateTest,VbmetaDigest)125 TEST_P(BootloaderStateTest, VbmetaDigest) {
126     AvbSlotVerifyData* avbSlotData;
127     auto suffix = fs_mgr_get_slot_suffix();
128     const char* partitions[] = {nullptr};
129     auto avbOps = avb_ops_user_new();
130 
131     // For VTS, devices run with vendor_boot-debug.img, which is not release key
132     // signed. Use AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR to bypass avb
133     // verification errors. This is OK since we only care about the digest for
134     // this test case.
135     auto result = avb_slot_verify(avbOps, partitions, suffix.c_str(),
136                                   AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR,
137                                   AVB_HASHTREE_ERROR_MODE_EIO, &avbSlotData);
138     ASSERT_TRUE(avb_slot_data_loaded(result)) << "Failed to load avb slot data";
139 
140     // Unfortunately, bootloader is not required to report the algorithm used
141     // to calculate the digest. There are only two supported options though,
142     // SHA256 and SHA512. Attested VBMeta digest must match one of these.
143     vector<uint8_t> digest256(AVB_SHA256_DIGEST_SIZE);
144     vector<uint8_t> digest512(AVB_SHA512_DIGEST_SIZE);
145 
146     avb_slot_verify_data_calculate_vbmeta_digest(avbSlotData, AVB_DIGEST_TYPE_SHA256,
147                                                  digest256.data());
148     avb_slot_verify_data_calculate_vbmeta_digest(avbSlotData, AVB_DIGEST_TYPE_SHA512,
149                                                  digest512.data());
150 
151     ASSERT_TRUE((attestedVbmetaDigest_ == digest256) || (attestedVbmetaDigest_ == digest512))
152             << "Attested digest does not match computed digest.";
153 }
154 
155 INSTANTIATE_KEYMINT_AIDL_TEST(BootloaderStateTest);
156 
157 }  // namespace aidl::android::hardware::security::keymint::test
158