summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1OutputStream.cs
blob: b0d4a18fa78e2983ecc9f48c9acbc98be2875ce6 (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.IO;

namespace Org.BouncyCastle.Asn1
{
    public class Asn1OutputStream
        : DerOutputStream
    {
        public static Asn1OutputStream Create(Stream output)
        {
            return new Asn1OutputStream(output);
        }

        public static Asn1OutputStream Create(Stream output, string encoding)
        {
            if (Asn1Encodable.Der.Equals(encoding))
            {
                return new DerOutputStreamNew(output);
            }
            else
            {
                return new Asn1OutputStream(output);
            }
        }

        [Obsolete("Use static Create method(s)")]
        public Asn1OutputStream(Stream os)
            : base(os)
        {
        }

        public override void WriteObject(Asn1Encodable encodable)
        {
            if (null == encodable)
                throw new IOException("null object detected");

            WritePrimitive(encodable.ToAsn1Object(), true);
            FlushInternal();
        }

        public override void WriteObject(Asn1Object primitive)
        {
            if (null == primitive)
                throw new IOException("null object detected");

            WritePrimitive(primitive, true);
            FlushInternal();
        }

        internal void FlushInternal()
        {
            // Placeholder to support future internal buffering
        }

        internal virtual bool IsBer
        {
            get { return true; }
        }

        internal virtual bool IsDer
        {
            get { return false; }
        }

        internal void WriteDL(int length)
        {
            if (length < 128)
            {
                WriteByte((byte)length);
            }
            else
            {
                byte[] stack = new byte[5];
                int pos = stack.Length;

                do
                {
                    stack[--pos] = (byte)length;
                    length >>= 8;
                }
                while (length > 0);

                int count = stack.Length - pos;
                stack[--pos] = (byte)(0x80 | count);

                Write(stack, pos, count + 1);
            }
        }

        internal virtual void WriteElements(Asn1Encodable[] elements)
        {
            for (int i = 0, count = elements.Length; i < count; ++i)
            {
                elements[i].ToAsn1Object().Encode(this, true);
            }
        }

        internal void WriteEncodingDL(bool withID, int identifier, byte contents)
        {
            WriteIdentifier(withID, identifier);
            WriteDL(1);
            WriteByte(contents);
        }

        internal void WriteEncodingDL(bool withID, int identifier, byte[] contents)
        {
            WriteIdentifier(withID, identifier);
            WriteDL(contents.Length);
            Write(contents, 0, contents.Length);
        }

        internal void WriteEncodingDL(bool withID, int identifier, byte[] contents, int contentsOff, int contentsLen)
        {
            WriteIdentifier(withID, identifier);
            WriteDL(contentsLen);
            Write(contents, contentsOff, contentsLen);
        }

        internal void WriteEncodingDL(bool withID, int identifier, byte contentsPrefix, byte[] contents,
            int contentsOff, int contentsLen)
        {
            WriteIdentifier(withID, identifier);
            WriteDL(1 + contentsLen);
            WriteByte(contentsPrefix);
            Write(contents, contentsOff, contentsLen);
        }

        internal void WriteEncodingDL(bool withID, int identifier, byte[] contents, int contentsOff, int contentsLen,
            byte contentsSuffix)
        {
            WriteIdentifier(withID, identifier);
            WriteDL(contentsLen + 1);
            Write(contents, contentsOff, contentsLen);
            WriteByte(contentsSuffix);
        }

        internal void WriteEncodingDL(bool withID, int flags, int tag, byte[] contents)
        {
            WriteIdentifier(withID, flags, tag);
            WriteDL(contents.Length);
            Write(contents, 0, contents.Length);
        }

        internal void WriteEncodingIL(bool withID, int identifier, Asn1Encodable[] elements)
        {
            WriteIdentifier(withID, identifier);
            WriteByte(0x80);
            WriteElements(elements);
            WriteByte(0x00);
            WriteByte(0x00);
        }

        internal void WriteIdentifier(bool withID, int identifier)
        {
            if (withID)
            {
                WriteByte((byte)identifier);
            }
        }

        internal void WriteIdentifier(bool withID, int flags, int tag)
        {
            if (!withID)
            {
                // Don't write the identifier
            }
            else if (tag < 31)
            {
                WriteByte((byte)(flags | tag));
            }
            else
            {
                byte[] stack = new byte[6];
                int pos = stack.Length;

                stack[--pos] = (byte)(tag & 0x7F);
                while (tag > 127)
                {
                    tag >>= 7;
                    stack[--pos] = (byte)(tag & 0x7F | 0x80);
                }

                stack[--pos] = (byte)(flags | 0x1F);

                Write(stack, pos, stack.Length - pos);
            }
        }

        internal virtual void WritePrimitive(Asn1Object primitive, bool withID)
        {
            primitive.Encode(this, withID);
        }

        internal virtual void WritePrimitives(Asn1Object[] primitives)
        {
            for (int i = 0, count = primitives.Length; i < count; ++i)
            {
                WritePrimitive(primitives[i], true);
            }
        }

        internal static int GetLengthOfDL(int dl)
        {
            if (dl < 128)
                return 1;

            int length = 2;
            while ((dl >>= 8) > 0)
            {
                ++length;
            }
            return length;
        }

        internal static int GetLengthOfEncodingDL(bool withID, int contentsLength)
        {
            return (withID ? 1 : 0) + GetLengthOfDL(contentsLength) + contentsLength;
        }

        internal static int GetLengthOfEncodingDL(bool withID, int tag, int contentsLength)
        {
            return (withID ? GetLengthOfIdentifier(tag) : 0) + GetLengthOfDL(contentsLength) + contentsLength;
        }

        internal static int GetLengthOfIdentifier(int tag)
        {
            if (tag < 31)
                return 1;

            int length = 2;
            while ((tag >>= 7) > 0)
            {
                ++length;
            }
            return length;
        }
    }
}