| 
									
										
										
										
											2016-05-18 21:16:36 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-09-02 21:05:45 +08:00
										 |  |  |  * Copyright 1998-2025 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-05-18 21:16:36 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 21:08:15 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 21:16:36 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-16 22:43:29 +08:00
										 |  |  | /*-
 | 
					
						
							|  |  |  |  * A minimal program to do SSL to a passed host and port. | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  * It is actually using non-blocking IO but in a very simple manner | 
					
						
							|  |  |  |  * sconnect host:port - it does a 'GET / HTTP/1.0' | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * cc -I../../include sconnect.c -L../.. -lssl -lcrypto | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											1999-04-24 06:13:45 +08:00
										 |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include <openssl/ssl.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-07 04:30:50 +08:00
										 |  |  | #if !defined(OPENSSL_SYS_WINDOWS)
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							|  |  |  | # define sleep(x) Sleep(x*1000)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  | #define HOSTPORT "localhost:4433"
 | 
					
						
							|  |  |  | #define CAFILE "root.pem"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  | int main(int argc, char *argv[]) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  |     const char *hostport = HOSTPORT; | 
					
						
							|  |  |  |     const char *CAfile = CAFILE; | 
					
						
							| 
									
										
										
										
											2019-06-21 04:39:38 +08:00
										 |  |  |     const char *hostname; | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  |     BIO *out = NULL; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     char buf[1024 * 10], *p; | 
					
						
							|  |  |  |     SSL_CTX *ssl_ctx = NULL; | 
					
						
							|  |  |  |     SSL *ssl; | 
					
						
							|  |  |  |     BIO *ssl_bio; | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  |     int i, len, off, ret = EXIT_FAILURE; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  |     if (argc > 1) | 
					
						
							|  |  |  |         hostport = argv[1]; | 
					
						
							|  |  |  |     if (argc > 2) | 
					
						
							|  |  |  |         CAfile = argv[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-09 20:02:20 +08:00
										 |  |  | #ifdef WATT32
 | 
					
						
							|  |  |  |     dbug_init(); | 
					
						
							|  |  |  |     sock_init(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 07:18:31 +08:00
										 |  |  |     ssl_ctx = SSL_CTX_new(TLS_client_method()); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  |     /* Enable trust chain verification */ | 
					
						
							|  |  |  |     SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL); | 
					
						
							| 
									
										
										
										
											2024-04-06 10:20:54 +08:00
										 |  |  |     if (!SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-08 04:47:32 +08:00
										 |  |  |     /* Let's make an SSL structure */ | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     ssl = SSL_new(ssl_ctx); | 
					
						
							|  |  |  |     SSL_set_connect_state(ssl); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     /* Use it inside an SSL BIO */ | 
					
						
							|  |  |  |     ssl_bio = BIO_new(BIO_f_ssl()); | 
					
						
							|  |  |  |     BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     /* Lets use a connect BIO under the SSL BIO */ | 
					
						
							|  |  |  |     out = BIO_new(BIO_s_connect()); | 
					
						
							| 
									
										
										
										
											2015-12-30 02:28:28 +08:00
										 |  |  |     BIO_set_conn_hostname(out, hostport); | 
					
						
							| 
									
										
										
										
											2019-06-21 04:39:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* The BIO has parsed the host:port and even IPv6 literals in [] */ | 
					
						
							|  |  |  |     hostname = BIO_get_conn_hostname(out); | 
					
						
							| 
									
										
										
										
											2025-05-16 21:37:48 +08:00
										 |  |  |     if (!hostname || SSL_set1_host(ssl, hostname) <= 0) { | 
					
						
							|  |  |  |         BIO_free(ssl_bio); | 
					
						
							| 
									
										
										
										
											2019-06-21 04:39:38 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2025-05-16 21:37:48 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-21 04:39:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     BIO_set_nbio(out, 1); | 
					
						
							|  |  |  |     out = BIO_push(ssl_bio, out); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     p = "GET / HTTP/1.0\r\n\r\n"; | 
					
						
							| 
									
										
										
										
											2025-06-19 16:12:13 +08:00
										 |  |  |     len = (int)strlen(p); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     off = 0; | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         i = BIO_write(out, &(p[off]), len); | 
					
						
							|  |  |  |         if (i <= 0) { | 
					
						
							|  |  |  |             if (BIO_should_retry(out)) { | 
					
						
							|  |  |  |                 fprintf(stderr, "write DELAY\n"); | 
					
						
							|  |  |  |                 sleep(1); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         off += i; | 
					
						
							|  |  |  |         len -= i; | 
					
						
							|  |  |  |         if (len <= 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     for (;;) { | 
					
						
							|  |  |  |         i = BIO_read(out, buf, sizeof(buf)); | 
					
						
							|  |  |  |         if (i == 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         if (i < 0) { | 
					
						
							|  |  |  |             if (BIO_should_retry(out)) { | 
					
						
							|  |  |  |                 fprintf(stderr, "read DELAY\n"); | 
					
						
							|  |  |  |                 sleep(1); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         fwrite(buf, 1, i, stdout); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  |     ret = EXIT_SUCCESS; | 
					
						
							| 
									
										
										
										
											2015-05-02 02:29:48 +08:00
										 |  |  |     goto done; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2015-05-02 02:29:48 +08:00
										 |  |  |     if (ERR_peek_error() == 0) { /* system call error */ | 
					
						
							|  |  |  |         fprintf(stderr, "errno=%d ", errno); | 
					
						
							|  |  |  |         perror("error"); | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-05-02 02:29:48 +08:00
										 |  |  |         ERR_print_errors_fp(stderr); | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-02 02:29:48 +08:00
										 |  |  |  done: | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     BIO_free_all(out); | 
					
						
							| 
									
										
										
										
											2015-04-11 22:22:36 +08:00
										 |  |  |     SSL_CTX_free(ssl_ctx); | 
					
						
							| 
									
										
										
										
											2017-07-12 12:18:00 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | } |