Skip to content
s_client.c 67.1 KiB
Newer Older
				}

			sbuf_len-=i;;
			sbuf_off+=i;
			if (sbuf_len <= 0)
				{
				read_ssl=1;
				write_tty=0;
				}
			}
		else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))
#ifdef RENEG
{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }
#endif
			k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );
#else
/* Demo for pending and peek :-) */
			k=SSL_read(con,sbuf,16);
{ char zbuf[10240]; 
printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));
}
#endif

			switch (SSL_get_error(con,k))
				{
			case SSL_ERROR_NONE:
				if (k <= 0)
					goto end;
				sbuf_off=0;
				sbuf_len=k;

				read_ssl=0;
				write_tty=1;
				break;
			case SSL_ERROR_WANT_WRITE:
				BIO_printf(bio_c_out,"read W BLOCK\n");
				write_ssl=1;
				read_tty=0;
				break;
			case SSL_ERROR_WANT_READ:
				BIO_printf(bio_c_out,"read R BLOCK\n");
				write_tty=0;
				read_ssl=1;
				if ((read_tty == 0) && (write_ssl == 0))
					write_ssl=1;
				break;
			case SSL_ERROR_WANT_X509_LOOKUP:
				BIO_printf(bio_c_out,"read X BLOCK\n");
				break;
			case SSL_ERROR_SYSCALL:
					BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
				else
					BIO_printf(bio_err,"read:errno=%d\n",ret);
				goto shut;
			case SSL_ERROR_ZERO_RETURN:
				BIO_printf(bio_c_out,"closed\n");
				goto shut;
			case SSL_ERROR_SSL:
				ERR_print_errors(bio_err);
				goto shut;
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
#if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
Richard Levitte's avatar
Richard Levitte committed
		else if (_kbhit())
#else
		else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
Richard Levitte's avatar
Richard Levitte committed
#endif
#elif defined (OPENSSL_SYS_NETWARE)
Ulf Möller's avatar
Ulf Möller committed
#elif defined(OPENSSL_SYS_BEOS_R5)
		else if (stdin_set)
		else if (FD_ISSET(fileno(stdin),&readfds))
				i=raw_read_stdin(cbuf,BUFSIZZ/2);
				lf_num = 0;
				/* both loops are skipped when i <= 0 */
				for (j = 0; j < i; j++)
					if (cbuf[j] == '\n')
						lf_num++;
				for (j = i-1; j >= 0; j--)
					{
					cbuf[j+lf_num] = cbuf[j];
					if (cbuf[j] == '\n')
						{
						lf_num--;
						i++;
						cbuf[j+lf_num] = '\r';
						}
					}
				assert(lf_num == 0);
				}
			else
				i=raw_read_stdin(cbuf,BUFSIZZ);
			if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))
				{
				BIO_printf(bio_err,"DONE\n");
			if ((!c_ign_eof) && (cbuf[0] == 'R'))
				BIO_printf(bio_err,"RENEGOTIATING\n");
				SSL_renegotiate(con);
				cbuf_len=0;
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
#ifndef OPENSSL_NO_HEARTBEATS
			else if ((!c_ign_eof) && (cbuf[0] == 'B'))
 				{
				BIO_printf(bio_err,"HEARTBEATING\n");
				SSL_heartbeat(con);
				cbuf_len=0;
				}
#endif
#ifdef CHARSET_EBCDIC
				ebcdic2ascii(cbuf, cbuf, i);
#endif
			read_tty=0;
	if (in_init)
		print_stuff(bio_c_out,con,full_log);
	SSL_shutdown(con);
	SHUTDOWN(SSL_get_fd(con));
end:
	if (con != NULL)
		{
		if (prexit != 0)
			print_stuff(bio_c_out,con,1);
		SSL_free(con);
		}
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
	if (next_proto.data)
		OPENSSL_free(next_proto.data);
#endif
	if (ctx != NULL) SSL_CTX_free(ctx);
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
	if (cert)
		X509_free(cert);
	if (crls)
		sk_X509_CRL_pop_free(crls, X509_CRL_free);
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
	if (key)
		EVP_PKEY_free(key);
	if (chain)
		sk_X509_pop_free(chain, X509_free);
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
	if (pass)
		OPENSSL_free(pass);
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
	if (vpm)
		X509_VERIFY_PARAM_free(vpm);
	if (ssl_args)
		sk_OPENSSL_STRING_free(ssl_args);
	if (cctx)
		SSL_CONF_CTX_free(cctx);
#ifndef OPENSSL_NO_JPAKE
	if (jpake_secret && psk_key)
		OPENSSL_free(psk_key);
#endif
	if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); }
	if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); }
	if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); }
	if (bio_c_out != NULL)
		{
		BIO_free(bio_c_out);
		bio_c_out=NULL;
		}
	if (bio_c_msg != NULL)
		{
		BIO_free(bio_c_msg);
		bio_c_msg=NULL;
		}
Ulf Möller's avatar
Ulf Möller committed
static void print_stuff(BIO *bio, SSL *s, int full)
Nils Larsch's avatar
Nils Larsch committed
	static const char *space="                ";
	STACK_OF(X509) *sk;
	STACK_OF(X509_NAME) *sk2;
	const SSL_CIPHER *c;
#ifndef OPENSSL_NO_COMP
	const COMP_METHOD *comp, *expansion;
Ben Laurie's avatar
Ben Laurie committed
	unsigned char *exportedkeymat;
		sk=SSL_get_peer_cert_chain(s);
		if (sk != NULL)
			{
			got_a_chain = 1; /* we don't have it for SSL2 (yet) */

			BIO_printf(bio,"---\nCertificate chain\n");
			for (i=0; i<sk_X509_num(sk); i++)
				X509_NAME_oneline(X509_get_subject_name(
					sk_X509_value(sk,i)),buf,sizeof buf);
				BIO_printf(bio,"%2d s:%s\n",i,buf);
				X509_NAME_oneline(X509_get_issuer_name(
					sk_X509_value(sk,i)),buf,sizeof buf);
				BIO_printf(bio,"   i:%s\n",buf);
				if (c_showcerts)
					PEM_write_bio_X509(bio,sk_X509_value(sk,i));
				}
			}

		BIO_printf(bio,"---\n");
		peer=SSL_get_peer_certificate(s);
		if (peer != NULL)
			{
			BIO_printf(bio,"Server certificate\n");
			if (!(c_showcerts && got_a_chain)) /* Redundant if we showed the whole chain */
				PEM_write_bio_X509(bio,peer);
			X509_NAME_oneline(X509_get_subject_name(peer),
				buf,sizeof buf);
			BIO_printf(bio,"subject=%s\n",buf);
			X509_NAME_oneline(X509_get_issuer_name(peer),
				buf,sizeof buf);
			BIO_printf(bio,"issuer=%s\n",buf);
			}
		else
			BIO_printf(bio,"no peer certificate available\n");

		sk2=SSL_get_client_CA_list(s);
		if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0))
			{
			BIO_printf(bio,"---\nAcceptable client certificate CA names\n");
			for (i=0; i<sk_X509_NAME_num(sk2); i++)
				xn=sk_X509_NAME_value(sk2,i);
				X509_NAME_oneline(xn,buf,sizeof(buf));
				BIO_write(bio,buf,strlen(buf));
				BIO_write(bio,"\n",1);
				}
			}
		else
			{
			BIO_printf(bio,"---\nNo client certificate CA names sent\n");
			}
		p=SSL_get_shared_ciphers(s,buf,sizeof buf);
Bodo Möller's avatar
Bodo Möller committed
			/* This works only for SSL 2.  In later protocol
			 * versions, the client does not know what other
			 * ciphers (in addition to the one to be used
			 * in the current connection) the server supports. */

			BIO_printf(bio,"---\nCiphers common between both SSL endpoints:\n");
			j=i=0;
			while (*p)
				{
				if (*p == ':')
					{
					BIO_write(bio,space,15-j%25);
					i++;
					j=0;
					BIO_write(bio,((i%3)?" ":"\n"),1);
					}
				else
					{
					BIO_write(bio,p,1);
					j++;
					}
				p++;
				}
			BIO_write(bio,"\n",1);
			}

		BIO_printf(bio,"---\nSSL handshake has read %ld bytes and written %ld bytes\n",
			BIO_number_read(SSL_get_rbio(s)),
			BIO_number_written(SSL_get_wbio(s)));
		}
	BIO_printf(bio,(SSL_cache_hit(s)?"---\nReused, ":"---\nNew, "));
	c=SSL_get_current_cipher(s);
	BIO_printf(bio,"%s, Cipher is %s\n",
		SSL_CIPHER_get_version(c),
		SSL_CIPHER_get_name(c));
	if (peer != NULL) {
		EVP_PKEY *pktmp;
		pktmp = X509_get_pubkey(peer);
		BIO_printf(bio,"Server public key is %d bit\n",
							 EVP_PKEY_bits(pktmp));
		EVP_PKEY_free(pktmp);
	}
	BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
			SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
#ifndef OPENSSL_NO_COMP
	expansion=SSL_get_current_expansion(s);
	BIO_printf(bio,"Compression: %s\n",
		comp ? SSL_COMP_get_name(comp) : "NONE");
	BIO_printf(bio,"Expansion: %s\n",
		expansion ? SSL_COMP_get_name(expansion) : "NONE");
#ifdef SSL_DEBUG
	{
	/* Print out local port of connection: useful for debugging */
	int sock;
	struct sockaddr_in ladd;
	socklen_t ladd_size = sizeof(ladd);
	sock = SSL_get_fd(s);
	getsockname(sock, (struct sockaddr *)&ladd, &ladd_size);
	BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port));
	}
#endif

Adam Langley's avatar
Adam Langley committed
#if !defined(OPENSSL_NO_TLSEXT)
# if !defined(OPENSSL_NO_NEXTPROTONEG)
	if (next_proto.status != -1) {
		const unsigned char *proto;
		unsigned int proto_len;
		SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
		BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
		BIO_write(bio, proto, proto_len);
		BIO_write(bio, "\n", 1);
	}
Adam Langley's avatar
Adam Langley committed
	{
		const unsigned char *proto;
		unsigned int proto_len;
		SSL_get0_alpn_selected(s, &proto, &proto_len);
		if (proto_len > 0)
			{
			BIO_printf(bio, "ALPN protocol: ");
			BIO_write(bio, proto, proto_len);
			BIO_write(bio, "\n", 1);
			}
		else
			BIO_printf(bio, "No ALPN negotiated\n");
	}
Ben Laurie's avatar
Ben Laurie committed
 	{
 	SRTP_PROTECTION_PROFILE *srtp_profile=SSL_get_selected_srtp_profile(s);
 
	if(srtp_profile)
		BIO_printf(bio,"SRTP Extension negotiated, profile=%s\n",
			   srtp_profile->name);
	}
 
	SSL_SESSION_print(bio,SSL_get_session(s));
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
	if (keymatexportlabel != NULL)
		{
Ben Laurie's avatar
Ben Laurie committed
		BIO_printf(bio, "Keying material exporter:\n");
		BIO_printf(bio, "    Label: '%s'\n", keymatexportlabel);
		BIO_printf(bio, "    Length: %i bytes\n", keymatexportlen);
		exportedkeymat = OPENSSL_malloc(keymatexportlen);
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
		if (exportedkeymat != NULL)
			{
			if (!SSL_export_keying_material(s, exportedkeymat,
						        keymatexportlen,
						        keymatexportlabel,
						        strlen(keymatexportlabel),
						        NULL, 0, 0))
				{
				BIO_printf(bio, "    Error\n");
				}
			else
				{
Ben Laurie's avatar
Ben Laurie committed
				BIO_printf(bio, "    Keying material: ");
				for (i=0; i<keymatexportlen; i++)
					BIO_printf(bio, "%02X",
						   exportedkeymat[i]);
				BIO_printf(bio, "\n");
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
				}
Ben Laurie's avatar
Ben Laurie committed
			OPENSSL_free(exportedkeymat);
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
			}
Ben Laurie's avatar
Ben Laurie committed
		}
	BIO_printf(bio,"---\n");
	if (peer != NULL)
		X509_free(peer);
	/* flush, or debugging output gets mixed with http response */
Dr. Stephen Henson's avatar
Dr. Stephen Henson committed
	(void)BIO_flush(bio);
#ifndef OPENSSL_NO_TLSEXT

static int ocsp_resp_cb(SSL *s, void *arg)
	{
	const unsigned char *p;
	int len;
	OCSP_RESPONSE *rsp;
	len = SSL_get_tlsext_status_ocsp_resp(s, &p);
	BIO_puts(arg, "OCSP response: ");
	if (!p)
		{
		BIO_puts(arg, "no response sent\n");
		return 1;
		}
	rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
	if (!rsp)
		{
		BIO_puts(arg, "response parse error\n");
		BIO_dump_indent(arg, (char *)p, len, 4);
		return 0;
		}
	BIO_puts(arg, "\n======================================\n");
	OCSP_RESPONSE_print(arg, rsp, 0);
	BIO_puts(arg, "======================================\n");
	OCSP_RESPONSE_free(rsp);
	return 1;
	}
static int authz_tlsext_cb(SSL *s, unsigned short ext_type,
			   const unsigned char *in,
			   unsigned short inlen, int *al,
			   void *arg)
Ben Laurie's avatar
Ben Laurie committed
	{
	if (TLSEXT_TYPE_server_authz == ext_type)
Ben Laurie's avatar
Ben Laurie committed
		server_provided_server_authz
		  = (memchr(in, TLSEXT_AUTHZDATAFORMAT_dtcp, inlen) != NULL);

	if (TLSEXT_TYPE_client_authz == ext_type)
Ben Laurie's avatar
Ben Laurie committed
		server_provided_client_authz
		  = (memchr(in, TLSEXT_AUTHZDATAFORMAT_dtcp, inlen) != NULL);

	return 1;
	}

static int authz_tlsext_generate_cb(SSL *s, unsigned short ext_type,
				    const unsigned char **out, unsigned short *outlen,
				    int *al, void *arg)
Ben Laurie's avatar
Ben Laurie committed
		{
		/*if auth_require_reneg flag is set, only send extensions if
		  renegotiation has occurred */
		if (!c_auth_require_reneg || (c_auth_require_reneg && SSL_num_renegotiations(s)))
			{
			*out = auth_ext_data;
			*outlen = 1;
			return 1;
			}
		}
Ben Laurie's avatar
Ben Laurie committed
	/* no auth extension to send */
	return -1;
	}

static int suppdata_cb(SSL *s, unsigned short supp_data_type,
		       const unsigned char *in,
		       unsigned short inlen, int *al,
		       void *arg)
	{
	if (supp_data_type == TLSEXT_SUPPLEMENTALDATATYPE_authz_data)
		{
		most_recent_supplemental_data = in;
		most_recent_supplemental_data_length = inlen;
Ben Laurie's avatar
Ben Laurie committed
		}
	return 1;
	}

static int auth_suppdata_generate_cb(SSL *s, unsigned short supp_data_type,
				     const unsigned char **out,
				     unsigned short *outlen, int *al, void *arg)
	{
	if (c_auth && server_provided_client_authz && server_provided_server_authz)
		{
		/*if auth_require_reneg flag is set, only send supplemental data if
		  renegotiation has occurred */
Ben Laurie's avatar
Ben Laurie committed
		if (!c_auth_require_reneg
		    || (c_auth_require_reneg && SSL_num_renegotiations(s)))
			generated_supp_data = OPENSSL_malloc(10);
			memcpy(generated_supp_data, "5432154321", 10);
			*out = generated_supp_data;
Ben Laurie's avatar
Ben Laurie committed
	/* no supplemental data to send */
#endif