我正在尝试通过TLS频道发送和接收数据。
此代码有什么问题?
示例代码:
/* gcc -o sslconnect sslconnect.c -lssl -lcrypto *
* ------------------------------------------------------------ */
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
int create_socket(char[], BIO *);
int main() {
char dest_url[] ="https://query.yahooapis.com";
BIO *certbio = NULL;
BIO *outbio = NULL;
X509 *cert = NULL;
X509_NAME *certname = NULL;
const SSL_METHOD *method;
SSL_CTX *ctx;
SSL *ssl;
int server = 0;
int ret, i;
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();
/* ---------------------------------------------------------- *
* Create the Input/Output BIO's. *
* ---------------------------------------------------------- */
certbio = BIO_new(BIO_s_file());
outbio = BIO_new_fp(stdout, BIO_NOCLOSE);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
if(SSL_library_init() < 0)
BIO_printf(outbio, "Could not initialize the OpenSSL library !\n");
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
method = SSLv23_client_method();
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
if ( (ctx = SSL_CTX_new(method)) == NULL)
BIO_printf(outbio, "Unable to create a new SSL context structure.\n");
/* ---------------------------------------------------------- *
* Disabling SSLv2 will leave v3 and TSLv1 for negotiation *
* ---------------------------------------------------------- */
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
ssl = SSL_new(ctx);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
server = create_socket(dest_url, outbio);
if(server != 0)
BIO_printf(outbio, "Successfully made the TCP connection to: %s.\n", dest_url);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
SSL_set_fd(ssl, server);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
if ( SSL_connect(ssl) != 1 )
BIO_printf(outbio, "Error: Could not build a SSL session to: %s.\n", dest_url);
else
BIO_printf(outbio, "Successfully enabled SSL/TLS session to: %s.\n", dest_url);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
cert = SSL_get_peer_certificate(ssl);
if (cert == NULL)
BIO_printf(outbio, "Error: Could not get a certificate from: %s.\n", dest_url);
else
BIO_printf(outbio, "Retrieved the server's certificate from: %s.\n", dest_url);
/* ---------------------------------------------------------- *
* -----------------------------------------------------------*/
certname = X509_NAME_new();
certname = X509_get_subject_name(cert);
/* ---------------------------------------------------------- *
* display the cert subject here *
* -----------------------------------------------------------*/
BIO_printf(outbio, "Displaying the certificate subject data:\n");
X509_NAME_print_ex(outbio, certname, 0, 0);
BIO_printf(outbio, "\n");
/*----------------------------Send-----------------------------------*/
char buf[5120];
char *yql = "GET /v1/public/yql?q HTTP/1.1\r\nHost:query.yahooapis.com";
printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
int result = SSL_write(ssl, yql, strlen(yql)); /* encrypt & send message */
printf("Result = %i\n",result);
int bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
printf("ReceivedLength: \"%i\"\n", bytes);
buf[bytes] = 0;
printf("Received: \"%s\"\n", buf);
/*----------------------------End sending-----------------------------------*/
/* ---------------------------------------------------------- *
* -----------------------------------------------------------*/
SSL_free(ssl);
close(server);
X509_free(cert);
SSL_CTX_free(ctx);
BIO_printf(outbio, "Finished SSL/TLS connection with server: %s.\n", dest_url);
return(0);
}
/* ---------------------------------------------------------- *
* create_socket() creates the socket & TCP-connect to server *
* ---------------------------------------------------------- */
int create_socket(char url_str[], BIO *out) {
int sockfd;
char hostname[256] = "";
char portnum[6] = "443";
char proto[6] = "";
char *tmp_ptr = NULL;
int port;
struct hostent *host;
struct sockaddr_in dest_addr;
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
if(url_str[strlen(url_str)] == '/')
url_str[strlen(url_str)] = '\0';
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
strncpy(proto, url_str, (strchr(url_str, ':')-url_str));
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
strncpy(hostname, strstr(url_str, "://")+3, sizeof(hostname));
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
if(strchr(hostname, ':')) {
tmp_ptr = strchr(hostname, ':');
/* the last : starts the port number, if avail, i.e. 8443 */
strncpy(portnum, tmp_ptr+1, sizeof(portnum));
*tmp_ptr = '\0';
}
port = atoi(portnum);
if ( (host = gethostbyname(hostname)) == NULL ) {
BIO_printf(out, "Error: Cannot resolve hostname %s.\n", hostname);
abort();
}
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
dest_addr.sin_family=AF_INET;
dest_addr.sin_port=htons(port);
dest_addr.sin_addr.s_addr = *(long*)(host->h_addr);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
memset(&(dest_addr.sin_zero), '\0', 8);
tmp_ptr = inet_ntoa(dest_addr.sin_addr);
/* ---------------------------------------------------------- *
* ---------------------------------------------------------- */
if ( connect(sockfd, (struct sockaddr *) &dest_addr,
sizeof(struct sockaddr)) == -1 ) {
BIO_printf(out, "Error: Cannot connect to host %s [%s] on port %d.\n",
hostname, tmp_ptr, port);
}
return sockfd;
}
此行延迟并返回0个字节。但是,通过 openssl s_client 发送相同的数据可以得到预期的结果。 ( openssl s_client -connect query.yahooapis.com:443-tls1_2 )
int bytes = SSL_read(ssl, buf, sizeof(buf));
预期结果:
<?xml version="1.0" encoding="UTF-8"?>
<error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-US"><description>You must specify a yql statement (q=) to execute</description></error>
这是输出:
[root@localhost tlssamples]# ./sslconnect
Successfully made the TCP connection to: https://query.yahooapis.com.
Successfully enabled SSL/TLS session to: https://query.yahooapis.com.
Retrieved the server's certificate from: https://query.yahooapis.com.
Displaying the certificate subject data:
C=US, ST=CA, L=Sunnyvale, O=Yahoo! Inc., CN=*.ue.yahoo.com
Connected with ECDHE-RSA-AES128-GCM-SHA256 encryption
Result = 55
ReceivedLength: "0"
Received: ""
Finished SSL/TLS connection with server: https://query.yahooapis.com.
答案 0 :(得分:0)
解决方案在这里。
用法:
$ openssl s_server -accept 50000 -cert node.crt -key node.key -CAfile ca.crt -cipher ECDHE-RSA-AES256-GCM-SHA384 -serverpref -state -debug -status_verbose -no_tls1_3
...
CIPHER is ECDHE-RSA-AES256-GCM-SHA384
Secure Renegotiation IS supported