2
* FreeRDP: A Remote Desktop Protocol Implementation
3
* ASN.1 Packed Encoding Rules (BER)
5
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
24
#include <freerdp/crypto/per.h>
29
* @param length length
33
BOOL per_read_length(wStream* s, UINT16* length)
37
if (Stream_GetRemainingLength(s) < 1)
40
Stream_Read_UINT8(s, byte);
44
if (Stream_GetRemainingLength(s) < 1)
48
*length = (byte << 8);
49
Stream_Read_UINT8(s, byte);
63
* @param length length
66
void per_write_length(wStream* s, int length)
69
Stream_Write_UINT16_BE(s, (length | 0x8000));
71
Stream_Write_UINT8(s, length);
77
* @param choice choice
81
BOOL per_read_choice(wStream* s, BYTE* choice)
83
if (Stream_GetRemainingLength(s) < 1)
86
Stream_Read_UINT8(s, *choice);
93
* @param choice index of chosen field
96
void per_write_choice(wStream* s, BYTE choice)
98
Stream_Write_UINT8(s, choice);
102
* Read PER selection.
104
* @param selection selection
108
BOOL per_read_selection(wStream* s, BYTE* selection)
110
if (Stream_GetRemainingLength(s) < 1)
113
Stream_Read_UINT8(s, *selection);
118
* Write PER selection for OPTIONAL fields.
120
* @param selection bit map of selected fields
123
void per_write_selection(wStream* s, BYTE selection)
125
Stream_Write_UINT8(s, selection);
129
* Read PER number of sets.
131
* @param number number of sets
135
BOOL per_read_number_of_sets(wStream* s, BYTE* number)
137
if (Stream_GetRemainingLength(s) < 1)
140
Stream_Read_UINT8(s, *number);
145
* Write PER number of sets for SET OF.
147
* @param number number of sets
150
void per_write_number_of_sets(wStream* s, BYTE number)
152
Stream_Write_UINT8(s, number);
156
* Read PER padding with zeros.
161
BOOL per_read_padding(wStream* s, int length)
163
if (Stream_GetRemainingLength(s) < length)
166
Stream_Seek(s, length);
171
* Write PER padding with zeros.
176
void per_write_padding(wStream* s, int length)
180
for (i = 0; i < length; i++)
181
Stream_Write_UINT8(s, 0);
187
* @param integer integer
191
BOOL per_read_integer(wStream* s, UINT32* integer)
195
if (!per_read_length(s, &length))
198
if (Stream_GetRemainingLength(s) < length)
202
Stream_Read_UINT8(s, *integer);
203
else if (length == 2)
204
Stream_Read_UINT16_BE(s, *integer);
214
* @param integer integer
217
void per_write_integer(wStream* s, UINT32 integer)
221
per_write_length(s, 1);
222
Stream_Write_UINT8(s, integer);
224
else if (integer <= 0xFFFF)
226
per_write_length(s, 2);
227
Stream_Write_UINT16_BE(s, integer);
229
else if (integer <= 0xFFFFFFFF)
231
per_write_length(s, 4);
232
Stream_Write_UINT32_BE(s, integer);
237
* Read PER INTEGER (UINT16).
239
* @param integer integer
240
* @param min minimum value
244
BOOL per_read_integer16(wStream* s, UINT16* integer, UINT16 min)
246
if (Stream_GetRemainingLength(s) < 2)
249
Stream_Read_UINT16_BE(s, *integer);
251
if (*integer + min > 0xFFFF)
260
* Write PER INTEGER (UINT16).
262
* @param integer integer
263
* @param min minimum value
266
void per_write_integer16(wStream* s, UINT16 integer, UINT16 min)
268
Stream_Write_UINT16_BE(s, integer - min);
272
* Read PER ENUMERATED.
274
* @param enumerated enumerated
275
* @param count enumeration count
279
BOOL per_read_enumerated(wStream* s, BYTE* enumerated, BYTE count)
281
if (Stream_GetRemainingLength(s) < 1)
284
Stream_Read_UINT8(s, *enumerated);
286
/* check that enumerated value falls within expected range */
287
if (*enumerated + 1 > count)
294
* Write PER ENUMERATED.
296
* @param enumerated enumerated
297
* @param count enumeration count
301
void per_write_enumerated(wStream* s, BYTE enumerated, BYTE count)
303
Stream_Write_UINT8(s, enumerated);
307
* Read PER OBJECT_IDENTIFIER (OID).
309
* @param oid object identifier (OID)
313
BOOL per_read_object_identifier(wStream* s, BYTE oid[6])
319
if (!per_read_length(s, &length))
325
if (Stream_GetRemainingLength(s) < length)
328
Stream_Read_UINT8(s, t12); /* first two tuples */
329
a_oid[0] = (t12 >> 4);
330
a_oid[1] = (t12 & 0x0F);
332
Stream_Read_UINT8(s, a_oid[2]); /* tuple 3 */
333
Stream_Read_UINT8(s, a_oid[3]); /* tuple 4 */
334
Stream_Read_UINT8(s, a_oid[4]); /* tuple 5 */
335
Stream_Read_UINT8(s, a_oid[5]); /* tuple 6 */
337
if ((a_oid[0] == oid[0]) && (a_oid[1] == oid[1]) &&
338
(a_oid[2] == oid[2]) && (a_oid[3] == oid[3]) &&
339
(a_oid[4] == oid[4]) && (a_oid[5] == oid[5]))
350
* Write PER OBJECT_IDENTIFIER (OID)
352
* @param oid object identifier (oid)
355
void per_write_object_identifier(wStream* s, BYTE oid[6])
357
BYTE t12 = (oid[0] << 4) & (oid[1] & 0x0F);
358
Stream_Write_UINT8(s, 5); /* length */
359
Stream_Write_UINT8(s, t12); /* first two tuples */
360
Stream_Write_UINT8(s, oid[2]); /* tuple 3 */
361
Stream_Write_UINT8(s, oid[3]); /* tuple 4 */
362
Stream_Write_UINT8(s, oid[4]); /* tuple 5 */
363
Stream_Write_UINT8(s, oid[5]); /* tuple 6 */
370
* @param length string length
373
void per_write_string(wStream* s, BYTE* str, int length)
377
for (i = 0; i < length; i++)
378
Stream_Write_UINT8(s, str[i]);
382
* Read PER OCTET_STRING.
384
* @param oct_str octet string
385
* @param length string length
386
* @param min minimum length
390
BOOL per_read_octet_string(wStream* s, BYTE* oct_str, int length, int min)
396
if (!per_read_length(s, &mlength))
399
if (mlength + min != length)
402
if (Stream_GetRemainingLength(s) < length)
405
a_oct_str = Stream_Pointer(s);
406
Stream_Seek(s, length);
408
for (i = 0; i < length; i++)
410
if (a_oct_str[i] != oct_str[i])
418
* Write PER OCTET_STRING
420
* @param oct_str octet string
421
* @param length string length
422
* @param min minimum string length
425
void per_write_octet_string(wStream* s, BYTE* oct_str, int length, int min)
430
mlength = (length - min >= 0) ? length - min : min;
432
per_write_length(s, mlength);
434
for (i = 0; i < length; i++)
435
Stream_Write_UINT8(s, oct_str[i]);
439
* Read PER NumericString.
441
* @param num_str numeric string
442
* @param length string length
443
* @param min minimum string length
446
BOOL per_read_numeric_string(wStream* s, int min)
451
if (!per_read_length(s, &mlength))
454
length = (mlength + min + 1) / 2;
456
if (Stream_GetRemainingLength(s) < length)
459
Stream_Seek(s, length);
464
* Write PER NumericString.
466
* @param num_str numeric string
467
* @param length string length
468
* @param min minimum string length
471
void per_write_numeric_string(wStream* s, BYTE* num_str, int length, int min)
477
mlength = (length - min >= 0) ? length - min : min;
479
per_write_length(s, mlength);
481
for (i = 0; i < length; i += 2)
484
c2 = ((i + 1) < length) ? num_str[i + 1] : 0x30;
486
c1 = (c1 - 0x30) % 10;
487
c2 = (c2 - 0x30) % 10;
488
num = (c1 << 4) | c2;
490
Stream_Write_UINT8(s, num); /* string */