/
XmlDsigExcC14NTransform.cs
125 lines (110 loc) · 5.06 KB
/
XmlDsigExcC14NTransform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Xml;
namespace System.Security.Cryptography.Xml
{
public class XmlDsigExcC14NTransform : Transform
{
private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlDocument), typeof(XmlNodeList) };
private readonly Type[] _outputTypes = { typeof(Stream) };
private readonly bool _includeComments;
private string? _inclusiveNamespacesPrefixList;
private ExcCanonicalXml? _excCanonicalXml;
public XmlDsigExcC14NTransform() : this(false, null) { }
public XmlDsigExcC14NTransform(bool includeComments) : this(includeComments, null) { }
public XmlDsigExcC14NTransform(string inclusiveNamespacesPrefixList) : this(false, inclusiveNamespacesPrefixList) { }
public XmlDsigExcC14NTransform(bool includeComments, string? inclusiveNamespacesPrefixList)
{
_includeComments = includeComments;
_inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList;
Algorithm = (includeComments ? SignedXml.XmlDsigExcC14NWithCommentsTransformUrl : SignedXml.XmlDsigExcC14NTransformUrl);
}
public string? InclusiveNamespacesPrefixList
{
get { return _inclusiveNamespacesPrefixList; }
set { _inclusiveNamespacesPrefixList = value; }
}
public override Type[] InputTypes
{
get { return _inputTypes; }
}
public override Type[] OutputTypes
{
get { return _outputTypes; }
}
public override void LoadInnerXml(XmlNodeList nodeList)
{
if (nodeList != null)
{
foreach (XmlNode n in nodeList)
{
XmlElement? e = n as XmlElement;
if (e != null)
{
if (e.LocalName.Equals("InclusiveNamespaces")
&& e.NamespaceURI.Equals(SignedXml.XmlDsigExcC14NTransformUrl) &&
Utils.HasAttribute(e, "PrefixList", SignedXml.XmlDsigNamespaceUrl))
{
if (!Utils.VerifyAttributes(e, "PrefixList"))
{
throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform);
}
this.InclusiveNamespacesPrefixList = Utils.GetAttribute(e, "PrefixList", SignedXml.XmlDsigNamespaceUrl);
return;
}
else
{
throw new CryptographicException(SR.Cryptography_Xml_UnknownTransform);
}
}
}
}
}
public override void LoadInput(object obj)
{
XmlResolver resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver());
if (obj is Stream)
{
_excCanonicalXml = new ExcCanonicalXml((Stream)obj, _includeComments, _inclusiveNamespacesPrefixList!, resolver, BaseURI!);
}
else if (obj is XmlDocument)
{
_excCanonicalXml = new ExcCanonicalXml((XmlDocument)obj, _includeComments, _inclusiveNamespacesPrefixList!, resolver);
}
else if (obj is XmlNodeList)
{
_excCanonicalXml = new ExcCanonicalXml((XmlNodeList)obj, _includeComments, _inclusiveNamespacesPrefixList!, resolver);
}
else
throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(obj));
}
protected override XmlNodeList? GetInnerXml()
{
if (InclusiveNamespacesPrefixList == null)
return null;
XmlDocument document = new XmlDocument();
XmlElement element = document.CreateElement("Transform", SignedXml.XmlDsigNamespaceUrl);
if (!string.IsNullOrEmpty(Algorithm))
element.SetAttribute("Algorithm", Algorithm);
XmlElement prefixListElement = document.CreateElement("InclusiveNamespaces", SignedXml.XmlDsigExcC14NTransformUrl);
prefixListElement.SetAttribute("PrefixList", InclusiveNamespacesPrefixList);
element.AppendChild(prefixListElement);
return element.ChildNodes;
}
public override object GetOutput()
{
return new MemoryStream(_excCanonicalXml!.GetBytes());
}
public override object GetOutput(Type type)
{
if (type != typeof(Stream) && !type.IsSubclassOf(typeof(Stream)))
throw new ArgumentException(SR.Cryptography_Xml_TransformIncorrectInputType, nameof(type));
return new MemoryStream(_excCanonicalXml!.GetBytes());
}
public override byte[] GetDigestedOutput(HashAlgorithm hash)
{
return _excCanonicalXml!.GetDigestedBytes(hash);
}
}
}