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
|
using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Security.Certificates;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.X509.Store;
namespace Org.BouncyCastle.Cms
{
internal class CmsUtilities
{
// TODO Is there a .NET equivalent to this?
// private static readonly Runtime RUNTIME = Runtime.getRuntime();
internal static int MaximumMemory
{
get
{
// TODO Is there a .NET equivalent to this?
long maxMem = int.MaxValue;//RUNTIME.maxMemory();
if (maxMem > int.MaxValue)
{
return int.MaxValue;
}
return (int)maxMem;
}
}
internal static ContentInfo ReadContentInfo(
byte[] input)
{
// enforce limit checking as from a byte array
return ReadContentInfo(new Asn1InputStream(input));
}
internal static ContentInfo ReadContentInfo(
Stream input)
{
// enforce some limit checking
return ReadContentInfo(new Asn1InputStream(input, MaximumMemory));
}
private static ContentInfo ReadContentInfo(
Asn1InputStream aIn)
{
try
{
return ContentInfo.GetInstance(aIn.ReadObject());
}
catch (IOException e)
{
throw new CmsException("IOException reading content.", e);
}
catch (InvalidCastException e)
{
throw new CmsException("Malformed content.", e);
}
catch (ArgumentException e)
{
throw new CmsException("Malformed content.", e);
}
}
public static byte[] StreamToByteArray(
Stream inStream)
{
return Streams.ReadAll(inStream);
}
public static byte[] StreamToByteArray(
Stream inStream,
int limit)
{
return Streams.ReadAllLimited(inStream, limit);
}
public static IList GetCertificatesFromStore(
IX509Store certStore)
{
try
{
IList certs = Platform.CreateArrayList();
if (certStore != null)
{
foreach (X509Certificate c in certStore.GetMatches(null))
{
certs.Add(
X509CertificateStructure.GetInstance(
Asn1Object.FromByteArray(c.GetEncoded())));
}
}
return certs;
}
catch (CertificateEncodingException e)
{
throw new CmsException("error encoding certs", e);
}
catch (Exception e)
{
throw new CmsException("error processing certs", e);
}
}
public static IList GetCrlsFromStore(
IX509Store crlStore)
{
try
{
IList crls = Platform.CreateArrayList();
if (crlStore != null)
{
foreach (X509Crl c in crlStore.GetMatches(null))
{
crls.Add(
CertificateList.GetInstance(
Asn1Object.FromByteArray(c.GetEncoded())));
}
}
return crls;
}
catch (CrlException e)
{
throw new CmsException("error encoding crls", e);
}
catch (Exception e)
{
throw new CmsException("error processing crls", e);
}
}
public static Asn1Set CreateBerSetFromList(
IList berObjects)
{
Asn1EncodableVector v = new Asn1EncodableVector();
foreach (Asn1Encodable ae in berObjects)
{
v.Add(ae);
}
return new BerSet(v);
}
public static Asn1Set CreateDerSetFromList(
IList derObjects)
{
Asn1EncodableVector v = new Asn1EncodableVector();
foreach (Asn1Encodable ae in derObjects)
{
v.Add(ae);
}
return new DerSet(v);
}
internal static Stream CreateBerOctetOutputStream(Stream s, int tagNo, bool isExplicit, int bufferSize)
{
BerOctetStringGenerator octGen = new BerOctetStringGenerator(s, tagNo, isExplicit);
return octGen.GetOctetOutputStream(bufferSize);
}
internal static TbsCertificateStructure GetTbsCertificateStructure(X509Certificate cert)
{
return TbsCertificateStructure.GetInstance(Asn1Object.FromByteArray(cert.GetTbsCertificate()));
}
internal static IssuerAndSerialNumber GetIssuerAndSerialNumber(X509Certificate cert)
{
TbsCertificateStructure tbsCert = GetTbsCertificateStructure(cert);
return new IssuerAndSerialNumber(tbsCert.Issuer, tbsCert.SerialNumber.Value);
}
}
}
|