Allow return of supported ciphers.
[openssl.git] / ssl / ssl_cert.c
index 2c66460..665623e 100644 (file)
@@ -257,6 +257,7 @@ CERT *ssl_cert_dup(CERT *cert)
                        }
                }
        ret->dh_tmp_cb = cert->dh_tmp_cb;
+       ret->dh_tmp_auto = cert->dh_tmp_auto;
 #endif
 
 #ifndef OPENSSL_NO_ECDH
@@ -1113,81 +1114,74 @@ static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
 int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
        {
        BUF_MEM *buf = s->init_buf;
-       int no_chain;
        int i;
 
        X509 *x;
        STACK_OF(X509) *extra_certs;
        X509_STORE *chain_store;
 
-       if (cpk)
-               x = cpk->x509;
-       else
-               x = NULL;
+       /* TLSv1 sends a chain with nothing in it, instead of an alert */
+       if (!BUF_MEM_grow_clean(buf,10))
+               {
+               SSLerr(SSL_F_SSL_ADD_CERT_CHAIN,ERR_R_BUF_LIB);
+               return 0;
+               }
 
-       if (s->cert->chain_store)
-               chain_store = s->cert->chain_store;
-       else
-               chain_store = s->ctx->cert_store;
+       if (!cpk || !cpk->x509)
+               return 1;
+
+       x = cpk->x509;
 
        /* If we have a certificate specific chain use it, else use
         * parent ctx.
         */
-       if (cpk && cpk->chain)
+       if (cpk->chain)
                extra_certs = cpk->chain;
        else
                extra_certs = s->ctx->extra_certs;
 
        if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
-               no_chain = 1;
+               chain_store = NULL;
+       else if (s->cert->chain_store)
+               chain_store = s->cert->chain_store;
        else
-               no_chain = 0;
+               chain_store = s->ctx->cert_store;
 
-       /* TLSv1 sends a chain with nothing in it, instead of an alert */
-       if (!BUF_MEM_grow_clean(buf,10))
-               {
-               SSLerr(SSL_F_SSL_ADD_CERT_CHAIN,ERR_R_BUF_LIB);
-               return 0;
-               }
-       if (x != NULL)
+       if (chain_store)
                {
-               if (no_chain)
+               X509_STORE_CTX xs_ctx;
+
+               if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL))
                        {
-                       if (!ssl_add_cert_to_buf(buf, l, x))
-                               return 0;
+                       SSLerr(SSL_F_SSL_ADD_CERT_CHAIN,ERR_R_X509_LIB);
+                       return(0);
                        }
-               else
+               X509_verify_cert(&xs_ctx);
+               /* Don't leave errors in the queue */
+               ERR_clear_error();
+               for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
                        {
-                       X509_STORE_CTX xs_ctx;
+                       x = sk_X509_value(xs_ctx.chain, i);
 
-                       if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL))
-                               {
-                               SSLerr(SSL_F_SSL_ADD_CERT_CHAIN,ERR_R_X509_LIB);
-                               return(0);
-                               }
-                       X509_verify_cert(&xs_ctx);
-                       /* Don't leave errors in the queue */
-                       ERR_clear_error();
-                       for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
+                       if (!ssl_add_cert_to_buf(buf, l, x))
                                {
-                               x = sk_X509_value(xs_ctx.chain, i);
-
-                               if (!ssl_add_cert_to_buf(buf, l, x))
-                                       {
-                                       X509_STORE_CTX_cleanup(&xs_ctx);
-                                       return 0;
-                                       }
+                               X509_STORE_CTX_cleanup(&xs_ctx);
+                               return 0;
                                }
-                       X509_STORE_CTX_cleanup(&xs_ctx);
                        }
+               X509_STORE_CTX_cleanup(&xs_ctx);
                }
-       for (i=0; i<sk_X509_num(extra_certs); i++)
+       else
                {
-               x=sk_X509_value(extra_certs,i);
                if (!ssl_add_cert_to_buf(buf, l, x))
                        return 0;
+               for (i=0; i<sk_X509_num(extra_certs); i++)
+                       {
+                       x=sk_X509_value(extra_certs,i);
+                       if (!ssl_add_cert_to_buf(buf, l, x))
+                               return 0;
+                       }
                }
-
        return 1;
        }
 
@@ -1199,6 +1193,7 @@ int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
        STACK_OF(X509) *chain = NULL, *untrusted = NULL;
        X509 *x;
        int i, rv = 0;
+       unsigned long error;
 
        if (!cpk->x509)
                {
@@ -1215,11 +1210,23 @@ int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
                        {
                        x = sk_X509_value(cpk->chain, i);
                        if (!X509_STORE_add_cert(chain_store, x))
-                               goto err;
+                               {
+                               error = ERR_peek_last_error();
+                               if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
+                                   ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
+                                       goto err;
+                               ERR_clear_error();
+                               }
                        }
                /* Add EE cert too: it might be self signed */
                if (!X509_STORE_add_cert(chain_store, cpk->x509))
-                       goto err;
+                       {
+                       error = ERR_peek_last_error();
+                       if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
+                           ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
+                               goto err;
+                       ERR_clear_error();
+                       }
                }
        else
                {
@@ -1241,8 +1248,10 @@ int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
        i = X509_verify_cert(&xs_ctx);
        if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)
                {
-               ERR_clear_error();
+               if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
+                       ERR_clear_error();
                i = 1;
+               rv = 2;
                }
        if (i > 0)
                chain = X509_STORE_CTX_get1_chain(&xs_ctx);
@@ -1277,7 +1286,8 @@ int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
                        }
                }
        cpk->chain = chain;
-       rv = 1;
+       if (rv == 0)
+               rv = 1;
        err:
        if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
                X509_STORE_free(chain_store);