-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
XmlDsigBase64Transform.cs
131 lines (118 loc) · 4.01 KB
/
XmlDsigBase64Transform.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
126
127
128
129
130
131
// 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.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml
{
// A class representing conversion from Base64 using CryptoStream
public class XmlDsigBase64Transform : Transform
{
private readonly Type[] _inputTypes = { typeof(Stream), typeof(XmlNodeList), typeof(XmlDocument) };
private readonly Type[] _outputTypes = { typeof(Stream) };
private CryptoStream? _cs;
public XmlDsigBase64Transform()
{
Algorithm = SignedXml.XmlDsigBase64TransformUrl;
}
public override Type[] InputTypes
{
get { return _inputTypes; }
}
public override Type[] OutputTypes
{
get { return _outputTypes; }
}
public override void LoadInnerXml(XmlNodeList nodeList)
{
}
protected override XmlNodeList? GetInnerXml()
{
return null;
}
public override void LoadInput(object obj)
{
if (obj is Stream)
{
LoadStreamInput((Stream)obj);
return;
}
if (obj is XmlNodeList)
{
LoadXmlNodeListInput((XmlNodeList)obj);
return;
}
if (obj is XmlDocument)
{
LoadXmlNodeListInput(((XmlDocument)obj).SelectNodes("//.")!);
return;
}
}
private void LoadStreamInput(Stream inputStream)
{
if (inputStream == null) throw new ArgumentException("obj");
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[1024];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, 1024);
if (bytesRead > 0)
{
int i;
int j = 0;
while ((j < bytesRead) && (!char.IsWhiteSpace((char)buffer[j]))) j++;
i = j; j++;
while (j < bytesRead)
{
if (!char.IsWhiteSpace((char)buffer[j]))
{
buffer[i] = buffer[j];
i++;
}
j++;
}
ms.Write(buffer, 0, i);
}
} while (bytesRead > 0);
ms.Position = 0;
_cs = new CryptoStream(ms, new FromBase64Transform(), CryptoStreamMode.Read);
}
private void LoadXmlNodeListInput(XmlNodeList nodeList)
{
StringBuilder sb = new StringBuilder();
foreach (XmlNode node in nodeList)
{
XmlNode? result = node.SelectSingleNode("self::text()");
if (result != null)
sb.Append(result.OuterXml);
}
byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString());
int i;
int j = 0;
while ((j < buffer.Length) && (!char.IsWhiteSpace((char)buffer[j]))) j++;
i = j; j++;
while (j < buffer.Length)
{
if (!char.IsWhiteSpace((char)buffer[j]))
{
buffer[i] = buffer[j];
i++;
}
j++;
}
MemoryStream ms = new MemoryStream(buffer, 0, i);
_cs = new CryptoStream(ms, new FromBase64Transform(), CryptoStreamMode.Read);
}
public override object GetOutput()
{
return _cs!;
}
public override object GetOutput(Type type)
{
if (type != typeof(Stream) && !type.IsSubclassOf(typeof(Stream)))
throw new ArgumentException(SR.Cryptography_Xml_TransformIncorrectInputType, nameof(type));
return _cs!;
}
}
}