blob: 5705197f4be4acc4994fbd3f4ffd85b25aa4a6b1 [file] [log] [blame]
Shawn Willden4db3fbd2014-08-08 22:13:44 -06001/*
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 Willden98d9b922014-08-26 08:14:10 -060019#include <keymaster/google_keymaster_utils.h>
Shawn Willden368bc772014-08-27 06:45:34 -060020#include <keymaster/key_blob.h>
Shawn Willdenf01329d2015-03-11 21:51:38 -060021#include <keymaster/logger.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060022
Shawn Willden4db3fbd2014-08-08 22:13:44 -060023namespace keymaster {
24
Shawn Willden4db3fbd2014-08-08 22:13:44 -060025const size_t KeyBlob::NONCE_LENGTH;
26const size_t KeyBlob::TAG_LENGTH;
27
Shawn Willden0c0f0362014-09-23 10:00:37 -060028KeyBlob::KeyBlob(const uint8_t* key_blob, size_t key_blob_length) : error_(KM_ERROR_OK) {
29 Deserialize(&key_blob, key_blob + key_blob_length);
30}
31
Shawn Willden5b877622014-09-17 14:32:43 -060032KeyBlob::KeyBlob(const keymaster_key_blob_t& key_blob) : error_(KM_ERROR_OK) {
Shawn Willden72014ad2014-09-17 13:04:10 -060033 const uint8_t* key_material = key_blob.key_material;
Shawn Willden0c0f0362014-09-23 10:00:37 -060034 Deserialize(&key_material, key_blob.key_material + key_blob.key_material_size);
Shawn Willden407d4122014-08-25 16:49:13 -060035}
36
Shawn Willden4db3fbd2014-08-08 22:13:44 -060037size_t KeyBlob::SerializedSize() const {
Shawn Willden5b877622014-09-17 14:32:43 -060038 return 1 /* version byte */ + sizeof(uint32_t) /* nonce length */ + NONCE_LENGTH +
39 sizeof(uint32_t) + key_material_length() + sizeof(uint32_t) /* tag length */ +
40 TAG_LENGTH + enforced_.SerializedSize() + unenforced_.SerializedSize();
Shawn Willden4db3fbd2014-08-08 22:13:44 -060041}
42
Shawn Willden5b877622014-09-17 14:32:43 -060043const uint8_t BLOB_VERSION = 0;
44
Shawn Willden4db3fbd2014-08-08 22:13:44 -060045uint8_t* KeyBlob::Serialize(uint8_t* buf, const uint8_t* end) const {
Andreas Gampe7568e032014-11-25 19:52:47 -080046 const uint8_t* start __attribute__((__unused__)) = buf;
Shawn Willden5b877622014-09-17 14:32:43 -060047 *buf++ = BLOB_VERSION;
48 buf = append_size_and_data_to_buf(buf, end, nonce(), NONCE_LENGTH);
Shawn Willden4db3fbd2014-08-08 22:13:44 -060049 buf = append_size_and_data_to_buf(buf, end, encrypted_key_material(), key_material_length());
Shawn Willden5b877622014-09-17 14:32:43 -060050 buf = append_size_and_data_to_buf(buf, end, tag(), TAG_LENGTH);
Shawn Willden4db3fbd2014-08-08 22:13:44 -060051 buf = enforced_.Serialize(buf, end);
52 buf = unenforced_.Serialize(buf, end);
53 assert(buf - start == static_cast<ptrdiff_t>(SerializedSize()));
54 return buf;
55}
56
Shawn Willden172f8c92014-08-17 07:50:34 -060057bool KeyBlob::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden5b877622014-09-17 14:32:43 -060058 const uint8_t* start = *buf_ptr;
59 uint8_t version = *(*buf_ptr)++;
60 size_t nonce_length;
61 size_t tag_length;
62 if (version != BLOB_VERSION ||
63 !copy_size_and_data_from_buf(buf_ptr, end, &nonce_length, &nonce_) ||
64 nonce_length != NONCE_LENGTH ||
65 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
66 &encrypted_key_material_) ||
67 !copy_size_and_data_from_buf(buf_ptr, end, &tag_length, &tag_) ||
68 tag_length != TAG_LENGTH || !enforced_.Deserialize(buf_ptr, end) ||
69 !unenforced_.Deserialize(buf_ptr, end)) {
70 *buf_ptr = start;
71 // This blob failed to parse. Either it's corrupted or it's a blob generated by an earlier
72 // version of keymaster using a previous blob format which did not include the version byte
73 // or the nonce or tag length fields. So we try to parse it as that previous version.
74 //
75 // Note that it's not really a problem if we erronously parse a corrupted blob, because
76 // decryption will fail the authentication check.
77 //
78 // A bigger potential problem is: What if a valid unversioned blob appears to parse
79 // correctly as a versioned blob? It would then be rejected during decryption, causing a
80 // valid key to become unusable. If this is a disk encryption key, upgrading to a keymaster
81 // version with the new format would destroy the user's data.
82 //
83 // What is the probability that an unversioned key could be successfully parsed as a version
84 // 0 key? The first 12 bytes of an unversioned key are the nonce, which, in the only
85 // keymaster version released with unversioned keys, is chosen randomly. In order for an
86 // unversioned key to parse as a version 0 key, the following must be true about the first
87 // five of those random bytes:
88 //
89 // 1. The first byte must be zero. This will happen with probability 1/2^8.
90 //
91 // 2. The second through fifth bytes must contain an unsigned integer value equal to
92 // NONCE_LENGTH. This will happen with probability 1/2^32.
93 //
94 // Based on those two checks alone, the probability of interpreting an unversioned blob as a
95 // version 0 blob is 1/2^40. That's small enough to be negligible, but there are additional
96 // checks which lower it further.
Shawn Willdenf01329d2015-03-11 21:51:38 -060097 LOG_I("Failed to deserialize versioned key blob. Assuming unversioned.");
Shawn Willden5b877622014-09-17 14:32:43 -060098 *buf_ptr = start;
99 if (!DeserializeUnversionedBlob(buf_ptr, end))
100 return false;
101 }
102 return ExtractKeyCharacteristics();
103}
104
105bool KeyBlob::DeserializeUnversionedBlob(const uint8_t** buf_ptr, const uint8_t* end) {
106 nonce_.reset(new uint8_t[NONCE_LENGTH]);
107 tag_.reset(new uint8_t[TAG_LENGTH]);
108 if (!nonce_.get() || !tag_.get()) {
109 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
110 return false;
111 }
112
Shawn Willdenf2282b32014-08-25 06:49:54 -0600113 if (!copy_from_buf(buf_ptr, end, nonce_.get(), NONCE_LENGTH) ||
Shawn Willden72014ad2014-09-17 13:04:10 -0600114 !copy_size_and_data_from_buf(buf_ptr, end, &key_material_length_,
115 &encrypted_key_material_) ||
Shawn Willdenf2282b32014-08-25 06:49:54 -0600116 !copy_from_buf(buf_ptr, end, tag_.get(), TAG_LENGTH) ||
117 !enforced_.Deserialize(buf_ptr, end) || !unenforced_.Deserialize(buf_ptr, end)) {
Shawn Willden5b877622014-09-17 14:32:43 -0600118 encrypted_key_material_.reset();
Shawn Willdenf01329d2015-03-11 21:51:38 -0600119 LOG_E("Failed to deserialize unversioned blob", 0);
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600120 error_ = KM_ERROR_INVALID_KEY_BLOB;
121 return false;
122 }
Shawn Willden72014ad2014-09-17 13:04:10 -0600123 return ExtractKeyCharacteristics();
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600124}
125
Shawn Willden72014ad2014-09-17 13:04:10 -0600126KeyBlob::KeyBlob(const AuthorizationSet& enforced, const AuthorizationSet& unenforced)
127 : error_(KM_ERROR_OK), enforced_(enforced), unenforced_(unenforced) {
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600128}
129
Shawn Willden72014ad2014-09-17 13:04:10 -0600130void KeyBlob::SetEncryptedKey(uint8_t* encrypted_key_material, size_t encrypted_key_material_length,
131 uint8_t* nonce, uint8_t* tag) {
132 ClearKeyData();
133 encrypted_key_material_.reset(encrypted_key_material);
134 key_material_length_ = encrypted_key_material_length;
135 nonce_.reset(nonce);
136 tag_.reset(tag);
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600137}
138
Shawn Willden1615f2e2014-08-13 10:37:40 -0600139bool KeyBlob::ExtractKeyCharacteristics() {
140 if (!enforced_.GetTagValue(TAG_ALGORITHM, &algorithm_) &&
141 !unenforced_.GetTagValue(TAG_ALGORITHM, &algorithm_)) {
142 error_ = KM_ERROR_UNSUPPORTED_ALGORITHM;
143 return false;
144 }
145 if (!enforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_) &&
146 !unenforced_.GetTagValue(TAG_KEY_SIZE, &key_size_bits_)) {
147 error_ = KM_ERROR_UNSUPPORTED_KEY_SIZE;
148 return false;
149 }
150 return true;
151}
152
Shawn Willden174fa242015-01-14 08:35:07 -0700153keymaster_key_origin_t KeyBlob::origin() const {
154 keymaster_key_origin_t origin = KM_ORIGIN_SOFTWARE;
155 if (!enforced_.GetTagValue(TAG_ORIGIN, &origin) &&
156 !unenforced_.GetTagValue(TAG_ORIGIN, &origin))
157 // This should be impossible.
158 assert(false);
159 return origin;
160}
161
Shawn Willden4db3fbd2014-08-08 22:13:44 -0600162} // namespace keymaster