gatekeeper_messages.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 2015 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 <gatekeeper/gatekeeper_messages.h>
  18. #include <string.h>
  19. namespace gatekeeper {
  20. /**
  21. * Methods for serializing/deserializing SizedBuffers
  22. */
  23. struct __attribute__((__packed__)) serial_header_t {
  24. uint32_t error;
  25. uint32_t user_id;
  26. };
  27. static inline uint32_t serialized_buffer_size(const SizedBuffer &buf) {
  28. return sizeof(buf.length) + buf.length;
  29. }
  30. static inline void append_to_buffer(uint8_t **buffer, const SizedBuffer *to_append) {
  31. memcpy(*buffer, &to_append->length, sizeof(to_append->length));
  32. *buffer += sizeof(to_append->length);
  33. if (to_append->length != 0) {
  34. memcpy(*buffer, to_append->buffer.get(), to_append->length);
  35. *buffer += to_append->length;
  36. }
  37. }
  38. static inline gatekeeper_error_t read_from_buffer(const uint8_t **buffer, const uint8_t *end,
  39. SizedBuffer *target) {
  40. if (*buffer + sizeof(target->length) > end) return ERROR_INVALID;
  41. memcpy(&target->length, *buffer, sizeof(target->length));
  42. *buffer += sizeof(target->length);
  43. if (target->length != 0) {
  44. const size_t buffer_size = end - *buffer;
  45. if (buffer_size < target->length) return ERROR_INVALID;
  46. target->buffer.reset(new uint8_t[target->length]);
  47. memcpy(target->buffer.get(), *buffer, target->length);
  48. *buffer += target->length;
  49. }
  50. return ERROR_NONE;
  51. }
  52. uint32_t GateKeeperMessage::GetSerializedSize() const {
  53. if (error == ERROR_NONE) {
  54. uint32_t size = sizeof(serial_header_t) + nonErrorSerializedSize();
  55. return size;
  56. } else {
  57. uint32_t size = sizeof(serial_header_t);
  58. if (error == ERROR_RETRY) {
  59. size += sizeof(retry_timeout);
  60. }
  61. return size;
  62. }
  63. }
  64. uint32_t GateKeeperMessage::Serialize(uint8_t *buffer, const uint8_t *end) const {
  65. uint32_t bytes_written = 0;
  66. if (buffer + GetSerializedSize() > end) {
  67. return 0;
  68. }
  69. serial_header_t *header = reinterpret_cast<serial_header_t *>(buffer);
  70. if (error != ERROR_NONE) {
  71. if (buffer + sizeof(serial_header_t) > end) return 0;
  72. header->error = error;
  73. header->user_id = user_id;
  74. bytes_written += sizeof(*header);
  75. if (error == ERROR_RETRY) {
  76. memcpy(buffer + sizeof(serial_header_t), &retry_timeout, sizeof(retry_timeout));
  77. bytes_written += sizeof(retry_timeout);
  78. }
  79. } else {
  80. if (buffer + sizeof(serial_header_t) + nonErrorSerializedSize() > end)
  81. return 0;
  82. header->error = error;
  83. header->user_id = user_id;
  84. nonErrorSerialize(buffer + sizeof(*header));
  85. bytes_written += sizeof(*header) + nonErrorSerializedSize();
  86. }
  87. return bytes_written;
  88. }
  89. gatekeeper_error_t GateKeeperMessage::Deserialize(const uint8_t *payload, const uint8_t *end) {
  90. if (payload + sizeof(serial_header_t) > end) return ERROR_INVALID;
  91. const serial_header_t *header = reinterpret_cast<const serial_header_t *>(payload);
  92. payload += sizeof(serial_header_t);
  93. user_id = header->user_id;
  94. if (header->error == ERROR_NONE) {
  95. error = nonErrorDeserialize(payload, end);
  96. } else {
  97. error = static_cast<gatekeeper_error_t>(header->error);
  98. if (error == ERROR_RETRY) {
  99. if (payload < end) {
  100. if (payload + sizeof(retry_timeout) <= end) {
  101. memcpy(&retry_timeout, payload, sizeof(retry_timeout));
  102. } else {
  103. error = ERROR_INVALID;
  104. }
  105. } else {
  106. retry_timeout = 0;
  107. }
  108. }
  109. }
  110. return error;
  111. }
  112. void GateKeeperMessage::SetRetryTimeout(uint32_t retry_timeout) {
  113. this->retry_timeout = retry_timeout;
  114. this->error = ERROR_RETRY;
  115. }
  116. VerifyRequest::VerifyRequest(uint32_t user_id, uint64_t challenge,
  117. SizedBuffer *enrolled_password_handle, SizedBuffer *provided_password_payload) {
  118. this->user_id = user_id;
  119. this->challenge = challenge;
  120. this->password_handle.buffer.reset(enrolled_password_handle->buffer.release());
  121. this->password_handle.length = enrolled_password_handle->length;
  122. this->provided_password.buffer.reset(provided_password_payload->buffer.release());
  123. this->provided_password.length = provided_password_payload->length;
  124. }
  125. VerifyRequest::VerifyRequest() {
  126. memset_s(&password_handle, 0, sizeof(password_handle));
  127. memset_s(&provided_password, 0, sizeof(provided_password));
  128. }
  129. VerifyRequest::~VerifyRequest() {
  130. if (password_handle.buffer.get()) {
  131. password_handle.buffer.reset();
  132. }
  133. if (provided_password.buffer.get()) {
  134. memset_s(provided_password.buffer.get(), 0, provided_password.length);
  135. provided_password.buffer.reset();
  136. }
  137. }
  138. uint32_t VerifyRequest::nonErrorSerializedSize() const {
  139. return sizeof(challenge) + serialized_buffer_size(password_handle)
  140. + serialized_buffer_size(provided_password);
  141. }
  142. void VerifyRequest::nonErrorSerialize(uint8_t *buffer) const {
  143. memcpy(buffer, &challenge, sizeof(challenge));
  144. buffer += sizeof(challenge);
  145. append_to_buffer(&buffer, &password_handle);
  146. append_to_buffer(&buffer, &provided_password);
  147. }
  148. gatekeeper_error_t VerifyRequest::nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) {
  149. gatekeeper_error_t error = ERROR_NONE;
  150. if (password_handle.buffer.get()) {
  151. password_handle.buffer.reset();
  152. }
  153. if (provided_password.buffer.get()) {
  154. memset_s(provided_password.buffer.get(), 0, provided_password.length);
  155. provided_password.buffer.reset();
  156. }
  157. if (payload + sizeof(challenge) > end) {
  158. return ERROR_INVALID;
  159. }
  160. memcpy(&challenge, payload, sizeof(challenge));
  161. payload += sizeof(challenge);
  162. error = read_from_buffer(&payload, end, &password_handle);
  163. if (error != ERROR_NONE) return error;
  164. return read_from_buffer(&payload, end, &provided_password);
  165. }
  166. VerifyResponse::VerifyResponse(uint32_t user_id, SizedBuffer *auth_token) {
  167. this->user_id = user_id;
  168. this->auth_token.buffer.reset(auth_token->buffer.release());
  169. this->auth_token.length = auth_token->length;
  170. this->request_reenroll = false;
  171. }
  172. VerifyResponse::VerifyResponse() {
  173. request_reenroll = false;
  174. memset_s(&auth_token, 0, sizeof(auth_token));
  175. };
  176. VerifyResponse::~VerifyResponse() {
  177. if (auth_token.length > 0) {
  178. auth_token.buffer.reset();
  179. }
  180. }
  181. void VerifyResponse::SetVerificationToken(SizedBuffer *auth_token) {
  182. this->auth_token.buffer.reset(auth_token->buffer.release());
  183. this->auth_token.length = auth_token->length;
  184. }
  185. uint32_t VerifyResponse::nonErrorSerializedSize() const {
  186. return serialized_buffer_size(auth_token) + sizeof(request_reenroll);
  187. }
  188. void VerifyResponse::nonErrorSerialize(uint8_t *buffer) const {
  189. append_to_buffer(&buffer, &auth_token);
  190. memcpy(buffer, &request_reenroll, sizeof(request_reenroll));
  191. }
  192. gatekeeper_error_t VerifyResponse::nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) {
  193. if (auth_token.buffer.get()) {
  194. auth_token.buffer.reset();
  195. }
  196. gatekeeper_error_t err = read_from_buffer(&payload, end, &auth_token);
  197. if (err != ERROR_NONE) {
  198. return err;
  199. }
  200. if (payload + sizeof(request_reenroll) > end) {
  201. return ERROR_INVALID;
  202. }
  203. memcpy(&request_reenroll, payload, sizeof(request_reenroll));
  204. return ERROR_NONE;
  205. }
  206. EnrollRequest::EnrollRequest(uint32_t user_id, SizedBuffer *password_handle,
  207. SizedBuffer *provided_password, SizedBuffer *enrolled_password) {
  208. this->user_id = user_id;
  209. this->provided_password.buffer.reset(provided_password->buffer.release());
  210. this->provided_password.length = provided_password->length;
  211. if (enrolled_password == NULL) {
  212. this->enrolled_password.buffer.reset();
  213. this->enrolled_password.length = 0;
  214. } else {
  215. this->enrolled_password.buffer.reset(enrolled_password->buffer.release());
  216. this->enrolled_password.length = enrolled_password->length;
  217. }
  218. if (password_handle == NULL) {
  219. this->password_handle.buffer.reset();
  220. this->password_handle.length = 0;
  221. } else {
  222. this->password_handle.buffer.reset(password_handle->buffer.release());
  223. this->password_handle.length = password_handle->length;
  224. }
  225. }
  226. EnrollRequest::EnrollRequest() {
  227. memset_s(&provided_password, 0, sizeof(provided_password));
  228. memset_s(&enrolled_password, 0, sizeof(enrolled_password));
  229. memset_s(&password_handle, 0, sizeof(password_handle));
  230. }
  231. EnrollRequest::~EnrollRequest() {
  232. if (provided_password.buffer.get()) {
  233. memset_s(provided_password.buffer.get(), 0, provided_password.length);
  234. provided_password.buffer.reset();
  235. }
  236. if (enrolled_password.buffer.get()) {
  237. memset_s(enrolled_password.buffer.get(), 0, enrolled_password.length);
  238. enrolled_password.buffer.reset();
  239. }
  240. if (password_handle.buffer.get()) {
  241. memset_s(password_handle.buffer.get(), 0, password_handle.length);
  242. password_handle.buffer.reset();
  243. }
  244. }
  245. uint32_t EnrollRequest::nonErrorSerializedSize() const {
  246. return serialized_buffer_size(provided_password) + serialized_buffer_size(enrolled_password)
  247. + serialized_buffer_size(password_handle);
  248. }
  249. void EnrollRequest::nonErrorSerialize(uint8_t *buffer) const {
  250. append_to_buffer(&buffer, &provided_password);
  251. append_to_buffer(&buffer, &enrolled_password);
  252. append_to_buffer(&buffer, &password_handle);
  253. }
  254. gatekeeper_error_t EnrollRequest::nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) {
  255. gatekeeper_error_t ret;
  256. if (provided_password.buffer.get()) {
  257. memset_s(provided_password.buffer.get(), 0, provided_password.length);
  258. provided_password.buffer.reset();
  259. }
  260. if (enrolled_password.buffer.get()) {
  261. memset_s(enrolled_password.buffer.get(), 0, enrolled_password.length);
  262. enrolled_password.buffer.reset();
  263. }
  264. if (password_handle.buffer.get()) {
  265. memset_s(password_handle.buffer.get(), 0, password_handle.length);
  266. password_handle.buffer.reset();
  267. }
  268. ret = read_from_buffer(&payload, end, &provided_password);
  269. if (ret != ERROR_NONE) {
  270. return ret;
  271. }
  272. ret = read_from_buffer(&payload, end, &enrolled_password);
  273. if (ret != ERROR_NONE) {
  274. return ret;
  275. }
  276. return read_from_buffer(&payload, end, &password_handle);
  277. }
  278. EnrollResponse::EnrollResponse(uint32_t user_id, SizedBuffer *enrolled_password_handle) {
  279. this->user_id = user_id;
  280. this->enrolled_password_handle.buffer.reset(enrolled_password_handle->buffer.release());
  281. this->enrolled_password_handle.length = enrolled_password_handle->length;
  282. }
  283. EnrollResponse::EnrollResponse() {
  284. memset_s(&enrolled_password_handle, 0, sizeof(enrolled_password_handle));
  285. }
  286. EnrollResponse::~EnrollResponse() {
  287. if (enrolled_password_handle.buffer.get()) {
  288. enrolled_password_handle.buffer.reset();
  289. }
  290. }
  291. void EnrollResponse::SetEnrolledPasswordHandle(SizedBuffer *enrolled_password_handle) {
  292. this->enrolled_password_handle.buffer.reset(enrolled_password_handle->buffer.release());
  293. this->enrolled_password_handle.length = enrolled_password_handle->length;
  294. }
  295. uint32_t EnrollResponse::nonErrorSerializedSize() const {
  296. return serialized_buffer_size(enrolled_password_handle);
  297. }
  298. void EnrollResponse::nonErrorSerialize(uint8_t *buffer) const {
  299. append_to_buffer(&buffer, &enrolled_password_handle);
  300. }
  301. gatekeeper_error_t EnrollResponse::nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) {
  302. if (enrolled_password_handle.buffer.get()) {
  303. enrolled_password_handle.buffer.reset();
  304. }
  305. return read_from_buffer(&payload, end, &enrolled_password_handle);
  306. }
  307. };