/src/ostree/src/libostree/ostree-sign-dummy.c
Line | Count | Source |
1 | | /* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */ |
2 | | |
3 | | /* |
4 | | * Copyright © 2019 Collabora Ltd. |
5 | | * |
6 | | * SPDX-License-Identifier: LGPL-2.0+ |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2 of the License, or (at your option) any later version. |
12 | | * |
13 | | * This library is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with this library. If not, see <https://www.gnu.org/licenses/>. |
20 | | * |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include "ostree-sign-dummy.h" |
26 | | #include <libglnx.h> |
27 | | #include <string.h> |
28 | | |
29 | | #undef G_LOG_DOMAIN |
30 | 0 | #define G_LOG_DOMAIN "OSTreeSign" |
31 | | |
32 | 0 | #define OSTREE_SIGN_DUMMY_NAME "dummy" |
33 | | |
34 | 0 | #define OSTREE_SIGN_METADATA_DUMMY_KEY "ostree.sign.dummy" |
35 | 0 | #define OSTREE_SIGN_METADATA_DUMMY_TYPE "aay" |
36 | | |
37 | | struct _OstreeSignDummy |
38 | | { |
39 | | GObject parent; |
40 | | gchar *sk_ascii; |
41 | | gchar *pk_ascii; |
42 | | }; |
43 | | |
44 | | static void ostree_sign_dummy_iface_init (OstreeSignInterface *self); |
45 | | |
46 | 0 | G_DEFINE_TYPE_WITH_CODE (OstreeSignDummy, _ostree_sign_dummy, G_TYPE_OBJECT, |
47 | 0 | G_IMPLEMENT_INTERFACE (OSTREE_TYPE_SIGN, ostree_sign_dummy_iface_init)); |
48 | 0 |
|
49 | 0 | static gboolean |
50 | 0 | check_dummy_sign_enabled (GError **error) |
51 | 0 | { |
52 | 0 | if (g_strcmp0 (g_getenv ("OSTREE_DUMMY_SIGN_ENABLED"), "1") != 0) |
53 | 0 | return glnx_throw (error, "dummy signature type is only for ostree testing"); |
54 | 0 | return TRUE; |
55 | 0 | } |
56 | | |
57 | | static void |
58 | | ostree_sign_dummy_iface_init (OstreeSignInterface *self) |
59 | 0 | { |
60 | |
|
61 | 0 | self->get_name = ostree_sign_dummy_get_name; |
62 | 0 | self->data = ostree_sign_dummy_data; |
63 | 0 | self->data_verify = ostree_sign_dummy_data_verify; |
64 | 0 | self->metadata_key = ostree_sign_dummy_metadata_key; |
65 | 0 | self->metadata_format = ostree_sign_dummy_metadata_format; |
66 | 0 | self->set_sk = ostree_sign_dummy_set_sk; |
67 | 0 | self->set_pk = ostree_sign_dummy_set_pk; |
68 | | /* Implementation for dummy engine just load the single public key */ |
69 | 0 | self->add_pk = ostree_sign_dummy_set_pk; |
70 | 0 | } |
71 | | |
72 | | static void |
73 | | _ostree_sign_dummy_class_init (OstreeSignDummyClass *self) |
74 | 0 | { |
75 | 0 | } |
76 | | |
77 | | static void |
78 | | _ostree_sign_dummy_init (OstreeSignDummy *self) |
79 | 0 | { |
80 | |
|
81 | 0 | self->sk_ascii = NULL; |
82 | 0 | self->pk_ascii = NULL; |
83 | 0 | } |
84 | | |
85 | | gboolean |
86 | | ostree_sign_dummy_set_sk (OstreeSign *self, GVariant *key, GError **error) |
87 | 0 | { |
88 | 0 | if (!check_dummy_sign_enabled (error)) |
89 | 0 | return FALSE; |
90 | | |
91 | 0 | OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private (OSTREE_SIGN_DUMMY (self)); |
92 | |
|
93 | 0 | g_free (sign->sk_ascii); |
94 | |
|
95 | 0 | sign->sk_ascii = g_variant_dup_string (key, 0); |
96 | |
|
97 | 0 | return TRUE; |
98 | 0 | } |
99 | | |
100 | | gboolean |
101 | | ostree_sign_dummy_set_pk (OstreeSign *self, GVariant *key, GError **error) |
102 | 0 | { |
103 | 0 | OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private (OSTREE_SIGN_DUMMY (self)); |
104 | |
|
105 | 0 | g_free (sign->pk_ascii); |
106 | |
|
107 | 0 | sign->pk_ascii = g_variant_dup_string (key, 0); |
108 | |
|
109 | 0 | return TRUE; |
110 | 0 | } |
111 | | |
112 | | gboolean |
113 | | ostree_sign_dummy_data (OstreeSign *self, GBytes *data, GBytes **signature, |
114 | | GCancellable *cancellable, GError **error) |
115 | 0 | { |
116 | 0 | if (!check_dummy_sign_enabled (error)) |
117 | 0 | return FALSE; |
118 | | |
119 | 0 | g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE); |
120 | | |
121 | 0 | OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private (OSTREE_SIGN_DUMMY (self)); |
122 | |
|
123 | 0 | *signature = g_bytes_new (sign->sk_ascii, strlen (sign->sk_ascii)); |
124 | |
|
125 | 0 | return TRUE; |
126 | 0 | } |
127 | | |
128 | | const gchar * |
129 | | ostree_sign_dummy_get_name (OstreeSign *self) |
130 | 0 | { |
131 | 0 | g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE); |
132 | | |
133 | 0 | return OSTREE_SIGN_DUMMY_NAME; |
134 | 0 | } |
135 | | |
136 | | const gchar * |
137 | | ostree_sign_dummy_metadata_key (OstreeSign *self) |
138 | 0 | { |
139 | |
|
140 | 0 | return OSTREE_SIGN_METADATA_DUMMY_KEY; |
141 | 0 | } |
142 | | |
143 | | const gchar * |
144 | | ostree_sign_dummy_metadata_format (OstreeSign *self) |
145 | 0 | { |
146 | |
|
147 | 0 | return OSTREE_SIGN_METADATA_DUMMY_TYPE; |
148 | 0 | } |
149 | | |
150 | | gboolean |
151 | | ostree_sign_dummy_data_verify (OstreeSign *self, GBytes *data, GVariant *signatures, |
152 | | char **out_success_message, GError **error) |
153 | 0 | { |
154 | 0 | if (!check_dummy_sign_enabled (error)) |
155 | 0 | return FALSE; |
156 | | |
157 | 0 | g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE); |
158 | 0 | g_return_val_if_fail (data != NULL, FALSE); |
159 | | |
160 | 0 | OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private (OSTREE_SIGN_DUMMY (self)); |
161 | |
|
162 | 0 | if (signatures == NULL) |
163 | 0 | return glnx_throw (error, "signature: dummy: commit have no signatures of my type"); |
164 | | |
165 | 0 | if (!g_variant_is_of_type (signatures, (GVariantType *)OSTREE_SIGN_METADATA_DUMMY_TYPE)) |
166 | 0 | return glnx_throw (error, "signature: dummy: wrong type passed for verification"); |
167 | | |
168 | 0 | gsize n = g_variant_n_children (signatures); |
169 | 0 | for (gsize i = 0; i < n; i++) |
170 | 0 | { |
171 | 0 | g_autoptr (GVariant) child = g_variant_get_child_value (signatures, i); |
172 | 0 | g_autoptr (GBytes) signature = g_variant_get_data_as_bytes (child); |
173 | |
|
174 | 0 | gsize sign_size = 0; |
175 | 0 | g_bytes_get_data (signature, &sign_size); |
176 | 0 | g_autofree gchar *sign_ascii = g_strndup (g_bytes_get_data (signature, NULL), sign_size); |
177 | 0 | g_debug ("Read signature %d: %s", (gint)i, sign_ascii); |
178 | 0 | g_debug ("Stored signature %d: %s", (gint)i, sign->pk_ascii); |
179 | |
|
180 | 0 | if (!g_strcmp0 (sign_ascii, sign->pk_ascii)) |
181 | 0 | { |
182 | 0 | if (out_success_message) |
183 | 0 | *out_success_message = g_strdup ("dummy: Signature verified"); |
184 | 0 | return TRUE; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 0 | if (n) |
189 | 0 | return glnx_throw (error, "signature: dummy: incorrect signatures found: %" G_GSIZE_FORMAT, n); |
190 | 0 | return glnx_throw (error, "signature: dummy: no signatures"); |
191 | 0 | } |