码迷,mamicode.com
首页 > 其他好文 > 详细

SSL探索02

时间:2015-03-16 16:23:47      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:openssl   ticket   

这篇文章探索TLS -ticket 的重用机制.

完整的SSL握手过程为:

Client                                               Server

         ClientHello
        (empty SessionTicket extension)-------->
                                                         ServerHello
                                     (empty SessionTicket extension)
                                                        Certificate*
                                                  ServerKeyExchange*
                                                 CertificateRequest*
                                      <--------      ServerHelloDone
         Certificate*
         ClientKeyExchange
         CertificateVerify*
         [ChangeCipherSpec]
         Finished                     -------->
                                                    NewSessionTicket
                                                  [ChangeCipherSpec]
                                      <--------             Finished
         Application Data             <------->     Application Data
使用TLS-ticket 的session复用机制时


Client                                                Server
         ClientHello
         (SessionTicket extension)      -------->
                                                          ServerHello
                                      (empty SessionTicket extension)
                                                     NewSessionTicket
                                                   [ChangeCipherSpec]
                                       <--------             Finished
         [ChangeCipherSpec]
         Finished                      -------->
         Application Data              <------->     Application Data
可见复用session可以缩短建立连接的时间.


复用session的流程:

在一次完整的SSL握手过程中,服务端会将ticket返回给客户端,客户端将此ticket保存,下次再进行连接时携带此ticket便可以回复session.

需要注意的是,ticket是ssl_st中的一个字段不能够单独保存,需要将整个session进行保存.

流程:

a. client1.cpp中建立SSL连接后,通过SSL_SESSION *session0 = SSL_get_session(ssl);得到session,然后通过PEM_write_SSL_SESSION(fp, session0);将session保存至文件中

b. client2.cpp若想重用上面的session,需要通过PEM_read_SSL_SESSION(fp, NULL, NULL, NULL);从文件中读取session.

c. 然后通过SSL_set_session(ssl, session_new);将其设置,这样便实现了连接复用.

注意:必须调用SSL_set_session(ssl, session_new);将其设置,否则仍是完整的连接.


示例代码如下:

client1.cpp(将session保存)

int main(int argc, char * *argv) {
	int sockfd, len;
	struct sockaddr_in dest;
	char buffer[MAXBUF + 1];
	SSL_CTX * ctx;
	SSL * ssl;

	/* SSL 库初始化*/
	SSL_library_init();
	/* 载入所有SSL 算法*/OpenSSL_add_all_algorithms();
	/* 载入所有SSL 错误消息*/
	SSL_load_error_strings();
	/* 以SSL V2 和V3 标准兼容方式产生一个SSL_CTX ,即SSL Content Text */
	ctx = SSL_CTX_new(SSLv23_client_method());
	if (ctx == NULL) {
		ERR_print_errors_fp(stdout);
		exit(1);
	}
	/* 创建一个socket 用于tcp 通信*/
	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Socket");
		exit(errno);
	}
	printf("socket created\n");
	/* 初始化服务器端(对方)的地址和端口信息*/
	bzero(&dest, sizeof(dest));
	dest.sin_family = AF_INET;
	//设置连接的端口
	dest.sin_port = htons(443);
	//设置连接的IP地址

	char *addr3 = "115.239.210.27";
	if (inet_aton(addr3, (struct in_addr *) &dest.sin_addr.s_addr) == 0) {
		perror(argv[0]);
		exit(errno);
	}
	printf("address created\n");
	/* 连接服务器*/
	if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
		perror("Connect ");
		exit(errno);
	}
	printf("server connected\n");

	/* 基于ctx 产生一个新的SSL */
	ssl = SSL_new(ctx);
	/* 将新连接的socket 加入到SSL */
	SSL_set_fd(ssl, sockfd);

	/* 建立SSL 连接*/
	if (SSL_connect(ssl) == -1) {
		ERR_print_errors_fp(stderr);
	} else {
		printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
		ShowCerts(ssl);
	}

	SSL_SESSION *session0 = SSL_get_session(ssl);

	FILE * fp;
	int leng = 20;
	int numwritten = 0;
	char data[leng];
	fp = fopen("/home/shuyan/file1.txt", "w+");

	if (fp == NULL) {
		printf("can't open file \n");
	} else {
		printf("open sucess\n");

		//将session 保存至文件中
		<span style="color:#ff0000;">PEM_write_SSL_SESSION(fp, session0);</span>
		fclose(fp);
	}

	unsigned char * session_id1 = session0->session_id;
	//cout << "sessionid : " << session_id << endl;
	int len3 = session0->session_id_length;
	cout << "session length:" << len3 << endl;
	unsigned char * ticket1 = session0->tlsext_tick;
	cout << "ticket:" << ticket1 << endl;
	size_t ticklen1 = session0->tlsext_ticklen;
	cout << "ticklen:" << ticklen1 << endl;
	long tick_lifetime_hint1 = session0->tlsext_tick_lifetime_hint;
	cout << "tick_lifetime_hint:" << tick_lifetime_hint1 << endl;

	/* 关闭连接*/
	SSL_shutdown(ssl);
	SSL_free(ssl);
	close(sockfd);
	SSL_CTX_free(ctx);

	return 0;
}

client2.cpp(从文件中恢复session)

int main(int argc, char * *argv) {
	int sockfd;
	struct sockaddr_in dest;
	char buffer[MAXBUF + 1];
	SSL_CTX * ctx;
	SSL * ssl;
	FILE *fp;

	/* SSL 库初始化*/
	SSL_library_init();
	/* 载入所有SSL 算法*/OpenSSL_add_all_algorithms();
	/* 载入所有SSL 错误消息*/
	SSL_load_error_strings();

	//---------------------------再次连接--------------------------------------

	//这里从文件中恢复session
	fp = fopen("/home/shuyan/file1.txt", "r");
	<span style="color:#ff0000;">SSL_SESSION * session_new = PEM_read_SSL_SESSION(fp, NULL, NULL, NULL);</span>
	if (session_new != NULL) {
		unsigned char * ticket2 = session_new->tlsext_tick;
		cout << "new ticket :" << ticket2 << endl;
	} else {
		cout << "=======NULL========" << endl;
	}

	ctx = SSL_CTX_new(SSLv23_client_method());
	/* 基于ctx 产生一个新的SSL */
	ssl = SSL_new(ctx);

	//必须有这一步骤,否则不会进行复用.
	SSL_set_session(ssl, session_new);
	/* 创建一个socket 用于tcp 通信*/
	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Socket");
		exit(errno);
	}
	printf("socket created\n");
	/* 初始化服务器端(对方)的地址和端口信息*/
	bzero(&dest, sizeof(dest));
	dest.sin_family = AF_INET;
	//设置连接的端口
	dest.sin_port = htons(443);
	//设置连接的IP地址

	char *addr2 = "115.239.210.27";
	if (inet_aton(addr2, (struct in_addr *) &dest.sin_addr.s_addr) == 0) {
		perror(argv[0]);
		exit(errno);
	}
	printf("address created\n");
	/* 连接服务器*/
	if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
		perror("Connect ");
		exit(errno);
	}
	printf("server connected\n");
	/* 将新连接的socket 加入到SSL */

	SSL_set_fd(ssl, sockfd);

	/* 建立SSL 连接*/
	if (SSL_connect(ssl) == -1) {
		ERR_print_errors_fp(stderr);
	} else {
		printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
		ShowCerts(ssl);
	}

	SSL_SESSION *session2 = SSL_get_session(ssl);

	int len2 = session2->session_id_length;
	cout << "session length:" << len2 << endl;
	unsigned char * ticket2 = session2->tlsext_tick;
	cout << "ticket:" << ticket2 << endl;
	size_t ticklen2 = session2->tlsext_ticklen;
	cout << "ticklen:" << ticklen2 << endl;
	long tick_lifetime_hint2 = session2->tlsext_tick_lifetime_hint;
	cout << "tick_lifetime_hint:" << tick_lifetime_hint2 << endl;

	/* 关闭连接*/
	SSL_shutdown(ssl);
	SSL_free(ssl);
	close(sockfd);
	SSL_CTX_free(ctx);

	return 0;
}




SSL探索02

标签:openssl   ticket   

原文地址:http://blog.csdn.net/leokelly001/article/details/44307171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!