1. 使用OpenSSL内置的数据结构BIO可以方便的创建安全和非安全链接,
在IBM Web上的"使用OpenSSL进行安全编程"系列的三篇文章是个不错的入门:
http://www.ibm.com/developerworks/cn/linux/l-openssl.htmlhttp://www.ibm.com/developerworks/cn/linux/l-openssl2.htmlhttp://www.ibm.com/developerworks/cn/linux/l-openssl3.html 安全链接的简要概述:
安全连接要求在连接建立后进行握手。在握手过程中,服务器向客户机发送一个证书, 然后,客户机根据一组可信任证书来核实该证书。它还将检查证书,以确保它没有过期。要 检验证书是可信任的,需要在连接建立之前提前加载一个可信任证书库。
只有在服务器发出请求时,客户机才会向服务器发送一个证书。该过程叫做客户机认证。使用证书, 在客户机和服务器之间传递密码参数,以建立安全连接。尽管握手是在建立连接之后才进行的,但是客户机或服务器可以在任何时刻请求进行一次新的握手。
附带两个Demo:分别是使用BIO建立普通的socket链接和ssl链接, 并下载google得主页.
1
#include "openssl/ssl.h"
2
#include "openssl/bio.h"
3
#include "openssl/err.h"
4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
5![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
6
#include <iostream>
7
#include <winsock2.h>
8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
9![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
10
#pragma comment( lib, "ws2_32.lib" )
11
#pragma comment( lib, "libeay32.lib" )
12
#pragma comment( lib, "ssleay32.lib" )
13![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
14![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
15![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int main( int argc, char* argv[] )
{
16![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////
17
// 初始化 //
18![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////
19
BIO* bio;
20
int ret;
21
char* request = "GET / HTTP/1.1\x0D\x0AHost: www.google.com\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A";
22
char buf[1024];
23![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
24
ERR_load_BIO_strings();
25
SSL_load_error_strings();
26
SSLeay_add_ssl_algorithms();
27![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
28![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
29![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////
30
// 建立链接 //
31![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////
32![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
33
bio = BIO_new_connect("www.google.com:80");
34![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(bio == NULL)
{
35
std::cout<<"BIO_new_connect error."<<std::endl;
36
return -1;
37
}
38![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
39![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(BIO_do_connect(bio) <= 0)
{
40
std::cout<<"BIO_new_connect error."<<std::endl;
41
BIO_free_all(bio);
42
return -1;
43
}
44![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
45
// 发送请求
46
BIO_write(bio, request, strlen(request));
47![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
48
// 接收数据
49![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(;;)
{
50
ret = BIO_read(bio, buf, 1023);
51![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ret <= 0)
{
52
break;
53
}
54
buf[ret] = 0;
55
std::cout<<buf<<std::endl;
56
}
57![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
58
BIO_free_all(bio);
59
return 0;
60
}
1
#include "openssl/ssl.h"
2
#include "openssl/bio.h"
3
#include "openssl/err.h"
4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
5![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
6
#include <iostream>
7
#include <winsock2.h>
8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
9![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
10
#pragma comment( lib, "ws2_32.lib" )
11
#pragma comment( lib, "libeay32.lib" )
12
#pragma comment( lib, "ssleay32.lib" )
13![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
14
// 我们默认对服务器的证书都是可信的,没有进行服务器证书的验证.
15![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int main( int argc, char* argv[] )
{
16![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////
17
// 初始化 //
18![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////
19
SSL* ssl;
20
SSL_CTX* ctx;
21![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
22
BIO* bio;
23
int ret;
24
char* request = "GET / HTTP/1.1\x0D\x0AHost: www.google.com\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A";
25
char buf[1024];
26![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
27
ERR_load_BIO_strings();
28
SSL_load_error_strings();
29
SSLeay_add_ssl_algorithms();
30![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
31
ctx = SSL_CTX_new ( SSLv23_client_method() );
32![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!ctx)
{
33
ERR_print_errors_fp(stderr);
34
std::cout<<"SSL_CTX_new error."<<std::endl;
35
return -1;
36
}
37![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
38![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////
39
// 建立链接 //
40![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////
41
bio = BIO_new_ssl_connect(ctx);
42
BIO_get_ssl(bio, & ssl);
43
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
44
BIO_set_conn_hostname(bio, "www.google.com:443");
45![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(BIO_do_connect(bio) <= 0)
{
46
std::cout<<"BIO_do_connect error."<<std::endl;
47
return -1;
48
}
49
50![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
51
// 发送请求
52
BIO_write(bio, request, strlen(request));
53![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
54
// 接收数据
55![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(;;)
{
56
ret = BIO_read(bio, buf, 1023);
57![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ret <= 0)
{
58
break;
59
}
60
buf[ret] = 0;
61
std::cout<<buf<<std::endl;
62
}
63![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
64
BIO_free_all(bio);
65
SSL_CTX_free(ctx);
66
return 0;
67
}
68![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)