Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -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 | |
| 17 | #include <assert.h> |
| 18 | |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 19 | #include <keymaster/google_keymaster_utils.h> |
Shawn Willden | 368bc77 | 2014-08-27 06:45:34 -0600 | [diff] [blame] | 20 | #include <keymaster/key_blob.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 21 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 22 | namespace keymaster { |
| 23 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 24 | const size_t KeyBlob::NONCE_LENGTH; |
| 25 | const size_t KeyBlob::TAG_LENGTH; |
| 26 | |
Shawn Willden | 0c0f036 | 2014-09-23 10:00:37 -0600 | [diff] [blame] | 27 | KeyBlob::KeyBlob(const uint8_t* key_blob, size_t key_blob_length) : error_(KM_ERROR_OK) { |
| 28 | Deserialize(&key_blob, key_blob + key_blob_length); |
| 29 | } |
| 30 | |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 31 | KeyBlob::KeyBlob(const keymaster_key_blob_t& key_blob) : error_(KM_ERROR_OK) { |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 32 | const uint8_t* key_material = key_blob.key_material; |
Shawn Willden | 0c0f036 | 2014-09-23 10:00:37 -0600 | [diff] [blame] | 33 | Deserialize(&key_material, key_blob.key_material + key_blob.key_material_size); |
Shawn Willden | 407d412 | 2014-08-25 16:49:13 -0600 | [diff] [blame] | 34 | } |
| 35 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 36 | size_t KeyBlob::SerializedSize() const { |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 37 | return 1 /* version byte */ + sizeof(uint32_t) /* nonce length */ + NONCE_LENGTH + |
| 38 | sizeof(uint32_t) + key_material_length() + sizeof(uint32_t) /* tag length */ + |
| 39 | TAG_LENGTH + enforced_.SerializedSize() + unenforced_.SerializedSize(); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 40 | } |
| 41 | |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 42 | const uint8_t BLOB_VERSION = 0; |
| 43 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 44 | uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const { |
Andreas Gampe | 894c4b6 | 2014-11-25 18:11:42 -0800 | [diff] [blame^] | 45 | const uint8_t* start __unused = buf; |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 46 | *buf++ = BLOB_VERSION; |
| 47 | buf = append_size_and_data_to_buf(buf, end, nonce(), NONCE_LENGTH); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 48 | buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length()); |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 49 | buf = append_size_and_data_to_buf(buf, end, tag(), TAG_LENGTH); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 50 | buf = enforced_.Serialize(buf, end); |
| 51 | buf = unenforced_.Serialize(buf, end); |
| 52 | assert(buf - start == static_cast<ptrdiff_t>(SerializedSize())); |
| 53 | return buf; |
| 54 | } |
| 55 | |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame] | 56 | bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 57 | const uint8_t* start = *buf_ptr; |
| 58 | uint8_t version = *(*buf_ptr)++; |
| 59 | size_t nonce_length; |
| 60 | size_t tag_length; |
| 61 | if (version != BLOB_VERSION || |
| 62 | !copy_size_and_data_from_buf(buf_ptr, end, &nonce_length, &nonce_) || |
| 63 | nonce_length != NONCE_LENGTH || |
| 64 | !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_, |
| 65 | &encrypted_key_material_) || |
| 66 | !copy_size_and_data_from_buf(buf_ptr, end, &tag_length, &tag_) || |
| 67 | tag_length != TAG_LENGTH || !enforced_.Deserialize(buf_ptr, end) || |
| 68 | !unenforced_.Deserialize(buf_ptr, end)) { |
| 69 | *buf_ptr = start; |
| 70 | // This blob failed to parse. Either it's corrupted or it's a blob generated by an earlier |
| 71 | // version of keymaster using a previous blob format which did not include the version byte |
| 72 | // or the nonce or tag length fields. So we try to parse it as that previous version. |
| 73 | // |
| 74 | // Note that it's not really a problem if we erronously parse a corrupted blob, because |
| 75 | // decryption will fail the authentication check. |
| 76 | // |
| 77 | // A bigger potential problem is: What if a valid unversioned blob appears to parse |
| 78 | // correctly as a versioned blob? It would then be rejected during decryption, causing a |
| 79 | // valid key to become unusable. If this is a disk encryption key, upgrading to a keymaster |
| 80 | // version with the new format would destroy the user's data. |
| 81 | // |
| 82 | // What is the probability that an unversioned key could be successfully parsed as a version |
| 83 | // 0 key? The first 12 bytes of an unversioned key are the nonce, which, in the only |
| 84 | // keymaster version released with unversioned keys, is chosen randomly. In order for an |
| 85 | // unversioned key to parse as a version 0 key, the following must be true about the first |
| 86 | // five of those random bytes: |
| 87 | // |
| 88 | // 1. The first byte must be zero. This will happen with probability 1/2^8. |
| 89 | // |
| 90 | // 2. The second through fifth bytes must contain an unsigned integer value equal to |
| 91 | // NONCE_LENGTH. This will happen with probability 1/2^32. |
| 92 | // |
| 93 | // Based on those two checks alone, the probability of interpreting an unversioned blob as a |
| 94 | // version 0 blob is 1/2^40. That's small enough to be negligible, but there are additional |
| 95 | // checks which lower it further. |
| 96 | *buf_ptr = start; |
| 97 | if (!DeserializeUnversionedBlob(buf_ptr, end)) |
| 98 | return false; |
| 99 | } |
| 100 | return ExtractKeyCharacteristics(); |
| 101 | } |
| 102 | |
| 103 | bool KeyBlob::DeserializeUnversionedBlob(const uint8_t** buf_ptr, const uint8_t* end) { |
| 104 | nonce_.reset(new uint8_t[NONCE_LENGTH]); |
| 105 | tag_.reset(new uint8_t[TAG_LENGTH]); |
| 106 | if (!nonce_.get() || !tag_.get()) { |
| 107 | error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 108 | return false; |
| 109 | } |
| 110 | |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 111 | if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) || |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 112 | !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_, |
| 113 | &encrypted_key_material_) || |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 114 | !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) || |
| 115 | !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) { |
Shawn Willden | 5b87762 | 2014-09-17 14:32:43 -0600 | [diff] [blame] | 116 | encrypted_key_material_.reset(); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 117 | error_ = KM_ERROR_INVALID_KEY_BLOB; |
| 118 | return false; |
| 119 | } |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 120 | return ExtractKeyCharacteristics(); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 121 | } |
| 122 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 123 | KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced) |
| 124 | : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced) { |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 125 | } |
| 126 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 127 | void KeyBlob::SetEncryptedKey(uint8_t* encrypted_key_material, size_t encrypted_key_material_length, |
| 128 | uint8_t* nonce, uint8_t* tag) { |
| 129 | ClearKeyData(); |
| 130 | encrypted_key_material_.reset(encrypted_key_material); |
| 131 | key_material_length_ = encrypted_key_material_length; |
| 132 | nonce_.reset(nonce); |
| 133 | tag_.reset(tag); |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 134 | } |
| 135 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 136 | bool KeyBlob::ExtractKeyCharacteristics() { |
| 137 | if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) && |
| 138 | !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) { |
| 139 | error_ = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 140 | return false; |
| 141 | } |
| 142 | if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) && |
| 143 | !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) { |
| 144 | error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 150 | } // namespace keymaster |