/* * http://www.cppblog.com/flyonok/archive/2011/03/24/133100.html */ #include <stdio.h> #include <stdlib.h> #include <memory.h> #include <errno.h> #include <sys/types.h> #include <winsock2.h> #include "openssl/rsa.h" #include "openssl/crypto.h" #include "openssl/x509.h" #include "openssl/pem.h" #include "openssl/ssl.h" #include "openssl/err.h" #define CERTF "ca.pem" /*服务端的证书(需经CA签名)*/ #define KEYF "cakey.pem" /*服务端的私钥(建议加密存储)*/ #define CACERT "caa.pem" /*CA 的证书*/ #define PORT 1111 /*准备绑定的端口*/ #define CHK_NULL(x) if ((x)==NULL) exit (1) #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); } #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); } int main() { int err; int listen_sd; int sd; struct sockaddr_in sa_serv; struct sockaddr_in sa_cli; int client_len; SSL_CTX* ctx; SSL* ssl; X509* client_cert; char* str; char buf[4096]; const SSL_METHOD *meth; WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("WSAStartup()fail:%d\n", GetLastError()); return -1; } SSL_load_error_strings(); /*为打印调试信息作准备*/ OpenSSL_add_ssl_algorithms(); /*初始化*/ meth = SSLv23_server_method(); /*采用什么协议(SSLv2/SSLv3/TLSv1)在此指定*/ ctx = SSL_CTX_new(meth); CHK_NULL(ctx); SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); /*验证与否*/ SSL_CTX_load_verify_locations(ctx, CACERT, NULL); /*若验证,则放置CA证书*/ if (SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stderr); exit(3); } if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stderr); exit(4); } if (!SSL_CTX_check_private_key(ctx)) { printf("Private key does not match the certificate public key\n"); exit(5); } //SSL_CTX_set_cipher_list(ctx, "EDH-RSA-DES-CBC3-SHA"); /*开始正常的TCP socket过程.................................*/ printf("Begin TCP socket...\n"); listen_sd = socket(AF_INET, SOCK_STREAM, 0); CHK_ERR(listen_sd, "socket"); memset(&sa_serv, ‘\0‘, sizeof(sa_serv)); sa_serv.sin_family = AF_INET; sa_serv.sin_addr.s_addr = INADDR_ANY; sa_serv.sin_port = htons(PORT); err = bind(listen_sd, (struct sockaddr*) &sa_serv, sizeof(sa_serv)); CHK_ERR(err, "bind"); /*接受TCP链接*/ err = listen(listen_sd, 5); CHK_ERR(err, "listen"); client_len = sizeof(sa_cli); sd = accept(listen_sd, (struct sockaddr*) &sa_cli, &client_len); CHK_ERR(sd, "accept"); closesocket(listen_sd); printf("Connection from %lx, port %x\n", sa_cli.sin_addr.s_addr, sa_cli.sin_port); /*TCP连接已建立,进行服务端的SSL过程. */ printf("Begin server side SSL\n"); ssl = SSL_new(ctx); CHK_NULL(ssl); SSL_set_fd(ssl, sd); err = SSL_accept(ssl); printf("SSL_accept finished\n"); CHK_SSL(err); /*打印所有加密算法的信息(可选)*/ printf("SSL connection using %s\n", SSL_get_cipher(ssl)); /*得到服务端的证书并打印些信息(可选) */ client_cert = SSL_get_peer_certificate(ssl); if (client_cert != NULL) { printf("Client certificate:\n"); str = X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0); CHK_NULL(str); printf("\t subject: %s\n", str); free(str); str = X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0); CHK_NULL(str); printf("\t issuer: %s\n", str); free(str); X509_free(client_cert);/*如不再需要,需将证书释放 */ } else printf("Client does not have certificate.\n"); /* 数据交换开始,用SSL_write,SSL_read代替write,read */ err = SSL_read(ssl, buf, sizeof(buf) - 1); CHK_SSL(err); buf[err] = ‘\0‘; printf("Got %d chars:‘%s‘\n", err, buf); err = SSL_write(ssl, "I hear you.", strlen("I hear you.")); CHK_SSL(err); /* 收尾工作*/ shutdown(sd, 2); SSL_free(ssl); SSL_CTX_free(ctx); return 0; }
#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <memory.h> #include <errno.h> #include <sys/types.h> #include <winsock2.h> #include "openssl/rsa.h" #include "openssl/crypto.h" #include "openssl/x509.h" #include "openssl/pem.h" #include "openssl/ssl.h" #include "openssl/err.h" #include "openssl/rand.h" #include "openssl/engine.h" #define CERTF "02.pem" /*服务端的证书(需经CA签名)*/ #define KEYF "02key.pem" /*服务端的私钥(建议加密存储)*/ #define CACERT "ca.pem" #define PORT 1111 #define SERVER_ADDR "127.0.0.1" #define CHK_NULL(x) if ((x)==NULL) exit (-1) #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(-2); } #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(-3); } int main() { int err; int sd; struct sockaddr_in sa; SSL_CTX* ctx; SSL* ssl; X509* server_cert; char* str; char buf[4096]; const SSL_METHOD *meth; int seed_int[100]; /*存放随机序列*/ //Sleep(10 * 1000); OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_CAPI, NULL); ENGINE_load_builtin_engines(); ENGINE *engine = ENGINE_by_id("capi"); ENGINE_init(engine); WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("WSAStartup()fail:%d\n", GetLastError()); return -1; } OpenSSL_add_ssl_algorithms(); /*初始化*/ SSL_load_error_strings(); /*为打印调试信息作准备*/ meth = SSLv23_client_method(); /*采用什么协议(SSLv2/SSLv3/TLSv1)在此指定*/ ctx = SSL_CTX_new(meth); CHK_NULL(ctx); SSL_CTX_set_client_cert_engine(ctx, engine); SSL_CTX_set_verify(ctx, 0, NULL); /*验证与否*/ SSL_CTX_load_verify_locations(ctx, CACERT, NULL); /*若验证,则放置CA证书*/ /* if (SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stderr); exit(-2); } if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stderr); exit(-3); } if (!SSL_CTX_check_private_key(ctx)) { printf("Private key does not match the certificate public key\n"); exit(-4); } */ //SSL_CTX_set_cipher_list(ctx, "EDH-RSA-DES-CBC3-SHA"); /*以下是正常的TCP socket建立过程 .............................. */ printf("Begin tcp socket...\n"); sd = socket(AF_INET, SOCK_STREAM, 0); CHK_ERR(sd, "socket"); memset(&sa, ‘\0‘, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(SERVER_ADDR); /* Server IP */ sa.sin_port = htons(PORT); /* Server Port number */ err = connect(sd, (struct sockaddr*) &sa, sizeof(sa)); CHK_ERR(err, "connect"); /* TCP 链接已建立.开始 SSL 握手过程.......................... */ printf("Begin SSL negotiation \n"); ssl = SSL_new(ctx); CHK_NULL(ssl); SSL_set_fd(ssl, sd); err = SSL_connect(ssl); CHK_SSL(err); /*打印所有加密算法的信息(可选)*/ printf("SSL connection using %s\n", SSL_get_cipher(ssl)); /*得到服务端的证书并打印些信息(可选) */ server_cert = SSL_get_peer_certificate(ssl); CHK_NULL(server_cert); printf("Server certificate:\n"); str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0); CHK_NULL(str); printf("\t subject: %s\n", str); free(str); str = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0); CHK_NULL(str); printf("\t issuer: %s\n", str); free(str); //X509_free(server_cert); /*如不再需要,需将证书释放 */ /* 数据交换开始,用SSL_write,SSL_read代替write,read */ printf("Begin SSL data exchange\n"); err = SSL_write(ssl, "Hello World!", strlen("Hello World!")); CHK_SSL(err); err = SSL_read(ssl, buf, sizeof(buf) - 1); CHK_SSL(err); buf[err] = ‘\0‘; printf("Got %d chars:‘%s‘\n", err, buf); SSL_shutdown(ssl); /* 收尾工作 */ shutdown(sd, 2); SSL_free(ssl); SSL_CTX_free(ctx); return 0; }