File: DEREncoding.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (342 lines) | stat: -rw-r--r-- 10,766 bytes parent folder | download | duplicates (7)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.IdentityModel.Tokens
{

    // ASN.1 DER. DER refers to Distinguished Encoding Rules.
    internal static class DEREncoding
    {
        // OID=1.2.840.113554.1.2.2 (Kerberos V5) { 0x06, 0x09, 0x2a, 0x86, 
        //      0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02 }

        // 1. 0x60 -- Tag for [APPLICATION 0] SEQUENCE; indicates that
        // -- constructed form, definite length encoding follows.
        // 2. Token length octets, specifying length of subsequent data
        // (i.e., the summed lengths of elements 3-5 in this list, and of the
        // mechanism-defined token object following the tag).  This element
        // comprises a variable number of octets:
        // 2a. If the indicated value is less than 128, it shall be
        // represented in a single octet with bit 8 (high order) set to
        // "0" and the remaining bits representing the value.
        // 2b. If the indicated value is 128 or more, it shall be
        // represented in two or more octets, with bit 8 of the first
        // octet set to "1" and the remaining bits of the first octet
        // specifying the number of additional octets.  The subsequent
        // octets carry the value, 8 bits per octet, most significant
        // digit first.  The minimum number of octets shall be used to
        // encode the length (i.e., no octets representing leading zeros
        // shall be included within the length encoding).

        // 3. 0x06 -- Tag for OBJECT IDENTIFIER

        // 4. Object identifier length -- length (number of octets) of
        // -- the encoded object identifier contained in element 5,
        // -- encoded per rules as described in 2a. and 2b. above.

        // 5. Object identifier octets -- variable number of octets,
        // -- encoded per ASN.1 BER rules:

        // 5a. The first octet contains the sum of two values: (1) the
        // top-level object identifier component, multiplied by 40
        // (decimal), and (2) the second-level object identifier
        // component.  This special case is the only point within an
        // object identifier encoding where a single octet represents
        // contents of more than one component.

        // 5b. Subsequent octets, if required, encode successively-lower
        // components in the represented object identifier.  A component's
        // encoding may span multiple octets, encoding 7 bits per octet
        // (most significant bits first) and with bit 8 set to "1" on all
        // but the final octet in the component's encoding.  The minimum
        // number of octets shall be used to encode each component (i.e.,
        // no octets representing leading zeros shall be included within a
        // component's encoding).

        // (Note: In many implementations, elements 3-5 may be stored and
        // referenced as a contiguous string constant.)


        static byte[] mech = new byte[] { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x12, 0x01, 0x02, 0x02 };
        static byte[] type = new byte[] { 0x01, 0x00 };

        private static bool BufferIsEqual(byte[] arrayOne, int offsetOne, byte[] arrayTwo, int offsetTwo, int length)
        {
            if (length > arrayOne.Length - offsetOne)
            {
                return false;
            }

            if (length > arrayTwo.Length - offsetTwo)
            {
                return false;
            }

            for (int i = 0; i < length; i++)
            {
                if (arrayOne[offsetOne + i] != arrayTwo[offsetTwo + i])
                {
                    return false;
                }
            }

            return true;
        }

        //
        // length is assumed to be non negative
        //

        public static int LengthSize(int length)
        {

            if (length < (1 << 7))
            {

                return 1;
            }
            else if (length < (1 << 8))
            {

                return 2;
            }
            else if (length < (1 << 16))
            {

                return 3;
            }
            else if (length < (1 << 24))
            {

                return 4;
            }
            else
            {

                return 5;
            }
        }

        //
        // fills in a buffer with the token header.  The buffer is assumed 
        // to be the right size.  buffer is advanced past the token header 
        //
        // bodySize includes TokenId
        // 

        public static void MakeTokenHeader(int bodySize, byte[] buffer, ref int offset, ref int len)
        {

            buffer[offset++] = 0x60;
            len--;

            WriteLength(buffer, ref offset, ref len, 1 + LengthSize(mech.Length) + mech.Length + type.Length + bodySize);

            buffer[offset++] = 0x06; // OID
            len--;

            WriteLength(buffer, ref offset, ref len, mech.Length);

            System.Buffer.BlockCopy(mech, 0, buffer, offset, mech.Length);
            offset += mech.Length;
            len -= mech.Length;

            System.Buffer.BlockCopy(type, 0, buffer, offset, type.Length);
            offset += type.Length;
            len -= type.Length;
        }

        //
        // returns decoded length, or < 0 on failure.  Advances buffer and
        // decrements length
        //

        public static int ReadLength(byte[] buffer, ref int offset, ref int length)
        {

            int tmp;
            int ret = 0;

            if (length < 1)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            tmp = buffer[offset++];

            length--;

            if ((tmp & 0x80) != 0)
            {

                if ((tmp &= 0x7f) > (length - 1))
                {

                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
                }

                if (tmp > 4)
                { // 4 == sizeof(int)

                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
                }

                for (; tmp != 0; tmp--)
                {

                    ret = (ret << 8) + buffer[offset++];
                    length--;
                }

            }
            else
            {

                ret = tmp;
            }

            return ret;
        }

        //
        // returns the length of a token, given the mech oid and the body
        // size 
        //

        public static int TokenSize(int bodySize)
        {

            // set body size to sequence contents size, 2 for token id
            bodySize += 2 + mech.Length + LengthSize(mech.Length) + 1;

            return (1 + LengthSize(bodySize) + bodySize);
        }

        //
        // given a buffer containing a token, reads and verifies the token,
        // leaving buffer advanced past the token header, and setting body_size
        // to the number of remaining bytes
        //

        public static void VerifyTokenHeader(byte[] buffer, ref int offset, ref int len)
        {

            if ((len -= 1) < 0)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            if (buffer[offset++] != 0x60)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            int seqSize = ReadLength(buffer, ref offset, ref len);

            if (seqSize != len)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            if ((len -= 1) < 0)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            if (buffer[offset++] != 0x06)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            int oidLength = ReadLength(buffer, ref offset, ref len); // (byte) buffer[offset++];

            if ((oidLength & 0x7fffffff) != mech.Length)
            { // Overflow??? 

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            if ((len -= oidLength) < 0)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            if (!BufferIsEqual(mech, 0, buffer, offset, mech.Length))
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            offset += oidLength;

            if ((len -= type.Length) < 0)
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            if (!BufferIsEqual(type, 0, buffer, offset, type.Length))
            {

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SystemException());
            }

            offset += type.Length;
        }

        public static void WriteLength(byte[] buffer, ref int offset, ref int bufferLength, int length)
        {

            if (length < (1 << 7))
            { // one byte

                // *(*buffer)++ = (unsigned char) length;
                buffer[offset++] = (byte)length;
                bufferLength--;
            }
            else
            {

                // *(*buffer)++ = (unsigned char) (der_length_size(length) + 127);
                buffer[offset++] = (byte)(LengthSize(length) + 127);

                if (length >= (1 << 24))
                {

                    // *(*buffer)++ = (unsigned char) (length >> 24);
                    buffer[offset++] = (byte)(length >> 24);
                    bufferLength--;
                }

                if (length >= (1 << 16))
                {

                    // *(*buffer)++ = (unsigned char) ((length >> 16) & 0xff);
                    buffer[offset++] = (byte)((length >> 16) & 0xFF);
                    bufferLength--;
                }

                if (length >= (1 << 8))
                {

                    // *(*buffer)++ = (unsigned char) ((length >> 8) & 0xff);
                    buffer[offset++] = (byte)((length >> 8) & 0xFF);
                    bufferLength--;
                }

                // *(*buffer)++ = (unsigned char) (length & 0xff);
                buffer[offset++] = (byte)(length & 0xFF);
                bufferLength--;
            }
        }
    }
}