openssl编程之客户端
继上一篇博文:http://blog.csdn.net/fly2010love/article/details/46415307 openssl证书生产过程,我们得到了:
client使用的文件有:ca.crt,client.crt,client.key
server使用的文件有:ca.crt,server.crt,server.key
新建一个目录为client,将ca.crt,client.crt,client.key三个文件拷贝进去,
[root@localhost SSLTestC]# ls -l
总用量 182
drwxrwx---. 1 root vboxsf 0 6月 9 16:04 client
drwxrwx---. 1 root vboxsf 0 6月 9 16:04 Debug
-rwxrwx---. 1 root vboxsf 1938 6月 8 11:03 Makefile
-rwxrwx---. 1 root vboxsf 3549 6月 10 09:40 SSLTestC.vcxproj
-rwxrwx---. 1 root vboxsf 946 6月 8 11:43 SSLTestC.vcxproj.filters
-rwxrwx---. 1 root vboxsf 143 6月 8 11:03 SSLTestC.vcxproj.user
-rwxrwx---. 1 root vboxsf 70154 6月 11 15:17 test
-rwxrwx---. 1 root vboxsf 3360 6月 9 14:54 TestMain.cpp
-rwxrwx---. 1 root vboxsf 104392 6月 11 15:17 TestMain.o
[root@localhost SSLTestC]#
在程序中加载三个文件,一个是ca证书,一个是自己的证书,一个是自己的私钥,分别使用openssl的接口加载进来,
注意:由于ca证书签名是自己做的,所以如果在浏览器或者其他一些地方访问时,会提示证书为不可信任证书,此阶段暂时忽略,继续访问即可,若客户端为自己开发的则无此问题,程序支持windows和linux
程序如下:
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include <sys/types.h>
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#else
#define close(x) closesocket(x)
#endif
#include <cstdio>
#include <string>
#define SERVER_PORT 8080
#define SERVER_IP "127.0.0.1"
#define CA_CERT_FILE "client/ca.crt"
#define CLIENT_CERT_FILE "client/client.crt"
#define CLIENT_KEY_FILE "client/client.key"
int main(int argc, char **argv)
{
std::string address = SERVER_IP;
#ifdef WIN32
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
{
printf("Error at WSAStartup()\n");
exit(-1);
}
printf("Server Running in WONDOWS\n");
#else
printf("Server Running in LINUX\n");
#endif
if(argc > 1)
{
address = argv[1];
}
SSL_METHOD *meth;
SSL_CTX *ctx;
SSL *ssl;
int nFd;
int nLen;
char szBuffer[1024];
SSLeay_add_ssl_algorithms();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_BIO_strings();
ctx = SSL_CTX_new (SSLv23_method());
if( ctx == NULL)
{
printf("SSL_CTX_new error!\n");
ERR_print_errors_fp(stderr);
return -1;
}
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
printf("SSL_CTX_load_verify_locations start!\n");
if(!SSL_CTX_load_verify_locations(ctx, CA_CERT_FILE, NULL))
{
printf("SSL_CTX_load_verify_locations error!\n");
ERR_print_errors_fp(stderr);
return -1;
}
if(SSL_CTX_use_certificate_file(ctx, CLIENT_CERT_FILE, SSL_FILETYPE_PEM) <= 0)
{
printf("SSL_CTX_use_certificate_file error!\n");
ERR_print_errors_fp(stderr);
return -1;
}
nFd = ::socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_addr.s_addr = inet_addr(address.c_str());
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVER_PORT);
if(connect(nFd, (sockaddr *)&addr, sizeof(addr)) < 0)
{
printf("connect\n");
ERR_print_errors_fp(stderr);
return -1;
}
ssl = SSL_new (ctx);
if( ssl == NULL)
{
printf("SSL_new error!\n");
ERR_print_errors_fp(stderr);
return -1;
}
SSL_set_fd (ssl, nFd);
if( SSL_connect (ssl) != 1)
{
printf("SSL_new error!\n");
ERR_print_errors_fp(stderr);
return -1;
}
sprintf(szBuffer, "\nthis is from client+++++++++++++++client send to server");
SSL_write(ssl, szBuffer, strlen(szBuffer));
memset(szBuffer, 0, sizeof(szBuffer));
nLen = SSL_read(ssl,szBuffer, sizeof(szBuffer));
fprintf(stderr, "Get Len %d %s ok\n", nLen, szBuffer);
SSL_free (ssl);
SSL_CTX_free (ctx);
close(nFd);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
编写makefile后者使用g++ -o xxx.cpp -I/usr/local/ssl/include/ -lssl testC进行编译得到testC可可执行文件
下篇博文将介绍服务器端编程
参考博客:http://hoytluo.blog.51cto.com/1154435/564822/
原文链接:http://blog.csdn.net/fly2010love/article/details/46458805