openssl编程之客户端

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 68 11:03 Makefile
-rwxrwx---. 1 root vboxsf   3549 610 09:40 SSLTestC.vcxproj
-rwxrwx---. 1 root vboxsf    946 68 11:43 SSLTestC.vcxproj.filters
-rwxrwx---. 1 root vboxsf    143 68 11:03 SSLTestC.vcxproj.user
-rwxrwx---. 1 root vboxsf  70154 611 15:17 test
-rwxrwx---. 1 root vboxsf   3360 69 14:54 TestMain.cpp
-rwxrwx---. 1 root vboxsf 104392 611 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 
#ifndef WIN32
#include 
#include 
#include 
#include 
#else
#define close(x) closesocket(x)
#endif
#include 
#include 


#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
    //windows初始化网络环境
    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();  

    // 使用SSL V3,V2  
    ctx = SSL_CTX_new (SSLv23_method());
    if( ctx == NULL)
    {
        printf("SSL_CTX_new error!\n");
        ERR_print_errors_fp(stderr);
        return -1;
    }

    // 要求校验对方证书,表示需要验证服务器端,若不需要验证则使用  SSL_VERIFY_NONE
    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);  


    // 加载CA的证书
    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;
    }

    //// 加载自己的私钥 加载私钥需要密码,意思是每次链接服务器都需要密码
    //若服务器需要验证客户端的身份,则需要客户端加载私钥,由于此处我们只需要验证服务器身份,故无需加载自己的私钥
    //printf("SSL_CTX_use_PrivateKey_file start!\n");
    //if(SSL_CTX_use_PrivateKey_file(ctx, CLIENT_KEY_FILE, SSL_FILETYPE_PEM) <= 0)
    //{
    // printf("SSL_CTX_use_PrivateKey_file error!\n");
    // ERR_print_errors_fp(stderr);
    // return -1;
    //}

    //// 判定私钥是否正确  
    //if(!SSL_CTX_check_private_key(ctx))
    //{
    // printf("SSL_CTX_check_private_key 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 = 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);     
} 

编写makefile后者使用g++ -o xxx.cpp -I/usr/local/ssl/include/ -lssl testC进行编译得到testC可可执行文件
下篇博文将介绍服务器端编程
参考博客:http://hoytluo.blog.51cto.com/1154435/564822/

你可能感兴趣的:(C/C++,linux)