blob: 77a8d96539ed37a94075a63e5f8f718ed1edaead [file] [log] [blame]
Shawn Willdend67afae2014-08-19 12:36:27 -06001/*
2 * Copyright 2014 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#ifndef SYSTEM_KEYMASTER_KEY_H_
18#define SYSTEM_KEYMASTER_KEY_H_
19
Shawn Willdenb9d584d2015-01-22 16:35:00 -070020#include <hardware/keymaster_defs.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060021#include <keymaster/authorization_set.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060022#include <keymaster/logger.h>
Shawn Willdend67afae2014-08-19 12:36:27 -060023
Shawn Willdena278f612014-12-23 11:22:21 -070024#include "abstract_factory_registry.h"
25#include "unencrypted_key_blob.h"
26
Shawn Willdend67afae2014-08-19 12:36:27 -060027namespace keymaster {
28
Shawn Willdena278f612014-12-23 11:22:21 -070029class Key;
30
31/**
32 * KeyFactory is a pure interface whose subclasses know how to construct a specific subclass of Key.
33 * There is a one to one correspondence between Key subclasses and KeyFactory subclasses.
34 */
35class KeyFactory {
36 public:
37 virtual ~KeyFactory() {}
38
39 // Required for registry
40 typedef keymaster_algorithm_t KeyType;
41 virtual keymaster_algorithm_t registry_key() const = 0;
42
43 // Factory methods.
44 virtual Key* GenerateKey(const AuthorizationSet& key_description, const Logger& logger,
45 keymaster_error_t* error) = 0;
46 virtual Key* ImportKey(const AuthorizationSet& key_description,
47 keymaster_key_format_t key_format, const uint8_t* key_data,
48 size_t key_data_length, const Logger& logger,
49 keymaster_error_t* error) = 0;
50 virtual Key* LoadKey(const UnencryptedKeyBlob& blob, const Logger& logger,
51 keymaster_error_t* error) = 0;
52
53 // Informational methods.
54 virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) = 0;
55 virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) = 0;
56};
57
58typedef AbstractFactoryRegistry<KeyFactory> KeyFactoryRegistry;
59
Shawn Willdend67afae2014-08-19 12:36:27 -060060class KeyBlob;
61class Operation;
Shawn Willden72014ad2014-09-17 13:04:10 -060062class UnencryptedKeyBlob;
Shawn Willdend67afae2014-08-19 12:36:27 -060063
64class Key {
65 public:
Shawn Willdend67afae2014-08-19 12:36:27 -060066 virtual ~Key() {}
67 virtual Operation* CreateOperation(keymaster_purpose_t purpose, keymaster_error_t* error) = 0;
68
69 /**
70 * Return a copy of raw key material, in the key's preferred binary format.
71 */
72 virtual keymaster_error_t key_material(UniquePtr<uint8_t[]>*, size_t* size) const = 0;
73
74 /**
75 * Return a copy of raw key material, in the specified format.
76 */
Shawn Willdenf268d742014-08-19 15:36:26 -060077 virtual keymaster_error_t formatted_key_material(keymaster_key_format_t format,
78 UniquePtr<uint8_t[]>* material,
Shawn Willdend67afae2014-08-19 12:36:27 -060079 size_t* size) const = 0;
80
81 const AuthorizationSet& authorizations() const { return authorizations_; }
82
83 protected:
Shawn Willden2f3be362014-08-25 11:31:39 -060084 Key(const KeyBlob& blob, const Logger& logger);
85 Key(const AuthorizationSet& authorizations, const Logger& logger)
Shawn Willden407d4122014-08-25 16:49:13 -060086 : logger_(logger), authorizations_(authorizations) {}
Shawn Willden2f3be362014-08-25 11:31:39 -060087
88 const Logger& logger_;
Shawn Willdend67afae2014-08-19 12:36:27 -060089
90 private:
91 AuthorizationSet authorizations_;
92};
93
94} // namespace keymaster
95
96#endif // SYSTEM_KEYMASTER_KEY_H_