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 | |
| 34 | uint8_t* GenerateKeyResponse::Serialize(uint8_t* buf) const { |
| 35 | buf = append_to_buf(buf, static_cast<int32_t>(error)); |
| 36 | if (error == KM_ERROR_OK) { |
| 37 | buf = append_size_and_data_to_buf(buf, key_blob.key_material, key_blob.key_material_size); |
| 38 | buf = append_to_buf(buf, static_cast<uint32_t>(enforced.SerializedSize())); |
| 39 | buf = enforced.Serialize(buf); |
| 40 | buf = append_to_buf(buf, static_cast<uint32_t>(unenforced.SerializedSize())); |
| 41 | buf = unenforced.Serialize(buf); |
| 42 | }; |
| 43 | return buf; |
| 44 | } |
| 45 | |
| 46 | bool GenerateKeyResponse::DeserializeToCopy(const uint8_t** buf, const uint8_t* end) { |
| 47 | delete[] key_blob.key_material; |
| 48 | |
| 49 | if (!copy_from_buf(buf, end, &error)) |
| 50 | return false; |
| 51 | |
| 52 | if (end == *buf) |
| 53 | // Nothing but an error |
| 54 | return true; |
| 55 | |
| 56 | uint32_t key_material_size; |
| 57 | if (!copy_from_buf(buf, end, &key_material_size) || end - *buf < key_material_size) |
| 58 | return false; |
| 59 | |
| 60 | key_blob.key_material = new uint8_t[key_material_size]; |
| 61 | key_blob.key_material_size = key_material_size; |
| 62 | |
| 63 | if (key_blob.key_material == NULL || |
| 64 | !copy_from_buf(buf, end, key_blob.key_material, key_blob.key_material_size)) |
| 65 | return false; |
| 66 | |
| 67 | uint32_t enforced_size; |
| 68 | if (!copy_from_buf(buf, end, &enforced_size) || end - *buf < enforced_size || |
| 69 | !enforced.DeserializeToCopy(buf, *buf + enforced_size)) |
| 70 | return false; |
| 71 | |
| 72 | uint32_t unenforced_size; |
| 73 | if (!copy_from_buf(buf, end, &unenforced_size) || end - *buf < unenforced_size || |
| 74 | !unenforced.DeserializeToCopy(buf, *buf + unenforced_size)) |
| 75 | return false; |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | } // namespace keymaster |