diff options
Diffstat (limited to 'src/lib/util/gnunet_crypto_dbus_lib_pop.c')
-rw-r--r-- | src/lib/util/gnunet_crypto_dbus_lib_pop.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/lib/util/gnunet_crypto_dbus_lib_pop.c b/src/lib/util/gnunet_crypto_dbus_lib_pop.c new file mode 100644 index 0000000..a598f1d --- /dev/null +++ b/src/lib/util/gnunet_crypto_dbus_lib_pop.c | |||
@@ -0,0 +1,145 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include "gnunet_dbus_lib.h" | ||
4 | |||
5 | DBusMessage * | ||
6 | GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key ( | ||
7 | DBusMessage *message, | ||
8 | DBusMessageIter *iter, | ||
9 | const char *arg_name, | ||
10 | struct GNUNET_CRYPTO_EcdsaPublicKey *value) | ||
11 | { | ||
12 | DBusMessage *ret = NULL; | ||
13 | DBusMessageIter iter_sub; | ||
14 | DBusMessageIter iter_sub_sub; | ||
15 | |||
16 | ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); | ||
17 | if (ret) | ||
18 | return ret; | ||
19 | |||
20 | const char *encoded; | ||
21 | int success; | ||
22 | int arg_type = dbus_message_iter_get_arg_type (&iter_sub); | ||
23 | int element_type; | ||
24 | int n_elements; | ||
25 | unsigned char *marshalled_array; | ||
26 | switch (arg_type) | ||
27 | { | ||
28 | case DBUS_TYPE_STRING: | ||
29 | dbus_message_iter_get_basic (&iter_sub, &encoded); | ||
30 | success = GNUNET_CRYPTO_ecdsa_public_key_from_string (encoded, strlen (encoded), value); | ||
31 | if (GNUNET_OK != success) | ||
32 | { | ||
33 | return dbus_message_new_error_printf ( | ||
34 | message, | ||
35 | DBUS_ERROR_INVALID_ARGS, | ||
36 | "String is not a valid base32 encoded ECDSA public key for argument '%s'", | ||
37 | arg_name); | ||
38 | } | ||
39 | GNUNET_DBUS_message_set_pretty (message, true); | ||
40 | return NULL; | ||
41 | case DBUS_TYPE_ARRAY: | ||
42 | element_type = dbus_message_iter_get_element_type (&iter_sub); | ||
43 | if (DBUS_TYPE_BYTE == element_type) | ||
44 | { | ||
45 | dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); | ||
46 | dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements); | ||
47 | if (sizeof (value->q_y) == n_elements) | ||
48 | { | ||
49 | memcpy (value->q_y, marshalled_array, n_elements); | ||
50 | return NULL; | ||
51 | }; | ||
52 | return dbus_message_new_error_printf ( | ||
53 | message, | ||
54 | DBUS_ERROR_INVALID_ARGS, | ||
55 | "ECDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.", | ||
56 | arg_name, | ||
57 | n_elements); | ||
58 | }; | ||
59 | return dbus_message_new_error_printf ( | ||
60 | message, | ||
61 | DBUS_ERROR_INVALID_ARGS, | ||
62 | "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).", | ||
63 | arg_name, | ||
64 | GNUNET_DBUS_signature_typecode_to_string (element_type)); | ||
65 | default: | ||
66 | return dbus_message_new_error_printf ( | ||
67 | message, | ||
68 | DBUS_ERROR_INVALID_ARGS, | ||
69 | "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'", | ||
70 | arg_name, | ||
71 | GNUNET_DBUS_signature_typecode_to_string (arg_type)); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | DBusMessage * | ||
76 | GNUNET_CRYPTO_DBUS_pop_eddsa_public_key ( | ||
77 | DBusMessage *message, | ||
78 | DBusMessageIter *iter, | ||
79 | const char *arg_name, | ||
80 | struct GNUNET_CRYPTO_EddsaPublicKey *value) | ||
81 | { | ||
82 | DBusMessage *ret = NULL; | ||
83 | DBusMessageIter iter_sub; | ||
84 | DBusMessageIter iter_sub_sub; | ||
85 | |||
86 | ret = GNUNET_DBUS_pop_enter_variant (message, iter, &iter_sub, arg_name); | ||
87 | if (ret) | ||
88 | return ret; | ||
89 | |||
90 | const char *encoded; | ||
91 | int success; | ||
92 | int arg_type = dbus_message_iter_get_arg_type (&iter_sub); | ||
93 | int element_type; | ||
94 | int n_elements; | ||
95 | unsigned char *marshalled_array; | ||
96 | switch (arg_type) | ||
97 | { | ||
98 | case DBUS_TYPE_STRING: | ||
99 | dbus_message_iter_get_basic (&iter_sub, &encoded); | ||
100 | success = GNUNET_CRYPTO_eddsa_public_key_from_string (encoded, strlen (encoded), value); | ||
101 | if (GNUNET_OK != success) | ||
102 | { | ||
103 | return dbus_message_new_error_printf ( | ||
104 | message, | ||
105 | DBUS_ERROR_INVALID_ARGS, | ||
106 | "String is not a valid base32 encoded EDDSA public key for argument '%s'", | ||
107 | arg_name); | ||
108 | } | ||
109 | GNUNET_DBUS_message_set_pretty (message, true); | ||
110 | return NULL; | ||
111 | case DBUS_TYPE_ARRAY: | ||
112 | element_type = dbus_message_iter_get_element_type (&iter_sub); | ||
113 | if (DBUS_TYPE_BYTE == element_type) | ||
114 | { | ||
115 | dbus_message_iter_recurse (&iter_sub, &iter_sub_sub); | ||
116 | dbus_message_iter_get_fixed_array (&iter_sub_sub, &marshalled_array, &n_elements); | ||
117 | if (sizeof (value->q_y) == n_elements) | ||
118 | { | ||
119 | memcpy (value->q_y, marshalled_array, n_elements); | ||
120 | return NULL; | ||
121 | }; | ||
122 | return dbus_message_new_error_printf ( | ||
123 | message, | ||
124 | DBUS_ERROR_INVALID_ARGS, | ||
125 | "EDDSA public key consists of 32 bytes (256 bits). Array given for argument '%s' contains %d bytes.", | ||
126 | arg_name, | ||
127 | n_elements); | ||
128 | }; | ||
129 | return dbus_message_new_error_printf ( | ||
130 | message, | ||
131 | DBUS_ERROR_INVALID_ARGS, | ||
132 | "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).", | ||
133 | arg_name, | ||
134 | GNUNET_DBUS_signature_typecode_to_string (element_type)); | ||
135 | default: | ||
136 | return dbus_message_new_error_printf ( | ||
137 | message, | ||
138 | DBUS_ERROR_INVALID_ARGS, | ||
139 | "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'", | ||
140 | arg_name, | ||
141 | GNUNET_DBUS_signature_typecode_to_string (arg_type)); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | |||