1 | /* $Id: RTCrStoreCreateSnapshotById-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Generic RTCrStoreCreateSnapshotById implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/crypto/store.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 | #include <iprt/file.h>
|
---|
47 | #include <iprt/dir.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Defined Constants And Macros *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** Unix root prefix. */
|
---|
54 | #ifdef RT_OS_OS2
|
---|
55 | # define UNIX_ROOT "/@unixroot@"
|
---|
56 | #elif defined(RT_OS_WINDOWS)
|
---|
57 | # define UNIX_ROOT "C:/cygwin"
|
---|
58 | #else
|
---|
59 | # define UNIX_ROOT
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | /*********************************************************************************************************************************
|
---|
64 | * Global Variables *
|
---|
65 | *********************************************************************************************************************************/
|
---|
66 | /** System PEM files worth looking at.
|
---|
67 | * @remarks Several of these could be symlinks to one of the others.
|
---|
68 | */
|
---|
69 | static const char *g_apszSystemPemFiles[] =
|
---|
70 | {
|
---|
71 | UNIX_ROOT "/etc/ssl/certs/ca-certificates.crt",
|
---|
72 | UNIX_ROOT "/etc/ssl/cert.pem",
|
---|
73 | UNIX_ROOT "/etc/ca-certificates/extracted/tls-ca-bundle.pem", /* Arch linux (ca 2015-08-xx) */
|
---|
74 | UNIX_ROOT "/etc/ca-certificates/extracted/email-ca-bundle.pem",
|
---|
75 | UNIX_ROOT "/etc/ca-certificates/extracted/objsign-ca-bundle.pem",
|
---|
76 | UNIX_ROOT "/etc/ca-certificates/extracted/ca-bundle.trust.crt",
|
---|
77 | UNIX_ROOT "/etc/ca-certificates/extracted/ca-bundle.trust.crt",
|
---|
78 | UNIX_ROOT "/etc/pki/tls/certs/ca-bundle.crt", /* Oracle Linux 5 */
|
---|
79 | UNIX_ROOT "/etc/pki/tls/cert.pem",
|
---|
80 | UNIX_ROOT "/etc/certs/ca-certificates.crt", /* Solaris 11 */
|
---|
81 | UNIX_ROOT "/etc/curl/curlCA",
|
---|
82 | };
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * System directories containing lots of pem/crt files.
|
---|
86 | */
|
---|
87 | static const char *g_apszSystemPemDirs[] =
|
---|
88 | {
|
---|
89 | UNIX_ROOT "/etc/openssl/certs/",
|
---|
90 | UNIX_ROOT "/etc/ssl/certs/",
|
---|
91 | UNIX_ROOT "/etc/ca-certificates/extracted/cadir/",
|
---|
92 | UNIX_ROOT "/etc/certs/CA/", /* Solaris 11 */
|
---|
93 | };
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(int) RTCrStoreCreateSnapshotById(PRTCRSTORE phStore, RTCRSTOREID enmStoreId, PRTERRINFO pErrInfo)
|
---|
97 | {
|
---|
98 | AssertReturn(enmStoreId > RTCRSTOREID_INVALID && enmStoreId < RTCRSTOREID_END, VERR_INVALID_PARAMETER);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Create an empty in-memory store.
|
---|
102 | */
|
---|
103 | RTCRSTORE hStore;
|
---|
104 | uint32_t cExpected = enmStoreId == RTCRSTOREID_SYSTEM_TRUSTED_CAS_AND_CERTIFICATES ? 256 : 0;
|
---|
105 | int rc = RTCrStoreCreateInMem(&hStore, cExpected);
|
---|
106 | if (RT_SUCCESS(rc))
|
---|
107 | {
|
---|
108 | *phStore = hStore;
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Add system certificates if part of the given store ID.
|
---|
112 | */
|
---|
113 | bool fFound = false;
|
---|
114 | rc = VINF_SUCCESS;
|
---|
115 | if (enmStoreId == RTCRSTOREID_SYSTEM_TRUSTED_CAS_AND_CERTIFICATES)
|
---|
116 | {
|
---|
117 | for (uint32_t i = 0; i < RT_ELEMENTS(g_apszSystemPemFiles); i++)
|
---|
118 | if (RTFileExists(g_apszSystemPemFiles[i]))
|
---|
119 | {
|
---|
120 | fFound = true;
|
---|
121 | int rc2 = RTCrStoreCertAddFromFile(hStore,
|
---|
122 | RTCRCERTCTX_F_ADD_IF_NOT_FOUND | RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR,
|
---|
123 | g_apszSystemPemFiles[i], pErrInfo);
|
---|
124 | if (RT_FAILURE(rc2))
|
---|
125 | rc = -rc2;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * If we didn't find any of the certificate collection files, go hunting
|
---|
130 | * for directories containing PEM/CRT files with single certificates.
|
---|
131 | */
|
---|
132 | if (!fFound)
|
---|
133 | for (uint32_t i = 0; i < RT_ELEMENTS(g_apszSystemPemDirs); i++)
|
---|
134 | if (RTDirExists(g_apszSystemPemDirs[i]))
|
---|
135 | {
|
---|
136 | static RTSTRTUPLE const s_aSuffixes[] =
|
---|
137 | {
|
---|
138 | { RT_STR_TUPLE(".crt") },
|
---|
139 | { RT_STR_TUPLE(".pem") },
|
---|
140 | { RT_STR_TUPLE(".PEM") },
|
---|
141 | { RT_STR_TUPLE(".CRT") },
|
---|
142 | };
|
---|
143 | fFound = true;
|
---|
144 | int rc2 = RTCrStoreCertAddFromDir(hStore,
|
---|
145 | RTCRCERTCTX_F_ADD_IF_NOT_FOUND | RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR,
|
---|
146 | g_apszSystemPemDirs[i], &s_aSuffixes[0], RT_ELEMENTS(s_aSuffixes),
|
---|
147 | pErrInfo);
|
---|
148 | if (RT_FAILURE(rc2))
|
---|
149 | rc = -rc2;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else
|
---|
154 | RTErrInfoAdd(pErrInfo, rc, " RTCrStoreCreateInMem failed");
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 | RT_EXPORT_SYMBOL(RTCrStoreCreateSnapshotById);
|
---|
158 |
|
---|