~ubuntu-branches/ubuntu/maverick/openssl/maverick

« back to all changes in this revision

Viewing changes to crypto/x509v3/v3_purp.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2005-12-13 21:37:42 UTC
  • mto: (11.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051213213742-d0ydaylf80l16bj1
Tags: upstream-0.9.8a
ImportĀ upstreamĀ versionĀ 0.9.8a

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
 
64
64
static void x509v3_cache_extensions(X509 *x);
65
65
 
66
 
static int ca_check(const X509 *x);
67
66
static int check_ssl_ca(const X509 *x);
68
67
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca);
69
68
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
140
139
X509_PURPOSE * X509_PURPOSE_get0(int idx)
141
140
{
142
141
        if(idx < 0) return NULL;
143
 
        if(idx < X509_PURPOSE_COUNT) return xstandard + idx;
 
142
        if(idx < (int)X509_PURPOSE_COUNT) return xstandard + idx;
144
143
        return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
145
144
}
146
145
 
240
239
 
241
240
void X509_PURPOSE_cleanup(void)
242
241
{
243
 
        int i;
 
242
        unsigned int i;
244
243
        sk_X509_PURPOSE_pop_free(xptable, xptable_free);
245
244
        for(i = 0; i < X509_PURPOSE_COUNT; i++) xptable_free(xstandard + i);
246
245
        xptable = NULL;
286
285
                NID_key_usage,          /* 83 */
287
286
                NID_subject_alt_name,   /* 85 */
288
287
                NID_basic_constraints,  /* 87 */
289
 
                NID_ext_key_usage       /* 126 */
 
288
                NID_ext_key_usage,      /* 126 */
 
289
                NID_proxyCertInfo       /* 661 */
290
290
        };
291
291
 
292
292
        int ex_nid;
307
307
static void x509v3_cache_extensions(X509 *x)
308
308
{
309
309
        BASIC_CONSTRAINTS *bs;
 
310
        PROXY_CERT_INFO_EXTENSION *pci;
310
311
        ASN1_BIT_STRING *usage;
311
312
        ASN1_BIT_STRING *ns;
312
313
        EXTENDED_KEY_USAGE *extusage;
335
336
                BASIC_CONSTRAINTS_free(bs);
336
337
                x->ex_flags |= EXFLAG_BCONS;
337
338
        }
 
339
        /* Handle proxy certificates */
 
340
        if((pci=X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
 
341
                if (x->ex_flags & EXFLAG_CA
 
342
                    || X509_get_ext_by_NID(x, NID_subject_alt_name, 0) >= 0
 
343
                    || X509_get_ext_by_NID(x, NID_issuer_alt_name, 0) >= 0) {
 
344
                        x->ex_flags |= EXFLAG_INVALID;
 
345
                }
 
346
                if (pci->pcPathLengthConstraint) {
 
347
                        x->ex_pcpathlen =
 
348
                                ASN1_INTEGER_get(pci->pcPathLengthConstraint);
 
349
                } else x->ex_pcpathlen = -1;
 
350
                PROXY_CERT_INFO_EXTENSION_free(pci);
 
351
                x->ex_flags |= EXFLAG_PROXY;
 
352
        }
338
353
        /* Handle key usage */
339
354
        if((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
340
355
                if(usage->length > 0) {
426
441
#define ns_reject(x, usage) \
427
442
        (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
428
443
 
429
 
static int ca_check(const X509 *x)
 
444
static int check_ca(const X509 *x)
430
445
{
431
446
        /* keyUsage if present should allow cert signing */
432
447
        if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
435
450
                /* If basicConstraints says not a CA then say so */
436
451
                else return 0;
437
452
        } else {
 
453
                /* we support V1 roots for...  uh, I don't really know why. */
438
454
                if((x->ex_flags & V1_ROOT) == V1_ROOT) return 3;
439
455
                /* If key usage present it must have certSign so tolerate it */
440
456
                else if (x->ex_flags & EXFLAG_KUSAGE) return 4;
441
 
                else return 2;
442
 
        }
 
457
                /* Older certificates could have Netscape-specific CA types */
 
458
                else if (x->ex_flags & EXFLAG_NSCERT
 
459
                         && x->ex_nscert & NS_ANY_CA) return 5;
 
460
                /* can this still be regarded a CA certificate?  I doubt it */
 
461
                return 0;
 
462
        }
 
463
}
 
464
 
 
465
int X509_check_ca(X509 *x)
 
466
{
 
467
        if(!(x->ex_flags & EXFLAG_SET)) {
 
468
                CRYPTO_w_lock(CRYPTO_LOCK_X509);
 
469
                x509v3_cache_extensions(x);
 
470
                CRYPTO_w_unlock(CRYPTO_LOCK_X509);
 
471
        }
 
472
 
 
473
        return check_ca(x);
443
474
}
444
475
 
445
476
/* Check SSL CA: common checks for SSL client and server */
446
477
static int check_ssl_ca(const X509 *x)
447
478
{
448
479
        int ca_ret;
449
 
        ca_ret = ca_check(x);
 
480
        ca_ret = check_ca(x);
450
481
        if(!ca_ret) return 0;
451
482
        /* check nsCertType if present */
452
 
        if(x->ex_flags & EXFLAG_NSCERT) {
453
 
                if(x->ex_nscert & NS_SSL_CA) return ca_ret;
454
 
                return 0;
455
 
        }
456
 
        if(ca_ret != 2) return ca_ret;
 
483
        if(ca_ret != 5 || x->ex_nscert & NS_SSL_CA) return ca_ret;
457
484
        else return 0;
458
485
}
459
486
 
498
525
        if(xku_reject(x,XKU_SMIME)) return 0;
499
526
        if(ca) {
500
527
                int ca_ret;
501
 
                ca_ret = ca_check(x);
 
528
                ca_ret = check_ca(x);
502
529
                if(!ca_ret) return 0;
503
530
                /* check nsCertType if present */
504
 
                if(x->ex_flags & EXFLAG_NSCERT) {
505
 
                        if(x->ex_nscert & NS_SMIME_CA) return ca_ret;
506
 
                        return 0;
507
 
                }
508
 
                if(ca_ret != 2) return ca_ret;
 
531
                if(ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) return ca_ret;
509
532
                else return 0;
510
533
        }
511
534
        if(x->ex_flags & EXFLAG_NSCERT) {
539
562
{
540
563
        if(ca) {
541
564
                int ca_ret;
542
 
                if((ca_ret = ca_check(x)) != 2) return ca_ret;
 
565
                if((ca_ret = check_ca(x)) != 2) return ca_ret;
543
566
                else return 0;
544
567
        }
545
568
        if(ku_reject(x, KU_CRL_SIGN)) return 0;
552
575
 
553
576
static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
554
577
{
555
 
        /* Must be a valid CA */
556
 
        if(ca) {
557
 
                int ca_ret;
558
 
                ca_ret = ca_check(x);
559
 
                if(ca_ret != 2) return ca_ret;
560
 
                if(x->ex_flags & EXFLAG_NSCERT) {
561
 
                        if(x->ex_nscert & NS_ANY_CA) return ca_ret;
562
 
                        return 0;
563
 
                }
564
 
                return 0;
565
 
        }
 
578
        /* Must be a valid CA.  Should we really support the "I don't know"
 
579
           value (2)? */
 
580
        if(ca) return check_ca(x);
566
581
        /* leaf certificate is checked in OCSP_verify() */
567
582
        return 1;
568
583
}
624
639
                                return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
625
640
                }
626
641
        }
627
 
        if(ku_reject(issuer, KU_KEY_CERT_SIGN)) return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
 
642
        if(subject->ex_flags & EXFLAG_PROXY)
 
643
                {
 
644
                if(ku_reject(issuer, KU_DIGITAL_SIGNATURE))
 
645
                        return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
 
646
                }
 
647
        else if(ku_reject(issuer, KU_KEY_CERT_SIGN))
 
648
                return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
628
649
        return X509_V_OK;
629
650
}
630
651