Пример #1
0
        public override void CreateContainer(ICadesExternalSignature externalSigner, string ouputFilePath)
        {
            if (externalSigner == null)
            {
                throw new ArgumentNullException(nameof(externalSigner));
            }

            if (ouputFilePath == null)
            {
                throw new ArgumentNullException(nameof(ouputFilePath));
            }

            AsicManifestBuilder asicManifestBuilder = new AsicManifestBuilder();

            asicManifestBuilder.AddP7Signature(SignaturePath);

            foreach ((FileInfo file, string mimeType) in this.inputFiles)
            {
                using Stream contentStream = file.OpenRead();
                asicManifestBuilder.AddFile(file.Name, mimeType, contentStream);
            }

            byte[] manifestData = asicManifestBuilder.ToByteArray();

            Pkcs7DetachedSignatureGenerator p7Generator = new Pkcs7DetachedSignatureGenerator(externalSigner);

            X509CertificateParser x509CertificateParser = new X509CertificateParser();
            X509Certificate       signingCertificate    = x509CertificateParser.ReadCertificate(externalSigner.GetCertificate());

            byte[] signature = p7Generator.GenerateP7s(manifestData, signingCertificate, this.BuildCertificatePath(signingCertificate));

            using ZipArchive archive = ZipFile.Open(ouputFilePath, ZipArchiveMode.Create);
            this.AddFileToArchive(archive, ContainerMimeTypePath, ContainerMimeType);
            this.AddFileToArchive(archive, ManifestPath, manifestData);
            this.AddFileToArchive(archive, SignaturePath, signature);

            foreach ((FileInfo file, _) in this.inputFiles)
            {
                this.AddFileToArchive(archive, file.Name, file);
            }
        }
        public override void CreateContainer(ICadesExternalSignature externalSigner, string ouputFilePath)
        {
            if (externalSigner == null)
            {
                throw new ArgumentNullException(nameof(externalSigner));
            }

            if (ouputFilePath == null)
            {
                throw new ArgumentNullException(nameof(ouputFilePath));
            }

            if (File.Exists(ouputFilePath))
            {
                File.Delete(ouputFilePath);
            }

            File.Copy(this.originalAsicePath, ouputFilePath);
            try
            {
                using ZipArchive archive = ZipFile.Open(ouputFilePath, ZipArchiveMode.Update);
                (string signaturePath, string manifestPath) = this.CreateMetadatNames(archive);
                AsicManifestBuilder asicManifestBuilder = new AsicManifestBuilder();
                asicManifestBuilder.AddP7Signature(signaturePath);

                foreach ((FileInfo file, string mimeType) in this.inputFiles)
                {
                    using Stream contentStream = file.OpenRead();
                    asicManifestBuilder.AddFile(file.Name, mimeType, contentStream);
                }

                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (!entry.FullName.StartsWith("META-INF/", StringComparison.Ordinal) && !string.Equals(entry.FullName, ContainerMimeTypePath, StringComparison.Ordinal))
                    {
                        using Stream contentStream = entry.Open();
                        asicManifestBuilder.AddFile(entry.FullName, MimeType.GetMimeTypeFromFileName(Path.GetFileName(entry.FullName)), contentStream);
                    }
                }

                byte[] manifestData = asicManifestBuilder.ToByteArray();
                X509CertificateParser x509CertificateParser = new X509CertificateParser();
                X509Certificate       signingCertificate    = x509CertificateParser.ReadCertificate(externalSigner.GetCertificate());

                Pkcs7DetachedSignatureGenerator p7Generator = new Pkcs7DetachedSignatureGenerator(externalSigner);
                byte[] signature = p7Generator.GenerateP7s(manifestData, signingCertificate, this.BuildCertificatePath(signingCertificate));

                this.AddFileToArchive(archive, manifestPath, manifestData);
                this.AddFileToArchive(archive, signaturePath, signature);

                foreach ((FileInfo file, _) in this.inputFiles)
                {
                    this.AddFileToArchive(archive, file.Name, file);
                }
            }
            catch
            {
                if (File.Exists(ouputFilePath))
                {
                    File.Delete(ouputFilePath);
                }

                throw;
            }
        }