~ubuntu-branches/ubuntu/hardy/gnutls13/hardy-updates

« back to all changes in this revision

Viewing changes to src/certtool.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2007-09-29 11:29:22 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070929112922-abqj0gl08kstu27v
Tags: 2.0.1-1
* New upstream version.
* Remove doc/*.info* on clean to allow building thrice in a row.
  (Closes: #441740)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004, 2005, 2006 Free Software Foundation
3
 
 * Copyright (C) 2004 Simon Josefsson
4
 
 * Copyright (C) 2003 Nikos Mavroyanopoulos
 
2
 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation
5
3
 *
6
4
 * This file is part of GNUTLS.
7
5
 *
23
21
#include <config.h>
24
22
#include <gnutls/gnutls.h>
25
23
 
26
 
#ifdef ENABLE_PKI
27
 
 
28
24
#include <stdio.h>
29
25
#include <stdlib.h>
30
26
#include <string.h>
36
32
#include <unistd.h>
37
33
#include <certtool-cfg.h>
38
34
 
 
35
#include <errno.h>
 
36
 
39
37
/* Gnulib portability files. */
40
 
#include <getline.h>
 
38
#include <read-file.h>
 
39
#include <error.h>
 
40
#include <progname.h>
 
41
#include <version-etc.h>
41
42
 
42
 
static void print_crl_info (gnutls_x509_crl crl, FILE *out, int all);
 
43
static void print_crl_info (gnutls_x509_crl_t crl, FILE *out);
43
44
int generate_prime (int bits, int how);
44
45
void pkcs7_info (void);
45
46
void smime_to_pkcs7 (void);
47
48
void generate_pkcs12 (void);
48
49
void verify_chain (void);
49
50
void verify_crl (void);
50
 
gnutls_x509_privkey load_private_key (int mand);
51
 
gnutls_x509_crq load_request (void);
52
 
gnutls_x509_privkey load_ca_private_key (void);
53
 
gnutls_x509_crt load_ca_cert (void);
54
 
gnutls_x509_crt load_cert (int mand);
 
51
gnutls_x509_privkey_t load_private_key (int mand);
 
52
gnutls_x509_crq_t load_request (void);
 
53
gnutls_x509_privkey_t load_ca_private_key (void);
 
54
gnutls_x509_crt_t load_ca_cert (void);
 
55
gnutls_x509_crt_t load_cert (int mand);
55
56
void certificate_info (void);
56
57
void crl_info (void);
57
58
void privkey_info (void);
58
 
static void print_certificate_info (gnutls_x509_crt crt, FILE *out,
 
59
static void print_certificate_info (gnutls_x509_crt_t crt, FILE *out,
59
60
                                    unsigned int);
60
61
static void gaa_parser (int argc, char **argv);
61
62
void generate_self_signed (void);
62
63
void generate_request (void);
63
 
gnutls_x509_crt *load_cert_list (int mand, int *size);
 
64
gnutls_x509_crt_t *load_cert_list (int mand, int *size);
64
65
 
65
66
static gaainfo info;
66
67
FILE *outfile;
67
68
FILE *infile;
68
 
static int in_cert_format;
69
 
static int out_cert_format;
70
69
gnutls_digest_algorithm_t dig = GNUTLS_DIG_SHA1;
71
70
 
72
71
#define UNKNOWN "Unknown"
87
86
int
88
87
main (int argc, char **argv)
89
88
{
 
89
  set_program_name (argv[0]);
90
90
  cfg_init ();
91
91
  gaa_parser (argc, argv);
92
92
 
114
114
  return buf;
115
115
}
116
116
 
117
 
 
118
 
 
119
 
static gnutls_x509_privkey
 
117
static gnutls_x509_privkey_t
120
118
generate_private_key_int (void)
121
119
{
122
 
  gnutls_x509_privkey key;
 
120
  gnutls_x509_privkey_t key;
123
121
  int ret, key_type;
124
 
  const char *msg;
125
122
 
126
123
  if (info.dsa)
127
124
    {
128
 
      msg = "DSA";
129
125
      key_type = GNUTLS_PK_DSA;
130
126
 
131
127
      if (info.bits > 1024)
132
 
        {
133
 
          fprintf (stderr,
134
 
                   "The DSA algorithm cannot be used with primes over 1024 bits.\n");
135
 
          exit (1);
136
 
        }
 
128
        error (EXIT_FAILURE, 0, "--dsa is incompatible with --bits > 1024");
137
129
    }
138
130
  else
139
 
    {
140
 
      msg = "RSA";
141
 
      key_type = GNUTLS_PK_RSA;
142
 
    }
143
 
 
144
 
 
145
 
  if (info.privkey)
146
 
    return load_private_key (1);
 
131
    key_type = GNUTLS_PK_RSA;
147
132
 
148
133
  ret = gnutls_x509_privkey_init (&key);
149
134
  if (ret < 0)
150
 
    {
151
 
      fprintf (stderr, "privkey_init: %s\n", gnutls_strerror (ret));
152
 
      exit (1);
153
 
    }
 
135
    error (EXIT_FAILURE, 0, "privkey_init: %s", gnutls_strerror (ret));
154
136
 
155
 
  fprintf (stderr, "Generating a %d bit %s private key...\n", info.bits, msg);
 
137
  fprintf (stderr, "Generating a %d bit %s private key...\n", info.bits,
 
138
           gnutls_pk_algorithm_get_name (key_type));
156
139
 
157
140
  ret = gnutls_x509_privkey_generate (key, key_type, info.bits, 0);
158
141
  if (ret < 0)
159
 
    {
160
 
      fprintf (stderr, "privkey_generate: %s\n", gnutls_strerror (ret));
161
 
      exit (1);
162
 
    }
 
142
    error (EXIT_FAILURE, 0, "privkey_generate: %s", gnutls_strerror (ret));
163
143
 
164
144
  return key;
165
 
 
166
 
}
167
 
 
168
 
static void
169
 
print_key_usage (unsigned int x, FILE *out)
170
 
{
171
 
  if (x & GNUTLS_KEY_DIGITAL_SIGNATURE)
172
 
    fprintf (out, "\t\tDigital signature.\n");
173
 
  if (x & GNUTLS_KEY_NON_REPUDIATION)
174
 
    fprintf (out, "\t\tNon repudiation.\n");
175
 
  if (x & GNUTLS_KEY_KEY_ENCIPHERMENT)
176
 
    fprintf (out, "\t\tKey encipherment.\n");
177
 
  if (x & GNUTLS_KEY_DATA_ENCIPHERMENT)
178
 
    fprintf (out, "\t\tData encipherment.\n");
179
 
  if (x & GNUTLS_KEY_KEY_AGREEMENT)
180
 
    fprintf (out, "\t\tKey agreement.\n");
181
 
  if (x & GNUTLS_KEY_KEY_CERT_SIGN)
182
 
    fprintf (out, "\t\tCertificate signing.\n");
183
 
  if (x & GNUTLS_KEY_CRL_SIGN)
184
 
    fprintf (out, "\t\tCRL signing.\n");
185
 
  if (x & GNUTLS_KEY_ENCIPHER_ONLY)
186
 
    fprintf (out, "\t\tKey encipher only.\n");
187
 
  if (x & GNUTLS_KEY_DECIPHER_ONLY)
188
 
    fprintf (out, "\t\tKey decipher only.\n");
189
 
}
190
 
 
191
 
static void
192
 
print_key_purpose (const char *x, FILE *out)
193
 
{
194
 
  if (strcasecmp (x, GNUTLS_KP_TLS_WWW_SERVER) == 0)
195
 
    fprintf (out, "\t\tTLS WWW Server.\n");
196
 
  else if (strcasecmp (x, GNUTLS_KP_TLS_WWW_CLIENT) == 0)
197
 
    fprintf (out, "\t\tTLS WWW Client.\n");
198
 
  else if (strcasecmp (x, GNUTLS_KP_CODE_SIGNING) == 0)
199
 
    fprintf (out, "\t\tCode signing.\n");
200
 
  else if (strcasecmp (x, GNUTLS_KP_EMAIL_PROTECTION) == 0)
201
 
    fprintf (out, "\t\tEmail protection.\n");
202
 
  else if (strcasecmp (x, GNUTLS_KP_TIME_STAMPING) == 0)
203
 
    fprintf (out, "\t\tTime stamping.\n");
204
 
  else if (strcasecmp (x, GNUTLS_KP_OCSP_SIGNING) == 0)
205
 
    fprintf (out, "\t\tOCSP signing.\n");
206
 
  else if (strcasecmp (x, GNUTLS_KP_ANY) == 0)
207
 
    fprintf (out, "\t\tAny purpose.\n");
208
 
  else
209
 
    fprintf (out, "\t\t%s\n", x);
210
 
}
211
 
 
212
 
 
213
 
 
214
 
static void
215
 
print_private_key (gnutls_x509_privkey key)
 
145
}
 
146
 
 
147
static void
 
148
print_private_key (gnutls_x509_privkey_t key)
216
149
{
217
150
  int ret;
218
151
  size_t size;
223
156
  if (!info.pkcs8)
224
157
    {
225
158
      size = sizeof (buffer);
226
 
      ret = gnutls_x509_privkey_export (key, out_cert_format, buffer, &size);
 
159
      ret = gnutls_x509_privkey_export (key, info.outcert_format,
 
160
                                        buffer, &size);
227
161
      if (ret < 0)
228
 
        {
229
 
          fprintf (stderr, "privkey_export: %s\n", gnutls_strerror (ret));
230
 
          exit (1);
231
 
        }
 
162
        error (EXIT_FAILURE, 0, "privkey_export: %s", gnutls_strerror (ret));
232
163
    }
233
164
  else
234
165
    {
245
176
 
246
177
      size = sizeof (buffer);
247
178
      ret =
248
 
        gnutls_x509_privkey_export_pkcs8 (key, out_cert_format, pass,
 
179
        gnutls_x509_privkey_export_pkcs8 (key, info.outcert_format, pass,
249
180
                                          flags, buffer, &size);
250
181
      if (ret < 0)
251
 
        {
252
 
          fprintf (stderr, "privkey_export_pkcs8: %s\n",
253
 
                   gnutls_strerror (ret));
254
 
          exit (1);
255
 
        }
 
182
        error (EXIT_FAILURE, 0, "privkey_export_pkcs8: %s",
 
183
               gnutls_strerror (ret));
256
184
    }
257
185
 
258
186
  fwrite (buffer, 1, size, outfile);
261
189
void
262
190
generate_private_key (void)
263
191
{
264
 
  gnutls_x509_privkey key;
265
 
 
266
 
  fprintf (stderr, "Generating a private key...\n");
 
192
  gnutls_x509_privkey_t key;
267
193
 
268
194
  key = generate_private_key_int ();
269
195
 
273
199
}
274
200
 
275
201
 
276
 
gnutls_x509_crt
277
 
generate_certificate (gnutls_x509_privkey * ret_key, gnutls_x509_crt ca_crt)
 
202
gnutls_x509_crt_t
 
203
generate_certificate (gnutls_x509_privkey_t * ret_key,
 
204
                      gnutls_x509_crt_t ca_crt,
 
205
                      int proxy)
278
206
{
279
 
  gnutls_x509_crt crt;
280
 
  gnutls_x509_privkey key = NULL;
 
207
  gnutls_x509_crt_t crt;
 
208
  gnutls_x509_privkey_t key = NULL;
281
209
  size_t size;
282
210
  int ret;
283
211
  int serial, client;
284
 
  int days, result, ca_status;
 
212
  int days, result, ca_status = 0, path_len;
285
213
  const char *str;
286
214
  int vers = 3;                 /* the default version in the certificate 
287
215
                                 */
288
216
  unsigned int usage = 0, server;
289
 
  gnutls_x509_crq crq;          /* request */
 
217
  gnutls_x509_crq_t crq;                /* request */
290
218
 
291
 
 ret = gnutls_x509_crt_init (&crt);
 
219
  ret = gnutls_x509_crt_init (&crt);
292
220
  if (ret < 0)
293
 
    {
294
 
      fprintf (stderr, "crt_init: %s\n", gnutls_strerror (ret));
295
 
      exit (1);
296
 
    }
297
 
 
 
221
    error (EXIT_FAILURE, 0, "crt_init: %s", gnutls_strerror (ret));
298
222
 
299
223
  crq = load_request ();
300
224
 
310
234
 
311
235
      /* set the DN.
312
236
       */
313
 
      get_country_crt_set (crt);
314
 
      get_organization_crt_set (crt);
315
 
      get_unit_crt_set (crt);
316
 
      get_locality_crt_set (crt);
317
 
      get_state_crt_set (crt);
318
 
      get_cn_crt_set (crt);
319
 
      get_uid_crt_set (crt);
320
 
      get_oid_crt_set (crt);
321
 
 
322
 
      if (!batch)
323
 
        fprintf (stderr,
324
 
                 "This field should not be used in new certificates.\n");
325
 
 
326
 
      get_pkcs9_email_crt_set (crt);
 
237
      if (proxy)
 
238
        {
 
239
          result = gnutls_x509_crt_set_proxy_dn (crt, ca_crt, 0, NULL, 0);
 
240
          if (result < 0)
 
241
              error (EXIT_FAILURE, 0, "set_proxy_dn: %s",
 
242
                     gnutls_strerror (result));
 
243
 
 
244
          get_cn_crt_set (crt);
 
245
        }
 
246
      else
 
247
        {
 
248
          get_country_crt_set (crt);
 
249
          get_organization_crt_set (crt);
 
250
          get_unit_crt_set (crt);
 
251
          get_locality_crt_set (crt);
 
252
          get_state_crt_set (crt);
 
253
          get_cn_crt_set (crt);
 
254
          get_uid_crt_set (crt);
 
255
          get_oid_crt_set (crt);
 
256
 
 
257
          if (!batch)
 
258
            fprintf (stderr,
 
259
                     "This field should not be used in new certificates.\n");
 
260
 
 
261
          get_pkcs9_email_crt_set (crt);
 
262
        }
327
263
 
328
264
      result = gnutls_x509_crt_set_key (crt, key);
329
265
      if (result < 0)
330
 
        {
331
 
          fprintf (stderr, "set_key: %s\n", gnutls_strerror (result));
332
 
          exit (1);
333
 
        }
334
 
 
 
266
        error (EXIT_FAILURE, 0, "set_key: %s", gnutls_strerror (result));
335
267
    }
336
268
  else
337
269
    {
338
270
      result = gnutls_x509_crt_set_crq (crt, crq);
339
271
      if (result < 0)
340
 
        {
341
 
          fprintf (stderr, "set_crq: %s\n", gnutls_strerror (result));
342
 
          exit (1);
343
 
        }
 
272
        error (EXIT_FAILURE, 0, "set_crq: %s", gnutls_strerror (result));
344
273
    }
345
274
 
346
275
 
347
276
  serial = get_serial ();
348
 
  buffer[3] = serial & 0xff;
349
 
  buffer[2] = (serial >> 8) & 0xff;
350
 
  buffer[1] = (serial >> 16) & 0xff;
 
277
  buffer[4] = serial & 0xff;
 
278
  buffer[3] = (serial >> 8) & 0xff;
 
279
  buffer[2] = (serial >> 16) & 0xff;
 
280
  buffer[1] = (serial >> 24) & 0xff;
351
281
  buffer[0] = 0;
352
282
 
353
 
  result = gnutls_x509_crt_set_serial (crt, buffer, 4);
 
283
  result = gnutls_x509_crt_set_serial (crt, buffer, 5);
354
284
  if (result < 0)
355
 
    {
356
 
      fprintf (stderr, "serial: %s\n", gnutls_strerror (result));
357
 
      exit (1);
358
 
    }
359
 
 
 
285
    error (EXIT_FAILURE, 0, "serial: %s", gnutls_strerror (result));
360
286
 
361
287
  if (!batch)
362
288
    fprintf (stderr, "\n\nActivation/Expiration time.\n");
369
295
    gnutls_x509_crt_set_expiration_time (crt,
370
296
                                         time (NULL) + days * 24 * 60 * 60);
371
297
  if (result < 0)
372
 
    {
373
 
      fprintf (stderr, "set_expiration: %s\n", gnutls_strerror (result));
374
 
      exit (1);
375
 
    }
376
 
 
 
298
    error (EXIT_FAILURE, 0, "set_expiration: %s", gnutls_strerror (result));
377
299
 
378
300
  if (!batch)
379
301
    fprintf (stderr, "\n\nExtensions.\n");
380
302
 
381
 
  ca_status = get_ca_status ();
382
 
 
383
 
  result = gnutls_x509_crt_set_ca_status (crt, ca_status);
384
 
  if (result < 0)
 
303
  if (proxy)
385
304
    {
386
 
      fprintf (stderr, "ca_status: %s\n", gnutls_strerror (result));
387
 
      exit (1);
 
305
      const char *policylanguage;
 
306
      char *policy;
 
307
      size_t policylen;
 
308
      int proxypathlen = get_path_len ();
 
309
 
 
310
      if (!batch)
 
311
        {
 
312
          printf ("1.3.6.1.5.5.7.21.1 ::= id-ppl-inheritALL\n");
 
313
          printf ("1.3.6.1.5.5.7.21.2 ::= id-ppl-independent\n");
 
314
        }
 
315
 
 
316
      policylanguage = get_proxy_policy (&policy, &policylen);
 
317
 
 
318
      result = gnutls_x509_crt_set_proxy (crt, proxypathlen, policylanguage,
 
319
                                          policy, policylen);
 
320
      if (result < 0)
 
321
        error (EXIT_FAILURE, 0, "set_proxy: %s", gnutls_strerror (result));
388
322
    }
389
323
 
 
324
  if (!proxy)
 
325
    ca_status = get_ca_status ();
 
326
  if (ca_status)
 
327
    path_len = get_path_len ();
 
328
  else
 
329
    path_len = -1;
 
330
 
 
331
  result = gnutls_x509_crt_set_basic_constraints (crt, ca_status, path_len);
 
332
  if (result < 0)
 
333
    error (EXIT_FAILURE, 0, "basic_constraints: %s", gnutls_strerror (result));
 
334
 
390
335
  client = get_tls_client_status ();
391
336
  if (client != 0)
392
337
    {
393
 
      result =
394
 
        gnutls_x509_crt_set_key_purpose_oid (crt,
395
 
                                             GNUTLS_KP_TLS_WWW_CLIENT, 0);
 
338
      result = gnutls_x509_crt_set_key_purpose_oid (crt,
 
339
                                                    GNUTLS_KP_TLS_WWW_CLIENT,
 
340
                                                    0);
396
341
      if (result < 0)
397
 
        {
398
 
          fprintf (stderr, "key_kp: %s\n", gnutls_strerror (result));
399
 
          exit (1);
400
 
        }
 
342
        error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
401
343
    }
402
344
 
403
345
  server = get_tls_server_status ();
405
347
    {
406
348
      result = 0;
407
349
 
408
 
      str = get_dns_name ();
409
 
      if (str != NULL)
410
 
        {
411
 
          result =
412
 
            gnutls_x509_crt_set_subject_alternative_name (crt,
413
 
                                                          GNUTLS_SAN_DNSNAME,
414
 
                                                          str);
415
 
        }
416
 
      else
417
 
        {
418
 
          str = get_ip_addr ();
 
350
      if (!proxy)
 
351
        {
 
352
          str = get_dns_name ();
419
353
          if (str != NULL)
420
354
            {
421
 
              result =
422
 
                gnutls_x509_crt_set_subject_alternative_name (crt,
423
 
                                                              GNUTLS_SAN_IPADDRESS,
424
 
                                                              str);
425
 
            }
426
 
        }
 
355
              result = gnutls_x509_crt_set_subject_alternative_name
 
356
                (crt, GNUTLS_SAN_DNSNAME, str);
 
357
            }
 
358
          else
 
359
            {
 
360
              str = get_ip_addr ();
 
361
              if (str != NULL)
 
362
                {
 
363
                  result = gnutls_x509_crt_set_subject_alternative_name
 
364
                    (crt, GNUTLS_SAN_IPADDRESS, str);
 
365
                }
 
366
            }
427
367
 
428
 
      if (result < 0)
429
 
        {
430
 
          fprintf (stderr, "subject_alt_name: %s\n",
 
368
          if (result < 0)
 
369
            error (EXIT_FAILURE, 0, "subject_alt_name: %s",
431
370
                   gnutls_strerror (result));
432
 
          exit (1);
433
371
        }
434
372
 
435
373
      result =
436
374
        gnutls_x509_crt_set_key_purpose_oid (crt,
437
375
                                             GNUTLS_KP_TLS_WWW_SERVER, 0);
438
376
      if (result < 0)
439
 
        {
440
 
          fprintf (stderr, "key_kp: %s\n", gnutls_strerror (result));
441
 
          exit (1);
442
 
        }
443
 
 
 
377
        error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
444
378
    }
445
 
  else
 
379
  else if (!proxy)
446
380
    {
447
 
 
448
381
      str = get_email ();
449
382
 
450
383
      if (str != NULL)
451
384
        {
452
 
          result =
453
 
            gnutls_x509_crt_set_subject_alternative_name (crt,
454
 
                                                          GNUTLS_SAN_RFC822NAME,
455
 
                                                          str);
 
385
          result = gnutls_x509_crt_set_subject_alternative_name
 
386
            (crt, GNUTLS_SAN_RFC822NAME, str);
456
387
          if (result < 0)
457
 
            {
458
 
              fprintf (stderr, "subject_alt_name: %s\n",
459
 
                       gnutls_strerror (result));
460
 
              exit (1);
461
 
            }
 
388
            error (EXIT_FAILURE, 0, "subject_alt_name: %s",
 
389
                   gnutls_strerror (result));
462
390
        }
463
391
    }
464
392
 
503
431
            gnutls_x509_crt_set_key_purpose_oid (crt,
504
432
                                                 GNUTLS_KP_CODE_SIGNING, 0);
505
433
          if (result < 0)
506
 
            {
507
 
              fprintf (stderr, "key_kp: %s\n", gnutls_strerror (result));
508
 
              exit (1);
509
 
            }
 
434
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
510
435
        }
511
436
 
512
437
      result = get_ocsp_sign_status ();
516
441
            gnutls_x509_crt_set_key_purpose_oid (crt,
517
442
                                                 GNUTLS_KP_OCSP_SIGNING, 0);
518
443
          if (result < 0)
519
 
            {
520
 
              fprintf (stderr, "key_kp: %s\n", gnutls_strerror (result));
521
 
              exit (1);
522
 
            }
 
444
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
523
445
        }
524
446
 
525
447
      result = get_time_stamp_status ();
529
451
            gnutls_x509_crt_set_key_purpose_oid (crt,
530
452
                                                 GNUTLS_KP_TIME_STAMPING, 0);
531
453
          if (result < 0)
532
 
            {
533
 
              fprintf (stderr, "key_kp: %s\n", gnutls_strerror (result));
534
 
              exit (1);
535
 
            }
 
454
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
536
455
        }
537
456
    }
538
457
 
540
459
    {
541
460
      result = gnutls_x509_crt_set_key_usage (crt, usage);
542
461
      if (result < 0)
543
 
        {
544
 
          fprintf (stderr, "key_usage: %s\n", gnutls_strerror (result));
545
 
          exit (1);
546
 
        }
 
462
        error (EXIT_FAILURE, 0, "key_usage: %s", gnutls_strerror (result));
547
463
    }
548
464
 
549
465
  /* Version.
550
466
   */
551
467
  result = gnutls_x509_crt_set_version (crt, vers);
552
468
  if (result < 0)
553
 
    {
554
 
      fprintf (stderr, "set_version: %s\n", gnutls_strerror (result));
555
 
      exit (1);
556
 
    }
 
469
    error (EXIT_FAILURE, 0, "set_version: %s", gnutls_strerror (result));
557
470
 
558
471
  /* Subject Key ID.
559
472
   */
563
476
    {
564
477
      result = gnutls_x509_crt_set_subject_key_id (crt, buffer, size);
565
478
      if (result < 0)
566
 
        {
567
 
          fprintf (stderr, "set_subject_key_id: %s\n",
568
 
                   gnutls_strerror (result));
569
 
          exit (1);
570
 
        }
 
479
        error (EXIT_FAILURE, 0, "set_subject_key_id: %s",
 
480
               gnutls_strerror (result));
571
481
    }
572
482
 
573
483
  /* Authority Key ID.
575
485
  if (ca_crt != NULL)
576
486
    {
577
487
      size = sizeof (buffer);
578
 
      result = gnutls_x509_crt_get_key_id (ca_crt, 0, buffer, &size);
 
488
      result = gnutls_x509_crt_get_subject_key_id (ca_crt, buffer,
 
489
                                                   &size, NULL);
 
490
      if (result < 0)
 
491
        {
 
492
          size = sizeof (buffer);
 
493
          result = gnutls_x509_crt_get_key_id (ca_crt, 0, buffer, &size);
 
494
        }
579
495
      if (result >= 0)
580
496
        {
581
497
          result = gnutls_x509_crt_set_authority_key_id (crt, buffer, size);
582
498
          if (result < 0)
583
 
            {
584
 
              fprintf (stderr, "set_authority_key_id: %s\n",
585
 
                       gnutls_strerror (result));
586
 
              exit (1);
587
 
            }
 
499
            error (EXIT_FAILURE, 0, "set_authority_key_id: %s",
 
500
                   gnutls_strerror (result));
588
501
        }
589
502
    }
590
503
 
593
506
 
594
507
}
595
508
 
596
 
gnutls_x509_crl
 
509
gnutls_x509_crl_t
597
510
generate_crl (void)
598
511
{
599
 
  gnutls_x509_crl crl;
600
 
  gnutls_x509_crt *crts;
 
512
  gnutls_x509_crl_t crl;
 
513
  gnutls_x509_crt_t *crts;
601
514
  int size;
602
515
  int days, result, i;
603
 
  int vers = 2;                 /* the default version in the CRL
604
 
                                 */
 
516
  time_t now = time (NULL);
605
517
 
606
518
  result = gnutls_x509_crl_init (&crl);
607
519
  if (result < 0)
608
 
    {
609
 
      fprintf (stderr, "crl_init: %s\n", gnutls_strerror (result));
610
 
      exit (1);
611
 
    }
 
520
    error (EXIT_FAILURE, 0, "crl_init: %s", gnutls_strerror (result));
612
521
 
613
 
  crts = load_cert_list (1, &size);
 
522
  crts = load_cert_list (0, &size);
614
523
 
615
524
  for (i = 0; i < size; i++)
616
525
    {
617
 
 
618
 
      result = gnutls_x509_crl_set_crt (crl, crts[i], time (0));
 
526
      result = gnutls_x509_crl_set_crt (crl, crts[i], now);
619
527
      if (result < 0)
620
 
        {
621
 
          fprintf (stderr, "crl_set_crt: %s\n", gnutls_strerror (result));
622
 
          exit (1);
623
 
        }
 
528
        error (EXIT_FAILURE, 0, "crl_set_crt: %s", gnutls_strerror (result));
624
529
    }
625
530
 
626
 
  fprintf (stderr, "\n\nthisUpdate/nextUpdate time.\n");
627
 
  gnutls_x509_crl_set_this_update (crl, time (NULL));
 
531
  result = gnutls_x509_crl_set_this_update (crl, now);
 
532
  if (result < 0)
 
533
    error (EXIT_FAILURE, 0, "this_update: %s", gnutls_strerror (result));
628
534
 
 
535
  fprintf (stderr, "Update times.\n");
629
536
  days = get_crl_next_update ();
630
537
 
631
 
  result =
632
 
    gnutls_x509_crl_set_next_update (crl, time (NULL) + days * 24 * 60 * 60);
 
538
  result = gnutls_x509_crl_set_next_update (crl, now + days * 24 * 60 * 60);
633
539
  if (result < 0)
634
 
    {
635
 
      fprintf (stderr, "next_update: %s\n", gnutls_strerror (result));
636
 
      exit (1);
637
 
    }
 
540
    error (EXIT_FAILURE, 0, "next_update: %s", gnutls_strerror (result));
638
541
 
639
 
  /* Version.
640
 
   */
641
 
  result = gnutls_x509_crl_set_version (crl, vers);
 
542
  result = gnutls_x509_crl_set_version (crl, 2);
642
543
  if (result < 0)
643
 
    {
644
 
      fprintf (stderr, "set_version: %s\n", gnutls_strerror (result));
645
 
      exit (1);
646
 
    }
 
544
    error (EXIT_FAILURE, 0, "set_version: %s", gnutls_strerror (result));
647
545
 
648
546
  return crl;
649
547
}
650
548
 
651
 
gnutls_x509_crt
652
 
update_certificate (void)
653
 
{
654
 
  gnutls_x509_crt crt;
655
 
  int size;
656
 
  int days, result;
657
 
 
658
 
  size = gnutls_x509_crt_init (&crt);
659
 
  if (size < 0)
660
 
    {
661
 
      fprintf (stderr, "crt_init: %s\n", gnutls_strerror (size));
662
 
      exit (1);
663
 
    }
664
 
 
665
 
  crt = load_cert (1);
666
 
 
667
 
  fprintf (stderr, "Activation/Expiration time.\n");
668
 
  gnutls_x509_crt_set_activation_time (crt, time (NULL));
669
 
 
670
 
  days = get_days ();
671
 
 
672
 
  result =
673
 
    gnutls_x509_crt_set_expiration_time (crt,
674
 
                                         time (NULL) + days * 24 * 60 * 60);
675
 
  if (result < 0)
676
 
    {
677
 
      fprintf (stderr, "set_expiration: %s\n", gnutls_strerror (result));
678
 
      exit (1);
679
 
    }
680
 
 
681
 
  return crt;
682
 
 
683
 
}
684
 
 
685
 
 
686
549
void
687
550
generate_self_signed (void)
688
551
{
689
 
  gnutls_x509_crt crt;
690
 
  gnutls_x509_privkey key;
 
552
  gnutls_x509_crt_t crt;
 
553
  gnutls_x509_privkey_t key;
691
554
  size_t size;
692
555
  int result;
693
556
  const char *uri;
694
557
 
695
558
  fprintf (stderr, "Generating a self signed certificate...\n");
696
559
 
697
 
  crt = generate_certificate (&key, NULL);
 
560
  crt = generate_certificate (&key, NULL, 0);
698
561
 
699
562
  if (!key)
700
563
    key = load_private_key (1);
706
569
                                                    uri,
707
570
                                                    0 /* all reasons */ );
708
571
      if (result < 0)
709
 
        {
710
 
          fprintf (stderr, "crl_dist_points: %s\n", gnutls_strerror (result));
711
 
          exit (1);
712
 
        }
 
572
        error (EXIT_FAILURE, 0, "crl_dist_points: %s",
 
573
               gnutls_strerror (result));
713
574
    }
714
575
 
715
576
  print_certificate_info (crt, stderr, 0);
718
579
 
719
580
  result = gnutls_x509_crt_sign2 (crt, crt, key, dig, 0);
720
581
  if (result < 0)
721
 
    {
722
 
      fprintf (stderr, "crt_sign: %s\n", gnutls_strerror (result));
723
 
      exit (1);
724
 
    }
 
582
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));
725
583
 
726
584
  size = sizeof (buffer);
727
 
  result = gnutls_x509_crt_export (crt, out_cert_format, buffer, &size);
 
585
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
728
586
  if (result < 0)
729
 
    {
730
 
      fprintf (stderr, "crt_export: %s\n", gnutls_strerror (result));
731
 
      exit (1);
732
 
    }
 
587
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));
733
588
 
734
589
  fwrite (buffer, 1, size, outfile);
735
590
 
740
595
void
741
596
generate_signed_certificate (void)
742
597
{
743
 
  gnutls_x509_crt crt;
744
 
  gnutls_x509_privkey key;
 
598
  gnutls_x509_crt_t crt;
 
599
  gnutls_x509_privkey_t key;
745
600
  size_t size;
746
601
  int result;
747
 
  gnutls_x509_privkey ca_key;
748
 
  gnutls_x509_crt ca_crt;
 
602
  gnutls_x509_privkey_t ca_key;
 
603
  gnutls_x509_crt_t ca_crt;
749
604
 
750
605
  fprintf (stderr, "Generating a signed certificate...\n");
751
606
 
752
607
  ca_key = load_ca_private_key ();
753
608
  ca_crt = load_ca_cert ();
754
609
 
755
 
  crt = generate_certificate (&key, ca_crt);
 
610
  crt = generate_certificate (&key, ca_crt, 0);
756
611
 
757
612
  /* Copy the CRL distribution points.
758
613
   */
766
621
 
767
622
  result = gnutls_x509_crt_sign2 (crt, ca_crt, ca_key, dig, 0);
768
623
  if (result < 0)
769
 
    {
770
 
      fprintf (stderr, "crt_sign: %s\n", gnutls_strerror (result));
771
 
      exit (1);
772
 
    }
773
 
 
774
 
  size = sizeof (buffer);
775
 
  result = gnutls_x509_crt_export (crt, out_cert_format, buffer, &size);
776
 
  if (result < 0)
777
 
    {
778
 
      fprintf (stderr, "crt_export: %s\n", gnutls_strerror (result));
779
 
      exit (1);
780
 
    }
 
624
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));
 
625
 
 
626
  size = sizeof (buffer);
 
627
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
 
628
  if (result < 0)
 
629
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));
 
630
 
 
631
  fwrite (buffer, 1, size, outfile);
 
632
 
 
633
  gnutls_x509_crt_deinit (crt);
 
634
  gnutls_x509_privkey_deinit (key);
 
635
}
 
636
 
 
637
void
 
638
generate_proxy_certificate (void)
 
639
{
 
640
  gnutls_x509_crt_t crt, eecrt;
 
641
  gnutls_x509_privkey_t key, eekey;
 
642
  size_t size;
 
643
  int result;
 
644
 
 
645
  fprintf (stderr, "Generating a proxy certificate...\n");
 
646
 
 
647
  eekey = load_ca_private_key ();
 
648
  eecrt = load_cert (1);
 
649
 
 
650
  crt = generate_certificate (&key, eecrt, 1);
 
651
 
 
652
  print_certificate_info (crt, stderr, 0);
 
653
 
 
654
  fprintf (stderr, "\n\nSigning certificate...\n");
 
655
 
 
656
  result = gnutls_x509_crt_sign2 (crt, eecrt, eekey, dig, 0);
 
657
  if (result < 0)
 
658
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));
 
659
 
 
660
  size = sizeof (buffer);
 
661
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
 
662
  if (result < 0)
 
663
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));
781
664
 
782
665
  fwrite (buffer, 1, size, outfile);
783
666
 
788
671
void
789
672
generate_signed_crl (void)
790
673
{
791
 
  gnutls_x509_crl crl;
792
 
  size_t size;
 
674
  gnutls_x509_crl_t crl;
793
675
  int result;
794
 
  gnutls_x509_privkey ca_key;
795
 
  gnutls_x509_crt ca_crt;
 
676
  gnutls_x509_privkey_t ca_key;
 
677
  gnutls_x509_crt_t ca_crt;
796
678
 
797
679
  fprintf (stderr, "Generating a signed CRL...\n");
798
680
 
799
681
  ca_key = load_ca_private_key ();
800
682
  ca_crt = load_ca_cert ();
801
 
 
802
683
  crl = generate_crl ();
803
684
 
804
 
  print_crl_info (crl, stderr, 0);
805
 
 
806
 
  fprintf (stderr, "\n\nSigning CRL...\n");
 
685
  fprintf (stderr, "\n");
807
686
 
808
687
  result = gnutls_x509_crl_sign (crl, ca_crt, ca_key);
809
688
  if (result < 0)
810
 
    {
811
 
      fprintf (stderr, "crl_sign: %s\n", gnutls_strerror (result));
812
 
      exit (1);
813
 
    }
814
 
 
815
 
  size = sizeof (buffer);
816
 
  result = gnutls_x509_crl_export (crl, out_cert_format, buffer, &size);
817
 
  if (result < 0)
818
 
    {
819
 
      fprintf (stderr, "crl_export: %s\n", gnutls_strerror (result));
820
 
      exit (1);
821
 
    }
822
 
 
823
 
  fwrite (buffer, 1, size, outfile);
 
689
    error (EXIT_FAILURE, 0, "crl_sign: %s", gnutls_strerror (result));
 
690
 
 
691
  print_crl_info (crl, stderr);
824
692
 
825
693
  gnutls_x509_crl_deinit (crl);
826
694
}
828
696
void
829
697
update_signed_certificate (void)
830
698
{
831
 
  gnutls_x509_crt crt;
 
699
  gnutls_x509_crt_t crt;
832
700
  size_t size;
833
701
  int result;
834
 
  gnutls_x509_privkey ca_key;
835
 
  gnutls_x509_crt ca_crt;
 
702
  gnutls_x509_privkey_t ca_key;
 
703
  gnutls_x509_crt_t ca_crt;
 
704
  int days;
 
705
  time_t tim = time (NULL);
836
706
 
837
707
  fprintf (stderr, "Generating a signed certificate...\n");
838
708
 
839
709
  ca_key = load_ca_private_key ();
840
710
  ca_crt = load_ca_cert ();
841
 
 
842
 
  crt = update_certificate ();
 
711
  crt = load_cert (1);
 
712
 
 
713
  fprintf (stderr, "Activation/Expiration time.\n");
 
714
  gnutls_x509_crt_set_activation_time (crt, tim);
 
715
 
 
716
  days = get_days ();
 
717
 
 
718
  result = gnutls_x509_crt_set_expiration_time (crt, tim + days * 24 * 60 * 60);
 
719
  if (result < 0)
 
720
    error (EXIT_FAILURE, 0, "set_expiration: %s", gnutls_strerror (result));
843
721
 
844
722
  fprintf (stderr, "\n\nSigning certificate...\n");
845
723
 
846
724
  result = gnutls_x509_crt_sign2 (crt, ca_crt, ca_key, dig, 0);
847
725
  if (result < 0)
848
 
    {
849
 
      fprintf (stderr, "crt_sign: %s\n", gnutls_strerror (result));
850
 
      exit (1);
851
 
    }
 
726
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));
852
727
 
853
728
  size = sizeof (buffer);
854
 
  result = gnutls_x509_crt_export (crt, out_cert_format, buffer, &size);
 
729
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
855
730
  if (result < 0)
856
 
    {
857
 
      fprintf (stderr, "crt_export: %s\n", gnutls_strerror (result));
858
 
      exit (1);
859
 
    }
 
731
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));
860
732
 
861
733
  fwrite (buffer, 1, size, outfile);
862
734
 
870
742
 
871
743
  if (gaa (argc, argv, &info) != -1)
872
744
    {
873
 
      fprintf (stderr,
874
 
               "Error in the arguments. Use the --help or -h parameters to get more information.\n");
 
745
      fprintf (stderr, "Try `%s --help' for more information.\n",
 
746
               program_name);
875
747
      exit (1);
876
748
    }
877
749
 
879
751
    {
880
752
      outfile = fopen (info.outfile, "wb");
881
753
      if (outfile == NULL)
882
 
        {
883
 
          fprintf (stderr, "error: could not open '%s'.\n", info.outfile);
884
 
          exit (1);
885
 
        }
 
754
        error (EXIT_FAILURE, errno, "%s", info.outfile);
886
755
    }
887
756
  else
888
757
    outfile = stdout;
891
760
    {
892
761
      infile = fopen (info.infile, "rb");
893
762
      if (infile == NULL)
894
 
        {
895
 
          fprintf (stderr, "error: could not open '%s'.\n", info.infile);
896
 
          exit (1);
897
 
        }
 
763
        error (EXIT_FAILURE, errno, "%s", info.infile);
898
764
    }
899
765
  else
900
766
    infile = stdin;
901
767
 
902
768
  if (info.incert_format)
903
 
    in_cert_format = GNUTLS_X509_FMT_DER;
 
769
    info.incert_format = GNUTLS_X509_FMT_DER;
904
770
  else
905
 
    in_cert_format = GNUTLS_X509_FMT_PEM;
 
771
    info.incert_format = GNUTLS_X509_FMT_PEM;
906
772
 
907
773
  if (info.outcert_format)
908
 
    out_cert_format = GNUTLS_X509_FMT_DER;
 
774
    info.outcert_format = GNUTLS_X509_FMT_DER;
909
775
  else
910
 
    out_cert_format = GNUTLS_X509_FMT_PEM;
 
776
    info.outcert_format = GNUTLS_X509_FMT_PEM;
911
777
 
912
778
  if (info.hash != NULL)
913
779
    {
919
785
        }
920
786
      else if (strcasecmp (info.hash, "sha1") == 0)
921
787
        dig = GNUTLS_DIG_SHA1;
 
788
      else if (strcasecmp (info.hash, "sha256") == 0)
 
789
        dig = GNUTLS_DIG_SHA256;
 
790
      else if (strcasecmp (info.hash, "sha384") == 0)
 
791
        dig = GNUTLS_DIG_SHA384;
 
792
      else if (strcasecmp (info.hash, "sha512") == 0)
 
793
        dig = GNUTLS_DIG_SHA512;
922
794
      else if (strcasecmp (info.hash, "rmd160") == 0)
923
795
        dig = GNUTLS_DIG_RMD160;
924
796
      else
925
 
        fprintf (stderr,
926
 
                 "Unsupported hash algorithm '%s'. Using the default.\n",
927
 
                 info.hash);
 
797
        error (EXIT_FAILURE, 0, "invalid hash: %s", info.hash);
928
798
    }
929
799
 
930
800
  batch = 0;
938
808
  gnutls_global_set_log_level (info.debug);
939
809
 
940
810
  if ((ret = gnutls_global_init ()) < 0)
941
 
    {
942
 
      fprintf (stderr, "global_init: %s\n", gnutls_strerror (ret));
943
 
      exit (1);
944
 
    }
 
811
    error (EXIT_FAILURE, 0, "global_init: %s", gnutls_strerror (ret));
945
812
 
946
813
  switch (info.action)
947
814
    {
996
863
    case 15:
997
864
      smime_to_pkcs7 ();
998
865
      break;
 
866
    case 17:
 
867
      generate_proxy_certificate ();
 
868
      break;
999
869
    default:
1000
 
      fprintf (stderr, "GnuTLS' certtool utility.\n");
1001
 
      fprintf (stderr,
1002
 
               "Please use the --help to get help on this program.\n");
 
870
      gaa_help ();
1003
871
      exit (0);
1004
872
    }
1005
873
  fclose (outfile);
1006
874
}
1007
875
 
1008
 
 
1009
 
/* OIDs that are handled by the gnutls' functions.
1010
 
 */
1011
 
static inline int
1012
 
known_oid (const char *oid)
1013
 
{
1014
 
  if (strcmp (oid, "2.5.29.17") == 0 ||
1015
 
      strcmp (oid, "2.5.29.19") == 0 ||
1016
 
      strcmp (oid, "2.5.29.31") == 0 ||
1017
 
      strcmp (oid, "2.5.29.37") == 0 ||
1018
 
      strcmp (oid, "2.5.29.14") == 0 ||
1019
 
      strcmp (oid, "2.5.29.35") == 0 || strcmp (oid, "2.5.29.15") == 0)
1020
 
    return 1;
1021
 
 
1022
 
  return 0;
1023
 
}
1024
 
 
1025
 
#define MAX_CRTS 64
 
876
#define MAX_CRTS 500
1026
877
void
1027
878
certificate_info (void)
1028
879
{
1029
 
  gnutls_x509_crt crt[MAX_CRTS];
 
880
  gnutls_x509_crt_t crt[MAX_CRTS];
1030
881
  size_t size;
1031
882
  int ret, i, count;
1032
 
  gnutls_datum pem;
 
883
  gnutls_datum_t pem;
1033
884
  unsigned int crt_num;
1034
885
 
1035
 
  size = fread (buffer, 1, sizeof (buffer) - 1, infile);
1036
 
  buffer[size] = 0;
1037
 
 
1038
 
  pem.data = buffer;
 
886
  pem.data = fread_file (infile, &size);
1039
887
  pem.size = size;
1040
888
 
1041
889
  crt_num = MAX_CRTS;
1042
 
  ret =
1043
 
    gnutls_x509_crt_list_import (crt, &crt_num, &pem, in_cert_format,
1044
 
                                 GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
 
890
  ret = gnutls_x509_crt_list_import (crt, &crt_num, &pem, info.incert_format,
 
891
                                     GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
1045
892
  if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
1046
893
    {
1047
 
      fprintf (stderr,
1048
 
               "Too many certificates (%d), will only read the first %d.\n",
1049
 
               crt_num, MAX_CRTS);
 
894
      error (0, 0, "Too many certificates (%d), will only read the first %d.",
 
895
             crt_num, MAX_CRTS);
1050
896
      crt_num = MAX_CRTS;
1051
 
      ret =
1052
 
        gnutls_x509_crt_list_import (crt, &crt_num, &pem, in_cert_format, 0);
 
897
      ret = gnutls_x509_crt_list_import (crt, &crt_num, &pem,
 
898
                                         info.incert_format, 0);
1053
899
    }
1054
 
 
1055
900
  if (ret < 0)
1056
 
    {
1057
 
      fprintf (stderr, "Decoding error: %s\n", gnutls_strerror (ret));
1058
 
      exit (1);
1059
 
    }
 
901
    error (EXIT_FAILURE, 0, "Import error: %s", gnutls_strerror (ret));
 
902
 
 
903
  free (pem.data);
 
904
 
1060
905
  count = ret;
1061
906
 
1062
 
  if (count > 1 && out_cert_format == GNUTLS_X509_FMT_DER)
 
907
  if (count > 1 && info.outcert_format == GNUTLS_X509_FMT_DER)
1063
908
    {
1064
 
      fprintf(stderr, "Cannot output multiple certificates in DER format, using PEM instead.\n");
1065
 
      out_cert_format = GNUTLS_X509_FMT_PEM;
 
909
      error(0, 0, "Cannot output multiple certificates in DER format, using PEM instead.");
 
910
      info.outcert_format = GNUTLS_X509_FMT_PEM;
1066
911
    }
1067
 
    
 
912
 
1068
913
  for (i = 0; i < count; i++)
1069
914
    {
1070
 
      if (out_cert_format == GNUTLS_X509_FMT_PEM)
1071
 
        print_certificate_info (crt[i], outfile, 1);
 
915
      if (i > 0)
 
916
        fprintf (outfile, "\n");
 
917
 
 
918
      if (info.outcert_format == GNUTLS_X509_FMT_PEM)
 
919
        print_certificate_info (crt[i], outfile, 1);
1072
920
 
1073
921
      if (!info.xml)
1074
922
        {
1075
923
          size = sizeof (buffer);
1076
 
          ret =
1077
 
            gnutls_x509_crt_export (crt[i], out_cert_format, buffer,
1078
 
                                    &size);
 
924
          ret = gnutls_x509_crt_export (crt[i], info.outcert_format, buffer,
 
925
                                        &size);
1079
926
          if (ret < 0)
1080
 
            {
1081
 
              fprintf (stderr, "Encoding error: %s\n", gnutls_strerror (ret));
1082
 
              exit (1);
1083
 
            }
1084
 
          fprintf (outfile, "\n%s\n", buffer);
1085
 
 
 
927
            error (EXIT_FAILURE, 0, "Export error: %s",
 
928
                   gnutls_strerror (ret));
 
929
          fprintf (outfile, "%s", buffer);
1086
930
        }
1087
931
      else
1088
932
        {
1089
 
          gnutls_datum xml;
 
933
          gnutls_datum_t xml;
1090
934
 
1091
935
          ret = gnutls_x509_crt_to_xml (crt[i], &xml, GNUTLS_XML_SHOW_ALL);
1092
936
          if (ret < 0)
1093
 
            {
1094
 
              fprintf (stderr, "XML encoding error: %s\n",
1095
 
                       gnutls_strerror (ret));
1096
 
              exit (1);
1097
 
            }
 
937
            error (EXIT_FAILURE, 0, "XML encoding error: %s",
 
938
                   gnutls_strerror (ret));
1098
939
 
1099
940
          fprintf (outfile, "\n%s\n", xml.data);
1100
941
          gnutls_free (xml.data);
1101
942
        }
1102
943
    }
1103
 
 
1104
944
}
1105
945
 
1106
946
static void
1107
 
print_hex_datum (gnutls_datum * dat)
 
947
print_hex_datum (gnutls_datum_t * dat)
1108
948
{
1109
949
  unsigned int j;
1110
950
#define SPACE "\t"
1120
960
 
1121
961
 
1122
962
static void
1123
 
print_certificate_info (gnutls_x509_crt crt, FILE *out, unsigned int all)
 
963
print_certificate_info (gnutls_x509_crt_t crt, FILE *out, unsigned int all)
1124
964
{
 
965
  gnutls_datum_t info;
1125
966
  int ret;
1126
 
  unsigned int i, indx, j, version;
1127
 
  unsigned int critical, key_usage, bits;
1128
 
  time_t tim;
1129
 
  char serial[40];
1130
 
  size_t serial_size = sizeof (serial), dn_size, size;
1131
 
  const char *cprint;
1132
 
  char dn[256];
1133
 
  char oid[128] = "";
1134
 
  char old_oid[128] = "";
1135
 
 
1136
 
  fprintf (out, "\n\nX.509 certificate info:\n\n");
1137
 
 
1138
 
  version = gnutls_x509_crt_get_version (crt);
1139
 
  fprintf (out, "Version: %d\n", version);
1140
 
 
1141
 
  /* serial number
1142
 
   */
1143
 
  if (gnutls_x509_crt_get_serial (crt, serial, &serial_size) >= 0)
1144
 
    {
1145
 
      fprintf (out, "Serial Number (hex): %s\n",
1146
 
               raw_to_string (serial, serial_size));
1147
 
    }
1148
 
 
1149
 
  /* Subject
1150
 
   */
1151
 
  dn_size = sizeof (dn);
1152
 
  ret = gnutls_x509_crt_get_dn (crt, dn, &dn_size);
1153
 
  if (ret >= 0)
1154
 
    fprintf (out, "Subject: %s\n", dn);
1155
 
  else
1156
 
    fprintf (stderr, "get_dn: %s\n", gnutls_strerror (ret));
1157
 
 
1158
 
  /* Issuer
1159
 
   */
1160
 
  if (all)
1161
 
    {
1162
 
      dn_size = sizeof (dn);
1163
 
      ret = gnutls_x509_crt_get_issuer_dn (crt, dn, &dn_size);
1164
 
      if (ret >= 0)
1165
 
        fprintf (out, "Issuer: %s\n", dn);
1166
 
      else
1167
 
        fprintf (stderr, "get_issuer_dn: %s\n", gnutls_strerror (ret));
1168
 
 
1169
 
 
1170
 
      /* signature algorithm
1171
 
       */
1172
 
      fprintf (out, "Signature Algorithm: ");
1173
 
      ret = gnutls_x509_crt_get_signature_algorithm (crt);
1174
 
 
1175
 
      cprint = gnutls_sign_algorithm_get_name (ret);
1176
 
      if (cprint == NULL)
1177
 
        cprint = UNKNOWN;
1178
 
      fprintf (out, "%s\n", cprint);
1179
 
      if (ret == GNUTLS_SIGN_RSA_MD5 || ret == GNUTLS_SIGN_RSA_MD2)
1180
 
        {
1181
 
          fprintf (stderr,
1182
 
                   "Warning: certificate uses a broken signature algorithm that can be forged.\n");
1183
 
        }
1184
 
    }
1185
 
 
1186
 
  /* Validity
1187
 
   */
1188
 
  fprintf (out, "Validity:\n");
1189
 
 
1190
 
  tim = gnutls_x509_crt_get_activation_time (crt);
1191
 
  fprintf (out, "\tNot Before: %s", ctime (&tim));
1192
 
 
1193
 
  tim = gnutls_x509_crt_get_expiration_time (crt);
1194
 
  fprintf (out, "\tNot After: %s", ctime (&tim));
1195
 
 
1196
 
  /* Public key algorithm
1197
 
   */
1198
 
  fprintf (out, "Subject Public Key Info:\n");
1199
 
  ret = gnutls_x509_crt_get_pk_algorithm (crt, &bits);
1200
 
  fprintf (out, "\tPublic Key Algorithm: ");
1201
 
 
1202
 
  cprint = gnutls_pk_algorithm_get_name (ret);
1203
 
  if (cprint == NULL)
1204
 
    cprint = UNKNOWN;
1205
 
  fprintf (out, "%s", cprint);
1206
 
  if (ret >= 0 && bits)
1207
 
    fprintf (out, " (%u bits)", bits);
1208
 
  fprintf (out, "\n");
1209
 
 
1210
 
  /* Print the raw public keys    
1211
 
   */
1212
 
  if (all)
1213
 
    {
1214
 
      if (ret == GNUTLS_PK_RSA)
1215
 
        {
1216
 
          gnutls_datum m, e;
1217
 
 
1218
 
          ret = gnutls_x509_crt_get_pk_rsa_raw (crt, &m, &e);
1219
 
          if (ret < 0)
1220
 
            {
1221
 
              fprintf (stderr, "Error in key RSA data export: %s\n",
1222
 
                       gnutls_strerror (ret));
1223
 
            }
1224
 
 
1225
 
          fprintf (outfile, "modulus:");
1226
 
          print_hex_datum (&m);
1227
 
          fprintf (outfile, "public exponent:");
1228
 
          print_hex_datum (&e);
1229
 
 
1230
 
        }
1231
 
      else if (ret == GNUTLS_PK_DSA)
1232
 
        {
1233
 
          gnutls_datum p, q, g, y;
1234
 
 
1235
 
          ret = gnutls_x509_crt_get_pk_dsa_raw (crt, &p, &q, &g, &y);
1236
 
          if (ret < 0)
1237
 
            {
1238
 
              fprintf (stderr, "Error in key DSA data export: %s\n",
1239
 
                       gnutls_strerror (ret));
1240
 
            }
1241
 
 
1242
 
          fprintf (outfile, "public key:");
1243
 
          print_hex_datum (&y);
1244
 
          fprintf (outfile, "p:");
1245
 
          print_hex_datum (&p);
1246
 
          fprintf (outfile, "q:");
1247
 
          print_hex_datum (&q);
1248
 
          fprintf (outfile, "g:");
1249
 
          print_hex_datum (&g);
1250
 
        }
1251
 
    }
1252
 
 
1253
 
  if (version >= 3)
1254
 
    fprintf (out, "\nX.509 Extensions:\n");
1255
 
 
1256
 
  /* subject alternative name
1257
 
   */
1258
 
  for (i = 0; !(ret < 0); i++)
1259
 
    {
1260
 
      size = sizeof (buffer);
1261
 
      ret =
1262
 
        gnutls_x509_crt_get_subject_alt_name (crt, i, buffer, &size,
1263
 
                                              &critical);
1264
 
 
1265
 
      if (i == 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1266
 
        {
1267
 
          fprintf (out, "\tSubject Alternative name:");
1268
 
          if (critical)
1269
 
            fprintf (out, " (critical)");
1270
 
          fprintf (out, "\n");
1271
 
        }
1272
 
 
1273
 
      if (ret < 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1274
 
        {
1275
 
          fprintf (out, "\t\tFound unsupported alternative name.\n");
1276
 
        }
1277
 
      else
1278
 
        switch (ret)
1279
 
          {
1280
 
          case GNUTLS_SAN_DNSNAME:
1281
 
            fprintf (out, "\t\tDNSname: %s\n", buffer);
1282
 
            break;
1283
 
          case GNUTLS_SAN_RFC822NAME:
1284
 
            fprintf (out, "\t\tRFC822name: %s\n", buffer);
1285
 
            break;
1286
 
          case GNUTLS_SAN_URI:
1287
 
            fprintf (out, "\t\tURI: %s\n", buffer);
1288
 
            break;
1289
 
          case GNUTLS_SAN_IPADDRESS:
1290
 
            fprintf (out, "\t\tIPAddress: %s\n", buffer);
1291
 
            break;
1292
 
          }
1293
 
    }
1294
 
 
1295
 
  /* CRL dist points.
1296
 
   */
1297
 
  ret = 0;
1298
 
  for (i = 0; !(ret < 0); i++)
1299
 
    {
1300
 
      size = sizeof (buffer);
1301
 
      ret =
1302
 
        gnutls_x509_crt_get_crl_dist_points (crt, i, buffer, &size,
1303
 
                                             NULL, &critical);
1304
 
 
1305
 
      if (i == 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1306
 
        {
1307
 
          fprintf (out, "\tCRL Distribution points:");
1308
 
          if (critical)
1309
 
            fprintf (out, " (critical)");
1310
 
          fprintf (out, "\n");
1311
 
        }
1312
 
 
1313
 
      if (ret < 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1314
 
        {
1315
 
          fprintf (out, "\t\tError decoding: %s\n", gnutls_strerror (ret));
1316
 
        }
1317
 
      else
1318
 
        switch (ret)
1319
 
          {
1320
 
          case GNUTLS_SAN_DNSNAME:
1321
 
            fprintf (out, "\t\tDNSname: %s\n", buffer);
1322
 
            break;
1323
 
          case GNUTLS_SAN_RFC822NAME:
1324
 
            fprintf (out, "\t\tRFC822name: %s\n", buffer);
1325
 
            break;
1326
 
          case GNUTLS_SAN_URI:
1327
 
            fprintf (out, "\t\tURI: %s\n", buffer);
1328
 
            break;
1329
 
          case GNUTLS_SAN_IPADDRESS:
1330
 
            fprintf (out, "\t\tIPAddress: %s\n", buffer);
1331
 
            break;
1332
 
          }
1333
 
    }
1334
 
 
1335
 
  /* check for basicConstraints
1336
 
   */
1337
 
  ret = gnutls_x509_crt_get_ca_status (crt, &critical);
1338
 
 
1339
 
  if (ret >= 0)
1340
 
    {
1341
 
      fprintf (out, "\tBasic Constraints:");
1342
 
      if (critical)
1343
 
        fprintf (out, " (critical)");
1344
 
      fprintf (out, "\n");
1345
 
 
1346
 
      if (ret == 0)
1347
 
        fprintf (out, "\t\tCA:FALSE\n");
1348
 
      else
1349
 
        fprintf (out, "\t\tCA:TRUE\n");
1350
 
 
1351
 
    }
1352
 
 
1353
 
  /* Key Usage.
1354
 
   */
1355
 
  ret = gnutls_x509_crt_get_key_usage (crt, &key_usage, &critical);
1356
 
 
1357
 
  if (ret >= 0)
1358
 
    {
1359
 
      fprintf (out, "\tKey usage: %s\n", critical ? "(critical)" : "");
1360
 
      print_key_usage (key_usage, out);
1361
 
    }
1362
 
 
1363
 
  /* Key Purpose identifiers.
1364
 
   */
1365
 
  i = 0;
1366
 
 
1367
 
  size = sizeof (buffer);
1368
 
  if ((ret =
1369
 
       gnutls_x509_crt_get_key_purpose_oid (crt, 0, buffer, &size,
1370
 
                                            &critical)) >= 0)
1371
 
    {
1372
 
      fprintf (out, "\tKey purpose OIDs: %s\n", critical ? "(critical)" : "");
1373
 
      do
1374
 
        {
1375
 
          size = sizeof (buffer);
1376
 
          ret =
1377
 
            gnutls_x509_crt_get_key_purpose_oid (crt, i, buffer, &size,
1378
 
                                                 &critical);
1379
 
 
1380
 
          if (ret >= 0)
1381
 
            {
1382
 
              print_key_purpose (buffer, out);
1383
 
            }
1384
 
          i++;
1385
 
        }
1386
 
      while (ret >= 0);
1387
 
    }
1388
 
 
1389
 
  /* Subject Key ID 
1390
 
   */
1391
 
  size = sizeof (buffer);
1392
 
  ret = gnutls_x509_crt_get_subject_key_id (crt, buffer, &size, &critical);
1393
 
 
1394
 
  if (ret < 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1395
 
    {
1396
 
      fprintf (out, "Error getting subject key id: %s\n",
1397
 
               gnutls_strerror (ret));
1398
 
    }
1399
 
 
1400
 
  if (ret >= 0)
1401
 
    {
1402
 
      fprintf (out, "\tSubject Key ID: %s\n\t\t%s\n",
1403
 
               critical ? "(critical)" : "", raw_to_string (buffer, size));
1404
 
    }
1405
 
 
1406
 
  /* Authority Key ID 
1407
 
   */
1408
 
  size = sizeof (buffer);
1409
 
  ret = gnutls_x509_crt_get_authority_key_id (crt, buffer, &size, &critical);
1410
 
 
1411
 
  if (ret < 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1412
 
    {
1413
 
      fprintf (out, "Error getting authority key id: %s\n",
1414
 
               gnutls_strerror (ret));
1415
 
    }
1416
 
 
1417
 
  if (ret >= 0)
1418
 
    {
1419
 
      fprintf (out, "\tAuthority Key ID: %s\n\t\t%s\n",
1420
 
               critical ? "(critical)" : "", raw_to_string (buffer, size));
1421
 
    }
1422
 
 
1423
 
  /* other extensions:
1424
 
   */
1425
 
  indx = 0;
1426
 
  ret = 0;
1427
 
  for (i = 0; !(ret < 0); i++)
1428
 
    {
1429
 
 
1430
 
      size = sizeof (oid);
1431
 
      ret = gnutls_x509_crt_get_extension_oid (crt, i, oid, &size);
1432
 
 
1433
 
      if (ret >= 0)
1434
 
        {
1435
 
          if (known_oid (oid))
1436
 
            continue;
1437
 
 
1438
 
          if (strcmp (oid, old_oid) == 0)
1439
 
            {
1440
 
              indx++;
1441
 
            }
1442
 
          else
1443
 
            {
1444
 
              indx = 0;
1445
 
            }
1446
 
 
1447
 
          fprintf (out, "\t%s: ", oid);
1448
 
 
1449
 
          size = sizeof (buffer);
1450
 
          ret =
1451
 
            gnutls_x509_crt_get_extension_by_oid (crt, oid, indx,
1452
 
                                                  buffer, &size, &critical);
1453
 
 
1454
 
          if (ret >= 0)
1455
 
            {
1456
 
              if (critical)
1457
 
                fprintf (out, "(critical)\n");
1458
 
              else
1459
 
                fprintf (out, "\n");
1460
 
 
1461
 
              fprintf (out, "\t\tDER Data: ");
1462
 
              for (j = 0; j < size; j++)
1463
 
                {
1464
 
                  fprintf (out, "%.2x", (unsigned char) buffer[j]);
1465
 
                }
1466
 
              fprintf (out, "\n");
1467
 
              fprintf (out, "\t\tASCII: ");
1468
 
              for (j = 0; j < size; j++)
1469
 
                {
1470
 
                  if (isprint (buffer[j]))
1471
 
                    fprintf (out, "%c", (unsigned char) buffer[j]);
1472
 
                  else
1473
 
                    fprintf (out, ".");
1474
 
                }
1475
 
              fprintf (out, "\n");
1476
 
            }
1477
 
 
1478
 
          ret = 0;
1479
 
          strcpy (old_oid, oid);
1480
 
        }
1481
 
    }
1482
 
 
1483
 
 
1484
 
 
1485
 
  /* fingerprint
1486
 
   */
1487
 
  fprintf (out, "\nOther information:\n");
1488
 
  if (all)
1489
 
    {
1490
 
      size = sizeof (buffer);
1491
 
      if ((ret =
1492
 
           gnutls_x509_crt_get_fingerprint (crt, GNUTLS_DIG_MD5, buffer,
1493
 
                                            &size)) < 0)
1494
 
        {
1495
 
          fprintf (out, "Error in fingerprint calculation: %s\n",
1496
 
                   gnutls_strerror (ret));
1497
 
        }
1498
 
      else
1499
 
        {
1500
 
          fprintf (out, "\tMD5 Fingerprint: %s\n",
1501
 
                   raw_to_string (buffer, size));
1502
 
        }
1503
 
 
1504
 
      size = sizeof (buffer);
1505
 
      if ((ret =
1506
 
           gnutls_x509_crt_get_fingerprint (crt, GNUTLS_DIG_SHA1, buffer,
1507
 
                                            &size)) < 0)
1508
 
        {
1509
 
          fprintf (out, "Error in fingerprint calculation: %s\n",
1510
 
                   gnutls_strerror (ret));
1511
 
        }
1512
 
      else
1513
 
        {
1514
 
          fprintf (out, "\tSHA1 Fingerprint: %s\n",
1515
 
                   raw_to_string (buffer, size));
1516
 
        }
1517
 
 
1518
 
    }
1519
 
 
1520
 
  size = sizeof (buffer);
1521
 
  if ((ret = gnutls_x509_crt_get_key_id (crt, 0, buffer, &size)) < 0)
1522
 
    {
1523
 
      fprintf (out, "Error in key id calculation: %s\n",
1524
 
               gnutls_strerror (ret));
1525
 
    }
1526
 
  else
1527
 
    {
1528
 
      fprintf (out, "\tPublic Key ID: %s\n", raw_to_string (buffer, size));
1529
 
    }
1530
 
 
1531
 
  fprintf (out, "\n");
 
967
 
 
968
  if (all)
 
969
    ret = gnutls_x509_crt_print (crt, GNUTLS_X509_CRT_FULL, &info);
 
970
  else
 
971
    ret = gnutls_x509_crt_print (crt, GNUTLS_X509_CRT_UNSIGNED_FULL, &info);
 
972
  if (ret == 0)
 
973
    {
 
974
      fprintf (out, "%s\n", info.data);
 
975
      gnutls_free (info.data);
 
976
    }
1532
977
 
1533
978
  if (out == stderr && batch == 0)      /* interactive */
1534
979
    if (read_yesno ("Is the above information ok? (Y/N): ") == 0)
1538
983
}
1539
984
 
1540
985
static void
1541
 
print_crl_info (gnutls_x509_crl crl, FILE *out, int all)
1542
 
{
1543
 
  int ret, rc;
1544
 
  time_t tim;
1545
 
  unsigned int j;
1546
 
  char serial[128];
1547
 
  size_t serial_size = sizeof (serial), dn_size;
1548
 
  char dn[256];
1549
 
  const char *cprint;
1550
 
 
1551
 
  fprintf (out, "CRL information:\n");
1552
 
  fprintf (out, "Version: %d\n", gnutls_x509_crl_get_version (crl));
1553
 
 
1554
 
  /* Issuer
1555
 
   */
1556
 
  if (all)
1557
 
    {
1558
 
      dn_size = sizeof (dn);
1559
 
 
1560
 
      ret = gnutls_x509_crl_get_issuer_dn (crl, dn, &dn_size);
1561
 
      if (ret >= 0)
1562
 
        fprintf (out, "Issuer: %s\n", dn);
1563
 
 
1564
 
      fprintf (out, "Signature Algorithm: ");
1565
 
      ret = gnutls_x509_crl_get_signature_algorithm (crl);
1566
 
 
1567
 
      cprint = gnutls_sign_algorithm_get_name (ret);
1568
 
      if (cprint == NULL)
1569
 
        cprint = UNKNOWN;
1570
 
      fprintf (out, "%s\n", cprint);
1571
 
    }
1572
 
 
1573
 
  /* Validity
1574
 
   */
1575
 
  fprintf (out, "Update dates:\n");
1576
 
 
1577
 
  tim = gnutls_x509_crl_get_this_update (crl);
1578
 
  fprintf (out, "\tIssued at: %s", ctime (&tim));
1579
 
 
1580
 
  tim = gnutls_x509_crl_get_next_update (crl);
1581
 
  fprintf (out, "\tNext at: %s", ctime (&tim));
1582
 
 
1583
 
  fprintf (out, "\n");
1584
 
 
1585
 
  /* Count the certificates.
1586
 
   */
1587
 
 
1588
 
  rc = gnutls_x509_crl_get_crt_count (crl);
1589
 
  fprintf (out, "Revoked certificates: %d\n", rc);
1590
 
 
1591
 
  for (j = 0; j < (unsigned int) rc; j++)
1592
 
    {
1593
 
      /* serial number
1594
 
       */
1595
 
      serial_size = sizeof (serial);
1596
 
      ret =
1597
 
        gnutls_x509_crl_get_crt_serial (crl, j, serial, &serial_size, &tim);
1598
 
 
1599
 
      if (ret < 0)
1600
 
        {
1601
 
          fprintf (stderr, "error: %s\n", gnutls_strerror (ret));
1602
 
        }
1603
 
      else
1604
 
        {
1605
 
          fprintf (out, "\tCertificate SN: %s\n",
1606
 
                   raw_to_string (serial, serial_size));
1607
 
          fprintf (out, "\tRevoked at: %s\n", ctime (&tim));
1608
 
        }
1609
 
    }
1610
 
 
1611
 
}
1612
 
 
1613
 
void
1614
 
crl_info ()
1615
 
{
1616
 
  gnutls_x509_crl crl;
 
986
print_crl_info (gnutls_x509_crl_t crl, FILE *out)
 
987
{
 
988
  gnutls_datum_t info;
1617
989
  int ret;
1618
990
  size_t size;
1619
 
  gnutls_datum pem;
1620
 
 
1621
 
  size = fread (buffer, 1, sizeof (buffer) - 1, infile);
1622
 
  buffer[size] = 0;
1623
 
 
1624
 
  gnutls_x509_crl_init (&crl);
1625
 
 
1626
 
  pem.data = buffer;
1627
 
  pem.size = size;
1628
 
 
1629
 
  ret = gnutls_x509_crl_import (crl, &pem, in_cert_format);
 
991
 
 
992
  ret = gnutls_x509_crl_print (crl, GNUTLS_X509_CRT_FULL, &info);
1630
993
  if (ret < 0)
1631
 
    {
1632
 
      fprintf (stderr, "Decoding error: %s\n", gnutls_strerror (ret));
1633
 
      exit (1);
1634
 
    }
1635
 
 
1636
 
  print_crl_info (crl, outfile, 1);
 
994
    error (EXIT_FAILURE, 0, "crl_print: %s", gnutls_strerror (ret));
 
995
 
 
996
  fprintf (out, "%s\n", info.data);
 
997
 
 
998
  gnutls_free (info.data);
1637
999
 
1638
1000
  size = sizeof (buffer);
1639
1001
  ret = gnutls_x509_crl_export (crl, GNUTLS_X509_FMT_PEM, buffer, &size);
1640
1002
  if (ret < 0)
1641
 
    {
1642
 
      fprintf (stderr, "Encoding error: %s\n", gnutls_strerror (ret));
1643
 
      exit (1);
1644
 
    }
1645
 
 
1646
 
  fprintf (outfile, "\n%s\n", buffer);
1647
 
 
1648
 
}
1649
 
 
 
1003
    error (EXIT_FAILURE, 0, "crl_export: %s", gnutls_strerror (ret));
 
1004
 
 
1005
  fprintf (outfile, "%s", buffer);
 
1006
}
 
1007
 
 
1008
void
 
1009
crl_info (void)
 
1010
{
 
1011
  gnutls_x509_crl_t crl;
 
1012
  int ret;
 
1013
  size_t size;
 
1014
  gnutls_datum_t pem;
 
1015
 
 
1016
  ret = gnutls_x509_crl_init (&crl);
 
1017
  if (ret < 0)
 
1018
    error (EXIT_FAILURE, 0, "crl_init: %s", gnutls_strerror (ret));
 
1019
 
 
1020
  pem.data = fread_file (infile, &size);
 
1021
  pem.size = size;
 
1022
 
 
1023
  if (!pem.data)
 
1024
    error (EXIT_FAILURE, errno, "%s", info.infile ? info.infile :
 
1025
           "standard input");
 
1026
 
 
1027
  ret = gnutls_x509_crl_import (crl, &pem, info.incert_format);
 
1028
  free (pem.data);
 
1029
  if (ret < 0)
 
1030
    error (EXIT_FAILURE, 0, "Import error: %s", gnutls_strerror (ret));
 
1031
 
 
1032
  print_crl_info (crl, outfile);
 
1033
}
1650
1034
 
1651
1035
void
1652
1036
privkey_info (void)
1653
1037
{
1654
 
  gnutls_x509_privkey key;
 
1038
  gnutls_x509_privkey_t key;
1655
1039
  size_t size;
1656
1040
  int ret;
1657
 
  gnutls_datum pem;
 
1041
  gnutls_datum_t pem;
1658
1042
  const char *cprint;
1659
1043
  const char *pass;
1660
1044
 
1669
1053
 
1670
1054
  if (!info.pkcs8)
1671
1055
    {
1672
 
      ret = gnutls_x509_privkey_import (key, &pem, in_cert_format);
 
1056
      ret = gnutls_x509_privkey_import (key, &pem, info.incert_format);
1673
1057
    }
1674
1058
  else
1675
1059
    {
1677
1061
        pass = info.pass;
1678
1062
      else
1679
1063
        pass = get_pass ();
1680
 
      ret =
1681
 
        gnutls_x509_privkey_import_pkcs8 (key, &pem, in_cert_format, pass, 0);
 
1064
      ret = gnutls_x509_privkey_import_pkcs8 (key, &pem, info.incert_format,
 
1065
                                              pass, 0);
1682
1066
    }
1683
 
 
1684
1067
  if (ret < 0)
1685
 
    {
1686
 
      fprintf (stderr, "Decoding error: %s\n", gnutls_strerror (ret));
1687
 
      exit (1);
1688
 
    }
 
1068
    error (EXIT_FAILURE, 0, "Import error: %s", gnutls_strerror (ret));
1689
1069
 
1690
1070
  /* Public key algorithm
1691
1071
   */
1702
1082
   */
1703
1083
  if (ret == GNUTLS_PK_RSA)
1704
1084
    {
1705
 
      gnutls_datum m, e, d, p, q, u;
 
1085
      gnutls_datum_t m, e, d, p, q, u;
1706
1086
 
1707
1087
      ret = gnutls_x509_privkey_export_rsa_raw (key, &m, &e, &d, &p, &q, &u);
1708
1088
      if (ret < 0)
1727
1107
    }
1728
1108
  else if (ret == GNUTLS_PK_DSA)
1729
1109
    {
1730
 
      gnutls_datum p, q, g, y, x;
 
1110
      gnutls_datum_t p, q, g, y, x;
1731
1111
 
1732
1112
      ret = gnutls_x509_privkey_export_dsa_raw (key, &p, &q, &g, &y, &x);
1733
1113
      if (ret < 0)
1765
1145
    {
1766
1146
      ret = gnutls_x509_privkey_fix (key);
1767
1147
      if (ret < 0)
1768
 
        {
1769
 
          fprintf (stderr, "Encoding error: %s\n", gnutls_strerror (ret));
1770
 
          exit (1);
1771
 
        }
 
1148
        error (EXIT_FAILURE, 0, "privkey_fix: %s", gnutls_strerror (ret));
1772
1149
    }
1773
1150
 
1774
1151
  size = sizeof (buffer);
1775
1152
  ret = gnutls_x509_privkey_export (key, GNUTLS_X509_FMT_PEM, buffer, &size);
1776
1153
  if (ret < 0)
1777
 
    {
1778
 
      fprintf (stderr, "Encoding error: %s\n", gnutls_strerror (ret));
1779
 
      exit (1);
1780
 
    }
 
1154
    error (EXIT_FAILURE, 0, "Export error: %s", gnutls_strerror (ret));
 
1155
 
1781
1156
  fprintf (outfile, "\n%s\n", buffer);
1782
1157
}
1783
1158
 
1784
 
/* mand should be non zero if it is required to read a private key.
 
1159
/* Load the private key.
 
1160
 * @mand should be non zero if it is required to read a private key.
1785
1161
 */
1786
 
gnutls_x509_privkey
 
1162
gnutls_x509_privkey_t
1787
1163
load_private_key (int mand)
1788
1164
{
1789
 
  FILE *fd;
1790
 
  gnutls_x509_privkey key;
 
1165
  gnutls_x509_privkey_t key;
1791
1166
  int ret;
1792
 
  gnutls_datum dat;
 
1167
  gnutls_datum_t dat;
1793
1168
  size_t size;
1794
 
  const char *pass;
1795
1169
 
1796
1170
  if (!info.privkey && !mand)
1797
1171
    return NULL;
1798
1172
 
1799
 
  if (!info.privkey)
1800
 
    {
1801
 
      fprintf (stderr, "error: a private key was not specified\n");
1802
 
      exit (1);
1803
 
    }
1804
 
 
1805
 
  fd = fopen (info.privkey, "r");
1806
 
  if (fd == NULL)
1807
 
    {
1808
 
      fprintf (stderr, "error: could not load key file '%s'.\n",
1809
 
               info.privkey);
1810
 
      exit (1);
1811
 
    }
1812
 
 
1813
 
  size = fread (buffer, 1, sizeof (buffer) - 1, fd);
1814
 
  buffer[size] = 0;
1815
 
 
1816
 
  fclose (fd);
 
1173
  if (info.privkey == NULL)
 
1174
    error (EXIT_FAILURE, 0, "missing --load-privkey");
1817
1175
 
1818
1176
  ret = gnutls_x509_privkey_init (&key);
1819
1177
  if (ret < 0)
1820
 
    {
1821
 
      fprintf (stderr, "privkey_init: %s\n", gnutls_strerror (ret));
1822
 
      exit (1);
1823
 
    }
 
1178
    error (EXIT_FAILURE, 0, "privkey_init: %s", gnutls_strerror (ret));
1824
1179
 
1825
 
  dat.data = buffer;
 
1180
  dat.data = read_binary_file (info.privkey, &size);
1826
1181
  dat.size = size;
1827
1182
 
1828
 
  if (!info.pkcs8)
1829
 
    ret = gnutls_x509_privkey_import (key, &dat, in_cert_format);
 
1183
  if (!dat.data)
 
1184
    error (EXIT_FAILURE, errno, "reading --load-privkey: %s",
 
1185
           info.privkey);
 
1186
 
 
1187
  if (info.pkcs8)
 
1188
    {
 
1189
      const char *pass = get_pass ();
 
1190
      ret = gnutls_x509_privkey_import_pkcs8 (key, &dat, info.incert_format,
 
1191
                                              pass, 0);
 
1192
    }
1830
1193
  else
1831
 
    {
1832
 
      pass = get_pass ();
1833
 
      ret = gnutls_x509_privkey_import_pkcs8 (key, &dat, in_cert_format,
1834
 
                                              pass, 0);
1835
 
    }
1836
 
 
 
1194
    ret = gnutls_x509_privkey_import (key, &dat, info.incert_format);
 
1195
  free (dat.data);
1837
1196
  if (ret < 0)
1838
 
    {
1839
 
      fprintf (stderr, "privkey_import: %s\n", gnutls_strerror (ret));
1840
 
      exit (1);
1841
 
    }
 
1197
    error (EXIT_FAILURE, 0, "importing --load-privkey: %s: %s",
 
1198
           info.privkey, gnutls_strerror (ret));
1842
1199
 
1843
1200
  return key;
1844
1201
}
1845
1202
 
1846
 
gnutls_x509_crq
1847
 
load_request ()
 
1203
/* Load the Certificate Request.
 
1204
 */
 
1205
gnutls_x509_crq_t
 
1206
load_request (void)
1848
1207
{
1849
 
  FILE *fd;
1850
 
  gnutls_x509_crq crq;
 
1208
  gnutls_x509_crq_t crq;
1851
1209
  int ret;
1852
 
  gnutls_datum dat;
 
1210
  gnutls_datum_t dat;
1853
1211
  size_t size;
1854
1212
 
1855
1213
  if (!info.request)
1856
1214
    return NULL;
1857
1215
 
1858
 
  fd = fopen (info.request, "r");
1859
 
  if (fd == NULL)
1860
 
    {
1861
 
      fprintf (stderr, "File %s does not exist.\n", info.request);
1862
 
      exit (1);
1863
 
    }
1864
 
 
1865
 
  size = fread (buffer, 1, sizeof (buffer) - 1, fd);
1866
 
  buffer[size] = 0;
1867
 
 
1868
 
  fclose (fd);
1869
 
 
1870
1216
  ret = gnutls_x509_crq_init (&crq);
1871
1217
  if (ret < 0)
1872
 
    {
1873
 
      fprintf (stderr, "crq_init: %s\n", gnutls_strerror (ret));
1874
 
      exit (1);
1875
 
    }
 
1218
    error (EXIT_FAILURE, 0, "crq_init: %s", gnutls_strerror (ret));
1876
1219
 
1877
 
  dat.data = buffer;
 
1220
  dat.data = read_binary_file (info.request, &size);
1878
1221
  dat.size = size;
1879
1222
 
1880
 
  ret = gnutls_x509_crq_import (crq, &dat, in_cert_format);
 
1223
  if (!dat.data)
 
1224
    error (EXIT_FAILURE, errno, "reading --load-request: %s",
 
1225
           info.request);
1881
1226
 
 
1227
  ret = gnutls_x509_crq_import (crq, &dat, info.incert_format);
 
1228
  free (dat.data);
1882
1229
  if (ret < 0)
1883
 
    {
1884
 
      fprintf (stderr, "crq_import: %s\n", gnutls_strerror (ret));
1885
 
      exit (1);
1886
 
    }
 
1230
    error (EXIT_FAILURE, 0, "importing --load-request: %s: %s",
 
1231
           info.request, gnutls_strerror (ret));
1887
1232
 
1888
1233
  return crq;
1889
1234
}
1890
1235
 
1891
 
gnutls_x509_privkey
1892
 
load_ca_private_key ()
 
1236
/* Load the CA's private key.
 
1237
 */
 
1238
gnutls_x509_privkey_t
 
1239
load_ca_private_key (void)
1893
1240
{
1894
 
  FILE *fd;
1895
 
  gnutls_x509_privkey key;
 
1241
  gnutls_x509_privkey_t key;
1896
1242
  int ret;
1897
 
  const char *pass;
1898
 
  gnutls_datum dat;
 
1243
  gnutls_datum_t dat;
1899
1244
  size_t size;
1900
1245
 
1901
 
  fprintf (stderr, "Loading CA's private key...\n");
1902
 
 
1903
1246
  if (info.ca_privkey == NULL)
1904
 
    {
1905
 
      fprintf (stderr, "You must specify a private key of the CA.\n");
1906
 
      exit (1);
1907
 
    }
1908
 
 
1909
 
  fd = fopen (info.ca_privkey, "r");
1910
 
  if (fd == NULL)
1911
 
    {
1912
 
      fprintf (stderr, "File %s does not exist.\n", info.ca_privkey);
1913
 
      exit (1);
1914
 
    }
1915
 
 
1916
 
  size = fread (buffer, 1, sizeof (buffer) - 1, fd);
1917
 
  buffer[size] = 0;
1918
 
 
1919
 
  fclose (fd);
 
1247
    error (EXIT_FAILURE, 0, "missing --load-ca-privkey");
1920
1248
 
1921
1249
  ret = gnutls_x509_privkey_init (&key);
1922
1250
  if (ret < 0)
1923
 
    {
1924
 
      fprintf (stderr, "privkey_init: %s\n", gnutls_strerror (ret));
1925
 
      exit (1);
1926
 
    }
 
1251
    error (EXIT_FAILURE, 0, "privkey_init: %s", gnutls_strerror (ret));
1927
1252
 
1928
 
  dat.data = buffer;
 
1253
  dat.data = read_binary_file (info.ca_privkey, &size);
1929
1254
  dat.size = size;
1930
1255
 
1931
 
  if (!info.pkcs8)
1932
 
    ret = gnutls_x509_privkey_import (key, &dat, in_cert_format);
 
1256
  if (!dat.data)
 
1257
    error (EXIT_FAILURE, errno, "reading --load-ca-privkey: %s",
 
1258
           info.ca_privkey);
 
1259
 
 
1260
  if (info.pkcs8)
 
1261
    {
 
1262
      const char *pass = get_pass ();
 
1263
      ret = gnutls_x509_privkey_import_pkcs8 (key, &dat, info.incert_format,
 
1264
                                              pass, 0);
 
1265
    }
1933
1266
  else
1934
 
    {
1935
 
      pass = get_pass ();
1936
 
      ret = gnutls_x509_privkey_import_pkcs8 (key, &dat, in_cert_format,
1937
 
                                              pass, 0);
1938
 
    }
1939
 
 
 
1267
    ret = gnutls_x509_privkey_import (key, &dat, info.incert_format);
 
1268
  free (dat.data);
1940
1269
  if (ret < 0)
1941
 
    {
1942
 
      fprintf (stderr, "privkey_import: %s\n", gnutls_strerror (ret));
1943
 
      exit (1);
1944
 
    }
 
1270
    error (EXIT_FAILURE, 0, "importing --load-ca-privkey: %s: %s",
 
1271
           info.ca_privkey, gnutls_strerror (ret));
1945
1272
 
1946
1273
  return key;
1947
1274
}
1948
1275
 
1949
1276
/* Loads the CA's certificate
1950
1277
 */
1951
 
gnutls_x509_crt
1952
 
load_ca_cert ()
 
1278
gnutls_x509_crt_t
 
1279
load_ca_cert (void)
1953
1280
{
1954
 
  FILE *fd;
1955
 
  gnutls_x509_crt crt;
 
1281
  gnutls_x509_crt_t crt;
1956
1282
  int ret;
1957
 
  gnutls_datum dat;
 
1283
  gnutls_datum_t dat;
1958
1284
  size_t size;
1959
1285
 
1960
 
  fprintf (stderr, "Loading CA's certificate...\n");
1961
 
 
1962
1286
  if (info.ca == NULL)
1963
 
    {
1964
 
      fprintf (stderr, "You must specify a certificate of the CA.\n");
1965
 
      exit (1);
1966
 
    }
1967
 
 
1968
 
  fd = fopen (info.ca, "r");
1969
 
  if (fd == NULL)
1970
 
    {
1971
 
      fprintf (stderr, "File %s does not exist.\n", info.ca);
1972
 
      exit (1);
1973
 
    }
1974
 
 
1975
 
  size = fread (buffer, 1, sizeof (buffer) - 1, fd);
1976
 
  buffer[size] = 0;
1977
 
 
1978
 
  fclose (fd);
 
1287
    error (EXIT_FAILURE, 0, "missing --load-ca-certificate");
1979
1288
 
1980
1289
  ret = gnutls_x509_crt_init (&crt);
1981
1290
  if (ret < 0)
1982
 
    {
1983
 
      fprintf (stderr, "crt_init: %s\n", gnutls_strerror (ret));
1984
 
      exit (1);
1985
 
    }
 
1291
    error (EXIT_FAILURE, 0, "crt_init: %s", gnutls_strerror (ret));
1986
1292
 
1987
 
  dat.data = buffer;
 
1293
  dat.data = read_binary_file (info.ca, &size);
1988
1294
  dat.size = size;
1989
1295
 
1990
 
  ret = gnutls_x509_crt_import (crt, &dat, in_cert_format);
 
1296
  if (!dat.data)
 
1297
    error (EXIT_FAILURE, errno, "reading --load-ca-certificate: %s",
 
1298
           info.ca);
 
1299
 
 
1300
  ret = gnutls_x509_crt_import (crt, &dat, info.incert_format);
 
1301
  free (dat.data);
1991
1302
  if (ret < 0)
1992
 
    {
1993
 
      fprintf (stderr, "crt_import: %s\n", gnutls_strerror (ret));
1994
 
      exit (1);
1995
 
    }
 
1303
    error (EXIT_FAILURE, 0, "importing --load-ca-certificate: %s: %s",
 
1304
           info.ca, gnutls_strerror (ret));
1996
1305
 
1997
1306
  return crt;
1998
1307
}
2001
1310
 * If mand is non zero then a certificate is mandatory. Otherwise
2002
1311
 * null will be returned if the certificate loading fails.
2003
1312
 */
2004
 
gnutls_x509_crt
 
1313
gnutls_x509_crt_t
2005
1314
load_cert (int mand)
2006
1315
{
2007
 
  gnutls_x509_crt *crt;
 
1316
  gnutls_x509_crt_t *crt;
2008
1317
  int size;
2009
1318
 
2010
1319
  crt = load_cert_list (mand, &size);
2016
1325
 
2017
1326
/* Loads a certificate list
2018
1327
 */
2019
 
gnutls_x509_crt *
 
1328
gnutls_x509_crt_t *
2020
1329
load_cert_list (int mand, int *crt_size)
2021
1330
{
2022
1331
  FILE *fd;
2023
 
  static gnutls_x509_crt crt[MAX_CERTS];
 
1332
  static gnutls_x509_crt_t crt[MAX_CERTS];
2024
1333
  char *ptr;
2025
1334
  int ret, i;
2026
 
  gnutls_datum dat;
 
1335
  gnutls_datum_t dat;
2027
1336
  size_t size;
2028
1337
  int ptr_size;
2029
1338
 
2032
1341
 
2033
1342
  if (info.cert == NULL)
2034
1343
    {
2035
 
      fprintf (stderr, "You must specify a certificate.\n");
2036
1344
      if (mand)
2037
 
        exit (1);
 
1345
        error (EXIT_FAILURE, 0, "missing --load-certificate");
2038
1346
      else
2039
1347
        return NULL;
2040
1348
    }
2041
1349
 
2042
1350
  fd = fopen (info.cert, "r");
2043
1351
  if (fd == NULL)
2044
 
    {
2045
 
      fprintf (stderr, "File %s does not exist.\n", info.cert);
2046
 
      exit (1);
2047
 
    }
 
1352
    error (EXIT_FAILURE, 0, "File %s does not exist", info.cert);
2048
1353
 
2049
1354
  size = fread (buffer, 1, sizeof (buffer) - 1, fd);
2050
1355
  buffer[size] = 0;
2058
1363
    {
2059
1364
      ret = gnutls_x509_crt_init (&crt[i]);
2060
1365
      if (ret < 0)
2061
 
        {
2062
 
          fprintf (stderr, "crt_init: %s\n", gnutls_strerror (ret));
2063
 
          exit (1);
2064
 
        }
 
1366
        error (EXIT_FAILURE, 0, "crt_init: %s", gnutls_strerror (ret));
2065
1367
 
2066
1368
      dat.data = ptr;
2067
1369
      dat.size = ptr_size;
2068
1370
 
2069
 
      ret = gnutls_x509_crt_import (crt[i], &dat, in_cert_format);
 
1371
      ret = gnutls_x509_crt_import (crt[i], &dat, info.incert_format);
2070
1372
      if (ret < 0 && *crt_size > 0)
2071
1373
        break;
2072
1374
      if (ret < 0)
2073
 
        {
2074
 
          fprintf (stderr, "crt_import: %s\n", gnutls_strerror (ret));
2075
 
          exit (1);
2076
 
        }
 
1375
        error (EXIT_FAILURE, 0, "crt_import: %s", gnutls_strerror (ret));
2077
1376
 
2078
1377
      ptr = strstr (ptr, "---END");
2079
1378
      if (ptr == NULL)
2100
1399
void
2101
1400
generate_request (void)
2102
1401
{
2103
 
  gnutls_x509_crq crq;
2104
 
  gnutls_x509_privkey key;
 
1402
  gnutls_x509_crq_t crq;
 
1403
  gnutls_x509_privkey_t key;
2105
1404
  int ret;
2106
1405
  const char *pass;
2107
1406
  size_t size;
2110
1409
 
2111
1410
  ret = gnutls_x509_crq_init (&crq);
2112
1411
  if (ret < 0)
2113
 
    {
2114
 
      fprintf (stderr, "crq_init: %s\n", gnutls_strerror (ret));
2115
 
      exit (1);
2116
 
    }
 
1412
    error (EXIT_FAILURE, 0, "crq_init: %s", gnutls_strerror (ret));
2117
1413
 
2118
1414
  /* Load the private key.
2119
1415
   */
2120
 
  key = generate_private_key_int ();
 
1416
  key = load_private_key (0);
 
1417
  if (!key)
 
1418
    key = generate_private_key_int ();
2121
1419
 
2122
1420
  /* Set the DN.
2123
1421
   */
2132
1430
 
2133
1431
  ret = gnutls_x509_crq_set_version (crq, 1);
2134
1432
  if (ret < 0)
2135
 
    {
2136
 
      fprintf (stderr, "set_version: %s\n", gnutls_strerror (ret));
2137
 
      exit (1);
2138
 
    }
 
1433
    error (EXIT_FAILURE, 0, "set_version: %s", gnutls_strerror (ret));
2139
1434
 
2140
1435
  pass = get_challenge_pass ();
2141
1436
 
2143
1438
    {
2144
1439
      ret = gnutls_x509_crq_set_challenge_password (crq, pass);
2145
1440
      if (ret < 0)
2146
 
        {
2147
 
          fprintf (stderr, "set_pass: %s\n", gnutls_strerror (ret));
2148
 
          exit (1);
2149
 
        }
 
1441
        error (EXIT_FAILURE, 0, "set_pass: %s", gnutls_strerror (ret));
2150
1442
    }
2151
1443
 
2152
1444
  ret = gnutls_x509_crq_set_key (crq, key);
2153
1445
  if (ret < 0)
2154
 
    {
2155
 
      fprintf (stderr, "set_key: %s\n", gnutls_strerror (ret));
2156
 
      exit (1);
2157
 
    }
 
1446
    error (EXIT_FAILURE, 0, "set_key: %s", gnutls_strerror (ret));
2158
1447
 
2159
1448
  ret = gnutls_x509_crq_sign (crq, key);
2160
1449
  if (ret < 0)
2161
 
    {
2162
 
      fprintf (stderr, "sign: %s\n", gnutls_strerror (ret));
2163
 
      exit (1);
2164
 
    }
 
1450
    error (EXIT_FAILURE, 0, "sign: %s", gnutls_strerror (ret));
2165
1451
 
2166
1452
  size = sizeof (buffer);
2167
 
  ret = gnutls_x509_crq_export (crq, out_cert_format, buffer, &size);
 
1453
  ret = gnutls_x509_crq_export (crq, info.outcert_format, buffer, &size);
2168
1454
  if (ret < 0)
2169
 
    {
2170
 
      fprintf (stderr, "export: %s\n", gnutls_strerror (ret));
2171
 
      exit (1);
2172
 
    }
 
1455
    error (EXIT_FAILURE, 0, "export: %s", gnutls_strerror (ret));
2173
1456
 
2174
1457
  fwrite (buffer, 1, size, outfile);
2175
1458
 
2178
1461
 
2179
1462
}
2180
1463
 
2181
 
static void print_verification_res (gnutls_x509_crt crt,
2182
 
                                    gnutls_x509_crt issuer,
2183
 
                                    gnutls_x509_crl * crl_list,
 
1464
static void print_verification_res (gnutls_x509_crt_t crt,
 
1465
                                    gnutls_x509_crt_t issuer,
 
1466
                                    gnutls_x509_crl_t * crl_list,
2184
1467
                                    int crl_list_size);
2185
1468
 
2186
1469
#define CERT_SEP "-----BEGIN CERT"
2195
1478
  char issuer_name[256];
2196
1479
  size_t name_size;
2197
1480
  size_t issuer_name_size;
2198
 
  gnutls_datum tmp;
2199
 
  gnutls_x509_crt *x509_cert_list = NULL;
2200
 
  gnutls_x509_crl *x509_crl_list = NULL;
 
1481
  gnutls_datum_t tmp;
 
1482
  gnutls_x509_crt_t *x509_cert_list = NULL;
 
1483
  gnutls_x509_crl_t *x509_crl_list = NULL;
2201
1484
  int x509_ncerts, x509_ncrls;
2202
1485
 
2203
1486
 
2215
1498
    do
2216
1499
      {
2217
1500
        x509_crl_list =
2218
 
          (gnutls_x509_crl *) realloc (x509_crl_list,
2219
 
                                       i * sizeof (gnutls_x509_crl));
 
1501
          (gnutls_x509_crl_t *) realloc (x509_crl_list,
 
1502
                                       i * sizeof (gnutls_x509_crl_t));
2220
1503
        if (x509_crl_list == NULL)
2221
 
          {
2222
 
            fprintf (stderr, "memory error\n");
2223
 
            exit (1);
2224
 
          }
 
1504
          error (EXIT_FAILURE, 0, "memory error");
2225
1505
 
2226
1506
        tmp.data = (char *) ptr;
2227
1507
        tmp.size = siz;
2228
1508
 
2229
1509
        ret = gnutls_x509_crl_init (&x509_crl_list[i - 1]);
2230
1510
        if (ret < 0)
2231
 
          {
2232
 
            fprintf (stderr, "Error parsing the CRL[%d]: %s\n", i,
2233
 
                     gnutls_strerror (ret));
2234
 
            exit (1);
2235
 
          }
 
1511
          error (EXIT_FAILURE, 0, "Error parsing the CRL[%d]: %s", i,
 
1512
                 gnutls_strerror (ret));
2236
1513
 
2237
 
        ret =
2238
 
          gnutls_x509_crl_import (x509_crl_list[i - 1], &tmp,
2239
 
                                  GNUTLS_X509_FMT_PEM);
 
1514
        ret = gnutls_x509_crl_import (x509_crl_list[i - 1], &tmp,
 
1515
                                      GNUTLS_X509_FMT_PEM);
2240
1516
        if (ret < 0)
2241
 
          {
2242
 
            fprintf (stderr, "Error parsing the CRL[%d]: %s\n", i,
2243
 
                     gnutls_strerror (ret));
2244
 
            exit (1);
2245
 
          }
 
1517
          error (EXIT_FAILURE, 0, "Error parsing the CRL[%d]: %s", i,
 
1518
                 gnutls_strerror (ret));
2246
1519
 
2247
1520
        /* now we move ptr after the pem header */
2248
1521
        ptr = strstr (ptr, CRL_SEP);
2266
1539
  do
2267
1540
    {
2268
1541
      x509_cert_list =
2269
 
        (gnutls_x509_crt *) realloc (x509_cert_list,
2270
 
                                     i * sizeof (gnutls_x509_crt));
 
1542
        (gnutls_x509_crt_t *) realloc (x509_cert_list,
 
1543
                                     i * sizeof (gnutls_x509_crt_t));
2271
1544
      if (x509_cert_list == NULL)
2272
 
        {
2273
 
          fprintf (stderr, "memory error\n");
2274
 
          exit (1);
2275
 
        }
 
1545
        error (EXIT_FAILURE, 0, "memory error");
2276
1546
 
2277
1547
      tmp.data = (char *) ptr;
2278
1548
      tmp.size = siz;
2279
1549
 
2280
1550
      ret = gnutls_x509_crt_init (&x509_cert_list[i - 1]);
2281
1551
      if (ret < 0)
2282
 
        {
2283
 
          fprintf (stderr, "Error parsing the certificate[%d]: %s\n", i,
2284
 
                   gnutls_strerror (ret));
2285
 
          exit (1);
2286
 
        }
 
1552
        error (EXIT_FAILURE, 0, "Error parsing the certificate[%d]: %s", i,
 
1553
               gnutls_strerror (ret));
2287
1554
 
2288
1555
      ret =
2289
1556
        gnutls_x509_crt_import (x509_cert_list[i - 1], &tmp,
2290
1557
                                GNUTLS_X509_FMT_PEM);
2291
1558
      if (ret < 0)
2292
 
        {
2293
 
          fprintf (stderr, "Error parsing the certificate[%d]: %s\n", i,
2294
 
                   gnutls_strerror (ret));
2295
 
          exit (1);
2296
 
        }
 
1559
        error (EXIT_FAILURE, 0, "Error parsing the certificate[%d]: %s", i,
 
1560
               gnutls_strerror (ret));
2297
1561
 
2298
1562
 
2299
1563
      if (i - 1 != 0)
2306
1570
          ret =
2307
1571
            gnutls_x509_crt_get_dn (x509_cert_list[i - 2], name, &name_size);
2308
1572
          if (ret < 0)
2309
 
            {
2310
 
              fprintf (stderr, "get_dn: %s\n", gnutls_strerror (ret));
2311
 
              exit (1);
2312
 
            }
 
1573
            error (EXIT_FAILURE, 0, "get_dn: %s", gnutls_strerror (ret));
2313
1574
 
2314
1575
          fprintf (outfile, "Certificate[%d]: %s\n", i - 2, name);
2315
1576
 
2320
1581
            gnutls_x509_crt_get_issuer_dn (x509_cert_list[i - 2],
2321
1582
                                           issuer_name, &issuer_name_size);
2322
1583
          if (ret < 0)
2323
 
            {
2324
 
              fprintf (stderr, "get_issuer_dn: %s\n", gnutls_strerror (ret));
2325
 
              exit (1);
2326
 
            }
 
1584
            error (EXIT_FAILURE, 0, "get_issuer_dn: %s", gnutls_strerror (ret));
2327
1585
 
2328
1586
          fprintf (outfile, "\tIssued by: %s\n", issuer_name);
2329
1587
 
2333
1591
          ret =
2334
1592
            gnutls_x509_crt_get_dn (x509_cert_list[i - 1], name, &name_size);
2335
1593
          if (ret < 0)
2336
 
            {
2337
 
              fprintf (stderr, "get_dn: %s\n", gnutls_strerror (ret));
2338
 
              exit (1);
2339
 
            }
 
1594
            error (EXIT_FAILURE, 0, "get_dn: %s", gnutls_strerror (ret));
2340
1595
 
2341
1596
          fprintf (outfile, "\tVerifying against certificate[%d].\n", i - 1);
2342
1597
 
2343
1598
          if (strcmp (issuer_name, name) != 0)
2344
1599
            {
2345
1600
              fprintf (stderr, "Error: Issuer's name: %s\n", name);
2346
 
              fprintf (stderr,
2347
 
                       "Error: Issuer's name does not match the next certificate.\n");
2348
 
              exit (1);
 
1601
              error (EXIT_FAILURE, 0,
 
1602
                     "Issuer's name does not match the next certificate");
2349
1603
            }
2350
1604
 
2351
1605
          fprintf (outfile, "\tVerification output: ");
2373
1627
   * a CA (should be self signed).
2374
1628
   */
2375
1629
  name_size = sizeof (name);
2376
 
  ret =
2377
 
    gnutls_x509_crt_get_dn (x509_cert_list[x509_ncerts - 1], name,
2378
 
                            &name_size);
 
1630
  ret = gnutls_x509_crt_get_dn (x509_cert_list[x509_ncerts - 1], name,
 
1631
                                &name_size);
2379
1632
  if (ret < 0)
2380
 
    {
2381
 
      fprintf (stderr, "get_dn: %s\n", gnutls_strerror (ret));
2382
 
      exit (1);
2383
 
    }
 
1633
    error (EXIT_FAILURE, 0, "get_dn: %s", gnutls_strerror (ret));
2384
1634
 
2385
1635
  fprintf (outfile, "Certificate[%d]: %s\n", x509_ncerts - 1, name);
2386
1636
 
2391
1641
    gnutls_x509_crt_get_issuer_dn (x509_cert_list[x509_ncerts - 1],
2392
1642
                                   issuer_name, &issuer_name_size);
2393
1643
  if (ret < 0)
2394
 
    {
2395
 
      fprintf (stderr, "get_issuer_dn: %s\n", gnutls_strerror (ret));
2396
 
      exit (1);
2397
 
    }
 
1644
    error (EXIT_FAILURE, 0, "get_issuer_dn: %s", gnutls_strerror (ret));
2398
1645
 
2399
1646
  fprintf (outfile, "\tIssued by: %s\n", name);
2400
1647
 
2401
1648
  if (strcmp (issuer_name, name) != 0)
2402
 
    {
2403
 
      fprintf (stderr, "Error: The last certificate is not self signed.\n");
2404
 
      exit (1);
2405
 
    }
 
1649
    error (EXIT_FAILURE, 0, "Error: The last certificate is not self signed.");
2406
1650
 
2407
1651
  fprintf (outfile, "\tVerification output: ");
2408
1652
  print_verification_res (x509_cert_list[x509_ncerts - 1],
2412
1656
  fprintf (outfile, ".\n\n");
2413
1657
 
2414
1658
  for (i = 0; i < x509_ncerts; i++)
2415
 
    {
2416
 
      gnutls_x509_crt_deinit (x509_cert_list[i]);
2417
 
    }
 
1659
    gnutls_x509_crt_deinit (x509_cert_list[i]);
2418
1660
 
2419
1661
  for (i = 0; i < x509_ncrls; i++)
2420
 
    {
2421
 
      gnutls_x509_crl_deinit (x509_crl_list[i]);
2422
 
    }
 
1662
    gnutls_x509_crl_deinit (x509_crl_list[i]);
2423
1663
 
2424
1664
  free (x509_cert_list);
2425
1665
  free (x509_crl_list);
2426
1666
 
2427
1667
  if (ret < 0)
2428
 
    {
2429
 
      fprintf (stderr, "Error in verification: %s\n", gnutls_strerror (ret));
2430
 
      exit (1);
2431
 
    }
 
1668
    error (EXIT_FAILURE, 0, "Error in verification: %s", gnutls_strerror (ret));
2432
1669
 
2433
1670
  return 0;
2434
1671
}
2435
1672
 
2436
1673
static void
2437
 
print_verification_res (gnutls_x509_crt crt,
2438
 
                        gnutls_x509_crt issuer,
2439
 
                        gnutls_x509_crl * crl_list, int crl_list_size)
 
1674
print_verification_res (gnutls_x509_crt_t crt,
 
1675
                        gnutls_x509_crt_t issuer,
 
1676
                        gnutls_x509_crl_t * crl_list, int crl_list_size)
2440
1677
{
2441
1678
  unsigned int output;
2442
1679
  int comma = 0;
2445
1682
 
2446
1683
  ret = gnutls_x509_crt_verify (crt, &issuer, 1, 0, &output);
2447
1684
  if (ret < 0)
2448
 
    {
2449
 
      fprintf (stderr, "Error in verification: %s\n", gnutls_strerror (ret));
2450
 
      exit (1);
2451
 
    }
 
1685
    error (EXIT_FAILURE, 0, "Error in verification: %s",
 
1686
           gnutls_strerror (ret));
2452
1687
 
2453
1688
  if (output & GNUTLS_CERT_INVALID)
2454
1689
    {
2498
1733
 
2499
1734
  ret = gnutls_x509_crt_check_revocation (crt, crl_list, crl_list_size);
2500
1735
  if (ret < 0)
2501
 
    {
2502
 
      fprintf (stderr, "Error in verification: %s\n", gnutls_strerror (ret));
2503
 
      exit (1);
2504
 
    }
 
1736
    error (EXIT_FAILURE, 0, "Revocation check: %s", gnutls_strerror (ret));
2505
1737
 
2506
1738
  if (ret == 1)
2507
1739
    {                           /* revoked */
2532
1764
  unsigned int output;
2533
1765
  int comma = 0;
2534
1766
  int ret;
2535
 
  gnutls_datum pem;
2536
 
  gnutls_x509_crl crl;
 
1767
  gnutls_datum_t pem;
 
1768
  gnutls_x509_crl_t crl;
2537
1769
  time_t now = time (0);
2538
 
  gnutls_x509_crt issuer;
 
1770
  gnutls_x509_crt_t issuer;
2539
1771
 
2540
1772
  issuer = load_ca_cert ();
2541
1773
 
2542
1774
  fprintf (outfile, "\nCA certificate:\n");
 
1775
 
2543
1776
  dn_size = sizeof (dn);
2544
1777
  ret = gnutls_x509_crt_get_dn (issuer, dn, &dn_size);
2545
 
  if (ret >= 0)
2546
 
    fprintf (outfile, "\tSubject: %s\n\n", dn);
2547
 
 
2548
 
  size = fread (buffer, 1, sizeof (buffer) - 1, infile);
2549
 
  buffer[size] = 0;
2550
 
 
2551
 
  pem.data = buffer;
 
1778
  if (ret < 0)
 
1779
    error (EXIT_FAILURE, 0, "crt_get_dn: %s", gnutls_strerror (ret));
 
1780
 
 
1781
  fprintf (outfile, "\tSubject: %s\n\n", dn);
 
1782
 
 
1783
  ret = gnutls_x509_crl_init (&crl);
 
1784
  if (ret < 0)
 
1785
    error (EXIT_FAILURE, 0, "crl_init: %s", gnutls_strerror (ret));
 
1786
 
 
1787
  pem.data = fread_file (infile, &size);
2552
1788
  pem.size = size;
2553
1789
 
2554
 
  gnutls_x509_crl_init (&crl);
2555
 
 
2556
 
  ret = gnutls_x509_crl_import (crl, &pem, in_cert_format);
 
1790
  ret = gnutls_x509_crl_import (crl, &pem, info.incert_format);
 
1791
  free (pem.data);
2557
1792
  if (ret < 0)
2558
 
    {
2559
 
      fprintf (stderr, "CRL decoding error: %s\n", gnutls_strerror (ret));
2560
 
      exit (1);
2561
 
    }
2562
 
 
2563
 
  print_crl_info (crl, outfile, 1);
2564
 
 
 
1793
    error (EXIT_FAILURE, 0, "Import error: %s", gnutls_strerror (ret));
 
1794
 
 
1795
  print_crl_info (crl, outfile);
2565
1796
 
2566
1797
  fprintf (outfile, "Verification output: ");
2567
1798
  ret = gnutls_x509_crl_verify (crl, &issuer, 1, 0, &output);
2568
1799
  if (ret < 0)
2569
 
    {
2570
 
      fprintf (stderr, "Error in verification: %s\n", gnutls_strerror (ret));
2571
 
      exit (1);
2572
 
    }
 
1800
    error (EXIT_FAILURE, 0, "Verification error: %s", gnutls_strerror (ret));
2573
1801
 
2574
1802
  if (output & GNUTLS_CERT_INVALID)
2575
1803
    {
2618
1846
    }
2619
1847
 
2620
1848
  fprintf (outfile, "\n");
2621
 
 
2622
1849
}
2623
1850
 
2624
1851
#include <gnutls/pkcs12.h>
2627
1854
void
2628
1855
generate_pkcs12 (void)
2629
1856
{
2630
 
  gnutls_pkcs12 pkcs12;
2631
 
  gnutls_pkcs12_bag bag, kbag;
2632
 
  gnutls_x509_crt crt;
2633
 
  gnutls_x509_privkey key;
 
1857
  gnutls_pkcs12_t pkcs12;
 
1858
  gnutls_x509_crt_t *crts;
 
1859
  gnutls_x509_privkey_t key;
2634
1860
  int result;
2635
1861
  size_t size;
2636
 
  gnutls_datum data;
 
1862
  gnutls_datum_t data;
2637
1863
  const char *password;
2638
1864
  const char *name;
2639
1865
  unsigned int flags;
2640
 
  gnutls_datum key_id;
 
1866
  gnutls_datum_t key_id;
2641
1867
  unsigned char _key_id[20];
2642
1868
  int index;
 
1869
  int ncrts;
 
1870
  int i;
2643
1871
 
2644
1872
  fprintf (stderr, "Generating a PKCS #12 structure...\n");
2645
1873
 
2646
1874
  key = load_private_key (0);
2647
 
  crt = load_cert (0);
 
1875
  crts = load_cert_list (0, &ncrts);
2648
1876
 
2649
1877
  name = get_pkcs12_key_name ();
2650
1878
 
 
1879
  result = gnutls_pkcs12_init (&pkcs12);
 
1880
  if (result < 0)
 
1881
    error (EXIT_FAILURE, 0, "pkcs12_init: %s", gnutls_strerror (result));
 
1882
 
2651
1883
  if (info.pass)
2652
1884
    password = info.pass;
2653
1885
  else
2654
1886
    password = get_pass ();
2655
1887
 
2656
 
  result = gnutls_pkcs12_bag_init (&bag);
2657
 
  if (result < 0)
 
1888
  for (i = 0; i < ncrts; i++)
2658
1889
    {
2659
 
      fprintf (stderr, "bag_init: %s\n", gnutls_strerror (result));
2660
 
      exit (1);
2661
 
    }
2662
 
 
2663
 
  if (crt)
2664
 
    {                           /* add the certificate only if it was specified.
2665
 
                                 */
2666
 
      result = gnutls_pkcs12_bag_set_crt (bag, crt);
2667
 
      if (result < 0)
2668
 
        {
2669
 
          fprintf (stderr, "set_crt: %s\n", gnutls_strerror (result));
2670
 
          exit (1);
2671
 
        }
 
1890
      gnutls_pkcs12_bag_t bag;
 
1891
 
 
1892
      result = gnutls_pkcs12_bag_init (&bag);
 
1893
      if (result < 0)
 
1894
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));
 
1895
 
 
1896
      result = gnutls_pkcs12_bag_set_crt (bag, crts[i]);
 
1897
      if (result < 0)
 
1898
        error (EXIT_FAILURE, 0, "set_crt[%d]: %s", i, gnutls_strerror (result));
2672
1899
 
2673
1900
      index = result;
2674
1901
 
2675
1902
      result = gnutls_pkcs12_bag_set_friendly_name (bag, index, name);
2676
1903
      if (result < 0)
2677
 
        {
2678
 
          fprintf (stderr, "bag_set_friendly_name: %s\n",
2679
 
                   gnutls_strerror (result));
2680
 
          exit (1);
2681
 
        }
 
1904
        error (EXIT_FAILURE, 0, "bag_set_friendly_name: %s",
 
1905
               gnutls_strerror (result));
2682
1906
 
2683
1907
      size = sizeof (_key_id);
2684
 
      result = gnutls_x509_crt_get_key_id (crt, 0, _key_id, &size);
 
1908
      result = gnutls_x509_crt_get_key_id (crts[i], 0, _key_id, &size);
2685
1909
      if (result < 0)
2686
 
        {
2687
 
          fprintf (stderr, "key_id: %s\n", gnutls_strerror (result));
2688
 
          exit (1);
2689
 
        }
 
1910
        error (EXIT_FAILURE, 0, "key_id[%d]: %s", i, gnutls_strerror (result));
2690
1911
 
2691
1912
      key_id.data = _key_id;
2692
1913
      key_id.size = size;
2693
1914
 
2694
1915
      result = gnutls_pkcs12_bag_set_key_id (bag, index, &key_id);
2695
1916
      if (result < 0)
2696
 
        {
2697
 
          fprintf (stderr, "bag_set_key_id: %s\n", gnutls_strerror (result));
2698
 
          exit (1);
2699
 
        }
 
1917
        error (EXIT_FAILURE, 0, "bag_set_key_id: %s", gnutls_strerror (result));
2700
1918
 
2701
1919
      if (info.export)
2702
1920
        flags = GNUTLS_PKCS_USE_PKCS12_RC2_40;
2705
1923
 
2706
1924
      result = gnutls_pkcs12_bag_encrypt (bag, password, flags);
2707
1925
      if (result < 0)
2708
 
        {
2709
 
          fprintf (stderr, "bag_encrypt: %s\n", gnutls_strerror (result));
2710
 
          exit (1);
2711
 
        }
 
1926
        error (EXIT_FAILURE, 0, "bag_encrypt: %s", gnutls_strerror (result));
 
1927
 
 
1928
      result = gnutls_pkcs12_set_bag (pkcs12, bag);
 
1929
      if (result < 0)
 
1930
        error (EXIT_FAILURE, 0, "set_bag: %s", gnutls_strerror (result));
2712
1931
    }
2713
1932
 
2714
1933
  if (key)
2715
 
    {                           /* add the key only if it was specified.
2716
 
                                 */
 
1934
    {
 
1935
      gnutls_pkcs12_bag_t kbag;
 
1936
 
2717
1937
      result = gnutls_pkcs12_bag_init (&kbag);
2718
1938
      if (result < 0)
2719
 
        {
2720
 
          fprintf (stderr, "bag_init: %s\n", gnutls_strerror (result));
2721
 
          exit (1);
2722
 
        }
 
1939
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));
2723
1940
 
2724
1941
      if (info.export)
2725
1942
        flags = GNUTLS_PKCS_USE_PKCS12_RC2_40;
2731
1948
        gnutls_x509_privkey_export_pkcs8 (key, GNUTLS_X509_FMT_DER,
2732
1949
                                          password, flags, buffer, &size);
2733
1950
      if (result < 0)
2734
 
        {
2735
 
          fprintf (stderr, "key_export: %s\n", gnutls_strerror (result));
2736
 
          exit (1);
2737
 
        }
 
1951
        error (EXIT_FAILURE, 0, "key_export: %s", gnutls_strerror (result));
2738
1952
 
2739
1953
      data.data = buffer;
2740
1954
      data.size = size;
2742
1956
        gnutls_pkcs12_bag_set_data (kbag,
2743
1957
                                    GNUTLS_BAG_PKCS8_ENCRYPTED_KEY, &data);
2744
1958
      if (result < 0)
2745
 
        {
2746
 
          fprintf (stderr, "bag_set_data: %s\n", gnutls_strerror (result));
2747
 
          exit (1);
2748
 
        }
 
1959
        error (EXIT_FAILURE, 0, "bag_set_data: %s", gnutls_strerror (result));
2749
1960
 
2750
1961
      index = result;
2751
1962
 
2752
1963
      result = gnutls_pkcs12_bag_set_friendly_name (kbag, index, name);
2753
1964
      if (result < 0)
2754
 
        {
2755
 
          fprintf (stderr, "bag_set_friendly_name: %s\n",
2756
 
                   gnutls_strerror (result));
2757
 
          exit (1);
2758
 
        }
 
1965
        error (EXIT_FAILURE, 0, "bag_set_friendly_name: %s",
 
1966
               gnutls_strerror (result));
2759
1967
 
2760
1968
      size = sizeof (_key_id);
2761
1969
      result = gnutls_x509_privkey_get_key_id (key, 0, _key_id, &size);
2762
1970
      if (result < 0)
2763
 
        {
2764
 
          fprintf (stderr, "key_id: %s\n", gnutls_strerror (result));
2765
 
          exit (1);
2766
 
        }
 
1971
        error (EXIT_FAILURE, 0, "key_id: %s", gnutls_strerror (result));
2767
1972
 
2768
1973
      key_id.data = _key_id;
2769
1974
      key_id.size = size;
2770
1975
 
2771
1976
      result = gnutls_pkcs12_bag_set_key_id (kbag, index, &key_id);
2772
1977
      if (result < 0)
2773
 
        {
2774
 
          fprintf (stderr, "bag_set_key_id: %s\n", gnutls_strerror (result));
2775
 
          exit (1);
2776
 
        }
2777
 
    }
2778
 
 
2779
 
  /* write the PKCS #12 structure.
2780
 
   */
2781
 
  result = gnutls_pkcs12_init (&pkcs12);
2782
 
  if (result < 0)
2783
 
    {
2784
 
      fprintf (stderr, "pkcs12_init: %s\n", gnutls_strerror (result));
2785
 
      exit (1);
2786
 
    }
2787
 
 
2788
 
  if (crt)
2789
 
    {
2790
 
      result = gnutls_pkcs12_set_bag (pkcs12, bag);
2791
 
      if (result < 0)
2792
 
        {
2793
 
          fprintf (stderr, "set_bag: %s\n", gnutls_strerror (result));
2794
 
          exit (1);
2795
 
        }
2796
 
    }
2797
 
 
2798
 
  if (key)
2799
 
    {
 
1978
        error (EXIT_FAILURE, 0, "bag_set_key_id: %s", gnutls_strerror (result));
 
1979
 
2800
1980
      result = gnutls_pkcs12_set_bag (pkcs12, kbag);
2801
1981
      if (result < 0)
2802
 
        {
2803
 
          fprintf (stderr, "set_bag: %s\n", gnutls_strerror (result));
2804
 
          exit (1);
2805
 
        }
 
1982
        error (EXIT_FAILURE, 0, "set_bag: %s", gnutls_strerror (result));
2806
1983
    }
2807
1984
 
2808
1985
  result = gnutls_pkcs12_generate_mac (pkcs12, password);
2809
1986
  if (result < 0)
2810
 
    {
2811
 
      fprintf (stderr, "generate_mac: %s\n", gnutls_strerror (result));
2812
 
      exit (1);
2813
 
    }
 
1987
    error (EXIT_FAILURE, 0, "generate_mac: %s", gnutls_strerror (result));
2814
1988
 
2815
1989
  size = sizeof (buffer);
2816
 
  result = gnutls_pkcs12_export (pkcs12, out_cert_format, buffer, &size);
 
1990
  result = gnutls_pkcs12_export (pkcs12, info.outcert_format, buffer, &size);
2817
1991
  if (result < 0)
2818
 
    {
2819
 
      fprintf (stderr, "pkcs12_export: %s\n", gnutls_strerror (result));
2820
 
      exit (1);
2821
 
    }
 
1992
    error (EXIT_FAILURE, 0, "pkcs12_export: %s", gnutls_strerror (result));
2822
1993
 
2823
1994
  fwrite (buffer, 1, size, outfile);
2824
1995
 
2825
1996
}
2826
1997
 
2827
1998
const char *
2828
 
BAGTYPE (gnutls_pkcs12_bag_type x)
 
1999
BAGTYPE (gnutls_pkcs12_bag_type_t x)
2829
2000
{
2830
2001
  switch (x)
2831
2002
    {
2847
2018
}
2848
2019
 
2849
2020
void
2850
 
print_bag_data (gnutls_pkcs12_bag bag)
 
2021
print_bag_data (gnutls_pkcs12_bag_t bag)
2851
2022
{
2852
2023
  int result;
2853
2024
  int count, i, type;
2854
 
  gnutls_datum cdata, id;
 
2025
  gnutls_datum_t cdata, id;
2855
2026
  const char *str, *name;
2856
 
  gnutls_datum out;
 
2027
  gnutls_datum_t out;
2857
2028
 
2858
2029
  count = gnutls_pkcs12_bag_get_count (bag);
2859
2030
  if (count < 0)
2860
 
    {
2861
 
      fprintf (stderr, "get_count: %s\n", gnutls_strerror (count));
2862
 
      exit (1);
2863
 
    }
2864
 
 
2865
 
 
 
2031
    error (EXIT_FAILURE, 0, "get_count: %s", gnutls_strerror (count));
2866
2032
 
2867
2033
  fprintf (outfile, "\tElements: %d\n", count);
2868
2034
 
2870
2036
    {
2871
2037
      type = gnutls_pkcs12_bag_get_type (bag, i);
2872
2038
      if (type < 0)
2873
 
        {
2874
 
          fprintf (stderr, "get_type: %s\n", gnutls_strerror (type));
2875
 
          exit (1);
2876
 
        }
 
2039
        error (EXIT_FAILURE, 0, "get_type: %s", gnutls_strerror (type));
2877
2040
 
2878
2041
      fprintf (stderr, "\tType: %s\n", BAGTYPE (type));
2879
2042
 
2880
2043
      name = NULL;
2881
2044
      result = gnutls_pkcs12_bag_get_friendly_name (bag, i, (char **) &name);
2882
2045
      if (result < 0)
2883
 
        {
2884
 
          fprintf (stderr, "get_friendly_name: %s\n", gnutls_strerror (type));
2885
 
          exit (1);
2886
 
        }
 
2046
        error (EXIT_FAILURE, 0, "get_friendly_name: %s",
 
2047
               gnutls_strerror (type));
2887
2048
      if (name)
2888
2049
        fprintf (outfile, "\tFriendly name: %s\n", name);
2889
2050
 
2891
2052
      id.size = 0;
2892
2053
      result = gnutls_pkcs12_bag_get_key_id (bag, i, &id);
2893
2054
      if (result < 0)
2894
 
        {
2895
 
          fprintf (stderr, "get_key_id: %s\n", gnutls_strerror (type));
2896
 
          exit (1);
2897
 
        }
 
2055
        error (EXIT_FAILURE, 0, "get_key_id: %s", gnutls_strerror (type));
2898
2056
      fprintf (outfile, "\tKey ID: %s\n", raw_to_string (id.data, id.size));
2899
2057
 
2900
2058
      result = gnutls_pkcs12_bag_get_data (bag, i, &cdata);
2901
2059
      if (result < 0)
2902
 
        {
2903
 
          fprintf (stderr, "get_data: %s\n", gnutls_strerror (result));
2904
 
          exit (1);
2905
 
        }
 
2060
        error (EXIT_FAILURE, 0, "get_data: %s", gnutls_strerror (result));
2906
2061
 
2907
2062
      switch (type)
2908
2063
        {
2938
2093
void
2939
2094
pkcs12_info (void)
2940
2095
{
2941
 
  gnutls_pkcs12 pkcs12;
2942
 
  gnutls_pkcs12_bag bag;
2943
 
  int result, ret;
 
2096
  gnutls_pkcs12_t pkcs12;
 
2097
  gnutls_pkcs12_bag_t bag;
 
2098
  int result;
2944
2099
  size_t size;
2945
 
  gnutls_datum data;
 
2100
  gnutls_datum_t data;
2946
2101
  const char *password;
2947
2102
  int index;
2948
2103
 
2949
 
  size = fread (buffer, 1, sizeof (buffer) - 1, infile);
2950
 
  buffer[size] = 0;
 
2104
  result = gnutls_pkcs12_init (&pkcs12);
 
2105
  if (result < 0)
 
2106
    error (EXIT_FAILURE, 0, "p12_init: %s", gnutls_strerror (result));
2951
2107
 
2952
 
  data.data = buffer;
 
2108
  data.data = fread_file (infile, &size);
2953
2109
  data.size = size;
2954
2110
 
 
2111
  result = gnutls_pkcs12_import (pkcs12, &data, info.incert_format, 0);
 
2112
  free (data.data);
 
2113
  if (result < 0)
 
2114
    error (EXIT_FAILURE, 0, "p12_import: %s", gnutls_strerror (result));
 
2115
 
2955
2116
  if (info.pass)
2956
2117
    password = info.pass;
2957
2118
  else
2958
2119
    password = get_pass ();
2959
2120
 
2960
 
  result = gnutls_pkcs12_init (&pkcs12);
2961
 
  if (result < 0)
2962
 
    {
2963
 
      fprintf (stderr, "p12_init: %s\n", gnutls_strerror (result));
2964
 
      exit (1);
2965
 
    }
2966
 
 
2967
 
  result = gnutls_pkcs12_import (pkcs12, &data, in_cert_format, 0);
2968
 
  if (result < 0)
2969
 
    {
2970
 
      fprintf (stderr, "p12_import: %s\n", gnutls_strerror (result));
2971
 
      exit (1);
2972
 
    }
2973
 
 
2974
2121
  result = gnutls_pkcs12_verify_mac (pkcs12, password);
2975
2122
  if (result < 0)
2976
 
    {
2977
 
      fprintf (stderr, "verify_mac: %s\n", gnutls_strerror (result));
2978
 
      exit (1);
2979
 
    }
2980
 
 
2981
 
 
 
2123
    error (EXIT_FAILURE, 0, "verify_mac: %s", gnutls_strerror (result));
2982
2124
 
2983
2125
  index = 0;
2984
2126
 
2985
 
  do
 
2127
  for (index = 0; ; index++)
2986
2128
    {
2987
2129
      result = gnutls_pkcs12_bag_init (&bag);
2988
2130
      if (result < 0)
2989
 
        {
2990
 
          fprintf (stderr, "bag_init: %s\n", gnutls_strerror (result));
2991
 
          exit (1);
2992
 
        }
 
2131
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));
2993
2132
 
2994
 
      ret = gnutls_pkcs12_get_bag (pkcs12, index, bag);
2995
 
      if (ret < 0)
2996
 
        {
2997
 
          break;
2998
 
        }
 
2133
      result = gnutls_pkcs12_get_bag (pkcs12, index, bag);
 
2134
      if (result < 0)
 
2135
        break;
2999
2136
 
3000
2137
      result = gnutls_pkcs12_bag_get_count (bag);
3001
2138
      if (result < 0)
3002
 
        {
3003
 
          fprintf (stderr, "bag_init: %s\n", gnutls_strerror (result));
3004
 
          exit (1);
3005
 
        }
 
2139
        error (EXIT_FAILURE, 0, "bag_count: %s", gnutls_strerror (result));
3006
2140
 
3007
2141
      fprintf (outfile, "BAG #%d\n", index);
3008
2142
 
3009
2143
      result = gnutls_pkcs12_bag_get_type (bag, 0);
3010
2144
      if (result < 0)
3011
 
        {
3012
 
          fprintf (stderr, "bag_init: %s\n", gnutls_strerror (result));
3013
 
          exit (1);
3014
 
        }
3015
 
 
 
2145
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));
3016
2146
 
3017
2147
      if (result == GNUTLS_BAG_ENCRYPTED)
3018
2148
        {
3022
2152
          result = gnutls_pkcs12_bag_decrypt (bag, password);
3023
2153
 
3024
2154
          if (result < 0)
3025
 
            {
3026
 
              fprintf (stderr, "bag_decrypt: %s\n", gnutls_strerror (result));
3027
 
              exit (1);
3028
 
            }
 
2155
            error (EXIT_FAILURE, 0, "bag_decrypt: %s",
 
2156
                   gnutls_strerror (result));
3029
2157
 
3030
2158
          result = gnutls_pkcs12_bag_get_count (bag);
3031
2159
          if (result < 0)
3032
 
            {
3033
 
              fprintf (stderr, "get_count: %s\n", gnutls_strerror (result));
3034
 
              exit (1);
3035
 
            }
3036
 
 
 
2160
            error (EXIT_FAILURE, 0, "encrypted bag_count: %s",
 
2161
                   gnutls_strerror (result));
3037
2162
        }
3038
2163
 
3039
2164
      print_bag_data (bag);
3040
2165
 
3041
2166
      gnutls_pkcs12_bag_deinit (bag);
3042
 
 
3043
 
      index++;
3044
2167
    }
3045
 
  while (ret == 0);
3046
 
 
3047
 
 
3048
2168
}
3049
2169
 
3050
2170
void
3051
2171
pkcs7_info (void)
3052
2172
{
3053
 
  gnutls_pkcs7 pkcs7;
 
2173
  gnutls_pkcs7_t pkcs7;
3054
2174
  int result;
3055
2175
  size_t size;
3056
 
  gnutls_datum data, b64;
 
2176
  gnutls_datum_t data, b64;
3057
2177
  int index, count;
3058
2178
 
3059
 
  size = fread (buffer, 1, sizeof (buffer) - 1, infile);
3060
 
  buffer[size] = 0;
3061
 
 
3062
 
  data.data = buffer;
3063
 
  data.size = size;
3064
 
 
3065
2179
  result = gnutls_pkcs7_init (&pkcs7);
3066
2180
  if (result < 0)
3067
 
    {
3068
 
      fprintf (stderr, "p7_init: %s\n", gnutls_strerror (result));
3069
 
      exit (1);
3070
 
    }
3071
 
 
3072
 
  result = gnutls_pkcs7_import (pkcs7, &data, in_cert_format);
 
2181
    error (EXIT_FAILURE, 0, "p7_init: %s", gnutls_strerror (result));
 
2182
 
 
2183
  data.data = fread_file (infile, &size);
 
2184
  data.size = size;
 
2185
 
 
2186
  result = gnutls_pkcs7_import (pkcs7, &data, info.incert_format);
 
2187
  free (data.data);
3073
2188
  if (result < 0)
3074
 
    {
3075
 
      fprintf (stderr, "p7_import: %s\n", gnutls_strerror (result));
3076
 
      exit (1);
3077
 
    }
 
2189
    error (EXIT_FAILURE, 0, "Import error: %s", gnutls_strerror (result));
3078
2190
 
3079
2191
  /* Read and print the certificates.
3080
2192
   */
3081
2193
  result = gnutls_pkcs7_get_crt_count (pkcs7);
3082
2194
  if (result < 0)
3083
 
    {
3084
 
      fprintf (stderr, "p7_count: %s\n", gnutls_strerror (result));
3085
 
      exit (1);
3086
 
    }
 
2195
    error (EXIT_FAILURE, 0, "p7_crt_count: %s", gnutls_strerror (result));
3087
2196
 
3088
2197
  count = result;
3089
2198
 
3090
2199
  if (count > 0)
3091
 
    fprintf (outfile, "Certificates: %u\n", count);
 
2200
    fprintf (outfile, "Number of certificates: %u\n", count);
3092
2201
 
3093
2202
  for (index = 0; index < count; index++)
3094
2203
    {
 
2204
      fputs ("\n", outfile);
 
2205
 
3095
2206
      size = sizeof (buffer);
3096
2207
      result = gnutls_pkcs7_get_crt_raw (pkcs7, index, buffer, &size);
3097
2208
      if (result < 0)
3098
 
        {
3099
2209
          break;
3100
 
        }
3101
2210
 
3102
2211
      data.data = buffer;
3103
2212
      data.size = size;
3104
2213
 
3105
2214
      result = gnutls_pem_base64_encode_alloc ("CERTIFICATE", &data, &b64);
3106
2215
      if (result < 0)
3107
 
        {
3108
 
          fprintf (stderr, "error encoding: %s\n", gnutls_strerror (result));
3109
 
          exit (1);
3110
 
        }
 
2216
        error (EXIT_FAILURE, 0, "encoding: %s", gnutls_strerror (result));
3111
2217
 
3112
2218
      fputs (b64.data, outfile);
3113
 
      fputs ("\n", outfile);
3114
2219
      gnutls_free (b64.data);
3115
2220
    }
3116
2221
 
3118
2223
   */
3119
2224
  result = gnutls_pkcs7_get_crl_count (pkcs7);
3120
2225
  if (result < 0)
3121
 
    {
3122
 
      fprintf (stderr, "p7_count: %s\n", gnutls_strerror (result));
3123
 
      exit (1);
3124
 
    }
 
2226
    error (EXIT_FAILURE, 0, "p7_crl_count: %s", gnutls_strerror (result));
3125
2227
 
3126
2228
  count = result;
3127
2229
 
3128
2230
  if (count > 0)
3129
 
    fprintf (outfile, "\nCRLs: %u\n", count);
 
2231
    fprintf (outfile, "\nNumber of CRLs: %u\n", count);
3130
2232
 
3131
2233
  for (index = 0; index < count; index++)
3132
2234
    {
 
2235
      fputs ("\n", outfile);
 
2236
 
3133
2237
      size = sizeof (buffer);
3134
2238
      result = gnutls_pkcs7_get_crl_raw (pkcs7, index, buffer, &size);
3135
2239
      if (result < 0)
3136
 
        {
3137
2240
          break;
3138
 
        }
3139
2241
 
3140
2242
      data.data = buffer;
3141
2243
      data.size = size;
3142
2244
 
3143
2245
      result = gnutls_pem_base64_encode_alloc ("X509 CRL", &data, &b64);
3144
2246
      if (result < 0)
3145
 
        {
3146
 
          fprintf (stderr, "error encoding: %s\n", gnutls_strerror (result));
3147
 
          exit (1);
3148
 
        }
 
2247
        error (EXIT_FAILURE, 0, "encoding: %s", gnutls_strerror (result));
3149
2248
 
3150
2249
      fputs (b64.data, outfile);
3151
 
      fputs ("\n", outfile);
3152
2250
      gnutls_free (b64.data);
3153
2251
    }
3154
 
 
3155
 
 
3156
2252
}
3157
2253
 
3158
2254
void
3168
2264
    {
3169
2265
      len = getline (&lineptr, &linesize, infile);
3170
2266
      if (len == -1)
3171
 
        {
3172
 
          fprintf (stderr, "Cannot find RFC 2822 header/body separator\n");
3173
 
          exit (1);
3174
 
        }
 
2267
        error (EXIT_FAILURE, 0, "Cannot find RFC 2822 header/body separator");
3175
2268
    }
3176
2269
  while (strcmp (lineptr, "\r\n") != 0 && strcmp (lineptr, "\n") != 0);
3177
2270
 
3179
2272
    {
3180
2273
      len = getline (&lineptr, &linesize, infile);
3181
2274
      if (len == -1)
3182
 
        {
3183
 
          fprintf (stderr, "Message has RFC 2822 header but no body\n");
3184
 
          exit (1);
3185
 
        }
 
2275
        error (EXIT_FAILURE, 0, "Message has RFC 2822 header but no body");
3186
2276
    }
3187
2277
  while (strcmp (lineptr, "\r\n") == 0 && strcmp (lineptr, "\n") == 0);
3188
2278
 
3204
2294
  free (lineptr);
3205
2295
}
3206
2296
 
3207
 
#else /* ENABLE_PKI */
3208
 
 
3209
 
#include <stdio.h>
3210
 
 
3211
 
int
3212
 
main (int argc, char **argv)
3213
 
{
3214
 
  printf ("\nX.509 PKI not supported. This program is a dummy.\n\n");
3215
 
  return 1;
3216
 
};
3217
 
 
3218
 
#endif
3219
 
 
3220
2297
void
3221
2298
certtool_version (void)
3222
2299
{
3223
 
  const char *v = gnutls_check_version (NULL);
3224
 
 
3225
 
  printf ("certtool (GnuTLS) %s\n", LIBGNUTLS_VERSION);
3226
 
  if (strcmp (v, LIBGNUTLS_VERSION) != 0)
3227
 
    printf ("libgnutls %s\n", v);
3228
 
}
3229
 
 
3230
 
void
3231
 
print_license (void)
3232
 
{
3233
 
  fputs ("\nCopyright (C) 2004 Free Software Foundation\n"
3234
 
         "This program is free software; you can redistribute it and/or modify \n"
3235
 
         "it under the terms of the GNU General Public License as published by \n"
3236
 
         "the Free Software Foundation; either version 2 of the License, or \n"
3237
 
         "(at your option) any later version. \n" "\n"
3238
 
         "This program is distributed in the hope that it will be useful, \n"
3239
 
         "but WITHOUT ANY WARRANTY; without even the implied warranty of \n"
3240
 
         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n"
3241
 
         "GNU General Public License for more details. \n" "\n"
3242
 
         "You should have received a copy of the GNU General Public License \n"
3243
 
         "along with this program; if not, write to the Free Software \n"
3244
 
         "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n",
3245
 
         stdout);
 
2300
  version_etc (stdout, program_name, PACKAGE_STRING,
 
2301
               gnutls_check_version (NULL), "Nikos Mavroyanopoulos",
 
2302
               "Simon Josefsson", (char*) NULL);
3246
2303
}