summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/include/CryptoUtils.h
blob: 7db74b965521bfefbdac1b667e53f00493b257cb (plain)
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
/* $Id: CryptoUtils.h $ */
/** @file
 * Main - Cryptographic utility functions used by both VBoxSVC and VBoxC.
 */

/*
 * Copyright (C) 2022 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
 */

#ifndef MAIN_INCLUDED_CryptoUtils_h
#define MAIN_INCLUDED_CryptoUtils_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include <iprt/cdefs.h>
#include <iprt/types.h>
#include <iprt/vfs.h>

#include <VBox/VBoxCryptoIf.h>
#include <VBox/com/string.h>

#include <VBox/vmm/ssm.h>
#include <VBox/vmm/vmmr3vtable.h>
#include <VBox/vmm/vmapi.h>

#include "SecretKeyStore.h"
#ifdef VBOX_COM_INPROC
# include "ConsoleImpl.h"
#else
# include "MachineImpl.h"
# include "VirtualBoxImpl.h"
#endif


/**
 * Class handling encrypted and non encrypted SSM files.
 */
class SsmStream
{
    public:
#ifdef VBOX_COM_INPROC
        SsmStream(Console *pParent, PCVMMR3VTABLE pVMM, SecretKeyStore *pKeyStore, const Utf8Str &strKeyId, const Utf8Str &strKeyStore);
#else
        SsmStream(VirtualBox *pParent, SecretKeyStore *pKeyStore, const Utf8Str &strKeyId, const Utf8Str &strKeyStore);
#endif
        ~SsmStream();

        /**
         * Actually opens the stream for either reading or writing.
         *
         * @returns VBox status code.
         * @param   strFilename The filename of the saved state to open or create.
         * @param   fWrite      Flag whether the stream should be opened for writing (true) or readonly (false).
         * @param   ppSsmHandle Where to store the SSM handle on success, don't call SSMR3Close() but the provided close() method.
         */
        int open(const Utf8Str &strFilename, bool fWrite, PSSMHANDLE *ppSsmHandle);

        /**
         * Opens the saved state file for reading, doesn't call SSMR3Open().
         *
         * @returns VBox status code.
         * @param   strFilename The filename of the saved state to open.
         */
        int open(const Utf8Str &strFilename);

        /**
         * Creates a new saved state file under the given path.
         *
         * @returns VBox status code.
         * @param   strFilename The filename of the saved state to create.
         */
        int create(const Utf8Str &strFilename);

        /**
         * Returns the pointer to the stream operations table after a succesful opening/creation.
         *
         * @return VBox status code.
         * @param  ppStrmOps      Where to store the pointer to the stream operations table on success.
         * @param  ppvStrmOpsUser Where to store the pointer to the opaque user data on success.
         */
        int querySsmStrmOps(PCSSMSTRMOPS *ppStrmOps, void **ppvStrmOpsUser);

        /**
         * Closes an previously opened stream.
         *
         * @returns VBox status code.
         */
        int close(void);

    private:

        static DECLCALLBACK(int) i_ssmCryptoWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite);
        static DECLCALLBACK(int) i_ssmCryptoRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead);
        static DECLCALLBACK(int) i_ssmCryptoSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual);
        static DECLCALLBACK(uint64_t) i_ssmCryptoTell(void *pvUser);
        static DECLCALLBACK(int) i_ssmCryptoSize(void *pvUser, uint64_t *pcb);
        static DECLCALLBACK(int) i_ssmCryptoIsOk(void *pvUser);
        static DECLCALLBACK(int) i_ssmCryptoClose(void *pvUser, bool fCancelled);

#ifdef VBOX_COM_INPROC
        Console        *m_pParent;
        PCVMMR3VTABLE  m_pVMM;
#else
        VirtualBox     *m_pParent;
#endif
        /** The key store for getting at passwords. */
        SecretKeyStore *m_pKeyStore;
        /** The key ID holding the password, empty if the saved state is not encrypted. */
        Utf8Str        m_strKeyId;
        /** The keystore holding the encrypted DEK. */
        Utf8Str        m_strKeyStore;
        /** The VFS file handle. */
        RTVFSFILE      m_hVfsFile;
        /** The SSM handle when opened. */
        PSSMHANDLE     m_pSsm;
        /** The SSM stream callbacks table. */
        SSMSTRMOPS     m_StrmOps;
        /** The cryptographic interfacer. */
        PCVBOXCRYPTOIF m_pCryptoIf;
};

#endif /* !MAIN_INCLUDED_CryptoUtils_h */