VirtualBox

source: vbox/trunk/src/VBox/Main/include/ApplianceImplPrivate.h@ 59679

Last change on this file since 59679 was 59679, checked in by vboxsync, 9 years ago

ApplianceImpl: Signature and certificate validation updates.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: ApplianceImplPrivate.h 59679 2016-02-15 13:10:06Z vboxsync $ */
2/** @file
3 * VirtualBox Appliance private data definitions
4 */
5
6/*
7 * Copyright (C) 2006-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
18#ifndef ____H_APPLIANCEIMPLPRIVATE
19#define ____H_APPLIANCEIMPLPRIVATE
20
21class VirtualSystemDescription;
22
23#include "ovfreader.h"
24#include "SecretKeyStore.h"
25#include "ThreadTask.h"
26#include <map>
27#include <vector>
28#include <iprt/manifest.h>
29#include <iprt/vfs.h>
30#include <iprt/crypto/x509.h>
31
32////////////////////////////////////////////////////////////////////////////////
33//
34// Appliance data definition
35//
36////////////////////////////////////////////////////////////////////////////////
37
38typedef std::pair<Utf8Str, Utf8Str> STRPAIR;
39
40typedef std::vector<com::Guid> GUIDVEC;
41
42/* Describe a location for the import/export. The location could be a file on a
43 * local hard disk or a remote target based on the supported inet protocols. */
44struct LocationInfo
45{
46 LocationInfo()
47 : storageType(VFSType_File) {}
48 VFSType_T storageType; /* Which type of storage should be handled */
49 Utf8Str strPath; /* File path for the import/export */
50 Utf8Str strHostname; /* Hostname on remote storage locations (could be empty) */
51 Utf8Str strUsername; /* Username on remote storage locations (could be empty) */
52 Utf8Str strPassword; /* Password on remote storage locations (could be empty) */
53};
54
55// opaque private instance data of Appliance class
56struct Appliance::Data
57{
58 enum ApplianceState { ApplianceIdle, ApplianceImporting, ApplianceExporting };
59 enum digest_T {SHA1, SHA256};
60
61 Data()
62 : state(ApplianceIdle)
63 , fDigestTypes(0)
64 , hOurManifest(NIL_RTMANIFEST)
65 , fManifest(true)
66 , fSha256(false)
67 , fDeterminedDigestTypes(false)
68 , hTheirManifest(NIL_RTMANIFEST)
69 , hMemFileTheirManifest(NIL_RTVFSFILE)
70 , fSignerCertLoaded(false)
71 , fCertificateIsSelfSigned(false)
72 , fSignatureValid(false)
73 , fCertificateValid(false)
74 , fCertificateMissingPath(true)
75 , fCertificateValidTime(false)
76 , pbSignedDigest(NULL)
77 , cbSignedDigest(0)
78 , enmSignedDigestType(RTDIGESTTYPE_INVALID)
79 , fExportISOImages(false)
80 , pReader(NULL)
81 , ulWeightForXmlOperation(0)
82 , ulWeightForManifestOperation(0)
83 , ulTotalDisksMB(0)
84 , cDisks(0)
85 , m_cPwProvided(0)
86 {
87 }
88
89 ~Data()
90 {
91 if (pReader)
92 {
93 delete pReader;
94 pReader = NULL;
95 }
96 resetReadData();
97 }
98
99 /**
100 * Resets data used by read.
101 */
102 void resetReadData(void)
103 {
104 strOvfManifestEntry.setNull();
105 if (hOurManifest != NIL_RTMANIFEST)
106 {
107 RTManifestRelease(hOurManifest);
108 hOurManifest = NIL_RTMANIFEST;
109 }
110 if (hTheirManifest != NIL_RTMANIFEST)
111 {
112 RTManifestRelease(hTheirManifest);
113 hTheirManifest = NIL_RTMANIFEST;
114 }
115 if (hMemFileTheirManifest)
116 {
117 RTVfsFileRelease(hMemFileTheirManifest);
118 hMemFileTheirManifest = NIL_RTVFSFILE;
119 }
120 if (pbSignedDigest)
121 {
122 RTMemFree(pbSignedDigest);
123 pbSignedDigest = NULL;
124 cbSignedDigest = 0;
125 }
126 if (fSignerCertLoaded)
127 {
128 RTCrX509Certificate_Delete(&SignerCert);
129 fSignerCertLoaded = false;
130 }
131 enmSignedDigestType = RTDIGESTTYPE_INVALID;
132 fCertificateIsSelfSigned = false;
133 fSignatureValid = false;
134 fCertificateValid = false;
135 fCertificateMissingPath = true;
136 fCertificateValidTime = false;
137 fDeterminedDigestTypes = false;
138 fDigestTypes = RTMANIFEST_ATTR_SHA1 | RTMANIFEST_ATTR_SHA256 | RTMANIFEST_ATTR_SHA512;
139 strCertError.setNull();
140 }
141
142 ApplianceState state;
143
144 LocationInfo locInfo; // location info for the currently processed OVF
145 /** The digests types to calculate (RTMANIFEST_ATTR_XXX) for the manifest.
146 * This will be a single value when exporting. Zero, one or two. */
147 uint32_t fDigestTypes;
148 /** Manifest created while importing or exporting. */
149 RTMANIFEST hOurManifest;
150
151 /** @name Write data
152 * @{ */
153 bool fManifest; // Create a manifest file on export
154 bool fSha256; // true = SHA256 (OVF 2.0), false = SHA1 (OVF 1.0)
155 /** @} */
156
157 /** @name Read data
158 * @{ */
159 /** The manifest entry name of the OVF-file. */
160 Utf8Str strOvfManifestEntry;
161
162 /** Set if we've parsed the manifest and determined the digest types. */
163 bool fDeterminedDigestTypes;
164
165 /** Manifest read in during read() and kept around for later verification. */
166 RTMANIFEST hTheirManifest;
167 /** Memorized copy of the manifest file for signature checking purposes. */
168 RTVFSFILE hMemFileTheirManifest;
169
170 /** The signer certificate from the signature fiel (.cert).
171 * This will be used in the future provide information about the signer via
172 * the API. */
173 RTCRX509CERTIFICATE SignerCert;
174 /** Set if the SignerCert member contains usable data. */
175 bool fSignerCertLoaded;
176 /** Cached RTCrX509Validity_IsValidAtTimeSpec result set by read(). */
177 bool fCertificateIsSelfSigned;
178 /** Set by read() if pbSignedDigest verified correctly against SignerCert. */
179 bool fSignatureValid;
180 /** Set by read() when the SignerCert checked out fine. */
181 bool fCertificateValid;
182 /** Set by read() when the SignerCert certificate path couldn't be built. */
183 bool fCertificateMissingPath;
184 /** Set by read() when the SignerCert (+path) is valid in the temporal sense. */
185 bool fCertificateValidTime;
186 /** For keeping certificate error messages we delay from read() to import(). */
187 Utf8Str strCertError;
188 /** The signed digest of the manifest. */
189 uint8_t *pbSignedDigest;
190 /** The size of the signed digest. */
191 size_t cbSignedDigest;
192 /** The digest type used to sign the manifest. */
193 RTDIGESTTYPE enmSignedDigestType;
194 /** @} */
195
196 bool fExportISOImages;// when 1 the ISO images are exported
197
198 RTCList<ImportOptions_T> optListImport;
199 RTCList<ExportOptions_T> optListExport;
200
201 ovf::OVFReader *pReader;
202
203 std::list< ComObjPtr<VirtualSystemDescription> >
204 virtualSystemDescriptions;
205
206 std::list<Utf8Str> llWarnings;
207
208 ULONG ulWeightForXmlOperation;
209 ULONG ulWeightForManifestOperation;
210 ULONG ulTotalDisksMB;
211 ULONG cDisks;
212
213 std::list<Guid> llGuidsMachinesCreated;
214
215 /** Sequence of password identifiers to encrypt disk images during export. */
216 std::vector<com::Utf8Str> m_vecPasswordIdentifiers;
217 /** Map to get all medium identifiers assoicated with a given password identifier. */
218 std::map<com::Utf8Str, GUIDVEC> m_mapPwIdToMediumIds;
219 /** Secret key store used to hold the passwords during export. */
220 SecretKeyStore *m_pSecretKeyStore;
221 /** Number of passwords provided. */
222 uint32_t m_cPwProvided;
223};
224
225struct Appliance::XMLStack
226{
227 std::map<Utf8Str, const VirtualSystemDescriptionEntry*> mapDisks;
228 std::map<Utf8Str, bool> mapNetworks;
229};
230
231class Appliance::TaskOVF: public ThreadTask
232{
233public:
234 enum TaskType
235 {
236 Read,
237 Import,
238 Write
239 };
240
241 TaskOVF(Appliance *aThat,
242 TaskType aType,
243 LocationInfo aLocInfo,
244 ComObjPtr<Progress> &aProgress)
245 : ThreadTask("TaskOVF"),
246 pAppliance(aThat),
247 taskType(aType),
248 locInfo(aLocInfo),
249 pProgress(aProgress),
250 enFormat(ovf::OVFVersion_unknown),
251 rc(S_OK)
252 {
253 switch (taskType)
254 {
255 case TaskOVF::Read: m_strTaskName = "ApplRead"; break;
256 case TaskOVF::Import: m_strTaskName = "ApplImp"; break;
257 case TaskOVF::Write: m_strTaskName = "ApplWrit"; break;
258 default: m_strTaskName = "ApplTask"; break;
259 }
260 }
261
262 static DECLCALLBACK(int) updateProgress(unsigned uPercent, void *pvUser);
263
264 Appliance *pAppliance;
265 TaskType taskType;
266 const LocationInfo locInfo;
267 ComObjPtr<Progress> pProgress;
268
269 ovf::OVFVersion_T enFormat;
270
271 HRESULT rc;
272
273 void handler()
274 {
275 int vrc = Appliance::i_taskThreadImportOrExport(NULL, this); NOREF(vrc);
276 }
277};
278
279struct MyHardDiskAttachment
280{
281 ComPtr<IMachine> pMachine;
282 Bstr controllerType;
283 int32_t lControllerPort; // 0-29 for SATA
284 int32_t lDevice; // IDE: 0 or 1, otherwise 0 always
285};
286
287/**
288 * Used by Appliance::importMachineGeneric() to store
289 * input parameters and rollback information.
290 */
291struct Appliance::ImportStack
292{
293 // input pointers
294 const LocationInfo &locInfo; // ptr to location info from Appliance::importFS()
295 Utf8Str strSourceDir; // directory where source files reside
296 const ovf::DiskImagesMap &mapDisks; // ptr to disks map in OVF
297 ComObjPtr<Progress> &pProgress; // progress object passed into Appliance::importFS()
298
299 // input parameters from VirtualSystemDescriptions
300 Utf8Str strNameVBox; // VM name
301 Utf8Str strMachineFolder; // FQ host folder where the VirtualBox machine would be created
302 Utf8Str strOsTypeVBox; // VirtualBox guest OS type as string
303 Utf8Str strDescription;
304 uint32_t cCPUs; // CPU count
305 bool fForceHWVirt; // if true, we force enabling hardware virtualization
306 bool fForceIOAPIC; // if true, we force enabling the IOAPIC
307 uint32_t ulMemorySizeMB; // virtual machine RAM in megabytes
308#ifdef VBOX_WITH_USB
309 bool fUSBEnabled;
310#endif
311 Utf8Str strAudioAdapter; // if not empty, then the guest has audio enabled, and this is the decimal
312 // representation of the audio adapter (should always be "0" for AC97 presently)
313
314 // session (not initially created)
315 ComPtr<ISession> pSession; // session opened in Appliance::importFS() for machine manipulation
316 bool fSessionOpen; // true if the pSession is currently open and needs closing
317
318 /** @name File access related stuff (TAR stream)
319 * @{ */
320 /** OVA file system stream handle. NIL if not OVA. */
321 RTVFSFSSTREAM hVfsFssOva;
322 /** OVA lookahead I/O stream object. */
323 RTVFSIOSTREAM hVfsIosOvaLookAhead;
324 /** OVA lookahead I/O stream object name. */
325 char *pszOvaLookAheadName;
326 /** @} */
327
328 // a list of images that we created/imported; this is initially empty
329 // and will be cleaned up on errors
330 std::list<MyHardDiskAttachment> llHardDiskAttachments; // disks that were attached
331 std::map<Utf8Str , Utf8Str> mapNewUUIDsToOriginalUUIDs;
332
333 ImportStack(const LocationInfo &aLocInfo,
334 const ovf::DiskImagesMap &aMapDisks,
335 ComObjPtr<Progress> &aProgress,
336 RTVFSFSSTREAM aVfsFssOva)
337 : locInfo(aLocInfo),
338 mapDisks(aMapDisks),
339 pProgress(aProgress),
340 cCPUs(1),
341 fForceHWVirt(false),
342 fForceIOAPIC(false),
343 ulMemorySizeMB(0),
344 fSessionOpen(false),
345 hVfsFssOva(aVfsFssOva),
346 hVfsIosOvaLookAhead(NIL_RTVFSIOSTREAM),
347 pszOvaLookAheadName(NULL)
348 {
349 if (hVfsFssOva != NIL_RTVFSFSSTREAM)
350 RTVfsFsStrmRetain(hVfsFssOva);
351
352 // disk images have to be on the same place as the OVF file. So
353 // strip the filename out of the full file path
354 strSourceDir = aLocInfo.strPath;
355 strSourceDir.stripFilename();
356 }
357
358 ~ImportStack()
359 {
360 if (hVfsFssOva != NIL_RTVFSFSSTREAM)
361 {
362 RTVfsFsStrmRelease(hVfsFssOva);
363 hVfsFssOva = NIL_RTVFSFSSTREAM;
364 }
365 if (hVfsIosOvaLookAhead != NIL_RTVFSIOSTREAM)
366 {
367 RTVfsIoStrmRelease(hVfsIosOvaLookAhead);
368 hVfsIosOvaLookAhead = NIL_RTVFSIOSTREAM;
369 }
370 if (pszOvaLookAheadName)
371 {
372 RTStrFree(pszOvaLookAheadName);
373 pszOvaLookAheadName = NULL;
374 }
375 }
376
377 HRESULT restoreOriginalUUIDOfAttachedDevice(settings::MachineConfigFile *config);
378 HRESULT saveOriginalUUIDOfAttachedDevice(settings::AttachedDevice &device,
379 const Utf8Str &newlyUuid);
380 RTVFSIOSTREAM claimOvaLookAHead(void);
381
382};
383
384////////////////////////////////////////////////////////////////////////////////
385//
386// VirtualSystemDescription data definition
387//
388////////////////////////////////////////////////////////////////////////////////
389
390struct VirtualSystemDescription::Data
391{
392 std::vector<VirtualSystemDescriptionEntry>
393 maDescriptions; // item descriptions
394
395 ComPtr<Machine> pMachine; // VirtualBox machine this description was exported from (export only)
396
397 settings::MachineConfigFile
398 *pConfig; // machine config created from <vbox:Machine> element if found (import only)
399};
400
401////////////////////////////////////////////////////////////////////////////////
402//
403// Internal helpers
404//
405////////////////////////////////////////////////////////////////////////////////
406
407void convertCIMOSType2VBoxOSType(Utf8Str &strType, ovf::CIMOSType_T c, const Utf8Str &cStr);
408
409ovf::CIMOSType_T convertVBoxOSType2CIMOSType(const char *pcszVBox, BOOL fLongMode);
410
411Utf8Str convertNetworkAttachmentTypeToString(NetworkAttachmentType_T type);
412
413
414typedef struct SHASTORAGE
415{
416 PVDINTERFACE pVDImageIfaces;
417 bool fCreateDigest;
418 bool fSha256; /* false = SHA1 (OVF 1.x), true = SHA256 (OVF 2.0) */
419 Utf8Str strDigest;
420} SHASTORAGE, *PSHASTORAGE;
421
422PVDINTERFACEIO ShaCreateInterface();
423PVDINTERFACEIO FileCreateInterface();
424PVDINTERFACEIO tarWriterCreateInterface(void);
425
426int writeBufferToFile(const char *pcszFilename, void *pvBuf, size_t cbSize, PVDINTERFACEIO pIfIo, void *pvUser);
427
428#endif // !____H_APPLIANCEIMPLPRIVATE
429
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette