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