• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #pragma once
18 
19 #include <keymaster/android_keymaster_messages.h>
20 #include <keymaster/authorization_set.h>
21 
22 namespace keymaster {
23 
24 class Key;
25 class KeyFactory;
26 class KeymasterContext;
27 class OperationTable;
28 
29 /**
30  * This is the reference implementation of Keymaster.  In addition to acting as a reference for
31  * other Keymaster implementers to check their assumptions against, it is used by Keystore as the
32  * default implementation when no secure implementation is available, and may be installed and
33  * executed in secure hardware as a secure implementation.
34  *
35  * Note that this class doesn't actually implement the Keymaster HAL interface, instead it
36  * implements an alternative API which is similar to and based upon the HAL, but uses C++ "message"
37  * classes which support serialization.
38  *
39  * For non-secure, pure software implementation there is a HAL translation layer that converts the
40  * HAL's parameters to and from the message representations, which are then passed in to this
41  * API.
42  *
43  * For secure implementation there is another HAL translation layer that serializes the messages to
44  * the TEE. In the TEE implementation there's another component which deserializes the messages,
45  * extracts the relevant parameters and calls this API.
46  */
47 class AndroidKeymaster {
48   public:
49     AndroidKeymaster(KeymasterContext* context, size_t operation_table_size,
50                      uint32_t message_version = kDefaultMessageVersion);
51     virtual ~AndroidKeymaster();
52     AndroidKeymaster(AndroidKeymaster&&);
53 
54     void GetVersion(const GetVersionRequest& request, GetVersionResponse* response);
55     void SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
56                              SupportedAlgorithmsResponse* response);
57     void SupportedBlockModes(const SupportedBlockModesRequest& request,
58                              SupportedBlockModesResponse* response);
59     void SupportedPaddingModes(const SupportedPaddingModesRequest& request,
60                                SupportedPaddingModesResponse* response);
61     void SupportedDigests(const SupportedDigestsRequest& request,
62                           SupportedDigestsResponse* response);
63     void SupportedImportFormats(const SupportedImportFormatsRequest& request,
64                                 SupportedImportFormatsResponse* response);
65     void SupportedExportFormats(const SupportedExportFormatsRequest& request,
66                                 SupportedExportFormatsResponse* response);
67 
68     GetHmacSharingParametersResponse GetHmacSharingParameters();
69     ComputeSharedHmacResponse ComputeSharedHmac(const ComputeSharedHmacRequest& request);
70     VerifyAuthorizationResponse VerifyAuthorization(const VerifyAuthorizationRequest& request);
71     void GenerateTimestampToken(GenerateTimestampTokenRequest& request,
72                                 GenerateTimestampTokenResponse* response);
73     void AddRngEntropy(const AddEntropyRequest& request, AddEntropyResponse* response);
74     void Configure(const ConfigureRequest& request, ConfigureResponse* response);
75     void GenerateKey(const GenerateKeyRequest& request, GenerateKeyResponse* response);
76     void GenerateRkpKey(const GenerateRkpKeyRequest& request, GenerateRkpKeyResponse* response);
77     void GenerateCsr(const GenerateCsrRequest& request, GenerateCsrResponse* response);
78     void GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
79                                GetKeyCharacteristicsResponse* response);
80     void ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response);
81     void ImportWrappedKey(const ImportWrappedKeyRequest& request,
82                           ImportWrappedKeyResponse* response);
83     void ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response);
84     void AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response);
85     void UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response);
86     void DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response);
87     void DeleteAllKeys(const DeleteAllKeysRequest& request, DeleteAllKeysResponse* response);
88     void BeginOperation(const BeginOperationRequest& request, BeginOperationResponse* response);
89     void UpdateOperation(const UpdateOperationRequest& request, UpdateOperationResponse* response);
90     void FinishOperation(const FinishOperationRequest& request, FinishOperationResponse* response);
91     void AbortOperation(const AbortOperationRequest& request, AbortOperationResponse* response);
92 
93     EarlyBootEndedResponse EarlyBootEnded();
94     DeviceLockedResponse DeviceLocked(const DeviceLockedRequest& request);
95     GetVersion2Response GetVersion2(const GetVersion2Request& request);
96     ConfigureVendorPatchlevelResponse
97     ConfigureVendorPatchlevel(const ConfigureVendorPatchlevelRequest& request);
98     ConfigureBootPatchlevelResponse
99     ConfigureBootPatchlevel(const ConfigureBootPatchlevelRequest& request);
100 
101     bool has_operation(keymaster_operation_handle_t op_handle) const;
102 
103     // Returns the message version negotiated in GetVersion2.  All response messages should have
104     // this passed to their constructors.  This is done automatically for the methods that return a
105     // response by value.  The caller must do it for the methods that take a response pointer.
message_version()106     uint32_t message_version() const { return message_version_; }
107 
108   private:
109     // Loads the KM key from `key_blob`, getting app ID and app data from `additional_params`, if
110     // needed.  If loading the key fails for any reason (including failure of the version binding
111     // check), the returned UniquePtr is null and `*error` is set (`error` must not be null).
112     UniquePtr<Key> LoadKey(const keymaster_key_blob_t& key_blob,
113                            const AuthorizationSet& additional_params, keymaster_error_t* error);
114 
115     UniquePtr<KeymasterContext> context_;
116     UniquePtr<OperationTable> operation_table_;
117 
118     // If the caller doesn't bother to use GetVersion2 or GetVersion to configure the message
119     // version, assume kDefaultVersion, i.e. assume the client and server always support the
120     // latest default, which is the latest, except when experimental features are being added.
121     uint32_t message_version_;
122 };
123 
124 }  // namespace keymaster
125