Merge pull request #5273 from vladimir-kazakov/xmlenc-from-corefx

XMLENC from .NET Core.
This commit is contained in:
Marek Safar 2017-11-26 22:36:57 +01:00 коммит произвёл GitHub
Родитель e51565c7e5 46c997fdf7
Коммит 5f7452f384
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
28 изменённых файлов: 117 добавлений и 3440 удалений

2
external/api-snapshot поставляемый

@ -1 +1 @@
Subproject commit 9cd0c7747d64731761bd936cace5792cb319effd
Subproject commit f01089a251d583510161dbc627eeff730c16c57e

Просмотреть файл

@ -1,146 +0,0 @@
//
// CipherData.cs - CipherData implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-CipherData
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Security.Cryptography;
using System.IO;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class CipherData {
#region Fields
byte[] cipherValue;
CipherReference cipherReference;
#endregion // Fields
#region Constructors
public CipherData ()
{
}
public CipherData (byte[] cipherValue)
{
CipherValue = cipherValue;
}
public CipherData (CipherReference cipherReference)
{
CipherReference = cipherReference;
}
#endregion // Constructors
#region Properties
public CipherReference CipherReference {
get { return cipherReference; }
set {
if (CipherValue != null)
throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
cipherReference = value;
}
}
public byte[] CipherValue {
get { return cipherValue; }
set {
if (CipherReference != null)
throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
cipherValue = value;
}
}
#endregion // Properties
#region Methods
public XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal XmlElement GetXml (XmlDocument document)
{
if (CipherReference == null && CipherValue == null)
throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, EncryptedXml.XmlEncNamespaceUrl);
if (CipherReference != null)
xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
if (CipherValue != null) {
XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, EncryptedXml.XmlEncNamespaceUrl);
StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
xcv.InnerText = reader.ReadToEnd ();
reader.Close ();
xel.AppendChild (xcv);
}
return xel;
}
public void LoadXml (XmlElement value)
{
CipherReference = null;
CipherValue = null;
if (value == null)
throw new ArgumentNullException ("value");
if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
throw new CryptographicException ("Malformed Cipher Data element.");
else {
foreach (XmlNode n in value.ChildNodes) {
if (n is XmlWhitespace)
continue;
switch (n.LocalName) {
case XmlEncryption.ElementNames.CipherReference:
cipherReference = new CipherReference ();
cipherReference.LoadXml ((XmlElement) n);
break;
case XmlEncryption.ElementNames.CipherValue:
CipherValue = Convert.FromBase64String (n.InnerText);
break;
}
}
if (CipherReference == null && CipherValue == null)
throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
}
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,91 +0,0 @@
//
// CipherReference.cs - CipherReference implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-CipherReference
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class CipherReference : EncryptedReference {
#region Constructors
public CipherReference ()
: base ()
{
}
public CipherReference (string uri)
: base (uri)
{
}
public CipherReference (string uri, TransformChain transformChain)
: base (uri, transformChain)
{
}
#endregion // Constructors
#region Methods
public override XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal override XmlElement GetXml (XmlDocument document)
{
XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherReference, EncryptedXml.XmlEncNamespaceUrl);
xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
if (TransformChain != null && TransformChain.Count > 0) {
XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
foreach (Transform t in TransformChain)
xtr.AppendChild (document.ImportNode (t.GetXml (), true));
xel.AppendChild (xtr);
}
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
throw new CryptographicException ("Malformed CipherReference element.");
base.LoadXml (value);
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,77 +0,0 @@
//
// DSAKeyValue.cs - DSA KeyValue implementation for XML Signature
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class DSAKeyValue : KeyInfoClause {
private DSA dsa;
public DSAKeyValue ()
{
dsa = (DSA)DSA.Create ();
}
public DSAKeyValue (DSA key)
{
dsa = key;
}
public DSA Key
{
get { return dsa; }
set { dsa = value; }
}
public override XmlElement GetXml ()
{
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyValue, XmlSignature.NamespaceURI);
xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
xel.InnerXml = dsa.ToXmlString (false);
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ();
if ((value.LocalName != XmlSignature.ElementNames.KeyValue) || (value.NamespaceURI != XmlSignature.NamespaceURI))
throw new CryptographicException ("value");
dsa.FromXmlString (value.InnerXml);
}
}
}

Просмотреть файл

@ -1,168 +0,0 @@
//
// DataObject.cs - DataObject implementation for XML Signature
// http://www.w3.org/2000/09/xmldsig#Object
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
// Atsushi Enomoto (atsushi@ximian.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
// XmlElement part of the signature
// Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
// required for "enveloping signatures"
public class DataObject {
private XmlElement element;
private bool propertyModified;
public DataObject ()
{
Build (null, null, null, null);
}
public DataObject (string id, string mimeType, string encoding, XmlElement data)
{
if (data == null)
throw new ArgumentNullException ("data");
Build (id, mimeType, encoding, data);
}
// this one accept a null "data" parameter
private void Build (string id, string mimeType, string encoding, XmlElement data)
{
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
if (id != null) {
xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
}
if (mimeType != null) {
xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
}
if (encoding != null) {
xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
}
if (data != null) {
XmlNode newNode = document.ImportNode (data, true);
xel.AppendChild (newNode);
}
element = xel;
}
// why is data a XmlNodeList instead of a XmlElement ?
public XmlNodeList Data {
get {
return element.ChildNodes;
}
set {
if (value == null)
throw new ArgumentNullException ("value");
XmlDocument doc = new XmlDocument ();
XmlElement el = (XmlElement) doc.ImportNode (element, true);
while (el.LastChild != null)
el.RemoveChild (el.LastChild);
foreach (XmlNode n in value)
el.AppendChild (doc.ImportNode (n, true));
element = el;
propertyModified = true;
}
}
// default to null - no encoding
public string Encoding {
get { return GetField (XmlSignature.AttributeNames.Encoding); }
set { SetField (XmlSignature.AttributeNames.Encoding, value); }
}
// default to null
public string Id {
get { return GetField (XmlSignature.AttributeNames.Id); }
set { SetField (XmlSignature.AttributeNames.Id, value); }
}
// default to null
public string MimeType {
get { return GetField (XmlSignature.AttributeNames.MimeType); }
set { SetField (XmlSignature.AttributeNames.MimeType, value); }
}
private string GetField (string attribute)
{
XmlNode attr = element.Attributes [attribute];
return attr != null ? attr.Value : null;
}
private void SetField (string attribute, string value)
{
// MS-BUGS: it never cleans attribute value up.
if (value == null)
return;
if (propertyModified)
element.SetAttribute (attribute, value);
else {
XmlDocument document = new XmlDocument ();
XmlElement el = document.ImportNode (element, true) as XmlElement;
el.SetAttribute (attribute, value);
element = el;
propertyModified = true;
}
}
public XmlElement GetXml ()
{
if (propertyModified) {
// It looks MS.NET returns element which comes from new XmlDocument every time
XmlElement oldElement = element;
XmlDocument doc = new XmlDocument ();
element = doc.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
foreach (XmlAttribute attribute in oldElement.Attributes) {
switch (attribute.Name) {
case XmlSignature.AttributeNames.Id:
case XmlSignature.AttributeNames.Encoding:
case XmlSignature.AttributeNames.MimeType:
element.SetAttribute (attribute.Name, attribute.Value);
break;
}
}
foreach (XmlNode n in oldElement.ChildNodes)
element.AppendChild (doc.ImportNode (n, true));
}
return element;
}
public void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
element = value;
propertyModified = false;
}
}
}

Просмотреть файл

@ -1,60 +0,0 @@
//
// DataReference.cs - DataReference implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-ReferenceList
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class DataReference : EncryptedReference {
#region Constructors
public DataReference ()
: base ()
{
ReferenceType = XmlEncryption.ElementNames.DataReference;
}
public DataReference (string uri)
: base (uri)
{
ReferenceType = XmlEncryption.ElementNames.DataReference;
}
public DataReference (string uri, TransformChain transformChain)
: base (uri, transformChain)
{
ReferenceType = XmlEncryption.ElementNames.DataReference;
}
#endregion // Constructors
}
}

Просмотреть файл

@ -1,140 +0,0 @@
//
// EncryptedData.cs - EncryptedData implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptedData
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Security.Cryptography.X509Certificates;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class EncryptedData : EncryptedType {
#region Constructors
public EncryptedData ()
{
}
#endregion // Constructors
#region Methods
public override XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal XmlElement GetXml (XmlDocument document)
{
if (CipherData == null)
throw new CryptographicException ("Cipher data is not specified.");
XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
if (EncryptionMethod != null)
xel.AppendChild (EncryptionMethod.GetXml (document));
if (KeyInfo != null)
xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
if (CipherData != null)
xel.AppendChild (CipherData.GetXml (document));
if (EncryptionProperties.Count > 0) {
XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
foreach (EncryptionProperty p in EncryptionProperties)
xep.AppendChild (p.GetXml (document));
xel.AppendChild (xep);
}
if (Id != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
if (Type != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
if (MimeType != null)
xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
if (Encoding != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
throw new CryptographicException ("Malformed EncryptedData element.");
else {
EncryptionMethod = null;
EncryptionMethod = null;
EncryptionProperties.Clear ();
Id = null;
Type = null;
MimeType = null;
Encoding = null;
foreach (XmlNode n in value.ChildNodes) {
if (n is XmlWhitespace)
continue;
switch (n.LocalName) {
case XmlEncryption.ElementNames.EncryptionMethod:
EncryptionMethod = new EncryptionMethod ();
EncryptionMethod.LoadXml ((XmlElement) n);
break;
case XmlSignature.ElementNames.KeyInfo:
KeyInfo = new KeyInfo ();
KeyInfo.LoadXml ((XmlElement) n);
break;
case XmlEncryption.ElementNames.CipherData:
CipherData = new CipherData ();
CipherData.LoadXml ((XmlElement) n);
break;
case XmlEncryption.ElementNames.EncryptionProperties:
foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
EncryptionProperties.Add (new EncryptionProperty (element));
break;
}
}
if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
}
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,217 +0,0 @@
//
// EncryptedKey.cs - EncryptedKey implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptedKey
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class EncryptedKey : EncryptedType {
#region Fields
string carriedKeyName;
string recipient;
ReferenceList referenceList;
#endregion // Fields
#region Constructors
public EncryptedKey ()
{
referenceList = new ReferenceList ();
}
#endregion // Constructors
#region Properties
public string CarriedKeyName {
get { return carriedKeyName; }
set { carriedKeyName = value; }
}
public string Recipient {
get { return recipient; }
set { recipient = value; }
}
public ReferenceList ReferenceList {
get { return referenceList; }
}
#endregion // Properties
#region Methods
public void AddReference (DataReference dataReference)
{
ReferenceList.Add (dataReference);
}
public void AddReference (KeyReference keyReference)
{
ReferenceList.Add (keyReference);
}
public override XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal XmlElement GetXml (XmlDocument document)
{
if (CipherData == null)
throw new CryptographicException ("Cipher data is not specified.");
XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedKey, EncryptedXml.XmlEncNamespaceUrl);
if (EncryptionMethod != null)
xel.AppendChild (EncryptionMethod.GetXml (document));
if (KeyInfo != null)
xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
if (CipherData != null)
xel.AppendChild (CipherData.GetXml (document));
if (EncryptionProperties.Count > 0) {
XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
foreach (EncryptionProperty p in EncryptionProperties)
xep.AppendChild (p.GetXml (document));
xel.AppendChild (xep);
}
if (ReferenceList.Count > 0) {
XmlElement xrl = document.CreateElement (XmlEncryption.ElementNames.ReferenceList, EncryptedXml.XmlEncNamespaceUrl);
foreach (EncryptedReference er in ReferenceList)
xrl.AppendChild (er.GetXml (document));
xel.AppendChild (xrl);
}
if (CarriedKeyName != null) {
XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
xck.InnerText = CarriedKeyName;
xel.AppendChild (xck);
}
if (Id != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
if (Type != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
if (MimeType != null)
xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
if (Encoding != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
if (Recipient != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Recipient, Recipient);
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
throw new CryptographicException ("Malformed EncryptedKey element.");
else {
EncryptionMethod = null;
EncryptionMethod = null;
EncryptionProperties.Clear ();
ReferenceList.Clear ();
CarriedKeyName = null;
Id = null;
Type = null;
MimeType = null;
Encoding = null;
Recipient = null;
foreach (XmlNode n in value.ChildNodes) {
if (n is XmlWhitespace)
continue;
switch (n.LocalName) {
case XmlEncryption.ElementNames.EncryptionMethod:
EncryptionMethod = new EncryptionMethod ();
EncryptionMethod.LoadXml ((XmlElement) n);
break;
case XmlSignature.ElementNames.KeyInfo:
KeyInfo = new KeyInfo ();
KeyInfo.LoadXml ((XmlElement) n);
break;
case XmlEncryption.ElementNames.CipherData:
CipherData = new CipherData ();
CipherData.LoadXml ((XmlElement) n);
break;
case XmlEncryption.ElementNames.EncryptionProperties:
foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
EncryptionProperties.Add (new EncryptionProperty (element));
break;
case XmlEncryption.ElementNames.ReferenceList:
foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
if (r is XmlWhitespace)
continue;
switch (r.LocalName) {
case XmlEncryption.ElementNames.DataReference:
DataReference dr = new DataReference ();
dr.LoadXml ((XmlElement) r);
AddReference (dr);
break;
case XmlEncryption.ElementNames.KeyReference:
KeyReference kr = new KeyReference ();
kr.LoadXml ((XmlElement) r);
AddReference (kr);
break;
}
}
break;
case XmlEncryption.ElementNames.CarriedKeyName:
CarriedKeyName = ((XmlElement) n).InnerText;
break;
}
}
if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
}
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,183 +0,0 @@
//
// EncryptedReference.cs - EncryptedReference implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptedReference
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public abstract class EncryptedReference {
#region Fields
string referenceType;
string uri;
TransformChain tc;
#endregion // Fields
#region Constructors
protected EncryptedReference ()
{
TransformChain = new TransformChain ();
}
protected EncryptedReference (string uri)
{
Uri = uri;
TransformChain = new TransformChain ();
}
protected EncryptedReference (string uri, TransformChain transformChain)
: this ()
{
Uri = uri;
TransformChain = transformChain;
}
#endregion // Constructors
#region Properties
[MonoTODO("Always returns false")]
protected internal bool CacheValid {
get { return false; }
}
protected string ReferenceType {
get { return referenceType; }
set { referenceType = value; }
}
public TransformChain TransformChain {
get { return tc; }
set { tc = value; }
}
public string Uri {
get { return uri; }
set { uri = value; }
}
#endregion // Properties
#region Methods
public void AddTransform (Transform transform)
{
TransformChain.Add (transform);
}
public virtual XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal virtual XmlElement GetXml (XmlDocument document)
{
XmlElement xel = document.CreateElement (ReferenceType, EncryptedXml.XmlEncNamespaceUrl);
xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
if (TransformChain != null && TransformChain.Count > 0) {
XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
foreach (Transform t in TransformChain)
xtr.AppendChild (document.ImportNode (t.GetXml (), true));
xel.AppendChild (xtr);
}
return xel;
}
[MonoTODO ("Make compliant.")]
public virtual void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
else {
Uri = null;
TransformChain = new TransformChain ();
foreach (XmlNode n in value.ChildNodes) {
if (n is XmlWhitespace)
continue;
switch (n.LocalName) {
case XmlEncryption.ElementNames.Transforms:
foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
Transform t = null;
switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
case SignedXml.XmlDsigBase64TransformUrl:
t = new XmlDsigBase64Transform ();
break;
case SignedXml.XmlDsigC14NTransformUrl:
t = new XmlDsigC14NTransform ();
break;
case SignedXml.XmlDsigC14NWithCommentsTransformUrl:
t = new XmlDsigC14NWithCommentsTransform ();
break;
case SignedXml.XmlDsigEnvelopedSignatureTransformUrl:
t = new XmlDsigEnvelopedSignatureTransform ();
break;
case SignedXml.XmlDsigXPathTransformUrl:
t = new XmlDsigXPathTransform ();
break;
case SignedXml.XmlDsigXsltTransformUrl:
t = new XmlDsigXsltTransform ();
break;
case SignedXml.XmlDsigExcC14NTransformUrl:
t = new XmlDsigExcC14NTransform ();
break;
case SignedXml.XmlDsigExcC14NWithCommentsTransformUrl:
t = new XmlDsigExcC14NWithCommentsTransform ();
break;
case SignedXml.XmlDecryptionTransformUrl:
t = new XmlDecryptionTransform ();
break;
default:
continue;
}
t.LoadInnerXml (((XmlElement) xn).ChildNodes);
TransformChain.Add (t);
}
break;
}
}
if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
}
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,117 +0,0 @@
//
// EncryptedType.cs - EncryptedType implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptedType
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public abstract class EncryptedType {
#region Fields
CipherData cipherData;
string encoding;
EncryptionMethod encryptionMethod;
EncryptionPropertyCollection encryptionProperties;
string id;
KeyInfo keyInfo;
string mimeType;
string type;
#endregion // Fields
#region Constructors
protected EncryptedType ()
{
cipherData = new CipherData ();
encryptionProperties = new EncryptionPropertyCollection ();
keyInfo = new KeyInfo ();
}
#endregion // Constructors
#region Properties
public virtual CipherData CipherData {
get { return cipherData; }
set { cipherData = value; }
}
public virtual string Encoding {
get { return encoding; }
set { encoding = value; }
}
public virtual EncryptionMethod EncryptionMethod {
get { return encryptionMethod; }
set { encryptionMethod = value; }
}
public virtual EncryptionPropertyCollection EncryptionProperties {
get { return encryptionProperties; }
}
public virtual string Id {
get { return id; }
set { id = value; }
}
public KeyInfo KeyInfo {
get { return keyInfo; }
set { keyInfo = value; }
}
public virtual string MimeType {
get { return mimeType; }
set { mimeType = value; }
}
public virtual string Type {
get { return type; }
set { type = value; }
}
#endregion // Properties
#region Methods
public void AddProperty (EncryptionProperty ep)
{
EncryptionProperties.Add (ep);
}
public abstract XmlElement GetXml ();
public abstract void LoadXml (XmlElement value);
#endregion // Methods
}
}

Просмотреть файл

@ -1,507 +0,0 @@
//
// EncryptedXml.cs - EncryptedXml implementation for XML Encryption
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Policy;
using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class EncryptedXml {
#region Fields
public const string XmlEncAES128KeyWrapUrl = XmlEncNamespaceUrl + "kw-aes128";
public const string XmlEncAES128Url = XmlEncNamespaceUrl + "aes128-cbc";
public const string XmlEncAES192KeyWrapUrl = XmlEncNamespaceUrl + "kw-aes192";
public const string XmlEncAES192Url = XmlEncNamespaceUrl + "aes192-cbc";
public const string XmlEncAES256KeyWrapUrl = XmlEncNamespaceUrl + "kw-aes256";
public const string XmlEncAES256Url = XmlEncNamespaceUrl + "aes256-cbc";
public const string XmlEncDESUrl = XmlEncNamespaceUrl + "des-cbc";
public const string XmlEncElementContentUrl = XmlEncNamespaceUrl + "Content";
public const string XmlEncElementUrl = XmlEncNamespaceUrl + "Element";
public const string XmlEncEncryptedKeyUrl = XmlEncNamespaceUrl + "EncryptedKey";
public const string XmlEncNamespaceUrl = "http://www.w3.org/2001/04/xmlenc#";
public const string XmlEncRSA15Url = XmlEncNamespaceUrl + "rsa-1_5";
public const string XmlEncRSAOAEPUrl = XmlEncNamespaceUrl + "rsa-oaep-mgf1p";
public const string XmlEncSHA256Url = XmlEncNamespaceUrl + "sha256";
public const string XmlEncSHA512Url = XmlEncNamespaceUrl + "sha512";
public const string XmlEncTripleDESKeyWrapUrl = XmlEncNamespaceUrl + "kw-tripledes";
public const string XmlEncTripleDESUrl = XmlEncNamespaceUrl + "tripledes-cbc";
Evidence documentEvidence;
Encoding encoding = Encoding.UTF8;
internal Hashtable keyNameMapping = new Hashtable ();
CipherMode mode = CipherMode.CBC;
PaddingMode padding = PaddingMode.ISO10126;
string recipient;
XmlResolver resolver;
XmlDocument document;
#endregion // Fields
#region Constructors
[MonoTODO]
public EncryptedXml ()
{
}
[MonoTODO]
public EncryptedXml (XmlDocument document)
{
this.document = document;
}
[MonoTODO]
public EncryptedXml (XmlDocument document, Evidence evidence)
{
this.document = document;
DocumentEvidence = evidence;
}
#endregion // Constructors
#region Properties
public Evidence DocumentEvidence {
get { return documentEvidence; }
set { documentEvidence = value; }
}
public Encoding Encoding {
get { return encoding; }
set { encoding = value; }
}
public CipherMode Mode {
get { return mode; }
set { mode = value; }
}
public PaddingMode Padding {
get { return padding; }
set { padding = value; }
}
public string Recipient {
get { return recipient; }
set { recipient = value; }
}
public XmlResolver Resolver {
get { return resolver; }
set { resolver = value; }
}
#endregion // Properties
#region Methods
public void AddKeyNameMapping (string keyName, object keyObject)
{
keyNameMapping [keyName] = keyObject;
}
public void ClearKeyNameMappings ()
{
keyNameMapping.Clear ();
}
public byte[] DecryptData (EncryptedData encryptedData, SymmetricAlgorithm symmetricAlgorithm)
{
if (encryptedData == null)
throw new ArgumentNullException ("encryptedData");
if (symmetricAlgorithm == null)
throw new ArgumentNullException ("symmetricAlgorithm");
PaddingMode bak = symmetricAlgorithm.Padding;
try {
symmetricAlgorithm.Padding = Padding;
return Transform (encryptedData.CipherData.CipherValue, symmetricAlgorithm.CreateDecryptor (), symmetricAlgorithm.BlockSize / 8, true);
} finally {
symmetricAlgorithm.Padding = bak;
}
}
public void DecryptDocument ()
{
XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", XmlEncNamespaceUrl);
foreach (XmlNode node in nodes) {
EncryptedData encryptedData = new EncryptedData ();
encryptedData.LoadXml ((XmlElement) node);
SymmetricAlgorithm symAlg = GetDecryptionKey (encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
ReplaceData ((XmlElement) node, DecryptData (encryptedData, symAlg));
}
}
public virtual byte[] DecryptEncryptedKey (EncryptedKey encryptedKey)
{
if (encryptedKey == null)
throw new ArgumentNullException ("encryptedKey");
object keyAlg = null;
foreach (KeyInfoClause innerClause in encryptedKey.KeyInfo) {
if (innerClause is KeyInfoName) {
keyAlg = keyNameMapping [((KeyInfoName) innerClause).Value];
break;
}
}
switch (encryptedKey.EncryptionMethod.KeyAlgorithm) {
case XmlEncRSA15Url:
return DecryptKey (encryptedKey.CipherData.CipherValue, (RSA) keyAlg, false);
case XmlEncRSAOAEPUrl:
return DecryptKey (encryptedKey.CipherData.CipherValue, (RSA) keyAlg, true);
}
return DecryptKey (encryptedKey.CipherData.CipherValue, (SymmetricAlgorithm) keyAlg);
}
public static byte[] DecryptKey (byte[] keyData, SymmetricAlgorithm symmetricAlgorithm)
{
if (keyData == null)
throw new ArgumentNullException ("keyData");
if (symmetricAlgorithm == null)
throw new ArgumentNullException ("symmetricAlgorithm");
if (symmetricAlgorithm is TripleDES)
return SymmetricKeyWrap.TripleDESKeyWrapDecrypt (symmetricAlgorithm.Key, keyData);
if (symmetricAlgorithm is Rijndael)
return SymmetricKeyWrap.AESKeyWrapDecrypt (symmetricAlgorithm.Key, keyData);
throw new CryptographicException ("The specified cryptographic transform is not supported.");
}
[MonoTODO ("Test this.")]
public static byte[] DecryptKey (byte[] keyData, RSA rsa, bool useOAEP)
{
AsymmetricKeyExchangeDeformatter deformatter = null;
if (useOAEP)
deformatter = new RSAOAEPKeyExchangeDeformatter (rsa);
else
deformatter = new RSAPKCS1KeyExchangeDeformatter (rsa);
return deformatter.DecryptKeyExchange (keyData);
}
public EncryptedData Encrypt (XmlElement inputElement, string keyName)
{
// There are two keys of note here.
// 1) KeyAlg: the key-encryption-key is used to wrap a key. The keyName
// parameter will give us the KEK.
// 2) SymAlg: A 256-bit AES key will be generated to encrypt the contents.
// This key will be wrapped using the KEK.
SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
symAlg.KeySize = 256;
symAlg.GenerateKey ();
symAlg.GenerateIV ();
EncryptedData encryptedData = new EncryptedData ();
EncryptedKey encryptedKey = new EncryptedKey();
object keyAlg = keyNameMapping [keyName];
encryptedKey.EncryptionMethod = new EncryptionMethod (GetKeyWrapAlgorithmUri (keyAlg));
if (keyAlg is RSA)
encryptedKey.CipherData = new CipherData (EncryptKey (symAlg.Key, (RSA) keyAlg, false));
else
encryptedKey.CipherData = new CipherData (EncryptKey (symAlg.Key, (SymmetricAlgorithm) keyAlg));
encryptedKey.KeyInfo = new KeyInfo();
encryptedKey.KeyInfo.AddClause (new KeyInfoName (keyName));
encryptedData.Type = XmlEncElementUrl;
encryptedData.EncryptionMethod = new EncryptionMethod (GetAlgorithmUri (symAlg));
encryptedData.KeyInfo = new KeyInfo ();
encryptedData.KeyInfo.AddClause (new KeyInfoEncryptedKey (encryptedKey));
encryptedData.CipherData = new CipherData (EncryptData (inputElement, symAlg, false));
return encryptedData;
}
[MonoTODO]
public EncryptedData Encrypt (XmlElement inputElement, X509Certificate2 certificate)
{
throw new NotImplementedException ();
}
public byte[] EncryptData (byte[] plaintext, SymmetricAlgorithm symmetricAlgorithm)
{
if (plaintext == null)
throw new ArgumentNullException ("plaintext");
if (symmetricAlgorithm == null)
throw new ArgumentNullException ("symmetricAlgorithm");
PaddingMode bak = symmetricAlgorithm.Padding;
try {
symmetricAlgorithm.Padding = Padding;
return EncryptDataCore (plaintext, symmetricAlgorithm);
} finally {
symmetricAlgorithm.Padding = bak;
}
}
byte[] EncryptDataCore (byte[] plainText, SymmetricAlgorithm symAlg)
{
// Write the symmetric algorithm IV and ciphertext together.
// We use a memory stream to accomplish this.
MemoryStream stream = new MemoryStream ();
BinaryWriter writer = new BinaryWriter (stream);
writer.Write (symAlg.IV);
writer.Write (Transform (plainText, symAlg.CreateEncryptor ()));
writer.Flush ();
byte [] output = stream.ToArray ();
writer.Close ();
stream.Close ();
return output;
}
public byte[] EncryptData (XmlElement inputElement, SymmetricAlgorithm symmetricAlgorithm, bool content)
{
if (inputElement == null)
throw new ArgumentNullException ("inputElement");
if (content)
return EncryptData (Encoding.GetBytes (inputElement.InnerXml), symmetricAlgorithm);
else
return EncryptData (Encoding.GetBytes (inputElement.OuterXml), symmetricAlgorithm);
}
public static byte[] EncryptKey (byte[] keyData, SymmetricAlgorithm symmetricAlgorithm)
{
if (keyData == null)
throw new ArgumentNullException ("keyData");
if (symmetricAlgorithm == null)
throw new ArgumentNullException ("symmetricAlgorithm");
if (symmetricAlgorithm is TripleDES)
return SymmetricKeyWrap.TripleDESKeyWrapEncrypt (symmetricAlgorithm.Key, keyData);
if (symmetricAlgorithm is Rijndael)
return SymmetricKeyWrap.AESKeyWrapEncrypt (symmetricAlgorithm.Key, keyData);
throw new CryptographicException ("The specified cryptographic transform is not supported.");
}
[MonoTODO ("Test this.")]
public static byte[] EncryptKey (byte[] keyData, RSA rsa, bool useOAEP)
{
AsymmetricKeyExchangeFormatter formatter = null;
if (useOAEP)
formatter = new RSAOAEPKeyExchangeFormatter (rsa);
else
formatter = new RSAPKCS1KeyExchangeFormatter (rsa);
return formatter.CreateKeyExchange (keyData);
}
private static SymmetricAlgorithm GetAlgorithm (string symAlgUri)
{
SymmetricAlgorithm symAlg = null;
switch (symAlgUri) {
case XmlEncAES128Url:
case XmlEncAES128KeyWrapUrl:
symAlg = SymmetricAlgorithm.Create ("Rijndael");
symAlg.KeySize = 128;
break;
case XmlEncAES192Url:
case XmlEncAES192KeyWrapUrl:
symAlg = SymmetricAlgorithm.Create ("Rijndael");
symAlg.KeySize = 192;
break;
case XmlEncAES256Url:
case XmlEncAES256KeyWrapUrl:
symAlg = SymmetricAlgorithm.Create ("Rijndael");
symAlg.KeySize = 256;
break;
case XmlEncDESUrl:
symAlg = SymmetricAlgorithm.Create ("DES");
break;
case XmlEncTripleDESUrl:
case XmlEncTripleDESKeyWrapUrl:
symAlg = SymmetricAlgorithm.Create ("TripleDES");
break;
default:
throw new CryptographicException ("symAlgUri");
}
return symAlg;
}
private static string GetAlgorithmUri (SymmetricAlgorithm symAlg)
{
if (symAlg is Rijndael)
{
switch (symAlg.KeySize) {
case 128:
return XmlEncAES128Url;
case 192:
return XmlEncAES192Url;
case 256:
return XmlEncAES256Url;
}
}
else if (symAlg is DES)
return XmlEncDESUrl;
else if (symAlg is TripleDES)
return XmlEncTripleDESUrl;
throw new ArgumentException ("symAlg");
}
private static string GetKeyWrapAlgorithmUri (object keyAlg)
{
if (keyAlg is Rijndael)
{
switch (((Rijndael) keyAlg).KeySize) {
case 128:
return XmlEncAES128KeyWrapUrl;
case 192:
return XmlEncAES192KeyWrapUrl;
case 256:
return XmlEncAES256KeyWrapUrl;
}
}
else if (keyAlg is RSA)
return XmlEncRSA15Url;
else if (keyAlg is TripleDES)
return XmlEncTripleDESKeyWrapUrl;
throw new ArgumentException ("keyAlg");
}
public virtual byte[] GetDecryptionIV (EncryptedData encryptedData, string symmetricAlgorithmUri)
{
if (encryptedData == null)
throw new ArgumentNullException ("encryptedData");
SymmetricAlgorithm symAlg = GetAlgorithm (symmetricAlgorithmUri);
byte[] iv = new Byte [symAlg.BlockSize / 8];
Buffer.BlockCopy (encryptedData.CipherData.CipherValue, 0, iv, 0, iv.Length);
return iv;
}
public virtual SymmetricAlgorithm GetDecryptionKey (EncryptedData encryptedData, string symmetricAlgorithmUri)
{
if (encryptedData == null)
throw new ArgumentNullException ("encryptedData");
if (symmetricAlgorithmUri == null)
return null;
SymmetricAlgorithm symAlg = GetAlgorithm (symmetricAlgorithmUri);
symAlg.IV = GetDecryptionIV (encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
KeyInfo keyInfo = encryptedData.KeyInfo;
foreach (KeyInfoClause clause in keyInfo) {
if (clause is KeyInfoEncryptedKey) {
symAlg.Key = DecryptEncryptedKey (((KeyInfoEncryptedKey) clause).EncryptedKey);
break;
}
}
return symAlg;
}
public virtual XmlElement GetIdElement (XmlDocument document, string idValue)
{
if ((document == null) || (idValue == null))
return null;
// this works only if there's a DTD or XSD available to define the ID
XmlElement xel = document.GetElementById (idValue);
if (xel == null) {
// search an "undefined" ID
xel = (XmlElement) document.SelectSingleNode ("//*[@Id='" + idValue + "']");
}
return xel;
}
public void ReplaceData (XmlElement inputElement, byte[] decryptedData)
{
if (inputElement == null)
throw new ArgumentNullException ("inputElement");
if (decryptedData == null)
throw new ArgumentNullException ("decryptedData");
XmlDocument ownerDocument = inputElement.OwnerDocument;
XmlTextReader reader = new XmlTextReader (new StringReader (Encoding.GetString (decryptedData, 0, decryptedData.Length)));
reader.MoveToContent ();
XmlNode node = ownerDocument.ReadNode (reader);
inputElement.ParentNode.ReplaceChild (node, inputElement);
}
public static void ReplaceElement (XmlElement inputElement, EncryptedData encryptedData, bool content)
{
if (inputElement == null)
throw new ArgumentNullException ("inputElement");
if (encryptedData == null)
throw new ArgumentNullException ("encryptedData");
XmlDocument ownerDocument = inputElement.OwnerDocument;
inputElement.ParentNode.ReplaceChild (encryptedData.GetXml (ownerDocument), inputElement);
}
private byte[] Transform (byte[] data, ICryptoTransform transform)
{
return Transform (data, transform, 0, false);
}
private byte[] Transform (byte[] data, ICryptoTransform transform, int blockOctetCount, bool trimPadding)
{
MemoryStream output = new MemoryStream ();
CryptoStream crypto = new CryptoStream (output, transform, CryptoStreamMode.Write);
crypto.Write (data, 0, data.Length);
crypto.FlushFinalBlock ();
// strip padding (see xmlenc spec 5.2)
int trimSize = 0;
if (trimPadding)
trimSize = output.GetBuffer () [output.Length - 1];
// It should not happen, but somehow .NET allows such cipher
// data as if there were no padding.
if (trimSize > blockOctetCount)
trimSize = 0;
byte[] result = new byte [output.Length - blockOctetCount - trimSize];
Array.Copy (output.GetBuffer (), blockOctetCount, result, 0, result.Length);
crypto.Close ();
output.Close ();
return result;
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,123 +0,0 @@
//
// EncryptionMethod.cs - EncryptionMethod implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptionMethod
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class EncryptionMethod {
#region Fields
string algorithm;
int keySize;
#endregion // Fields
#region Constructors
public EncryptionMethod ()
{
KeyAlgorithm = null;
}
public EncryptionMethod (string algorithm)
{
KeyAlgorithm = algorithm;
}
#endregion // Constructors
#region Properties
public string KeyAlgorithm {
get { return algorithm; }
set { algorithm = value; }
}
public int KeySize {
get { return keySize; }
set {
if (value <= 0)
throw new ArgumentOutOfRangeException ("The key size should be a non negative integer.");
keySize = value;
}
}
#endregion // Properties
#region Methods
public XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal XmlElement GetXml (XmlDocument document)
{
XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptionMethod, EncryptedXml.XmlEncNamespaceUrl);
if (KeySize != 0) {
XmlElement xks = document.CreateElement (XmlEncryption.ElementNames.KeySize, EncryptedXml.XmlEncNamespaceUrl);
xks.InnerText = String.Format ("{0}", keySize);
xel.AppendChild (xks);
}
if (KeyAlgorithm != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Algorithm, KeyAlgorithm);
return xel;
}
public void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
if ((value.LocalName != XmlEncryption.ElementNames.EncryptionMethod) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
throw new CryptographicException ("Malformed EncryptionMethod element.");
else {
KeyAlgorithm = null;
foreach (XmlNode n in value.ChildNodes) {
if (n is XmlWhitespace)
continue;
switch (n.LocalName) {
case XmlEncryption.ElementNames.KeySize:
KeySize = Int32.Parse (n.InnerText);
break;
}
}
if (value.HasAttribute (XmlEncryption.AttributeNames.Algorithm))
KeyAlgorithm = value.Attributes [XmlEncryption.AttributeNames.Algorithm].Value;
}
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,175 +0,0 @@
//
// EncryptionProperties.cs - EncryptionProperties implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptionProperties
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.Runtime.CompilerServices;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class EncryptionPropertyCollection : IList, ICollection, IEnumerable {
#region Fields
ArrayList list;
#endregion // Fields
#region Constructors
public EncryptionPropertyCollection ()
{
list = new ArrayList ();
}
#endregion // Constructors
#region Properties
public int Count {
get { return list.Count; }
}
public bool IsFixedSize {
get { return list.IsFixedSize; }
}
public bool IsReadOnly {
get { return list.IsReadOnly; }
}
public bool IsSynchronized {
get { return list.IsSynchronized; }
}
object IList.this [int index] {
get { return this [index]; }
set { this [index] = (EncryptionProperty) value; }
}
[IndexerName ("ItemOf")]
public EncryptionProperty this [int index] {
get { return (EncryptionProperty) list [index]; }
set { list [index] = value; }
}
public object SyncRoot {
get { return list.SyncRoot; }
}
#endregion // Properties
#region Methods
public int Add (EncryptionProperty value)
{
return list.Add (value);
}
public void Clear ()
{
list.Clear ();
}
public bool Contains (EncryptionProperty value)
{
return list.Contains (value);
}
public void CopyTo (Array array, int index)
{
list.CopyTo (array, index);
}
public void CopyTo (EncryptionProperty[] array, int index)
{
list.CopyTo (array, index);
}
public IEnumerator GetEnumerator ()
{
return list.GetEnumerator ();
}
bool IList.Contains (object value)
{
return Contains ((EncryptionProperty) value);
}
int IList.Add (object value)
{
return Add ((EncryptionProperty) value);
}
int IList.IndexOf (object value)
{
return IndexOf ((EncryptionProperty) value);
}
void IList.Insert (int index, object value)
{
Insert (index, (EncryptionProperty) value);
}
void IList.Remove (object value)
{
Remove ((EncryptionProperty) value);
}
public int IndexOf (EncryptionProperty value)
{
return list.IndexOf (value);
}
public void Insert (int index, EncryptionProperty value)
{
list.Insert (index, value);
}
public EncryptionProperty Item (int index)
{
return (EncryptionProperty) list [index];
}
public void Remove (EncryptionProperty value)
{
list.Remove (value);
}
public void RemoveAt (int index)
{
list.RemoveAt (index);
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,112 +0,0 @@
//
// EncryptionProperty.cs - EncryptionProperty implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptionProperty
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class EncryptionProperty {
#region Fields
string id;
string target;
#endregion // Fields
#region Constructors
public EncryptionProperty ()
{
}
public EncryptionProperty (XmlElement elementProperty)
{
LoadXml (elementProperty);
}
#endregion // Constructors
#region Properties
public string Id {
get { return id; }
}
[MonoTODO ("Always returns null")]
public XmlElement PropertyElement {
get { return null; }
set { LoadXml (value); }
}
public string Target {
get { return target; }
}
#endregion // Properties
#region Methods
public XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal XmlElement GetXml (XmlDocument document)
{
XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl);
if (Id != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
if (Target != null)
xel.SetAttribute (XmlEncryption.AttributeNames.Target, Target);
return xel;
}
public void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
if ((value.LocalName != XmlEncryption.ElementNames.EncryptionProperty) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
throw new CryptographicException ("Malformed EncryptionProperty element.");
else {
if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
this.id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
if (value.HasAttribute (XmlEncryption.AttributeNames.Target))
this.target = value.Attributes [XmlEncryption.AttributeNames.Target].Value;
}
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,39 +0,0 @@
//
// System.Security.Cryptography.Xml.IRelDecryptor interface
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.IO;
namespace System.Security.Cryptography.Xml {
public interface IRelDecryptor {
Stream Decrypt (EncryptionMethod encryptionMethod, KeyInfo keyInfo, Stream toDecrypt);
}
}

Просмотреть файл

@ -1,155 +0,0 @@
//
// KeyInfo.cs - Xml Signature KeyInfo implementation
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfo : IEnumerable {
private ArrayList Info;
private string id;
public KeyInfo()
{
Info = new ArrayList ();
}
public int Count {
get { return Info.Count; }
}
public string Id {
get { return id; }
set { id = value; }
}
public void AddClause (KeyInfoClause clause)
{
Info.Add (clause);
}
public IEnumerator GetEnumerator ()
{
return Info.GetEnumerator ();
}
public IEnumerator GetEnumerator (Type requestedObjectType)
{
// Build a new ArrayList...
ArrayList TypeList = new ArrayList ();
IEnumerator e = Info.GetEnumerator ();
while (true) {
// ...with all object of specified type...
if ((e.Current).GetType().Equals (requestedObjectType))
TypeList.Add (e.Current);
if (!e.MoveNext ())
break;
}
// ...and return its enumerator
return TypeList.GetEnumerator ();
}
public XmlElement GetXml ()
{
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI);
// we add References afterward so we don't end up with extraneous
// xmlns="..." in each reference elements.
foreach (KeyInfoClause kic in Info) {
XmlNode xn = kic.GetXml ();
XmlNode newNode = document.ImportNode (xn, true);
xel.AppendChild (newNode);
}
return xel;
}
public void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ("value");
Id = value.Attributes ["Id"] != null ? value.GetAttribute ("Id") : null;
if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
foreach (XmlNode n in value.ChildNodes) {
if (n.NodeType != XmlNodeType.Element)
continue;
KeyInfoClause kic = null;
switch (n.LocalName) {
case XmlSignature.ElementNames.KeyValue:
XmlNodeList xnl = n.ChildNodes;
if (xnl.Count > 0) {
// we must now treat the whitespace !
foreach (XmlNode m in xnl) {
switch (m.LocalName) {
case XmlSignature.ElementNames.DSAKeyValue:
kic = (KeyInfoClause) new DSAKeyValue ();
break;
case XmlSignature.ElementNames.RSAKeyValue:
kic = (KeyInfoClause) new RSAKeyValue ();
break;
}
}
}
break;
case XmlSignature.ElementNames.KeyName:
kic = (KeyInfoClause) new KeyInfoName ();
break;
case XmlSignature.ElementNames.RetrievalMethod:
kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
break;
case XmlSignature.ElementNames.X509Data:
kic = (KeyInfoClause) new KeyInfoX509Data ();
break;
case XmlSignature.ElementNames.RSAKeyValue:
kic = (KeyInfoClause) new RSAKeyValue ();
break;
case XmlSignature.ElementNames.EncryptedKey:
kic = (KeyInfoClause) new KeyInfoEncryptedKey ();
break;
default:
kic = (KeyInfoClause) new KeyInfoNode ();
break;
}
if (kic != null) {
kic.LoadXml ((XmlElement) n);
AddClause (kic);
}
}
}
// No check is performed on MS.NET...
}
}
}

Просмотреть файл

@ -1,44 +0,0 @@
//
// KeyInfoClause.cs - Abstract KeyInfoClause implementation for XML Signature
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2006 Novell Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public abstract class KeyInfoClause {
protected KeyInfoClause ()
{
}
public abstract XmlElement GetXml ();
public abstract void LoadXml (XmlElement element);
}
}

Просмотреть файл

@ -1,90 +0,0 @@
//
// KeyInfoEncryptedKey.cs - KeyInfoEncryptedKey implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-EncryptedKey
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoEncryptedKey : KeyInfoClause {
#region Fields
EncryptedKey encryptedKey;
#endregion // Fields
#region Constructors
public KeyInfoEncryptedKey ()
{
}
public KeyInfoEncryptedKey (EncryptedKey encryptedKey)
{
EncryptedKey = encryptedKey;
}
#endregion // Constructors
#region Properties
public EncryptedKey EncryptedKey {
get { return encryptedKey; }
set { encryptedKey = value; }
}
#endregion // Properties
#region Methods
public override XmlElement GetXml ()
{
return GetXml (new XmlDocument ());
}
internal XmlElement GetXml (XmlDocument document)
{
if (encryptedKey != null)
return encryptedKey.GetXml (document);
return null;
}
public override void LoadXml (XmlElement value)
{
EncryptedKey = new EncryptedKey ();
EncryptedKey.LoadXml (value);
}
#endregion // Methods
}
}

Просмотреть файл

@ -1,73 +0,0 @@
//
// KeyInfoName.cs - KeyInfoName implementation for XML Signature
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
// Tim Coleman (tim@timcoleman.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoName : KeyInfoClause {
private string name;
public KeyInfoName ()
{
}
public KeyInfoName (string keyName)
{
name = keyName;
}
public string Value {
get { return name; }
set { name = value; }
}
public override XmlElement GetXml ()
{
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyName, XmlSignature.NamespaceURI);
xel.InnerText = name;
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ();
if ((value.LocalName != XmlSignature.ElementNames.KeyName) || (value.NamespaceURI != XmlSignature.NamespaceURI))
name = "";
else
name = value.InnerText;
}
}
}

Просмотреть файл

@ -1,62 +0,0 @@
//
// KeyInfoNode.cs - KeyInfoNode implementation for XML Signature
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoNode : KeyInfoClause {
private XmlElement Node;
public KeyInfoNode () {}
public KeyInfoNode (XmlElement node)
{
LoadXml (node);
}
public XmlElement Value {
get { return Node; }
set { Node = value; }
}
public override XmlElement GetXml ()
{
return Node;
}
// LAMESPEC: No ArgumentNullException is thrown if value == null
public override void LoadXml (XmlElement value)
{
Node = value;
}
}
}

Просмотреть файл

@ -1,105 +0,0 @@
//
// KeyInfoRetrievalMethod.cs - KeyInfoRetrievalMethod implementation for XML Signature
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
// Tim Coleman (tim@timcoleman.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Runtime.InteropServices;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoRetrievalMethod : KeyInfoClause {
private string URI;
private XmlElement element;
private string type;
public KeyInfoRetrievalMethod ()
{
}
public KeyInfoRetrievalMethod (string strUri)
{
URI = strUri;
}
public KeyInfoRetrievalMethod (string strUri, string typeName)
: this (strUri)
{
Type = typeName;
}
[ComVisible (false)]
public string Type {
get { return type; }
set {
element = null;
type = value;
}
}
public string Uri {
get { return URI; }
set {
element = null;
URI = value;
}
}
public override XmlElement GetXml ()
{
if (element != null)
return element;
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement (XmlSignature.ElementNames.RetrievalMethod, XmlSignature.NamespaceURI);
if ((URI != null) && (URI.Length > 0))
xel.SetAttribute (XmlSignature.AttributeNames.URI, URI);
if (Type != null)
xel.SetAttribute (XmlSignature.AttributeNames.Type, Type);
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ();
if ((value.LocalName != XmlSignature.ElementNames.RetrievalMethod) || (value.NamespaceURI != XmlSignature.NamespaceURI)) {
URI = ""; // not null - so we return URI="" as attribute !!!
} else {
URI = value.Attributes [XmlSignature.AttributeNames.URI].Value;
if (value.HasAttribute (XmlSignature.AttributeNames.Type))
Type = value.Attributes [XmlSignature.AttributeNames.Type].Value;
element = value;
}
}
}
}

Просмотреть файл

@ -1,60 +0,0 @@
//
// KeyReference.cs - KeyReference implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-ReferenceList
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class KeyReference : EncryptedReference {
#region Constructors
public KeyReference ()
: base ()
{
ReferenceType = XmlEncryption.ElementNames.KeyReference;
}
public KeyReference (string uri)
: base (uri)
{
ReferenceType = XmlEncryption.ElementNames.KeyReference;
}
public KeyReference (string uri, TransformChain transformChain)
: base (uri, transformChain)
{
ReferenceType = XmlEncryption.ElementNames.KeyReference;
}
#endregion // Constructors
}
}

Просмотреть файл

@ -1,75 +0,0 @@
//
// RSAKeyValue.cs - RSAKeyValue implementation for XML Signature
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class RSAKeyValue : KeyInfoClause {
private RSA rsa;
public RSAKeyValue ()
{
rsa = (RSA)RSA.Create ();
}
public RSAKeyValue (RSA key)
{
rsa = key;
}
public RSA Key {
get { return rsa; }
set { rsa = value; }
}
public override XmlElement GetXml ()
{
XmlDocument document = new XmlDocument ();
XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyValue, XmlSignature.NamespaceURI);
xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
xel.InnerXml = rsa.ToXmlString (false);
return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ();
if ((value.LocalName != XmlSignature.ElementNames.KeyValue) || (value.NamespaceURI != XmlSignature.NamespaceURI))
throw new CryptographicException ("value");
rsa.FromXmlString (value.InnerXml);
}
}
}

Просмотреть файл

@ -1,149 +0,0 @@
//
// ReferenceList.cs - ReferenceList implementation for XML Encryption
// http://www.w3.org/2001/04/xmlenc#sec-ReferenceList
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.Runtime.CompilerServices;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public sealed class ReferenceList : IList, ICollection, IEnumerable {
#region Fields
ArrayList list;
#endregion // Fields
#region Constructors
public ReferenceList ()
{
list = new ArrayList ();
}
#endregion // Constructors
#region Properties
public int Count {
get { return list.Count; }
}
object IList.this [int index] {
get { return this [index]; }
set { this [index] = (EncryptedReference) value; }
}
bool IList.IsFixedSize {
get { return false; }
}
bool IList.IsReadOnly {
get { return false; }
}
public bool IsSynchronized {
get { return list.IsSynchronized; }
}
[IndexerName ("ItemOf")]
public EncryptedReference this [int index] {
get { return (EncryptedReference) list [index]; }
set { list [index] = value; }
}
public object SyncRoot {
get { return list.SyncRoot; }
}
#endregion // Properties
#region Methods
public int Add (object value)
{
if (!(value is EncryptedReference))
throw new ArgumentException ("value");
return list.Add (value);
}
public void Clear ()
{
list.Clear ();
}
public bool Contains (object value)
{
return list.Contains (value);
}
public void CopyTo (Array array, int index)
{
list.CopyTo (array, index);
}
public IEnumerator GetEnumerator ()
{
return list.GetEnumerator ();
}
public EncryptedReference Item (int index)
{
return (EncryptedReference) list [index];
}
public int IndexOf (object value)
{
return list.IndexOf (value);
}
public void Insert (int index, object value)
{
if (!(value is EncryptedReference))
throw new ArgumentException ("value");
list.Insert (index, value);
}
public void Remove (object value)
{
list.Remove (value);
}
public void RemoveAt (int index)
{
list.RemoveAt (index);
}
#endregion // Methods
}
}

Просмотреть файл

@ -734,6 +734,97 @@ namespace System.Security.Cryptography.Xml {
return xel;
}
internal static XmlElement DefaultGetIdElement(XmlDocument document, string idValue)
{
if (document == null)
return null;
try
{
XmlConvert.VerifyNCName(idValue);
}
catch
{
// Identifiers are required to be an NCName
// (xml:id version 1.0, part 4, paragraph 2, bullet 1)
//
// If it isn't an NCName, it isn't allowed to match.
return null;
}
// Get the element with idValue
XmlElement elem = document.GetElementById(idValue);
if (elem != null)
{
// Have to check for duplicate ID values from the DTD.
XmlDocument docClone = (XmlDocument)document.CloneNode(true);
XmlElement cloneElem = docClone.GetElementById(idValue);
// If it's null here we want to know about it, because it means that
// GetElementById failed to work across the cloning, and our uniqueness
// test is invalid.
System.Diagnostics.Debug.Assert(cloneElem != null);
// Guard against null anyways
if (cloneElem != null)
{
cloneElem.Attributes.RemoveAll();
XmlElement cloneElem2 = docClone.GetElementById(idValue);
if (cloneElem2 != null)
{
throw new CryptographicException(
SR.Cryptography_Xml_InvalidReference);
}
}
return elem;
}
elem = GetSingleReferenceTarget(document, "Id", idValue);
if (elem != null)
return elem;
elem = GetSingleReferenceTarget(document, "id", idValue);
if (elem != null)
return elem;
elem = GetSingleReferenceTarget(document, "ID", idValue);
return elem;
}
private static XmlElement GetSingleReferenceTarget(XmlDocument document, string idAttributeName, string idValue)
{
// idValue has already been tested as an NCName (unless overridden for compatibility), so there's no
// escaping that needs to be done here.
string xPath = "//*[@" + idAttributeName + "=\"" + idValue + "\"]";
// http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel says that for the form URI="#chapter1":
//
// Identifies a node-set containing the element with ID attribute value 'chapter1' ...
//
// Note that it uses the singular. Therefore, if the match is ambiguous, we should consider the document invalid.
//
// In this case, we'll treat it the same as having found nothing across all fallbacks (but shortcut so that we don't
// fall into a trap of finding a secondary element which wasn't the originally signed one).
XmlNodeList nodeList = document.SelectNodes(xPath);
if (nodeList == null || nodeList.Count == 0)
{
return null;
}
if (nodeList.Count == 1)
{
return nodeList[0] as XmlElement;
}
throw new CryptographicException(SR.Cryptography_Xml_InvalidReference);
}
// According to book ".NET Framework Security" this method
// iterates all possible keys then return null
protected virtual AsymmetricAlgorithm GetPublicKey ()

Просмотреть файл

@ -1,366 +0,0 @@
//
// SymmetricKeyWrap.cs - Implements symmetric key wrap algorithms
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2004
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.IO;
using System.Security.Cryptography;
namespace System.Security.Cryptography.Xml {
internal class SymmetricKeyWrap {
public SymmetricKeyWrap ()
{
}
public static byte[] AESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
{
SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
// Apparently no one felt the need to document that this requires Electronic Codebook mode.
symAlg.Mode = CipherMode.ECB;
// This was also not documented anywhere.
symAlg.IV = new byte [16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
ICryptoTransform transform = symAlg.CreateEncryptor (rgbKey, symAlg.IV);
int N = rgbWrappedKeyData.Length / 8;
byte[] A;
byte[] B = new Byte [16];
byte [] C = new byte [8 * (N + 1)];
// 1. if N is 1:
// B = AES(K)enc(0xA6A6A6A6A6A6A6A6|P(1))
// C(0) = MSB(B)
// C(1) = LSB(B)
if (N == 1) {
A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
transform.TransformBlock (Concatenate (A, rgbWrappedKeyData), 0, 16, B, 0);
Buffer.BlockCopy (MSB(B), 0, C, 0, 8);
Buffer.BlockCopy (LSB(B), 0, C, 8, 8);
} else {
// if N > 1, perform the following steps:
// 2. Initialize variables:
// Set A to 0xA6A6A6A6A6A6A6A6
// For i = 1 to N,
// R(i) = P(i)
A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
byte[][] R = new byte [N + 1][];
for (int i = 1; i <= N; i += 1) {
R [i] = new byte [8];
Buffer.BlockCopy (rgbWrappedKeyData, 8 * (i - 1), R [i], 0, 8);
}
// 3. Calculate intermediate values:
// For j = 0 to 5
// For i = 1 to N
// t = i + j * N
// B = AES(K)enc(A|R(i))
// A = XOR(t, MSB(B))
// R(i) = LSB(B)
for (int j = 0; j <= 5; j += 1) {
for (int i = 1; i <= N; i += 1) {
transform.TransformBlock (Concatenate (A, R [i]), 0, 16, B, 0);
// Yawn. It was nice of those at NIST to document how exactly we should XOR
// an integer value with a byte array. Not.
byte[] T = BitConverter.GetBytes ((long) (N * j + i));
// This is nice.
if (BitConverter.IsLittleEndian)
Array.Reverse (T);
A = Xor (T, MSB(B));
R [i] = LSB (B);
}
}
// 4. Output the results:
// Set C(0) = A
// For i = 1 to N
// C(i) = R(i)
Buffer.BlockCopy (A, 0, C, 0, 8);
for (int i = 1; i <= N; i += 1)
Buffer.BlockCopy (R [i], 0, C, 8 * i, 8);
}
return C;
}
public static byte[] AESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
{
SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
symAlg.Mode = CipherMode.ECB;
symAlg.Key = rgbKey;
int N = ( rgbEncryptedWrappedKeyData.Length / 8 ) - 1;
// From RFC 3394 - Advanced Encryption Standard (AES) Key Wrap Algorithm
//
// Inputs: Ciphertext, (n+1) 64-bit values (C0, C1, ..., Cn), and Key, K (the KEK)
// Outputs: Plaintext, n 64-bit values (P1, P2, ..., Pn)
//
// 1. Initialize variables.
// Set A = C[0]
byte[] A = new byte [8];
Buffer.BlockCopy (rgbEncryptedWrappedKeyData, 0, A, 0, 8);
// For i = 1 to n
// R[i] = C[i]
byte[] R = new byte [N * 8];
Buffer.BlockCopy (rgbEncryptedWrappedKeyData, 8, R, 0, rgbEncryptedWrappedKeyData.Length - 8);
// 2. Compute intermediate values.
// For j = 5 to 0
// For i = n to 1
// B = AES-1(K, (A^t) | R[i]) where t = n*j+i
// A = MSB (64,B)
// R[i] = LSB (64,B)
ICryptoTransform transform = symAlg.CreateDecryptor ();
for (int j = 5; j >= 0; j -= 1) {
for (int i = N; i >= 1; i -= 1) {
byte[] T = BitConverter.GetBytes ((long) N * j + i);
if (BitConverter.IsLittleEndian)
Array.Reverse (T);
byte[] B = new Byte [16];
byte[] r = new Byte [8];
Buffer.BlockCopy (R, 8 * (i - 1), r, 0, 8);
byte[] ciphertext = Concatenate (Xor (A, T), r);
transform.TransformBlock (ciphertext, 0, 16, B, 0);
A = MSB (B);
Buffer.BlockCopy (LSB (B), 0, R, 8 * (i - 1), 8);
}
}
// 3. Output results
// If A is an appropriate initial value
// Then
// For i = 1 to n
// P[i] = R[i]
// Else
// Return an error
return R;
}
public static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
{
SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("TripleDES");
// Algorithm from http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
// The following algorithm wraps (encrypts) a key (the wrapped key, WK) under a TRIPLEDES
// key-encryption-key (KEK) as adopted from [CMS-Algorithms].
// 1. Represent the key being wrapped as an octet sequence. If it is a TRIPLEDES key,
// this is 24 octets (192 bits) with odd parity bit as the bottom bit of each octet.
// rgbWrappedKeyData is the key being wrapped.
// 2. Compute the CMS key checksum (Section 5.6.1) call this CKS.
byte[] cks = ComputeCMSKeyChecksum (rgbWrappedKeyData);
// 3. Let WKCKS = WK || CKS, where || is concatenation.
byte[] wkcks = Concatenate (rgbWrappedKeyData, cks);
// 4. Generate 8 random octets and call this IV.
symAlg.GenerateIV ();
// 5. Encrypt WKCKS in CBC mode using KEK as the key and IV as the initialization vector.
// Call the results TEMP1.
symAlg.Mode = CipherMode.CBC;
symAlg.Padding = PaddingMode.None;
symAlg.Key = rgbKey;
byte[] temp1 = Transform (wkcks, symAlg.CreateEncryptor ());
// 6. Let TEMP2 = IV || TEMP1.
byte[] temp2 = Concatenate (symAlg.IV, temp1);
// 7. Reverse the order of the octets in TEMP2 and call the result TEMP3.
Array.Reverse (temp2); // TEMP3 is TEMP2
// 8. Encrypt TEMP3 in CBC mode using the KEK and an initialization vector of 0x4adda22c79e82105.
// The resulting cipher text is the desired result. It is 40 octets long if a 168 bit key
// is being wrapped.
symAlg.IV = new Byte [8] {0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05};
byte[] rtnval = Transform (temp2, symAlg.CreateEncryptor ());
return rtnval;
}
public static byte[] TripleDESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
{
SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("TripleDES");
// Algorithm from http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
// The following algorithm unwraps (decrypts) a key as adopted from [CMS-Algorithms].
// 1. Check the length of the cipher text is reasonable given the key type. It must be
// 40 bytes for a 168 bit key and either 32, 40, or 48 bytes for a 128, 192, or 256 bit
// key. If the length is not supported or inconsistent with the algorithm for which the
// key is intended, return error.
// 2. Decrypt the cipher text with TRIPLEDES in CBC mode using the KEK and an initialization
// vector (IV) of 0x4adda22c79e82105. Call the output TEMP3.
symAlg.Mode = CipherMode.CBC;
symAlg.Padding = PaddingMode.None;
symAlg.Key = rgbKey;
symAlg.IV = new Byte [8] {0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05};
byte[] temp3 = Transform (rgbEncryptedWrappedKeyData, symAlg.CreateDecryptor ());
// 3. Reverse the order of the octets in TEMP3 and call the result TEMP2.
Array.Reverse (temp3); // TEMP2 is TEMP3.
// 4. Decompose TEMP2 into IV, the first 8 octets, and TEMP1, the remaining octets.
byte[] temp1 = new Byte [temp3.Length - 8];
byte[] iv = new Byte [8];
Buffer.BlockCopy (temp3, 0, iv, 0, 8);
Buffer.BlockCopy (temp3, 8, temp1, 0, temp1.Length);
// 5. Decrypt TEMP1 using TRIPLEDES in CBC mode using the KEK and the IV found in the previous step.
// Call the result WKCKS.
symAlg.IV = iv;
byte[] wkcks = Transform (temp1, symAlg.CreateDecryptor ());
// 6. Decompose WKCKS. CKS is the last 8 octets and WK, the wrapped key, are those octets before
// the CKS.
byte[] cks = new byte [8];
byte[] wk = new byte [wkcks.Length - 8];
Buffer.BlockCopy (wkcks, 0, wk, 0, wk.Length);
Buffer.BlockCopy (wkcks, wk.Length, cks, 0, 8);
// 7. Calculate the CMS key checksum over the WK and compare with the CKS extracted in the above
// step. If they are not equal, return error.
// 8. WK is the wrapped key, now extracted for use in data decryption.
return wk;
}
private static byte[] Transform (byte[] data, ICryptoTransform t)
{
MemoryStream output = new MemoryStream ();
CryptoStream crypto = new CryptoStream (output, t, CryptoStreamMode.Write);
crypto.Write (data, 0, data.Length);
crypto.FlushFinalBlock ();
byte[] result = output.ToArray ();
output.Close ();
crypto.Close ();
return result;
}
private static byte[] ComputeCMSKeyChecksum (byte[] data)
{
byte[] hash = HashAlgorithm.Create ("SHA1").ComputeHash (data);
byte[] output = new byte [8];
Buffer.BlockCopy (hash, 0, output, 0, 8);
return output;
}
private static byte[] Concatenate (byte[] buf1, byte[] buf2)
{
byte[] output = new byte [buf1.Length + buf2.Length];
Buffer.BlockCopy (buf1, 0, output, 0, buf1.Length);
Buffer.BlockCopy (buf2, 0, output, buf1.Length, buf2.Length);
return output;
}
private static byte[] MSB (byte[] input)
{
return MSB (input, 8);
}
private static byte[] MSB (byte[] input, int bytes)
{
byte[] output = new byte [bytes];
Buffer.BlockCopy (input, 0, output, 0, bytes);
return output;
}
private static byte[] LSB (byte[] input)
{
return LSB (input, 8);
}
private static byte[] LSB (byte[] input, int bytes)
{
byte[] output = new byte [bytes];
Buffer.BlockCopy (input, bytes, output, 0, bytes);
return output;
}
private static byte[] Xor (byte[] x, byte[] y)
{
// This should *not* happen.
if (x.Length != y.Length)
throw new CryptographicException ("Error performing Xor: arrays different length.");
byte[] output = new byte [x.Length];
for (int i = 0; i < x.Length; i += 1)
output [i] = (byte) (x [i] ^ y [i]);
return output;
}
/* private static byte[] Xor (byte[] x, int n)
{
byte[] output = new Byte [x.Length];
for (int i = 0; i < x.Length; i += 1)
output [i] = (byte) ((int) x [i] ^ n);
return output;
}*/
}
}

Просмотреть файл

@ -1,80 +0,0 @@
//
// XmlEncryption.cs: Handles Xml Encryption
//
// Author:
// Tim Coleman (tim@timcoleman.com)
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
namespace System.Security.Cryptography.Xml {
// following the design of WSE
internal class XmlEncryption {
public class ElementNames {
public const string CarriedKeyName = "CarriedKeyName";
public const string CipherData = "CipherData";
public const string CipherReference = "CipherReference";
public const string CipherValue = "CipherValue";
public const string DataReference = "DataReference";
public const string EncryptedData = "EncryptedData";
public const string EncryptedKey = "EncryptedKey";
public const string EncryptionMethod = "EncryptionMethod";
public const string EncryptionProperties = "EncryptionProperties";
public const string EncryptionProperty = "EncryptionProperty";
public const string KeyReference = "KeyReference";
public const string KeySize = "KeySize";
public const string ReferenceList = "ReferenceList";
public const string Transforms = "Transforms";
public ElementNames () {}
}
public class AttributeNames {
public const string Algorithm = "Algorithm";
public const string Encoding = "Encoding";
public const string Id = "Id";
public const string MimeType = "MimeType";
public const string Recipient = "Recipient";
public const string Target = "Target";
public const string Type = "Type";
public const string URI = "URI";
public AttributeNames () {}
}
public const string Prefix = "xenc";
public XmlEncryption () {}
}
}

Просмотреть файл

@ -30,42 +30,43 @@ System.Security.Cryptography.X509Certificates/X509SelectionFlag.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlText.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlWhitespace.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CertUsageType.cs
System.Security.Cryptography.Xml/CipherData.cs
System.Security.Cryptography.Xml/CipherReference.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherData.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CipherReference.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CryptoHelpers.cs
System.Security.Cryptography.Xml/DataObject.cs
System.Security.Cryptography.Xml/DataReference.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CryptoSignedXmlRecursionException.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataObject.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DataReference.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DocPosition.cs
System.Security.Cryptography.Xml/DSAKeyValue.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSAKeyValue.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/DSASignatureDescription.cs
System.Security.Cryptography.Xml/EncryptedData.cs
System.Security.Cryptography.Xml/EncryptedKey.cs
System.Security.Cryptography.Xml/EncryptedReference.cs
System.Security.Cryptography.Xml/EncryptedType.cs
System.Security.Cryptography.Xml/EncryptedXml.cs
System.Security.Cryptography.Xml/EncryptionMethod.cs
System.Security.Cryptography.Xml/EncryptionProperties.cs
System.Security.Cryptography.Xml/EncryptionProperty.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedData.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedKey.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedReference.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedType.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptedXml.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionMethod.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionProperty.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/EncryptionPropertyCollection.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcAncestralNamespaceContextManager.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ExcCanonicalXml.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ICanonicalizableNode.cs
System.Security.Cryptography.Xml/IRelDecryptor.cs
System.Security.Cryptography.Xml/KeyInfoClause.cs
System.Security.Cryptography.Xml/KeyInfo.cs
System.Security.Cryptography.Xml/KeyInfoEncryptedKey.cs
System.Security.Cryptography.Xml/KeyInfoName.cs
System.Security.Cryptography.Xml/KeyInfoNode.cs
System.Security.Cryptography.Xml/KeyInfoRetrievalMethod.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/IRelDecryptor.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfo.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoClause.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoEncryptedKey.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoName.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoNode.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyInfoRetrievalMethod.cs
System.Security.Cryptography.Xml/KeyInfoX509Data.cs
System.Security.Cryptography.Xml/KeyReference.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/KeyReference.cs
System.Security.Cryptography.Xml/Manifest.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/MyXmlDocument.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceFrame.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/NamespaceSortOrder.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs
System.Security.Cryptography.Xml/ReferenceList.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceList.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/ReferenceTargetType.cs
System.Security.Cryptography.Xml/RSAKeyValue.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAKeyValue.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SHA1SignatureDescription.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SHA256SignatureDescription.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/RSAPKCS1SHA384SignatureDescription.cs
@ -75,7 +76,7 @@ System.Security.Cryptography.Xml/Signature.cs
System.Security.Cryptography.Xml/SignedInfo.cs
System.Security.Cryptography.Xml/SignedXml.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SignedXmlDebugLog.cs
System.Security.Cryptography.Xml/SymmetricKeyWrap.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/SymmetricKeyWrap.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Transform.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/TransformChain.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs
@ -88,7 +89,6 @@ System.Security.Cryptography.Xml/SymmetricKeyWrap.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigExcC14NWithCommentsTransform.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXPathTransform.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlDsigXsltTransform.cs
System.Security.Cryptography.Xml/XmlEncryption.cs
../../../external/corefx/src/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/XmlLicenseTransform.cs
System.Security.Cryptography.Xml/XmlSignature.cs
../../build/common/MonoTODOAttribute.cs