1 | /* $Id: tstHttp.cpp 45331 2013-04-04 09:37:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple cURL testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/http.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 |
|
---|
38 | #define CAFILE_NAME "tstHttp-tempcafile.crt"
|
---|
39 |
|
---|
40 | int main()
|
---|
41 | {
|
---|
42 | unsigned cErrors = 0;
|
---|
43 |
|
---|
44 | RTR3InitExeNoArguments(RTR3INIT_FLAGS_SUPLIB);
|
---|
45 |
|
---|
46 | RTHTTP hHttp;
|
---|
47 | int rc = RTHttpCreate(&hHttp);
|
---|
48 | 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);
|
---|
102 | if (RT_SUCCESS(rc))
|
---|
103 | rc = RTHttpGet(hHttp,
|
---|
104 | "https://update.virtualbox.org/query.php?platform=LINUX_32BITS_UBUNTU_12_04&version=4.1.18",
|
---|
105 | &pszBuf);
|
---|
106 | RTHttpDestroy(hHttp);
|
---|
107 |
|
---|
108 | if (RT_FAILURE(rc))
|
---|
109 | cErrors++;
|
---|
110 |
|
---|
111 | RTPrintf("Error code: %Rrc\nGot: %s\n", rc, pszBuf);
|
---|
112 | RTMemFree(pszBuf);
|
---|
113 |
|
---|
114 | // RTFileDelete(CAFILE_NAME);
|
---|
115 |
|
---|
116 | return !!cErrors;
|
---|
117 | }
|
---|