VirtualBox

Changeset 45331 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Apr 4, 2013 9:37:00 AM (12 years ago)
Author:
vboxsync
Message:

Runtime/http: add new function to set the filename containing the trusted root certificates, plus an extension of the testcase which fetches the root certificates

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/http.cpp

    r43713 r45331  
    1 
    21/* $Id$ */
    32/** @file
     
    65
    76/*
    8  * Copyright (C) 2012 Oracle Corporation
     7 * Copyright (C) 2012-2013 Oracle Corporation
    98 *
    109 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3534#include <iprt/mem.h>
    3635#include <iprt/string.h>
     36#include <iprt/file.h>
    3737
    3838#include <curl/curl.h>
     
    4949    long lLastResp;
    5050    struct curl_slist *pHeaders;
     51    const char *pcszCAFile;
    5152} RTHTTPINTERNAL;
    5253typedef RTHTTPINTERNAL *PRTHTTPINTERNAL;
     
    198199}
    199200
     201RTR3DECL(int) RTHttpSetCAFile(RTHTTP hHttp, const char *pcszCAFile)
     202{
     203    PRTHTTPINTERNAL pHttpInt = hHttp;
     204    RTHTTP_VALID_RETURN(pHttpInt);
     205
     206    pHttpInt->pcszCAFile = pcszCAFile;
     207
     208    return VINF_SUCCESS;
     209}
     210
    200211RTR3DECL(int) RTHttpGet(RTHTTP hHttp, const char *pcszUrl, char **ppszResponse)
    201212{
     
    213224#endif
    214225
    215     /* XXX */
    216     rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
    217     if (CURL_FAILED(rcCurl))
    218         return VERR_INTERNAL_ERROR;
     226    const char *pcszCAFile = "/etc/ssl/certs/ca-certificates.crt";
     227    if (pHttpInt->pcszCAFile)
     228        pcszCAFile = pHttpInt->pcszCAFile;
     229    if (RTFileExists(pcszCAFile))
     230    {
     231        rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, pcszCAFile);
     232        if (CURL_FAILED(rcCurl))
     233            return VERR_INTERNAL_ERROR;
     234    }
    219235
    220236    RTHTTPMEMCHUNK chunk = { NULL, 0 };
  • trunk/src/VBox/Runtime/testcase/tstHttp.cpp

    r43645 r45331  
    55
    66/*
    7  * Copyright (C) 2012 Oracle Corporation
     7 * Copyright (C) 2012-2013 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3131#include <iprt/http.h>
    3232#include <iprt/mem.h>
     33#include <iprt/file.h>
    3334#include <iprt/stream.h>
     35#include <iprt/string.h>
    3436#include <iprt/initterm.h>
    35 #include <iprt/thread.h>
     37
     38#define CAFILE_NAME "tstHttp-tempcafile.crt"
    3639
    3740int main()
     
    4447    int rc = RTHttpCreate(&hHttp);
    4548    char *pszBuf = NULL;
     49    PRTSTREAM CAFile = NULL;
     50
     51    // create certificate file
     52    rc = RTStrmOpen(CAFILE_NAME, "w+b", &CAFile);
     53
     54    // fetch root CA certificate (new one, often avoided in cert chains by
     55    // using an intermediate cert which is signed by old root)
     56    if (RT_SUCCESS(rc))
     57        rc = RTHttpGet(hHttp,
     58                       "http://www.verisign.com/repository/roots/root-certificates/PCA-3G5.pem",
     59                       &pszBuf);
     60    if (RT_SUCCESS(rc) && pszBuf)
     61    {
     62        /// @todo check certificate fingerprint against a strong hash,
     63        // otherwise there's a simple way for a man-in-the-middle attack
     64        rc = RTStrmWrite(CAFile, pszBuf, strlen(pszBuf));
     65        if (RT_SUCCESS(rc))
     66            rc = RTStrmWrite(CAFile, RTFILE_LINEFEED, strlen(RTFILE_LINEFEED));
     67    }
     68    if (pszBuf)
     69    {
     70        RTMemFree(pszBuf);
     71        pszBuf = NULL;
     72    }
     73
     74    // fetch root CA certificate (old one, but still very widely used)
     75    if (RT_SUCCESS(rc))
     76        rc = RTHttpGet(hHttp,
     77                       "http://www.verisign.com/repository/roots/root-certificates/PCA-3.pem",
     78                       &pszBuf);
     79    if (RT_SUCCESS(rc) && pszBuf)
     80    {
     81        /// @todo check certificate fingerprint against a strong hash,
     82        // otherwise there's a simple way for a man-in-the-middle attack
     83        rc = RTStrmWrite(CAFile, pszBuf, strlen(pszBuf));
     84        if (RT_SUCCESS(rc))
     85            rc = RTStrmWrite(CAFile, RTFILE_LINEFEED, strlen(RTFILE_LINEFEED));
     86    }
     87    if (pszBuf)
     88    {
     89        RTMemFree(pszBuf);
     90        pszBuf = NULL;
     91    }
     92
     93    // close certificate file
     94    if (CAFile)
     95    {
     96        RTStrmClose(CAFile);
     97        CAFile = NULL;
     98    }
     99
     100    if (RT_SUCCESS(rc))
     101        rc = RTHttpSetCAFile(hHttp, CAFILE_NAME);
    46102    if (RT_SUCCESS(rc))
    47103        rc = RTHttpGet(hHttp,
     
    50106    RTHttpDestroy(hHttp);
    51107
     108    if (RT_FAILURE(rc))
     109        cErrors++;
     110
    52111    RTPrintf("Error code: %Rrc\nGot: %s\n", rc, pszBuf);
    53112    RTMemFree(pszBuf);
    54113
     114//    RTFileDelete(CAFILE_NAME);
     115
    55116    return !!cErrors;
    56117}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette