blob: 2b748e4c918dbe1d1bc36c7f1050593fad29b3ed [file] [log] [blame]
Martijn Coenen95194842020-09-24 16:56:46 +02001/*
2 * Copyright (C) 2020 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 <string>
18
19#include <android-base/file.h>
20#include <android-base/logging.h>
21
22#include <fcntl.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
26#include "CertUtils.h"
27#include "Keymaster.h"
28#include "KeymasterSigningKey.h"
29
30using android::base::ErrnoError;
31using android::base::Error;
32using android::base::ReadFileToString;
33using android::base::Result;
34using android::base::unique_fd;
35
36KeymasterSigningKey::KeymasterSigningKey() {}
37
38Result<KeymasterSigningKey> KeymasterSigningKey::loadFromBlobAndVerify(const std::string& path) {
39 KeymasterSigningKey signingKey;
40
41 auto status = signingKey.initializeFromKeyblob(path);
42
43 if (!status.ok()) {
44 return status.error();
45 }
46
47 return std::move(signingKey);
48}
49
50Result<KeymasterSigningKey> KeymasterSigningKey::createNewKey() {
51 KeymasterSigningKey signingKey;
52
53 auto status = signingKey.createSigningKey();
54
55 if (!status.ok()) {
56 return status.error();
57 }
58
59 return std::move(signingKey);
60}
61
62Result<void> KeymasterSigningKey::createSigningKey() {
63 KeymasterSigningKey signingKey;
Martijn Coenen3883ec62021-02-25 09:14:01 +010064 auto keymaster = Keymaster::getInstance();
65 if (!keymaster.has_value()) {
66 return Error() << "Failed to initialize keymaster.";
67 }
68 mKeymaster = keymaster;
Martijn Coenen95194842020-09-24 16:56:46 +020069
70 auto keyBlob = mKeymaster->createKey();
71
72 if (!keyBlob.ok()) {
73 return keyBlob.error();
74 }
75
76 mVerifiedKeyBlob.assign(keyBlob->begin(), keyBlob->end());
77
78 return {};
79}
80
81Result<void> KeymasterSigningKey::saveKeyblob(const std::string& path) const {
82 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
83
84 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, 0600)));
85 if (fd == -1) {
86 return ErrnoError() << "Error creating key blob file " << path;
87 }
88
89 if (!android::base::WriteFully(fd, mVerifiedKeyBlob.data(), mVerifiedKeyBlob.size())) {
90 return ErrnoError() << "Error writing key blob file " << path;
91 } else {
92 return {};
93 }
94}
95
96Result<std::vector<uint8_t>> KeymasterSigningKey::getPublicKey() const {
97 auto publicKeyX509 = mKeymaster->extractPublicKey(mVerifiedKeyBlob);
98 if (!publicKeyX509.ok()) {
99 return publicKeyX509.error();
100 }
101 return extractPublicKeyFromX509(publicKeyX509.value());
102}
103
104Result<void> KeymasterSigningKey::createX509Cert(const std::string& outPath) const {
105 auto publicKey = mKeymaster->extractPublicKey(mVerifiedKeyBlob);
106
107 if (!publicKey.ok()) {
108 return publicKey.error();
109 }
110
111 auto keymasterSignFunction = [&](const std::string& to_be_signed) {
112 return this->sign(to_be_signed);
113 };
114 createSelfSignedCertificate(*publicKey, keymasterSignFunction, outPath);
115 return {};
116}
117
118Result<void> KeymasterSigningKey::initializeFromKeyblob(const std::string& path) {
Martijn Coenen95194842020-09-24 16:56:46 +0200119 std::string keyBlobData;
Martijn Coenen3883ec62021-02-25 09:14:01 +0100120 auto keymaster = Keymaster::getInstance();
121 if (!keymaster.has_value()) {
122 return Error() << "Failed to initialize keymaster.";
123 }
124 mKeymaster = keymaster;
Martijn Coenen95194842020-09-24 16:56:46 +0200125
126 bool result = ReadFileToString(path, &keyBlobData);
127 if (!result) {
128 return ErrnoError() << "Failed to read " << path;
129 }
130
131 std::vector<uint8_t> keyBlob = {keyBlobData.begin(), keyBlobData.end()};
132
133 auto verifyResult = mKeymaster->verifyKey(keyBlob);
134 if (!verifyResult.ok()) {
135 return Error() << "Failed to verify key: " << verifyResult.error().message();
136 }
137
138 if (*verifyResult == KeymasterVerifyResult::UPGRADE) {
139 auto upgradeResult = mKeymaster->upgradeKey(keyBlob);
140 if (!upgradeResult.ok()) {
141 return Error() << "Failed to upgrade key: " << upgradeResult.error().message();
142 }
143 mVerifiedKeyBlob = *upgradeResult;
144 // Make sure we persist the new blob
145 auto saveResult = saveKeyblob(path);
146 if (!saveResult.ok()) {
147 return Error() << "Failed to store upgraded key";
148 }
149 } else {
150 mVerifiedKeyBlob = keyBlob;
151 }
152
153 return {};
154}
155
156Result<std::string> KeymasterSigningKey::sign(const std::string& message) const {
157 return mKeymaster->sign(mVerifiedKeyBlob, message);
158}