OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用
1.安装开发库
sudo apt-get install libssl-dev
2.创建c语言文件
我命名为 test.c
3.包含ssl头文件
/*openssl*/
#include <openssl/ssl.h>
#include <openssl/bio.h>
4.包含socket头文件
/*socket*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
5.添加标准库
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
6.添加一个结构体保存基本信息
struct email{
char *username; //登陆用户名
char *password; //登陆密码
char *from; //邮箱来自
char *send; //邮箱发送
char *title; //邮件标题
char *body; //邮件内容
}mail;
7.新建一个函数 GetEmailInfo() 用于获取基本信息
int GetEmailInfo() {
mail.username = (char *)malloc(64 * (sizeof(char)));
mail.password = (char *)malloc(64 * (sizeof(char)));
mail.title = (char *)malloc(64 * (sizeof(char)));
mail.body = (char *)malloc(64 * (sizeof(char)));
mail.send = (char *)malloc(64 * (sizeof(char)));
mail.from = (char *)malloc(64 * (sizeof(char)));
printf("请输入你的邮箱地址:");
scanf("%s",mail.username);
printf("请输入你的邮箱密码:");
scanf("%s", mail.password);
printf("请输入收件人信息:");
scanf("%s", mail.send);
printf("请输入邮件的标题:");
scanf("%s",mail.title);
printf("请输入邮件内容:");
scanf("%s",mail.body);
strcpy(mail.from, mail.username);
return 0;
}
8. 新建函数 char *GetIpAddress(const char *domain) 用于把域名转换为ip
char *GetIpAddress(const char *domain){
const char *ip = NULL;
struct in_addr *host_name;
struct hostent *list =gethostbyname(domain);
int x=0;
for(x;list->h_addr_list[x]!=0;x++){
host_name= list->h_addr_list[x];
ip = inet_ntoa(*host_name);
}
return ip;
}
9.新建函数 ConnectServer(const char * ServerAddr)用于连接服务器
int connectToServer(const char *ServerAdd){
int socket_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
struct sockaddr_in client;
memset(&client,0,sizeof(client));
client.sin_family = AF_INET;
client.sin_port =htons(465);
if(inet_pton(AF_INET,GetIpAddress(ServerAdd),&client.sin_addr)==1){
connect(socket_fd,&client,sizeof(client));
}
return socket_fd;
}
10.base64
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char * mybase64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
}
int main(void){
GetEmailInfo();
BIO *obj_bio =NULL;
BIO *obj_out =NULL;
const SSL_METHOD *method =NULL;
SSL_CTX *ctx=NULL;
SSL *ssl;
int connect_fd = 0;
int ret,i;
ret=i=0;
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_CRYPTO_strings();
SSL_load_error_strings();
obj_bio = BIO_new(BIO_s_file());
obj_out = BIO_new_fp(stdout,BIO_NOCLOSE);
if(SSL_library_init()<=0){
BIO_printf(obj_out,"ssl iniliatile fail");
}
else{
method = SSLv23_client_method();
if((ctx=SSL_CTX_new(method))==NULL){
BIO_printf(obj_out,"open ssl fail");
}
else{
SSL_CTX_set_options(ctx,SSL_OP_NO_SSLv2);
ssl=SSL_new(ctx);
connect_fd = connectToServer("smtp.gmail.com");
if(connect_fd!=0){
BIO_printf(obj_out,"success connect\n");
SSL_set_fd(ssl,connect_fd);
if(SSL_connect(ssl) !=0){
char *rn ="\r\n";
char buf[4096];
char rbuf[4768];
memset(buf,0,sizeof(buf));
memset(rbuf,0,sizeof(rbuf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(&buf,"HELO SERVER");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(&buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(&buf,"AUTH LOGIN");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(&buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
mybase64_encode(mail.username,&buf,strlen(mail.username));
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));;
mybase64_encode(mail.password,&buf,strlen(mail.password));
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(buf,"mail from:<");
strcat(buf,mail.from);
strcat(buf,">");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(buf,"rcpt to:<");
strcat(buf,mail.send);
strcat(buf,">");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(buf,"data");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(buf,"subject:");
strcat(buf,mail.title);
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
strcat(buf,mail.body);
strcat(buf,rn);
strcat(buf,".");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
strcpy(buf,"quit");
strcat(buf,rn);
SSL_write(ssl,buf,strlen(buf));
BIO_printf(obj_out,buf);
memset(buf,0,sizeof(buf));
SSL_read(ssl,rbuf,sizeof(rbuf));
BIO_printf(obj_out,rbuf);
memset(rbuf,0,sizeof(rbuf));
}
}
}
}
return 0;
}