protected override void Init()
        {
            base.Init();
            connectionString = Settings.DS_document;
            mode             = FolderRuleMode.Undefined;

            optMessageFrom   = new MessageFrom("MessageSender");
            optMessageTo     = new MessageTo("MessageReceiver");
            optMessageText   = new MessageText("MessageText");
            optSignedBy      = new SignedBy("DocumentSigner");
            optDocumentType  = new FolderRuleOptions.DocumentType("DocumentType");
            optPerson        = new Person("LinkedPerson");
            optDocument      = new FolderRuleOptions.Document("LinkedDocuments");
            optNoMessageText = new NotMessageText("NotMessageText");
            optNotSignedBy   = new NotSignetBy("NoDocumentSigner");
            optNoPerson      = new NoPerson("NoLinkedPerson");
            optNoDocument    = new NoDocument("NoLinkedDocuments");

            allOptions = new Option[]
            {
                optMessageFrom,
                optMessageTo,
                optMessageText,
                optSignedBy,
                optDocumentType,
                optPerson,
                optDocument,
                optNoDocument,
                optNoMessageText,
                optNoPerson,
                optNotSignedBy
            };
        }
Exemple #2
0
        /// <summary>
        /// Applies a digital signature to the collection.
        ///
        /// The signature is generated when the document is saved, so you can still modify the collection after this call.
        /// </summary>
        public void AddSignature(X509Certificate2 signerCertificate)
        {
            if (signerCertificate == null)
            {
                throw new ArgumentNullException(nameof(signerCertificate));
            }

            // Cannot add signatures to the collection if the document itself is signed!
            Document.VerifyIsNotReadOnly();

            if (SignedBy.Contains(signerCertificate))
            {
                throw new InvalidOperationException("The collection is already signed by this identity.");
            }

            CryptographyHelpers.ValidateSignerCertificate(signerCertificate);

            _newSigners.Add(signerCertificate);
        }
Exemple #3
0
        internal override void SaveChanges(XmlDocument document, XmlNamespaceManager namespaces)
        {
            var containerElement = (XmlElement)document.SelectSingleNode("/cpix:CPIX/cpix:" + ContainerName, namespaces);

            if (Count == 0 && SignedBy.Count() == 0)
            {
                // We don't have any contents to put in it AND we don't have any signatures to apply.
                // This is the only scenario where we do not need the container element in the document, so remove it
                // and consider the save operation completed. All other paths follow the longer logic chain.

                if (containerElement != null)
                {
                    containerElement.ParentNode.RemoveChild(containerElement);
                }

                return;
            }

            // We need a container element, so create one if it is missing.
            if (containerElement == null)
            {
                var rootElement = document.DocumentElement;

                // This namespace must exist, as the root element itself uses it. We will reuse the same prefix.
                var prefix  = rootElement.GetPrefixOfNamespace(Constants.CpixNamespace);
                var element = document.CreateElement(prefix, ContainerName, Constants.CpixNamespace);

                containerElement = XmlHelpers.InsertTopLevelCpixXmlElementInCorrectOrder(element, document);
            }

            // Add any new items and then mark them as loaded items.
            foreach (var item in _newItems.ToArray())
            {
                var element = SerializeEntity(document, namespaces, containerElement, item);

                _newItems.Remove(item);
                _loadedItemsData.Add(new Tuple <TEntity, XmlElement>(item, element));
            }

            SaveNewSignatures(document, containerElement);
        }