iridium-remote.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* iridium-remote.c
  2. *
  3. * Copyright 2018 Matthias Vogelgesang
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <json-glib/json-glib.h>
  19. #include <libsoup/soup.h>
  20. #include "iridium-remote.h"
  21. struct _IridiumRemote {
  22. GObject parent_instance;
  23. SoupSession *session;
  24. SoupURI *base_uri;
  25. gchar *password;
  26. IridiumAuthParams params;
  27. };
  28. G_DEFINE_TYPE (IridiumRemote, iridium_remote, G_TYPE_OBJECT)
  29. IridiumRemote *
  30. iridium_remote_new (void)
  31. {
  32. return g_object_new (IRIDIUM_TYPE_REMOTE, NULL);
  33. }
  34. static void
  35. on_auth_params_response_parsed (GObject *object,
  36. GAsyncResult *result,
  37. gpointer user_data)
  38. {
  39. GTask *task;
  40. IridiumRemote *self;
  41. JsonParser *parser;
  42. JsonObject *root;
  43. const gchar *version;
  44. GError *error = NULL;
  45. task = user_data;
  46. self = g_task_get_task_data (task);
  47. parser = JSON_PARSER (object);
  48. if (!json_parser_load_from_stream_finish (parser, result, &error)) {
  49. g_task_return_error (task, error);
  50. return;
  51. }
  52. root = json_node_get_object (json_parser_get_root (parser));
  53. self->params.func = IRIDIUM_CRYPTO_SF_FUNC_PBKDF2;
  54. self->params.hash = IRIDIUM_CRYPTO_SF_HASH_SHA512;
  55. self->params.cost = json_object_get_int_member (root, "pw_cost");
  56. self->params.key_size = json_object_get_int_member (root, "pw_key_size");
  57. self->params.salt = g_strdup (json_object_get_string_member (root, "pw_salt"));
  58. if (g_strcmp0 (json_object_get_string_member (root, "pw_alg"), "sha512")) {
  59. g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  60. "Hash algorithm other than sha512 is not supported");
  61. g_task_return_error (task, error);
  62. return;
  63. }
  64. if (g_strcmp0 (json_object_get_string_member (root, "pw_func"), "pbkdf2")) {
  65. g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  66. "Password derivative function other than PBKDF2 is not supported");
  67. g_task_return_error (task, error);
  68. return;
  69. }
  70. version = json_object_get_string_member (root, "version");
  71. if (!g_strcmp0 (version, "001"))
  72. self->params.version = IRIDIUM_CRYPTO_SF_VERSION_001;
  73. else if (!g_strcmp0 (version, "002"))
  74. self->params.version = IRIDIUM_CRYPTO_SF_VERSION_002;
  75. else {
  76. g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
  77. "StandardFile protocols other than 001 and 002 are not supported");
  78. g_task_return_error (task, error);
  79. return;
  80. }
  81. iridium_crypto_derive_keys (&self->params, self->password);
  82. g_debug ("StandardFile parameters: version=%i func=%i hash=%i key_size=%u iterations=%u",
  83. self->params.version,
  84. self->params.func,
  85. self->params.hash,
  86. self->params.key_size,
  87. self->params.cost);
  88. g_object_unref (parser);
  89. }
  90. static void
  91. on_send_auth_params_message (GObject *object,
  92. GAsyncResult *result,
  93. gpointer user_data)
  94. {
  95. GInputStream *stream;
  96. GTask *task;
  97. JsonParser *parser;
  98. GError *error = NULL;
  99. task = user_data;
  100. stream = soup_request_send_finish (SOUP_REQUEST (object), result, &error);
  101. if (stream == NULL) {
  102. g_task_return_error (task, error);
  103. return;
  104. }
  105. parser = json_parser_new ();
  106. json_parser_load_from_stream_async (parser, stream, g_task_get_cancellable (task),
  107. on_auth_params_response_parsed, task);
  108. /* g_object_unref (object); */
  109. g_object_unref (stream);
  110. }
  111. void
  112. iridium_remote_get_auth_params_async (IridiumRemote *remote,
  113. const gchar *server,
  114. const gchar *email,
  115. const gchar *password,
  116. GCancellable *cancellable,
  117. GAsyncReadyCallback callback,
  118. gpointer user_data)
  119. {
  120. SoupURI *uri;
  121. SoupRequestHTTP *request;
  122. GTask *task;
  123. GError *error = NULL;
  124. if (remote->base_uri)
  125. soup_uri_free (remote->base_uri);
  126. remote->base_uri = soup_uri_new (server);
  127. remote->password = g_strdup (password);
  128. uri = soup_uri_new_with_base (remote->base_uri, "api/auth/params");
  129. soup_uri_set_query_from_fields (uri, "email", email, NULL);
  130. task = g_task_new (remote, cancellable, callback, user_data);
  131. /* TODO: what happens to request? */
  132. request = soup_session_request_http_uri (remote->session, "GET", uri, &error);
  133. if (request == NULL) {
  134. g_task_return_error (task, error);
  135. return;
  136. }
  137. g_task_set_task_data (task, remote, NULL);
  138. soup_request_send_async (SOUP_REQUEST (request), cancellable, on_send_auth_params_message, task);
  139. soup_uri_free (uri);
  140. }
  141. const IridiumAuthParams *
  142. iridium_remote_get_auth_params_finish (IridiumRemote *remote,
  143. GAsyncResult *result,
  144. GError **error)
  145. {
  146. g_return_val_if_fail (g_task_is_valid (result, remote), NULL);
  147. return &remote->params;
  148. }
  149. static void
  150. iridium_remote_dispose (GObject *object)
  151. {
  152. IridiumRemote *self;
  153. self = IRIDIUM_REMOTE (object);
  154. g_object_unref (self->session);
  155. if (self->base_uri)
  156. soup_uri_free (self->base_uri);
  157. G_OBJECT_CLASS (iridium_remote_parent_class)->dispose (object);
  158. }
  159. static void
  160. iridium_remote_class_init (IridiumRemoteClass *klass)
  161. {
  162. GObjectClass *oclass;
  163. oclass = G_OBJECT_CLASS (klass);
  164. oclass->dispose = iridium_remote_dispose;
  165. }
  166. static void
  167. iridium_remote_init (IridiumRemote *self)
  168. {
  169. self->base_uri = NULL;
  170. self->session = soup_session_new ();
  171. }