Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -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 "google_keymaster_messages.h" |
| 18 | |
| 19 | namespace keymaster { |
| 20 | |
| 21 | GenerateKeyResponse::~GenerateKeyResponse() { delete[] key_blob.key_material; } |
| 22 | |
| 23 | size_t GenerateKeyResponse::SerializedSize() const { |
| 24 | if (error == KM_ERROR_OK) { |
| 25 | return sizeof(int32_t) /* error */ + sizeof(uint32_t) /* key size */ + |
| 26 | key_blob.key_material_size + sizeof(uint32_t) /* enforced size */ + |
| 27 | enforced.SerializedSize() + sizeof(uint32_t) /* unenforced size */ + |
| 28 | unenforced.SerializedSize(); |
| 29 | } else { |
| 30 | return sizeof(error); |
| 31 | } |
| 32 | } |
| 33 | |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 34 | uint8_t* GenerateKeyResponse::Serialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 35 | buf = append_uint32_to_buf(buf, end, error); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 36 | if (error == KM_ERROR_OK) { |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 37 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, |
| 38 | key_blob.key_material_size); |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 39 | buf = append_uint32_to_buf(buf, end, enforced.SerializedSize()); |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 40 | buf = enforced.Serialize(buf, end); |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 41 | buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize()); |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 42 | buf = unenforced.Serialize(buf, end); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 43 | }; |
| 44 | return buf; |
| 45 | } |
| 46 | |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 47 | bool GenerateKeyResponse::Deserialize(const uint8_t** buf, const uint8_t* end) { |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 48 | delete[] key_blob.key_material; |
| 49 | |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 50 | if (!copy_uint32_from_buf(buf, end, &error)) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 51 | return false; |
| 52 | |
| 53 | if (end == *buf) |
| 54 | // Nothing but an error |
| 55 | return true; |
| 56 | |
| 57 | uint32_t key_material_size; |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 58 | if (!copy_uint32_from_buf(buf, end, &key_material_size) || end - *buf < key_material_size) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 59 | return false; |
| 60 | |
| 61 | key_blob.key_material = new uint8_t[key_material_size]; |
| 62 | key_blob.key_material_size = key_material_size; |
| 63 | |
| 64 | if (key_blob.key_material == NULL || |
| 65 | !copy_from_buf(buf, end, key_blob.key_material, key_blob.key_material_size)) |
| 66 | return false; |
| 67 | |
| 68 | uint32_t enforced_size; |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 69 | if (!copy_uint32_from_buf(buf, end, &enforced_size) || end - *buf < enforced_size || |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 70 | !enforced.Deserialize(buf, *buf + enforced_size)) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 71 | return false; |
| 72 | |
| 73 | uint32_t unenforced_size; |
Shawn Willden | 172f8c9 | 2014-08-17 07:50:34 -0600 | [diff] [blame^] | 74 | if (!copy_uint32_from_buf(buf, end, &unenforced_size) || end - *buf < unenforced_size || |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 75 | !unenforced.Deserialize(buf, *buf + unenforced_size)) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 76 | return false; |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | } // namespace keymaster |