Chinaunix首页 | 论坛 | 博客
  • 博客访问: 663091
  • 博文数量: 109
  • 博客积分: 6081
  • 博客等级: 准将
  • 技术积分: 1318
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:28
文章分类
文章存档

2011年(8)

2010年(39)

2009年(62)

分类: C/C++

2010-06-08 23:15:49


/* OpenSSL headers */
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
/* Initializing OpenSSL */
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
SSL_library_init();
int len;
BIO *bio;
char *buf = "user lailaigq\n";
char *buf1 = "pass XXXX\n";
char *buf3 ="list\n";
char *buf4 ="uidl 1\n";
len = strlen(buf);
char buf2[1024];
char buf11[1024];
SSL_CTX  *ctx=SSL_CTX_new(TLSv1_client_method());    //新建ssl认证方法
SSL *ssl;
if(!SSL_CTX_load_verify_locations(ctx,"/etc/pki/tls/certs/ca-bundle.crt",NULL))  //加载所有证书进行验证
{
    /* Handle failed load here */
  printf("Error: %s\n",ERR_reason_error_string(ERR_get_error()));   //加载所有证书是否正常
}

bio = BIO_new_ssl_connect(ctx);     //设置BIO 对像
BIO_get_ssl(bio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
/* Attempt to connect */
//设置连接
BIO_set_conn_hostname(bio,"72.14.213.109:995");

/* Verify the connection opened and perform the handshake */
//验证连接是否打开,而且执行握手情况
if(BIO_do_connect(bio) <= 0)   //连接失败
{
    /* Handle failed connection */
  printf("Error: %s\n",ERR_reason_error_string(ERR_get_error()));   //加载所有证书是否正常
  return 0;
}
//检查证书是否有效
if(SSL_get_verify_result(ssl) != X509_V_OK)
{
    /* Handle the failed verification */
  printf("Error: %s\n",ERR_reason_error_string(ERR_get_error()));   //加载所有证书是否正常
  return 0;
}
//写入到连接
if(BIO_write(bio, buf, len) <= 0)
{
    if(! BIO_should_retry(bio))
    {
        /* Handle failed write here */
        printf("Handle failed write here");
    }
 
    /* Do something to handle the retry */
}
//从连接读取
int x = BIO_read(bio, buf2, 1024);
if(x == 0)
{
   /* Handle closed connection */
   return 0;
}
else if(x < 0)
{
   if(! BIO_should_retry(bio))
    {
        /* Handle failed read here */
        printf("Handle failed read here ");
   }
   /* Do something to handle the retry */
}else
{
 printf("result:%s\n",buf2);

}
//写入到连接
if(BIO_write(bio, buf1, strlen(buf1)) <= 0)
{
    if(! BIO_should_retry(bio))
    {
        /* Handle failed write here */
        printf(" Handle failed write here");
    }
 
    /* Do something to handle the retry */
}
//从连接读取
x = BIO_read(bio, buf2, 1024);
if(x == 0)
{
   /* Handle closed connection */
   return 0;
}
else if(x < 0)
{
   if(! BIO_should_retry(bio))
    {
        /* Handle failed read here */
        printf("Handle failed read  here ");
   }
   /* Do something to handle the retry */
 return 0;
}
else
{
 printf("result:%s\n",buf2);
}

//写入到连接
if(BIO_write(bio,buf3, strlen(buf3)) <= 0)
{
    if(! BIO_should_retry(bio))
    {
        /* Handle failed write here */
        printf(" Handle failed write here");
    }
 
    /* Do something to handle the retry */
}
//从连接读取
x = BIO_read(bio, buf11,1024);
if(x == 0)
{
   /* Handle closed connection */
   return 0;
}
else if(x < 0)
{
   if(! BIO_should_retry(bio))
    {
        /* Handle failed read here */
   }
   /* Do something to handle the retry */
 return 0;
}
else
{
 printf("result:%s\n",buf11);
}

//写入到连接
if(BIO_write(bio,buf4, strlen(buf4)) <= 0)
{
    if(! BIO_should_retry(bio))
    {
        /* Handle failed write here */
        printf(" Handle failed write here");
    }
 
    /* Do something to handle the retry */
}
//从连接读取
x = BIO_read(bio, buf11,1024);
if(x == 0)
{
   /* Handle closed connection */
   return 0;
}
else if(x < 0)
{
   if(! BIO_should_retry(bio))
    {
        /* Handle failed read here */
   }
   /* Do something to handle the retry */
 return 0;
}
else
{
 printf("result:%s\n",buf11);
}

SSL_CTX_free(ctx);
BIO_free_all(bio);
return 1;
}
 
Makefile 如下:
project = testssl
$(project):$(project).c
        gcc -Wall -o $@ -lssl  $<
 
编译
make
阅读(3717) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~