static int set_cert_cb(SSL *ssl, void *arg)
	{
	int i, rv;
	SSL_EXCERT *exc = arg;
	SSL_certs_clear(ssl);

	if (!exc)
		return 1;

	/* Go to end of list and traverse backwards since we prepend
	 * newer entries this retains the original order.
	 */
	while (exc->next)
		exc = exc->next;

	i = 0;	

	while(exc)
		{
		i++;
		rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain);
		BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i);
		X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0,
							XN_FLAG_ONELINE);
		BIO_puts(bio_err, "\n");
		
		print_chain_flags(bio_err, rv);
		if (rv & CERT_PKEY_VALID)
			{
			SSL_use_certificate(ssl, exc->cert);
			SSL_use_PrivateKey(ssl, exc->key);
			/* NB: we wouldn't normally do this as it is
			 * not efficient building chains on each connection
			 * better to cache the chain in advance.
			 */
			if (exc->build_chain)
				{
				if (!SSL_build_cert_chain(ssl, 0))
					return 0;
				}
			else if (exc->chain)
				SSL_set1_chain(ssl, exc->chain);
			}
		exc = exc->prev;
		}
	return 1;
	}