Option to set current cert to server certificate.
[openssl.git] / ssl / ssl_cert.c
index 9547814..47c8b86 100644 (file)
@@ -321,36 +321,32 @@ CERT *ssl_cert_dup(CERT *cert)
 
                if (cpk->chain)
                        {
-                       int j;
-                       rpk->chain = sk_X509_dup(cpk->chain);
+                       rpk->chain = X509_chain_up_ref(cpk->chain);
                        if (!rpk->chain)
                                {
                                SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
-                       for (j = 0; j < sk_X509_num(rpk->chain); j++)
-                               {
-                               X509 *x = sk_X509_value(rpk->chain, j);
-                               CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
-                               }
                        }
                rpk->valid_flags = 0;
-                if (cert->pkeys[i].authz != NULL)
+#ifndef OPENSSL_NO_TLSEXT
+               if (cert->pkeys[i].serverinfo != NULL)
                        {
                        /* Just copy everything. */
-                       ret->pkeys[i].authz_length =
-                               cert->pkeys[i].authz_length;
-                       ret->pkeys[i].authz =
-                               OPENSSL_malloc(ret->pkeys[i].authz_length);
-                       if (ret->pkeys[i].authz == NULL)
+                       ret->pkeys[i].serverinfo =
+                               OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
+                       if (ret->pkeys[i].serverinfo == NULL)
                                {
                                SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
-                               return(NULL);
+                               return NULL;
                                }
-                       memcpy(ret->pkeys[i].authz,
-                              cert->pkeys[i].authz,
-                              cert->pkeys[i].authz_length);
+                       ret->pkeys[i].serverinfo_length =
+                               cert->pkeys[i].serverinfo_length;
+                       memcpy(ret->pkeys[i].serverinfo,
+                              cert->pkeys[i].serverinfo,
+                              cert->pkeys[i].serverinfo_length);
                        }
+#endif
                }
        
        ret->references=1;
@@ -415,6 +411,8 @@ CERT *ssl_cert_dup(CERT *cert)
                ret->chain_store = cert->chain_store;
                }
 
+       ret->ciphers_raw = NULL;
+
        return(ret);
        
 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH)
@@ -464,10 +462,15 @@ void ssl_cert_clear_certs(CERT *c)
                        cpk->chain = NULL;
                        }
 #ifndef OPENSSL_NO_TLSEXT
-                if (cpk->authz != NULL)
-                       OPENSSL_free(cpk->authz);
+               if (cpk->serverinfo)
+                       {
+                       OPENSSL_free(cpk->serverinfo);
+                       cpk->serverinfo = NULL;
+                       cpk->serverinfo_length = 0;
+                       }
 #endif
-               cpk->valid_flags = 0;
+               /* Clear all flags apart from explicit sign */
+               cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
                }
        }
 
@@ -516,6 +519,8 @@ void ssl_cert_free(CERT *c)
                X509_STORE_free(c->verify_store);
        if (c->chain_store)
                X509_STORE_free(c->chain_store);
+       if (c->ciphers_raw)
+               OPENSSL_free(c->ciphers_raw);
        OPENSSL_free(c);
        }
 
@@ -561,18 +566,11 @@ int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
 int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
        {
        STACK_OF(X509) *dchain;
-       X509 *x;
-       int i;
        if (!chain)
                return ssl_cert_set0_chain(c, NULL);
-       dchain = sk_X509_dup(chain);
+       dchain = X509_chain_up_ref(chain);
        if (!dchain)
                return 0;
-       for (i = 0; i < sk_X509_num(dchain); i++)
-               {
-               x = sk_X509_value(dchain, i);
-               CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
-               }
        if (!ssl_cert_set0_chain(c, dchain))
                {
                sk_X509_pop_free(dchain, X509_free);
@@ -601,6 +599,57 @@ int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
        return 1;
        }
 
+int ssl_cert_select_current(CERT *c, X509 *x)
+       {
+       int i;
+       if (x == NULL)
+               return 0;
+       for (i = 0; i < SSL_PKEY_NUM; i++)
+               {
+               if (c->pkeys[i].x509 == x)
+                       {
+                       c->key = &c->pkeys[i];
+                       return 1;
+                       }
+               }
+
+       for (i = 0; i < SSL_PKEY_NUM; i++)
+               {
+               if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x))
+                       {
+                       c->key = &c->pkeys[i];
+                       return 1;
+                       }
+               }
+       return 0;
+       }
+
+int ssl_cert_set_current(CERT *c, long op)
+       {
+       int i, idx;
+       if (!c)
+               return 0;
+       if (op == SSL_CERT_SET_FIRST)
+               idx = 0;
+       else if (op == SSL_CERT_SET_NEXT)
+               {
+               idx = (int)(c->key - c->pkeys + 1);
+               if (idx >= SSL_PKEY_NUM)
+                       return 0;
+               }
+       else
+               return 0;
+       for (i = idx; i < SSL_PKEY_NUM; i++)
+               {
+               if (c->pkeys[i].x509)
+                       {
+                       c->key = &c->pkeys[i];
+                       return 1;
+                       }
+               }
+       return 0;
+       }
+
 void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg)
        {
        c->cert_cb = cb;
@@ -704,6 +753,8 @@ int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
                SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB);
                return(0);
                }
+       /* Set suite B flags if needed */
+       X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s));
 #if 0
        if (SSL_get_verify_depth(s) >= 0)
                X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
@@ -1163,6 +1214,8 @@ int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
                SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
                return 0;
                }
+       /* Set suite B flags if needed */
+       X509_STORE_CTX_set_flags(&xs_ctx, c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
 
        i = X509_verify_cert(&xs_ctx);
        if (i > 0)