当我一起使用curl和hmac时为什么coredump?

时间:2016-10-17 10:21:17

标签: libcurl hmac

当我使用hmac和curl然后使用programe coredump。并且日志在那里运行

"Oct 17 17:58:37 info [6382]: 6385 httpsPost before apped headers"

我在gdb中获得了信息

Program terminated with signal 11, Segmentation fault.
#0  0x0819a8e7 in EVP_MD_CTX_md ()
(gdb) where
#0  0x0819a8e7 in EVP_MD_CTX_md ()
Cannot access memory at address 0x4004

这些信息对我没有帮助,有人知道吗?

但是当我删除代码时:

HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, AUTH_KEY, strlen(AUTH_KEY), EVP_sha256(), NULL);
HMAC_Update(&ctx, (unsigned char*)&szJsonData, strlen(szJsonData));
HMAC_Final(&ctx, result, &len);
HMAC_CTX_cleanup(&ctx);

运行良好,我不知道为什么

我收到了这些信息:

  • 成功设置证书验证位置:
  • CAfile:无 CApath:/ etc / ssl / certs

    void postHttps(uint32_t uid,std :: map& headResult,std :: map& bodyResult) {

    char szJsonData[1024];
    memset(szJsonData, 0, sizeof(szJsonData));
    std::ostringstream requetContent;
    requetContent<<"{\"cmuid\":\""<<uid<<"\",\"s\":2}";
    
    strcpy(szJsonData, requetContent.str().c_str());
    
    char bodyBuff[1024];
    memset(bodyBuff, 0, sizeof(bodyBuff));
    
    char headBuff[1024];
    memset(headBuff, 0, sizeof(headBuff));
    uint32_t start = sox::env::now;
    uint32_t ssend = sox::env::now;
    
    
    unsigned char* result;
    char sign[1024] = {0};
    unsigned int len = 256;
    result = (unsigned char*)malloc(sizeof(char) * len);
    log(Info,"postHttps accpet");
    
    
    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);
    HMAC_Init_ex(&ctx, AUTH_KEY, strlen(AUTH_KEY), EVP_sha256(), NULL);
    HMAC_Update(&ctx, (unsigned char*)&szJsonData, strlen(szJsonData));
    HMAC_Final(&ctx, result, &len);
    HMAC_CTX_cleanup(&ctx);
    
    for (unsigned int i = 0; i != len; i++){
        int length = strlen(sign);
        char *s = sign;
        sprintf(s + length, "%02x", (unsigned int)result[i]);
    }
    
    free(result);
    
    string auth_head = "Authorization:";
    auth_head += UrlEncode(sign).c_str();
    
    log(Info,"postHttps requetContent %s auth_head %s size %u len %u", requetContent.str().c_str(),auth_head.c_str(),requetContent.str().size(),strlen(szJsonData));
    
    try 
    {
        CURL *pCurl = NULL;
        CURLcode res;
    
        pCurl = curl_easy_init();
        if (NULL != pCurl) 
        {
            curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 2);
    
            curl_easy_setopt(pCurl, CURLOPT_URL, "https://www.xxxx.com/id");
            curl_easy_setopt(pCurl, CURLOPT_POST, 1L);
            curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, false);
            curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 2);
            //curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);
            //curl_easy_setopt(pCurl, CURLOPT_UPLOAD, 1);
            curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
            struct curl_slist *headers = NULL;
    
            log(Info,"httpsPost before apped headers");
    
            headers = curl_slist_append(headers, auth_head.c_str());
            headers = curl_slist_append(headers, "Accept: text/plain");
            headers = curl_slist_append(headers, "charset:utf-8");
            headers = curl_slist_append(headers, "Content-Type: application/json");
    
            curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(pCurl, CURLOPT_FORBID_REUSE, 1);
            curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, on_writebuff); 
            curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, bodyBuff);
            curl_easy_setopt(pCurl, CURLOPT_HEADERFUNCTION, header_handler);    
            curl_easy_setopt(pCurl, CURLOPT_WRITEHEADER, headBuff);
    
            curl_easy_setopt(pCurl,CURLOPT_POSTFIELDS, requetContent.str().c_str());
            curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE,requetContent.str().size()); 
    
            int HTTP_flag = 0;
            curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &HTTP_flag); 
            res = curl_easy_perform(pCurl);
            log(Info,"httpsPost after curl_easy_perform");
    
            curl_slist_free_all(headers);
            curl_easy_cleanup(pCurl);
    
    
            if (res == CURLE_OK) 
            {
                std::string httpRes = headBuff;
    
    
                HttpParser::ParseResponseHead(httpRes,headResult);
    
                std::map<std::string,std::string>::iterator it = headResult.find("HTTP");
                if(it != headResult.end())
                {
                    std::string& value = it->second;
                    if(value != "200 OK")
                    {
                        log(Error,"postHttps HTTP %s", value.c_str());
                    }
                    else 
                    {
    
                        json_char* json = (json_char*)bodyBuff;
                        json_value* value = json_parse(json,strlen(bodyBuff));
    
                        process_value(value, 0,bodyResult);
                        json_value_free(value);
    
                        log(Info,"httpsPost headMap %d bodyMap %d", headResult.size(),bodyResult.size());
    
                    }
                }
    
            }
            else 
            {
                log(Info,"curl_easy_perform() failed:%s", curl_easy_strerror(res));
            }
    
    
    
        }
    
    }
    catch (std::exception &ex)
    {
        log(Info,"curl exception %s", ex.what());
    }
    
    log(Info,"httpPost uid %u elapse %u", uid,ssend-start);
    

    }

1 个答案:

答案 0 :(得分:0)

使用ssl,你应该初衷一些。如下所示

void Callbackfee::init_locks(void)
{
    int i;

    if(m_lockarray == NULL)
    {
        m_lockarray = (pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
                                                    sizeof(pthread_mutex_t));
    }
    if(m_lockarray != NULL)
    {
        for (i = 0; i < CRYPTO_num_locks(); ++i) {
            pthread_mutex_init(&(m_lockarray[i]), NULL);
        }
        CRYPTO_set_id_callback(&thread_id);
        CRYPTO_set_locking_callback(&lock_callback);
    }
}