Shawn Willden | ac39806 | 2015-05-20 16:36:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #include "keymaster0_engine.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <memory> |
| 22 | |
| 23 | #define LOG_TAG "Keymaster0Engine" |
| 24 | #include <cutils/log.h> |
| 25 | |
| 26 | #include "keymaster/android_keymaster_utils.h" |
| 27 | |
| 28 | #include <openssl/ec_key.h> |
| 29 | #include <openssl/ecdsa.h> |
| 30 | |
| 31 | #include "openssl_utils.h" |
| 32 | |
| 33 | using std::shared_ptr; |
| 34 | using std::unique_ptr; |
| 35 | |
| 36 | namespace keymaster { |
| 37 | |
| 38 | // int Keymaster0Engine::rsa_index_ = -1; |
| 39 | // int Keymaster0Engine::ec_key_index_ = -1; |
| 40 | Keymaster0Engine* Keymaster0Engine::instance_ = nullptr; |
| 41 | const RSA_METHOD Keymaster0Engine::rsa_method_ = { |
| 42 | { |
| 43 | 0 /* references */, 1 /* is_static */, |
| 44 | }, |
| 45 | .app_data = nullptr, |
| 46 | .init = nullptr, |
| 47 | .finish = nullptr, |
| 48 | .size = nullptr, |
| 49 | .sign = nullptr, |
| 50 | .verify = nullptr, |
| 51 | |
| 52 | .encrypt = nullptr, |
| 53 | .sign_raw = nullptr, |
| 54 | .decrypt = nullptr, |
| 55 | .verify_raw = nullptr, |
| 56 | |
| 57 | .private_transform = Keymaster0Engine::rsa_private_transform, |
| 58 | |
| 59 | .mod_exp = nullptr, |
| 60 | .bn_mod_exp = BN_mod_exp_mont, |
| 61 | |
| 62 | .flags = RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_OPAQUE | RSA_FLAG_EXT_PKEY, |
| 63 | |
| 64 | .keygen = nullptr, |
| 65 | .supports_digest = nullptr, |
| 66 | }; |
| 67 | |
| 68 | Keymaster0Engine::Keymaster0Engine(const keymaster0_device_t* keymaster0_device) |
| 69 | : keymaster0_device_(keymaster0_device), engine_(ENGINE_new()) { |
| 70 | assert(!instance_); |
| 71 | instance_ = this; |
| 72 | |
| 73 | rsa_index_ = RSA_get_ex_new_index(0 /* argl */, NULL /* argp */, NULL /* new_func */, |
| 74 | keyblob_dup, keyblob_free); |
| 75 | ENGINE_set_RSA_method(engine_, &rsa_method_, sizeof(rsa_method_)); |
| 76 | } |
| 77 | |
| 78 | Keymaster0Engine::~Keymaster0Engine() { |
| 79 | if (keymaster0_device_) |
| 80 | keymaster0_device_->common.close( |
| 81 | reinterpret_cast<hw_device_t*>(const_cast<keymaster0_device_t*>(keymaster0_device_))); |
| 82 | ENGINE_free(engine_); |
| 83 | instance_ = nullptr; |
| 84 | } |
| 85 | |
| 86 | bool Keymaster0Engine::GenerateRsaKey(uint64_t public_exponent, uint32_t public_modulus, |
| 87 | KeymasterKeyBlob* key_material) const { |
| 88 | assert(key_material); |
| 89 | keymaster_rsa_keygen_params_t params; |
| 90 | params.public_exponent = public_exponent; |
| 91 | params.modulus_size = public_modulus; |
| 92 | |
| 93 | uint8_t* key_blob = 0; |
| 94 | if (keymaster0_device_->generate_keypair(keymaster0_device_, TYPE_RSA, ¶ms, &key_blob, |
| 95 | &key_material->key_material_size) < 0) { |
| 96 | ALOGE("Error generating RSA key pair with keymaster0 device"); |
| 97 | return false; |
| 98 | } |
| 99 | unique_ptr<uint8_t, Malloc_Delete> key_blob_deleter(key_blob); |
| 100 | key_material->key_material = dup_buffer(key_blob, key_material->key_material_size); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool Keymaster0Engine::ImportRsaKey(keymaster_key_format_t key_format, |
| 105 | const KeymasterKeyBlob& to_import, |
| 106 | KeymasterKeyBlob* imported_key) const { |
| 107 | assert(imported_key); |
| 108 | if (key_format != KM_KEY_FORMAT_PKCS8) |
| 109 | return false; |
| 110 | |
| 111 | uint8_t* key_blob = 0; |
| 112 | if (keymaster0_device_->import_keypair(keymaster0_device_, to_import.key_material, |
| 113 | to_import.key_material_size, &key_blob, |
| 114 | &imported_key->key_material_size) < 0) { |
| 115 | ALOGW("Error importing RSA keypair with keymaster0 device"); |
| 116 | return false; |
| 117 | } |
| 118 | unique_ptr<uint8_t, Malloc_Delete> key_blob_deleter(key_blob); |
| 119 | imported_key->key_material = dup_buffer(key_blob, imported_key->key_material_size); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | static keymaster_key_blob_t* duplicate_blob(const uint8_t* key_data, size_t key_data_size) { |
| 124 | unique_ptr<uint8_t[]> key_material_copy(new uint8_t[key_data_size]); |
| 125 | if (!key_material_copy) |
| 126 | return nullptr; |
| 127 | |
| 128 | memcpy(key_material_copy.get(), key_data, key_data_size); |
| 129 | unique_ptr<keymaster_key_blob_t> blob_copy(new keymaster_key_blob_t); |
| 130 | blob_copy->key_material_size = key_data_size; |
| 131 | blob_copy->key_material = key_material_copy.release(); |
| 132 | return blob_copy.release(); |
| 133 | } |
| 134 | |
| 135 | inline keymaster_key_blob_t* duplicate_blob(const keymaster_key_blob_t& blob) { |
| 136 | return duplicate_blob(blob.key_material, blob.key_material_size); |
| 137 | } |
| 138 | |
| 139 | RSA* Keymaster0Engine::BlobToRsaKey(const KeymasterKeyBlob& blob) const { |
| 140 | // Create new RSA key (with engine methods) and insert blob |
| 141 | unique_ptr<RSA, RSA_Delete> rsa(RSA_new_method(engine_)); |
| 142 | if (!rsa) |
| 143 | return nullptr; |
| 144 | |
| 145 | keymaster_key_blob_t* blob_copy = duplicate_blob(blob); |
| 146 | if (!blob_copy->key_material || !RSA_set_ex_data(rsa.get(), rsa_index_, blob_copy)) |
| 147 | return nullptr; |
| 148 | |
| 149 | // Copy public key into new RSA key |
| 150 | unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(GetKeymaster0PublicKey(blob)); |
| 151 | if (!pkey) |
| 152 | return nullptr; |
| 153 | unique_ptr<RSA, RSA_Delete> public_rsa(EVP_PKEY_get1_RSA(pkey.get())); |
| 154 | if (!public_rsa) |
| 155 | return nullptr; |
| 156 | rsa->n = BN_dup(public_rsa->n); |
| 157 | rsa->e = BN_dup(public_rsa->e); |
| 158 | if (!rsa->n || !rsa->e) |
| 159 | return nullptr; |
| 160 | |
| 161 | return rsa.release(); |
| 162 | } |
| 163 | |
| 164 | const keymaster_key_blob_t* Keymaster0Engine::rsa_get_blob(const RSA* rsa) const { |
| 165 | return reinterpret_cast<keymaster_key_blob_t*>(RSA_get_ex_data(rsa, rsa_index_)); |
| 166 | } |
| 167 | |
| 168 | /* static */ |
| 169 | int Keymaster0Engine::keyblob_dup(CRYPTO_EX_DATA* /* to */, const CRYPTO_EX_DATA* /* from */, |
| 170 | void** from_d, int /* index */, long /* argl */, |
| 171 | void* /* argp */) { |
| 172 | keymaster_key_blob_t* blob = reinterpret_cast<keymaster_key_blob_t*>(*from_d); |
| 173 | *from_d = duplicate_blob(*blob); |
| 174 | if (*from_d) |
| 175 | return 1; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* static */ |
| 180 | void Keymaster0Engine::keyblob_free(void* /* parent */, void* ptr, CRYPTO_EX_DATA* /* data */, |
| 181 | int /* index*/, long /* argl */, void* /* argp */) { |
| 182 | keymaster_key_blob_t* blob = reinterpret_cast<keymaster_key_blob_t*>(ptr); |
| 183 | if (blob) { |
| 184 | delete[] blob->key_material; |
| 185 | delete blob; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* static */ |
| 190 | int Keymaster0Engine::rsa_private_transform(RSA* rsa, uint8_t* out, const uint8_t* in, size_t len) { |
| 191 | ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned)len); |
| 192 | |
| 193 | assert(instance_); |
| 194 | return instance_->RsaPrivateTransform(rsa, out, in, len); |
| 195 | } |
| 196 | |
| 197 | bool Keymaster0Engine::Keymaster0Sign(const void* signing_params, const keymaster_key_blob_t& blob, |
| 198 | const uint8_t* data, const size_t data_length, |
| 199 | unique_ptr<uint8_t[], Malloc_Delete>* signature, |
| 200 | size_t* signature_length) const { |
| 201 | uint8_t* signed_data; |
| 202 | int err = keymaster0_device_->sign_data(keymaster0_device_, signing_params, blob.key_material, |
| 203 | blob.key_material_size, data, data_length, &signed_data, |
| 204 | signature_length); |
| 205 | if (err < 0) { |
| 206 | ALOGE("Keymaster0 signing failed with error %d", err); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | signature->reset(signed_data); |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | EVP_PKEY* Keymaster0Engine::GetKeymaster0PublicKey(const KeymasterKeyBlob& blob) const { |
| 215 | uint8_t* pub_key_data; |
| 216 | size_t pub_key_data_length; |
| 217 | int err = keymaster0_device_->get_keypair_public(keymaster0_device_, blob.key_material, |
| 218 | blob.key_material_size, &pub_key_data, |
| 219 | &pub_key_data_length); |
| 220 | if (err < 0) { |
| 221 | ALOGE("Error %d extracting public key", err); |
| 222 | return nullptr; |
| 223 | } |
| 224 | unique_ptr<uint8_t, Malloc_Delete> pub_key(pub_key_data); |
| 225 | |
| 226 | const uint8_t* p = pub_key_data; |
| 227 | return d2i_PUBKEY(nullptr /* allocate new struct */, &p, pub_key_data_length); |
| 228 | } |
| 229 | |
| 230 | int Keymaster0Engine::RsaPrivateTransform(RSA* rsa, uint8_t* out, const uint8_t* in, |
| 231 | size_t len) const { |
| 232 | const keymaster_key_blob_t* key_blob = rsa_get_blob(rsa); |
| 233 | if (key_blob == NULL) { |
| 234 | ALOGE("key had no key_blob!"); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | keymaster_rsa_sign_params_t sign_params = {DIGEST_NONE, PADDING_NONE}; |
| 239 | unique_ptr<uint8_t[], Malloc_Delete> signature; |
| 240 | size_t signature_length; |
| 241 | if (!Keymaster0Sign(&sign_params, *key_blob, in, len, &signature, &signature_length)) |
| 242 | return 0; |
| 243 | Eraser eraser(signature.get(), signature_length); |
| 244 | |
| 245 | if (signature_length > len) { |
| 246 | /* The result of the RSA operation can never be larger than the size of |
| 247 | * the modulus so we assume that the result has extra zeros on the |
| 248 | * left. This provides attackers with an oracle, but there's nothing |
| 249 | * that we can do about it here. */ |
| 250 | memcpy(out, signature.get() + signature_length - len, len); |
| 251 | } else if (signature_length < len) { |
| 252 | /* If the keymaster0 implementation returns a short value we assume that |
| 253 | * it's because it removed leading zeros from the left side. This is |
| 254 | * bad because it provides attackers with an oracle but we cannot do |
| 255 | * anything about a broken keymaster0 implementation here. */ |
| 256 | memset(out, 0, len); |
| 257 | memcpy(out + len - signature_length, signature.get(), signature_length); |
| 258 | } else { |
| 259 | memcpy(out, signature.get(), len); |
| 260 | } |
| 261 | |
| 262 | ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa); |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | } // namespace keymaster |