Skip to content

Commit 12ba337

Browse files
committed
1 parent 6194ceb commit 12ba337

36 files changed

+446
-13
lines changed

crypto/src/asn1/Asn1Null.cs

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public static Asn1Null GetInstance(object obj)
4646
throw new ArgumentException("failed to construct NULL from byte[]: " + e.Message);
4747
}
4848
}
49+
else if (obj is ArraySegment<byte>)
50+
{
51+
try
52+
{
53+
return (Asn1Null)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
54+
}
55+
catch (IOException e)
56+
{
57+
throw new ArgumentException("failed to construct NULL from ArraySegment<byte>: " + e.Message);
58+
}
59+
}
4960

5061
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
5162
}

crypto/src/asn1/Asn1Object.cs

+25-13
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,33 @@ public bool Equals(Asn1Object other)
3434
public static Asn1Object FromByteArray(
3535
byte[] data)
3636
{
37-
try
38-
{
39-
MemoryStream input = new MemoryStream(data, false);
40-
Asn1InputStream asn1 = new Asn1InputStream(input, data.Length);
41-
Asn1Object result = asn1.ReadObject();
42-
if (input.Position != input.Length)
43-
throw new IOException("extra data found after object");
44-
return result;
45-
}
46-
catch (InvalidCastException)
47-
{
48-
throw new IOException("cannot recognise object in byte array");
49-
}
37+
return FromByteArray(new ArraySegment<byte>(data, 0, data.Length));
5038
}
5139

40+
/// <summary>Create a base ASN.1 object from a byte array segment.</summary>
41+
/// <param name="data">The byte array segment to parse.</param>
42+
/// <returns>The base ASN.1 object represented by the byte array.</returns>
43+
/// <exception cref="IOException">
44+
/// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
45+
/// </exception>
46+
public static Asn1Object FromByteArray(
47+
ArraySegment<byte> data)
48+
{
49+
try
50+
{
51+
MemoryStream input = new MemoryStream(data.Array ?? new byte[0], data.Offset, data.Count, false);
52+
Asn1InputStream asn1 = new Asn1InputStream(input, data.Count);
53+
Asn1Object result = asn1.ReadObject();
54+
if (input.Position != input.Length)
55+
throw new IOException("extra data found after object");
56+
return result;
57+
}
58+
catch (InvalidCastException)
59+
{
60+
throw new IOException("cannot recognise object in byte array");
61+
}
62+
}
63+
5264
/// <summary>Read a base ASN.1 object from a stream.</summary>
5365
/// <param name="inStr">The stream to parse.</param>
5466
/// <returns>The base ASN.1 object represented by the byte array.</returns>

crypto/src/asn1/Asn1ObjectDescriptor.cs

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public static Asn1ObjectDescriptor GetInstance(object obj)
5757
throw new ArgumentException("failed to construct object descriptor from byte[]: " + e.Message);
5858
}
5959
}
60+
else if (obj is ArraySegment<byte>)
61+
{
62+
try
63+
{
64+
return (Asn1ObjectDescriptor)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
65+
}
66+
catch (IOException e)
67+
{
68+
throw new ArgumentException("failed to construct object descriptor from ArraySegment<byte>: " + e.Message);
69+
}
70+
}
6071

6172
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
6273
}

crypto/src/asn1/Asn1OctetString.cs

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ public static Asn1OctetString GetInstance(object obj)
5858
throw new ArgumentException("failed to construct OCTET STRING from byte[]: " + e.Message);
5959
}
6060
}
61+
else if (obj is ArraySegment<byte>)
62+
{
63+
try
64+
{
65+
return (Asn1OctetString)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
66+
}
67+
catch (IOException e)
68+
{
69+
throw new ArgumentException("failed to construct OCTET STRING from ArraySegment<byte>: " + e.Message);
70+
}
71+
}
6172

6273
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
6374
}

crypto/src/asn1/Asn1RelativeOid.cs

+11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public static Asn1RelativeOid GetInstance(object obj)
5050
throw new ArgumentException("failed to construct relative OID from byte[]: " + e.Message);
5151
}
5252
}
53+
else if (obj is ArraySegment<byte>)
54+
{
55+
try
56+
{
57+
return (Asn1RelativeOid)FromByteArray((ArraySegment<byte>)obj);
58+
}
59+
catch (IOException e)
60+
{
61+
throw new ArgumentException("failed to construct relative OID from ArraySegment<byte>: " + e.Message);
62+
}
63+
}
5364

5465
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
5566
}

crypto/src/asn1/Asn1Sequence.cs

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ public static Asn1Sequence GetInstance(object obj)
5252
throw new ArgumentException("failed to construct sequence from byte[]: " + e.Message);
5353
}
5454
}
55+
else if (obj is ArraySegment<byte>)
56+
{
57+
try
58+
{
59+
return (Asn1Sequence)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
60+
}
61+
catch (IOException e)
62+
{
63+
throw new ArgumentException("failed to construct sequence from ArraySegment<byte>: " + e.Message);
64+
}
65+
}
5566

5667
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
5768
}

crypto/src/asn1/Asn1Set.cs

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ public static Asn1Set GetInstance(object obj)
5858
throw new ArgumentException("failed to construct set from byte[]: " + e.Message);
5959
}
6060
}
61+
else if (obj is ArraySegment<byte>)
62+
{
63+
try
64+
{
65+
return (Asn1Set)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
66+
}
67+
catch (IOException e)
68+
{
69+
throw new ArgumentException("failed to construct set from ArraySegment<byte>: " + e.Message);
70+
}
71+
}
6172

6273
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
6374
}

crypto/src/asn1/Asn1TaggedObject.cs

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public static Asn1TaggedObject GetInstance(object obj)
4343
throw new ArgumentException("failed to construct tagged object from byte[]: " + e.Message);
4444
}
4545
}
46+
else if (obj is ArraySegment<byte>)
47+
{
48+
try
49+
{
50+
return CheckedCast(FromByteArray((ArraySegment<byte>)obj));
51+
}
52+
catch (IOException e)
53+
{
54+
throw new ArgumentException("failed to construct tagged object from ArraySegment<byte>: " + e.Message);
55+
}
56+
}
4657

4758
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
4859
}

crypto/src/asn1/Asn1UniversalType.cs

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ internal Asn1Object FromByteArray(byte[] bytes)
4040
return CheckedCast(Asn1Object.FromByteArray(bytes));
4141
}
4242

43+
/// <exception cref="IOException"/>
44+
internal Asn1Object FromByteArray(ArraySegment<byte> bytes)
45+
{
46+
return CheckedCast(Asn1Object.FromByteArray(bytes));
47+
}
48+
4349
internal Asn1Object GetContextInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
4450
{
4551
if (Asn1Tags.ContextSpecific != taggedObject.TagClass)

crypto/src/asn1/DERExternal.cs

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public static DerExternal GetInstance(object obj)
4646
throw new ArgumentException("failed to construct external from byte[]: " + e.Message);
4747
}
4848
}
49+
else if (obj is ArraySegment<byte>)
50+
{
51+
try
52+
{
53+
return (DerExternal)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
54+
}
55+
catch (IOException e)
56+
{
57+
throw new ArgumentException("failed to construct external from ArraySegment<byte>: " + e.Message);
58+
}
59+
}
4960

5061
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
5162
}

crypto/src/asn1/DerApplicationSpecific.cs

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public static DerApplicationSpecific GetInstance(object obj)
2828
throw new ArgumentException("failed to construct application-specific from byte[]: " + e.Message);
2929
}
3030
}
31+
else if (obj is ArraySegment<byte>)
32+
{
33+
try
34+
{
35+
return GetInstance(FromByteArray((ArraySegment<byte>)obj));
36+
}
37+
catch (IOException e)
38+
{
39+
throw new ArgumentException("failed to construct application-specific from ArraySegment<byte>: " + e.Message);
40+
}
41+
}
3142

3243
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
3344
}

crypto/src/asn1/DerBMPString.cs

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ public static DerBmpString GetInstance(object obj)
5252
throw new ArgumentException("failed to construct BMP string from byte[]: " + e.Message);
5353
}
5454
}
55+
else if (obj is ArraySegment<byte>)
56+
{
57+
try
58+
{
59+
return (DerBmpString)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
60+
}
61+
catch (IOException e)
62+
{
63+
throw new ArgumentException("failed to construct BMP string from ArraySegment<byte>: " + e.Message);
64+
}
65+
}
5566

5667
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
5768
}

crypto/src/asn1/DerBitString.cs

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public static DerBitString GetInstance(object obj)
6060
throw new ArgumentException("failed to construct BIT STRING from byte[]: " + e.Message);
6161
}
6262
}
63+
else if (obj is ArraySegment<byte>)
64+
{
65+
try
66+
{
67+
return GetInstance(FromByteArray((ArraySegment<byte>)obj));
68+
}
69+
catch (IOException e)
70+
{
71+
throw new ArgumentException("failed to construct BIT STRING from ArraySegment<byte>: " + e.Message);
72+
}
73+
}
6374

6475
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
6576
}

crypto/src/asn1/DerBoolean.cs

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public static DerBoolean GetInstance(object obj)
5151
throw new ArgumentException("failed to construct boolean from byte[]: " + e.Message);
5252
}
5353
}
54+
else if (obj is ArraySegment<byte>)
55+
{
56+
try
57+
{
58+
return (DerBoolean)Meta.Instance.FromByteArray((byte[])obj);
59+
}
60+
catch (IOException e)
61+
{
62+
throw new ArgumentException("failed to construct boolean from ArraySegment<byte>: " + e.Message);
63+
}
64+
}
5465

5566
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
5667
}

crypto/src/asn1/DerEnumerated.cs

+11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public static DerEnumerated GetInstance(object obj)
4949
throw new ArgumentException("failed to construct enumerated from byte[]: " + e.Message);
5050
}
5151
}
52+
else if (obj is ArraySegment<byte>)
53+
{
54+
try
55+
{
56+
return (DerEnumerated)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
57+
}
58+
catch (IOException e)
59+
{
60+
throw new ArgumentException("failed to construct enumerated from ArraySegment<byte>: " + e.Message);
61+
}
62+
}
5263

5364
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
5465
}

crypto/src/asn1/DerGeneralString.cs

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public static DerGeneralString GetInstance(object obj)
4343
throw new ArgumentException("failed to construct general string from byte[]: " + e.Message);
4444
}
4545
}
46+
else if (obj is ArraySegment<byte>)
47+
{
48+
try
49+
{
50+
return (DerGeneralString)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
51+
}
52+
catch (IOException e)
53+
{
54+
throw new ArgumentException("failed to construct general string from ArraySegment<byte>: " + e.Message);
55+
}
56+
}
4657

4758
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
4859
}

crypto/src/asn1/DerGeneralizedTime.cs

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public static DerGeneralizedTime GetInstance(object obj)
5353
throw new ArgumentException("failed to construct generalized time from byte[]: " + e.Message);
5454
}
5555
}
56+
else if (obj is ArraySegment<byte>)
57+
{
58+
try
59+
{
60+
return (DerGeneralizedTime)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
61+
}
62+
catch (IOException e)
63+
{
64+
throw new ArgumentException("failed to construct generalized time from ArraySegment<byte>: " + e.Message);
65+
}
66+
}
5667

5768
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
5869
}

crypto/src/asn1/DerGraphicString.cs

+11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public static DerGraphicString GetInstance(object obj)
5050
throw new ArgumentException("failed to construct graphic string from byte[]: " + e.Message);
5151
}
5252
}
53+
else if (obj is ArraySegment<byte>)
54+
{
55+
try
56+
{
57+
return (DerGraphicString)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
58+
}
59+
catch (IOException e)
60+
{
61+
throw new ArgumentException("failed to construct graphic string from ArraySegment<byte>: " + e.Message);
62+
}
63+
}
5364

5465
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
5566
}

crypto/src/asn1/DerIA5String.cs

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public static DerIA5String GetInstance(object obj)
5151
throw new ArgumentException("failed to construct IA5 string from byte[]: " + e.Message);
5252
}
5353
}
54+
else if (obj is ArraySegment<byte>)
55+
{
56+
try
57+
{
58+
return (DerIA5String)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
59+
}
60+
catch (IOException e)
61+
{
62+
throw new ArgumentException("failed to construct IA5 string from ArraySegment<byte>: " + e.Message);
63+
}
64+
}
5465

5566
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
5667
}

crypto/src/asn1/DerInteger.cs

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ public static DerInteger GetInstance(object obj)
6363
throw new ArgumentException("failed to construct integer from byte[]: " + e.Message);
6464
}
6565
}
66+
else if (obj is ArraySegment<byte>)
67+
{
68+
try
69+
{
70+
return (DerInteger)Meta.Instance.FromByteArray((ArraySegment<byte>)obj);
71+
}
72+
catch (IOException e)
73+
{
74+
throw new ArgumentException("failed to construct integer from ArraySegment<byte>: " + e.Message);
75+
}
76+
}
6677

6778
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
6879
}

0 commit comments

Comments
 (0)