blob: 76310597af7867cfa1d14a10cf87aabda8e414b1 [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"
26
27class KeymasterSigningKey {
28 using KmDevice = ::android::hardware::keymaster::V4_1::IKeymasterDevice;
29
30 public:
31 // Allow the key to be moved around
32 KeymasterSigningKey& operator=(KeymasterSigningKey&& other) = default;
33 KeymasterSigningKey(KeymasterSigningKey&& other) = default;
34
35 static android::base::Result<KeymasterSigningKey>
36 loadFromBlobAndVerify(const std::string& path);
37 static android::base::Result<KeymasterSigningKey> createNewKey();
38
39 /* Sign a message with an initialized signing key */
40 android::base::Result<std::string> sign(const std::string& message) const;
41 android::base::Result<void> saveKeyblob(const std::string& path) const;
42 android::base::Result<std::vector<uint8_t>> getPublicKey() const;
43 android::base::Result<void> createX509Cert(const std::string& path) const;
44
45 private:
46 KeymasterSigningKey();
47
48 android::base::Result<void> createSigningKey();
49 android::base::Result<void> initializeFromKeyblob(const std::string& path);
50
51 std::optional<Keymaster> mKeymaster;
52 std::vector<uint8_t> mVerifiedKeyBlob;
53
54 DISALLOW_COPY_AND_ASSIGN(KeymasterSigningKey);
55};