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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/* $OpenBSD: sign.c,v 1.1 2005/05/28 01:57:30 marius Exp $ */
/*
* sign.c
*
* Copyright (c) 2001 Dug Song <dugsong@arbor.net>
* Copyright (c) 2001 Arbor Networks, Inc.
*
* 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.
* 3. The names of the copyright holders may not be used to endorse or
* promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``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.
*
* $Id: sign.c,v 1.1 2005/05/28 01:57:30 marius Exp $
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/ssl.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#include "gzip.h"
#include "key.h"
#include "util.h"
static char *passphrase = NULL;
static int
embed_signature(struct key *key, FILE *fin, FILE *fout)
{
struct gzip_header gh;
struct gzip_xfield *gx;
struct gzsig_data *gd;
u_char *sig, digest[20], buf[8192];
SHA_CTX ctx;
int i, siglen;
long offset;
/* Read gzip header. */
if (fread((u_char *)&gh, 1, sizeof(gh), fin) != sizeof(gh)) {
fprintf(stderr, "Error reading gzip header: %s\n",
strerror(errno));
return (-1);
}
/* Verify gzip header. */
if (memcmp(gh.magic, GZIP_MAGIC, sizeof(gh.magic)) != 0) {
fprintf(stderr, "Invalid gzip file\n");
return (-1);
}
if (gh.flags & GZIP_FCONT) {
fprintf(stderr, "Multi-part gzip files not supported\n");
return (-1);
}
/* Skip over any existing signature. */
if (gh.flags & GZIP_FEXTRA) {
gx = (struct gzip_xfield *)buf;
gd = (struct gzsig_data *)(gx + 1);
if (fread((u_char *)gx, 1, sizeof(*gx), fin) != sizeof(*gx)) {
fprintf(stderr, "Error reading extra field: %s\n",
strerror(errno));
return (-1);
}
if (memcmp(gx->subfield.id, GZSIG_ID, 2) != 0) {
fprintf(stderr, "Unknown extra field\n");
return (-1);
}
gx->subfield.len = letoh16(gx->subfield.len);
if (gx->subfield.len < sizeof(*gd) ||
gx->subfield.len > sizeof(buf) - sizeof(*gx)) {
fprintf(stderr, "Invalid signature length\n");
return (-1);
}
if (fread((u_char *)gd, 1, gx->subfield.len, fin) !=
gx->subfield.len) {
fprintf(stderr, "Error reading signature: %s\n",
strerror(errno));
return (-1);
}
fprintf(stderr, "Overwriting existing signature\n");
}
/* Skip over any options. */
offset = ftell(fin);
if (gh.flags & GZIP_FNAME) {
while (getc(fin) != '\0')
;
}
if (gh.flags & GZIP_FCOMMENT) {
while (getc(fin) != '\0')
;
}
if (gh.flags & GZIP_FENCRYPT) {
if (fread(buf, 1, GZIP_FENCRYPT_LEN, fin) != GZIP_FENCRYPT_LEN)
return (-1);
}
/* Compute checksum over compressed data and trailer. */
SHA1_Init(&ctx);
while ((i = fread(buf, 1, sizeof(buf), fin)) > 0) {
SHA1_Update(&ctx, buf, i);
}
SHA1_Final(digest, &ctx);
/* Generate signature. */
gx = (struct gzip_xfield *)buf;
gd = (struct gzsig_data *)(gx + 1);
sig = (u_char *)(gd + 1);
siglen = key_sign(key, digest, sizeof(digest), sig,
sizeof(buf) - (sig - buf));
if (siglen < 0) {
fprintf(stderr, "Error signing checksum\n");
return (-1);
}
i = sizeof(*gd) + siglen;
gx->subfield.len = htole16(i);
gx->len = htole16(sizeof(gx->subfield) + i);
memcpy(gx->subfield.id, GZSIG_ID, sizeof(gx->subfield.id));
gd->version = GZSIG_VERSION;
/* Write out gzip header. */
gh.flags |= GZIP_FEXTRA;
if (fwrite((u_char *)&gh, 1, sizeof(gh), fout) != sizeof(gh)) {
fprintf(stderr, "Error writing output: %s\n", strerror(errno));
return (-1);
}
/* Write out signature. */
if (fwrite(buf, 1, sizeof(*gx) + i, fout) != sizeof(*gx) + i) {
fprintf(stderr, "Error writing output: %s\n", strerror(errno));
return (-1);
}
/* Write out options, compressed data, and trailer. */
if (fseek(fin, offset, SEEK_SET) < 0) {
fprintf(stderr, "Error writing output: %s\n", strerror(errno));
return (-1);
}
while ((i = fread(buf, 1, sizeof(buf), fin)) > 0) {
if (fwrite(buf, 1, i, fout) != i) {
fprintf(stderr, "Error writing output: %s\n",
strerror(errno));
return (-1);
}
}
if (ferror(fin)) {
fprintf(stderr, "Error reading input: %s\n", strerror(errno));
return (-1);
}
return (0);
}
void
sign_usage(void)
{
fprintf(stderr, "Usage: gzsig sign [-v] privkey [file ...]\n");
}
int
sign_passwd_cb(char *buf, int size, int rwflag, void *u)
{
char *p;
if (passphrase != NULL) {
if (strlcpy(buf, passphrase, size) >= size)
errx(1, "Passphrase too long");
} else {
p = getpass("Enter passphrase: ");
if (strlcpy(buf, p, size) >= size)
errx(1, "Passphrase too long");
memset(p, 0, strlen(p));
}
return (strlen(buf));
}
void
sign(int argc, char *argv[])
{
struct key *key;
char *gzipfile, tmpfile[MAXPATHLEN];
FILE *fin, *fout;
int i, fd, error, vflag;
vflag = 0;
while ((i = getopt(argc, argv, "vh?p:")) != -1) {
switch (i) {
case 'v':
vflag = 1;
break;
case 'p':
passphrase = optarg;
break;
default:
sign_usage();
exit(1);
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
sign_usage();
exit(1);
}
OpenSSL_add_all_algorithms();
if ((key = key_new()) == NULL)
fatal(1, "Couldn't initialize private key");
if (key_load_private(key, argv[0]) < 0)
fatal(1, "Couldn't load private key");
if (argc == 1 || *argv[1] == '-') {
argc = 0;
if (embed_signature(key, stdin, stdout) == 0) {
if (vflag)
fprintf(stderr, "Signed input\n");
} else
fatal(1, "Couldn't sign input");
}
for (i = 1; i < argc; i++) {
gzipfile = argv[i];
if ((fin = fopen(gzipfile, "r+")) == NULL) {
fprintf(stderr, "Error opening %s: %s\n",
gzipfile, strerror(errno));
continue;
}
snprintf(tmpfile, sizeof(tmpfile), "%s.XXXXXX", gzipfile);
if ((fd = mkstemp(tmpfile)) < 0) {
fprintf(stderr, "Error creating %s: %s\n",
tmpfile, strerror(errno));
fclose(fin);
continue;
}
if ((fout = fdopen(fd, "w")) == NULL) {
fprintf(stderr, "Error opening %s: %s\n",
tmpfile, strerror(errno));
fclose(fin);
close(fd);
continue;
}
if (copy_permissions(gzipfile, tmpfile) < 0) {
fprintf(stderr, "Error initializing %s: %s\n",
tmpfile, strerror(errno));
fclose(fin);
fclose(fout);
continue;
}
error = embed_signature(key, fin, fout);
fclose(fin);
fclose(fout);
if (!error) {
if (rename(tmpfile, gzipfile) < 0) {
unlink(tmpfile);
fatal(1, "Couldn't sign %s", gzipfile);
}
if (vflag)
fprintf(stderr, "Signed %s\n", gzipfile);
} else {
unlink(tmpfile);
fatal(1, "Couldn't sign %s", gzipfile);
}
}
key_free(key);
if (passphrase != NULL)
memset(passphrase, 0, strlen(passphrase));
}
|