summaryrefslogtreecommitdiffstats
path: root/src/wgtpkg-digsig.c
blob: 4b18a4301850e319fd90f1a9b270b179a2f88985 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 Copyright 2015, 2016 IoT.bzh

 author: José Bollo <jose.bollo@iot.bzh>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/


#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/uri.h>
#include <libxml/xmlsave.h>


#include "verbose.h"
#include "wgtpkg-files.h"
#include "wgtpkg-workdir.h"
#include "wgtpkg-certs.h"
#include "wgtpkg-xmlsec.h"
#include "wgtpkg-digsig.h"



static const char uri_role_author[] = "http://www.w3.org/ns/widgets-digsig#role-author";
static const char uri_role_distributor[] = "http://www.w3.org/ns/widgets-digsig#role-distributor";
static const char uri_profile[] = "http://www.w3.org/ns/widgets-digsig#profile";


/* global data */
static xmlDocPtr document;  /* the document */

/* facility to get the first element node (skip text nodes) starting with 'node' */
static xmlNodePtr next_element(xmlNodePtr node)
{
	while (node && node->type != XML_ELEMENT_NODE)
		node = node->next;
	return node;
}

/* is the 'node' an element node of 'name'? */
static int is_element(xmlNodePtr node, const char *name)
{
	return node->type == XML_ELEMENT_NODE
		&& !strcmp(name, node->name);
}

/* is the 'node' an element node of 'name'? */
static int is_node(xmlNodePtr node, const char *name)
{
	return node != NULL && is_element(node, name);
}

#if 0
/* facility to get the first element node (skip text nodes) starting with 'node' */
static xmlNodePtr next_element_type(xmlNodePtr node, const char *name)
{
	while (node && node->type != XML_ELEMENT_NODE && strcmp(name, node->name))
		node = node->next;
	return node;
}

/* search the element node of id. NOTE : not optimized at all */
static xmlNodePtr search_for(const char *attrname, const char *value)
{
	char *val;
	xmlNodePtr iter, next;
	xmlNodePtr result;

	result = NULL;
	iter = xmlDocGetRootElement(document);
	while (iter != NULL) {
		val = xmlGetProp(iter, attrname);
		if (val != NULL && !strcmp(val, value)) {
			if (result != NULL) {
				ERROR("duplicated %s %s", attrname, value);
				free(val);
				return NULL;
			}
			result = iter;
		}
		free(val);
		next = next_element(iter->children);
		if (next == NULL) {
			/* no child, try sibling */
			next = next_element(iter->next);
			if (next == NULL) {
				iter = iter->parent;
				while (iter != NULL && next == NULL) {
					next = next_element(iter->next);
					iter = iter->parent;
				}
			}
		}
		iter = next;
	}
	if (result == NULL)
		ERROR("node of %s '%s' not found", attrname, value);
	return result;
}

/* search the element node of id. NOTE : not optimized at all */
static xmlNodePtr search_id(const char *id)
{
	return search_for("Id", id);
}
#endif

/* check the digest of one element */
static int check_one_reference(xmlNodePtr ref)
{
	int rc;
	char *uri;
	xmlURIPtr u;
	struct filedesc *fdesc;

	/* start */
	rc = -1;

	/* get the uri */
	uri = xmlGetProp(ref, "URI");
	if (uri == NULL) {
		ERROR("attribute URI of element <Reference> not found");
		goto error;
	}

	/* parse the uri */
	u = xmlParseURI(uri);
	if (!u) {
		ERROR("error while parsing URI %s", uri);
		goto error2;
	}

	/* check that unexpected parts are not there */
	if (u->scheme || u->opaque || u->authority || u->server || u->user || u->query) {
		ERROR("unexpected uri component in %s", uri);
		goto error3;
	}

	/* check path and fragment */
	if (!u->path && !u->fragment) {
		ERROR("invalid uri %s", uri);
		goto error3;
	}
	if (u->path && u->fragment) {
		ERROR("not allowed to sign foreign fragment in %s", uri);
		goto error3;
	}

	if (u->path) {
		/* check that the path is valid */
		fdesc = file_of_name(u->path);
		if (fdesc == NULL) {
			ERROR("reference to unknown file %s", u->path);
			goto error3;
		}
		if (fdesc->type != type_file) {
			ERROR("reference to directory %s", u->path);
			goto error3;
		}
		if ((fdesc->flags & flag_distributor_signature) != 0) {
			ERROR("reference to signature %s", u->path);
			goto error3;
		}
		fdesc->flags |= flag_referenced;
		rc = 0;
	} else {
		rc = 0;
	}

error3:
	xmlFreeURI(u);
error2:
	xmlFree(uri);
error:
	return rc;
}

static int check_references(xmlNodePtr sinfo)
{
	unsigned int i, n, flags;
	struct filedesc *f;
	int result;
	xmlNodePtr elem;

	result = 0;
	elem = sinfo->children;
	while (elem != NULL) {
		if (is_element(elem, "Reference"))
			if (check_one_reference(elem))
				result = -1;
		elem = elem->next;
	}

	n = file_count();
	i = 0;
	while(i < n) {
		f = file_of_index(i++);
		if (f->type == type_file) {
			flags = f->flags;
			if (!(flags & (flag_signature | flag_referenced))) {
				ERROR("file not referenced in signature: %s", f->name);
				result = -1;
			}
		}
	}

	return result;
}


static int get_certificates(xmlNodePtr kinfo)
{
	xmlNodePtr n1, n2;
	char *b;
	int rc;

	n1 = kinfo->children;
	while (n1) {
		if (is_element(n1, "X509Data")) {
			n2 = n1->children;
			while (n2) {
				if (is_element(n2, "X509Certificate")) {
					b = xmlNodeGetContent(n2);
					if (b == NULL) {
						ERROR("xmlNodeGetContent of X509Certificate failed");
						return -1;
					}
					rc = add_certificate_b64(b);
					xmlFree(b);
					if (rc)
						return rc;
				}
				n2 = n2->next;
			}
		}
		n1 = n1->next;
	}
	return 0;
}

/* checks the current document */
static int checkdocument()
{
	int rc;
	xmlNodePtr sinfo, svalue, kinfo, objs, rootsig;

	rc = -1;

	rootsig = xmlDocGetRootElement(document);
	if (!is_node(rootsig, "Signature")) {
		ERROR("root element <Signature> not found");
		goto error;
	}

	sinfo = next_element(rootsig->children);
	if (!is_node(sinfo, "SignedInfo")) {
		ERROR("element <SignedInfo> not found");
		goto error;
	}

	svalue = next_element(sinfo->next);
	if (!is_node(svalue, "SignatureValue")) {
		ERROR("element <SignatureValue> not found");
		goto error;
	}

	kinfo = next_element(svalue->next);
	if (is_node(kinfo, "KeyInfo")) {
		objs = kinfo->next;
	} else {
		objs = kinfo;
		kinfo = NULL;
	}

	rc = check_references(sinfo);
	if (rc)
		goto error;

	rc = xmlsec_verify(rootsig);
	if (rc)
		goto error;

	rc = get_certificates(kinfo);

error:
	return rc;
}

/* verify the digital signature of the file described by 'fdesc' */
int verify_digsig(struct filedesc *fdesc)
{
	int res, fd;

	assert ((fdesc->flags & flag_signature) != 0);
	DEBUG("-- checking file %s",fdesc->name);

	/* reset the flags */
	file_clear_flags();
	clear_certificates();

	/* reads and xml parses the signature file */
	fd = openat(workdirfd, fdesc->name, O_RDONLY);
	if (fd < 0) {
		ERROR("cant't open file %s", fdesc->name);
		return -1;
	}
	document = xmlReadFd(fd, fdesc->name, NULL, 0);
	close(fd);
	if (document == NULL) {
		ERROR("xml parse of file %s failed", fdesc->name);
		return -1;
	}

	res = checkdocument();
	if (res)
		ERROR("previous error was during check of file %s", fdesc->name);

	xmlFreeDoc(document);
	return res;
}

/* check all the signature files */
int check_all_signatures()
{
	int rc, irc;
	unsigned int i, n;
	struct filedesc *fdesc;

	n = signature_count();
	rc = 0;
	for (i = n ; i-- > 0 ; ) {
		fdesc = signature_of_index(i);
		irc = verify_digsig(fdesc);
		if (!irc)
			rc = irc;
	}

	return rc;
}

/* create a signature of 'index' (0 for author, other values for distributors)
using the private 'key' (filename) and the certificates 'certs' (filenames)
as trusted chain */
int create_digsig(unsigned int index, const char *key, const char **certs)
{
	struct filedesc *fdesc;
	xmlDocPtr doc;
	int rc, fd;
	long len;
	xmlSaveCtxtPtr ctx;

	rc = -1;

	/* create the doc */
	doc = xmlsec_create(index, key, certs);
	if (doc == NULL)
		goto error;

	/* instanciate the filename */
	fdesc = create_signature(index);
	if (fdesc == NULL)
		goto error2;

	/* save the doc as file */
	fd = openat(workdirfd, fdesc->name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
	if (fd < 0) {
		ERROR("cant open %s for write", fdesc->name);
		goto error2;
	}
	ctx = xmlSaveToFd(fd, NULL, XML_SAVE_FORMAT);
	if (!ctx) {
		ERROR("xmlSaveToFd failed for %s", fdesc->name);
		goto error3;
	}
	len = xmlSaveDoc(ctx, doc);
	if (len < 0) {
		ERROR("xmlSaveDoc to %s failed", fdesc->name);
		goto error4;
	}

	rc = 0;
error4:
	xmlSaveClose(ctx);
error3:
	close(fd);
error2:
	xmlFreeDoc(doc);
error:
	return rc;
}