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 pathDERGenerator.cs
More file actions
66 lines (60 loc) · 2.28 KB
/
Copy pathDERGenerator.cs
File metadata and controls
66 lines (60 loc) · 2.28 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
using System.IO;
namespace Org.BouncyCastle.Asn1
{
public abstract class DerGenerator
: Asn1Generator
{
private bool _tagged = false;
private bool _isExplicit;
private int _tagNo;
protected DerGenerator(Stream outStream)
: base(outStream)
{
}
protected DerGenerator(Stream outStream, int tagNo, bool isExplicit)
: base(outStream)
{
_tagged = true;
_isExplicit = isExplicit;
_tagNo = tagNo;
}
internal void WriteDerEncoded(int tag, byte[] bytes)
{
if (!_tagged)
{
WriteDerEncoded(OutStream, tag, bytes);
}
else if (_isExplicit)
{
/*
* X.690-0207 8.14.2. If implicit tagging [..] was not used [..], the encoding shall be constructed
* and the contents octets shall be the complete base encoding.
*/
MemoryStream buf = new MemoryStream();
WriteDerEncoded(buf, tag, bytes);
WriteDerEncoded(OutStream, Asn1Tags.ContextSpecific | Asn1Tags.Constructed, _tagNo, buf.ToArray());
}
else
{
/*
* X.690-0207 8.14.3. If implicit tagging was used [..], then: a) the encoding shall be constructed
* if the base encoding is constructed, and shall be primitive otherwise; and b) the contents octets
* shall be [..] the contents octets of the base encoding.
*/
WriteDerEncoded(OutStream, InheritConstructedFlag(Asn1Tags.ContextSpecific, tag), _tagNo, bytes);
}
}
internal static void WriteDerEncoded(Stream outStream, int tag, byte[] bytes)
{
outStream.WriteByte((byte)tag);
Asn1OutputStream.WriteDL(outStream, bytes.Length);
outStream.Write(bytes, 0, bytes.Length);
}
private void WriteDerEncoded(Stream outStream, int flags, int tagNo, byte[] bytes)
{
Asn1OutputStream.WriteIdentifier(outStream, flags, tagNo);
Asn1OutputStream.WriteDL(outStream, bytes.Length);
outStream.Write(bytes, 0, bytes.Length);
}
}
}