blob: 54ea70c4c313326a3b05fbc886828b0396a6dbc9 [file] [log] [blame]
Max Bires01799e02021-04-19 18:58:04 -07001/*
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
34namespace aidl::android::hardware::security::keymint {
35
Shawn Willden9a3792e2021-04-08 09:38:14 -060036using keymaster::GenerateCsrRequest;
37using keymaster::GenerateCsrResponse;
38using keymaster::GenerateRkpKeyRequest;
39using keymaster::GenerateRkpKeyResponse;
40using keymaster::KeymasterBlob;
Max Bires01799e02021-04-19 18:58:04 -070041using ::std::string;
Max Bires01799e02021-04-19 18:58:04 -070042using ::std::unique_ptr;
Max Bires01799e02021-04-19 18:58:04 -070043using ::std::vector;
44using bytevec = ::std::vector<uint8_t>;
45
Max Bires01799e02021-04-19 18:58:04 -070046namespace {
47
48constexpr auto STATUS_FAILED = IRemotelyProvisionedComponent::STATUS_FAILED;
49
50struct AStatusDeleter {
51 void operator()(AStatus* p) { AStatus_delete(p); }
52};
53
54class Status {
55 public:
56 Status() : status_(AStatus_newOk()) {}
57 Status(int32_t errCode, const std::string& errMsg)
58 : status_(AStatus_fromServiceSpecificErrorWithMessage(errCode, errMsg.c_str())) {}
59 explicit Status(const std::string& errMsg)
60 : status_(AStatus_fromServiceSpecificErrorWithMessage(STATUS_FAILED, errMsg.c_str())) {}
61 explicit Status(AStatus* status) : status_(status ? status : AStatus_newOk()) {}
62
63 Status(Status&&) = default;
64 Status(const Status&) = delete;
65
Shawn Willden9a3792e2021-04-08 09:38:14 -060066 operator ::ndk::ScopedAStatus() && { // NOLINT(google-explicit-constructor)
67 return ndk::ScopedAStatus(status_.release());
68 }
Max Bires01799e02021-04-19 18:58:04 -070069
70 bool isOk() const { return AStatus_isOk(status_.get()); }
71
72 const char* getMessage() const { return AStatus_getMessage(status_.get()); }
73
74 private:
75 std::unique_ptr<AStatus, AStatusDeleter> status_;
76};
77
78} // namespace
79
80AndroidRemotelyProvisionedComponentDevice::AndroidRemotelyProvisionedComponentDevice(
81 const std::shared_ptr<AndroidKeyMintDevice>& keymint) {
82 impl_ = keymint->getKeymasterImpl();
83}
84
85ScopedAStatus AndroidRemotelyProvisionedComponentDevice::getHardwareInfo(RpcHardwareInfo* info) {
David Drysdale34754162021-12-06 10:03:56 +000086 info->versionNumber = 2;
Max Bires01799e02021-04-19 18:58:04 -070087 info->rpcAuthorName = "Google";
88 info->supportedEekCurve = RpcHardwareInfo::CURVE_25519;
Seth Moore8b003c52021-12-14 13:14:40 -080089 info->uniqueId = "default keymint";
Max Bires01799e02021-04-19 18:58:04 -070090 return ScopedAStatus::ok();
91}
92
93ScopedAStatus 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
108ScopedAStatus 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();
David Drysdale9355fad2022-01-24 08:07:34 +0000115 request.keys_to_sign_array = new (std::nothrow) KeymasterBlob[keysToSign.size()];
David Drysdale513e6152022-01-25 09:46:45 +0000116 if (request.keys_to_sign_array == nullptr) {
117 return km_utils::kmError2ScopedAStatus(KM_ERROR_MEMORY_ALLOCATION_FAILED);
118 }
Max Bires01799e02021-04-19 18:58:04 -0700119 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