// Copyright (C) 2024, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Generic; using System.Security.Cryptography; namespace Duplicati.Library.Utility { public class SignatureReadingStream : System.IO.Stream, IDisposable { /// /// The size of the SHA256 output hash in bytes /// /// internal const int SIGNED_HASH_SIZE = 128; /// /// The stream to read from /// private readonly System.IO.Stream m_stream; protected SignatureReadingStream() { } /// /// Creates a new stream that reads from the given stream and verifies the signature using any of the given keys /// /// The stream with a signature /// The allowed keys public SignatureReadingStream(System.IO.Stream stream, IEnumerable keys) { if (!VerifySignature(stream, keys)) throw new System.IO.InvalidDataException("Unable to verify signature"); m_stream = stream; this.Position = 0; } /// /// Wraps trying the keys one by one, returning true if any of the keys validate /// /// The stream to verify /// The keys to try /// true if the stream is valid; false otherwise private static bool VerifySignature(System.IO.Stream stream, IEnumerable keys) { if (keys == null) return false; foreach (var key in keys) try { if (VerifySignature(stream, key)) return true; } catch { } return false; } /// /// Verifies the signature of the stream using the given key /// /// The stream to verify /// The key to validate with /// true if the stream signature matches the key; false otherwise private static bool VerifySignature(System.IO.Stream stream, System.Security.Cryptography.RSA key) { stream.Position = 0; var signature = new byte[SIGNED_HASH_SIZE]; if (Duplicati.Library.Utility.Utility.ForceStreamRead(stream, signature, signature.Length) != signature.Length) throw new System.IO.InvalidDataException("Unexpected end-of-stream while reading signature"); var sha256 = System.Security.Cryptography.SHA256.Create(); sha256.Initialize(); var bytes = stream.Length - (signature.Length); var buf = new byte[8 * 1024]; while (bytes > 0) { var r = stream.Read(buf, 0, (int)Math.Min(bytes, buf.Length)); if (r == 0) throw new Exception("Unexpected end-of-stream while reading content"); bytes -= r; sha256.TransformBlock(buf, 0, r, buf, 0); } sha256.TransformFinalBlock(buf, 0, 0); var hash = sha256.Hash; return key.VerifyHash(hash, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); } /// /// Creates a signed stream from the given data stream and writes the signature to the signed stream /// /// The stream to sign /// The stream with the signature /// The key used to sign it public static void CreateSignedStream(System.IO.Stream datastream, System.IO.Stream signedstream, System.Security.Cryptography.RSA key) { var sha256 = System.Security.Cryptography.SHA256.Create(); datastream.Position = 0; signedstream.Position = SIGNED_HASH_SIZE; var buf = new byte[8 * 1024]; var bytes = datastream.Length; while (bytes > 0) { var r = datastream.Read(buf, 0, (int)Math.Min(bytes, buf.Length)); if (r == 0) throw new Exception("Unexpected end-of-stream while reading content"); signedstream.Write(buf, 0, r); bytes -= r; sha256.TransformBlock(buf, 0, r, buf, 0); } sha256.TransformFinalBlock(buf, 0, 0); var hash = sha256.Hash; var signature = key.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); if (signature.Length != SIGNED_HASH_SIZE) throw new System.IO.InvalidDataException("Unexpected signature length"); signedstream.Position = 0; signedstream.Write(signature, 0, signature.Length); signedstream.Position = 0; if (!VerifySignature(signedstream, key)) throw new System.IO.InvalidDataException("Unable to verify signature"); } #region implemented abstract members of Stream public override void Flush() { try { m_stream.Flush(); } catch { } } public override int Read(byte[] buffer, int offset, int count) { return m_stream.Read(buffer, offset, count); } public override long Seek(long offset, System.IO.SeekOrigin origin) { switch (origin) { case System.IO.SeekOrigin.Current: return Seek(offset + this.Position, System.IO.SeekOrigin.Begin); case System.IO.SeekOrigin.End: return Seek(this.Length - offset, System.IO.SeekOrigin.Begin); case System.IO.SeekOrigin.Begin: default: return this.Position = offset; } } public override void SetLength(long value) { throw new InvalidOperationException(); } public override void Write(byte[] buffer, int offset, int count) { throw new InvalidOperationException(); } public override bool CanRead { get { return true; } } public override bool CanSeek { get { return true; } } public override bool CanWrite { get { return false; } } public override long Length { get { return m_stream.Length - SIGNED_HASH_SIZE; } } // Since the constructor sets the Position, we seal the implementation here to prevent subclasses // from potentially referencing uninitialized members. public sealed override long Position { get { return m_stream.Position - SIGNED_HASH_SIZE; } set { m_stream.Position = value + SIGNED_HASH_SIZE; } } #endregion } }