1 /*
2 **
3 ** Copyright 2017, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef SOFTWARE_CONTEXT_KEYMASTER2_PASSTHROUGH_CONTEXT_H_
19 #define SOFTWARE_CONTEXT_KEYMASTER2_PASSTHROUGH_CONTEXT_H_
20 
21 #include <unordered_map>
22 
23 #include <hardware/keymaster2.h>
24 #include <hardware/keymaster_defs.h>
25 
26 #include <keymaster/keymaster_context.h>
27 #include <keymaster/km_version.h>
28 #include <keymaster/legacy_support/keymaster_passthrough_engine.h>
29 #include <keymaster/legacy_support/keymaster_passthrough_key.h>
30 
31 namespace keymaster {
32 
33 class Keymaster2PassthroughContext : public KeymasterContext {
34   public:
35     explicit Keymaster2PassthroughContext(KmVersion version, keymaster2_device_t* dev);
36 
GetKmVersion()37     KmVersion GetKmVersion() const override { return version_; }
38 
39     /**
40      * Sets the system version as reported by the system *itself*.  This is used to verify that the
41      * system believes itself to be running the same version that is reported by the bootloader, in
42      * hardware implementations.  For SoftKeymasterDevice, this sets the version information used.
43      *
44      * If the specified values don't match the bootloader-provided values, this method must return
45      * KM_ERROR_INVALID_ARGUMENT;
46      */
47     keymaster_error_t SetSystemVersion(uint32_t os_version, uint32_t os_patchlevel) override;
48 
49     /**
50      * Returns the system version.  For hardware-based implementations this will be the value
51      * reported by the bootloader.  For SoftKeymasterDevice it will be the verion information set by
52      * SetSystemVersion above.
53      */
54     void GetSystemVersion(uint32_t* os_version, uint32_t* os_patchlevel) const override;
55 
56     KeyFactory* GetKeyFactory(keymaster_algorithm_t algorithm) const override;
57     OperationFactory* GetOperationFactory(keymaster_algorithm_t algorithm,
58                                           keymaster_purpose_t purpose) const override;
59     keymaster_algorithm_t* GetSupportedAlgorithms(size_t* algorithms_count) const override;
60 
61     /**
62      * UpgradeKeyBlob takes an existing blob, parses out key material and constructs a new blob with
63      * the current format and OS version info.
64      */
65     keymaster_error_t UpgradeKeyBlob(const KeymasterKeyBlob& key_to_upgrade,
66                                      const AuthorizationSet& upgrade_params,
67                                      KeymasterKeyBlob* upgraded_key) const override;
68 
69     /**
70      * ParseKeyBlob takes a blob and extracts authorization sets and key material, returning an
71      * error if the blob fails integrity checking or decryption.  Note that the returned key
72      * material may itself be an opaque blob usable only by secure hardware (in the hybrid case).
73      *
74      * This method is called by AndroidKeymaster.
75      */
76     keymaster_error_t ParseKeyBlob(const KeymasterKeyBlob& blob,
77                                    const AuthorizationSet& additional_params,
78                                    UniquePtr<Key>* key) const override;
79 
80     /**
81      * Take whatever environment-specific action is appropriate (if any) to delete the specified
82      * key.
83      */
84     keymaster_error_t DeleteKey(const KeymasterKeyBlob& /* blob */) const override;
85 
86     /**
87      * Take whatever environment-specific action is appropriate to delete all keys.
88      */
89     keymaster_error_t DeleteAllKeys() const override;
90 
91     /**
92      * Adds entropy to the Cryptographic Pseudo Random Number Generator used to generate key
93      * material, and other cryptographic protocol elements.  Note that if the underlying CPRNG
94      * tracks the size of its entropy pool, it should not assume that the provided data contributes
95      * any entropy, and it should also ensure that data provided through this interface cannot
96      * "poison" the CPRNG outputs, making them predictable.
97      */
98     keymaster_error_t AddRngEntropy(const uint8_t* buf, size_t length) const override;
99 
100     /**
101      * Return the enforcement policy for this context, or null if no enforcement should be done.
102      */
103     KeymasterEnforcement* enforcement_policy() override;
104 
105     CertificateChain GenerateAttestation(const Key& key,  //
106                                          const AuthorizationSet& attest_params,
107                                          UniquePtr<Key> attest_key,
108                                          const KeymasterBlob& issuer_subject,
109                                          keymaster_error_t* error) const override;
GenerateSelfSignedCertificate(const Key &,const AuthorizationSet &,bool,keymaster_error_t * error)110     CertificateChain GenerateSelfSignedCertificate(const Key& /* key */,
111                                                    const AuthorizationSet& /* cert_params */,
112                                                    bool /* fake_signature */,
113                                                    keymaster_error_t* error) const override {
114         *error = KM_ERROR_UNIMPLEMENTED;
115         return {};
116     }
117 
118     keymaster_error_t
119     UnwrapKey(const KeymasterKeyBlob& wrapped_key_blob, const KeymasterKeyBlob& wrapping_key_blob,
120               const AuthorizationSet& wrapping_key_params, const KeymasterKeyBlob& masking_key,
121               AuthorizationSet* wrapped_key_params, keymaster_key_format_t* wrapped_key_format,
122               KeymasterKeyBlob* wrapped_key_material) const override;
123 
124   private:
125     keymaster2_device_t* device_;
126     mutable std::unordered_map<keymaster_algorithm_t, UniquePtr<KeymasterPassthroughKeyFactory>>
127         factories_;
128     UniquePtr<KeymasterPassthroughEngine> engine_;
129     uint32_t os_version_;
130     uint32_t os_patchlevel_;
131     const KmVersion version_;
132 };
133 
134 }  // namespace keymaster
135 
136 #endif  // SOFTWARE_CONTEXT_KEYMASTER2_PASSTHROUGH_CONTEXT_H_
137