Exemplo n.º 1
0
        private static async Task AddSignatureToPackageAsync(SimpleTestPackageContext packageContext, ISignedPackage package, SignPackageRequest request, ILogger logger)
        {
            var testSignatureProvider = new X509SignatureProvider(packageContext.PrimaryTimestampProvider);

            var zipArchiveHash = await package.GetArchiveHashAsync(request.SignatureHashAlgorithm, CancellationToken.None);

            var base64ZipArchiveHash = Convert.ToBase64String(zipArchiveHash);
            var signatureContent     = new SignatureContent(SigningSpecifications.V1, request.SignatureHashAlgorithm, base64ZipArchiveHash);

            var signature = await testSignatureProvider.CreatePrimarySignatureAsync(request, signatureContent, logger, CancellationToken.None);

            using (var stream = new MemoryStream(signature.GetBytes()))
            {
                await package.AddSignatureAsync(stream, CancellationToken.None);
            }
        }
        /// <summary>
        /// Generates a Signature for a given package for tests.
        /// </summary>
        /// <param name="package">Package to be used for the signature.</param>
        /// <param name="request">Sign package request for primary signature</param>
        /// <param name="timestampProvider">Provider to add timestamp to package. Defaults to null.</param>
        /// <returns>Signature for the package.</returns>
        public static async Task <PrimarySignature> CreatePrimarySignatureForPackageAsync(
            PackageArchiveReader package,
            SignPackageRequest request,
            ITimestampProvider timestampProvider = null)
        {
            Assert.False(await package.IsSignedAsync(CancellationToken.None));

            var testLogger        = new TestLogger();
            var signatureProvider = new X509SignatureProvider(timestampProvider);

            var zipArchiveHash = await package.GetArchiveHashAsync(request.SignatureHashAlgorithm, CancellationToken.None);

            var base64ZipArchiveHash = Convert.ToBase64String(zipArchiveHash);
            var signatureContent     = new SignatureContent(SigningSpecifications.V1, request.SignatureHashAlgorithm, base64ZipArchiveHash);

            return(await signatureProvider.CreatePrimarySignatureAsync(request, signatureContent, testLogger, CancellationToken.None));
        }
        /// <summary>
        /// Generates a Signature for a package.
        /// </summary>
        /// <param name="testCert">Certificate to be used while generating the signature.</param>
        /// <param name="packageStream">Package stream for which the signature has to be generated.</param>
        /// <param name="timestampProvider">An optional timestamp provider.</param>
        /// <returns>Signature for the package.</returns>
        public static async Task <PrimarySignature> CreatePrimarySignatureForPackageAsync(
            X509Certificate2 testCert,
            Stream packageStream,
            ITimestampProvider timestampProvider = null)
        {
            var testLogger    = new TestLogger();
            var hashAlgorithm = HashAlgorithmName.SHA256;

            using (var request = new AuthorSignPackageRequest(testCert, hashAlgorithm))
                using (var package = new PackageArchiveReader(packageStream, leaveStreamOpen: true))
                {
                    var zipArchiveHash = await package.GetArchiveHashAsync(request.SignatureHashAlgorithm, CancellationToken.None);

                    var base64ZipArchiveHash  = Convert.ToBase64String(zipArchiveHash);
                    var signatureContent      = new SignatureContent(SigningSpecifications.V1, hashAlgorithm, base64ZipArchiveHash);
                    var testSignatureProvider = new X509SignatureProvider(timestampProvider);

                    return(await testSignatureProvider.CreatePrimarySignatureAsync(request, signatureContent, testLogger, CancellationToken.None));
                }
        }