Option to set current cert to server certificate.
[openssl.git] / crypto / x509 / x509_trs.c
index 0de252c65d5cc138e6e4d34bcd577353d683c61a..3d7e06815c8e04eee3bc2d50e958ec3a493ce57f 100644 (file)
@@ -1,5 +1,5 @@
 /* x509_trs.c */
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 1999.
  */
 /* ====================================================================
@@ -80,9 +80,12 @@ static int (*default_trust)(int id, X509 *x, int flags) = obj_trust;
 static X509_TRUST trstandard[] = {
 {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL},
 {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, NULL},
-{X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Client", NID_server_auth, NULL},
+{X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, NULL},
 {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, NULL},
-{X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL}
+{X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, NULL},
+{X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL},
+{X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, NULL},
+{X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
 };
 
 #define X509_TRUST_COUNT       (sizeof(trstandard)/sizeof(X509_TRUST))
@@ -99,10 +102,10 @@ static int tr_cmp(const X509_TRUST * const *a,
 
 int (*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int)
 {
-int (*oldtrust)(int , X509 *, int);
-oldtrust = default_trust;
-default_trust = trust;
-return oldtrust;
+       int (*oldtrust)(int , X509 *, int);
+       oldtrust = default_trust;
+       default_trust = trust;
+       return oldtrust;
 }
 
 
@@ -111,6 +114,15 @@ int X509_check_trust(X509 *x, int id, int flags)
        X509_TRUST *pt;
        int idx;
        if(id == -1) return 1;
+       /* We get this as a default value */
+       if (id == 0)
+               {
+               int rv;
+               rv = obj_trust(NID_anyExtendedKeyUsage, x, 0);
+               if (rv != X509_TRUST_UNTRUSTED)
+                       return rv;
+               return trust_compat(NULL, x, 0);
+               }
        idx = X509_TRUST_get_by_id(id);
        if(idx == -1) return default_trust(id, x, flags);
        pt = X509_TRUST_get0(idx);
@@ -126,7 +138,7 @@ int X509_TRUST_get_count(void)
 X509_TRUST * X509_TRUST_get0(int idx)
 {
        if(idx < 0) return NULL;
-       if(idx < X509_TRUST_COUNT) return trstandard + idx;
+       if(idx < (int)X509_TRUST_COUNT) return trstandard + idx;
        return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
 }
 
@@ -143,6 +155,16 @@ int X509_TRUST_get_by_id(int id)
        return idx + X509_TRUST_COUNT;
 }
 
+int X509_TRUST_set(int *t, int trust)
+{
+       if(X509_TRUST_get_by_id(trust) == -1) {
+               X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST);
+               return 0;
+       }
+       *t = trust;
+       return 1;
+}
+
 int X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
                                        char *name, int arg1, void *arg2)
 {
@@ -207,7 +229,7 @@ static void trtable_free(X509_TRUST *p)
 
 void X509_TRUST_cleanup(void)
 {
-       int i;
+       unsigned int i;
        for(i = 0; i < X509_TRUST_COUNT; i++) trtable_free(trstandard + i);
        sk_X509_TRUST_pop_free(trtable, trtable_free);
        trtable = NULL;
@@ -230,7 +252,8 @@ int X509_TRUST_get_trust(X509_TRUST *xp)
 
 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
 {
-       if(x->aux) return obj_trust(trust->arg1, x, flags);
+       if(x->aux && (x->aux->trust || x->aux->reject))
+               return obj_trust(trust->arg1, x, flags);
        /* we don't have any trust settings: for compatibility
         * we return trusted if it is self signed
         */