@@ -19,7 +19,7 @@ namespace SAPTeam.EasySign
1919 public class Bundle
2020 {
2121 private readonly string bundleName = ".eSign" ;
22- private byte [ ] rawZipContents = null ;
22+ private byte [ ] ? rawZipContents = null ;
2323
2424 /// <summary>
2525 /// Gets the root path of the bundle. This path used for relative path resolution.
@@ -90,7 +90,7 @@ public string BundleName
9090 /// <summary>
9191 /// Occurs when the bundle file is being updated.
9292 /// </summary>
93- public event Action < ZipArchive > OnUpdating ;
93+ public event Action < ZipArchive > ? OnUpdating ;
9494
9595 /// <summary>
9696 /// Initializes a new instance of the <see cref="Bundle"/> class with the specified root path and bundle name.
@@ -132,7 +132,9 @@ public ZipArchive OpenZipArchive(ZipArchiveMode mode = ZipArchiveMode.Read)
132132
133133 if ( IsLoadedFromMemory )
134134 {
135+ #pragma warning disable CS8604 // Possible null reference argument.
135136 var ms = new MemoryStream ( rawZipContents ) ;
137+ #pragma warning restore CS8604 // Possible null reference argument.
136138 return new ZipArchive ( ms , mode ) ;
137139 }
138140 else
@@ -185,19 +187,19 @@ public void LoadFromBytes(byte[] bundleContent)
185187 /// <param name="zip">The <see cref="ZipArchive"/> to read from.</param>
186188 protected virtual void ReadBundle ( ZipArchive zip )
187189 {
188- ZipArchiveEntry entry ;
190+ ZipArchiveEntry ? entry ;
189191 if ( ( entry = zip . GetEntry ( ".manifest.ec" ) ) != null )
190192#if NET6_0_OR_GREATER
191- Manifest = JsonSerializer . Deserialize ( entry . Open ( ) , typeof ( Manifest ) , SourceGenerationManifestContext . Default ) as Manifest ;
193+ Manifest = JsonSerializer . Deserialize ( entry . Open ( ) , typeof ( Manifest ) , SourceGenerationManifestContext . Default ) as Manifest ?? new Manifest ( ) ;
192194#else
193- Manifest = JsonSerializer . Deserialize < Manifest > ( entry . Open ( ) , SerializerOptions ) ;
195+ Manifest = JsonSerializer . Deserialize < Manifest > ( entry . Open ( ) , SerializerOptions ) ?? new Manifest ( ) ; ;
194196#endif
195197
196198 if ( ( entry = zip . GetEntry ( ".signatures.ec" ) ) != null )
197199#if NET6_0_OR_GREATER
198- Signatures = JsonSerializer . Deserialize ( entry . Open ( ) , typeof ( Signatures ) , SourceGenerationSignaturesContext . Default ) as Signatures ;
200+ Signatures = JsonSerializer . Deserialize ( entry . Open ( ) , typeof ( Signatures ) , SourceGenerationSignaturesContext . Default ) as Signatures ?? new Signatures ( ) ;
199201#else
200- Signatures = JsonSerializer . Deserialize < Signatures > ( entry . Open ( ) , SerializerOptions ) ;
202+ Signatures = JsonSerializer . Deserialize < Signatures > ( entry . Open ( ) , SerializerOptions ) ?? new Signatures ( ) ;
201203#endif
202204 }
203205
@@ -209,7 +211,7 @@ protected virtual void ReadBundle(ZipArchive zip)
209211 /// <param name="path">The path of the file to add.</param>
210212 /// <param name="destinationPath">The destination path within the bundle. Ignore when <see cref="Manifest.StoreOriginalFiles"/> is <see langword="false"/></param>
211213 /// <param name="rootPath">The root path for relative paths.</param>
212- public void AddEntry ( string path , string destinationPath = "./" , string rootPath = null )
214+ public void AddEntry ( string path , string destinationPath = "./" , string ? rootPath = null )
213215 {
214216 EnsureWritable ( ) ;
215217
@@ -299,6 +301,8 @@ public bool VerifySignature(string certificateHash)
299301 X509Certificate2 certificate = GetCertificate ( certificateHash ) ;
300302 var pubKey = certificate . GetRSAPublicKey ( ) ;
301303
304+ if ( pubKey == null ) return false ;
305+
302306#if NET6_0_OR_GREATER
303307 var manifestData = Export ( Manifest , SourceGenerationManifestContext . Default ) ;
304308#else
@@ -315,7 +319,7 @@ public bool VerifySignature(string certificateHash)
315319 /// <param name="statuses">The chain statuses of the certificate.</param>
316320 /// <param name="policy">The chain policy to use for verification.</param>
317321 /// <returns>True if the certificate is valid; otherwise, false.</returns>
318- public bool VerifyCertificate ( string certificateHash , out X509ChainStatus [ ] statuses , X509ChainPolicy policy = null )
322+ public bool VerifyCertificate ( string certificateHash , out X509ChainStatus [ ] statuses , X509ChainPolicy ? policy = null )
319323 {
320324 X509Certificate2 certificate = GetCertificate ( certificateHash ) ;
321325 return VerifyCertificate ( certificate , out statuses , policy ) ;
@@ -327,7 +331,7 @@ public bool VerifyCertificate(string certificateHash, out X509ChainStatus[] stat
327331 /// <param name="certificateHash">The hash of the certificate to verify.</param>
328332 /// <param name="policy">The chain policy to use for verification.</param>
329333 /// <returns>True if the certificate is valid; otherwise, false.</returns>
330- public bool VerifyCertificate ( string certificateHash , X509ChainPolicy policy = null )
334+ public bool VerifyCertificate ( string certificateHash , X509ChainPolicy ? policy = null )
331335 {
332336 return VerifyCertificate ( certificateHash , out _ , policy ) ;
333337 }
@@ -339,7 +343,7 @@ public bool VerifyCertificate(string certificateHash, X509ChainPolicy policy = n
339343 /// <param name="statuses">The chain statuses of the certificate.</param>
340344 /// <param name="policy">The chain policy to use for verification.</param>
341345 /// <returns>True if the certificate is valid; otherwise, false.</returns>
342- public bool VerifyCertificate ( X509Certificate2 certificate , out X509ChainStatus [ ] statuses , X509ChainPolicy policy = null )
346+ public bool VerifyCertificate ( X509Certificate2 certificate , out X509ChainStatus [ ] statuses , X509ChainPolicy ? policy = null )
343347 {
344348 X509Chain chain = new X509Chain
345349 {
@@ -357,7 +361,7 @@ public bool VerifyCertificate(X509Certificate2 certificate, out X509ChainStatus[
357361 /// <param name="certificate">The certificate to verify.</param>
358362 /// <param name="policy">The chain policy to use for verification.</param>
359363 /// <returns>True if the certificate is valid; otherwise, false.</returns>
360- public bool VerifyCertificate ( X509Certificate2 certificate , X509ChainPolicy policy = null )
364+ public bool VerifyCertificate ( X509Certificate2 certificate , X509ChainPolicy ? policy = null )
361365 {
362366 return VerifyCertificate ( certificate , out _ , policy ) ;
363367 }
@@ -369,7 +373,7 @@ public bool VerifyCertificate(X509Certificate2 certificate, X509ChainPolicy poli
369373 /// <returns>The certificate.</returns>
370374 public X509Certificate2 GetCertificate ( string certificateHash )
371375 {
372- if ( ! certCache . TryGetValue ( certificateHash , out X509Certificate2 certificate ) )
376+ if ( ! certCache . TryGetValue ( certificateHash , out X509Certificate2 ? certificate ) )
373377 {
374378 using var zip = OpenZipArchive ( ) ;
375379 var certData = ReadEntry ( zip , certificateHash ) ;
@@ -389,7 +393,8 @@ public Stream GetFileStream(string entryName)
389393 if ( Manifest . StoreOriginalFiles )
390394 {
391395 using var zip = OpenZipArchive ( ) ;
392- return zip . GetEntry ( entryName ) . Open ( ) ;
396+ var entry = zip . GetEntry ( entryName ) ?? throw new FileNotFoundException ( "Entry not found" , entryName ) ;
397+ return entry . Open ( ) ;
393398 }
394399 else
395400 {
@@ -486,7 +491,8 @@ protected byte[] ReadEntry(ZipArchive zip, string entryName)
486491 {
487492 if ( ! fileCache . TryGetValue ( entryName , out var data ) )
488493 {
489- using var stream = zip . GetEntry ( entryName ) . Open ( ) ;
494+ var entry = zip . GetEntry ( entryName ) ?? throw new FileNotFoundException ( "Entry not found" , entryName ) ;
495+ using var stream = entry . Open ( ) ;
490496 data = ReadStream ( stream ) ;
491497
492498 if ( IsReadOnly )
@@ -517,14 +523,14 @@ private static byte[] ReadStream(Stream stream)
517523 /// <param name="data">The data to write.</param>
518524 protected void WriteEntry ( ZipArchive zip , string entryName , byte [ ] data )
519525 {
520- ZipArchiveEntry tempEntry ;
526+ ZipArchiveEntry ? tempEntry ;
521527 if ( ( tempEntry = zip . GetEntry ( entryName ) ) != null )
522528 tempEntry . Delete ( ) ;
523529
524530#if NET6_0_OR_GREATER
525531 ZipArchiveEntry entry = zip . CreateEntry ( entryName , CompressionLevel . SmallestSize ) ;
526532#else
527- ZipArchiveEntry entry = zip . CreateEntry ( entryName , CompressionLevel . Optimal ) ;
533+ ZipArchiveEntry entry = zip . CreateEntry ( entryName , CompressionLevel . Optimal ) ;
528534#endif
529535
530536 using var stream = entry . Open ( ) ;
0 commit comments