2 * Copyright (c) 2022 Josh Rickmar <jrick@zettaport.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/queue.h>
34 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_opentemp.h"
44 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #ifndef GOT_TAG_PATH_SSH_KEYGEN
52 #define GOT_TAG_PATH_SSH_KEYGEN "/usr/bin/ssh-keygen"
55 #ifndef GOT_TAG_PATH_SIGNIFY
56 #define GOT_TAG_PATH_SIGNIFY "/usr/bin/signify"
59 const struct got_error *
60 got_sigs_apply_unveil(void)
62 if (unveil(GOT_TAG_PATH_SSH_KEYGEN, "x") != 0) {
63 return got_error_from_errno2("unveil",
64 GOT_TAG_PATH_SSH_KEYGEN);
66 if (unveil(GOT_TAG_PATH_SIGNIFY, "x") != 0) {
67 return got_error_from_errno2("unveil",
68 GOT_TAG_PATH_SIGNIFY);
74 const struct got_error *
75 got_sigs_sign_tag_ssh(pid_t *newpid, int *in_fd, int *out_fd,
76 const char* key_file, int verbosity)
78 const struct got_error *error = NULL;
79 int pid, in_pfd[2], out_pfd[2];
87 argv[i++] = GOT_TAG_PATH_SSH_KEYGEN;
97 /* ssh(1) allows up to 3 "-v" options. */
98 for (j = 0; j < MIN(3, verbosity); j++)
102 assert(i <= nitems(argv));
104 if (pipe(in_pfd) == -1)
105 return got_error_from_errno("pipe");
106 if (pipe(out_pfd) == -1)
107 return got_error_from_errno("pipe");
111 error = got_error_from_errno("fork");
117 } else if (pid == 0) {
118 if (close(in_pfd[1]) == -1)
120 if (close(out_pfd[0]) == -1)
122 if (dup2(in_pfd[0], 0) == -1)
124 if (dup2(out_pfd[1], 1) == -1)
126 if (execv(GOT_TAG_PATH_SSH_KEYGEN, (char **const)argv) == -1)
128 abort(); /* not reached */
130 if (close(in_pfd[0]) == -1)
131 return got_error_from_errno("close");
132 if (close(out_pfd[1]) == -1)
133 return got_error_from_errno("close");
136 *out_fd = out_pfd[0];
141 signer_identity(const char *tagger)
145 lt = strstr(tagger, " <");
146 gt = strrchr(tagger, '>');
147 if (lt && gt && lt+1 < gt)
148 return strndup(lt+2, gt-lt-2);
152 static const char* BEGIN_SSH_SIG = "-----BEGIN SSH SIGNATURE-----\n";
153 static const char* END_SSH_SIG = "-----END SSH SIGNATURE-----\n";
156 got_sigs_get_tagmsg_ssh_signature(const char *tagmsg)
158 const char *s = tagmsg, *begin = NULL, *end = NULL;
160 while ((s = strstr(s, BEGIN_SSH_SIG)) != NULL) {
162 s += strlen(BEGIN_SSH_SIG);
165 end = strstr(begin+strlen(BEGIN_SSH_SIG), END_SSH_SIG);
168 return (end[strlen(END_SSH_SIG)] == '\0') ? begin : NULL;
171 static const struct got_error *
172 got_tag_write_signed_data(BUF *buf, struct got_tag_object *tag,
173 const char *start_sig)
175 const struct got_error *err = NULL;
176 struct got_object_id *id;
183 id = got_object_tag_get_object_id(tag);
184 err = got_object_id_str(&id_str, id);
188 const char *type_label = NULL;
189 switch (got_object_tag_get_object_type(tag)) {
190 case GOT_OBJ_TYPE_BLOB:
191 type_label = GOT_OBJ_LABEL_BLOB;
193 case GOT_OBJ_TYPE_TREE:
194 type_label = GOT_OBJ_LABEL_TREE;
196 case GOT_OBJ_TYPE_COMMIT:
197 type_label = GOT_OBJ_LABEL_COMMIT;
199 case GOT_OBJ_TYPE_TAG:
200 type_label = GOT_OBJ_LABEL_TAG;
205 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
206 got_object_tag_get_tagger_gmtoff(tag));
207 if (asprintf(&tagger, "%s %lld %s", got_object_tag_get_tagger(tag),
208 (long long)got_object_tag_get_tagger_time(tag), gmtoff) == -1) {
209 err = got_error_from_errno("asprintf");
213 err = buf_puts(&len, buf, GOT_TAG_LABEL_OBJECT);
216 err = buf_puts(&len, buf, id_str);
219 err = buf_putc(buf, '\n');
222 err = buf_puts(&len, buf, GOT_TAG_LABEL_TYPE);
225 err = buf_puts(&len, buf, type_label);
228 err = buf_putc(buf, '\n');
231 err = buf_puts(&len, buf, GOT_TAG_LABEL_TAG);
234 err = buf_puts(&len, buf, got_object_tag_get_name(tag));
237 err = buf_putc(buf, '\n');
240 err = buf_puts(&len, buf, GOT_TAG_LABEL_TAGGER);
243 err = buf_puts(&len, buf, tagger);
246 err = buf_puts(&len, buf, "\n");
249 tagmsg = got_object_tag_get_message(tag);
250 err = buf_append(&len, buf, tagmsg, start_sig-tagmsg);
260 const struct got_error *
261 got_sigs_verify_tag_ssh(char **msg, struct got_tag_object *tag,
262 const char *start_sig, const char* allowed_signers, const char* revoked,
265 const struct got_error *error = NULL;
266 const char* argv[17];
267 int pid, status, in_pfd[2], out_pfd[2];
268 char* parsed_identity = NULL;
269 const char *identity;
270 char *tmppath = NULL;
277 error = got_opentemp_named(&tmppath, &tmpsig,
278 GOT_TMPDIR_STR "/got-tagsig", "");
282 identity = got_object_tag_get_tagger(tag);
283 parsed_identity = signer_identity(identity);
284 if (parsed_identity != NULL)
285 identity = parsed_identity;
287 if (fputs(start_sig, tmpsig) == EOF) {
288 error = got_error_from_errno("fputs");
291 if (fflush(tmpsig) == EOF) {
292 error = got_error_from_errno("fflush");
296 error = buf_alloc(&buf, 0);
299 error = got_tag_write_signed_data(buf, tag, start_sig);
303 argv[i++] = GOT_TAG_PATH_SSH_KEYGEN;
305 argv[i++] = "verify";
307 argv[i++] = allowed_signers;
309 argv[i++] = identity;
319 /* ssh(1) allows up to 3 "-v" options. */
320 for (j = 0; j < MIN(3, verbosity); j++)
324 assert(i <= nitems(argv));
326 if (pipe(in_pfd) == -1) {
327 error = got_error_from_errno("pipe");
330 if (pipe(out_pfd) == -1) {
331 error = got_error_from_errno("pipe");
337 error = got_error_from_errno("fork");
343 } else if (pid == 0) {
344 if (close(in_pfd[1]) == -1)
346 if (close(out_pfd[0]) == -1)
348 if (dup2(in_pfd[0], 0) == -1)
350 if (dup2(out_pfd[1], 1) == -1)
352 if (execv(GOT_TAG_PATH_SSH_KEYGEN, (char **const)argv) == -1)
354 abort(); /* not reached */
356 if (close(in_pfd[0]) == -1) {
357 error = got_error_from_errno("close");
360 if (close(out_pfd[1]) == -1) {
361 error = got_error_from_errno("close");
364 if (buf_write_fd(buf, in_pfd[1]) == -1) {
365 error = got_error_from_errno("write");
368 if (close(in_pfd[1]) == -1) {
369 error = got_error_from_errno("close");
372 if (waitpid(pid, &status, 0) == -1) {
373 error = got_error_from_errno("waitpid");
376 if (!WIFEXITED(status)) {
377 error = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
381 error = buf_load_fd(&buf, out_pfd[0]);
384 error = buf_putc(buf, '\0');
387 if (close(out_pfd[0]) == -1) {
388 error = got_error_from_errno("close");
392 if (WEXITSTATUS(status) != 0)
393 error = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
396 free(parsed_identity);
397 if (tmppath && unlink(tmppath) == -1 && error == NULL)
398 error = got_error_from_errno("unlink");
401 if (tmpsig && fclose(tmpsig) == EOF && error == NULL)
402 error = got_error_from_errno("fclose");