/
BerConverter.cs
640 lines (561 loc) · 24.8 KB
/
BerConverter.cs
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace System.DirectoryServices.Protocols
{
public static partial class BerConverter
{
public static byte[] Encode(string format, params object[] value)
{
ArgumentNullException.ThrowIfNull(format);
// no need to turn on invalid encoding detection as we just do string->byte[] conversion.
UTF8Encoding utf8Encoder = new UTF8Encoding();
byte[] encodingResult = null;
// value is allowed to be null in certain scenario, so if it is null, just set it to empty array.
value ??= Array.Empty<object>();
Debug.WriteLine("Begin encoding\n");
// allocate the berelement
SafeBerHandle berElement = new SafeBerHandle();
int valueCount = 0;
int error = 0;
// We can't use vararg on Unix and can't do ber_printf(tag), so we use ber_put_int(val, tag)
// and this local keeps tag value for the next element.
nuint tag = 0;
bool tagIsSet = false;
for (int formatCount = 0; formatCount < format.Length; formatCount++)
{
if (tagIsSet)
{
tagIsSet = false;
}
else
{
int lberTagDefaultInt = -1;
nuint lberTagDefaultNuint = (nuint)lberTagDefaultInt;
tag = lberTagDefaultNuint;
}
char fmt = format[formatCount];
if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n')
{
// no argument needed
error = BerPal.PrintEmptyArgument(berElement, new string(fmt, 1), tag);
}
else if (fmt == 'i' || fmt == 'e')
{
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (!(value[valueCount] is int))
{
// argument is wrong
Debug.WriteLine("type should be int\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
// one int argument
error = BerPal.PrintInt(berElement, new string(fmt, 1), (int)value[valueCount], tag);
// increase the value count
valueCount++;
}
else if (fmt == 'b')
{
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (!(value[valueCount] is bool))
{
// argument is wrong
Debug.WriteLine("type should be boolean\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
// one int argument
error = BerPal.PrintInt(berElement, new string(fmt, 1), (bool)value[valueCount] ? 1 : 0, tag);
// increase the value count
valueCount++;
}
else if (fmt == 's')
{
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (value[valueCount] != null && !(value[valueCount] is string))
{
// argument is wrong
Debug.WriteLine("type should be string, but receiving value has type of ");
Debug.WriteLine(value[valueCount].GetType());
throw new ArgumentException(SR.BerConverterNotMatch);
}
// one string argument
byte[] tempValue = null;
if (value[valueCount] != null)
{
tempValue = utf8Encoder.GetBytes((string)value[valueCount]);
}
error = EncodingByteArrayHelper(berElement, tempValue, 'o', tag);
// increase the value count
valueCount++;
}
else if (fmt == 'o' || fmt == 'X')
{
// we need to have one arguments
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (value[valueCount] != null && !(value[valueCount] is byte[]))
{
// argument is wrong
Debug.WriteLine("type should be byte[], but receiving value has type of ");
Debug.WriteLine(value[valueCount].GetType());
throw new ArgumentException(SR.BerConverterNotMatch);
}
byte[] tempValue = (byte[])value[valueCount];
error = EncodingByteArrayHelper(berElement, tempValue, fmt, tag);
valueCount++;
}
else if (fmt == 'v')
{
// we need to have one arguments
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (value[valueCount] != null && !(value[valueCount] is string[]))
{
// argument is wrong
Debug.WriteLine("type should be string[], but receiving value has type of ");
Debug.WriteLine(value[valueCount].GetType());
throw new ArgumentException(SR.BerConverterNotMatch);
}
string[] stringValues = (string[])value[valueCount];
byte[][] tempValues;
if (stringValues != null)
{
tempValues = new byte[stringValues.Length][];
for (int i = 0; i < stringValues.Length; i++)
{
string s = stringValues[i];
if (s == null)
tempValues[i] = null;
else
{
tempValues[i] = utf8Encoder.GetBytes(s);
}
error = EncodingByteArrayHelper(berElement, tempValues[i], 'o', tag);
if (error == -1)
{
break;
}
}
}
valueCount++;
}
else if (fmt == 'V')
{
// we need to have one arguments
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (value[valueCount] != null && !(value[valueCount] is byte[][]))
{
// argument is wrong
Debug.WriteLine("type should be byte[][], but receiving value has type of ");
Debug.WriteLine(value[valueCount].GetType());
throw new ArgumentException(SR.BerConverterNotMatch);
}
byte[][] tempValue = (byte[][])value[valueCount];
error = EncodingMultiByteArrayHelper(berElement, tempValue, fmt, tag);
valueCount++;
}
else if (fmt == 't')
{
if (valueCount >= value.Length)
{
// we don't have enough argument for the format string
Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
if (!(value[valueCount] is int))
{
// argument is wrong
Debug.WriteLine("type should be int\n");
throw new ArgumentException(SR.BerConverterNotMatch);
}
tag = (uint)(int)value[valueCount];
tagIsSet = true;
// It will set the tag on Windows and only check the tag on Unix.
error = BerPal.PrintTag(berElement, new string(fmt, 1), tag);
// increase the value count
valueCount++;
}
else
{
Debug.WriteLine("Format string contains undefined character: ");
Debug.WriteLine(new string(fmt, 1));
throw new ArgumentException(SR.BerConverterUndefineChar);
}
// process the return value
if (error == -1)
{
Debug.WriteLine("ber_printf failed\n");
throw new BerConversionException();
}
}
// get the binary value back
BerVal binaryValue = new BerVal();
IntPtr flattenptr = IntPtr.Zero;
try
{
// can't use SafeBerval here as CLR creates a SafeBerval which points to a different memory location, but when doing memory
// deallocation, wldap has special check. So have to use IntPtr directly here.
error = BerPal.FlattenBerElement(berElement, ref flattenptr);
if (error == -1)
{
Debug.WriteLine("ber_flatten failed\n");
throw new BerConversionException();
}
if (flattenptr != IntPtr.Zero)
{
Marshal.PtrToStructure(flattenptr, binaryValue);
}
if (binaryValue == null || binaryValue.bv_len == 0)
{
encodingResult = Array.Empty<byte>();
}
else
{
encodingResult = new byte[binaryValue.bv_len];
Marshal.Copy(binaryValue.bv_val, encodingResult, 0, binaryValue.bv_len);
}
}
finally
{
if (flattenptr != IntPtr.Zero)
BerPal.FreeBerval(flattenptr);
}
return encodingResult;
}
public static object[] Decode(string format, byte[] value)
{
bool decodeSucceeded;
object[] decodeResult = TryDecode(format, value, out decodeSucceeded);
if (decodeSucceeded)
return decodeResult;
else
throw new BerConversionException();
}
internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
{
ArgumentNullException.ThrowIfNull(format);
Debug.WriteLine("Begin decoding");
UTF8Encoding utf8Encoder = new UTF8Encoding(false, true);
BerVal berValue = new BerVal();
ArrayList resultList = new ArrayList();
SafeBerHandle berElement = null;
object[] decodeResult = null;
decodeSucceeded = false;
if (value == null)
{
berValue.bv_len = 0;
berValue.bv_val = IntPtr.Zero;
}
else
{
berValue.bv_len = value.Length;
berValue.bv_val = Marshal.AllocHGlobal(value.Length);
Marshal.Copy(value, 0, berValue.bv_val, value.Length);
}
try
{
berElement = new SafeBerHandle(berValue);
}
finally
{
if (berValue.bv_val != IntPtr.Zero)
Marshal.FreeHGlobal(berValue.bv_val);
}
int error;
for (int formatCount = 0; formatCount < format.Length; formatCount++)
{
char fmt = format[formatCount];
if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n' || fmt == 'x')
{
error = BerPal.ScanNext(berElement, new string(fmt, 1));
if (BerPal.IsBerDecodeError(error))
Debug.WriteLine("ber_scanf for {, }, [, ], n or x failed");
}
else if (fmt == 'i' || fmt == 'e' || fmt == 'b')
{
int result = 0;
error = BerPal.ScanNextInt(berElement, new string(fmt, 1), ref result);
if (!BerPal.IsBerDecodeError(error))
{
if (fmt == 'b')
{
// should return a bool
bool boolResult;
if (result == 0)
boolResult = false;
else
boolResult = true;
resultList.Add(boolResult);
}
else
{
resultList.Add(result);
}
}
else
Debug.WriteLine("ber_scanf for format character 'i', 'e' or 'b' failed");
}
else if (fmt == 'a')
{
// return a string
byte[] byteArray = DecodingByteArrayHelper(berElement, 'O', out error);
if (!BerPal.IsBerDecodeError(error))
{
string s = null;
if (byteArray != null)
s = utf8Encoder.GetString(byteArray);
resultList.Add(s);
}
}
else if (fmt == 'O')
{
// return BerVal
byte[] byteArray = DecodingByteArrayHelper(berElement, fmt, out error);
if (!BerPal.IsBerDecodeError(error))
{
// add result to the list
resultList.Add(byteArray);
}
}
else if (fmt == 'B')
{
error = DecodeBitStringHelper(resultList, berElement);
}
else if (fmt == 'v')
{
//null terminate strings
byte[][] byteArrayresult;
string[] stringArray = null;
byteArrayresult = DecodingMultiByteArrayHelper(berElement, 'V', out error);
if (!BerPal.IsBerDecodeError(error))
{
if (byteArrayresult != null)
{
stringArray = new string[byteArrayresult.Length];
for (int i = 0; i < byteArrayresult.Length; i++)
{
if (byteArrayresult[i] == null)
{
stringArray[i] = null;
}
else
{
stringArray[i] = utf8Encoder.GetString(byteArrayresult[i]);
}
}
}
resultList.Add(stringArray);
}
}
else if (fmt == 'V')
{
byte[][] result;
result = DecodingMultiByteArrayHelper(berElement, fmt, out error);
if (!BerPal.IsBerDecodeError(error))
{
resultList.Add(result);
}
}
else
{
Debug.WriteLine("Format string contains undefined character\n");
throw new ArgumentException(SR.BerConverterUndefineChar);
}
if (BerPal.IsBerDecodeError(error))
{
// decode failed, just return
return decodeResult;
}
}
decodeResult = new object[resultList.Count];
for (int count = 0; count < resultList.Count; count++)
{
decodeResult[count] = resultList[count];
}
decodeSucceeded = true;
return decodeResult;
}
private static int EncodingByteArrayHelper(SafeBerHandle berElement, byte[] tempValue, char fmt, nuint tag)
{
int error;
// one byte array, one int arguments
if (tempValue != null)
{
IntPtr tmp = Marshal.AllocHGlobal(tempValue.Length);
Marshal.Copy(tempValue, 0, tmp, tempValue.Length);
HGlobalMemHandle memHandle = new HGlobalMemHandle(tmp);
error = BerPal.PrintByteArray(berElement, new string(fmt, 1), memHandle, (uint)tempValue.Length, tag);
}
else
{
HGlobalMemHandle memHandle = new HGlobalMemHandle(HGlobalMemHandle._dummyPointer);
error = BerPal.PrintByteArray(berElement, new string(fmt, 1), memHandle, 0, tag);
}
return error;
}
private static byte[] DecodingByteArrayHelper(SafeBerHandle berElement, char fmt, out int error)
{
IntPtr result = IntPtr.Zero;
BerVal binaryValue = new BerVal();
byte[] byteArray = null;
// can't use SafeBerval here as CLR creates a SafeBerval which points to a different memory location, but when doing memory
// deallocation, wldap has special check. So have to use IntPtr directly here.
error = BerPal.ScanNextPtr(berElement, new string(fmt, 1), ref result);
try
{
if (!BerPal.IsBerDecodeError(error))
{
if (result != IntPtr.Zero)
{
Marshal.PtrToStructure(result, binaryValue);
byteArray = new byte[binaryValue.bv_len];
Marshal.Copy(binaryValue.bv_val, byteArray, 0, binaryValue.bv_len);
}
}
else
Debug.WriteLine("ber_scanf for format character 'O' failed");
}
finally
{
if (result != IntPtr.Zero)
BerPal.FreeBerval(result);
}
return byteArray;
}
private static unsafe int EncodingMultiByteArrayHelper(SafeBerHandle berElement, byte[][] tempValue, char fmt, nuint tag)
{
IntPtr berValArray = IntPtr.Zero;
BerVal[] managedBervalArray = null;
int error = 0;
try
{
if (tempValue != null)
{
int i = 0;
berValArray = Utility.AllocHGlobalIntPtrArray(tempValue.Length + 1);
int structSize = Marshal.SizeOf(typeof(BerVal));
managedBervalArray = new BerVal[tempValue.Length];
void** pBerValArray = (void**)berValArray;
for (i = 0; i < tempValue.Length; i++)
{
byte[] byteArray = tempValue[i];
// construct the managed BerVal
managedBervalArray[i] = new BerVal();
if (byteArray != null)
{
managedBervalArray[i].bv_len = byteArray.Length;
managedBervalArray[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
Marshal.Copy(byteArray, 0, managedBervalArray[i].bv_val, byteArray.Length);
}
// allocate memory for the unmanaged structure
IntPtr valPtr = Marshal.AllocHGlobal(structSize);
Marshal.StructureToPtr(managedBervalArray[i], valPtr, false);
pBerValArray[i] = (void*)valPtr;
}
pBerValArray[i] = null;
}
error = BerPal.PrintBerArray(berElement, new string(fmt, 1), berValArray, tag);
}
finally
{
if (berValArray != IntPtr.Zero)
{
for (int i = 0; i < tempValue.Length; i++)
{
IntPtr ptr = Marshal.ReadIntPtr(berValArray, IntPtr.Size * i);
if (ptr != IntPtr.Zero)
Marshal.FreeHGlobal(ptr);
}
Marshal.FreeHGlobal(berValArray);
}
if (managedBervalArray != null)
{
foreach (BerVal managedBerval in managedBervalArray)
{
if (managedBerval.bv_val != IntPtr.Zero)
{
Marshal.FreeHGlobal(managedBerval.bv_val);
}
}
}
}
return error;
}
private static byte[][] DecodingMultiByteArrayHelper(SafeBerHandle berElement, char fmt, out int error)
{
// several BerVal
IntPtr ptrResult = IntPtr.Zero;
int i = 0;
ArrayList binaryList = new ArrayList();
IntPtr tempPtr;
byte[][] result = null;
try
{
error = BerPal.ScanNextPtr(berElement, new string(fmt, 1), ref ptrResult);
if (!BerPal.IsBerDecodeError(error))
{
if (ptrResult != IntPtr.Zero)
{
tempPtr = Marshal.ReadIntPtr(ptrResult);
while (tempPtr != IntPtr.Zero)
{
BerVal ber = new BerVal();
Marshal.PtrToStructure(tempPtr, ber);
byte[] berArray = new byte[ber.bv_len];
Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len);
binaryList.Add(berArray);
i++;
tempPtr = Marshal.ReadIntPtr(ptrResult, i * IntPtr.Size);
}
result = new byte[binaryList.Count][];
for (int j = 0; j < binaryList.Count; j++)
{
result[j] = (byte[])binaryList[j];
}
}
}
else
Debug.WriteLine("ber_scanf for format character 'V' failed");
}
finally
{
if (ptrResult != IntPtr.Zero)
{
BerPal.FreeBervalArray(ptrResult);
}
}
return result;
}
}
}