mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathAsn1Null.cs
More file actions
93 lines (75 loc) · 2.8 KB
/
Copy pathAsn1Null.cs
File metadata and controls
93 lines (75 loc) · 2.8 KB
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
using System;
using System.IO;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1
{
/// <summary>A NULL object.</summary>
public abstract class Asn1Null
: Asn1Object
{
internal class Meta : Asn1UniversalType
{
internal static readonly Asn1UniversalType Instance = new Meta();
private Meta() : base(typeof(Asn1Null), Asn1Tags.Null) {}
internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
{
return CreatePrimitive(octetString.GetOctetsLength());
}
}
public static Asn1Null GetInstance(object obj)
{
if (obj == null)
return null;
if (obj is Asn1Null asn1Null)
return asn1Null;
if (obj is IAsn1Convertible asn1Convertible)
{
if (!(obj is Asn1Object) && asn1Convertible.ToAsn1Object() is Asn1Null converted)
return converted;
}
else if (obj is byte[] bytes)
{
try
{
return (Asn1Null)Meta.Instance.FromByteArray(bytes);
}
catch (IOException e)
{
throw new ArgumentException("failed to construct NULL from byte[]", nameof(obj), e);
}
}
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
}
public static Asn1Null GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
{
return (Asn1Null)Meta.Instance.GetContextTagged(taggedObject, declaredExplicit);
}
public static Asn1Null GetOptional(Asn1Encodable element)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (element is Asn1Null existing)
return existing;
return null;
}
public static Asn1Null GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit)
{
return (Asn1Null)Meta.Instance.GetTagged(taggedObject, declaredExplicit);
}
internal Asn1Null()
{
}
public override string ToString() => "NULL";
private static void CheckContentsLength(int contentsLength)
{
if (0 != contentsLength)
throw new InvalidOperationException("malformed NULL encoding encountered");
}
internal static Asn1Null CreatePrimitive(DefiniteLengthInputStream defIn) => CreatePrimitive(defIn.Remaining);
private static Asn1Null CreatePrimitive(int contentsLength)
{
CheckContentsLength(contentsLength);
return DerNull.Instance;
}
}
}