forked from xenserver/xenadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage.cs
594 lines (505 loc) · 20.3 KB
/
Package.cs
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using XenCenterLib;
using XenCenterLib.Archive;
using XenOvf.Definitions;
using XenOvf.Utilities;
namespace XenOvf
{
/// <summary>
/// Provides the properties of a digest for a file in the manifest of an OVF/OVA appliance.
/// </summary>
public class FileDigest
{
/// <summary>
/// Creates a new instance from a line in the manifest.
/// </summary>
/// <param name="line"></param>
public FileDigest(string line)
{
var fields = line.Split('(', ')', '=');
// Expect the first field to be the security algorithm used to compute the hash of the file.
AlgorithmName = fields[0].Trim();
// Expect the second field to be the name of the file.
Match nameMatch = new Regex(@"[\(](.*)[\)][=]").Match(line);
Name = nameMatch.Success ? nameMatch.Groups[1].Value.Trim() : fields[1].Trim();
// Expect the last field to be the digest of the file.
DigestAsString = fields[fields.Length - 1].Trim();
Digest = ToArray(DigestAsString);
}
/// <summary>
/// Name of the file with the digest.
/// </summary>
public string Name { get; }
/// <summary>
/// Name of the algorithm to compute the digest. It must be recognized by HashAlgorithm.Create().
/// </summary>
public string AlgorithmName { get; }
public string DigestAsString { get; }
public byte[] Digest{ get; }
/// <summary>
/// Convert a hex string to a binary array.
/// </summary>
private static byte[] ToArray(string HexString)
{
if (HexString.Length % 2 > 0)
{
// The length is odd.
// Pad the hex string on the left with a space to interpret as a leading zero.
HexString = HexString.PadLeft(HexString.Length + 1);
}
var array = new byte[HexString.Length / 2];
try
{
for (int i = 0; i < HexString.Length; i += 2)
{
array[i / 2] = byte.Parse(HexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
}
}
catch
{
throw new Exception(HexString + " contains an invalid hexadecimal number.");
}
return array;
}
}
/// <summary>
/// OVF package, i.e. appliance in the form of a folder
/// </summary>
internal class FolderPackage : Package
{
private string _Folder;
public FolderPackage(string path)
: base(path)
{
_Folder = path;
var extension = Path.GetExtension(path);
if (string.Compare(extension, Properties.Settings.Default.ovfFileExtension, true) == 0)
_Folder = Path.GetDirectoryName(path);
if (_Folder == null)
{
// Handle a package in the root or current folder.
_Folder = "";
}
}
public override string DescriptorFileName => Path.GetFileName(PackageSourceFile);
protected override byte[] ReadAllBytes(string fileName)
{
try
{
return File.ReadAllBytes(Path.Combine(_Folder, fileName));
}
catch
{
return null;
}
}
protected override string ReadAllText(string fileName)
{
try
{
return File.ReadAllText(Path.Combine(_Folder, fileName));
}
catch
{
return null;
}
}
public override void VerifyManifest()
{
// Verify the presence of a manifest.
var manifest = Manifest;
// For a folder package, it is efficient to iterate by the order of files in the manifest.
foreach (FileDigest fileDigest in manifest)
{
using (var stream = File.OpenRead(Path.Combine(_Folder, fileDigest.Name)))
{
if (!StreamUtilities.VerifyAgainstDigest(stream, stream.Length, fileDigest.AlgorithmName, fileDigest.Digest))
throw new Exception(string.Format(Messages.SECURITY_SIGNATURE_FAILED, fileDigest.Name));
}
}
}
public override bool HasFile(string fileName)
{
try
{
return File.Exists(Path.Combine(_Folder, fileName));
}
catch
{
return false;
}
}
public override void ExtractToWorkingDir()
{
WorkingDir = Path.GetDirectoryName(PackageSourceFile);
}
}
/// <summary>
/// OVA package (may additionally be compressed), i.e. Appliance in the form of a tape archive (tar).
/// </summary>
internal class TarPackage : Package
{
private readonly Dictionary<string, byte[]> _DirectoryCache = new Dictionary<string, byte[]>();
private string _DescriptorFileName;
private Stream _tarStream;
private ArchiveIterator _archiveIterator;
private readonly ArchiveFactory.Type _tarType = ArchiveFactory.Type.Tar;
public TarPackage(string path)
: base(path)
{
var extension = Path.GetExtension(PackageSourceFile);
if (string.Compare(extension, ".gz", true) == 0)
_tarType = ArchiveFactory.Type.TarGz;
else if (string.Compare(extension, ".bz2", true) == 0)
_tarType = ArchiveFactory.Type.TarBz2;
}
public override string DescriptorFileName
{
get
{
if (_DescriptorFileName == null)
Load();
return _DescriptorFileName;
}
}
protected override byte[] ReadAllBytes(string fileName)
{
if (!_DirectoryCache.ContainsKey(fileName))
Load();
return _DirectoryCache.TryGetValue(fileName, out byte[] buffer) ? buffer : null;
}
protected override string ReadAllText(string fileName)
{
var buffer = ReadAllBytes(fileName);
if (buffer == null)
return null;
using (var stream = new MemoryStream(buffer))
using (var reader = new StreamReader(stream))
return reader.ReadToEnd();
}
/// <summary>
/// Load metadata files from an OVA package. Rules:
/// - Load the first file name with an ovf extension as the package descriptor
/// - Load the first manifest or certificate file that has the same base name as the descriptor
/// - Do not load any other files
/// In most cases the ovf file is first in the package, so all metadata files
/// can be read in one go. Otherwise, the package will have to be searched more times.
/// </summary>
private void Load()
{
try
{
Open();
while (_archiveIterator.HasNext())
{
var extension = Path.GetExtension(_archiveIterator.CurrentFileName());
if (_DescriptorFileName == null && string.Compare(extension, Properties.Settings.Default.ovfFileExtension, true) == 0)
{
_DescriptorFileName = _archiveIterator.CurrentFileName();
}
else if (string.Compare(extension, Properties.Settings.Default.manifestFileExtension, true) == 0)
{
if (_DescriptorFileName == null || string.Compare(_archiveIterator.CurrentFileName(), ManifestFileName, true) != 0)
continue;
}
else if (string.Compare(extension, Properties.Settings.Default.certificateFileExtension, true) == 0)
{
if (_DescriptorFileName == null || string.Compare(_archiveIterator.CurrentFileName(), CertificateFileName, true) != 0)
continue;
}
else
{
//add other files to the directory cache without, obviously, loading them
_DirectoryCache[_archiveIterator.CurrentFileName()] = null;
continue;
}
_DirectoryCache[_archiveIterator.CurrentFileName()] = ReadAllBytes();
}
}
finally
{
Close();
}
}
/// <summary>
/// Always remember to call Close after having used the opened package,
/// usually in a try-finally block
/// </summary>
private void Open()
{
_tarStream = File.OpenRead(PackageSourceFile);
_archiveIterator = ArchiveFactory.Reader(_tarType, _tarStream);
}
/// <summary>
/// Normally used in conjunction with Open in a try-finally block
/// </summary>
private void Close()
{
_archiveIterator?.Dispose();
_tarStream?.Dispose();
}
public override void CleanUpWorkingDir()
{
try
{
Directory.Delete(WorkingDir, true);
}
catch
{
//ignore
}
}
public override void ExtractToWorkingDir()
{
WorkingDir = OVF.ExtractArchive(PackageSourceFile);
}
public override void VerifyManifest()
{
var manifest = new List<FileDigest>(Manifest);
try
{
Open();
// For a tar package, it is more efficient to iterate the files in the order within the archive.
while (_archiveIterator.HasNext())
{
var extension = Path.GetExtension(_archiveIterator.CurrentFileName());
if (string.Compare(extension, Properties.Settings.Default.manifestFileExtension, true) == 0 ||
string.Compare(extension, Properties.Settings.Default.certificateFileExtension, true) == 0)
continue;
var fileDigest = manifest.Find(fd => string.Compare(fd.Name, _archiveIterator.CurrentFileName(), true) == 0);
if (fileDigest == null)
{
log.ErrorFormat("File {0} contained in the appliance is not listed in the manifest.", _archiveIterator.CurrentFileName());
throw new Exception(Messages.SECURITY_FILE_MISSING_FROM_MANIFEST);
}
manifest.Remove(fileDigest);
if (!_archiveIterator.VerifyCurrentFileAgainstDigest(fileDigest.AlgorithmName, fileDigest.Digest))
throw new Exception(string.Format(Messages.SECURITY_SIGNATURE_FAILED, fileDigest.Name));
}
}
finally
{
Close();
}
if (manifest.Count > 0)
{
log.ErrorFormat("The following files are listed in the manifest but missing from the appliance: {0}",
string.Join(", ", manifest.Select(fd => fd.Name)));
throw new Exception(Messages.SECURITY_FILE_MISSING_FROM_PACKAGE);
}
}
public override bool HasFile(string fileName)
{
try
{
return DescriptorFileName != null && _DirectoryCache.ContainsKey(fileName);
}
catch
{
return false;
}
}
private byte[] ReadAllBytes()
{
using (var ms = new MemoryStream())
{
_archiveIterator.ExtractCurrentFile(ms);
return ms.Length > 0 ? ms.ToArray() : null;
}
}
}
/// <summary>
/// Abstract class to describe an OVF/OVA appliance.
/// </summary>
public abstract class Package
{
protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Cache these properties because they are expensive to get
private string _descriptorXml;
private byte[] _RawManifest;
private byte[] _RawCertificate;
private EnvelopeType _ovfEnvelope;
protected Package(string path)
{
PackageSourceFile = path;
}
public static Package Create(string path)
{
var extension = Path.GetExtension(path);
if (string.Compare(extension, Properties.Settings.Default.ovfFileExtension, true) == 0)
return new FolderPackage(path);
// Assume it is an archive.
return new TarPackage(path);
}
#region Properties
/// <summary>
/// According to the OVF specification, name of the *package* is base name of the descriptor file.
/// </summary>
public string Name => Path.GetFileNameWithoutExtension(DescriptorFileName);
/// <summary>
/// According to the OVF specification, base name of the manifest file must be the same as the descriptor file.
/// </summary>
protected string ManifestFileName => Name + Properties.Settings.Default.manifestFileExtension;
/// <summary>
/// According to the OVF specification, base name of the certificate file must be the same as the descriptor file.
/// </summary>
protected string CertificateFileName => Name + Properties.Settings.Default.certificateFileExtension;
/// <summary>
/// Contents of the OVF file.
/// </summary>
public string DescriptorXml
{
get
{
if (_descriptorXml == null)
_descriptorXml = ReadAllText(DescriptorFileName);
return _descriptorXml;
}
}
protected List<FileDigest> Manifest
{
get
{
var manifest = new List<FileDigest>();
using (var stream = new MemoryStream(RawManifest))
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
manifest.Add(new FileDigest(reader.ReadLine()));
}
}
return manifest;
}
}
private byte[] RawManifest
{
get
{
if (_RawManifest == null)
_RawManifest = ReadAllBytes(ManifestFileName);
return _RawManifest;
}
}
public byte[] RawCertificate
{
get
{
if (_RawCertificate == null)
_RawCertificate = ReadAllBytes(CertificateFileName);
return _RawCertificate;
}
}
public string PackageSourceFile { get; }
public string WorkingDir { get; protected set; }
/// <summary>
/// May be null if no valid OVF file has been found
/// </summary>
public EnvelopeType OvfEnvelope
{
get
{
if (_ovfEnvelope == null)
{
try
{
_ovfEnvelope = Tools.DeserializeOvfXml(DescriptorXml);
}
catch (Exception ex)
{
log.Error($"Failed to load OVF content from appliance {PackageSourceFile}", ex);
}
}
return _ovfEnvelope;
}
}
#endregion
public bool HasEncryption()
{
return OVF.HasEncryption(OvfEnvelope);
}
public bool HasManifest()
{
return RawManifest != null;
}
public bool HasSignature()
{
return RawCertificate != null;
}
public void VerifySignature()
{
using (var certificate = new X509Certificate2(RawCertificate))
{
if (!certificate.Verify())
throw new Exception(Messages.CERTIFICATE_IS_INVALID);
// Get the package signature from the certificate file.
// This is the digest of the first file listed in the certificate file,
// hence we only need to read the first line
FileDigest fileDigest;
using (Stream stream = new MemoryStream(RawCertificate))
using (StreamReader reader = new StreamReader(stream))
{
fileDigest = new FileDigest(reader.ReadLine());
}
// Verify the stored signature against the computed signature using the certificate's public key.
// Do this independently to minimize the number of files opened concurrently.
using (Stream stream = new MemoryStream(RawManifest))
{
if (!StreamUtilities.VerifyAgainstDigest(stream, stream.Length, fileDigest.AlgorithmName, fileDigest.Digest, certificate.PublicKey.Key as RSACryptoServiceProvider))
throw new Exception(string.Format(Messages.SECURITY_SIGNATURE_FAILED, fileDigest.Name));
}
}
}
public virtual void CleanUpWorkingDir()
{
}
#region Abstract members
/// <summary>
/// The full name of the OVF file within the package
/// </summary>
public abstract string DescriptorFileName { get; }
protected abstract byte[] ReadAllBytes(string fileName);
protected abstract string ReadAllText(string fileName);
public abstract void VerifyManifest();
public abstract bool HasFile(string fileName);
/// <returns>The directory with the extracted files</returns>
public abstract void ExtractToWorkingDir();
#endregion
}
}