EasySign BETA
Digital Signing Tool
Loading...
Searching...
No Matches
SAPTeam.EasySign.Bundle Class Reference

Represents a bundle that holds file hashes and signatures. More...

Public Member Functions

 Bundle (string bundlePath, ILogger? logger=null, int maxCacheSize=0x8000000)
 Initializes a new instance of the Bundle class.
 
ZipArchive GetZipArchive (ZipArchiveMode mode=ZipArchiveMode.Read)
 Gets a ZipArchive for the bundle.
 
void LoadFromFile (bool readOnly=true)
 Loads the bundle from the file system.
 
void LoadFromBytes (byte[] bundleContent)
 Loads the bundle from a byte array.
 
void AddEntry (string path, string destinationPath="./", string? rootPath=null)
 Adds a file entry to the bundle.
 
void DeleteEntry (string entryName)
 Deletes an entry from the bundle.
 
void Sign (X509Certificate2 certificate, RSA privateKey)
 Signs the bundle with the specified certificate and private key.
 
bool VerifyFile (string entryName)
 Verifies the integrity of a file in the bundle.
 
bool VerifySignature (string certificateHash)
 Verifies the signature of the bundle using the specified certificate hash.
 
bool VerifyCertificate (string certificateHash, out X509ChainStatus[] statuses, X509ChainPolicy? policy=null)
 Verifies the validity of a certificate using the specified certificate hash.
 
bool VerifyCertificate (string certificateHash, X509ChainPolicy? policy=null)
 Verifies the validity of a certificate using the specified certificate hash.
 
bool VerifyCertificate (X509Certificate2 certificate, out X509ChainStatus[] statuses, X509ChainPolicy? policy=null)
 Verifies the validity of a certificate.
 
bool VerifyCertificate (X509Certificate2 certificate, X509ChainPolicy? policy=null)
 Verifies the validity of a certificate.
 
X509Certificate2 GetCertificate (string certificateHash)
 Gets a certificate from the bundle using the specified certificate hash.
 
byte[] GetBytes (string entryName, ReadSource readSource)
 Gets the data of an entry in the bundle as bytes array and caches the entry data if the bundle is Read-only.
 
Stream GetStream (string entryName, ReadSource readSource=ReadSource.Automatic)
 Gets a read-only stream for an entry in the bundle and caches the entry data if the bundle is Read-only.
 
bool Exists (string entryName, ReadSource readSource=ReadSource.Automatic)
 Checks whether an entry exists in the bundle or on the disk.
 
void Update ()
 Writes changes to the bundle file.
 

Protected Member Functions

void EnsureWritable ()
 Throws an exception if the bundle is read-only.
 
bool CheckEntryNameSecurity (string entryName, bool throwException=true)
 Checks whether the entry name is protected and throws an exception if it is.
 
bool CacheEntry (string entryName, byte[] data)
 Caches an entry in memory.
 
virtual void Parse (ZipArchive zip)
 Parses the bundle contents from a ZipArchive.
 
ReadSource GetReadSource (string entryName, ReadSource readSource=ReadSource.Automatic)
 Gets the read source for an entry name.
 
virtual byte[] GetManifestData ()
 Gets the manifest data as a byte array.
 
byte[] Export (object structuredData, JsonSerializerContext jsonSerializerContext)
 Exports the specified structured data to a byte array.
 
byte[] Export (object structuredData)
 Exports the specified structured data to a byte array.
 
void WriteEntry (ZipArchive zip, string entryName, byte[] data)
 Writes an entry to a ZipArchive.
 

Static Protected Member Functions

static byte[] ReadStream (Stream stream)
 Reads a stream into a byte array.
 
static byte[] ComputeSHA512Hash (Stream stream)
 Computes the SHA-512 hash of a stream.
 
static byte[] ComputeSHA512Hash (byte[] data)
 Computes the SHA-512 hash of a byte array.
 

Protected Attributes

readonly JsonSerializerOptions SerializerOptions
 Gets the JSON serializer options.
 

Properties

ILogger Logger [get]
 Gets the logger to use for logging.
 
HashSet< string > ProtectedEntryNames [get]
 Gets the list of sensitive names.
 
virtual string DefaultBundleName [get]
 Gets the default name of the bundle.
 
string RootPath [get]
 Gets the root path of the bundle.
 
string BundleName [get]
 Gets the name of the bundle file.
 
string BundlePath [get]
 Gets the full path of the bundle file.
 
Manifest Manifest = new() [get]
 Gets the manifest of the bundle.
 
Signatures Signatures = new() [get]
 Gets the signatures of the bundle.
 
bool ReadOnly [get]
 Gets a value indicating whether the bundle is read-only.
 
bool LoadedFromMemory [get]
 Gets a value indicating whether the bundle is loaded from memory.
 
bool Loaded [get]
 Gets a value indicating whether the bundle is loaded.
 

Events

Action< ZipArchive >? Updating
 Occurs when the bundle file is being updated.
 

Detailed Description

Represents a bundle that holds file hashes and signatures.

Definition at line 21 of file Bundle.cs.

Constructor & Destructor Documentation

◆ Bundle()

SAPTeam.EasySign.Bundle.Bundle ( string bundlePath,
ILogger? logger = null,
int maxCacheSize = 0x8000000 )

Initializes a new instance of the Bundle class.

Parameters
bundlePathThe path of the bundle.
loggerThe logger to use for logging.
maxCacheSizeThe maximum size of the cache in bytes.

Definition at line 121 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.DefaultBundleName, SAPTeam.EasySign.Bundle.Logger, and SAPTeam.EasySign.Bundle.RootPath.

Member Function Documentation

◆ AddEntry()

void SAPTeam.EasySign.Bundle.AddEntry ( string path,
string destinationPath = "./",
string? rootPath = null )

Adds a file entry to the bundle.

if the Manifest.StoreOriginalFiles is true, the file will be embedded in the bundle and it's hash added to manifest. Otherwise just the file hash added to the bundle.

Parameters
pathThe path of the file to add.
destinationPathThe destination path within the bundle. Ignore when Manifest.StoreOriginalFiles is false
rootPathThe root path for relative paths.

Definition at line 361 of file Bundle.cs.

References SAPTeam.EasySign.Manifest.AddEntry(), SAPTeam.EasySign.Bundle.CheckEntryNameSecurity(), SAPTeam.EasySign.Bundle.ComputeSHA512Hash(), SAPTeam.EasySign.Bundle.EnsureWritable(), SAPTeam.EasySign.Manifest.GetNormalizedEntryName(), SAPTeam.EasySign.Bundle.Logger, SAPTeam.EasySign.Bundle.RootPath, and SAPTeam.EasySign.Manifest.StoreOriginalFiles.

Referenced by SAPTeam.EasySign.CommandLine.CommandProvider< TBundle, TConfiguration >.RunAdd().

◆ CacheEntry()

bool SAPTeam.EasySign.Bundle.CacheEntry ( string entryName,
byte[] data )
protected

Caches an entry in memory.

Parameters
entryNameThe name of the entry to cache.
dataThe data of the entry to cache.
Returns
true if the entry was cached; otherwise, false.

Definition at line 194 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.Logger, and SAPTeam.EasySign.Bundle.ReadOnly.

Referenced by SAPTeam.EasySign.Bundle.GetBytes().

◆ CheckEntryNameSecurity()

bool SAPTeam.EasySign.Bundle.CheckEntryNameSecurity ( string entryName,
bool throwException = true )
protected

Checks whether the entry name is protected and throws an exception if it is.

Parameters
entryNameThe name of the entry to check.
throwExceptionWhether to throw an exception if the entry name is protected.
Exceptions
UnauthorizedAccessException
Returns
True if the entry name is not protected; otherwise, false.

Definition at line 161 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.ProtectedEntryNames.

Referenced by SAPTeam.EasySign.Bundle.AddEntry(), SAPTeam.EasySign.Bundle.DeleteEntry(), and SAPTeam.EasySign.Bundle.GetReadSource().

◆ ComputeSHA512Hash() [1/2]

static byte[] SAPTeam.EasySign.Bundle.ComputeSHA512Hash ( byte[] data)
staticprotected

Computes the SHA-512 hash of a byte array.

Parameters
dataThe data to hash.
Returns
A byte array containing the hash.

Definition at line 910 of file Bundle.cs.

◆ ComputeSHA512Hash() [2/2]

static byte[] SAPTeam.EasySign.Bundle.ComputeSHA512Hash ( Stream stream)
staticprotected

Computes the SHA-512 hash of a stream.

Parameters
streamThe stream to hash.
Returns
A byte array containing the hash.

Definition at line 897 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.AddEntry(), SAPTeam.EasySign.Bundle.VerifyFile(), and SAPTeam.EasySign.Bundle.VerifySignature().

◆ DeleteEntry()

void SAPTeam.EasySign.Bundle.DeleteEntry ( string entryName)

◆ EnsureWritable()

void SAPTeam.EasySign.Bundle.EnsureWritable ( )
protected

◆ Exists()

bool SAPTeam.EasySign.Bundle.Exists ( string entryName,
ReadSource readSource = ReadSource::Automatic )

Checks whether an entry exists in the bundle or on the disk.

Parameters
entryNameThe name of the entry to check.
readSourceThe source from which to check the entry.
Returns
true if the entry exists; otherwise, false.

Definition at line 675 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.GetReadSource(), SAPTeam.EasySign.Bundle.GetZipArchive(), SAPTeam.EasySign.Bundle.Logger, and SAPTeam.EasySign.Bundle.RootPath.

◆ Export() [1/2]

byte[] SAPTeam.EasySign.Bundle.Export ( object structuredData)
protected

Exports the specified structured data to a byte array.

Parameters
structuredDataThe structured data to export.
Returns
A byte array containing the exported data.

Definition at line 819 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.Logger, and SAPTeam.EasySign.Bundle.SerializerOptions.

◆ Export() [2/2]

byte[] SAPTeam.EasySign.Bundle.Export ( object structuredData,
JsonSerializerContext jsonSerializerContext )
protected

Exports the specified structured data to a byte array.

Parameters
structuredDataThe structured data to export.
jsonSerializerContextA metadata provider for serializable types.
Returns
A byte array containing the exported data.

Definition at line 797 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.Logger.

Referenced by SAPTeam.EasySign.Bundle.GetManifestData(), and SAPTeam.EasySign.Bundle.Update().

◆ GetBytes()

byte[] SAPTeam.EasySign.Bundle.GetBytes ( string entryName,
ReadSource readSource )

Gets the data of an entry in the bundle as bytes array and caches the entry data if the bundle is Read-only.

Protected entries are only resolved with ReadSource.Bundle.

Parameters
entryNameThe name of the entry to get the bytes for.
readSourceThe source from which to read the data.
Returns
The entry data as bytes array.

Definition at line 597 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.CacheEntry(), SAPTeam.EasySign.Bundle.GetStream(), SAPTeam.EasySign.Bundle.Logger, and SAPTeam.EasySign.Bundle.ReadStream().

Referenced by SAPTeam.EasySign.Bundle.VerifySignature().

◆ GetCertificate()

X509Certificate2 SAPTeam.EasySign.Bundle.GetCertificate ( string certificateHash)

Gets a certificate from the bundle using the specified certificate hash.

and caches the certificate if the bundle is Read-only.

Parameters
certificateHashThe hash of the certificate to get.
Returns
The certificate.

Definition at line 571 of file Bundle.cs.

References SAPTeam.EasySign.Signatures.Certificates, and SAPTeam.EasySign.Bundle.Logger.

Referenced by SAPTeam.EasySign.CommandLine.CommandProvider< TBundle, TConfiguration >.RunInfo(), SAPTeam.EasySign.CommandLine.CommandProvider< TBundle, TConfiguration >.RunVerify(), SAPTeam.EasySign.Bundle.VerifyCertificate(), and SAPTeam.EasySign.Bundle.VerifySignature().

◆ GetManifestData()

virtual byte[] SAPTeam.EasySign.Bundle.GetManifestData ( )
protectedvirtual

Gets the manifest data as a byte array.

Returns
A byte array containing the manifest data.

Definition at line 783 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.Export(), SAPTeam.EasySign.Bundle.ProtectedEntryNames, and SAPTeam.EasySign.Manifest.ProtectedEntryNames.

Referenced by SAPTeam.EasySign.Bundle.Sign(), and SAPTeam.EasySign.Bundle.Update().

◆ GetReadSource()

ReadSource SAPTeam.EasySign.Bundle.GetReadSource ( string entryName,
ReadSource readSource = ReadSource::Automatic )
protected

Gets the read source for an entry name.

Parameters
entryNameThe name of the entry to get the read source for.
readSourceThe suggested read source.
Returns
The read source for the entry name based on protected entry names and bundle properties.

Definition at line 711 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.CheckEntryNameSecurity().

Referenced by SAPTeam.EasySign.Bundle.Exists(), and SAPTeam.EasySign.Bundle.GetStream().

◆ GetStream()

Stream SAPTeam.EasySign.Bundle.GetStream ( string entryName,
ReadSource readSource = ReadSource::Automatic )

Gets a read-only stream for an entry in the bundle and caches the entry data if the bundle is Read-only.

Protected entries are only resolved with ReadSource.Bundle.

Parameters
entryNameThe name of the entry to get the stream for.
readSourceThe source from which to read the data.
Returns
A read-only stream for the entry.

Definition at line 623 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.GetReadSource(), SAPTeam.EasySign.Bundle.GetZipArchive(), SAPTeam.EasySign.Bundle.Logger, and SAPTeam.EasySign.Bundle.RootPath.

Referenced by SAPTeam.EasySign.Bundle.GetBytes(), and SAPTeam.EasySign.Bundle.VerifyFile().

◆ GetZipArchive()

ZipArchive SAPTeam.EasySign.Bundle.GetZipArchive ( ZipArchiveMode mode = ZipArchiveMode::Read)

◆ LoadFromBytes()

void SAPTeam.EasySign.Bundle.LoadFromBytes ( byte[] bundleContent)

Loads the bundle from a byte array.

This method is more secure and faster than loading from the file as it stores the bundle in memory.

Parameters
bundleContentThe byte array containing the bundle content.

Definition at line 299 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.GetZipArchive(), SAPTeam.EasySign.Bundle.Loaded, SAPTeam.EasySign.Bundle.Logger, SAPTeam.EasySign.Bundle.Parse(), and SAPTeam.EasySign.Bundle.ReadOnly.

◆ LoadFromFile()

void SAPTeam.EasySign.Bundle.LoadFromFile ( bool readOnly = true)

◆ Parse()

virtual void SAPTeam.EasySign.Bundle.Parse ( ZipArchive zip)
protectedvirtual

◆ ReadStream()

static byte[] SAPTeam.EasySign.Bundle.ReadStream ( Stream stream)
staticprotected

Reads a stream into a byte array.

Parameters
streamThe stream to read.
Returns
A byte array containing the stream data.

Definition at line 872 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.GetBytes(), and SAPTeam.EasySign.Bundle.GetZipArchive().

◆ Sign()

void SAPTeam.EasySign.Bundle.Sign ( X509Certificate2 certificate,
RSA privateKey )

Signs the bundle with the specified certificate and private key.

Parameters
certificateThe certificate to use for signing.
privateKeyThe private key to use for signing.

Definition at line 437 of file Bundle.cs.

References SAPTeam.EasySign.Signatures.Certificates, SAPTeam.EasySign.Bundle.EnsureWritable(), SAPTeam.EasySign.Signatures.Entries, SAPTeam.EasySign.Bundle.GetManifestData(), and SAPTeam.EasySign.Bundle.Logger.

Referenced by SAPTeam.EasySign.CommandLine.CommandProvider< TBundle, TConfiguration >.RunSign().

◆ Update()

◆ VerifyCertificate() [1/4]

bool SAPTeam.EasySign.Bundle.VerifyCertificate ( string certificateHash,
out X509ChainStatus[] statuses,
X509ChainPolicy? policy = null )

Verifies the validity of a certificate using the specified certificate hash.

Parameters
certificateHashThe hash of the certificate to verify.
statusesThe chain statuses of the certificate.
policyThe chain policy to use for verification.
Returns
True if the certificate is valid; otherwise, false.

Definition at line 511 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.GetCertificate(), and SAPTeam.EasySign.Bundle.VerifyCertificate().

Referenced by SAPTeam.EasySign.Bundle.VerifyCertificate(), and SAPTeam.EasySign.CommandLine.CommandProvider< TBundle, TConfiguration >.VerifyCertificateImpl().

◆ VerifyCertificate() [2/4]

bool SAPTeam.EasySign.Bundle.VerifyCertificate ( string certificateHash,
X509ChainPolicy? policy = null )

Verifies the validity of a certificate using the specified certificate hash.

Parameters
certificateHashThe hash of the certificate to verify.
policyThe chain policy to use for verification.
Returns
True if the certificate is valid; otherwise, false.

◆ VerifyCertificate() [3/4]

bool SAPTeam.EasySign.Bundle.VerifyCertificate ( X509Certificate2 certificate,
out X509ChainStatus[] statuses,
X509ChainPolicy? policy = null )

Verifies the validity of a certificate.

Parameters
certificateThe certificate to verify.
statusesThe chain statuses of the certificate.
policyThe chain policy to use for verification.
Returns
True if the certificate is valid; otherwise, false.

Definition at line 534 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.Logger.

◆ VerifyCertificate() [4/4]

bool SAPTeam.EasySign.Bundle.VerifyCertificate ( X509Certificate2 certificate,
X509ChainPolicy? policy = null )

Verifies the validity of a certificate.

Parameters
certificateThe certificate to verify.
policyThe chain policy to use for verification.
Returns
True if the certificate is valid; otherwise, false.

◆ VerifyFile()

bool SAPTeam.EasySign.Bundle.VerifyFile ( string entryName)

Verifies the integrity of a file in the bundle.

Parameters
entryNameThe name of the entry to verify.
Returns
True if the file is valid; otherwise, false.

Definition at line 466 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.ComputeSHA512Hash(), SAPTeam.EasySign.Manifest.GetEntries(), SAPTeam.EasySign.Bundle.GetStream(), and SAPTeam.EasySign.Bundle.Logger.

◆ VerifySignature()

bool SAPTeam.EasySign.Bundle.VerifySignature ( string certificateHash)

Verifies the signature of the bundle using the specified certificate hash.

Parameters
certificateHashThe hash of the certificate to use for verification.
Returns
True if the signature is valid; otherwise, false.

Definition at line 486 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.ComputeSHA512Hash(), SAPTeam.EasySign.Signatures.Entries, SAPTeam.EasySign.Bundle.GetBytes(), SAPTeam.EasySign.Bundle.GetCertificate(), and SAPTeam.EasySign.Bundle.Logger.

Referenced by SAPTeam.EasySign.CommandLine.CommandProvider< TBundle, TConfiguration >.RunVerify().

◆ WriteEntry()

void SAPTeam.EasySign.Bundle.WriteEntry ( ZipArchive zip,
string entryName,
byte[] data )
protected

Writes an entry to a ZipArchive.

if the entry is already exists, it will be deleted.

Parameters
zipThe ZipArchive to write to.
entryNameThe name of the entry to write.
dataThe data to write.

Definition at line 836 of file Bundle.cs.

References SAPTeam.EasySign.Bundle.Logger.

Referenced by SAPTeam.EasySign.Bundle.Update().

Member Data Documentation

◆ SerializerOptions

readonly JsonSerializerOptions SAPTeam.EasySign.Bundle.SerializerOptions
protected
Initial value:
= new JsonSerializerOptions()
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
}

Gets the JSON serializer options.

Definition at line 37 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.Export().

Property Documentation

◆ BundleName

string SAPTeam.EasySign.Bundle.BundleName
get

Gets the name of the bundle file.

Definition at line 78 of file Bundle.cs.

◆ BundlePath

◆ DefaultBundleName

virtual string SAPTeam.EasySign.Bundle.DefaultBundleName
getprotected

Gets the default name of the bundle.

Only used when the bundle path does not specify a file name.

Definition at line 68 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.Bundle().

◆ Loaded

bool SAPTeam.EasySign.Bundle.Loaded
get

◆ LoadedFromMemory

bool SAPTeam.EasySign.Bundle.LoadedFromMemory
get

Gets a value indicating whether the bundle is loaded from memory.

Definition at line 103 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.GetZipArchive().

◆ Logger

◆ Manifest

◆ ProtectedEntryNames

HashSet<string> SAPTeam.EasySign.Bundle.ProtectedEntryNames
getprotected
Initial value:
=
[
".manifest.ec",
".signatures.ec",
]

Gets the list of sensitive names.

Regex patterns are supported.

These names are not allowed for add or delete through AddEntry(string, string, string) or DeleteEntry(string). The entries with these names are only resolved with ReadSource.Bundle. This feature is only designed to prevent accidental modification of important files.

Definition at line 56 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.CheckEntryNameSecurity(), SAPTeam.EasySign.Bundle.GetManifestData(), and SAPTeam.EasySign.Bundle.Parse().

◆ ReadOnly

bool SAPTeam.EasySign.Bundle.ReadOnly
get

◆ RootPath

string SAPTeam.EasySign.Bundle.RootPath
get

◆ Signatures

Event Documentation

◆ Updating

Action<ZipArchive>? SAPTeam.EasySign.Bundle.Updating

Occurs when the bundle file is being updated.

Definition at line 113 of file Bundle.cs.

Referenced by SAPTeam.EasySign.Bundle.Update().


The documentation for this class was generated from the following file: