~ubuntu-branches/ubuntu/karmic/openvas-server/karmic

« back to all changes in this revision

Viewing changes to openvasd/openvas-check-signature.c

  • Committer: Bazaar Package Importer
  • Author(s): Javier Fernandez-Sanguino Pen~a
  • Date: 2009-01-02 01:38:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090102013847-74xy2uo1e3hovqjo
Tags: 2.0.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* OpenVAS
2
 
* $Id: openvas-check-signature.c 272 2007-07-18 14:10:27Z bh $
3
 
* Description: generates/checks a signature for a given file.
4
 
*
5
 
* Authors: - Renaud Deraison <deraison@nessus.org> (Original pre-fork develoment)
6
 
*          - Tim Brown <mailto:timb@openvas.org> (Initial fork)
7
 
*          - Laban Mwangi <mailto:labanm@openvas.org> (Renaming work)
8
 
*          - Tarik El-Yassem <mailto:tarik@openvas.org> (Headers section)
9
 
*
10
 
* Copyright:
11
 
* Portions Copyright (C) 2006 Software in the Public Interest, Inc.
12
 
* Based on work Copyright (C) 1998 - 2006 Tenable Network Security, Inc.
13
 
*
14
 
* This program is free software; you can redistribute it and/or modify
15
 
* it under the terms of the GNU General Public License version 2,
16
 
* as published by the Free Software Foundation
17
 
*
18
 
* This program is distributed in the hope that it will be useful,
19
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
* GNU General Public License for more details.
22
 
*
23
 
* You should have received a copy of the GNU General Public License
24
 
* along with this program; if not, write to the Free Software
25
 
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26
 
*
27
 
*
28
 
*/
29
 
 
30
 
/* FIXME: The code here is mostly a duplicate of code in
31
 
 * openvas-libnasl/nasl/nasl_crypto2.c.  The main difference is that the
32
 
 * signatures dealt with here are detached, whereas the signatures
33
 
 * handled by nasl_crypto2.c are part of the signed file.
34
 
 *
35
 
 * Also, the original OpenSSL code in this file was probably better at
36
 
 * handling larger files.  The new code read the file to sign or verify
37
 
 * completely into memory which may be inefficient for large files.
38
 
 *
39
 
 * Before something is done about it, OpenVAS needs to decide how to
40
 
 * deal with signed files in general.
41
 
 */
42
 
 
43
 
#include <includes.h>
44
 
#include <gnutls/gnutls.h>
45
 
#include <gnutls/x509.h>
46
 
 
47
 
 
48
 
void
49
 
print_tls_error(char *txt, int err)
50
 
{
51
 
  fprintf(stderr, "%s: %s (%d)\n", txt, gnutls_strerror(err), err);
52
 
}
53
 
 
54
 
gnutls_datum_t
55
 
map_file(const char * filename)
56
 
{
57
 
  FILE *f;
58
 
  gnutls_datum loaded_file = { NULL, 0 };
59
 
  long filelen;
60
 
  void *ptr;
61
 
 
62
 
  if (!(f = fopen(filename, "r"))
63
 
      || fseek(f, 0, SEEK_END) != 0
64
 
      || (filelen = ftell(f)) < 0
65
 
      || fseek(f, 0, SEEK_SET) != 0
66
 
      || !(ptr = emalloc((size_t) filelen))
67
 
      || fread(ptr, 1, (size_t) filelen, f) < (size_t) filelen)
68
 
    {
69
 
      return loaded_file;
70
 
    }
71
 
 
72
 
  loaded_file.data = ptr;
73
 
  loaded_file.size = (unsigned int) filelen;
74
 
  return loaded_file;
75
 
}
76
 
 
77
 
static ptrdiff_t
78
 
hexdecode(unsigned char *binary, const unsigned char *hex, size_t fromlen)
79
 
{
80
 
  char temp[3] = {0, 0, 0};
81
 
  unsigned char * to = binary;
82
 
  const unsigned char * from = hex;
83
 
 
84
 
  while ((from - hex) < fromlen - 1)
85
 
    {
86
 
      temp[0] = from[0];
87
 
      temp[1] = from[1];
88
 
      *to = strtoul(temp, NULL, 16);
89
 
      to += 1;
90
 
      from += 2;
91
 
    }
92
 
 
93
 
  return to - binary;
94
 
}
95
 
 
96
 
 
97
 
/*
98
 
 * Signs a given file
99
 
 */
100
 
static int
101
 
generate_signature(char * keyfilename, char * filename)
102
 
{
103
 
  int result = -1;
104
 
  int i;
105
 
  int be_len;
106
 
  gnutls_datum_t pem = {NULL, 0};
107
 
  gnutls_datum_t script = {NULL, 0};
108
 
  gnutls_x509_privkey_t privkey = NULL;
109
 
  unsigned char* signature = NULL;
110
 
  size_t signature_size = 0;
111
 
  int err;
112
 
 
113
 
  err = gnutls_x509_privkey_init(&privkey);
114
 
  if (err)
115
 
    {
116
 
      print_tls_error("gnutls_x509_privkey_init", err);
117
 
      goto fail;
118
 
    }
119
 
 
120
 
  pem = map_file(keyfilename);
121
 
  if (!pem.data)
122
 
    goto fail;
123
 
 
124
 
  err = gnutls_x509_privkey_import(privkey, &pem, GNUTLS_X509_FMT_PEM);
125
 
  if (err)
126
 
    {
127
 
      print_tls_error("gnutls_x509_privkey_import", err);
128
 
      goto fail;
129
 
    }
130
 
 
131
 
  script = map_file(filename);
132
 
  if (!script.data)
133
 
    {
134
 
      goto fail;
135
 
    }
136
 
 
137
 
  /* append the size of the file at the end of the script */
138
 
  script.data = erealloc(script.data, script.size + sizeof(be_len));
139
 
  be_len = htonl(script.size);
140
 
  memcpy(script.data + script.size, &be_len, sizeof(be_len));
141
 
  script.size += sizeof(be_len);
142
 
 
143
 
  /* call gnutls_x509_privkey_sign_data twice: once to determine the
144
 
   * size of the signature and then again to actually create the
145
 
   * signature */
146
 
  err = gnutls_x509_privkey_sign_data(privkey, GNUTLS_DIG_SHA1, 0, &script,
147
 
                                      signature, &signature_size);
148
 
  if (err != GNUTLS_E_SHORT_MEMORY_BUFFER)
149
 
    {
150
 
      print_tls_error("gnutls_x509_privkey_sign_data", err);
151
 
      goto fail;
152
 
    }
153
 
 
154
 
  signature = emalloc(signature_size);
155
 
  err = gnutls_x509_privkey_sign_data(privkey, GNUTLS_DIG_SHA1, 0, &script,
156
 
                                      signature, &signature_size);
157
 
  if (err)
158
 
    {
159
 
      print_tls_error("gnutls_x509_privkey_sign_data", err);
160
 
      goto fail;
161
 
    }
162
 
 
163
 
  /* print the signature to stdout in hexadecimal */
164
 
  for (i = 0; i < signature_size; i++)
165
 
    {
166
 
      printf("%.2x", signature[i]);
167
 
    }
168
 
  printf("\n");
169
 
 
170
 
  result = 0;
171
 
 
172
 
 fail:
173
 
  efree(&pem.data);
174
 
  efree(&script.data);
175
 
  efree(&signature);
176
 
  gnutls_x509_privkey_deinit(privkey);
177
 
 
178
 
  return result;
179
 
}
180
 
 
181
 
 
182
 
/*
183
 
 * Verify an archive signature
184
 
 *
185
 
 * Returns :
186
 
 *      -1 : if an error occured
187
 
 *       0 : if the signature matches
188
 
 *       1 : if the signature does NOT match
189
 
 */
190
 
static int
191
 
verify_signature(char * certfilename, char * filename, char * sigfilename)
192
 
{
193
 
  int be_len;
194
 
  gnutls_x509_crt_t cert = NULL;
195
 
  gnutls_datum_t pem = {NULL, 0};
196
 
  gnutls_datum_t script = {NULL, 0};
197
 
  gnutls_datum_t signature = {NULL, 0};
198
 
  int result = -1;
199
 
  int err;
200
 
 
201
 
  pem = map_file(certfilename);
202
 
  if (!pem.data)
203
 
    goto fail;
204
 
 
205
 
  err = gnutls_x509_crt_init(&cert);
206
 
  if (err)
207
 
    {
208
 
      print_tls_error("gnutls_x509_crt_init", err);
209
 
      goto fail;
210
 
    }
211
 
 
212
 
  err = gnutls_x509_crt_import(cert, &pem, GNUTLS_X509_FMT_PEM);
213
 
  if (err)
214
 
    {
215
 
      print_tls_error("gnutls_x509_crt_import", err);
216
 
      goto fail;
217
 
    }
218
 
 
219
 
  script = map_file(filename);
220
 
  if (!script.data)
221
 
    {
222
 
      goto fail;
223
 
    }
224
 
 
225
 
  /* Make room for the size of the file at the end of the script and
226
 
   * append the size */
227
 
  script.data = erealloc(script.data, script.size + sizeof(be_len));
228
 
  be_len = htonl(script.size);
229
 
  memcpy(script.data + script.size, &be_len, sizeof(be_len));
230
 
  script.size += sizeof(be_len);
231
 
 
232
 
  /* read and decode the hex signature.  Decoding can be done in place
233
 
   * because the binary signature is always shorter than its hexadecimal
234
 
   * representation. */
235
 
  signature = map_file(sigfilename);
236
 
  if (!signature.data)
237
 
    {
238
 
      goto fail;
239
 
    }
240
 
  signature.size = hexdecode(signature.data, signature.data, signature.size);
241
 
 
242
 
  err = gnutls_x509_crt_verify_data(cert, 0, &script, &signature);
243
 
  if (err < 0)
244
 
    {
245
 
      print_tls_error("gnutls_x509_crt_verify_data", err);
246
 
      goto fail;
247
 
    }
248
 
 
249
 
  result = err == 1 ? 0 : 1;
250
 
 
251
 
 fail:
252
 
  gnutls_x509_crt_deinit(cert);
253
 
  efree(&script.data);
254
 
  efree(&signature.data);
255
 
  efree(&pem);
256
 
 
257
 
  return result;
258
 
 
259
 
}
260
 
 
261
 
 
262
 
int
263
 
main(int argc, char ** argv)
264
 
{
265
 
  int do_sign = 0;
266
 
  int do_print_usage = 0;
267
 
  char * keyfile = NULL;
268
 
  char * certfile = NULL;
269
 
  int opt;
270
 
  int option_index = 0;
271
 
  struct option long_options[] =
272
 
    {
273
 
      {"help",          no_argument,       0, 'h'},
274
 
      {"certificate",   required_argument, 0, 'c'},
275
 
      {"key",           required_argument, 0, 'k'},
276
 
      {"sign",          no_argument,       0, 's'},
277
 
      {0, 0, 0, 0}
278
 
    };
279
 
 
280
 
  while ((opt = getopt_long(argc, argv, "c:hk:s", long_options, &option_index))
281
 
         != -1)
282
 
    {
283
 
      switch (opt)
284
 
        {
285
 
        case 'c':
286
 
          certfile = optarg;
287
 
          break;
288
 
 
289
 
        case 'h':
290
 
          do_print_usage = 1;
291
 
          break;
292
 
 
293
 
        case 'k':
294
 
          keyfile = optarg;
295
 
          break;
296
 
 
297
 
        case 's':
298
 
          do_sign = 1;
299
 
          break;
300
 
 
301
 
        case '?':
302
 
          fprintf(stderr, "unknown option or missing"
303
 
                  " parameter for option '%c'\n", opt);
304
 
          return 1;
305
 
 
306
 
        default:
307
 
          fprintf(stderr, "option '%c' not implemented\n", opt);
308
 
          return 1;
309
 
        }
310
 
    }
311
 
 
312
 
  if (do_print_usage)
313
 
    {
314
 
      fprintf(stderr,
315
 
              "Usage: openvas-check-signature [options]"
316
 
              " filename [signaturefile]\n");
317
 
      fprintf(stderr, "Options:\n");
318
 
      fprintf(stderr, " -h           Print this help message\n");
319
 
      fprintf(stderr, " -k keyfile   File with private key for signature\n");
320
 
      fprintf(stderr, " -c certfile  File with certificate for signature"
321
 
              " verificationi\n");
322
 
      return 0;
323
 
    }
324
 
 
325
 
  nessus_SSL_init(NULL);
326
 
 
327
 
  if (do_sign)
328
 
    {
329
 
      if (!keyfile)
330
 
        {
331
 
          fprintf(stderr, "Missing parameter -k required for"
332
 
                  " signature generation\n");
333
 
          return 1;
334
 
        }
335
 
      if (optind >= argc)
336
 
        {
337
 
          fprintf(stderr, "missing filename parameter\n");
338
 
          return 1;
339
 
        }
340
 
 
341
 
      generate_signature(keyfile, argv[optind]);
342
 
    }
343
 
  else
344
 
    {
345
 
      if (!certfile)
346
 
        {
347
 
          fprintf(stderr, "Missing parameter -c required for"
348
 
                  " signature verification\n");
349
 
          return 1;
350
 
        }
351
 
 
352
 
      if (optind + 1 >= argc)
353
 
        {
354
 
          fprintf(stderr, "for signature verification, a filename and the"
355
 
                  " signature filename must be given\n");
356
 
          return 1;
357
 
        }
358
 
      else
359
 
        {
360
 
          char * filename = argv[optind];
361
 
          char * signaturefile = argv[optind + 1];
362
 
 
363
 
          if (verify_signature(certfile, filename, signaturefile) == 0)
364
 
            return 0;
365
 
          else
366
 
            {
367
 
              fprintf(stderr, "%s is not the valid signature for %s\n",
368
 
                      signaturefile, filename);
369
 
              return 1;
370
 
            }
371
 
        }
372
 
    }
373
 
 
374
 
  return 0;
375
 
}