Author: atsushi
Date: 2007-02-27 03:59:28 -0500 (Tue, 27 Feb 2007)
New Revision: 73454

Modified:
   trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/ChangeLog
   
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageDecryptor.cs
   
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageGenerator.cs
Log:
2007-02-27  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * SecureMessageDecryptor.cs : verify that endorsing supporting tokens
          actually endorsed the primary signature.
        * SecureMessageGenerator.cs : implemented endorsing of the primary
          signature. So, now supporting tokens are fully implemented.



Modified: 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/ChangeLog
===================================================================
--- 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/ChangeLog    
    2007-02-27 05:42:01 UTC (rev 73453)
+++ 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/ChangeLog    
    2007-02-27 08:59:28 UTC (rev 73454)
@@ -1,5 +1,12 @@
 2007-02-27  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
+       * SecureMessageDecryptor.cs : verify that endorsing supporting tokens
+         actually endorsed the primary signature.
+       * SecureMessageGenerator.cs : implemented endorsing of the primary
+         signature. So, now supporting tokens are fully implemented.
+
+2007-02-27  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
        * MessageSecurityBindingSupport.cs : so, those supporting tokens are
          totally signing tokens.
        * SecureMessageGenerator.cs : Endorsing tokens should also be

Modified: 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageDecryptor.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageDecryptor.cs
        2007-02-27 05:42:01 UTC (rev 73453)
+++ 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageDecryptor.cs
        2007-02-27 08:59:28 UTC (rev 73454)
@@ -366,13 +366,13 @@
 
                void ValidateTokensByParameters (SupportingTokenParameters 
supp, List<SupportingTokenInfo> tokens, bool optional)
                {
-                       ValidateTokensByParameters (supp.Endorsing, tokens, 
optional);
-                       ValidateTokensByParameters (supp.Signed, tokens, 
optional);
-                       ValidateTokensByParameters (supp.SignedEndorsing, 
tokens, optional);
-                       ValidateTokensByParameters (supp.SignedEncrypted, 
tokens, optional);
+                       ValidateTokensByParameters (supp.Endorsing, tokens, 
optional, SecurityTokenAttachmentMode.Endorsing);
+                       ValidateTokensByParameters (supp.Signed, tokens, 
optional, SecurityTokenAttachmentMode.Signed);
+                       ValidateTokensByParameters (supp.SignedEndorsing, 
tokens, optional, SecurityTokenAttachmentMode.SignedEndorsing);
+                       ValidateTokensByParameters (supp.SignedEncrypted, 
tokens, optional, SecurityTokenAttachmentMode.SignedEncrypted);
                }
 
-               void ValidateTokensByParameters 
(IEnumerable<SecurityTokenParameters> plist, List<SupportingTokenInfo> tokens, 
bool optional)
+               void ValidateTokensByParameters 
(IEnumerable<SecurityTokenParameters> plist, List<SupportingTokenInfo> tokens, 
bool optional, SecurityTokenAttachmentMode attachMode)
                {
                        foreach (SecurityTokenParameters p in plist) {
                                SecurityTokenResolver r;
@@ -383,12 +383,36 @@
                                        if (optional)
                                                continue;
                                        else
-                                               throw new 
MessageSecurityException (String.Format ("No security token could be validated 
for authenticator '{0}' which is indicated by the security token parameters", 
a));
+                                               throw new 
MessageSecurityException (String.Format ("No security token could be validated 
for authenticator '{0}' which is indicated by the '{1}' supporting token 
parameters", a, attachMode));
+                               } else {
+                                       // For endorsing tokens, verify 
corresponding signatures.
+                                       switch (attachMode) {
+                                       case 
SecurityTokenAttachmentMode.Endorsing:
+                                       case 
SecurityTokenAttachmentMode.SignedEndorsing:
+                                               WSSignedXml esxml = 
GetSignatureForToken (spec.SecurityToken);
+                                               if (esxml == null)
+                                                       throw new 
MessageSecurityException (String.Format ("The '{1}' token '{0}' is expected to 
endorse the primary signature but no corresponding signature is found.", 
spec.SecurityToken, attachMode));
+                                               break;
+                                       }
                                }
+
                                sec_prop.IncomingSupportingTokens.Add (spec);
                        }
                }
 
+               WSSignedXml GetSignatureForToken (SecurityToken token)
+               {
+                       int count = 0;
+                       foreach (WSSignedXml sxml in 
wss_header.FindAll<WSSignedXml> ()) {
+                               if (count++ == 0)
+                                       continue; // primary signature
+                               foreach (SecurityTokenReferenceKeyInfo r in 
sxml.KeyInfo)
+                                       if (token.MatchesKeyIdentifierClause 
(r.Clause))
+                                               return sxml;
+                       }
+                       return null;
+               }
+
                SupportingTokenSpecification ValidateTokensByParameters 
(SecurityTokenAuthenticator a, SecurityTokenResolver r, 
List<SupportingTokenInfo> tokens)
                {
                        foreach (SupportingTokenInfo info in tokens)

Modified: 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageGenerator.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageGenerator.cs
        2007-02-27 05:42:01 UTC (rev 73453)
+++ 
trunk/olive/class/System.ServiceModel/System.ServiceModel.Channels/SecureMessageGenerator.cs
        2007-02-27 08:59:28 UTC (rev 73454)
@@ -309,7 +309,6 @@
                        SecurityToken actualToken = null;
                        SecurityKeyIdentifierClause actualClause = null;
                        Signature sig = null;
-                       EncryptedData sigenc = null;
 
                        List<DerivedKeySecurityToken> derivedKeys =
                                new List<DerivedKeySecurityToken> ();
@@ -423,7 +422,18 @@
 
                        XmlElement body = doc.SelectSingleNode 
("/s:Envelope/s:Body/*", nsmgr) as XmlElement;
                        string bodyId = null;
+                       XmlElement secElem = null;
+                       Collection<WSSignedXml> endorsedSignatures =
+                               new Collection<WSSignedXml> ();
+                       bool signatureProtection = (protectionOrder == 
MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature);
 
+                       // Below are o:Security contents that are not signed...
+                       if (includeSigToken)
+                               header.Contents.Add (signToken);
+                       // When we do not output EncryptedKey, output 
ReferenceList here.
+                       if (!ShouldOutputEncryptedKey)
+                               header.Contents.Add 
(primaryToken.ReferenceList);
+
                        switch (protectionOrder) {
                        case MessageProtectionOrder.EncryptBeforeSign:
                                // FIXME: implement
@@ -450,8 +460,8 @@
                                for (int i = 0; i < msg.Headers.Count; i++) {
                                        MessageHeaderInfo h = msg.Headers [i];
                                        if (h.Name == "Security" && h.Namespace 
== Constants.WssNamespace)
-                                               continue;
-                                       if (sigSpec.HeaderTypes.Count == 0 ||
+                                               secElem = nodes [i] as 
XmlElement;
+                                       else if (sigSpec.HeaderTypes.Count == 0 
||
                                            sigSpec.HeaderTypes.Contains (new 
XmlQualifiedName (h.Name, h.Namespace))) {
                                                string id = GenerateId (doc);
                                                h.Id = id;
@@ -481,6 +491,43 @@
                                sxml.KeyInfo = new KeyInfo ();
                                sxml.KeyInfo.AddClause (sigKeyInfo);
 
+                               if (!signatureProtection)
+                                       header.Contents.Add (sig);
+
+                               // endorse the signature with (signed)endorsing
+                               // supporting tokens.
+
+                               foreach (SupportingTokenInfo tinfo in 
tokenInfos) {
+                                       switch (tinfo.Mode) {
+                                       case 
SecurityTokenAttachmentMode.Endorsing:
+                                       case 
SecurityTokenAttachmentMode.SignedEndorsing:
+                                               if (sxml.Signature.Id == null) {
+                                                       sig.Id = GenerateId 
(doc);
+                                                       secElem.AppendChild 
(sxml.GetXml ());
+                                               }
+                                               WSSignedXml ssxml = new 
WSSignedXml (doc);
+                                               
ssxml.Signature.SignedInfo.CanonicalizationMethod = 
suite.DefaultCanonicalizationAlgorithm;
+                                               CreateReference 
(ssxml.Signature, doc, sig.Id);
+                                               SecurityToken sst = tinfo.Token;
+                                               SecurityKey ssk = 
sst.SecurityKeys [0]; // FIXME: could be different?
+                                               SecurityKeyIdentifierClause 
tclause = new LocalIdKeyIdentifierClause (sst.Id); // FIXME: could be different?
+                                               if (ssk is 
SymmetricSecurityKey) {
+                                                       SymmetricSecurityKey 
signKey = (SymmetricSecurityKey) ssk;
+                                                       ssxml.ComputeSignature 
(signKey.GetKeyedHashAlgorithm (suite.DefaultSymmetricSignatureAlgorithm));
+                                               } else {
+                                                       AsymmetricSecurityKey 
signKey = (AsymmetricSecurityKey) ssk;
+                                                       ssxml.SigningKey = 
signKey.GetAsymmetricAlgorithm (suite.DefaultAsymmetricSignatureAlgorithm, 
true);
+                                                       ssxml.ComputeSignature 
();
+                                               }
+                                               ssxml.KeyInfo.AddClause (new 
SecurityTokenReferenceKeyInfo (tclause, serializer, doc));
+                                               if (!signatureProtection)
+                                                       header.Contents.Add 
(ssxml.Signature);
+                                               endorsedSignatures.Add (ssxml);
+
+                                               break;
+                                       }
+                               }
+
                                // encrypt
 
                                WSEncryptedXml exml = new WSEncryptedXml (doc);
@@ -489,9 +536,16 @@
                                EncryptedXml.ReplaceElement (body, edata, 
false);
 
                                // encrypt signature
-                               if (protectionOrder == 
MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature) {
+                               if (signatureProtection) {
                                        XmlElement sigxml = sig.GetXml ();
-                                       sigenc = Encrypt (sigxml, actualKey, 
actualToken.Id, refList, actualClause, exml, doc);
+                                       EncryptedData sigenc = Encrypt (sigxml, 
actualKey, actualToken.Id, refList, actualClause, exml, doc);
+                                       header.Contents.Add (sigenc);
+
+                                       foreach (WSSignedXml ssxml in 
endorsedSignatures) {
+                                               sigxml = ssxml.GetXml ();
+                                               sigenc = Encrypt (sigxml, 
actualKey, actualToken.Id, refList, actualClause, exml, doc);
+                                               header.Contents.Add (sigenc);
+                                       }
                                }
 
                                // encrypt Encrypted supporting tokens
@@ -507,11 +561,6 @@
                                break;
                        }
 
-                       if (includeSigToken)
-                               header.Contents.Add (signToken);
-                       //if (signToken != encToken && includeEncToken)
-                       //      header.Contents.Add (encToken);
-
                        Message ret = Message.CreateMessage (msg.Version, 
msg.Headers.Action, new XmlNodeReader (doc.SelectSingleNode 
("/s:Envelope/s:Body/*", nsmgr) as XmlElement));
                        ret.Properties.Security = (SecurityMessageProperty) 
secprop.CreateCopy ();
                        ret.Properties.Security.EncryptionKey = masterKey.Key;
@@ -528,7 +577,7 @@
                        ret.Headers.Clear ();
                        ret.Headers.CopyHeadersFrom (msg);
 
-                       // FIXME: Header contents should be:
+                       // Header contents are:
                        //      - Timestamp
                        //      - SignatureConfirmation if required
                        //      - EncryptionToken if included
@@ -551,15 +600,6 @@
                        //        endorsing token.
                        //      
 
-                       // When we do not output EncryptedKey, output 
ReferenceList here.
-                       if (!ShouldOutputEncryptedKey)
-                               header.Contents.Add 
(primaryToken.ReferenceList);
-
-                       if (sigenc != null) // [Signature Protection]
-                               header.Contents.Add (sigenc);
-                       else if (sig != null) // ![Signature Protection]
-                               header.Contents.Add (sig);
-
 //MessageBuffer zzz = ret.CreateBufferedCopy (100000);
 //ret = zzz.CreateMessage ();
 //Console.WriteLine (zzz.CreateMessage ());
@@ -568,17 +608,23 @@
 
                void CreateReference (Signature sig, XmlElement el, string id)
                {
+                       CreateReference (sig, el.OwnerDocument, id);
+
+                       if (el.GetAttribute ("Id", Constants.WsuNamespace) != 
id) {
+                               XmlAttribute a = el.SetAttributeNode ("Id", 
Constants.WsuNamespace);
+                               a.Prefix = "u";
+                               a.Value = id;
+                       }
+               }
+
+               void CreateReference (Signature sig, XmlDocument doc, string id)
+               {
                        SecurityAlgorithmSuite suite = 
security.Element.DefaultAlgorithmSuite;
                        if (id == String.Empty)
-                               id = GenerateId (el.OwnerDocument);
+                               id = GenerateId (doc);
                        Reference r = new Reference ("#" + id);
                        r.AddTransform (CreateTransform 
(suite.DefaultCanonicalizationAlgorithm));
                        r.DigestMethod = suite.DefaultDigestAlgorithm;
-                       if (el.GetAttribute ("Id", Constants.WsuNamespace) != 
id) {
-                               XmlAttribute a = el.SetAttributeNode ("Id", 
Constants.WsuNamespace);
-                               a.Prefix = "u";
-                               a.Value = id;
-                       }
                        sig.SignedInfo.AddReference (r);
                }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to