Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 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 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 17 | #include "asymmetric_key.h" |
| 18 | |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 19 | #include <new> |
| 20 | |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 21 | #include <openssl/asn1.h> |
| 22 | #include <openssl/stack.h> |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 23 | #include <openssl/x509.h> |
Shawn Willden | eabae30 | 2016-04-28 08:48:40 -0600 | [diff] [blame] | 24 | #include <openssl/x509v3.h> |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 25 | |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 26 | #include "attestation_record.h" |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 27 | #include "openssl_err.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 28 | #include "openssl_utils.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 29 | |
| 30 | namespace keymaster { |
| 31 | |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 32 | namespace { |
| 33 | template <typename T> T min(T a, T b) { |
| 34 | return (a < b) ? a : b; |
| 35 | } |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 36 | } // anonymous namespace |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 37 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 38 | keymaster_error_t AsymmetricKey::formatted_key_material(keymaster_key_format_t format, |
| 39 | UniquePtr<uint8_t[]>* material, |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 40 | size_t* size) const { |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 41 | if (format != KM_KEY_FORMAT_X509) |
| 42 | return KM_ERROR_UNSUPPORTED_KEY_FORMAT; |
| 43 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 44 | if (material == NULL || size == NULL) |
| 45 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
| 46 | |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 47 | EVP_PKEY_Ptr pkey(EVP_PKEY_new()); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 48 | if (!InternalToEvp(pkey.get())) |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 49 | return TranslateLastOpenSslError(); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 50 | |
| 51 | int key_data_length = i2d_PUBKEY(pkey.get(), NULL); |
| 52 | if (key_data_length <= 0) |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 53 | return TranslateLastOpenSslError(); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 54 | |
Shawn Willden | 0f906ec | 2015-06-20 09:16:30 -0600 | [diff] [blame] | 55 | material->reset(new (std::nothrow) uint8_t[key_data_length]); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 56 | if (material->get() == NULL) |
| 57 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 58 | |
| 59 | uint8_t* tmp = material->get(); |
| 60 | if (i2d_PUBKEY(pkey.get(), &tmp) != key_data_length) { |
| 61 | material->reset(); |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 62 | return TranslateLastOpenSslError(); |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | *size = key_data_length; |
| 66 | return KM_ERROR_OK; |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 67 | } |
| 68 | |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 69 | static keymaster_error_t build_attestation_extension(const AuthorizationSet& attest_params, |
| 70 | const AuthorizationSet& tee_enforced, |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 71 | const AuthorizationSet& sw_enforced, |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 72 | const KeymasterContext& context, |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 73 | X509_EXTENSION_Ptr* extension) { |
| 74 | ASN1_OBJECT_Ptr oid( |
| 75 | OBJ_txt2obj(kAttestionRecordOid, 1 /* accept numerical dotted string form only */)); |
| 76 | if (!oid.get()) |
| 77 | return TranslateLastOpenSslError(); |
| 78 | |
| 79 | UniquePtr<uint8_t[]> attest_bytes; |
| 80 | size_t attest_bytes_len; |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 81 | keymaster_error_t error = build_attestation_record(attest_params, sw_enforced, tee_enforced, |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 82 | context, &attest_bytes, &attest_bytes_len); |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 83 | if (error != KM_ERROR_OK) |
| 84 | return error; |
| 85 | |
| 86 | ASN1_OCTET_STRING_Ptr attest_str(ASN1_OCTET_STRING_new()); |
| 87 | if (!attest_str.get() || |
| 88 | !ASN1_OCTET_STRING_set(attest_str.get(), attest_bytes.get(), attest_bytes_len)) |
| 89 | return TranslateLastOpenSslError(); |
| 90 | |
| 91 | extension->reset( |
| 92 | X509_EXTENSION_create_by_OBJ(nullptr, oid.get(), 0 /* not critical */, attest_str.get())); |
| 93 | if (!extension->get()) |
| 94 | return TranslateLastOpenSslError(); |
| 95 | |
| 96 | return KM_ERROR_OK; |
| 97 | } |
| 98 | |
| 99 | static bool add_public_key(EVP_PKEY* key, X509* certificate, keymaster_error_t* error) { |
| 100 | if (!X509_set_pubkey(certificate, key)) { |
| 101 | *error = TranslateLastOpenSslError(); |
| 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 107 | static bool add_attestation_extension(const AuthorizationSet& attest_params, |
| 108 | const AuthorizationSet& tee_enforced, |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 109 | const AuthorizationSet& sw_enforced, |
| 110 | const KeymasterContext& context, X509* certificate, |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 111 | keymaster_error_t* error) { |
| 112 | X509_EXTENSION_Ptr attest_extension; |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 113 | *error = build_attestation_extension(attest_params, tee_enforced, sw_enforced, context, |
| 114 | &attest_extension); |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 115 | if (*error != KM_ERROR_OK) |
| 116 | return false; |
| 117 | |
| 118 | if (!X509_add_ext(certificate, attest_extension.get() /* Don't release; copied */, |
| 119 | -1 /* insert at end */)) { |
| 120 | *error = TranslateLastOpenSslError(); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | static keymaster_error_t get_certificate_blob(X509* certificate, keymaster_blob_t* blob) { |
| 128 | int len = i2d_X509(certificate, nullptr); |
| 129 | if (len < 0) |
| 130 | return TranslateLastOpenSslError(); |
| 131 | |
| 132 | uint8_t* data = new uint8_t[len]; |
| 133 | if (!data) |
| 134 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 135 | |
| 136 | uint8_t* p = data; |
| 137 | i2d_X509(certificate, &p); |
| 138 | |
| 139 | blob->data_length = len; |
| 140 | blob->data = data; |
| 141 | |
| 142 | return KM_ERROR_OK; |
| 143 | } |
| 144 | |
| 145 | static bool allocate_cert_chain(size_t entry_count, keymaster_cert_chain_t* chain, |
| 146 | keymaster_error_t* error) { |
| 147 | if (chain->entries) { |
| 148 | for (size_t i = 0; i < chain->entry_count; ++i) |
| 149 | delete[] chain->entries[i].data; |
| 150 | delete[] chain->entries; |
| 151 | } |
| 152 | |
| 153 | chain->entry_count = entry_count; |
| 154 | chain->entries = new keymaster_blob_t[entry_count]; |
| 155 | if (!chain->entries) { |
| 156 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 157 | return false; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | // Copies the intermediate and root certificates into chain, leaving the first slot for the leaf |
| 163 | // certificate. |
| 164 | static bool copy_attestation_chain(const KeymasterContext& context, |
| 165 | keymaster_algorithm_t sign_algorithm, |
| 166 | keymaster_cert_chain_t* chain, keymaster_error_t* error) { |
| 167 | |
| 168 | UniquePtr<keymaster_cert_chain_t, CertificateChainDelete> attest_key_chain( |
| 169 | context.AttestationChain(sign_algorithm, error)); |
| 170 | if (!attest_key_chain.get()) |
| 171 | return false; |
| 172 | |
| 173 | if (!allocate_cert_chain(attest_key_chain->entry_count + 1, chain, error)) |
| 174 | return false; |
| 175 | |
| 176 | chain->entries[0].data = nullptr; // Leave empty for the leaf certificate. |
| 177 | chain->entries[1].data_length = 0; |
| 178 | |
| 179 | for (size_t i = 0; i < attest_key_chain->entry_count; ++i) { |
| 180 | chain->entries[i + 1] = attest_key_chain->entries[i]; |
| 181 | attest_key_chain->entries[i].data = nullptr; |
| 182 | } |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | keymaster_error_t AsymmetricKey::GenerateAttestation(const KeymasterContext& context, |
| 188 | const AuthorizationSet& attest_params, |
| 189 | const AuthorizationSet& tee_enforced, |
| 190 | const AuthorizationSet& sw_enforced, |
| 191 | keymaster_cert_chain_t* cert_chain) const { |
| 192 | |
| 193 | keymaster_algorithm_t sign_algorithm; |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 194 | if ((!sw_enforced.GetTagValue(TAG_ALGORITHM, &sign_algorithm) && |
| 195 | !tee_enforced.GetTagValue(TAG_ALGORITHM, &sign_algorithm))) |
| 196 | return KM_ERROR_UNKNOWN_ERROR; |
| 197 | |
| 198 | if ((sign_algorithm != KM_ALGORITHM_RSA && sign_algorithm != KM_ALGORITHM_EC)) |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 199 | return KM_ERROR_INCOMPATIBLE_ALGORITHM; |
| 200 | |
| 201 | EVP_PKEY_Ptr pkey(EVP_PKEY_new()); |
| 202 | if (!InternalToEvp(pkey.get())) |
| 203 | return TranslateLastOpenSslError(); |
| 204 | |
| 205 | X509_Ptr certificate(X509_new()); |
| 206 | if (!certificate.get()) |
| 207 | return TranslateLastOpenSslError(); |
| 208 | |
| 209 | if (!X509_set_version(certificate.get(), 2 /* version 3, but zero-based */)) |
| 210 | return TranslateLastOpenSslError(); |
| 211 | |
| 212 | ASN1_INTEGER_Ptr serialNumber(ASN1_INTEGER_new()); |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 213 | if (!serialNumber.get() || !ASN1_INTEGER_set(serialNumber.get(), 1) || |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 214 | !X509_set_serialNumber(certificate.get(), serialNumber.get() /* Don't release; copied */)) |
| 215 | return TranslateLastOpenSslError(); |
| 216 | |
| 217 | // TODO(swillden): Find useful values (if possible) for issuerName and subjectName. |
| 218 | X509_NAME_Ptr issuerName(X509_NAME_new()); |
| 219 | if (!issuerName.get() || |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 220 | !X509_NAME_add_entry_by_txt(issuerName.get(), "CN", MBSTRING_ASC, |
| 221 | reinterpret_cast<const uint8_t*>("Android Keymaster"), |
| 222 | -1 /* len */, -1 /* loc */, 0 /* set */) || |
| 223 | !X509_set_issuer_name(certificate.get(), issuerName.get() /* Don't release; copied */)) |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 224 | return TranslateLastOpenSslError(); |
| 225 | |
| 226 | X509_NAME_Ptr subjectName(X509_NAME_new()); |
| 227 | if (!subjectName.get() || |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 228 | !X509_NAME_add_entry_by_txt(subjectName.get(), "CN", MBSTRING_ASC, |
| 229 | reinterpret_cast<const uint8_t*>("A Keymaster Key"), |
| 230 | -1 /* len */, -1 /* loc */, 0 /* set */) || |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 231 | !X509_set_subject_name(certificate.get(), subjectName.get() /* Don't release; copied */)) |
| 232 | return TranslateLastOpenSslError(); |
| 233 | |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 234 | ASN1_TIME_Ptr notBefore(ASN1_TIME_new()); |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 235 | uint64_t activeDateTime = 0; |
| 236 | authorizations().GetTagValue(TAG_ACTIVE_DATETIME, &activeDateTime); |
| 237 | if (!notBefore.get() || !ASN1_TIME_set(notBefore.get(), activeDateTime / 1000) || |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 238 | !X509_set_notBefore(certificate.get(), notBefore.get() /* Don't release; copied */)) |
| 239 | return TranslateLastOpenSslError(); |
| 240 | |
| 241 | ASN1_TIME_Ptr notAfter(ASN1_TIME_new()); |
Shawn Willden | 22dcdb7 | 2016-02-03 07:38:46 -0700 | [diff] [blame] | 242 | uint64_t usageExpireDateTime = UINT64_MAX; |
| 243 | authorizations().GetTagValue(TAG_USAGE_EXPIRE_DATETIME, &usageExpireDateTime); |
| 244 | // TODO(swillden): When trusty can use the C++ standard library change the calculation of |
| 245 | // notAfterTime to use std::numeric_limits<time_t>::max(), rather than assuming that time_t is |
| 246 | // 32 bits. |
| 247 | time_t notAfterTime = min(static_cast<uint64_t>(UINT32_MAX), usageExpireDateTime / 1000); |
| 248 | if (!notAfter.get() || !ASN1_TIME_set(notAfter.get(), notAfterTime) || |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 249 | !X509_set_notAfter(certificate.get(), notAfter.get() /* Don't release; copied */)) |
| 250 | return TranslateLastOpenSslError(); |
| 251 | |
| 252 | keymaster_error_t error = KM_ERROR_OK; |
| 253 | EVP_PKEY_Ptr sign_key(context.AttestationKey(sign_algorithm, &error)); |
| 254 | |
| 255 | if (!sign_key.get() || // |
| 256 | !add_public_key(pkey.get(), certificate.get(), &error) || |
Shawn Willden | 3609584 | 2016-03-09 19:38:44 -0700 | [diff] [blame] | 257 | !add_attestation_extension(attest_params, tee_enforced, sw_enforced, context, |
| 258 | certificate.get(), &error)) |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 259 | return error; |
| 260 | |
Shawn Willden | 31746ba | 2016-04-28 14:36:55 +0000 | [diff] [blame] | 261 | if (!copy_attestation_chain(context, sign_algorithm, cert_chain, &error)) |
| 262 | return error; |
| 263 | |
Shawn Willden | eabae30 | 2016-04-28 08:48:40 -0600 | [diff] [blame] | 264 | // Copy subject key identifier from cert_chain->entries[1] as authority key_id. |
| 265 | if (cert_chain->entry_count < 2) { |
| 266 | // cert_chain must have at least two entries, one for the cert we're trying to create and |
| 267 | // one for the cert for the key that signs the new cert. |
| 268 | return KM_ERROR_UNKNOWN_ERROR; |
| 269 | } |
| 270 | |
| 271 | const uint8_t* p = cert_chain->entries[1].data; |
| 272 | X509_Ptr signing_cert(d2i_X509(nullptr, &p, cert_chain->entries[1].data_length)); |
| 273 | if (!signing_cert.get()) { |
| 274 | return TranslateLastOpenSslError(); |
| 275 | } |
| 276 | |
| 277 | UniquePtr<X509V3_CTX> x509v3_ctx(new X509V3_CTX); |
| 278 | *x509v3_ctx = {}; |
| 279 | X509V3_set_ctx(x509v3_ctx.get(), signing_cert.get(), certificate.get(), nullptr /* req */, |
| 280 | nullptr /* crl */, 0 /* flags */); |
| 281 | |
| 282 | X509_EXTENSION_Ptr auth_key_id(X509V3_EXT_nconf_nid(nullptr /* conf */, x509v3_ctx.get(), |
| 283 | NID_authority_key_identifier, |
| 284 | const_cast<char*>("keyid:always"))); |
| 285 | if (!auth_key_id.get() || |
| 286 | !X509_add_ext(certificate.get(), auth_key_id.get() /* Don't release; copied */, |
| 287 | -1 /* insert at end */)) { |
| 288 | return TranslateLastOpenSslError(); |
| 289 | } |
| 290 | |
| 291 | if (!X509_sign(certificate.get(), sign_key.get(), EVP_sha256())) |
| 292 | return TranslateLastOpenSslError(); |
| 293 | |
Shawn Willden | aa58329 | 2016-01-28 12:13:28 -0700 | [diff] [blame] | 294 | return get_certificate_blob(certificate.get(), &cert_chain->entries[0]); |
| 295 | } |
| 296 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 297 | } // namespace keymaster |