MessageSignature FindTrustedSignature(IncomingMessage message, X509Certificate2Collection anchors) { DirectAddress sender = message.Sender; MessageSignatureCollection signatures = message.SenderSignatures; MessageSignature lastTrustedSignature = null; foreach (MessageSignature signature in signatures) { if (m_certChainValidator.IsTrustedCertificate(signature.Certificate, anchors) && signature.CheckSignature()) { if (!sender.HasCertificates) { // Can't really check thumbprints etc. So, this is about as good as its going to get return(signature); } if (signature.CheckThumbprint(sender)) { return(signature); } // // We'll save this guy, but keep looking for a signer whose thumbprint we can verify // If we can't find one, we'll use the last trusted signer we found.. and just mark the recipient's trust // enforcement status as Success_ThumbprintMismatch // lastTrustedSignature = signature; } } return(lastTrustedSignature); }
/// <summary> /// Enforces the trust model on an incoming message by marking /// the <c>Status</c> property of <see cref="DirectAddress"/> instances for the receivers /// </summary> /// <param name="message">The <see cref="IncomingMessage"/> to validate trust for.</param> /// <exception cref="AgentException">If this message has no signatures</exception> public void Enforce(IncomingMessage message) { if (message == null) { throw new ArgumentNullException("message"); } if (!message.HasSignatures) { throw new AgentException(AgentError.UnsignedMessage); } // // The message could have multiple signatures, including, possibly, some not by the sender // this.FindSenderSignatures(message); if (!message.HasSenderSignatures) { throw new AgentException(AgentError.MissingSenderSignature); } // // For each recipient, find at least one valid sender signature that the recipient trusts // DirectAddress sender = message.Sender; DirectAddressCollection recipients = message.DomainRecipients; foreach (DirectAddress recipient in recipients) { recipient.Status = TrustEnforcementStatus.Failed; // // First, find a signature that this recipient trusts // MessageSignature trustedSignature = this.FindTrustedSignature(message, recipient, recipient.TrustAnchors); if (trustedSignature != null) { recipient.Status = TrustEnforcementStatus.Success; // // Signature has already been verified by FindTrustedSignature! // } } }