File: sigs.c

package info (click to toggle)
got 0.119-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,448 kB
  • sloc: ansic: 124,378; sh: 50,814; yacc: 4,353; makefile: 2,241; perl: 357
file content (403 lines) | stat: -rw-r--r-- 9,497 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2022 Josh Rickmar <jrick@zettaport.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "got_compat.h"

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/wait.h>

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <assert.h>

#include "got_error.h"
#include "got_date.h"
#include "got_object.h"
#include "got_opentemp.h"

#include "got_sigs.h"
#include "buf.h"

#ifndef MIN
#define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif

#ifndef nitems
#define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
#endif

#ifndef GOT_TAG_PATH_SSH_KEYGEN
#define GOT_TAG_PATH_SSH_KEYGEN	"/usr/bin/ssh-keygen"
#endif

#ifndef GOT_TAG_PATH_SIGNIFY
#define GOT_TAG_PATH_SIGNIFY "/usr/bin/signify"
#endif

const struct got_error *
got_sigs_apply_unveil(void)
{
	if (unveil(GOT_TAG_PATH_SSH_KEYGEN, "x") != 0) {
		return got_error_from_errno2("unveil",
		    GOT_TAG_PATH_SSH_KEYGEN);
	}
	if (unveil(GOT_TAG_PATH_SIGNIFY, "x") != 0) {
		return got_error_from_errno2("unveil",
		    GOT_TAG_PATH_SIGNIFY);
	}

	return NULL;
}

const struct got_error *
got_sigs_sign_tag_ssh(pid_t *newpid, int *in_fd, int *out_fd,
    const char* key_file, int verbosity)
{
	const struct got_error *error = NULL;
	int pid, in_pfd[2], out_pfd[2];
	const char* argv[11];
	int i = 0, j;

	*newpid = -1;
	*in_fd = -1;
	*out_fd = -1;

	argv[i++] = GOT_TAG_PATH_SSH_KEYGEN;
	argv[i++] = "-Y";
	argv[i++] = "sign";
	argv[i++] = "-f";
	argv[i++] = key_file;
	argv[i++] = "-n";
	argv[i++] = "git";
	if (verbosity <= 0) {
		argv[i++] = "-q";
	} else {
		/* ssh(1) allows up to 3 "-v" options. */
		for (j = 0; j < MIN(3, verbosity); j++)
			argv[i++] = "-v";
	}
	argv[i++] = NULL;
	assert(i <= nitems(argv));

	if (pipe(in_pfd) == -1)
		return got_error_from_errno("pipe");
	if (pipe(out_pfd) == -1)
		return got_error_from_errno("pipe");

	pid = fork();
	if (pid == -1) {
		error = got_error_from_errno("fork");
		close(in_pfd[0]);
		close(in_pfd[1]);
		close(out_pfd[0]);
		close(out_pfd[1]);
		return error;
	} else if (pid == 0) {
		if (close(in_pfd[1]) == -1)
			err(1, "close");
		if (close(out_pfd[0]) == -1)
			err(1, "close");
		if (dup2(in_pfd[0], 0) == -1)
			err(1, "dup2");
		if (dup2(out_pfd[1], 1) == -1)
			err(1, "dup2");
		if (execv(GOT_TAG_PATH_SSH_KEYGEN, (char **const)argv) == -1)
			err(1, "execv %s", GOT_TAG_PATH_SSH_KEYGEN);
		abort(); /* not reached */
	}
	if (close(in_pfd[0]) == -1)
		return got_error_from_errno("close");
	if (close(out_pfd[1]) == -1)
		return got_error_from_errno("close");
	*newpid = pid;
	*in_fd = in_pfd[1];
	*out_fd = out_pfd[0];
	return NULL;
}

static char *
signer_identity(const char *tagger)
{
	char *lt, *gt;

	lt = strstr(tagger, " <");
	gt = strrchr(tagger, '>');
	if (lt && gt && lt+1 < gt)
		return strndup(lt+2, gt-lt-2);
	return NULL;
}

static const char* BEGIN_SSH_SIG = "-----BEGIN SSH SIGNATURE-----\n";
static const char* END_SSH_SIG = "-----END SSH SIGNATURE-----\n";

const char *
got_sigs_get_tagmsg_ssh_signature(const char *tagmsg)
{
	const char *s = tagmsg, *begin = NULL, *end = NULL;

	while ((s = strstr(s, BEGIN_SSH_SIG)) != NULL) {
		begin = s;
		s += strlen(BEGIN_SSH_SIG);
	}
	if (begin)
		end = strstr(begin+strlen(BEGIN_SSH_SIG), END_SSH_SIG);
	if (end == NULL)
		return NULL;
	return (end[strlen(END_SSH_SIG)] == '\0') ? begin : NULL;
}

static const struct got_error *
got_tag_write_signed_data(BUF *buf, struct got_tag_object *tag,
    const char *start_sig)
{
	const struct got_error *err = NULL;
	struct got_object_id *id;
	char *id_str = NULL;
	char *tagger = NULL;
	const char *tagmsg;
	char gmtoff[6];
	size_t len;

	id = got_object_tag_get_object_id(tag);
	err = got_object_id_str(&id_str, id);
	if (err)
		goto done;

	const char *type_label = NULL;
	switch (got_object_tag_get_object_type(tag)) {
	case GOT_OBJ_TYPE_BLOB:
		type_label = GOT_OBJ_LABEL_BLOB;
		break;
	case GOT_OBJ_TYPE_TREE:
		type_label = GOT_OBJ_LABEL_TREE;
		break;
	case GOT_OBJ_TYPE_COMMIT:
		type_label = GOT_OBJ_LABEL_COMMIT;
		break;
	case GOT_OBJ_TYPE_TAG:
		type_label = GOT_OBJ_LABEL_TAG;
		break;
	default:
		break;
	}
	got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
	    got_object_tag_get_tagger_gmtoff(tag));
	if (asprintf(&tagger, "%s %lld %s", got_object_tag_get_tagger(tag),
	    (long long)got_object_tag_get_tagger_time(tag), gmtoff) == -1) {
		err = got_error_from_errno("asprintf");
		goto done;
	}

	err = buf_puts(&len, buf, GOT_TAG_LABEL_OBJECT);
	if (err)
		goto done;
	err = buf_puts(&len, buf, id_str);
	if (err)
		goto done;
	err = buf_putc(buf, '\n');
	if (err)
		goto done;
	err = buf_puts(&len, buf, GOT_TAG_LABEL_TYPE);
	if (err)
		goto done;
	err = buf_puts(&len, buf, type_label);
	if (err)
		goto done;
	err = buf_putc(buf, '\n');
	if (err)
		goto done;
	err = buf_puts(&len, buf, GOT_TAG_LABEL_TAG);
	if (err)
		goto done;
	err = buf_puts(&len, buf, got_object_tag_get_name(tag));
	if (err)
		goto done;
	err = buf_putc(buf, '\n');
	if (err)
		goto done;
	err = buf_puts(&len, buf, GOT_TAG_LABEL_TAGGER);
	if (err)
		goto done;
	err = buf_puts(&len, buf, tagger);
	if (err)
		goto done;
	err = buf_puts(&len, buf, "\n");
	if (err)
		goto done;
	tagmsg = got_object_tag_get_message(tag);
	err = buf_append(&len, buf, tagmsg, start_sig-tagmsg);
	if (err)
		goto done;

done:
	free(id_str);
	free(tagger);
	return err;
}

const struct got_error *
got_sigs_verify_tag_ssh(char **msg, struct got_tag_object *tag,
    const char *start_sig, const char* allowed_signers, const char* revoked,
    int verbosity)
{
	const struct got_error *error = NULL;
	const char* argv[17];
	int pid, status, in_pfd[2], out_pfd[2];
	char* parsed_identity = NULL;
	const char *identity;
	char *tmppath = NULL;
	FILE *tmpsig = NULL;
	BUF *buf;
	int i = 0, j;

	*msg = NULL;

	error = got_opentemp_named(&tmppath, &tmpsig,
	    GOT_TMPDIR_STR "/got-tagsig", "");
	if (error)
		goto done;

	identity = got_object_tag_get_tagger(tag);
	parsed_identity = signer_identity(identity);
	if (parsed_identity != NULL)
		identity = parsed_identity;

	if (fputs(start_sig, tmpsig) == EOF) {
		error = got_error_from_errno("fputs");
		goto done;
	}
	if (fflush(tmpsig) == EOF) {
		error = got_error_from_errno("fflush");
		goto done;
	}

	error = buf_alloc(&buf, 0);
	if (error)
		goto done;
	error = got_tag_write_signed_data(buf, tag, start_sig);
	if (error)
		goto done;

	argv[i++] = GOT_TAG_PATH_SSH_KEYGEN;
	argv[i++] = "-Y";
	argv[i++] = "verify";
	argv[i++] = "-f";
	argv[i++] = allowed_signers;
	argv[i++] = "-I";
	argv[i++] = identity;
	argv[i++] = "-n";
	argv[i++] = "git";
	argv[i++] = "-s";
	argv[i++] = tmppath;
	if (revoked) {
		argv[i++] = "-r";
		argv[i++] = revoked;
	}
	if (verbosity > 0) {
		/* ssh(1) allows up to 3 "-v" options. */
		for (j = 0; j < MIN(3, verbosity); j++)
			argv[i++] = "-v";
	}
	argv[i++] = NULL;
	assert(i <= nitems(argv));

	if (pipe(in_pfd) == -1) {
		error = got_error_from_errno("pipe");
		goto done;
	}
	if (pipe(out_pfd) == -1) {
		error = got_error_from_errno("pipe");
		goto done;
	}

	pid = fork();
	if (pid == -1) {
		error = got_error_from_errno("fork");
		close(in_pfd[0]);
		close(in_pfd[1]);
		close(out_pfd[0]);
		close(out_pfd[1]);
		return error;
	} else if (pid == 0) {
		if (close(in_pfd[1]) == -1)
			err(1, "close");
		if (close(out_pfd[0]) == -1)
			err(1, "close");
		if (dup2(in_pfd[0], 0) == -1)
			err(1, "dup2");
		if (dup2(out_pfd[1], 1) == -1)
			err(1, "dup2");
		if (execv(GOT_TAG_PATH_SSH_KEYGEN, (char **const)argv) == -1)
			err(1, "execv %s", GOT_TAG_PATH_SSH_KEYGEN);
		abort(); /* not reached */
	}
	if (close(in_pfd[0]) == -1) {
		error = got_error_from_errno("close");
		goto done;
	}
	if (close(out_pfd[1]) == -1) {
		error = got_error_from_errno("close");
		goto done;
	}
	if (buf_write_fd(buf, in_pfd[1]) == -1) {
		error = got_error_from_errno("write");
		goto done;
	}
	if (close(in_pfd[1]) == -1) {
		error = got_error_from_errno("close");
		goto done;
	}
	if (waitpid(pid, &status, 0) == -1) {
		error = got_error_from_errno("waitpid");
		goto done;
	}
	if (!WIFEXITED(status)) {
		error = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
		goto done;
	}

	error = buf_load_fd(&buf, out_pfd[0]);
	if (error)
		goto done;
	error = buf_putc(buf, '\0');
	if (error)
		goto done;
	if (close(out_pfd[0]) == -1) {
		error = got_error_from_errno("close");
		goto done;
	}
	*msg = buf_get(buf);
	if (WEXITSTATUS(status) != 0)
		error = got_error(GOT_ERR_BAD_TAG_SIGNATURE);

done:
	free(parsed_identity);
	if (tmppath && unlink(tmppath) == -1 && error == NULL)
		error = got_error_from_errno("unlink");
	free(tmppath);
	close(out_pfd[0]);
	if (tmpsig && fclose(tmpsig) == EOF && error == NULL)
		error = got_error_from_errno("fclose");
	return error;
}