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]# 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在程序中加载三个文件,一个是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
    //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);     
} 
 
 
  • 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值