2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* 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;
|
2019-07-22 16:39:27 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2019-07-22 16:39:27 +02:00
|
|
|
|
using XenCenterLib;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenCenterLib.Archive;
|
2019-11-25 17:07:58 +01:00
|
|
|
|
using XenOvf.Definitions;
|
|
|
|
|
using XenOvf.Utilities;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenOvf
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides the properties of a digest for a file in the manifest of an OVF/OVA appliance.
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public class FileDigest
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance from a line in the manifest.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="line"></param>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public FileDigest(string line)
|
|
|
|
|
{
|
2019-06-03 19:04:52 +02:00
|
|
|
|
var fields = line.Split('(', ')', '=');
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Expect the first field to be the security algorithm used to compute the hash of the file.
|
2019-06-03 19:04:52 +02:00
|
|
|
|
AlgorithmName = fields[0].Trim();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Expect the second field to be the name of the file.
|
|
|
|
|
Match nameMatch = new Regex(@"[\(](.*)[\)][=]").Match(line);
|
2019-06-03 19:04:52 +02:00
|
|
|
|
Name = nameMatch.Success ? nameMatch.Groups[1].Value.Trim() : fields[1].Trim();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Expect the last field to be the digest of the file.
|
2019-06-03 19:04:52 +02:00
|
|
|
|
DigestAsString = fields[fields.Length - 1].Trim();
|
2019-07-22 16:39:27 +02:00
|
|
|
|
Digest = ToArray(DigestAsString);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 19:04:52 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Name of the file with the digest.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name { get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-06-03 19:04:52 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Name of the algorithm to compute the digest. It must be recognized by HashAlgorithm.Create().
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AlgorithmName { get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-06-03 19:04:52 +02:00
|
|
|
|
public string DigestAsString { get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public byte[] Digest{ get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert a hex string to a binary array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static byte[] ToArray(string HexString)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (HexString.Length % 2 > 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// OVF package, i.e. appliance in the form of a folder
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
internal class FolderPackage : Package
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
private string _Folder;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public FolderPackage(string path)
|
2019-11-25 17:07:58 +01:00
|
|
|
|
: base(path)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
_Folder = path;
|
|
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (string.Compare(extension, Properties.Settings.Default.ovfFileExtension, true) == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
_Folder = Path.GetDirectoryName(path);
|
|
|
|
|
|
|
|
|
|
if (_Folder == null)
|
|
|
|
|
{
|
|
|
|
|
// Handle a package in the root or current folder.
|
|
|
|
|
_Folder = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public override string DescriptorFileName => Path.GetFileName(PackageSourceFile);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
protected override byte[] ReadAllBytes(string fileName)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
return File.ReadAllBytes(Path.Combine(_Folder, fileName));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
return null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string ReadAllText(string fileName)
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return File.ReadAllText(Path.Combine(_Folder, fileName));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)))
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (!StreamUtilities.VerifyAgainstDigest(stream, stream.Length, fileDigest.AlgorithmName, fileDigest.Digest))
|
|
|
|
|
throw new Exception(string.Format(Messages.SECURITY_SIGNATURE_FAILED, fileDigest.Name));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-25 17:07:58 +01:00
|
|
|
|
|
|
|
|
|
public override bool HasFile(string fileName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return File.Exists(Path.Combine(_Folder, fileName));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 01:44:01 +01:00
|
|
|
|
public override void ExtractToWorkingDir()
|
2019-11-25 17:07:58 +01:00
|
|
|
|
{
|
2020-01-25 01:44:01 +01:00
|
|
|
|
WorkingDir = Path.GetDirectoryName(PackageSourceFile);
|
2019-11-25 17:07:58 +01:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// OVA package (may additionally be compressed), i.e. Appliance in the form of a tape archive (tar).
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class TarPackage : Package
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
private readonly Dictionary<string, byte[]> _DirectoryCache = new Dictionary<string, byte[]>();
|
|
|
|
|
private string _DescriptorFileName;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private Stream _tarStream;
|
2019-07-22 16:39:27 +02:00
|
|
|
|
private ArchiveIterator _archiveIterator;
|
|
|
|
|
private readonly ArchiveFactory.Type _tarType = ArchiveFactory.Type.Tar;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public TarPackage(string path)
|
2019-11-25 17:07:58 +01:00
|
|
|
|
: base(path)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
var extension = Path.GetExtension(PackageSourceFile);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (string.Compare(extension, ".gz", true) == 0)
|
|
|
|
|
_tarType = ArchiveFactory.Type.TarGz;
|
|
|
|
|
else if (string.Compare(extension, ".bz2", true) == 0)
|
|
|
|
|
_tarType = ArchiveFactory.Type.TarBz2;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string DescriptorFileName
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
get
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (_DescriptorFileName == null)
|
|
|
|
|
Load();
|
|
|
|
|
return _DescriptorFileName;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override byte[] ReadAllBytes(string fileName)
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (!_DirectoryCache.ContainsKey(fileName))
|
|
|
|
|
Load();
|
|
|
|
|
return _DirectoryCache.TryGetValue(fileName, out byte[] buffer) ? buffer : null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string ReadAllText(string fileName)
|
|
|
|
|
{
|
|
|
|
|
var buffer = ReadAllBytes(fileName);
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (buffer == null)
|
|
|
|
|
return null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(buffer))
|
|
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
|
return reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <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>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private void Load()
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
try
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
Open();
|
|
|
|
|
|
|
|
|
|
while (_archiveIterator.HasNext())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
var extension = Path.GetExtension(_archiveIterator.CurrentFileName());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (_DescriptorFileName == null && string.Compare(extension, Properties.Settings.Default.ovfFileExtension, true) == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
_DescriptorFileName = _archiveIterator.CurrentFileName();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2019-07-22 16:39:27 +02:00
|
|
|
|
else if (string.Compare(extension, Properties.Settings.Default.manifestFileExtension, true) == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (_DescriptorFileName == null || string.Compare(_archiveIterator.CurrentFileName(), ManifestFileName, true) != 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-07-22 16:39:27 +02:00
|
|
|
|
else if (string.Compare(extension, Properties.Settings.Default.certificateFileExtension, true) == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (_DescriptorFileName == null || string.Compare(_archiveIterator.CurrentFileName(), CertificateFileName, true) != 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-11-25 17:07:58 +01:00
|
|
|
|
//add other files to the directory cache without, obviously, loading them
|
|
|
|
|
_DirectoryCache[_archiveIterator.CurrentFileName()] = null;
|
2019-07-22 16:39:27 +02:00
|
|
|
|
continue;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
_DirectoryCache[_archiveIterator.CurrentFileName()] = ReadAllBytes();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-22 16:39:27 +02:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <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);
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Normally used in conjunction with Open in a try-finally block
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Close()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
_archiveIterator?.Dispose();
|
|
|
|
|
_tarStream?.Dispose();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-01-25 01:44:01 +01:00
|
|
|
|
public override void CleanUpWorkingDir()
|
2019-11-25 17:07:58 +01:00
|
|
|
|
{
|
2020-01-25 01:44:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(WorkingDir, true);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
//ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ExtractToWorkingDir()
|
|
|
|
|
{
|
|
|
|
|
WorkingDir = OVF.ExtractArchive(PackageSourceFile);
|
2019-11-25 17:07:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public override void VerifyManifest()
|
|
|
|
|
{
|
|
|
|
|
var manifest = new List<FileDigest>(Manifest);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
try
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
Open();
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// For a tar package, it is more efficient to iterate the files in the order within the archive.
|
2019-07-22 16:39:27 +02:00
|
|
|
|
while (_archiveIterator.HasNext())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
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)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
var fileDigest = manifest.Find(fd => string.Compare(fd.Name, _archiveIterator.CurrentFileName(), true) == 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (fileDigest == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
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);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
manifest.Remove(fileDigest);
|
|
|
|
|
if (!_archiveIterator.VerifyCurrentFileAgainstDigest(fileDigest.AlgorithmName, fileDigest.Digest))
|
|
|
|
|
throw new Exception(string.Format(Messages.SECURITY_SIGNATURE_FAILED, fileDigest.Name));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-22 16:39:27 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:07:58 +01:00
|
|
|
|
public override bool HasFile(string fileName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return DescriptorFileName != null && _DirectoryCache.ContainsKey(fileName);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
private byte[] ReadAllBytes()
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
_archiveIterator.ExtractCurrentFile(ms);
|
|
|
|
|
return ms.Length > 0 ? ms.ToArray() : null;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Abstract class to describe an OVF/OVA appliance.
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public abstract class Package
|
|
|
|
|
{
|
2014-01-06 13:31:19 +01:00
|
|
|
|
protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
// Cache these properties because they are expensive to get
|
2019-11-25 17:07:58 +01:00
|
|
|
|
private string _descriptorXml;
|
2019-07-22 16:39:27 +02:00
|
|
|
|
private byte[] _RawManifest;
|
|
|
|
|
private byte[] _RawCertificate;
|
2019-11-25 17:07:58 +01:00
|
|
|
|
private EnvelopeType _ovfEnvelope;
|
|
|
|
|
|
|
|
|
|
protected Package(string path)
|
|
|
|
|
{
|
|
|
|
|
PackageSourceFile = path;
|
|
|
|
|
}
|
2019-07-22 16:39:27 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public static Package Create(string path)
|
|
|
|
|
{
|
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (string.Compare(extension, Properties.Settings.Default.ovfFileExtension, true) == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return new FolderPackage(path);
|
|
|
|
|
|
|
|
|
|
// Assume it is an archive.
|
|
|
|
|
return new TarPackage(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// According to the OVF specification, name of the *package* is base name of the descriptor file.
|
|
|
|
|
/// </summary>
|
2019-11-25 17:07:58 +01:00
|
|
|
|
public string Name => Path.GetFileNameWithoutExtension(DescriptorFileName);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <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;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <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;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contents of the OVF file.
|
|
|
|
|
/// </summary>
|
2019-11-25 17:07:58 +01:00
|
|
|
|
public string DescriptorXml
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-11-25 17:07:58 +01:00
|
|
|
|
if (_descriptorXml == null)
|
|
|
|
|
_descriptorXml = ReadAllText(DescriptorFileName);
|
|
|
|
|
return _descriptorXml;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
protected List<FileDigest> Manifest
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
private byte[] RawManifest
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (_RawManifest == null)
|
|
|
|
|
_RawManifest = ReadAllBytes(ManifestFileName);
|
|
|
|
|
return _RawManifest;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public byte[] RawCertificate
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (_RawCertificate == null)
|
|
|
|
|
_RawCertificate = ReadAllBytes(CertificateFileName);
|
|
|
|
|
return _RawCertificate;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:07:58 +01:00
|
|
|
|
public string PackageSourceFile { get; }
|
|
|
|
|
|
2020-01-25 01:44:01 +01:00
|
|
|
|
public string WorkingDir { get; protected set; }
|
|
|
|
|
|
2019-11-25 17:07:58 +01:00
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2019-11-25 17:07:58 +01:00
|
|
|
|
public bool HasEncryption()
|
|
|
|
|
{
|
|
|
|
|
return OVF.HasEncryption(OvfEnvelope);
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public bool HasManifest()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
return RawManifest != null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public bool HasSignature()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
return RawCertificate != null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
public void VerifySignature()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
using (var certificate = new X509Certificate2(RawCertificate))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-07-22 16:39:27 +02:00
|
|
|
|
if (!certificate.Verify())
|
|
|
|
|
throw new Exception(Messages.CERTIFICATE_IS_INVALID);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
// 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;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
using (Stream stream = new MemoryStream(RawCertificate))
|
|
|
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
|
|
|
{
|
|
|
|
|
fileDigest = new FileDigest(reader.ReadLine());
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
// 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));
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 01:44:01 +01:00
|
|
|
|
public virtual void CleanUpWorkingDir()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 16:39:27 +02:00
|
|
|
|
#region Abstract members
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The full name of the OVF file within the package
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract string DescriptorFileName { get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected abstract byte[] ReadAllBytes(string fileName);
|
|
|
|
|
|
|
|
|
|
protected abstract string ReadAllText(string fileName);
|
|
|
|
|
|
|
|
|
|
public abstract void VerifyManifest();
|
|
|
|
|
|
2019-11-25 17:07:58 +01:00
|
|
|
|
public abstract bool HasFile(string fileName);
|
|
|
|
|
|
|
|
|
|
/// <returns>The directory with the extracted files</returns>
|
2020-01-25 01:44:01 +01:00
|
|
|
|
public abstract void ExtractToWorkingDir();
|
2019-11-25 17:07:58 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|