• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::GenerateRkpKeyRequest;
39 using keymaster::GenerateRkpKeyResponse;
40 using keymaster::KeymasterBlob;
41 using ::std::string;
42 using ::std::unique_ptr;
43 using ::std::vector;
44 using bytevec = ::std::vector<uint8_t>;
45 
46 namespace {
47 
48 constexpr auto STATUS_FAILED = IRemotelyProvisionedComponent::STATUS_FAILED;
49 
50 struct AStatusDeleter {
operator ()aidl::android::hardware::security::keymint::__anon6e7f3adf0111::AStatusDeleter51     void operator()(AStatus* p) { AStatus_delete(p); }
52 };
53 
54 class Status {
55   public:
Status()56     Status() : status_(AStatus_newOk()) {}
Status(int32_t errCode,const std::string & errMsg)57     Status(int32_t errCode, const std::string& errMsg)
58         : status_(AStatus_fromServiceSpecificErrorWithMessage(errCode, errMsg.c_str())) {}
Status(const std::string & errMsg)59     explicit Status(const std::string& errMsg)
60         : status_(AStatus_fromServiceSpecificErrorWithMessage(STATUS_FAILED, errMsg.c_str())) {}
Status(AStatus * status)61     explicit Status(AStatus* status) : status_(status ? status : AStatus_newOk()) {}
62 
63     Status(Status&&) = default;
64     Status(const Status&) = delete;
65 
operator ::ndk::ScopedAStatus()66     operator ::ndk::ScopedAStatus() && {  // NOLINT(google-explicit-constructor)
67         return ndk::ScopedAStatus(status_.release());
68     }
69 
isOk() const70     bool isOk() const { return AStatus_isOk(status_.get()); }
71 
getMessage() const72     const char* getMessage() const { return AStatus_getMessage(status_.get()); }
73 
74   private:
75     std::unique_ptr<AStatus, AStatusDeleter> status_;
76 };
77 
78 }  // namespace
79 
AndroidRemotelyProvisionedComponentDevice(const std::shared_ptr<AndroidKeyMintDevice> & keymint)80 AndroidRemotelyProvisionedComponentDevice::AndroidRemotelyProvisionedComponentDevice(
81     const std::shared_ptr<AndroidKeyMintDevice>& keymint) {
82     impl_ = keymint->getKeymasterImpl();
83 }
84 
getHardwareInfo(RpcHardwareInfo * info)85 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::getHardwareInfo(RpcHardwareInfo* info) {
86     info->versionNumber = 2;
87     info->rpcAuthorName = "Google";
88     info->supportedEekCurve = RpcHardwareInfo::CURVE_25519;
89     info->uniqueId = "default keymint";
90     return ScopedAStatus::ok();
91 }
92 
generateEcdsaP256KeyPair(bool testMode,MacedPublicKey * macedPublicKey,bytevec * privateKeyHandle)93 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateEcdsaP256KeyPair(
94     bool testMode, MacedPublicKey* macedPublicKey, bytevec* privateKeyHandle) {
95     GenerateRkpKeyRequest request(impl_->message_version());
96     request.test_mode = testMode;
97     GenerateRkpKeyResponse response(impl_->message_version());
98     impl_->GenerateRkpKey(request, &response);
99     if (response.error != KM_ERROR_OK) {
100         return Status(-static_cast<int32_t>(response.error), "Failure in key generation.");
101     }
102 
103     macedPublicKey->macedKey = km_utils::kmBlob2vector(response.maced_public_key);
104     *privateKeyHandle = km_utils::kmBlob2vector(response.key_blob);
105     return ScopedAStatus::ok();
106 }
107 
generateCertificateRequest(bool testMode,const vector<MacedPublicKey> & keysToSign,const bytevec & endpointEncCertChain,const bytevec & challenge,DeviceInfo * deviceInfo,ProtectedData * protectedData,bytevec * keysToSignMac)108 ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateCertificateRequest(
109     bool testMode, const vector<MacedPublicKey>& keysToSign, const bytevec& endpointEncCertChain,
110     const bytevec& challenge, DeviceInfo* deviceInfo, ProtectedData* protectedData,
111     bytevec* keysToSignMac) {
112     GenerateCsrRequest request(impl_->message_version());
113     request.test_mode = testMode;
114     request.num_keys = keysToSign.size();
115     request.keys_to_sign_array = new (std::nothrow) KeymasterBlob[keysToSign.size()];
116     if (request.keys_to_sign_array == nullptr) {
117         return km_utils::kmError2ScopedAStatus(KM_ERROR_MEMORY_ALLOCATION_FAILED);
118     }
119     for (size_t i = 0; i < keysToSign.size(); i++) {
120         request.SetKeyToSign(i, keysToSign[i].macedKey.data(), keysToSign[i].macedKey.size());
121     }
122     request.SetEndpointEncCertChain(endpointEncCertChain.data(), endpointEncCertChain.size());
123     request.SetChallenge(challenge.data(), challenge.size());
124     GenerateCsrResponse response(impl_->message_version());
125     impl_->GenerateCsr(request, &response);
126 
127     if (response.error != KM_ERROR_OK) {
128         return Status(-static_cast<int32_t>(response.error), "Failure in CSR Generation.");
129     }
130     deviceInfo->deviceInfo = km_utils::kmBlob2vector(response.device_info_blob);
131     protectedData->protectedData = km_utils::kmBlob2vector(response.protected_data_blob);
132     *keysToSignMac = km_utils::kmBlob2vector(response.keys_to_sign_mac);
133     return ScopedAStatus::ok();
134 }
135 
136 }  // namespace aidl::android::hardware::security::keymint
137