/// <summary> /// See IDigitalSignatureProvider /// </summary> Guid IDigitalSignatureProvider.AddRequestSignature(DigitalSignature digitalSignature) { AssertIsSignable(); // Create guid used for signature ID Guid guidID = Guid.NewGuid(); // Create a new SignatureDefinition XpsSignatureDefinition xpsSignatureDefinition = new XpsSignatureDefinition(); // Use the digSig to setup the SignatureDefinition. xpsSignatureDefinition.RequestedSigner = digitalSignature.SubjectName; xpsSignatureDefinition.Intent = digitalSignature.Reason; xpsSignatureDefinition.SigningLocale = digitalSignature.Location; xpsSignatureDefinition.SignBy = digitalSignature.SignedOn; // Use our new guid to setup the ID xpsSignatureDefinition.SpotId = guidID; // Add the signature definition to the document FixedDocument.AddSignatureDefinition(xpsSignatureDefinition); FixedDocument.CommitSignatureDefinition(); // Set the signature's status to Not Signed before adding to our list digitalSignature.SignatureState = SignatureStatus.NotSigned; // Add the new signature to our list of signatures and definitions DigitalSignatureList.Add(digitalSignature); return(guidID); }
/// <summary> /// See IDigitalSignatureProvider /// </summary> void IDigitalSignatureProvider.RemoveRequestSignature(Guid spotId) { AssertIsSignable(); XpsSignatureDefinition definition = FindSignatureDefinition(spotId); if (definition != null) { FixedDocument.RemoveSignatureDefinition(definition); FixedDocument.CommitSignatureDefinition(); } // Loop through the signature list and remove the entry for the // requested signature foreach (DigitalSignature signature in DigitalSignatureList) { if (signature.GuidID == spotId) { // We only want to remove unsigned signature definitions // (requested signatures) and not actual signatures. if (signature.SignatureState == SignatureStatus.NotSigned) { DigitalSignatureList.Remove(signature); } // It is safe to remove an element from the list that we're // currently enumerating because we stop enumerating as soon // as we find the element we're looking for. break; } } }
/// <summary> /// See IDigitalSignatureProvider /// </summary> void IDigitalSignatureProvider.SignDocument(DigitalSignature digitalSignature) { AssertIsSignable(); XpsDigSigPartAlteringRestrictions reachRestrictions = XpsDigSigPartAlteringRestrictions.None; if (digitalSignature.IsDocumentPropertiesRestricted) { reachRestrictions |= XpsDigSigPartAlteringRestrictions.CoreMetadata; } // If additional signatures should invalidate this signature, we // need to sign the signature origin part if (digitalSignature.IsAddingSignaturesRestricted) { reachRestrictions |= XpsDigSigPartAlteringRestrictions.SignatureOrigin; } // a null guid means there was no associated spot, so create a guid if (digitalSignature.GuidID == null) { digitalSignature.GuidID = Guid.NewGuid(); } XpsDigitalSignature xpsDigitalSignature = XpsDocument.SignDigitally( digitalSignature.Certificate, true, reachRestrictions, (Guid)digitalSignature.GuidID, false /* don't re-verify IsSignable, we've already done it */ ); if (xpsDigitalSignature != null) { // Fill in relevant fields from the XPS signature digitalSignature.XpsDigitalSignature = xpsDigitalSignature; digitalSignature.SignatureState = SignatureStatus.Valid; digitalSignature.SignedOn = xpsDigitalSignature.SigningTime; // Save the simple name from the certificate as the subject name // in the signature digitalSignature.SubjectName = digitalSignature.Certificate.GetNameInfo( X509NameType.SimpleName, false /* don't include issuer name */); // Add the new signature to the list (if it isn't already there). // That is a possibility since the first signature in a document // is always added as a signature definition and a signature. if (!DigitalSignatureList.Contains(digitalSignature)) { DigitalSignatureList.Add(digitalSignature); } } }
/// <summary> /// See IDigitalSignatureProvider /// </summary> void IDigitalSignatureProvider.UnsignDocument(Guid id) { AssertIsSignable(); foreach (DigitalSignature signature in DigitalSignatureList) { if (signature.GuidID == id) { // Remove the associated XpsDigitalSignature from the // document if (signature.XpsDigitalSignature != null) { XpsDocument.RemoveSignature(signature.XpsDigitalSignature); signature.XpsDigitalSignature = null; } // Check if the document contains a signature definition // corresponding to this signature bool matchesDefinition = (FindSignatureDefinition(id) != null); // If the signature matches a signature definition in the // document, mark the signature as NotSigned (i.e. an // unsigned request) but leave it in the list if (matchesDefinition) { signature.SignatureState = SignatureStatus.NotSigned; } else { // Remove the signature from the list DigitalSignatureList.Remove(signature); } // It is safe to remove an element from the list that we're // currently enumerating because we stop enumerating as soon // as we find the element we're looking for. break; } } }