aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/gnunet_crypto_dbus_lib_pop.c
blob: a598f1d341e4aff9abc8e080cab0d903902c84f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "config.h"

#include "gnunet_dbus_lib.h"

DBusMessage *
GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key (
    DBusMessage *message,
    DBusMessageIter *iter,
    const char *arg_name,
    struct GNUNET_CRYPTO_EcdsaPublicKey *value)
{
  DBusMessage *ret = NULL;
  DBusMessageIter iter_sub;
  DBusMessageIter iter_sub_sub;

  ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name);
  if (ret)
    return ret;

  const char *encoded;
  int success;
  int arg_type = dbus_message_iter_get_arg_type (&iter_sub);
  int element_type;
  int n_elements;
  unsigned char *marshalled_array;
  switch (arg_type)
  {
  case DBUS_TYPE_STRING: 
    dbus_message_iter_get_basic (&iter_sub, &encoded);
    success = GNUNET_CRYPTO_ecdsa_public_key_from_string (encoded, strlen (encoded), value);
    if (GNUNET_OK != success)
    {
      return dbus_message_new_error_printf (
          message,
          DBUS_ERROR_INVALID_ARGS,
          "String is not a valid base32 encoded ECDSA public key for argument '%s'",
            arg_name);
    }
    GNUNET_DBUS_message_set_pretty (message, true);
    return NULL;
  case DBUS_TYPE_ARRAY:
    element_type = dbus_message_iter_get_element_type (&iter_sub);
    if (DBUS_TYPE_BYTE == element_type)
    {
      dbus_message_iter_recurse (&iter_sub, &iter_sub_sub);
      dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements);
      if (sizeof (value->q_y) == n_elements)
      {
        memcpy (value->q_y, marshalled_array, n_elements);
        return NULL;
      };
      return dbus_message_new_error_printf (
          message,
          DBUS_ERROR_INVALID_ARGS,
          "ECDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.",
            arg_name,
            n_elements);
    };
    return dbus_message_new_error_printf (
        message,
        DBUS_ERROR_INVALID_ARGS,
        "Invalid type for argument '%s'. Variant contains an array of '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits).",
          arg_name,
          GNUNET_DBUS_signature_typecode_to_string (element_type));
  default:
    return dbus_message_new_error_printf (
        message,
        DBUS_ERROR_INVALID_ARGS,
        "Invalid type in variant for argument '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits). Contains '%s'",
          arg_name,
          GNUNET_DBUS_signature_typecode_to_string (arg_type));
  }
}

DBusMessage *
GNUNET_CRYPTO_DBUS_pop_eddsa_public_key (
    DBusMessage *message,
    DBusMessageIter *iter,
    const char *arg_name,
    struct GNUNET_CRYPTO_EddsaPublicKey *value)
{
  DBusMessage *ret = NULL;
  DBusMessageIter iter_sub;
  DBusMessageIter iter_sub_sub;

  ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name);
  if (ret)
    return ret;

  const char *encoded;
  int success;
  int arg_type = dbus_message_iter_get_arg_type (&iter_sub);
  int element_type;
  int n_elements;
  unsigned char *marshalled_array;
  switch (arg_type)
  {
  case DBUS_TYPE_STRING: 
    dbus_message_iter_get_basic (&iter_sub, &encoded);
    success = GNUNET_CRYPTO_eddsa_public_key_from_string (encoded, strlen (encoded), value);
    if (GNUNET_OK != success)
    {
      return dbus_message_new_error_printf (
          message,
          DBUS_ERROR_INVALID_ARGS,
          "String is not a valid base32 encoded EDDSA public key for argument '%s'",
            arg_name);
    }
    GNUNET_DBUS_message_set_pretty (message, true);
    return NULL;
  case DBUS_TYPE_ARRAY:
    element_type = dbus_message_iter_get_element_type (&iter_sub);
    if (DBUS_TYPE_BYTE == element_type)
    {
      dbus_message_iter_recurse (&iter_sub, &iter_sub_sub);
      dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements);
      if (sizeof (value->q_y) == n_elements)
      {
        memcpy (value->q_y, marshalled_array, n_elements);
        return NULL;
      };
      return dbus_message_new_error_printf (
          message,
          DBUS_ERROR_INVALID_ARGS,
          "EDDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.",
            arg_name,
            n_elements);
    };
    return dbus_message_new_error_printf (
        message,
        DBUS_ERROR_INVALID_ARGS,
        "Invalid type for argument '%s'. Variant contains an array of '%s'. Should contain an EDDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits).",
          arg_name,
          GNUNET_DBUS_signature_typecode_to_string (element_type));
  default:
    return dbus_message_new_error_printf (
        message,
        DBUS_ERROR_INVALID_ARGS,
        "Invalid type in variant for argument '%s'. Should contain an ECDSA public key in the form of a base32 encoded string or an array of 32 bytes (256 bits). Contains '%s'",
          arg_name,
          GNUNET_DBUS_signature_typecode_to_string (arg_type));
  }
}