summaryrefslogtreecommitdiff
path: root/security/netpgpverify/files/main.c
blob: 793f5355cdbdd2a93fe5e67df0e2b6f1734b2e6f (plain)
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
/*-
 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config.h"

#include <sys/types.h>

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "verify.h"

#include "array.h"

/* print the time nicely */
static void
ptime(int64_t secs)
{
	time_t	t;

	t = (time_t)secs;
	printf("%s", ctime(&t));
}

/* print entry n */
static void
pentry(pgpv_t *pgp, int n)
{
	char	*s;

	pgpv_get_entry(pgp, (unsigned)n, &s);
	printf("%s", s);
	free(s);
}

#define MB(x)	((x) * 1024 * 1024)

/* get stdin into memory so we can verify it */
static char *
getstdin(ssize_t *cc, size_t *size)
{
	size_t	 newsize;
	char	*newin;
	char	*in;
	int	 rc;

	*cc = 0;
	*size = 0;
	in = NULL;
	do {
		newsize = *size + MB(1);
		if ((newin = realloc(in, newsize)) == NULL) {
			break;
		}
		in = newin;
		*size = newsize;
		if ((rc = read(STDIN_FILENO, &in[*cc], newsize - *cc)) > 0) {
			*cc += rc;
		}
	} while (rc > 0);
	return in;
}

/* verify memory or file */
static int
verify_data(pgpv_t *pgp, const char *cmd, const char *inname, char *in, ssize_t cc)
{
	pgpv_cursor_t	 cursor;
	size_t		 size;
	size_t		 cookie;
	char		*data;

	memset(&cursor, 0x0, sizeof(cursor));
	if (strcasecmp(cmd, "cat") == 0) {
		if ((cookie = pgpv_verify(&cursor, pgp, in, cc)) != 0) {
			if ((size = pgpv_get_verified(&cursor, cookie, &data)) > 0) {
				write(STDOUT_FILENO, data, size);
			}
			return 1;
		}
	} else if (strcasecmp(cmd, "verify") == 0) {
		if (pgpv_verify(&cursor, pgp, in, cc)) {
			printf("Good signature for %s made ", inname);
			ptime(cursor.sigtime);
			pentry(pgp, ARRAY_ELEMENT(cursor.found, 0));
			return 1;
		}
		fprintf(stderr, "Signature did not match contents -- %s", cursor.why);
	} else {
		fprintf(stderr, "unrecognised command \"%s\"", cmd);
	}
	return 0;
}

int
main(int argc, char **argv)
{
	const char	*keyring;
	const char	*cmd;
	ssize_t		 cc;
	size_t		 size;
	pgpv_t		 pgp;
	char		*in;
	int		 ok;
	int		 i;

	memset(&pgp, 0x0, sizeof(pgp));
	keyring = NULL;
	ok = 1;
	cmd = "verify";
	while ((i = getopt(argc, argv, "c:k:v")) != -1) {
		switch(i) {
		case 'c':
			cmd = optarg;
			break;
		case 'k':
			keyring = optarg;
			break;
		case 'v':
			printf("%s\n", NETPGPVERIFY_VERSION);
			exit(EXIT_SUCCESS);
		default:
			break;
		}
	}
	if (!pgpv_read_pubring(&pgp, keyring, -1)) {
		errx(EXIT_FAILURE, "can't read keyring");
	}
	if (optind == argc) {
		in = getstdin(&cc, &size);
		ok = verify_data(&pgp, cmd, "[stdin]", in, cc);
	} else {
		for (ok = 1, i = optind ; i < argc ; i++) {
			if (!verify_data(&pgp, cmd, argv[i], argv[i], -1)) {
				ok = 0;
			}
		}
	}
	pgpv_close(&pgp);
	exit((ok) ? EXIT_SUCCESS : EXIT_FAILURE);
}