首页 > 解决方案 > 在 windows 上添加 openssl c 语言库

问题描述

我正在用 c 制作我的第一个项目。我想建立一个邮件客户端。我用套接字来做到这一点。

但是这种方法有些麻烦。由于编码协议,我无法使用 gmail smtp 服务器发送邮件。所以经过一些研究,我明白我没有很多选择。我必须使用 openssl 库。

这就是问题所在。我了解如何使用该库(我希望您可以通过附上我的代码来判断它),但我永远不明白如何添加该库。

在 stackoverflow 上,我找到了适用于 linux 和 android 的解决方案,因此我创建了一个新帖子。

所以这就是我的问题:我应该如何处理从openssl 官方网站下载的 zip 文件?甚至我需要下载哪个 zip 文件?

SOCKET connexion(char* server_name,unsigned short port)
{
    char buf[1024]={0};
    int res_l= 0;
    int nbrecv;
    struct sockaddr_in   serverSockAddr;                                               
    struct hostent     * serverHostEnt;                                                                 
    SOCKET to_server_socket = 0;
    memset(&serverSockAddr,0, sizeof(serverSockAddr) );                             
    serverHostEnt = gethostbyname( server_name );                                     
    if ( serverHostEnt == NULL )
    {
        res_l = h_errno;
        return (SOCKET)-1;
    }
    memcpy(&serverSockAddr.sin_addr,serverHostEnt->h_addr, serverHostEnt->h_length );   
        serverSockAddr.sin_port = htons( port );                                    
        serverSockAddr.sin_family = AF_INET;                                        

    to_server_socket = socket( AF_INET, SOCK_STREAM, 0 );                               

    if( connect( to_server_socket, ( struct sockaddr * ) &serverSockAddr,sizeof( serverSockAddr ) ) < 0 ) return (SOCKET)-3;
        while( !buf[0] ) nbrecv = recv( to_server_socket, buf, 1024, 0 );
        printf("Welcome message : %s\n",buf);                                                                                                               
        return to_server_socket;
}

void SendAndReceiveSSL(SSL * ssl, char * messagesend, int n){
{   
    char bufreceive[1024];                                                                  
    int size,retVal,nbrecv;                                                           
    size  = (int)strlen( messagesend );                                                
    retVal = SSL_write( ssl, messagesend, size);                                                                      
    printf("Envoye : %s\n",messagesend)   ;
    memset(bufreceive,0,1024);                                                                                                       
    if (n!=1)  //n=0 if i don't want an answer of the server.
    {
        while(!bufreceive[0]) nbrecv = SSL_read( ssl, bufreceive,1024);                                                                          
        printf("Recu : %s\n",bufreceive);                                                      
    }    
}

int mail_ssl(SOCKET sock, SSL ssl, const char* from,const char* to,const char* body)
{
#define SIZEMAX 1000

    char buffer[SIZEMAX]; int n=0;              
    SendAndReceive(sock, "EHLO localhost\r\n",n);       //no ssl for the 2 1st sentences//EHLO localhose ... wait 250
    SendAndReceive(sock, "STARTTLS\r\n,n)

    SendAndReceiveSSL(ssl, "EHLO localhost\r\n",n);     //sll for the next
    sprintf(buffer,"MAIL FROM: <%s>\r\n",from); 
    SendAndReceiveSSL(ssl, buffer,n);                           //MAIL FROM:<******> wait 250
    sprintf(buffer,"RCPT TO: <%s>\r\n",to);      
    SendAndReceiveSSL(ssl, buffer,n);                           //RCPT TO:<******> wait 250
    SendAndReceiveSSL(ssl, "DATA\r\n",n);                           //DATA wait 340
    sprintf(buffer, "Subject: %s\r\n",body); n=1;   
    SendAndReceiveSSL(ssl, buffer,n);    n=0;                       //Subject : subject \r\n body\r\n  ///// DON'T WAIT
    SendAndReceiveSSL(ssl, ".\r\n",n);                          //.\r\n                   wait 250
    SendAndReceiveSSL(ssl, "QUIT\r\n",n);
    return 0;
}

SSL_CTX* InitCTX(void)
{
    SSL_METHOD *method;
        SSL_CTX *ctx;

        OpenSSL_add_all_algorithms();  // Load cryptos, et.al.
        SSL_load_error_strings();   // Bring in and register error messages 
        method = TLSv1_2_client_method();  // Create new client-method instance 
        ctx = SSL_CTX_new(method);   // Create new context 
        if ( ctx == NULL )
     {
             ERR_print_errors_fp(stderr);
             abort();
        }
     return ctx;
}

void ShowCerts(SSL* ssl)
{
    X509 *cert;
    char *line;

    cert = SSL_get_peer_certificate(ssl); // get the server's certificate
    if ( cert != NULL )
    {
        printf("Server certificates:\n");
        line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
        printf("Subject: %s\n", line);
        free(line);       // free the malloc'ed string 
        line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
        printf("Issuer: %s\n", line);
        free(line);       // free the malloc'ed string 
            X509_free(cert);     // free the malloc'ed certificate copy 
    }
        else printf("Info: No client certificates configured.\n");
}

int authSSL(SOCKET sock, const char* from,const char* to,const char* body)
{   
    SSL_CTX *ctx;
    SSL *ssl;

    SSL_library_init();

    ctx = InitCTX();
    ssl = SSL_new(ctx);      
    SSL_set_fd(ssl, sock);     
    if ( SSL_connect(ssl) == FAIL )    
        ERR_print_errors_fp(stderr);
    else
    {  
            printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        ShowCerts(ssl);         
            SSL_write(ssl, msg, strlen(msg));    

        mail_ssl(sock, ssl, from, to, body); //In this function i collect
//all the informations to create the mail and i use all the function above
        SSL_free(ssl);
        }
    close(server);         // close socket 
    SSL_CTX_free(ctx);        // release context 
    return 0;
}

标签: cemailsslopenssl

解决方案


您可以使用VCPKG 包管理器,如果您使用 Visual Studio,它将下载并配置您的环境


推荐阅读