blob: 0e5387c4f13f1f7e5366da5fbbcedf49ecf808ca [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
36using ::std::string;
37using ::std::tuple;
38using ::std::unique_ptr;
39using ::std::variant;
40using ::std::vector;
41using bytevec = ::std::vector<uint8_t>;
42
43using namespace cppcose;
44using namespace keymaster;
45
46namespace {
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
66 operator ::ndk::ScopedAStatus() && { return ndk::ScopedAStatus(status_.release()); }
67
68 bool isOk() const { return AStatus_isOk(status_.get()); }
69
70 const char* getMessage() const { return AStatus_getMessage(status_.get()); }
71
72 private:
73 std::unique_ptr<AStatus, AStatusDeleter> status_;
74};
75
76} // namespace
77
78AndroidRemotelyProvisionedComponentDevice::AndroidRemotelyProvisionedComponentDevice(
79 const std::shared_ptr<AndroidKeyMintDevice>& keymint) {
80 impl_ = keymint->getKeymasterImpl();
81}
82
83ScopedAStatus AndroidRemotelyProvisionedComponentDevice::getHardwareInfo(RpcHardwareInfo* info) {
84 info->versionNumber = 1;
85 info->rpcAuthorName = "Google";
86 info->supportedEekCurve = RpcHardwareInfo::CURVE_25519;
87 return ScopedAStatus::ok();
88}
89
90ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateEcdsaP256KeyPair(
91 bool testMode, MacedPublicKey* macedPublicKey, bytevec* privateKeyHandle) {
92 GenerateRkpKeyRequest request(impl_->message_version());
93 request.test_mode = testMode;
94 GenerateRkpKeyResponse response(impl_->message_version());
95 impl_->GenerateRkpKey(request, &response);
96 if (response.error != KM_ERROR_OK) {
97 return Status(-static_cast<int32_t>(response.error), "Failure in key generation.");
98 }
99
100 macedPublicKey->macedKey = km_utils::kmBlob2vector(response.maced_public_key);
101 *privateKeyHandle = km_utils::kmBlob2vector(response.key_blob);
102 return ScopedAStatus::ok();
103}
104
105ScopedAStatus AndroidRemotelyProvisionedComponentDevice::generateCertificateRequest(
106 bool testMode, const vector<MacedPublicKey>& keysToSign, const bytevec& endpointEncCertChain,
107 const bytevec& challenge, DeviceInfo* deviceInfo, ProtectedData* protectedData,
108 bytevec* keysToSignMac) {
109 GenerateCsrRequest request(impl_->message_version());
110 request.test_mode = testMode;
111 request.num_keys = keysToSign.size();
112 request.keys_to_sign_array = new KeymasterBlob[keysToSign.size()];
113 for (size_t i = 0; i < keysToSign.size(); i++) {
114 request.SetKeyToSign(i, keysToSign[i].macedKey.data(), keysToSign[i].macedKey.size());
115 }
116 request.SetEndpointEncCertChain(endpointEncCertChain.data(), endpointEncCertChain.size());
117 request.SetChallenge(challenge.data(), challenge.size());
118 GenerateCsrResponse response(impl_->message_version());
119 impl_->GenerateCsr(request, &response);
120
121 if (response.error != KM_ERROR_OK) {
122 return Status(-static_cast<int32_t>(response.error), "Failure in CSR Generation.");
123 }
124 deviceInfo->deviceInfo = km_utils::kmBlob2vector(response.device_info_blob);
125 protectedData->protectedData = km_utils::kmBlob2vector(response.protected_data_blob);
126 *keysToSignMac = km_utils::kmBlob2vector(response.keys_to_sign_mac);
127 return ScopedAStatus::ok();
128}
129
130} // namespace aidl::android::hardware::security::keymint