-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsnReaderExtensions.cs
195 lines (168 loc) · 5.56 KB
/
AsnReaderExtensions.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System.Buffers;
using System.Buffers.Binary;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace System.Security.Cryptography.Asn1
{
internal static class AsnReaderExtensions
{
public static long ReadInt64(this AsnReader reader)
{
if (reader.TryReadInt64(out long value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static ulong ReadUInt64(this AsnReader reader)
{
if (reader.TryReadUInt64(out ulong value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static int ReadInt32(this AsnReader reader)
{
if (reader.TryReadInt32(out int value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static uint ReadUInt32(this AsnReader reader)
{
if (reader.TryReadUInt32(out uint value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static short ReadInt16(this AsnReader reader)
{
if (reader.TryReadInt16(out short value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static ushort ReadUInt16(this AsnReader reader)
{
if (reader.TryReadUInt16(out ushort value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static sbyte ReadInt8(this AsnReader reader)
{
if (reader.TryReadInt8(out sbyte value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static byte ReadUInt8(this AsnReader reader)
{
if (reader.TryReadUInt8(out byte value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static long ReadInt64(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadInt64(tag, out long value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static ulong ReadUInt64(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadUInt64(tag, out ulong value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static int ReadInt32(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadInt32(tag, out int value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static uint ReadUInt32(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadUInt32(tag, out uint value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static short ReadInt16(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadInt16(tag, out short value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static ushort ReadUInt16(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadUInt16(tag, out ushort value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static sbyte ReadInt8(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadInt8(tag, out sbyte value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static byte ReadUInt8(this AsnReader reader, Asn1Tag tag)
{
if (reader.TryReadUInt8(tag, out byte value))
return value;
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
public static ReadOnlyMemory<byte> GetEncodedValue(this AsnReader reader, Asn1Tag matchTag)
{
Asn1Tag nextTag = reader.PeekTag();
if (matchTag.TagClass != nextTag.TagClass ||
matchTag.TagValue != nextTag.TagValue)
{
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
return reader.GetEncodedValue();
}
public static ReadOnlyMemory<byte> GetBitStringBytes(this AsnReader reader, Asn1Tag expectedTag)
{
if (reader.TryGetPrimitiveBitStringValue(expectedTag, out _, out ReadOnlyMemory<byte> contents))
{
return contents;
}
// Guaranteed too big, because it has the tag and length.
int length = reader.PeekEncodedValue().Length;
byte[] rented = ArrayPool<byte>.Shared.Rent(length);
try
{
if (reader.TryCopyBitStringBytes(expectedTag, rented, out _, out int bytesWritten))
{
return new ReadOnlyMemory<byte>(rented.AsSpan(0, bytesWritten).ToArray());
}
Debug.Fail("TryCopyBitStringBytes produced more data than the encoded size");
throw new CryptographicException();
}
finally
{
Array.Clear(rented, 0, length);
ArrayPool<byte>.Shared.Return(rented);
}
}
public static ReadOnlyMemory<byte> GetOctetStringBytes(this AsnReader reader, Asn1Tag expectedTag)
{
if (reader.TryGetPrimitiveOctetStringBytes(expectedTag, out ReadOnlyMemory<byte> contents))
{
return contents;
}
// Guaranteed too big, because it has the tag and length.
int length = reader.PeekEncodedValue().Length;
byte[] rented = ArrayPool<byte>.Shared.Rent(length);
try
{
if (reader.TryCopyOctetStringBytes(expectedTag, rented, out int bytesWritten))
{
return new ReadOnlyMemory<byte>(rented.AsSpan(0, bytesWritten).ToArray());
}
Debug.Fail("TryCopyOctetStringBytes produced more data than the encoded size");
throw new CryptographicException();
}
finally
{
Array.Clear(rented, 0, length);
ArrayPool<byte>.Shared.Return(rented);
}
}
}
}