1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/* $Id: vboximgCrypto.cpp $ */
/** @file
* vboximgCypto.cpp - Disk Image Flattening FUSE Program.
*/
/*
* Copyright (C) 2009-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <iprt/cdefs.h>
#include <VBox/err.h>
#include <VBox/settings.h>
#include <VBox/vd.h>
#include "vboximgCrypto.h"
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/asm.h>
#include <iprt/memsafer.h>
/*
* Apparently there is a more COM:: oriented (but less efficient?) approach to dealing
* with the keystore and disk encryption, which will need to be investigated. Keeping
* all this duplicated code in a separate file until the ideal approach is determined.
*/
SecretKey::SecretKey(const uint8_t *pbKey, size_t cbKey, bool fKeyBufNonPageable)
{
m_cRefs = 0;
m_fRemoveOnSuspend = false;
m_cUsers = 0;
m_cbKey = cbKey;
int rc = RTMemSaferAllocZEx((void **)&this->m_pbKey, cbKey,
fKeyBufNonPageable ? RTMEMSAFER_F_REQUIRE_NOT_PAGABLE : 0);
if (RT_SUCCESS(rc))
{
memcpy(this->m_pbKey, pbKey, cbKey);
/* Scramble content to make retrieving the key more difficult. */
rc = RTMemSaferScramble(this->m_pbKey, cbKey);
}
else
throw rc;
}
SecretKey::~SecretKey()
{
Assert(!m_cRefs);
RTMemSaferFree(m_pbKey, m_cbKey);
m_cRefs = 0;
m_pbKey = NULL;
m_cbKey = 0;
m_fRemoveOnSuspend = false;
m_cUsers = 0;
}
uint32_t SecretKey::retain()
{
uint32_t cRefs = ASMAtomicIncU32(&m_cRefs);
if (cRefs == 1)
{
int rc = RTMemSaferUnscramble(m_pbKey, m_cbKey);
AssertRC(rc);
}
return cRefs;
}
uint32_t SecretKey::release()
{
uint32_t cRefs = ASMAtomicDecU32(&m_cRefs);
if (!cRefs)
{
int rc = RTMemSaferScramble(m_pbKey, m_cbKey);
AssertRC(rc);
}
return cRefs;
}
uint32_t SecretKey::refCount()
{
return m_cRefs;
}
int SecretKey::setUsers(uint32_t cUsers)
{
m_cUsers = cUsers;
return VINF_SUCCESS;
}
uint32_t SecretKey::getUsers()
{
return m_cUsers;
}
int SecretKey::setRemoveOnSuspend(bool fRemoveOnSuspend)
{
m_fRemoveOnSuspend = fRemoveOnSuspend;
return VINF_SUCCESS;
}
bool SecretKey::getRemoveOnSuspend()
{
return m_fRemoveOnSuspend;
}
const void *SecretKey::getKeyBuffer()
{
AssertReturn(m_cRefs > 0, NULL);
return m_pbKey;
}
size_t SecretKey::getKeySize()
{
return m_cbKey;
}
SecretKeyStore::SecretKeyStore(bool fKeyBufNonPageable)
{
m_fKeyBufNonPageable = fKeyBufNonPageable;
}
SecretKeyStore::~SecretKeyStore()
{
int rc = deleteAllSecretKeys(false /* fSuspend */, true /* fForce */);
AssertRC(rc);
}
int SecretKeyStore::addSecretKey(const com::Utf8Str &strKeyId, const uint8_t *pbKey, size_t cbKey)
{
/* Check that the ID is not existing already. */
SecretKeyMap::const_iterator it = m_mapSecretKeys.find(strKeyId);
if (it != m_mapSecretKeys.end())
return VERR_ALREADY_EXISTS;
SecretKey *pKey = NULL;
try
{
pKey = new SecretKey(pbKey, cbKey, m_fKeyBufNonPageable);
m_mapSecretKeys.insert(std::make_pair(strKeyId, pKey));
}
catch (int rc)
{
return rc;
}
catch (std::bad_alloc &)
{
if (pKey)
delete pKey;
return VERR_NO_MEMORY;
}
return VINF_SUCCESS;
}
int SecretKeyStore::deleteSecretKey(const com::Utf8Str &strKeyId)
{
SecretKeyMap::iterator it = m_mapSecretKeys.find(strKeyId);
if (it == m_mapSecretKeys.end())
return VERR_NOT_FOUND;
SecretKey *pKey = it->second;
if (pKey->refCount() != 0)
return VERR_RESOURCE_IN_USE;
m_mapSecretKeys.erase(it);
delete pKey;
return VINF_SUCCESS;
}
int SecretKeyStore::retainSecretKey(const com::Utf8Str &strKeyId, SecretKey **ppKey)
{
SecretKeyMap::const_iterator it = m_mapSecretKeys.find(strKeyId);
if (it == m_mapSecretKeys.end())
return VERR_NOT_FOUND;
SecretKey *pKey = it->second;
pKey->retain();
*ppKey = pKey;
return VINF_SUCCESS;
}
int SecretKeyStore::releaseSecretKey(const com::Utf8Str &strKeyId)
{
SecretKeyMap::const_iterator it = m_mapSecretKeys.find(strKeyId);
if (it == m_mapSecretKeys.end())
return VERR_NOT_FOUND;
SecretKey *pKey = it->second;
pKey->release();
return VINF_SUCCESS;
}
int SecretKeyStore::deleteAllSecretKeys(bool fSuspend, bool fForce)
{
/* First check whether a key is still in use. */
if (!fForce)
{
for (SecretKeyMap::iterator it = m_mapSecretKeys.begin();
it != m_mapSecretKeys.end();
++it)
{
SecretKey *pKey = it->second;
if ( pKey->refCount()
&& ( ( pKey->getRemoveOnSuspend()
&& fSuspend)
|| !fSuspend))
return VERR_RESOURCE_IN_USE;
}
}
SecretKeyMap::iterator it = m_mapSecretKeys.begin();
while (it != m_mapSecretKeys.end())
{
SecretKey *pKey = it->second;
if ( pKey->getRemoveOnSuspend()
|| !fSuspend)
{
AssertMsg(!pKey->refCount(), ("No one should access the stored key at this point anymore!\n"));
delete pKey;
SecretKeyMap::iterator itNext = it;
++itNext;
m_mapSecretKeys.erase(it);
it = itNext;
}
else
++it;
}
return VINF_SUCCESS;
}
void vboxImageCryptoSetup(VDISKCRYPTOSETTINGS *pSettings, const char *pszCipher,
const char *pszKeyStore, const char *pszPassword,
bool fCreateKeyStore)
{
pSettings->pszCipher = pszCipher;
pSettings->pszPassword = pszPassword;
pSettings->pszKeyStoreLoad = pszKeyStore;
pSettings->fCreateKeyStore = fCreateKeyStore;
pSettings->pbDek = NULL;
pSettings->cbDek = 0;
pSettings->vdFilterIfaces = NULL;
pSettings->vdIfCfg.pfnAreKeysValid = vboximgVdCryptoConfigAreKeysValid;
pSettings->vdIfCfg.pfnQuerySize = vboximgVdCryptoConfigQuerySize;
pSettings->vdIfCfg.pfnQuery = vboximgVdCryptoConfigQuery;
pSettings->vdIfCfg.pfnQueryBytes = NULL;
pSettings->vdIfCrypto.pfnKeyRetain = vboximgVdCryptoKeyRetain;
pSettings->vdIfCrypto.pfnKeyRelease = vboximgVdCryptoKeyRelease;
pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = vboximgVdCryptoKeyStorePasswordRetain;
pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = vboximgVdCryptoKeyStorePasswordRelease;
pSettings->vdIfCrypto.pfnKeyStoreSave = vboximgVdCryptoKeyStoreSave;
pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = vboximgVdCryptoKeyStoreReturnParameters;
int rc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
"vboximgVdInterfaceCfgCrypto",
VDINTERFACETYPE_CONFIG, pSettings,
sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
AssertRC(rc);
rc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
"vboximgVdInterfaceCrypto",
VDINTERFACETYPE_CRYPTO, pSettings,
sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
AssertRC(rc);
}
DECLCALLBACK(bool) vboximgVdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
{
/* Just return always true here. */
NOREF(pvUser);
NOREF(pszzValid);
return true;
}
DECLCALLBACK(int) vboximgVdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
AssertPtrReturn(pcbValue, VERR_INVALID_POINTER);
size_t cbValue = 0;
if (!strcmp(pszName, "Algorithm"))
cbValue = strlen(pSettings->pszCipher) + 1;
else if (!strcmp(pszName, "KeyId"))
cbValue = sizeof("irrelevant");
else if (!strcmp(pszName, "KeyStore"))
{
if (!pSettings->pszKeyStoreLoad)
return VERR_CFGM_VALUE_NOT_FOUND;
cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
}
else if (!strcmp(pszName, "CreateKeyStore"))
cbValue = 2; /* Single digit + terminator. */
else
return VERR_CFGM_VALUE_NOT_FOUND;
*pcbValue = cbValue + 1 /* include terminator */;
return VINF_SUCCESS;
}
DECLCALLBACK(int) vboximgVdCryptoConfigQuery(void *pvUser, const char *pszName,
char *pszValue, size_t cchValue)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
const char *psz = NULL;
if (!strcmp(pszName, "Algorithm"))
psz = pSettings->pszCipher;
else if (!strcmp(pszName, "KeyId"))
psz = "irrelevant";
else if (!strcmp(pszName, "KeyStore"))
psz = pSettings->pszKeyStoreLoad;
else if (!strcmp(pszName, "CreateKeyStore"))
{
if (pSettings->fCreateKeyStore)
psz = "1";
else
psz = "0";
}
else
return VERR_CFGM_VALUE_NOT_FOUND;
size_t cch = strlen(psz);
if (cch >= cchValue)
return VERR_CFGM_NOT_ENOUGH_SPACE;
memcpy(pszValue, psz, cch + 1);
return VINF_SUCCESS;
}
DECLCALLBACK(int) vboximgVdCryptoKeyRetain(void *pvUser, const char *pszId,
const uint8_t **ppbKey, size_t *pcbKey)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
NOREF(pszId);
NOREF(ppbKey);
NOREF(pcbKey);
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
}
DECLCALLBACK(int) vboximgVdCryptoKeyRelease(void *pvUser, const char *pszId)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
NOREF(pszId);
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
}
DECLCALLBACK(int) vboximgVdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
NOREF(pszId);
*ppszPassword = pSettings->pszPassword;
return VINF_SUCCESS;
}
DECLCALLBACK(int) vboximgVdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
NOREF(pszId);
return VINF_SUCCESS;
}
DECLCALLBACK(int) vboximgVdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
if (!pSettings->pszKeyStore)
return VERR_NO_MEMORY;
memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
return VINF_SUCCESS;
}
DECLCALLBACK(int) vboximgVdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
const uint8_t *pbDek, size_t cbDek)
{
VDISKCRYPTOSETTINGS *pSettings = (VDISKCRYPTOSETTINGS *)pvUser;
AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
pSettings->pszCipherReturned = RTStrDup(pszCipher);
pSettings->pbDek = pbDek;
pSettings->cbDek = cbDek;
return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
}
|