unsigned char *psk,
unsigned int max_psk_len)
{
- unsigned int psk_len = 0;
int ret;
- BIGNUM *bn = NULL;
+ long key_len;
+ unsigned char *key;
if (c_debug)
BIO_printf(bio_c_out, "psk_client_cb\n");
if (c_debug)
BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity,
ret);
- ret = BN_hex2bn(&bn, psk_key);
- if (!ret) {
- BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
+
+ /* convert the PSK key to binary */
+ key = OPENSSL_hexstr2buf(psk_key, &key_len);
+ if (key == NULL) {
+ BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
psk_key);
- BN_free(bn);
return 0;
}
-
- if ((unsigned int)BN_num_bytes(bn) > max_psk_len) {
+ if (key_len > max_psk_len) {
BIO_printf(bio_err,
- "psk buffer of callback is too small (%d) for key (%d)\n",
- max_psk_len, BN_num_bytes(bn));
- BN_free(bn);
+ "psk buffer of callback is too small (%d) for key (%ld)\n",
+ max_psk_len, key_len);
+ OPENSSL_free(key);
return 0;
}
- psk_len = BN_bn2bin(bn, psk);
- BN_free(bn);
- if (psk_len == 0)
- goto out_err;
+ memcpy(psk, key, key_len);
+ OPENSSL_free(key);
if (c_debug)
- BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len);
+ BIO_printf(bio_c_out, "created PSK len=%ld\n", key_len);
- return psk_len;
+ return key_len;
out_err:
if (c_debug)
BIO_printf(bio_err, "Error in PSK client callback\n");
{"unix", OPT_UNIX, 's', "Connect over unix domain sockets"},
#endif
{"4", OPT_4, '-', "Use IPv4 only"},
+#ifdef AF_INET6
{"6", OPT_6, '-', "Use IPv6 only"},
+#endif
{"verify", OPT_VERIFY, 'p', "Turn on peer certificate verification"},
{"cert", OPT_CERT, '<', "Certificate file to use, PEM format assumed"},
{"certform", OPT_CERTFORM, 'F',
{NULL, 0}
};
+#define IS_INET_FLAG(o) \
+ (o == OPT_4 || o == OPT_6 || o == OPT_HOST || o == OPT_PORT || o == OPT_CONNECT)
+#define IS_UNIX_FLAG(o) (o == OPT_UNIX)
+
+/* Free |*dest| and optionally set it to a copy of |source|. */
+static void freeandcopy(char **dest, const char *source)
+{
+ OPENSSL_free(*dest);
+ *dest = NULL;
+ if (source != NULL)
+ *dest = OPENSSL_strdup(source);
+}
+
int s_client_main(int argc, char **argv)
{
BIO *sbio;
char *mbuf = NULL, *proxystr = NULL, *connectstr = NULL;
char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
char *chCApath = NULL, *chCAfile = NULL, *host = NULL;
- char *port = BUF_strdup(PORT);
+ char *port = OPENSSL_strdup(PORT);
char *inrand = NULL;
char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;
char *sess_in = NULL, *sess_out = NULL, *crl_file = NULL, *p;
int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM;
int starttls_proto = PROTO_OFF, crl_format = FORMAT_PEM, crl_download = 0;
int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
+ int at_eof = 0;
int read_buf_len = 0;
int fallback_scsv = 0;
long randamt = 0;
int async = 0;
unsigned int split_send_fragment = 0;
unsigned int max_pipelines = 0;
+ enum { use_inet, use_unix, use_unknown } connect_type = use_unknown;
+ int count4or6 = 0;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
prog = opt_init(argc, argv, s_client_options);
while ((o = opt_next()) != OPT_EOF) {
+ /* Check for intermixing flags. */
+ if (connect_type == use_unix && IS_INET_FLAG(o)) {
+ BIO_printf(bio_err,
+ "%s: Intermixed protocol flags (unix and internet domains)\n",
+ prog);
+ goto end;
+ }
+ if (connect_type == use_inet && IS_UNIX_FLAG(o)) {
+ BIO_printf(bio_err,
+ "%s: Intermixed protocol flags (internet and unix domains)\n",
+ prog);
+ goto end;
+ }
switch (o) {
case OPT_EOF:
case OPT_ERR:
ret = 0;
goto end;
case OPT_4:
-#ifdef AF_UNIX
- if (socket_family == AF_UNIX) {
- OPENSSL_free(host); host = NULL;
- OPENSSL_free(port); port = NULL;
- }
-#endif
+ connect_type = use_inet;
socket_family = AF_INET;
+ count4or6++;
break;
- case OPT_6:
- if (1) {
#ifdef AF_INET6
-#ifdef AF_UNIX
- if (socket_family == AF_UNIX) {
- OPENSSL_free(host); host = NULL;
- OPENSSL_free(port); port = NULL;
- }
-#endif
- socket_family = AF_INET6;
- } else {
-#endif
- BIO_printf(bio_err, "%s: IPv6 domain sockets unsupported\n", prog);
- goto end;
- }
+ case OPT_6:
+ connect_type = use_inet;
+ socket_family = AF_INET6;
+ count4or6++;
break;
- case OPT_HOST:
-#ifdef AF_UNIX
- if (socket_family == AF_UNIX) {
- OPENSSL_free(host); host = NULL;
- OPENSSL_free(port); port = NULL;
- socket_family = AF_UNSPEC;
- }
#endif
- OPENSSL_free(host); host = BUF_strdup(opt_arg());
+ case OPT_HOST:
+ connect_type = use_inet;
+ freeandcopy(&host, opt_arg());
break;
case OPT_PORT:
-#ifdef AF_UNIX
- if (socket_family == AF_UNIX) {
- OPENSSL_free(host); host = NULL;
- OPENSSL_free(port); port = NULL;
- socket_family = AF_UNSPEC;
- }
-#endif
- OPENSSL_free(port); port = BUF_strdup(opt_arg());
+ connect_type = use_inet;
+ freeandcopy(&port, opt_arg());
break;
case OPT_CONNECT:
-#ifdef AF_UNIX
- if (socket_family == AF_UNIX) {
- socket_family = AF_UNSPEC;
- }
-#endif
- OPENSSL_free(host); host = NULL;
- OPENSSL_free(port); port = NULL;
- connectstr = opt_arg();
+ connect_type = use_inet;
+ freeandcopy(&connectstr, opt_arg());
break;
case OPT_PROXY:
proxystr = opt_arg();
break;
#ifdef AF_UNIX
case OPT_UNIX:
+ connect_type = use_unix;
socket_family = AF_UNIX;
- OPENSSL_free(host); host = BUF_strdup(opt_arg());
- OPENSSL_free(port); port = NULL;
+ freeandcopy(&host, opt_arg());
break;
#endif
case OPT_XMPPHOST:
break;
}
}
+ if (count4or6 >= 2) {
+ BIO_printf(bio_err, "%s: Can't use both -4 and -6\n", prog);
+ goto opthelp;
+ }
argc = opt_num_rest();
if (argc != 0)
goto opthelp;
if (!ssl_pending) {
#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
if (tty_on) {
- if (read_tty)
+ /*
+ * Note that select() returns when read _would not block_,
+ * and EOF satisfies that. To avoid a CPU-hogging loop,
+ * set the flag so we exit.
+ */
+ if (read_tty && !at_eof)
openssl_fdset(fileno(stdin), &readfds);
if (write_tty)
openssl_fdset(fileno(stdout), &writefds);
tv.tv_usec = 0;
i = select(width, (void *)&readfds, (void *)&writefds,
NULL, &tv);
-# if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
- if (!i && (!_kbhit() || !read_tty))
+ if (!i && (!has_stdin_waiting() || !read_tty))
continue;
-# else
- if (!i && (!((_kbhit())
- || (WAIT_OBJECT_0 ==
- WaitForSingleObject(GetStdHandle
- (STD_INPUT_HANDLE),
- 0)))
- || !read_tty))
- continue;
-# endif
} else
i = select(width, (void *)&readfds, (void *)&writefds,
NULL, timeoutp);
/* break; */
}
}
-#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
-# if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)
- else if (_kbhit())
-# else
- else if ((_kbhit())
- || (WAIT_OBJECT_0 ==
- WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))
-# endif
+/* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */
+#if defined(OPENSSL_SYS_MSDOS)
+ else if (has_stdin_waiting())
#else
else if (FD_ISSET(fileno(stdin), &readfds))
#endif
} else
i = raw_read_stdin(cbuf, BUFSIZZ);
+ if (i == 0)
+ at_eof = 1;
+
if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q' && cmdletters))) {
BIO_printf(bio_err, "DONE\n");
ret = 0;
if (in_init)
print_stuff(bio_c_out, con, full_log);
do_ssl_shutdown(con);
+#if defined(OPENSSL_SYS_WINDOWS)
+ /*
+ * Give the socket time to send its last data before we close it.
+ * No amount of setting SO_LINGER etc on the socket seems to persuade
+ * Windows to send the data before closing the socket...but sleeping
+ * for a short time seems to do it (units in ms)
+ * TODO: Find a better way to do this
+ */
+ Sleep(50);
+#endif
BIO_closesocket(SSL_get_fd(con));
end:
if (con != NULL) {
}
# endif
-#endif
+#endif /* OPENSSL_NO_SOCK */