blob: 6b9cb57afa21f8f404cce09de6fe7103d45038a7 [file] [log] [blame]
Martijn Coenenba1c9dc2021-02-04 13:18:29 +01001/*
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 <optional>
20
21#include <android-base/macros.h>
22#include <android-base/result.h>
23#include <android-base/unique_fd.h>
24
25#include <utils/StrongPointer.h>
26
27#include <android/system/keystore2/IKeystoreService.h>
28
29#include "SigningKey.h"
30
31class KeystoreKey : public SigningKey {
32 using IKeystoreService = ::android::system::keystore2::IKeystoreService;
33 using IKeystoreSecurityLevel = ::android::system::keystore2::IKeystoreSecurityLevel;
34 using KeyDescriptor = ::android::system::keystore2::KeyDescriptor;
35 using KeyMetadata = ::android::system::keystore2::KeyMetadata;
36
37 public:
38 virtual ~KeystoreKey(){};
39 static android::base::Result<SigningKey*> getInstance();
40
41 virtual android::base::Result<std::string> sign(const std::string& message) const;
42 virtual android::base::Result<std::vector<uint8_t>> getPublicKey() const;
43
44 private:
45 KeystoreKey();
46 bool initialize();
47 android::base::Result<KeyMetadata> createNewKey(const KeyDescriptor& descriptor);
48
49 android::sp<IKeystoreService> mService;
50 android::sp<IKeystoreSecurityLevel> mSecurityLevel;
51 KeyMetadata mKeyMetadata;
52};