WINCE 6.0中的OpenSSL

时间:2018-03-13 07:34:35

标签: c++ c azure openssl windows-ce

我已经为Win CE 6.0编译了OpenSSL 1.0.0,并尝试在Visual Studio 2008智能设备项目中使用C ++与Microsoft azure IOT Hub建立安全的SSL连接。使用其他安全网站作为主机名,我能够连接并从服务器获得响应,而与azure IOT集线器主机名连接失败,GetLastError()错误代码 - 87。 相同的代码也适用于具有azure主机名的Windows平台。 以下是我正在使用的代码

#include "tchar.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>

#define HOST "xxxxx.azure-devices.net"
//#define HOST "www.random.org"
#define PORT "443"
#define HOST_RESOURCE "/devices/X2Pro/messages/deviceBound?api-version=2016-11-14"
#if (SSLEAY_VERSION_NUMBER >= 0x0907000L)
# include <openssl/conf.h>
#endif

int wmain(int argc, _TCHAR* argv[])
{
 /* Define varialbles */ 
    SSL * ssl; 
    SSL_CTX * ctx; 
    X509 * server_cert; 
    int p,err; 
    char r[1024]; 
    int size,i;
    char buf[1024];
    BIO * bio; 
    DWORD Temp;
    char * request = "GET / HTTP/1.1\x0D\x0AHost: www.verisign.com\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A"; 

    /* Set up the library */ 
    ERR_load_BIO_strings(); 
    SSL_load_error_strings(); 
    OpenSSL_add_all_algorithms(); 
    SSL_library_init(); 

    /* Set up the SSL context */ 
    ctx = SSL_CTX_new(SSLv23_client_method()); 

    /* Setup the connection */ 
    bio = BIO_new_ssl_connect(ctx); 
    if(bio == NULL)
    {
         fprintf(stderr, "Error attempting to connect\n");
    }

    /* Set the SSL_MODE_AUTO_RETRY flag */ 
    BIO_get_ssl(bio, & ssl); 
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 


    /* Create and setup the connection */ 
    i = BIO_set_conn_hostname(bio,HOST); 
    i = BIO_set_conn_port(bio,PORT);

    if(BIO_do_connect(bio) <= 0) 
    { 
        fprintf(stderr, "Error attempting to connect\n"); 
        ERR_print_errors_fp(stderr); 
        Temp = GetLastError();
        printf("Error Code : %d\n",Temp); 
        BIO_free_all(bio); 
        SSL_CTX_free(ctx); 
        //return 0; 
    } 

    /*information about connection*/ 
    printf ("SSL connection using %s\n", SSL_get_cipher (ssl)); 
    server_cert = SSL_get_peer_certificate (ssl); 
    printf ("%s\n",SSL_get_version(ssl)); 
    printf ("Server certificate: %s\n\n\n", server_cert); 

    request =  "POST /devices/X2Pro/messages/events?api-version=2016-11-14 HTTP/1.1\r\n"
                      "Host: xxxxxxxxx.azure-devices.net\r\n"
                      "Authorization: SharedAccessSignature sr=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"
                      "Content-Type: application/json\r\n" 
                      "Content-Length: 5\r\n" 
                      "\n"
                      "Hello\r\n";
    BIO_write(bio, request, strlen(request)); 
    /*for(;;)
    {
        size = BIO_read(bio, buf, 1023);
        if(size <= 0)
        {
            break;
        }
        buf[size] = 0;
        printf("%s", buf);
    }*/
}

0 个答案:

没有答案