blob: e66781fde26ab075ec38eb868805dfb5501a7a73 [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#pragma once
18
19#include <android-base/macros.h>
20#include <android-base/result.h>
21#include <android-base/unique_fd.h>
22
23#include <utils/StrongPointer.h>
24
25#include "Keymaster.h"
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010026#include "SigningKey.h"
Martijn Coenen95194842020-09-24 16:56:46 +020027
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010028class KeymasterSigningKey : public SigningKey {
Martijn Coenen95194842020-09-24 16:56:46 +020029 using KmDevice = ::android::hardware::keymaster::V4_1::IKeymasterDevice;
30
31 public:
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010032 friend std::unique_ptr<KeymasterSigningKey> std::make_unique<KeymasterSigningKey>();
33 virtual ~KeymasterSigningKey(){};
34
Martijn Coenen95194842020-09-24 16:56:46 +020035 // Allow the key to be moved around
36 KeymasterSigningKey& operator=(KeymasterSigningKey&& other) = default;
37 KeymasterSigningKey(KeymasterSigningKey&& other) = default;
38
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010039 static android::base::Result<SigningKey*> getInstance();
Martijn Coenen95194842020-09-24 16:56:46 +020040
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010041 virtual android::base::Result<std::string> sign(const std::string& message) const;
42 virtual android::base::Result<std::vector<uint8_t>> getPublicKey() const;
Martijn Coenen95194842020-09-24 16:56:46 +020043
44 private:
45 KeymasterSigningKey();
46
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010047 static android::base::Result<std::unique_ptr<KeymasterSigningKey>> createAndPersistNewKey();
48 static android::base::Result<std::unique_ptr<KeymasterSigningKey>>
49 loadFromBlobAndVerify(const std::string& path);
50
Martijn Coenen95194842020-09-24 16:56:46 +020051 android::base::Result<void> createSigningKey();
52 android::base::Result<void> initializeFromKeyblob(const std::string& path);
Martijn Coenenba1c9dc2021-02-04 13:18:29 +010053 android::base::Result<void> saveKeyblob(const std::string& path) const;
54
55 static android::base::Result<KeymasterSigningKey> createNewKey();
Martijn Coenen95194842020-09-24 16:56:46 +020056
57 std::optional<Keymaster> mKeymaster;
58 std::vector<uint8_t> mVerifiedKeyBlob;
59
60 DISALLOW_COPY_AND_ASSIGN(KeymasterSigningKey);
61};