1 /*
2 * Copyright (C) 2021 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 "AndroidRemotelyProvisionedComponentDevice.h"
18
19 #include <assert.h>
20 #include <variant>
21
22 #include <cppbor.h>
23 #include <cppbor_parse.h>
24
25 #include <KeyMintUtils.h>
26 #include <keymaster/cppcose/cppcose.h>
27 #include <keymaster/keymaster_configuration.h>
28
29 #include <openssl/bn.h>
30 #include <openssl/ec.h>
31 #include <openssl/rand.h>
32 #include <openssl/x509.h>
33
34 namespace aidl::android::hardware::security::keymint {
35
36 using keymaster::GenerateCsrRequest;
37 using keymaster::GenerateCsrResponse;
38 using keymaster::GenerateCsrV2Request;
39 using keymaster::GenerateCsrV2Response;
40 using keymaster::GenerateRkpKeyRequest;
41 using keymaster::GenerateRkpKeyResponse;
42 using keymaster::GetHwInfoRequest;
43 using keymaster::GetHwInfoResponse;
44 using keymaster::KeymasterBlob;
45 using ::std::string;
46 using ::std::unique_ptr;
47 using ::std::vector;
48 using bytevec = ::std::vector<uint8_t>;
49
50 namespace {
51
52 constexpr auto STATUS_FAILED = IRemotelyProvisionedComponent::STATUS_FAILED;
53
54 struct AStatusDeleter {
operator ()aidl::android::hardware::security::keymint::__anonba2fe5580111::AStatusDeleter55 void operator()(AStatus* p) { AStatus_delete(p); }
56 };
57
58 class Status {
59 public:
Status()60 Status() : status_(AStatus_newOk()) {}
Status(int32_t errCode,const std::string & errMsg)61 Status(int32_t errCode, const std::string& errMsg)
62 : status_(AStatus_fromServiceSpecificErrorWithMessage(errCode, errMsg.c_str())) {}
Status(const std::string & errMsg)63 explicit Status(const std::string& errMsg)
64 : status_(AStatus_fromServiceSpecificErrorWithMessage(STATUS_FAILED, errMsg.c_str())) {}
Status(AStatus * status)65 explicit Status(AStatus* status) : status_(status ? status : AStatus_newOk()) {}
66
67 Status(Status&&) = default;
68 Status(const Status&) = delete;
69
operator ::ndk::ScopedAStatus()70 operator ::ndk::ScopedAStatus() && { // NOLINT(google-explicit-constructor)
71 return ndk::ScopedAStatus(status_.release());
72 }
73
isOk() const74 bool isOk() const { return AStatus_isOk(status_.get()); }
75
getMessage() const76 const char* getMessage() const { return AStatus_getMessage(status_.get()); }
77
78 private:
79 std::unique_ptr<AStatus, AStatusDeleter> status_;
80 };
81
82 } // namespace
83
AndroidRemotelyProvisionedComponentDevice(const std::shared_ptr<AndroidKeyMintDevice> & keymint)84 AndroidRemotelyProvisionedComponentDevice::AndroidRemotelyProvisionedComponentDevice(
85 const std::shared_ptr<AndroidKeyMintDevice>& keymint) {
86 impl_ = keymint->getKeymasterImpl();
87 }
88
getHardwareInfo(RpcHardwareInfo * info)89 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::getHardwareInfo(RpcHardwareInfo* info) {
90 GetHwInfoResponse response = impl_->GetHwInfo();
91 if (response.error != KM_ERROR_OK) {
92 return Status(-static_cast<int32_t>(response.error), "Failed to get hardware info.");
93 }
94
95 info->versionNumber = response.version;
96 info->rpcAuthorName = response.rpcAuthorName;
97 info->supportedEekCurve = response.supportedEekCurve;
98 info->uniqueId = response.uniqueId;
99 info->supportedNumKeysInCsr = response.supportedNumKeysInCsr;
100 return ScopedAStatus::ok();
101 }
102
generateEcdsaP256KeyPair(bool testMode,MacedPublicKey * macedPublicKey,bytevec * privateKeyHandle)103 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateEcdsaP256KeyPair(
104 bool testMode, MacedPublicKey* macedPublicKey, bytevec* privateKeyHandle) {
105 GenerateRkpKeyRequest request(impl_->message_version());
106 request.test_mode = testMode;
107 GenerateRkpKeyResponse response(impl_->message_version());
108 impl_->GenerateRkpKey(request, &response);
109 if (response.error != KM_ERROR_OK) {
110 return Status(-static_cast<int32_t>(response.error), "Failure in key generation.");
111 }
112
113 macedPublicKey->macedKey = km_utils::kmBlob2vector(response.maced_public_key);
114 *privateKeyHandle = km_utils::kmBlob2vector(response.key_blob);
115 return ScopedAStatus::ok();
116 }
117
generateCertificateRequest(bool testMode,const vector<MacedPublicKey> & keysToSign,const bytevec & endpointEncCertChain,const bytevec & challenge,DeviceInfo * deviceInfo,ProtectedData * protectedData,bytevec * keysToSignMac)118 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateCertificateRequest(
119 bool testMode, const vector<MacedPublicKey>& keysToSign, const bytevec& endpointEncCertChain,
120 const bytevec& challenge, DeviceInfo* deviceInfo, ProtectedData* protectedData,
121 bytevec* keysToSignMac) {
122 GenerateCsrRequest request(impl_->message_version());
123 request.test_mode = testMode;
124 request.num_keys = keysToSign.size();
125 request.keys_to_sign_array = new (std::nothrow) KeymasterBlob[keysToSign.size()];
126 if (request.keys_to_sign_array == nullptr) {
127 return km_utils::kmError2ScopedAStatus(KM_ERROR_MEMORY_ALLOCATION_FAILED);
128 }
129 for (size_t i = 0; i < keysToSign.size(); i++) {
130 request.SetKeyToSign(i, keysToSign[i].macedKey.data(), keysToSign[i].macedKey.size());
131 }
132 request.SetEndpointEncCertChain(endpointEncCertChain.data(), endpointEncCertChain.size());
133 request.SetChallenge(challenge.data(), challenge.size());
134 GenerateCsrResponse response(impl_->message_version());
135 impl_->GenerateCsr(request, &response);
136
137 if (response.error != KM_ERROR_OK) {
138 return Status(-static_cast<int32_t>(response.error), "Failure in CSR Generation.");
139 }
140 deviceInfo->deviceInfo = km_utils::kmBlob2vector(response.device_info_blob);
141 protectedData->protectedData = km_utils::kmBlob2vector(response.protected_data_blob);
142 *keysToSignMac = km_utils::kmBlob2vector(response.keys_to_sign_mac);
143 return ScopedAStatus::ok();
144 }
145
generateCertificateRequestV2(const std::vector<MacedPublicKey> & keysToSign,const std::vector<uint8_t> & challenge,std::vector<uint8_t> * csr)146 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateCertificateRequestV2(
147 const std::vector<MacedPublicKey>& keysToSign, const std::vector<uint8_t>& challenge,
148 std::vector<uint8_t>* csr) {
149 GenerateCsrV2Request request(impl_->message_version());
150 if (!request.InitKeysToSign(keysToSign.size())) {
151 return km_utils::kmError2ScopedAStatus(static_cast<keymaster_error_t>(STATUS_FAILED));
152 }
153 for (size_t i = 0; i < keysToSign.size(); i++) {
154 request.SetKeyToSign(i, keysToSign[i].macedKey.data(), keysToSign[i].macedKey.size());
155 }
156 request.SetChallenge(challenge.data(), challenge.size());
157 GenerateCsrV2Response response(impl_->message_version());
158 impl_->GenerateCsrV2(request, &response);
159
160 if (response.error != KM_ERROR_OK) {
161 return Status(-static_cast<int32_t>(response.error), "Failure in CSR v2 generation.");
162 }
163 *csr = km_utils::kmBlob2vector(response.csr);
164 return ScopedAStatus::ok();
165 }
166
167 } // namespace aidl::android::hardware::security::keymint
168