2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Security / KeyIdentifier.cs
index 8c58b1a724cf66faf68cab09381b8d1ab5e0e427..984cfc8f6e0a1080568deb5348cc9b3a1123f40c 100644 (file)
-//\r
-// KeyIdentifier.cs: Handles WS-Security KeyIdentifier\r
-//\r
-// Author:\r
-//     Sebastien Pouliot (spouliot@motus.com)\r
-//\r
-// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)\r
-//\r
-// Licensed under MIT X11 (see LICENSE) with this specific addition:\r
-//\r
-// \93This source code may incorporate intellectual property owned by Microsoft \r
-// Corporation. Our provision of this source code does not include any licenses\r
-// or any other rights to you under any Microsoft intellectual property. If you\r
-// would like a license from Microsoft (e.g. rebrand, redistribute), you need \r
-// to contact Microsoft directly.\94 \r
-//\r
-\r
-using System;\r
-using System.Security.Cryptography.Xml;\r
-using System.Xml;\r
-using Microsoft.Web.Services;\r
-using Microsoft.Web.Services.Xml;\r
-\r
-namespace Microsoft.Web.Services.Security {\r
-\r
-       public class KeyIdentifier : IXmlElement {\r
-\r
-               private byte[] kivalue;\r
-               private XmlQualifiedName vtype;\r
-\r
-               public KeyIdentifier (byte[] identifier) \r
-               {\r
-                       if (identifier == null)\r
-                               throw new ArgumentNullException ("identifier");\r
-                       kivalue = (byte[]) identifier.Clone ();\r
-               }\r
-\r
-               public KeyIdentifier (XmlElement element) \r
-               {\r
-                       LoadXml (element);\r
-               }\r
-\r
-               public KeyIdentifier (byte[] identifier, XmlQualifiedName valueType) \r
-               {\r
-                       if (identifier == null)\r
-                               throw new ArgumentNullException ("identifier");\r
-                       kivalue = (byte[]) identifier.Clone ();\r
-                       vtype = valueType;\r
-               }\r
-\r
-               public byte[] Value {\r
-                       get { return (byte[]) kivalue.Clone (); }\r
-                       set {\r
-                               if (value == null)\r
-                                       throw new ArgumentNullException ("value");\r
-                               kivalue = value;\r
-                       }\r
-               }\r
-\r
-               public XmlQualifiedName ValueType {\r
-                       get { return vtype; }\r
-                       set { vtype = value; }\r
-               }\r
-\r
-               public XmlElement GetXml (XmlDocument document) \r
-               {\r
-                       if (document == null)\r
-                               throw new ArgumentNullException ("document");\r
-                       return null;\r
-               }\r
-\r
-               public void LoadXml (XmlElement element) \r
-               {\r
-                       if ((element.LocalName != WSSecurity.ElementNames.KeyIdentifier) || (element.NamespaceURI != WSSecurity.NamespaceURI))\r
-                               throw new System.ArgumentException ("invalid LocalName or NamespaceURI");\r
-                       // TODO\r
-               }\r
-       }\r
-}\r
+//
+// KeyIdentifier.cs: Handles WS-Security KeyIdentifier
+//
+// Author:
+//     Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using System;
+using System.Security.Cryptography.Xml;
+using System.Xml;
+using Microsoft.Web.Services;
+using Microsoft.Web.Services.Security.X509;
+#if !WSE1
+using Microsoft.Web.Services.Xml;
+#endif
+
+namespace Microsoft.Web.Services.Security {
+
+       public class KeyIdentifier : IXmlElement {
+
+               private byte[] kivalue;
+               private XmlQualifiedName vtype;
+               static private char[] separator = { ':' };
+
+               public KeyIdentifier (byte[] identifier) 
+               {
+                       if (identifier == null)
+                               throw new ArgumentNullException ("identifier");
+                       kivalue = (byte[]) identifier.Clone ();
+               }
+
+               public KeyIdentifier (XmlElement element) 
+               {
+                       LoadXml (element);
+               }
+
+               public KeyIdentifier (byte[] identifier, XmlQualifiedName valueType) 
+               {
+                       if (identifier == null)
+                               throw new ArgumentNullException ("identifier");
+                       kivalue = (byte[]) identifier.Clone ();
+                       vtype = valueType;
+               }
+
+               public byte[] Value {
+                       get { return (byte[]) kivalue.Clone (); }
+                       set {
+                               if (value == null)
+                                       throw new ArgumentNullException ("value");
+                               kivalue = value;
+                       }
+               }
+
+               public XmlQualifiedName ValueType {
+                       get { return vtype; }
+                       set { vtype = value; }
+               }
+
+               public XmlElement GetXml (XmlDocument document) 
+               {
+                       if (document == null)
+                               throw new ArgumentNullException ("document");
+
+                       XmlElement ki = document.CreateElement (WSSecurity.Prefix, WSSecurity.ElementNames.KeyIdentifier, WSSecurity.NamespaceURI);
+                       ki.InnerText = Convert.ToBase64String (kivalue);
+                       if ((vtype != null) && (!vtype.IsEmpty)) {
+                               string ns = ki.GetPrefixOfNamespace (vtype.Namespace);
+                               if ((ns == null) || (ns == String.Empty)) {
+                                       ns = "vt";
+                                       XmlAttribute nsa = document.CreateAttribute ("xmlns:vt");
+                                       nsa.InnerText = vtype.Namespace;
+                                       ki.Attributes.Append (nsa);
+                               }
+                               XmlAttribute vt = document.CreateAttribute (WSSecurity.AttributeNames.ValueType);
+                               vt.InnerText = String.Concat (ns, ":", vtype.Name);
+                               ki.Attributes.Append (vt);
+                       }
+                       return ki;
+               }
+
+               public void LoadXml (XmlElement element) 
+               {
+                       if (element == null)
+                               throw new ArgumentNullException ("element");
+
+                       if ((element.LocalName != WSSecurity.ElementNames.KeyIdentifier) || (element.NamespaceURI != WSSecurity.NamespaceURI))
+                               throw new ArgumentException ("invalid LocalName or NamespaceURI");
+
+                       try {
+                               kivalue = Convert.FromBase64String (element.InnerText);
+                       }
+                       catch {
+                               kivalue = null;
+                       }
+
+                       XmlAttribute vt = element.Attributes [WSSecurity.AttributeNames.ValueType];
+                       if (vt != null) {
+                               string[] nsvt = vt.InnerText.Split (separator);
+                               switch (nsvt.Length) {
+                                       case 2:
+                                               string ns = element.GetNamespaceOfPrefix (nsvt [0]);
+                                               vtype = new XmlQualifiedName (nsvt [1], ns);
+                                               break;
+                                       default:
+                                               throw new SecurityFormatException ("missing namespace");
+                               }
+                       }
+               }
+
+               internal X509Certificate Certificate {
+                       get {
+                               if ((vtype.Name == "X509v3") && (vtype.Namespace == WSSecurity.NamespaceURI)) {
+                                       // TODO - use microsoft.web.service config in .exe.config for store location
+                                       X509CertificateStore store = X509CertificateStore.LocalMachineStore (X509CertificateStore.MyStore);
+                                       if (store.OpenRead ()) {
+                                               X509CertificateCollection coll = store.FindCertificateByKeyIdentifier (kivalue);
+                                               if ((coll != null) && (coll.Count > 0)) {
+                                                       return coll [0];
+                                               }
+                                               store.Close ();
+                                       }
+                               }
+                               return null;
+                       }
+               }
+
+               internal DecryptionKey DecryptionKey {
+                       get {
+                               X509Certificate x509 = Certificate;
+                               if (x509 != null) {
+                                       return new AsymmetricDecryptionKey (x509.Key);
+                               }
+                               return null;
+                       }
+               }
+
+               internal EncryptionKey EncryptionKey {
+                       get {
+                               X509Certificate x509 = Certificate;
+                               if (x509 != null) {
+                                       return new AsymmetricEncryptionKey (x509.PublicKey);
+                               }
+                               return null;
+                       }
+               }
+       }
+}