~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to sm/certlist.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* certlist.c - build list of certificates
2
 
 *      Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2003, 2004, 2005, 2007,
 
3
 *               2008 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
214
214
same_subject_issuer (const char *subject, const char *issuer, ksba_cert_t cert)
215
215
{
216
216
  char *subject2 = ksba_cert_get_subject (cert, 0);
217
 
  char *issuer2 = ksba_cert_get_subject (cert, 0);
 
217
  char *issuer2 = ksba_cert_get_issuer (cert, 0);
218
218
  int tmp;
219
219
  
220
220
  tmp = (subject && subject2
226
226
  return tmp;
227
227
}
228
228
 
 
229
 
 
230
/* Return true if CERT_A is the same as CERT_B.  */
 
231
int
 
232
gpgsm_certs_identical_p (ksba_cert_t cert_a, ksba_cert_t cert_b)
 
233
{
 
234
  const unsigned char *img_a, *img_b;
 
235
  size_t len_a, len_b;
 
236
 
 
237
  img_a = ksba_cert_get_image (cert_a, &len_a);
 
238
  if (img_a)
 
239
    {
 
240
      img_b = ksba_cert_get_image (cert_b, &len_b);
 
241
      if (img_b && len_a == len_b && !memcmp (img_a, img_b, len_a))
 
242
        return 1; /* Identical. */
 
243
    }
 
244
  return 0;
 
245
}
 
246
 
 
247
 
229
248
/* Return true if CERT is already contained in CERTLIST. */
230
249
static int
231
250
is_cert_in_certlist (ksba_cert_t cert, certlist_t certlist)
257
276
    {
258
277
      certlist_t cl = xtrycalloc (1, sizeof *cl);
259
278
      if (!cl)
260
 
        return OUT_OF_CORE (errno);
 
279
        return out_of_core ();
261
280
      cl->cert = cert;
262
281
      ksba_cert_ref (cert);
263
282
      cl->next = *listaddr;
273
292
   flag in the new create LISTADDR item.  */
274
293
int
275
294
gpgsm_add_to_certlist (ctrl_t ctrl, const char *name, int secret,
276
 
                       CERTLIST *listaddr, int is_encrypt_to)
 
295
                       certlist_t *listaddr, int is_encrypt_to)
277
296
{
278
297
  int rc;
279
298
  KEYDB_SEARCH_DESC desc;
289
308
      else
290
309
        {
291
310
          int wrong_usage = 0;
292
 
          char *subject = NULL;
293
 
          char *issuer = NULL;
 
311
          char *first_subject = NULL;
 
312
          char *first_issuer = NULL;
294
313
 
295
314
        get_next:
296
315
          rc = keydb_search (kh, &desc, 1);
298
317
            rc = keydb_get_cert (kh, &cert);
299
318
          if (!rc)
300
319
            {
 
320
              if (!first_subject)
 
321
                {
 
322
                  /* Save the the subject and the issuer for key usage
 
323
                     and ambiguous name tests. */
 
324
                  first_subject = ksba_cert_get_subject (cert, 0);
 
325
                  first_issuer = ksba_cert_get_issuer (cert, 0);
 
326
                }
301
327
              rc = secret? gpgsm_cert_use_sign_p (cert)
302
328
                         : gpgsm_cert_use_encrypt_p (cert);
303
329
              if (gpg_err_code (rc) == GPG_ERR_WRONG_KEY_USAGE)
307
333
                  if (!wrong_usage)
308
334
                    { /* save the first match */
309
335
                      wrong_usage = rc;
310
 
                      subject = ksba_cert_get_subject (cert, 0);
311
 
                      issuer = ksba_cert_get_subject (cert, 0);
312
336
                      ksba_cert_release (cert);
313
337
                      cert = NULL;
314
338
                      goto get_next;
315
339
                    }
316
 
                  else if (same_subject_issuer (subject, issuer, cert))
 
340
                  else if (same_subject_issuer (first_subject, first_issuer,
 
341
                                                cert))
317
342
                    {
318
343
                      wrong_usage = rc;
319
344
                      ksba_cert_release (cert);
331
356
          
332
357
          if (!rc)
333
358
            {
 
359
              certlist_t dup_certs = NULL;
 
360
 
334
361
            next_ambigious:
335
362
              rc = keydb_search (kh, &desc, 1);
336
363
              if (rc == -1)
338
365
              else if (!rc)
339
366
                {
340
367
                  ksba_cert_t cert2 = NULL;
 
368
                  
 
369
                  /* If this is the first possible duplicate, add the original 
 
370
                     certificate to our list of duplicates.  */
 
371
                  if (!dup_certs)
 
372
                    gpgsm_add_cert_to_certlist (ctrl, cert, &dup_certs, 0);
341
373
 
342
374
                  /* We have to ignore ambigious names as long as
343
 
                     there only fault is a bad key usage */
 
375
                     there only fault is a bad key usage.  This is
 
376
                     required to support encryption and signing
 
377
                     certificates of the same subject.
 
378
 
 
379
                     Further we ignore them if they are due to an
 
380
                     identical certificate (which may happen if a
 
381
                     certificate is accidential duplicated in the
 
382
                     keybox).  */
344
383
                  if (!keydb_get_cert (kh, &cert2))
345
384
                    {
346
 
                      int tmp = (same_subject_issuer (subject, issuer, cert2)
 
385
                      int tmp = (same_subject_issuer (first_subject, 
 
386
                                                      first_issuer, 
 
387
                                                      cert2)
347
388
                                 && ((gpg_err_code (
348
389
                                      secret? gpgsm_cert_use_sign_p (cert2)
349
 
                                            : gpgsm_cert_use_encrypt_p (cert2)
 
390
                                      : gpgsm_cert_use_encrypt_p (cert2)
350
391
                                      )
351
392
                                     )  == GPG_ERR_WRONG_KEY_USAGE));
 
393
                      if (tmp)
 
394
                        gpgsm_add_cert_to_certlist (ctrl, cert2,
 
395
                                                    &dup_certs, 0);
 
396
                      else
 
397
                        {
 
398
                          if (is_cert_in_certlist (cert2, dup_certs))
 
399
                            tmp = 1;
 
400
                        }
 
401
                      
352
402
                      ksba_cert_release (cert2);
353
403
                      if (tmp)
354
404
                        goto next_ambigious;
355
405
                    }
356
406
                  rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
357
407
                }
 
408
              gpgsm_release_certlist (dup_certs);
358
409
            }
359
 
          xfree (subject);
360
 
          xfree (issuer);
 
410
          xfree (first_subject);
 
411
          xfree (first_issuer);
 
412
          first_subject = NULL;
 
413
          first_issuer = NULL;
361
414
 
362
415
          if (!rc && !is_cert_in_certlist (cert, *listaddr))
363
416
            {
375
428
                    }
376
429
                }
377
430
              if (!rc)
378
 
                rc = gpgsm_validate_chain (ctrl, cert, NULL, 0, NULL, 0);
 
431
                rc = gpgsm_validate_chain (ctrl, cert, "", NULL,
 
432
                                           0, NULL, 0, NULL);
379
433
              if (!rc)
380
434
                {
381
 
                  CERTLIST cl = xtrycalloc (1, sizeof *cl);
 
435
                  certlist_t cl = xtrycalloc (1, sizeof *cl);
382
436
                  if (!cl)
383
 
                    rc = OUT_OF_CORE (errno);
 
437
                    rc = out_of_core ();
384
438
                  else 
385
439
                    {
386
440
                      cl->cert = cert; cert = NULL;
398
452
  return rc == -1? gpg_error (GPG_ERR_NO_PUBKEY): rc;
399
453
}
400
454
 
 
455
 
401
456
void
402
 
gpgsm_release_certlist (CERTLIST list)
 
457
gpgsm_release_certlist (certlist_t list)
403
458
{
404
459
  while (list)
405
460
    {
406
 
      CERTLIST cl = list->next;
 
461
      certlist_t cl = list->next;
407
462
      ksba_cert_release (list->cert);
408
463
      xfree (list);
409
464
      list = cl;
412
467
 
413
468
 
414
469
/* Like gpgsm_add_to_certlist, but look only for one certificate.  No
415
 
   chain validation is done. If KEYID is not NULL it is take as an
 
470
   chain validation is done.  If KEYID is not NULL it is taken as an
416
471
   additional filter value which must match the
417
472
   subjectKeyIdentifier. */
418
473
int
464
519
             won't lead to ambiguous names. */
465
520
          if (!rc && !keyid)
466
521
            {
 
522
            next_ambiguous:
467
523
              rc = keydb_search (kh, &desc, 1);
468
524
              if (rc == -1)
469
525
                rc = 0;
470
526
              else 
471
527
                {
472
528
                  if (!rc)
473
 
                    rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
 
529
                    {
 
530
                      ksba_cert_t cert2 = NULL;
 
531
 
 
532
                      if (!keydb_get_cert (kh, &cert2))
 
533
                        {
 
534
                          if (gpgsm_certs_identical_p (*r_cert, cert2))
 
535
                            {
 
536
                              ksba_cert_release (cert2);
 
537
                              goto next_ambiguous;
 
538
                            }
 
539
                          ksba_cert_release (cert2);
 
540
                        }
 
541
                      rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
 
542
                    }
474
543
                  ksba_cert_release (*r_cert);
475
544
                  *r_cert = NULL;
476
545
                }