6 * Copyright (C) 2006-2015 wolfSSL Inc.
8 * This file is part of wolfSSL. (formerly known as CyaSSL)
10 * wolfSSL is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * wolfSSL is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
33 #include <sys/socket.h>
35 #include <wolfssl/wolfcrypt/types.h>
36 #include <wolfssl/ssl.h>
39 # define dbg(...) say(__VA_ARGS__)
41 # define dbg(...) ((void)0)
44 static ssize_t safe_write(int fd, const void *buf, size_t count)
49 n = write(fd, buf, count);
50 } while (n < 0 && errno == EINTR);
55 static ssize_t full_write(int fd, const void *buf, size_t len)
63 cc = safe_write(fd, buf, len);
67 /* we already wrote some! */
68 /* user can do another write to know the error code */
71 return cc; /* write() returns -1 on failure. */
75 buf = ((const char *)buf) + cc;
82 static void say(const char *s, ...)
89 sz = vsnprintf(buf, sizeof(buf), s, p);
90 full_write(STDERR_FILENO, buf, sz >= 0 && sz < sizeof(buf) ? sz : strlen(buf));
94 static void die(const char *s, ...)
101 sz = vsnprintf(buf, sizeof(buf), s, p);
102 full_write(STDERR_FILENO, buf, sz >= 0 && sz < sizeof(buf) ? sz : strlen(buf));
107 static void err_sys(const char *msg)
115 static void showPeer(WOLFSSL* ssl)
117 WOLFSSL_CIPHER* cipher;
118 WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
120 ShowX509(peer, "peer's cert info:");
122 say("peer has no cert!\n");
123 say("SSL version is %s\n", wolfSSL_get_version(ssl));
125 cipher = wolfSSL_get_current_cipher(ssl);
126 say("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher));
129 WOLFSSL_X509_CHAIN* chain = wolfSSL_get_peer_chain(ssl);
130 int count = wolfSSL_get_chain_count(chain);
133 for (i = 0; i < count; i++) {
135 unsigned char buffer[3072];
136 WOLFSSL_X509* chainX509;
138 wolfSSL_get_chain_cert_pem(chain, i, buffer, sizeof(buffer), &length);
140 say("cert %d has length %d data = \n%s\n", i, length, buffer);
142 chainX509 = wolfSSL_get_chain_X509(chain, i);
144 ShowX509(chainX509, "session cert info:");
146 say("get_chain_X509 failed\n");
147 wolfSSL_FreeX509(chainX509);
153 WOLFSSL *prepare(int sockfd)
155 WOLFSSL_METHOD* method;
161 method = wolfTLSv1_1_client_method();
163 err_sys("out of memory");
164 ctx = wolfSSL_CTX_new(method);
166 err_sys("out of memory");
168 // if (wolfSSL_CTX_set_cipher_list(ctx, cipherList) != SSL_SUCCESS)
169 // err_sys("client can't set cipher list 1");
172 // wolfSSL_CTX_set_group_messages(ctx);
175 // wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits);
179 // wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
180 // if (cipherList == NULL) {
181 // const char *defaultCipherList;
182 //#if defined(HAVE_AESGCM) && !defined(NO_DH)
183 // defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
184 //#elif defined(HAVE_NULL_CIPHER)
185 // defaultCipherList = "PSK-NULL-SHA256";
187 // defaultCipherList = "PSK-AES128-CBC-SHA256";
189 // if (wolfSSL_CTX_set_cipher_list(ctx,defaultCipherList) != SSL_SUCCESS)
190 // err_sys("client can't set cipher list 2");
192 // useClientCert = 0;
196 // if (cipherList == NULL) {
197 // wolfSSL_CTX_allow_anon_cipher(ctx);
198 // if (wolfSSL_CTX_set_cipher_list(ctx,"ADH-AES128-SHA") != SSL_SUCCESS)
199 // err_sys("client can't set cipher list 4");
201 // useClientCert = 0;
204 //#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
205 // wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
209 // if (ocspUrl != NULL) {
210 // wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
211 // wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
212 // | WOLFSSL_OCSP_URL_OVERRIDE);
215 // wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE);
219 // wolfSSL_CTX_SetCACb(ctx, CaCb);
222 //#ifdef VERIFY_CALLBACK
223 // wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
225 //#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
226 // if (useClientCert) {
227 // if (wolfSSL_CTX_use_certificate_chain_file(ctx, ourCert) != SSL_SUCCESS)
228 // err_sys("can't load client cert file, check file and run from"
229 // " wolfSSL home dir");
230 // if (wolfSSL_CTX_use_PrivateKey_file(ctx, ourKey, SSL_FILETYPE_PEM) != SSL_SUCCESS)
231 // err_sys("can't load client private key file, check file and run "
232 // "from wolfSSL home dir");
235 // if (!usePsk && !useAnon) {
236 // if (wolfSSL_CTX_load_verify_locations(ctx, verifyCert,0) != SSL_SUCCESS)
237 // err_sys("can't load ca file, Please run from wolfSSL home dir");
239 // /* load ecc verify too, echoserver uses it by default w/ ecc */
240 // if (wolfSSL_CTX_load_verify_locations(ctx, eccCert, 0) != SSL_SUCCESS)
241 // err_sys("can't load ecc ca file, Please run from wolfSSL home dir");
244 //#endif /* !NO_FILESYSTEM && !NO_CERTS */
246 //#if !defined(NO_CERTS)
247 // if (!usePsk && !useAnon && doPeerCheck == 0)
248 // wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
249 // if (!usePsk && !useAnon && overrideDateErrors == 1)
250 // wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myDateCb);
253 wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
257 // if (wolfSSL_CTX_UseSNI(ctx, 0, sniHostName, XSTRLEN(sniHostName)) != SSL_SUCCESS)
258 // err_sys("UseSNI failed");
261 //#ifdef HAVE_MAX_FRAGMENT
263 // if (wolfSSL_CTX_UseMaxFragment(ctx, maxFragment) != SSL_SUCCESS)
264 // err_sys("UseMaxFragment failed");
266 //#ifdef HAVE_TRUNCATED_HMAC
267 // if (truncatedHMAC)
268 // if (wolfSSL_CTX_UseTruncatedHMAC(ctx) != SSL_SUCCESS)
269 // err_sys("UseTruncatedHMAC failed");
271 //#ifdef HAVE_SESSION_TICKET
272 // if (wolfSSL_CTX_UseSessionTicket(ctx) != SSL_SUCCESS)
273 // err_sys("UseSessionTicket failed");
276 //#if defined(WOLFSSL_MDK_ARM)
277 // wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
280 ssl = wolfSSL_new(ctx);
282 err_sys("out of memory");
284 //#ifdef HAVE_SESSION_TICKET
285 // wolfSSL_set_SessionTicket_cb(ssl, sessionTicketCB, (void*)"initial session");
289 // SOCKADDR_IN_T addr;
290 // build_addr(&addr, host, port, 1);
291 // wolfSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
292 // tcp_socket(&sockfd, 1);
294 // tcp_connect(&sockfd, host, port, 0);
297 //#ifdef HAVE_POLY1305
298 // /* use old poly to connect with google server */
299 // if (!XSTRNCMP(domain, "www.google.com", 14)) {
300 // if (wolfSSL_use_old_poly(ssl, 1) != 0)
301 // err_sys("unable to set to old poly");
305 wolfSSL_set_fd(ssl, sockfd);
308 // if (disableCRL == 0) {
309 // if (wolfSSL_EnableCRL(ssl, WOLFSSL_CRL_CHECKALL) != SSL_SUCCESS)
310 // err_sys("can't enable crl check");
311 // if (wolfSSL_LoadCRL(ssl, crlPemDir, SSL_FILETYPE_PEM, 0) != SSL_SUCCESS)
312 // err_sys("can't load crl, check crlfile and date validity");
313 // if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != SSL_SUCCESS)
314 // err_sys("can't set crl callback");
317 //#ifdef HAVE_SECURE_RENEGOTIATION
319 // if (wolfSSL_UseSecureRenegotiation(ssl) != SSL_SUCCESS)
320 // err_sys("can't enable secure renegotiation");
325 // SetupAtomicUser(ctx, ssl);
327 //#ifdef HAVE_PK_CALLBACKS
329 // SetupPkCallbacks(ctx, ssl);
331 // if (matchName && doPeerCheck)
332 // wolfSSL_check_domain_name(ssl, domain);
334 if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
335 // /* see note at top of README */
336 // int err = wolfSSL_get_error(ssl, 0);
337 // char buffer[WOLFSSL_MAX_ERROR_SZ];
338 // say("err = %d, %s\n", err,
339 // wolfSSL_ERR_error_string(err, buffer));
340 err_sys("SSL_connect failed");
344 //#ifdef HAVE_SECURE_RENEGOTIATION
345 // if (scr && forceScr) {
346 // if (wolfSSL_Rehandshake(ssl) != SSL_SUCCESS) {
347 // int err = wolfSSL_get_error(ssl, 0);
348 // char buffer[WOLFSSL_MAX_ERROR_SZ];
349 // say("err = %d, %s\n", err,
350 // wolfSSL_ERR_error_string(err, buffer));
351 // err_sys("wolfSSL_Rehandshake failed");
359 static struct pollfd pfd[2] = {
360 { -1, POLLIN|POLLERR|POLLHUP, 0 },
361 { -1, POLLIN|POLLERR|POLLHUP, 0 },
364 #define NETWORK pfd[1]
365 #define STDIN_READY() (pfd[0].revents & (POLLIN|POLLERR|POLLHUP))
366 #define NETWORK_READY() (pfd[1].revents & (POLLIN|POLLERR|POLLHUP))
368 static void wait_for_input(void)
370 if (STDIN.fd == NETWORK.fd) /* means both are -1 */
373 STDIN.revents = NETWORK.revents = 0;
374 while (poll(pfd, 2, -1) < 0 && errno == EINTR)
378 static void do_io_until_eof_and_exit(WOLFSSL *ssl, int fd)
386 len = 0; /* only to suppress compiler warning */
391 dbg("reading stdin\n");
392 len = read(STDIN_FILENO, ibuf, sizeof(ibuf));
394 die("read error on stdin\n");
396 dbg("read len = 0, stdin not polled anymore\n");
399 int n = wolfSSL_write(ssl, ibuf, len);
401 die("SSL_write(%d) failed (returned %d)\n", len, n);
405 if (NETWORK_READY()) {
407 (pfd[1].revents & POLLIN) ? "POLLIN" : "",
408 (pfd[1].revents & POLLERR) ? "|POLLERR" : "",
409 (pfd[1].revents & POLLHUP) ? "|POLLHUP" : ""
411 /* We are using blocking socket here.
412 * (Nonblocking socket would complicate writing to it).
413 * Therefore, SSL_read _can block_ here.
414 * This is not what wget expects (it wants to see short reads).
415 * Therefore, we use smallish buffer here, to approximate that.
417 len = wolfSSL_read(ssl, ibuf,
418 sizeof(ibuf) < 1024 ? sizeof(ibuf) : 1024
421 die("SSL_read error on network (%d)\n", len);
424 n = full_write(STDOUT_FILENO, ibuf, len);
426 die("write(%d) to stdout returned %d\n", len, n);
429 /* Blocking reads are easier wtr EOF detection (no EAGAIN error to check for) */
430 dbg("read len = 0, network not polled anymore\n");
432 /* saw EOF on network, and we processed
433 * and wrote out all ssl data. Signal it:
435 close(STDOUT_FILENO);
440 int main(int argc, char **argv)
447 die("Syntax error\n");
448 if (argv[1][0] != '-')
449 die("Syntax error\n");
450 if (argv[1][1] != 'd')
451 die("Syntax error\n");
452 fd_str = argv[1] + 2;
455 if (!fd_str || fd_str[0] < '0' || fd_str[0] > '9')
456 die("Syntax error\n");
460 die("Syntax error\n");
463 do_io_until_eof_and_exit(ssl, fd);
464 /* does not return */
466 // if (doDTLS == 0) { /* don't send alert after "break" command */
467 // ret = wolfSSL_shutdown(ssl);
468 // if (wc_shutdown && ret == SSL_SHUTDOWN_NOT_DONE)
469 // wolfSSL_shutdown(ssl); /* bidirectional shutdown */
473 // FreeAtomicUser(ssl);
475 // wolfSSL_free(ssl);
476 // CloseSocket(sockfd);
477 // wolfSSL_CTX_free(ctx);