1 | /* $Id: CryptoUtils.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Main - Cryptographic utility functions used by both VBoxSVC and VBoxC.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2022-2024 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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef MAIN_INCLUDED_CryptoUtils_h
|
---|
29 | #define MAIN_INCLUDED_CryptoUtils_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <iprt/cdefs.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 | #include <iprt/vfs.h>
|
---|
37 |
|
---|
38 | #include <VBox/VBoxCryptoIf.h>
|
---|
39 | #include <VBox/com/string.h>
|
---|
40 |
|
---|
41 | #include <VBox/vmm/ssm.h>
|
---|
42 | #include <VBox/vmm/vmmr3vtable.h>
|
---|
43 | #include <VBox/vmm/vmapi.h>
|
---|
44 |
|
---|
45 | #include "SecretKeyStore.h"
|
---|
46 | #ifdef VBOX_COM_INPROC
|
---|
47 | # include "ConsoleImpl.h"
|
---|
48 | #else
|
---|
49 | # include "MachineImpl.h"
|
---|
50 | # include "VirtualBoxImpl.h"
|
---|
51 | #endif
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Class handling encrypted and non encrypted SSM files.
|
---|
56 | */
|
---|
57 | class SsmStream
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | #ifdef VBOX_COM_INPROC
|
---|
61 | SsmStream(Console *pParent, PCVMMR3VTABLE pVMM, SecretKeyStore *pKeyStore, const Utf8Str &strKeyId, const Utf8Str &strKeyStore);
|
---|
62 | #else
|
---|
63 | SsmStream(VirtualBox *pParent, SecretKeyStore *pKeyStore, const Utf8Str &strKeyId, const Utf8Str &strKeyStore);
|
---|
64 | #endif
|
---|
65 | ~SsmStream();
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Actually opens the stream for either reading or writing.
|
---|
69 | *
|
---|
70 | * @returns VBox status code.
|
---|
71 | * @param strFilename The filename of the saved state to open or create.
|
---|
72 | * @param fWrite Flag whether the stream should be opened for writing (true) or readonly (false).
|
---|
73 | * @param ppSsmHandle Where to store the SSM handle on success, don't call SSMR3Close() but the provided close() method.
|
---|
74 | */
|
---|
75 | int open(const Utf8Str &strFilename, bool fWrite, PSSMHANDLE *ppSsmHandle);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Opens the saved state file for reading, doesn't call SSMR3Open().
|
---|
79 | *
|
---|
80 | * @returns VBox status code.
|
---|
81 | * @param strFilename The filename of the saved state to open.
|
---|
82 | */
|
---|
83 | int open(const Utf8Str &strFilename);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Creates a new saved state file under the given path.
|
---|
87 | *
|
---|
88 | * @returns VBox status code.
|
---|
89 | * @param strFilename The filename of the saved state to create.
|
---|
90 | */
|
---|
91 | int create(const Utf8Str &strFilename);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Returns the pointer to the stream operations table after a succesful opening/creation.
|
---|
95 | *
|
---|
96 | * @return VBox status code.
|
---|
97 | * @param ppStrmOps Where to store the pointer to the stream operations table on success.
|
---|
98 | * @param ppvStrmOpsUser Where to store the pointer to the opaque user data on success.
|
---|
99 | */
|
---|
100 | int querySsmStrmOps(PCSSMSTRMOPS *ppStrmOps, void **ppvStrmOpsUser);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Closes an previously opened stream.
|
---|
104 | *
|
---|
105 | * @returns VBox status code.
|
---|
106 | */
|
---|
107 | int close(void);
|
---|
108 |
|
---|
109 | private:
|
---|
110 |
|
---|
111 | static DECLCALLBACK(int) i_ssmCryptoWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite);
|
---|
112 | static DECLCALLBACK(int) i_ssmCryptoRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
113 | static DECLCALLBACK(int) i_ssmCryptoSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual);
|
---|
114 | static DECLCALLBACK(uint64_t) i_ssmCryptoTell(void *pvUser);
|
---|
115 | static DECLCALLBACK(int) i_ssmCryptoSize(void *pvUser, uint64_t *pcb);
|
---|
116 | static DECLCALLBACK(int) i_ssmCryptoIsOk(void *pvUser);
|
---|
117 | static DECLCALLBACK(int) i_ssmCryptoClose(void *pvUser, bool fCancelled);
|
---|
118 |
|
---|
119 | #ifdef VBOX_COM_INPROC
|
---|
120 | Console *m_pParent;
|
---|
121 | PCVMMR3VTABLE m_pVMM;
|
---|
122 | #else
|
---|
123 | VirtualBox *m_pParent;
|
---|
124 | #endif
|
---|
125 | /** The key store for getting at passwords. */
|
---|
126 | SecretKeyStore *m_pKeyStore;
|
---|
127 | /** The key ID holding the password, empty if the saved state is not encrypted. */
|
---|
128 | Utf8Str m_strKeyId;
|
---|
129 | /** The keystore holding the encrypted DEK. */
|
---|
130 | Utf8Str m_strKeyStore;
|
---|
131 | /** The VFS file handle. */
|
---|
132 | RTVFSFILE m_hVfsFile;
|
---|
133 | /** The SSM handle when opened. */
|
---|
134 | PSSMHANDLE m_pSsm;
|
---|
135 | /** The SSM stream callbacks table. */
|
---|
136 | SSMSTRMOPS m_StrmOps;
|
---|
137 | /** The cryptographic interfacer. */
|
---|
138 | PCVBOXCRYPTOIF m_pCryptoIf;
|
---|
139 | };
|
---|
140 |
|
---|
141 | #endif /* !MAIN_INCLUDED_CryptoUtils_h */
|
---|