xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/netpgpverify/main.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*-
2  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "config.h"
26 
27 #include <sys/types.h>
28 
29 #include <inttypes.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35 
36 #include "verify.h"
37 
38 /* print the time nicely */
39 static void
40 ptime(int64_t secs)
41 {
42 	time_t	t;
43 
44 	t = (time_t)secs;
45 	printf("%s", ctime(&t));
46 }
47 
48 /* print entry n */
49 static void
50 pentry(pgpv_t *pgp, int n, const char *modifiers)
51 {
52 	char	*s;
53 
54 	pgpv_get_entry(pgp, (unsigned)n, &s, modifiers);
55 	printf("%s", s);
56 	free(s);
57 }
58 
59 #define MB(x)	((x) * 1024 * 1024)
60 
61 /* get stdin into memory so we can verify it */
62 static char *
63 getstdin(ssize_t *cc, size_t *size)
64 {
65 	size_t	 newsize;
66 	char	*newin;
67 	char	*in;
68 	int	 rc;
69 
70 	*cc = 0;
71 	*size = 0;
72 	in = NULL;
73 	do {
74 		newsize = *size + MB(1);
75 		if ((newin = realloc(in, newsize)) == NULL) {
76 			break;
77 		}
78 		in = newin;
79 		*size = newsize;
80 		if ((rc = read(STDIN_FILENO, &in[*cc], newsize - *cc)) > 0) {
81 			*cc += rc;
82 		}
83 	} while (rc > 0);
84 	return in;
85 }
86 
87 /* verify memory or file */
88 static int
89 verify_data(pgpv_t *pgp, const char *cmd, const char *inname, char *in, ssize_t cc)
90 {
91 	pgpv_cursor_t	 cursor;
92 	const char	*modifiers;
93 	size_t		 size;
94 	size_t		 cookie;
95 	char		*data;
96 	int		 el;
97 
98 	memset(&cursor, 0x0, sizeof(cursor));
99 	if (strcasecmp(cmd, "cat") == 0) {
100 		if ((cookie = pgpv_verify(&cursor, pgp, in, cc)) != 0) {
101 			if ((size = pgpv_get_verified(&cursor, cookie, &data)) > 0) {
102 				write(STDOUT_FILENO, data, size);
103 			}
104 			return 1;
105 		}
106 	} else if (strcasecmp(cmd, "dump") == 0) {
107 		if ((cookie = pgpv_verify(&cursor, pgp, in, cc)) != 0) {
108 			size = pgpv_dump(pgp, &data);
109 			write(STDOUT_FILENO, data, size);
110 			return 1;
111 		}
112 	} else if (strcasecmp(cmd, "verify") == 0 || strcasecmp(cmd, "trust") == 0) {
113 		modifiers = (strcasecmp(cmd, "trust") == 0) ? "trust" : NULL;
114 		if (pgpv_verify(&cursor, pgp, in, cc)) {
115 			printf("Good signature for %s made ", inname);
116 			ptime(cursor.sigtime);
117 			el = pgpv_get_cursor_element(&cursor, 0);
118 			pentry(pgp, el, modifiers);
119 			return 1;
120 		}
121 		fprintf(stderr, "Signature did not match contents -- %s\n", cursor.why);
122 	} else {
123 		fprintf(stderr, "unrecognised command \"%s\"\n", cmd);
124 	}
125 	return 0;
126 }
127 
128 int
129 main(int argc, char **argv)
130 {
131 	const char	*keyring;
132 	const char	*cmd;
133 	ssize_t		 cc;
134 	size_t		 size;
135 	pgpv_t		 pgp;
136 	char		*in;
137 	int		 ssh;
138 	int		 ok;
139 	int		 i;
140 
141 	memset(&pgp, 0x0, sizeof(pgp));
142 	keyring = NULL;
143 	ssh = 0;
144 	ok = 1;
145 	cmd = "verify";
146 	while ((i = getopt(argc, argv, "S:c:k:v")) != -1) {
147 		switch(i) {
148 		case 'S':
149 			ssh = 1;
150 			keyring = optarg;
151 			break;
152 		case 'c':
153 			cmd = optarg;
154 			break;
155 		case 'k':
156 			keyring = optarg;
157 			break;
158 		case 'v':
159 			printf("%s\n", NETPGPVERIFY_VERSION);
160 			exit(EXIT_SUCCESS);
161 		default:
162 			break;
163 		}
164 	}
165 	if (ssh) {
166 		if (!pgpv_read_ssh_pubkeys(&pgp, keyring, -1)) {
167 			fprintf(stderr, "can't read ssh keyring\n");
168 			exit(EXIT_FAILURE);
169 		}
170 	} else if (!pgpv_read_pubring(&pgp, keyring, -1)) {
171 		fprintf(stderr, "can't read keyring\n");
172 		exit(EXIT_FAILURE);
173 	}
174 	if (optind == argc) {
175 		in = getstdin(&cc, &size);
176 		ok = verify_data(&pgp, cmd, "[stdin]", in, cc);
177 	} else {
178 		for (ok = 1, i = optind ; i < argc ; i++) {
179 			if (!verify_data(&pgp, cmd, argv[i], argv[i], -1)) {
180 				ok = 0;
181 			}
182 		}
183 	}
184 	pgpv_close(&pgp);
185 	exit((ok) ? EXIT_SUCCESS : EXIT_FAILURE);
186 }
187