Blame


1 871bd038 2022-07-03 thomas /*
2 871bd038 2022-07-03 thomas * Copyright (c) 2022 Josh Rickmar <jrick@zettaport.com>
3 871bd038 2022-07-03 thomas *
4 871bd038 2022-07-03 thomas * Permission to use, copy, modify, and distribute this software for any
5 871bd038 2022-07-03 thomas * purpose with or without fee is hereby granted, provided that the above
6 871bd038 2022-07-03 thomas * copyright notice and this permission notice appear in all copies.
7 871bd038 2022-07-03 thomas *
8 871bd038 2022-07-03 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 871bd038 2022-07-03 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 871bd038 2022-07-03 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 871bd038 2022-07-03 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 871bd038 2022-07-03 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 871bd038 2022-07-03 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 871bd038 2022-07-03 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 871bd038 2022-07-03 thomas */
16 871bd038 2022-07-03 thomas
17 d06b3506 2022-07-04 thomas #include <sys/time.h>
18 871bd038 2022-07-03 thomas #include <sys/types.h>
19 871bd038 2022-07-03 thomas #include <sys/stat.h>
20 871bd038 2022-07-03 thomas #include <sys/socket.h>
21 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
22 871bd038 2022-07-03 thomas #include <sys/wait.h>
23 871bd038 2022-07-03 thomas
24 871bd038 2022-07-03 thomas #include <stdlib.h>
25 871bd038 2022-07-03 thomas #include <stdio.h>
26 871bd038 2022-07-03 thomas #include <fcntl.h>
27 871bd038 2022-07-03 thomas #include <unistd.h>
28 871bd038 2022-07-03 thomas #include <string.h>
29 871bd038 2022-07-03 thomas #include <err.h>
30 871bd038 2022-07-03 thomas #include <assert.h>
31 871bd038 2022-07-03 thomas
32 871bd038 2022-07-03 thomas #include "got_error.h"
33 871bd038 2022-07-03 thomas #include "got_date.h"
34 871bd038 2022-07-03 thomas #include "got_object.h"
35 871bd038 2022-07-03 thomas #include "got_opentemp.h"
36 871bd038 2022-07-03 thomas
37 871bd038 2022-07-03 thomas #include "got_sigs.h"
38 eb427b1c 2022-07-03 thomas #include "got_compat.h"
39 871bd038 2022-07-03 thomas #include "buf.h"
40 871bd038 2022-07-03 thomas
41 871bd038 2022-07-03 thomas #ifndef MIN
42 871bd038 2022-07-03 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 871bd038 2022-07-03 thomas #endif
44 871bd038 2022-07-03 thomas
45 871bd038 2022-07-03 thomas #ifndef nitems
46 871bd038 2022-07-03 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 871bd038 2022-07-03 thomas #endif
48 871bd038 2022-07-03 thomas
49 871bd038 2022-07-03 thomas #ifndef GOT_TAG_PATH_SSH_KEYGEN
50 871bd038 2022-07-03 thomas #define GOT_TAG_PATH_SSH_KEYGEN "/usr/bin/ssh-keygen"
51 871bd038 2022-07-03 thomas #endif
52 871bd038 2022-07-03 thomas
53 871bd038 2022-07-03 thomas #ifndef GOT_TAG_PATH_SIGNIFY
54 871bd038 2022-07-03 thomas #define GOT_TAG_PATH_SIGNIFY "/usr/bin/signify"
55 871bd038 2022-07-03 thomas #endif
56 871bd038 2022-07-03 thomas
57 871bd038 2022-07-03 thomas const struct got_error *
58 7a61761f 2022-08-12 thomas got_sigs_apply_unveil(void)
59 871bd038 2022-07-03 thomas {
60 871bd038 2022-07-03 thomas if (unveil(GOT_TAG_PATH_SSH_KEYGEN, "x") != 0) {
61 871bd038 2022-07-03 thomas return got_error_from_errno2("unveil",
62 871bd038 2022-07-03 thomas GOT_TAG_PATH_SSH_KEYGEN);
63 871bd038 2022-07-03 thomas }
64 871bd038 2022-07-03 thomas if (unveil(GOT_TAG_PATH_SIGNIFY, "x") != 0) {
65 871bd038 2022-07-03 thomas return got_error_from_errno2("unveil",
66 871bd038 2022-07-03 thomas GOT_TAG_PATH_SIGNIFY);
67 871bd038 2022-07-03 thomas }
68 871bd038 2022-07-03 thomas
69 871bd038 2022-07-03 thomas return NULL;
70 871bd038 2022-07-03 thomas }
71 871bd038 2022-07-03 thomas
72 871bd038 2022-07-03 thomas const struct got_error *
73 871bd038 2022-07-03 thomas got_sigs_sign_tag_ssh(pid_t *newpid, int *in_fd, int *out_fd,
74 871bd038 2022-07-03 thomas const char* key_file, int verbosity)
75 871bd038 2022-07-03 thomas {
76 871bd038 2022-07-03 thomas const struct got_error *error = NULL;
77 871bd038 2022-07-03 thomas int pid, in_pfd[2], out_pfd[2];
78 871bd038 2022-07-03 thomas const char* argv[11];
79 871bd038 2022-07-03 thomas int i = 0, j;
80 871bd038 2022-07-03 thomas
81 871bd038 2022-07-03 thomas *newpid = -1;
82 871bd038 2022-07-03 thomas *in_fd = -1;
83 871bd038 2022-07-03 thomas *out_fd = -1;
84 871bd038 2022-07-03 thomas
85 871bd038 2022-07-03 thomas argv[i++] = GOT_TAG_PATH_SSH_KEYGEN;
86 871bd038 2022-07-03 thomas argv[i++] = "-Y";
87 871bd038 2022-07-03 thomas argv[i++] = "sign";
88 871bd038 2022-07-03 thomas argv[i++] = "-f";
89 871bd038 2022-07-03 thomas argv[i++] = key_file;
90 871bd038 2022-07-03 thomas argv[i++] = "-n";
91 871bd038 2022-07-03 thomas argv[i++] = "git";
92 871bd038 2022-07-03 thomas if (verbosity <= 0) {
93 871bd038 2022-07-03 thomas argv[i++] = "-q";
94 871bd038 2022-07-03 thomas } else {
95 871bd038 2022-07-03 thomas /* ssh(1) allows up to 3 "-v" options. */
96 871bd038 2022-07-03 thomas for (j = 0; j < MIN(3, verbosity); j++)
97 871bd038 2022-07-03 thomas argv[i++] = "-v";
98 871bd038 2022-07-03 thomas }
99 871bd038 2022-07-03 thomas argv[i++] = NULL;
100 871bd038 2022-07-03 thomas assert(i <= nitems(argv));
101 871bd038 2022-07-03 thomas
102 c4d68ce0 2022-07-03 thomas if (pipe(in_pfd) == -1)
103 c4d68ce0 2022-07-03 thomas return got_error_from_errno("pipe");
104 c4d68ce0 2022-07-03 thomas if (pipe(out_pfd) == -1)
105 c4d68ce0 2022-07-03 thomas return got_error_from_errno("pipe");
106 871bd038 2022-07-03 thomas
107 871bd038 2022-07-03 thomas pid = fork();
108 871bd038 2022-07-03 thomas if (pid == -1) {
109 871bd038 2022-07-03 thomas error = got_error_from_errno("fork");
110 871bd038 2022-07-03 thomas close(in_pfd[0]);
111 871bd038 2022-07-03 thomas close(in_pfd[1]);
112 871bd038 2022-07-03 thomas close(out_pfd[0]);
113 871bd038 2022-07-03 thomas close(out_pfd[1]);
114 871bd038 2022-07-03 thomas return error;
115 871bd038 2022-07-03 thomas } else if (pid == 0) {
116 871bd038 2022-07-03 thomas if (close(in_pfd[1]) == -1)
117 871bd038 2022-07-03 thomas err(1, "close");
118 3cb93379 2022-07-12 thomas if (close(out_pfd[0]) == -1)
119 871bd038 2022-07-03 thomas err(1, "close");
120 871bd038 2022-07-03 thomas if (dup2(in_pfd[0], 0) == -1)
121 871bd038 2022-07-03 thomas err(1, "dup2");
122 3cb93379 2022-07-12 thomas if (dup2(out_pfd[1], 1) == -1)
123 871bd038 2022-07-03 thomas err(1, "dup2");
124 871bd038 2022-07-03 thomas if (execv(GOT_TAG_PATH_SSH_KEYGEN, (char **const)argv) == -1)
125 871bd038 2022-07-03 thomas err(1, "execv");
126 871bd038 2022-07-03 thomas abort(); /* not reached */
127 871bd038 2022-07-03 thomas }
128 871bd038 2022-07-03 thomas if (close(in_pfd[0]) == -1)
129 871bd038 2022-07-03 thomas return got_error_from_errno("close");
130 3cb93379 2022-07-12 thomas if (close(out_pfd[1]) == -1)
131 871bd038 2022-07-03 thomas return got_error_from_errno("close");
132 871bd038 2022-07-03 thomas *newpid = pid;
133 871bd038 2022-07-03 thomas *in_fd = in_pfd[1];
134 3cb93379 2022-07-12 thomas *out_fd = out_pfd[0];
135 871bd038 2022-07-03 thomas return NULL;
136 871bd038 2022-07-03 thomas }
137 871bd038 2022-07-03 thomas
138 871bd038 2022-07-03 thomas static char *
139 871bd038 2022-07-03 thomas signer_identity(const char *tagger)
140 871bd038 2022-07-03 thomas {
141 871bd038 2022-07-03 thomas char *lt, *gt;
142 871bd038 2022-07-03 thomas
143 871bd038 2022-07-03 thomas lt = strstr(tagger, " <");
144 871bd038 2022-07-03 thomas gt = strrchr(tagger, '>');
145 871bd038 2022-07-03 thomas if (lt && gt && lt+1 < gt)
146 871bd038 2022-07-03 thomas return strndup(lt+2, gt-lt-2);
147 871bd038 2022-07-03 thomas return NULL;
148 871bd038 2022-07-03 thomas }
149 871bd038 2022-07-03 thomas
150 871bd038 2022-07-03 thomas static const char* BEGIN_SSH_SIG = "-----BEGIN SSH SIGNATURE-----\n";
151 871bd038 2022-07-03 thomas static const char* END_SSH_SIG = "-----END SSH SIGNATURE-----\n";
152 871bd038 2022-07-03 thomas
153 871bd038 2022-07-03 thomas const char *
154 871bd038 2022-07-03 thomas got_sigs_get_tagmsg_ssh_signature(const char *tagmsg)
155 871bd038 2022-07-03 thomas {
156 871bd038 2022-07-03 thomas const char *s = tagmsg, *begin = NULL, *end = NULL;
157 871bd038 2022-07-03 thomas
158 871bd038 2022-07-03 thomas while ((s = strstr(s, BEGIN_SSH_SIG)) != NULL) {
159 871bd038 2022-07-03 thomas begin = s;
160 871bd038 2022-07-03 thomas s += strlen(BEGIN_SSH_SIG);
161 871bd038 2022-07-03 thomas }
162 871bd038 2022-07-03 thomas if (begin)
163 871bd038 2022-07-03 thomas end = strstr(begin+strlen(BEGIN_SSH_SIG), END_SSH_SIG);
164 871bd038 2022-07-03 thomas if (end == NULL)
165 871bd038 2022-07-03 thomas return NULL;
166 871bd038 2022-07-03 thomas return (end[strlen(END_SSH_SIG)] == '\0') ? begin : NULL;
167 871bd038 2022-07-03 thomas }
168 871bd038 2022-07-03 thomas
169 871bd038 2022-07-03 thomas static const struct got_error *
170 871bd038 2022-07-03 thomas got_tag_write_signed_data(BUF *buf, struct got_tag_object *tag,
171 871bd038 2022-07-03 thomas const char *start_sig)
172 871bd038 2022-07-03 thomas {
173 871bd038 2022-07-03 thomas const struct got_error *err = NULL;
174 871bd038 2022-07-03 thomas struct got_object_id *id;
175 871bd038 2022-07-03 thomas char *id_str = NULL;
176 871bd038 2022-07-03 thomas char *tagger = NULL;
177 871bd038 2022-07-03 thomas const char *tagmsg;
178 871bd038 2022-07-03 thomas char gmtoff[6];
179 871bd038 2022-07-03 thomas size_t len;
180 871bd038 2022-07-03 thomas
181 871bd038 2022-07-03 thomas id = got_object_tag_get_object_id(tag);
182 871bd038 2022-07-03 thomas err = got_object_id_str(&id_str, id);
183 871bd038 2022-07-03 thomas if (err)
184 871bd038 2022-07-03 thomas goto done;
185 871bd038 2022-07-03 thomas
186 871bd038 2022-07-03 thomas const char *type_label = NULL;
187 871bd038 2022-07-03 thomas switch (got_object_tag_get_object_type(tag)) {
188 871bd038 2022-07-03 thomas case GOT_OBJ_TYPE_BLOB:
189 871bd038 2022-07-03 thomas type_label = GOT_OBJ_LABEL_BLOB;
190 871bd038 2022-07-03 thomas break;
191 871bd038 2022-07-03 thomas case GOT_OBJ_TYPE_TREE:
192 871bd038 2022-07-03 thomas type_label = GOT_OBJ_LABEL_TREE;
193 871bd038 2022-07-03 thomas break;
194 871bd038 2022-07-03 thomas case GOT_OBJ_TYPE_COMMIT:
195 871bd038 2022-07-03 thomas type_label = GOT_OBJ_LABEL_COMMIT;
196 871bd038 2022-07-03 thomas break;
197 871bd038 2022-07-03 thomas case GOT_OBJ_TYPE_TAG:
198 871bd038 2022-07-03 thomas type_label = GOT_OBJ_LABEL_TAG;
199 871bd038 2022-07-03 thomas break;
200 871bd038 2022-07-03 thomas default:
201 871bd038 2022-07-03 thomas break;
202 871bd038 2022-07-03 thomas }
203 871bd038 2022-07-03 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
204 871bd038 2022-07-03 thomas got_object_tag_get_tagger_gmtoff(tag));
205 871bd038 2022-07-03 thomas if (asprintf(&tagger, "%s %lld %s", got_object_tag_get_tagger(tag),
206 9267f6d0 2022-07-04 thomas (long long)got_object_tag_get_tagger_time(tag), gmtoff) == -1) {
207 871bd038 2022-07-03 thomas err = got_error_from_errno("asprintf");
208 871bd038 2022-07-03 thomas goto done;
209 871bd038 2022-07-03 thomas }
210 871bd038 2022-07-03 thomas
211 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, GOT_TAG_LABEL_OBJECT);
212 871bd038 2022-07-03 thomas if (err)
213 871bd038 2022-07-03 thomas goto done;
214 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, id_str);
215 871bd038 2022-07-03 thomas if (err)
216 871bd038 2022-07-03 thomas goto done;
217 871bd038 2022-07-03 thomas err = buf_putc(buf, '\n');
218 871bd038 2022-07-03 thomas if (err)
219 871bd038 2022-07-03 thomas goto done;
220 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, GOT_TAG_LABEL_TYPE);
221 871bd038 2022-07-03 thomas if (err)
222 871bd038 2022-07-03 thomas goto done;
223 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, type_label);
224 871bd038 2022-07-03 thomas if (err)
225 871bd038 2022-07-03 thomas goto done;
226 871bd038 2022-07-03 thomas err = buf_putc(buf, '\n');
227 871bd038 2022-07-03 thomas if (err)
228 871bd038 2022-07-03 thomas goto done;
229 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, GOT_TAG_LABEL_TAG);
230 871bd038 2022-07-03 thomas if (err)
231 871bd038 2022-07-03 thomas goto done;
232 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, got_object_tag_get_name(tag));
233 871bd038 2022-07-03 thomas if (err)
234 871bd038 2022-07-03 thomas goto done;
235 871bd038 2022-07-03 thomas err = buf_putc(buf, '\n');
236 871bd038 2022-07-03 thomas if (err)
237 871bd038 2022-07-03 thomas goto done;
238 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, GOT_TAG_LABEL_TAGGER);
239 871bd038 2022-07-03 thomas if (err)
240 871bd038 2022-07-03 thomas goto done;
241 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, tagger);
242 871bd038 2022-07-03 thomas if (err)
243 871bd038 2022-07-03 thomas goto done;
244 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, "\n");
245 871bd038 2022-07-03 thomas if (err)
246 871bd038 2022-07-03 thomas goto done;
247 871bd038 2022-07-03 thomas tagmsg = got_object_tag_get_message(tag);
248 871bd038 2022-07-03 thomas err = buf_append(&len, buf, tagmsg, start_sig-tagmsg);
249 871bd038 2022-07-03 thomas if (err)
250 871bd038 2022-07-03 thomas goto done;
251 871bd038 2022-07-03 thomas
252 871bd038 2022-07-03 thomas done:
253 871bd038 2022-07-03 thomas free(id_str);
254 871bd038 2022-07-03 thomas free(tagger);
255 871bd038 2022-07-03 thomas return err;
256 871bd038 2022-07-03 thomas }
257 871bd038 2022-07-03 thomas
258 871bd038 2022-07-03 thomas const struct got_error *
259 871bd038 2022-07-03 thomas got_sigs_verify_tag_ssh(char **msg, struct got_tag_object *tag,
260 871bd038 2022-07-03 thomas const char *start_sig, const char* allowed_signers, const char* revoked,
261 871bd038 2022-07-03 thomas int verbosity)
262 871bd038 2022-07-03 thomas {
263 871bd038 2022-07-03 thomas const struct got_error *error = NULL;
264 871bd038 2022-07-03 thomas const char* argv[17];
265 871bd038 2022-07-03 thomas int pid, status, in_pfd[2], out_pfd[2];
266 871bd038 2022-07-03 thomas char* parsed_identity = NULL;
267 871bd038 2022-07-03 thomas const char *identity;
268 9b464143 2022-07-14 thomas char *tmppath = NULL;
269 3cb93379 2022-07-12 thomas FILE *tmpsig = NULL;
270 871bd038 2022-07-03 thomas BUF *buf;
271 871bd038 2022-07-03 thomas int i = 0, j;
272 871bd038 2022-07-03 thomas
273 871bd038 2022-07-03 thomas *msg = NULL;
274 871bd038 2022-07-03 thomas
275 871bd038 2022-07-03 thomas error = got_opentemp_named(&tmppath, &tmpsig,
276 871bd038 2022-07-03 thomas GOT_TMPDIR_STR "/got-tagsig");
277 871bd038 2022-07-03 thomas if (error)
278 871bd038 2022-07-03 thomas goto done;
279 871bd038 2022-07-03 thomas
280 871bd038 2022-07-03 thomas identity = got_object_tag_get_tagger(tag);
281 871bd038 2022-07-03 thomas parsed_identity = signer_identity(identity);
282 871bd038 2022-07-03 thomas if (parsed_identity != NULL)
283 871bd038 2022-07-03 thomas identity = parsed_identity;
284 871bd038 2022-07-03 thomas
285 871bd038 2022-07-03 thomas if (fputs(start_sig, tmpsig) == EOF) {
286 871bd038 2022-07-03 thomas error = got_error_from_errno("fputs");
287 871bd038 2022-07-03 thomas goto done;
288 871bd038 2022-07-03 thomas }
289 871bd038 2022-07-03 thomas if (fflush(tmpsig) == EOF) {
290 871bd038 2022-07-03 thomas error = got_error_from_errno("fflush");
291 871bd038 2022-07-03 thomas goto done;
292 871bd038 2022-07-03 thomas }
293 871bd038 2022-07-03 thomas
294 871bd038 2022-07-03 thomas error = buf_alloc(&buf, 0);
295 871bd038 2022-07-03 thomas if (error)
296 871bd038 2022-07-03 thomas goto done;
297 871bd038 2022-07-03 thomas error = got_tag_write_signed_data(buf, tag, start_sig);
298 871bd038 2022-07-03 thomas if (error)
299 871bd038 2022-07-03 thomas goto done;
300 871bd038 2022-07-03 thomas
301 871bd038 2022-07-03 thomas argv[i++] = GOT_TAG_PATH_SSH_KEYGEN;
302 871bd038 2022-07-03 thomas argv[i++] = "-Y";
303 871bd038 2022-07-03 thomas argv[i++] = "verify";
304 871bd038 2022-07-03 thomas argv[i++] = "-f";
305 871bd038 2022-07-03 thomas argv[i++] = allowed_signers;
306 871bd038 2022-07-03 thomas argv[i++] = "-I";
307 871bd038 2022-07-03 thomas argv[i++] = identity;
308 871bd038 2022-07-03 thomas argv[i++] = "-n";
309 871bd038 2022-07-03 thomas argv[i++] = "git";
310 871bd038 2022-07-03 thomas argv[i++] = "-s";
311 871bd038 2022-07-03 thomas argv[i++] = tmppath;
312 871bd038 2022-07-03 thomas if (revoked) {
313 871bd038 2022-07-03 thomas argv[i++] = "-r";
314 871bd038 2022-07-03 thomas argv[i++] = revoked;
315 871bd038 2022-07-03 thomas }
316 871bd038 2022-07-03 thomas if (verbosity > 0) {
317 871bd038 2022-07-03 thomas /* ssh(1) allows up to 3 "-v" options. */
318 871bd038 2022-07-03 thomas for (j = 0; j < MIN(3, verbosity); j++)
319 871bd038 2022-07-03 thomas argv[i++] = "-v";
320 871bd038 2022-07-03 thomas }
321 871bd038 2022-07-03 thomas argv[i++] = NULL;
322 871bd038 2022-07-03 thomas assert(i <= nitems(argv));
323 871bd038 2022-07-03 thomas
324 c4d68ce0 2022-07-03 thomas if (pipe(in_pfd) == -1) {
325 c4d68ce0 2022-07-03 thomas error = got_error_from_errno("pipe");
326 871bd038 2022-07-03 thomas goto done;
327 871bd038 2022-07-03 thomas }
328 c4d68ce0 2022-07-03 thomas if (pipe(out_pfd) == -1) {
329 c4d68ce0 2022-07-03 thomas error = got_error_from_errno("pipe");
330 871bd038 2022-07-03 thomas goto done;
331 871bd038 2022-07-03 thomas }
332 871bd038 2022-07-03 thomas
333 871bd038 2022-07-03 thomas pid = fork();
334 871bd038 2022-07-03 thomas if (pid == -1) {
335 871bd038 2022-07-03 thomas error = got_error_from_errno("fork");
336 871bd038 2022-07-03 thomas close(in_pfd[0]);
337 871bd038 2022-07-03 thomas close(in_pfd[1]);
338 871bd038 2022-07-03 thomas close(out_pfd[0]);
339 871bd038 2022-07-03 thomas close(out_pfd[1]);
340 871bd038 2022-07-03 thomas return error;
341 871bd038 2022-07-03 thomas } else if (pid == 0) {
342 871bd038 2022-07-03 thomas if (close(in_pfd[1]) == -1)
343 871bd038 2022-07-03 thomas err(1, "close");
344 3cb93379 2022-07-12 thomas if (close(out_pfd[0]) == -1)
345 871bd038 2022-07-03 thomas err(1, "close");
346 871bd038 2022-07-03 thomas if (dup2(in_pfd[0], 0) == -1)
347 871bd038 2022-07-03 thomas err(1, "dup2");
348 3cb93379 2022-07-12 thomas if (dup2(out_pfd[1], 1) == -1)
349 871bd038 2022-07-03 thomas err(1, "dup2");
350 871bd038 2022-07-03 thomas if (execv(GOT_TAG_PATH_SSH_KEYGEN, (char **const)argv) == -1)
351 871bd038 2022-07-03 thomas err(1, "execv");
352 871bd038 2022-07-03 thomas abort(); /* not reached */
353 871bd038 2022-07-03 thomas }
354 871bd038 2022-07-03 thomas if (close(in_pfd[0]) == -1) {
355 871bd038 2022-07-03 thomas error = got_error_from_errno("close");
356 871bd038 2022-07-03 thomas goto done;
357 871bd038 2022-07-03 thomas }
358 3cb93379 2022-07-12 thomas if (close(out_pfd[1]) == -1) {
359 871bd038 2022-07-03 thomas error = got_error_from_errno("close");
360 871bd038 2022-07-03 thomas goto done;
361 871bd038 2022-07-03 thomas }
362 871bd038 2022-07-03 thomas if (buf_write_fd(buf, in_pfd[1]) == -1) {
363 871bd038 2022-07-03 thomas error = got_error_from_errno("write");
364 871bd038 2022-07-03 thomas goto done;
365 871bd038 2022-07-03 thomas }
366 871bd038 2022-07-03 thomas if (close(in_pfd[1]) == -1) {
367 871bd038 2022-07-03 thomas error = got_error_from_errno("close");
368 871bd038 2022-07-03 thomas goto done;
369 871bd038 2022-07-03 thomas }
370 871bd038 2022-07-03 thomas if (waitpid(pid, &status, 0) == -1) {
371 871bd038 2022-07-03 thomas error = got_error_from_errno("waitpid");
372 871bd038 2022-07-03 thomas goto done;
373 871bd038 2022-07-03 thomas }
374 871bd038 2022-07-03 thomas if (!WIFEXITED(status)) {
375 871bd038 2022-07-03 thomas error = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
376 871bd038 2022-07-03 thomas goto done;
377 871bd038 2022-07-03 thomas }
378 871bd038 2022-07-03 thomas
379 3cb93379 2022-07-12 thomas error = buf_load_fd(&buf, out_pfd[0]);
380 871bd038 2022-07-03 thomas if (error)
381 871bd038 2022-07-03 thomas goto done;
382 871bd038 2022-07-03 thomas error = buf_putc(buf, '\0');
383 871bd038 2022-07-03 thomas if (error)
384 871bd038 2022-07-03 thomas goto done;
385 3cb93379 2022-07-12 thomas if (close(out_pfd[0]) == -1) {
386 871bd038 2022-07-03 thomas error = got_error_from_errno("close");
387 871bd038 2022-07-03 thomas goto done;
388 871bd038 2022-07-03 thomas }
389 871bd038 2022-07-03 thomas *msg = buf_get(buf);
390 871bd038 2022-07-03 thomas if (WEXITSTATUS(status) != 0)
391 871bd038 2022-07-03 thomas error = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
392 871bd038 2022-07-03 thomas
393 871bd038 2022-07-03 thomas done:
394 871bd038 2022-07-03 thomas free(parsed_identity);
395 9b464143 2022-07-14 thomas if (tmppath && unlink(tmppath) == -1 && error == NULL)
396 9b464143 2022-07-14 thomas error = got_error_from_errno("unlink");
397 871bd038 2022-07-03 thomas free(tmppath);
398 3cb93379 2022-07-12 thomas close(out_pfd[0]);
399 871bd038 2022-07-03 thomas if (tmpsig && fclose(tmpsig) == EOF && error == NULL)
400 871bd038 2022-07-03 thomas error = got_error_from_errno("fclose");
401 871bd038 2022-07-03 thomas return error;
402 871bd038 2022-07-03 thomas }