android_keymaster_messages.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. #include <keymaster/android_keymaster_messages.h>
  17. #include <keymaster/android_keymaster_utils.h>
  18. namespace keymaster {
  19. /*
  20. * Helper functions for working with key blobs.
  21. */
  22. static void set_key_blob(keymaster_key_blob_t* key_blob, const void* key_material, size_t length) {
  23. delete[] key_blob->key_material;
  24. key_blob->key_material = dup_buffer(key_material, length);
  25. key_blob->key_material_size = length;
  26. }
  27. static size_t key_blob_size(const keymaster_key_blob_t& key_blob) {
  28. return sizeof(uint32_t) /* key size */ + key_blob.key_material_size;
  29. }
  30. static uint8_t* serialize_key_blob(const keymaster_key_blob_t& key_blob, uint8_t* buf,
  31. const uint8_t* end) {
  32. return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
  33. }
  34. static bool deserialize_key_blob(keymaster_key_blob_t* key_blob, const uint8_t** buf_ptr,
  35. const uint8_t* end) {
  36. delete[] key_blob->key_material;
  37. key_blob->key_material = nullptr;
  38. UniquePtr<uint8_t[]> deserialized_key_material;
  39. if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob->key_material_size,
  40. &deserialized_key_material))
  41. return false;
  42. key_blob->key_material = deserialized_key_material.release();
  43. return true;
  44. }
  45. static size_t blob_size(const keymaster_blob_t& blob) {
  46. return sizeof(uint32_t) /* data size */ + blob.data_length;
  47. }
  48. static uint8_t* serialize_blob(const keymaster_blob_t& blob, uint8_t* buf, const uint8_t* end) {
  49. return append_size_and_data_to_buf(buf, end, blob.data, blob.data_length);
  50. }
  51. static bool deserialize_blob(keymaster_blob_t* blob, const uint8_t** buf_ptr, const uint8_t* end) {
  52. delete[] blob->data;
  53. *blob = {};
  54. UniquePtr<uint8_t[]> deserialized_blob;
  55. if (!copy_size_and_data_from_buf(buf_ptr, end, &blob->data_length, &deserialized_blob))
  56. return false;
  57. blob->data = deserialized_blob.release();
  58. return true;
  59. }
  60. size_t KeymasterResponse::SerializedSize() const {
  61. if (error != KM_ERROR_OK)
  62. return sizeof(int32_t);
  63. else
  64. return sizeof(int32_t) + NonErrorSerializedSize();
  65. }
  66. uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const {
  67. buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error));
  68. if (error == KM_ERROR_OK)
  69. buf = NonErrorSerialize(buf, end);
  70. return buf;
  71. }
  72. bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  73. if (!copy_uint32_from_buf(buf_ptr, end, &error))
  74. return false;
  75. if (error != KM_ERROR_OK)
  76. return true;
  77. return NonErrorDeserialize(buf_ptr, end);
  78. }
  79. GenerateKeyResponse::~GenerateKeyResponse() {
  80. delete[] key_blob.key_material;
  81. }
  82. size_t GenerateKeyResponse::NonErrorSerializedSize() const {
  83. return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize();
  84. }
  85. uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  86. buf = serialize_key_blob(key_blob, buf, end);
  87. buf = enforced.Serialize(buf, end);
  88. return unenforced.Serialize(buf, end);
  89. }
  90. bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  91. return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) &&
  92. unenforced.Deserialize(buf_ptr, end);
  93. }
  94. GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() {
  95. delete[] key_blob.key_material;
  96. }
  97. void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) {
  98. set_key_blob(&key_blob, key_material, length);
  99. }
  100. size_t GetKeyCharacteristicsRequest::SerializedSize() const {
  101. return key_blob_size(key_blob) + additional_params.SerializedSize();
  102. }
  103. uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  104. buf = serialize_key_blob(key_blob, buf, end);
  105. return additional_params.Serialize(buf, end);
  106. }
  107. bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  108. return deserialize_key_blob(&key_blob, buf_ptr, end) &&
  109. additional_params.Deserialize(buf_ptr, end);
  110. }
  111. size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const {
  112. return enforced.SerializedSize() + unenforced.SerializedSize();
  113. }
  114. uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  115. buf = enforced.Serialize(buf, end);
  116. return unenforced.Serialize(buf, end);
  117. }
  118. bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr,
  119. const uint8_t* end) {
  120. return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end);
  121. }
  122. void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) {
  123. set_key_blob(&key_blob, key_material, length);
  124. }
  125. size_t BeginOperationRequest::SerializedSize() const {
  126. return sizeof(uint32_t) /* purpose */ + key_blob_size(key_blob) +
  127. additional_params.SerializedSize();
  128. }
  129. uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  130. buf = append_uint32_to_buf(buf, end, purpose);
  131. buf = serialize_key_blob(key_blob, buf, end);
  132. return additional_params.Serialize(buf, end);
  133. }
  134. bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  135. return copy_uint32_from_buf(buf_ptr, end, &purpose) &&
  136. deserialize_key_blob(&key_blob, buf_ptr, end) &&
  137. additional_params.Deserialize(buf_ptr, end);
  138. }
  139. size_t BeginOperationResponse::NonErrorSerializedSize() const {
  140. if (message_version == 0)
  141. return sizeof(op_handle);
  142. else
  143. return sizeof(op_handle) + output_params.SerializedSize();
  144. }
  145. uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  146. buf = append_uint64_to_buf(buf, end, op_handle);
  147. if (message_version > 0)
  148. buf = output_params.Serialize(buf, end);
  149. return buf;
  150. }
  151. bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  152. bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle);
  153. if (retval && message_version > 0)
  154. retval = output_params.Deserialize(buf_ptr, end);
  155. return retval;
  156. }
  157. size_t UpdateOperationRequest::SerializedSize() const {
  158. if (message_version == 0)
  159. return sizeof(op_handle) + input.SerializedSize();
  160. else
  161. return sizeof(op_handle) + input.SerializedSize() + additional_params.SerializedSize();
  162. }
  163. uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  164. buf = append_uint64_to_buf(buf, end, op_handle);
  165. buf = input.Serialize(buf, end);
  166. if (message_version > 0)
  167. buf = additional_params.Serialize(buf, end);
  168. return buf;
  169. }
  170. bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  171. bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end);
  172. if (retval && message_version > 0)
  173. retval = additional_params.Deserialize(buf_ptr, end);
  174. return retval;
  175. }
  176. size_t UpdateOperationResponse::NonErrorSerializedSize() const {
  177. size_t size = 0;
  178. switch (message_version) {
  179. case 3:
  180. case 2:
  181. size += output_params.SerializedSize();
  182. FALLTHROUGH;
  183. case 1:
  184. size += sizeof(uint32_t);
  185. FALLTHROUGH;
  186. case 0:
  187. size += output.SerializedSize();
  188. break;
  189. default:
  190. assert(false);
  191. }
  192. return size;
  193. }
  194. uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  195. buf = output.Serialize(buf, end);
  196. if (message_version > 0)
  197. buf = append_uint32_to_buf(buf, end, input_consumed);
  198. if (message_version > 1)
  199. buf = output_params.Serialize(buf, end);
  200. return buf;
  201. }
  202. bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  203. bool retval = output.Deserialize(buf_ptr, end);
  204. if (retval && message_version > 0)
  205. retval = copy_uint32_from_buf(buf_ptr, end, &input_consumed);
  206. if (retval && message_version > 1)
  207. retval = output_params.Deserialize(buf_ptr, end);
  208. return retval;
  209. }
  210. size_t FinishOperationRequest::SerializedSize() const {
  211. size_t size = 0;
  212. switch (message_version) {
  213. case 3:
  214. size += input.SerializedSize();
  215. FALLTHROUGH;
  216. case 2:
  217. case 1:
  218. size += additional_params.SerializedSize();
  219. FALLTHROUGH;
  220. case 0:
  221. size += sizeof(op_handle) + signature.SerializedSize();
  222. break;
  223. default:
  224. assert(false); // Should never get here.
  225. }
  226. return size;
  227. }
  228. uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  229. buf = append_uint64_to_buf(buf, end, op_handle);
  230. buf = signature.Serialize(buf, end);
  231. if (message_version > 0)
  232. buf = additional_params.Serialize(buf, end);
  233. if (message_version > 2)
  234. buf = input.Serialize(buf, end);
  235. return buf;
  236. }
  237. bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  238. bool retval =
  239. copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
  240. if (retval && message_version > 0)
  241. retval = additional_params.Deserialize(buf_ptr, end);
  242. if (retval && message_version > 2)
  243. retval = input.Deserialize(buf_ptr, end);
  244. return retval;
  245. }
  246. size_t FinishOperationResponse::NonErrorSerializedSize() const {
  247. if (message_version < 2)
  248. return output.SerializedSize();
  249. else
  250. return output.SerializedSize() + output_params.SerializedSize();
  251. }
  252. uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  253. buf = output.Serialize(buf, end);
  254. if (message_version > 1)
  255. buf = output_params.Serialize(buf, end);
  256. return buf;
  257. }
  258. bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  259. bool retval = output.Deserialize(buf_ptr, end);
  260. if (retval && message_version > 1)
  261. retval = output_params.Deserialize(buf_ptr, end);
  262. return retval;
  263. }
  264. size_t AddEntropyRequest::SerializedSize() const {
  265. return random_data.SerializedSize();
  266. }
  267. uint8_t* AddEntropyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  268. return random_data.Serialize(buf, end);
  269. }
  270. bool AddEntropyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  271. return random_data.Deserialize(buf_ptr, end);
  272. }
  273. void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
  274. delete[] key_data;
  275. key_data = dup_buffer(key_material, length);
  276. key_data_length = length;
  277. }
  278. size_t ImportKeyRequest::SerializedSize() const {
  279. return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
  280. sizeof(uint32_t) /* key_data_length */ + key_data_length;
  281. }
  282. uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  283. buf = key_description.Serialize(buf, end);
  284. buf = append_uint32_to_buf(buf, end, key_format);
  285. return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
  286. }
  287. bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  288. delete[] key_data;
  289. key_data = nullptr;
  290. UniquePtr<uint8_t[]> deserialized_key_material;
  291. if (!key_description.Deserialize(buf_ptr, end) ||
  292. !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
  293. !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
  294. return false;
  295. key_data = deserialized_key_material.release();
  296. return true;
  297. }
  298. void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
  299. set_key_blob(&key_blob, key_material, length);
  300. }
  301. size_t ImportKeyResponse::NonErrorSerializedSize() const {
  302. return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize();
  303. }
  304. uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  305. buf = serialize_key_blob(key_blob, buf, end);
  306. buf = enforced.Serialize(buf, end);
  307. return unenforced.Serialize(buf, end);
  308. }
  309. bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  310. return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) &&
  311. unenforced.Deserialize(buf_ptr, end);
  312. }
  313. void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
  314. set_key_blob(&key_blob, key_material, length);
  315. }
  316. size_t ExportKeyRequest::SerializedSize() const {
  317. return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
  318. key_blob_size(key_blob);
  319. }
  320. uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  321. buf = additional_params.Serialize(buf, end);
  322. buf = append_uint32_to_buf(buf, end, key_format);
  323. return serialize_key_blob(key_blob, buf, end);
  324. }
  325. bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  326. return additional_params.Deserialize(buf_ptr, end) &&
  327. copy_uint32_from_buf(buf_ptr, end, &key_format) &&
  328. deserialize_key_blob(&key_blob, buf_ptr, end);
  329. }
  330. void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
  331. delete[] key_data;
  332. key_data = dup_buffer(key_material, length);
  333. key_data_length = length;
  334. }
  335. size_t ExportKeyResponse::NonErrorSerializedSize() const {
  336. return sizeof(uint32_t) /* key_data_length */ + key_data_length;
  337. }
  338. uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  339. return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
  340. }
  341. bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  342. delete[] key_data;
  343. key_data = nullptr;
  344. UniquePtr<uint8_t[]> deserialized_key_material;
  345. if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
  346. return false;
  347. key_data = deserialized_key_material.release();
  348. return true;
  349. }
  350. void DeleteKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
  351. set_key_blob(&key_blob, key_material, length);
  352. }
  353. size_t DeleteKeyRequest::SerializedSize() const {
  354. return key_blob_size(key_blob);
  355. }
  356. uint8_t* DeleteKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  357. return serialize_key_blob(key_blob, buf, end);
  358. }
  359. bool DeleteKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  360. return deserialize_key_blob(&key_blob, buf_ptr, end);
  361. }
  362. size_t GetVersionResponse::NonErrorSerializedSize() const {
  363. return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver);
  364. }
  365. uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  366. if (buf + NonErrorSerializedSize() <= end) {
  367. *buf++ = major_ver;
  368. *buf++ = minor_ver;
  369. *buf++ = subminor_ver;
  370. } else {
  371. buf += NonErrorSerializedSize();
  372. }
  373. return buf;
  374. }
  375. bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  376. if (*buf_ptr + NonErrorSerializedSize() > end)
  377. return false;
  378. const uint8_t* tmp = *buf_ptr;
  379. major_ver = *tmp++;
  380. minor_ver = *tmp++;
  381. subminor_ver = *tmp++;
  382. *buf_ptr = tmp;
  383. return true;
  384. }
  385. AttestKeyRequest::~AttestKeyRequest() {
  386. delete[] key_blob.key_material;
  387. }
  388. void AttestKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
  389. set_key_blob(&key_blob, key_material, length);
  390. }
  391. size_t AttestKeyRequest::SerializedSize() const {
  392. return key_blob_size(key_blob) + attest_params.SerializedSize();
  393. }
  394. uint8_t* AttestKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  395. buf = serialize_key_blob(key_blob, buf, end);
  396. return attest_params.Serialize(buf, end);
  397. }
  398. bool AttestKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  399. return deserialize_key_blob(&key_blob, buf_ptr, end) && attest_params.Deserialize(buf_ptr, end);
  400. }
  401. AttestKeyResponse::~AttestKeyResponse() {
  402. for (size_t i = 0; i < certificate_chain.entry_count; ++i)
  403. delete[] certificate_chain.entries[i].data;
  404. delete[] certificate_chain.entries;
  405. }
  406. const size_t kMaxChainEntryCount = 10;
  407. bool AttestKeyResponse::AllocateChain(size_t entry_count) {
  408. if (entry_count > kMaxChainEntryCount)
  409. return false;
  410. if (certificate_chain.entries) {
  411. for (size_t i = 0; i < certificate_chain.entry_count; ++i)
  412. delete[] certificate_chain.entries[i].data;
  413. delete[] certificate_chain.entries;
  414. }
  415. certificate_chain.entry_count = entry_count;
  416. certificate_chain.entries = new (std::nothrow) keymaster_blob_t[entry_count];
  417. if (!certificate_chain.entries) {
  418. certificate_chain.entry_count = 0;
  419. return false;
  420. }
  421. memset(certificate_chain.entries, 0, sizeof(certificate_chain.entries[0]) * entry_count);
  422. return true;
  423. }
  424. size_t AttestKeyResponse::NonErrorSerializedSize() const {
  425. size_t result = sizeof(uint32_t); /* certificate_chain.entry_count */
  426. for (size_t i = 0; i < certificate_chain.entry_count; ++i) {
  427. result += sizeof(uint32_t); /* certificate_chain.entries[i].data_length */
  428. result += certificate_chain.entries[i].data_length;
  429. }
  430. return result;
  431. }
  432. uint8_t* AttestKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  433. buf = append_uint32_to_buf(buf, end, certificate_chain.entry_count);
  434. for (size_t i = 0; i < certificate_chain.entry_count; ++i) {
  435. buf = append_size_and_data_to_buf(buf, end, certificate_chain.entries[i].data,
  436. certificate_chain.entries[i].data_length);
  437. }
  438. return buf;
  439. }
  440. bool AttestKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  441. size_t entry_count;
  442. if (!copy_uint32_from_buf(buf_ptr, end, &entry_count) || !AllocateChain(entry_count))
  443. return false;
  444. for (size_t i = 0; i < certificate_chain.entry_count; ++i) {
  445. UniquePtr<uint8_t[]> data;
  446. size_t data_length;
  447. if (!copy_size_and_data_from_buf(buf_ptr, end, &data_length, &data))
  448. return false;
  449. certificate_chain.entries[i].data = data.release();
  450. certificate_chain.entries[i].data_length = data_length;
  451. }
  452. return true;
  453. }
  454. UpgradeKeyRequest::~UpgradeKeyRequest() {
  455. delete[] key_blob.key_material;
  456. }
  457. void UpgradeKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
  458. set_key_blob(&key_blob, key_material, length);
  459. }
  460. size_t UpgradeKeyRequest::SerializedSize() const {
  461. return key_blob_size(key_blob) + upgrade_params.SerializedSize();
  462. }
  463. uint8_t* UpgradeKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  464. buf = serialize_key_blob(key_blob, buf, end);
  465. return upgrade_params.Serialize(buf, end);
  466. }
  467. bool UpgradeKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  468. return deserialize_key_blob(&key_blob, buf_ptr, end) &&
  469. upgrade_params.Deserialize(buf_ptr, end);
  470. }
  471. UpgradeKeyResponse::~UpgradeKeyResponse() {
  472. delete[] upgraded_key.key_material;
  473. }
  474. size_t UpgradeKeyResponse::NonErrorSerializedSize() const {
  475. return key_blob_size(upgraded_key);
  476. }
  477. uint8_t* UpgradeKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  478. return serialize_key_blob(upgraded_key, buf, end);
  479. }
  480. bool UpgradeKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  481. return deserialize_key_blob(&upgraded_key, buf_ptr, end);
  482. }
  483. size_t HmacSharingParameters::SerializedSize() const {
  484. return blob_size(seed) + sizeof(nonce);
  485. }
  486. uint8_t* HmacSharingParameters::Serialize(uint8_t* buf, const uint8_t* end) const {
  487. buf = serialize_blob(seed, buf, end);
  488. return append_to_buf(buf, end, nonce, sizeof(nonce));
  489. }
  490. bool HmacSharingParameters::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  491. return deserialize_blob(&seed, buf_ptr, end) &&
  492. copy_from_buf(buf_ptr, end, nonce, sizeof(nonce));
  493. }
  494. size_t HmacSharingParametersArray::SerializedSize() const {
  495. size_t size = sizeof(uint32_t); // num_params size
  496. for (size_t i = 0; i < num_params; ++i) {
  497. size += params_array[i].SerializedSize();
  498. }
  499. return size;
  500. }
  501. uint8_t* HmacSharingParametersArray::Serialize(uint8_t* buf, const uint8_t* end) const {
  502. buf = append_uint32_to_buf(buf, end, num_params);
  503. for (size_t i = 0; i < num_params; ++i) {
  504. buf = params_array[i].Serialize(buf, end);
  505. }
  506. return buf;
  507. }
  508. bool HmacSharingParametersArray::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  509. if (!copy_uint32_from_buf(buf_ptr, end, &num_params)) return false;
  510. params_array = new (std::nothrow) HmacSharingParameters[num_params];
  511. if (!params_array) return false;
  512. for (size_t i = 0; i < num_params; ++i) {
  513. if (!params_array[i].Deserialize(buf_ptr, end)) return false;
  514. }
  515. return true;
  516. }
  517. size_t ComputeSharedHmacResponse::NonErrorSerializedSize() const {
  518. return blob_size(sharing_check);
  519. }
  520. uint8_t* ComputeSharedHmacResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  521. return serialize_blob(sharing_check, buf, end);
  522. }
  523. bool ComputeSharedHmacResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  524. return deserialize_blob(&sharing_check, buf_ptr, end);
  525. }
  526. size_t ImportWrappedKeyRequest::SerializedSize() const {
  527. return sizeof(uint32_t) /* wrapped_key_data_length */ + wrapped_key.key_material_size +
  528. sizeof(uint32_t) /* wrapping_key_data_length */ + wrapping_key.key_material_size +
  529. sizeof(uint32_t) /* masking_key_data_length */ + masking_key.key_material_size +
  530. additional_params.SerializedSize() + sizeof(uint64_t) /* password_sid */ +
  531. sizeof(uint64_t) /* biometric_sid */;
  532. }
  533. uint8_t* ImportWrappedKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
  534. buf = serialize_key_blob(wrapped_key, buf, end);
  535. buf = serialize_key_blob(wrapping_key, buf, end);
  536. buf = serialize_key_blob(masking_key, buf, end);
  537. buf = additional_params.Serialize(buf, end);
  538. buf = append_uint64_to_buf(buf, end, password_sid);
  539. return append_uint64_to_buf(buf, end, biometric_sid);
  540. }
  541. bool ImportWrappedKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  542. return deserialize_key_blob(&wrapped_key, buf_ptr, end) &&
  543. deserialize_key_blob(&wrapping_key, buf_ptr, end) &&
  544. deserialize_key_blob(&masking_key, buf_ptr, end) &&
  545. additional_params.Deserialize(buf_ptr, end) &&
  546. copy_uint64_from_buf(buf_ptr, end, &password_sid) &&
  547. copy_uint64_from_buf(buf_ptr, end, &biometric_sid);
  548. }
  549. void ImportWrappedKeyRequest::SetWrappedMaterial(const void* key_material, size_t length) {
  550. set_key_blob(&wrapped_key, key_material, length);
  551. }
  552. void ImportWrappedKeyRequest::SetWrappingMaterial(const void* key_material, size_t length) {
  553. set_key_blob(&wrapping_key, key_material, length);
  554. }
  555. void ImportWrappedKeyRequest::SetMaskingKeyMaterial(const void* key_material, size_t length) {
  556. set_key_blob(&masking_key, key_material, length);
  557. }
  558. void ImportWrappedKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
  559. set_key_blob(&key_blob, key_material, length);
  560. }
  561. size_t ImportWrappedKeyResponse::NonErrorSerializedSize() const {
  562. return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize();
  563. }
  564. uint8_t* ImportWrappedKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
  565. buf = serialize_key_blob(key_blob, buf, end);
  566. buf = enforced.Serialize(buf, end);
  567. return unenforced.Serialize(buf, end);
  568. }
  569. bool ImportWrappedKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  570. return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) &&
  571. unenforced.Deserialize(buf_ptr, end);
  572. }
  573. size_t HardwareAuthToken::SerializedSize() const {
  574. return sizeof(challenge) + sizeof(user_id) + sizeof(authenticator_id) +
  575. sizeof(authenticator_type) + sizeof(timestamp) + blob_size(mac);
  576. }
  577. uint8_t* HardwareAuthToken::Serialize(uint8_t* buf, const uint8_t* end) const {
  578. buf = append_uint64_to_buf(buf, end, challenge);
  579. buf = append_uint64_to_buf(buf, end, user_id);
  580. buf = append_uint64_to_buf(buf, end, authenticator_id);
  581. buf = append_uint32_to_buf(buf, end, authenticator_type);
  582. buf = append_uint64_to_buf(buf, end, timestamp);
  583. return serialize_blob(mac, buf, end);
  584. }
  585. bool HardwareAuthToken::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  586. return copy_uint64_from_buf(buf_ptr, end, &challenge) &&
  587. copy_uint64_from_buf(buf_ptr, end, &user_id) &&
  588. copy_uint64_from_buf(buf_ptr, end, &authenticator_id) &&
  589. copy_uint32_from_buf(buf_ptr, end, &authenticator_type) &&
  590. copy_uint64_from_buf(buf_ptr, end, &timestamp) && //
  591. deserialize_blob(&mac, buf_ptr, end);
  592. }
  593. size_t VerificationToken::SerializedSize() const {
  594. return sizeof(challenge) + sizeof(timestamp) + parameters_verified.SerializedSize() +
  595. sizeof(security_level) + blob_size(mac);
  596. }
  597. uint8_t* VerificationToken::Serialize(uint8_t* buf, const uint8_t* end) const {
  598. buf = append_uint64_to_buf(buf, end, challenge);
  599. buf = append_uint64_to_buf(buf, end, timestamp);
  600. buf = parameters_verified.Serialize(buf, end);
  601. buf = append_uint32_to_buf(buf, end, security_level);
  602. return serialize_blob(mac, buf, end);
  603. }
  604. bool VerificationToken::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  605. return copy_uint64_from_buf(buf_ptr, end, &challenge) &&
  606. copy_uint64_from_buf(buf_ptr, end, &timestamp) &&
  607. parameters_verified.Deserialize(buf_ptr, end) &&
  608. copy_uint32_from_buf(buf_ptr, end, &security_level) &&
  609. deserialize_blob(&mac, buf_ptr, end);
  610. }
  611. } // namespace keymaster