2 // KeyIdentifier.cs: Handles WS-Security KeyIdentifier
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System.Security.Cryptography.Xml;
13 using Microsoft.Web.Services;
14 using Microsoft.Web.Services.Security.X509;
16 using Microsoft.Web.Services.Xml;
19 namespace Microsoft.Web.Services.Security {
21 public class KeyIdentifier : IXmlElement {
23 private byte[] kivalue;
24 private XmlQualifiedName vtype;
25 static private char[] separator = { ':' };
27 public KeyIdentifier (byte[] identifier)
29 if (identifier == null)
30 throw new ArgumentNullException ("identifier");
31 kivalue = (byte[]) identifier.Clone ();
34 public KeyIdentifier (XmlElement element)
39 public KeyIdentifier (byte[] identifier, XmlQualifiedName valueType)
41 if (identifier == null)
42 throw new ArgumentNullException ("identifier");
43 kivalue = (byte[]) identifier.Clone ();
48 get { return (byte[]) kivalue.Clone (); }
51 throw new ArgumentNullException ("value");
56 public XmlQualifiedName ValueType {
58 set { vtype = value; }
61 public XmlElement GetXml (XmlDocument document)
64 throw new ArgumentNullException ("document");
66 XmlElement ki = document.CreateElement (WSSecurity.Prefix, WSSecurity.ElementNames.KeyIdentifier, WSSecurity.NamespaceURI);
67 ki.InnerText = Convert.ToBase64String (kivalue);
68 if ((vtype != null) && (!vtype.IsEmpty)) {
69 string ns = ki.GetPrefixOfNamespace (vtype.Namespace);
70 if ((ns == null) || (ns == String.Empty)) {
72 XmlAttribute nsa = document.CreateAttribute ("xmlns:vt");
73 nsa.InnerText = vtype.Namespace;
74 ki.Attributes.Append (nsa);
76 XmlAttribute vt = document.CreateAttribute (WSSecurity.AttributeNames.ValueType);
77 vt.InnerText = String.Concat (ns, ":", vtype.Name);
78 ki.Attributes.Append (vt);
83 public void LoadXml (XmlElement element)
86 throw new ArgumentNullException ("element");
88 if ((element.LocalName != WSSecurity.ElementNames.KeyIdentifier) || (element.NamespaceURI != WSSecurity.NamespaceURI))
89 throw new ArgumentException ("invalid LocalName or NamespaceURI");
92 kivalue = Convert.FromBase64String (element.InnerText);
98 XmlAttribute vt = element.Attributes [WSSecurity.AttributeNames.ValueType];
100 string[] nsvt = vt.InnerText.Split (separator);
101 switch (nsvt.Length) {
103 string ns = element.GetNamespaceOfPrefix (nsvt [0]);
104 vtype = new XmlQualifiedName (nsvt [1], ns);
107 throw new SecurityFormatException ("missing namespace");
112 internal X509Certificate Certificate {
114 if ((vtype.Name == "X509v3") && (vtype.Namespace == WSSecurity.NamespaceURI)) {
115 // TODO - use microsoft.web.service config in .exe.config for store location
116 X509CertificateStore store = X509CertificateStore.LocalMachineStore (X509CertificateStore.MyStore);
117 if (store.OpenRead ()) {
118 X509CertificateCollection coll = store.FindCertificateByKeyIdentifier (kivalue);
119 if ((coll != null) && (coll.Count > 0)) {
129 internal DecryptionKey DecryptionKey {
131 X509Certificate x509 = Certificate;
133 return new AsymmetricDecryptionKey (x509.Key);
139 internal EncryptionKey EncryptionKey {
141 X509Certificate x509 = Certificate;
143 return new AsymmetricEncryptionKey (x509.PublicKey);