ASN 1 decoding

Hi,

I am developing a pos terminal application using "C" language. In that case I have to communicate with a mobile device using RSA certificates. But terminal kernel not support for X.509 certificates (I mean there are no specific functions to do signing and verification). So I have to do ASN 1 decoding using a "C" program for get the modules and the public exponent. Do you guys have a sample code for ASN decoding? or any other suggestions?

Thank you.
The OpenSSL library has X509 certificate handling built in.
Hi,

Thanks for your reply. My application is based on a POS terminal. So if I have a simple C code for ANS decoding it will easiest for me and the terminal.

Thank you.
Hi,

Thanks for your reply. Finally I wrote a simple code for this.

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
/*
 ****************************************************************************
 *      Author: Dushantha Bandara Rathnayake
 *       email: dushantha12@gmail.com
 *        File: asn1_decoder.c
 *  Created on: Jan 22, 2015
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define RET_TAG_NEXT_SAME_LEVEL_THIS_OBJECT_ONLY			0
#define RET_TAG_NEXT_SAME_LEVEL_OBJECT_CONTAINS				1

struct ASN1{
	unsigned char tag;
	int length;
	char data[2048];
	char value[2048];
	short activeStatus;
};

struct X509_TAG_OBJECT{
	int numberOfTagElements;
	int numberOfasnElements;
	struct ASN1 asnObjectList[32];
};

unsigned is_bit_set(unsigned value, unsigned bitindex)
{
	return (value & (1 << bitindex)) != 0;
}

int ipow(int base, int exp)
{
	int result = 1;
	while (exp)
	{
		if (exp & 1)
			result *= base;
		exp >>= 1;
		base *= base;
	}

	return result;
}

int read_generic_tag(char *hexStr,struct ASN1 *asnObject,unsigned char tag,int *nextPointIndex,int *endPointIndex,int *lengthOfData){
	char data[10240];
	int length = 0;
	int recvStrFullLength = 0;
	int startIndex = 0;
	int i = 0;
	int j = 0;

	recvStrFullLength = strlen(hexStr);

	memcpy(data,hexStr+2,2);
	data[2] = '\0';

	startIndex = 4;

	if(0 == strcmp("82",data)){
		memcpy(data,hexStr+4,4);
		data[4] = '\0';
		startIndex = 8;
	}else if(0 == strcmp("81",data)){
		memcpy(data,hexStr+4,2);
		data[2] = '\0';
		startIndex = 6;
	}

	length = (int)strtol(data, NULL, 16);

	asnObject->tag = tag;
	asnObject->length = length;
	memcpy(asnObject->data,hexStr+startIndex,(length*2));
	asnObject->data[(length*2)] = '\0';

	if(0x06 == tag){
		int tmpLen = 0;
		char temp[64];
		char tempTwo[64];
		int valueSet[10];
		int numberOfItem = 0;
		int tempVal = 0;

		strcpy(data,asnObject->data);
		strcpy(tempTwo,"");

		for (i = 0; i < strlen(data); i += 2) {
			strcpy(temp,"");
			memcpy(temp,data+i,2);
			temp[2] = '\0';

			tmpLen = (int)strtol(temp, NULL, 16);

			if(128 > tmpLen){

				if(0 == i && 40 <= tmpLen){
					if(0 != tmpLen%40){
						tempVal = (tmpLen - (tmpLen%40))/40;
						sprintf(temp,"%d",tempVal);
						strcat(tempTwo,temp);
						strcat(tempTwo,".");
						sprintf(temp,"%d",(tmpLen%40));
						strcat(tempTwo,temp);
					}else{
						tempVal = tmpLen/40;
						sprintf(temp,"%d",tempVal);
						strcat(tempTwo,temp);
						strcat(tempTwo,".0");
					}
				}else{
					sprintf(temp,"%d",tmpLen);
					strcat(tempTwo,temp);
				}

				strcat(tempTwo,".");
			}else{
				numberOfItem = 0;

				strcpy(temp,"");
				memcpy(temp,data+i+1,1);
				temp[1] = '\0';

				valueSet[numberOfItem] = atoi(temp);
				numberOfItem++;

				for (j = (i+2); j <= strlen(data); j += 2) {
					strcpy(temp,"");
					memcpy(temp,data+j,2);
					temp[2] = '\0';

					tmpLen = (int)strtol(temp, NULL, 16);

					if(128 <= tmpLen && 143 >= tmpLen){
						break;
					}else if(!is_bit_set(tmpLen,7)){
						i += 2;
						valueSet[numberOfItem] = tmpLen;
						numberOfItem++;
						break;
					}

					tmpLen &= ~(1 << 7);

					valueSet[numberOfItem] = tmpLen;

					i += 2;
					numberOfItem++;
				}

				tmpLen = 0;

				for (j = (numberOfItem-1); j >= 0; --j) {
					tmpLen += (valueSet[j] * ipow(2,(7*((numberOfItem-1)-j))));
				}

				sprintf(temp,"%d",tmpLen);
				strcat(tempTwo,temp);
				strcat(tempTwo,".");
			}
		}

		tempTwo[strlen(tempTwo)-1] = '\0';
		strcpy(asnObject->value,tempTwo);

		memcpy(temp,hexStr+(startIndex+(length*2)),4);
		temp[4] = '\0';

		if(0 == strcmp("0500",temp)){
			length += 2;
		}
	}else if(0x13 == tag || 0x14 == tag){
		unsigned byteData[256];

		memset(byteData,0x00,sizeof(byteData));

		strcpy(data,asnObject->data);

		convertHexStringToByte(data,byteData);

		sprintf(data,"%s",byteData);
		strcpy(asnObject->value,data);
	}else if(0x01 == tag){
		if(0 == strcasecmp("FF",asnObject->data)){
			strcpy(asnObject->value,"TRUE");
		}else{
			strcpy(asnObject->value,"FALSE");
		}
	}

	*nextPointIndex = startIndex;
	*endPointIndex  = (startIndex + (length*2));
	*lengthOfData 	= (length*2);

	if((recvStrFullLength-startIndex) == (length*2)){
		asnObject->activeStatus = 1;
		return RET_TAG_NEXT_SAME_LEVEL_THIS_OBJECT_ONLY;
	}else{
		asnObject->activeStatus = 0;
		return RET_TAG_NEXT_SAME_LEVEL_OBJECT_CONTAINS;
	}
}

int convertHexStringToByte(char *dataset,unsigned char *bytearray){
	int i = strlen(dataset),j=0,counter=0;
	char c[2];
	unsigned int bytes[2];

	memset(bytearray,0x00,sizeof(bytearray));

	for(j=0;j<i;j++){
		if(0 == j%2){

			c[0] = dataset[j];
			c[1] = dataset[j+1];

			sscanf(c, "%02x", &bytes[0]);

			bytearray[counter] = bytes[0];

			counter++;
		}
	}

	return counter;

}

int decode_hex_x509_certificate(char *hexStr){
	unsigned char byteData[5120];
	char hexStrCopy[10240];
	char data[10240];
	char tmpChar[10240];
	int startindex = 0;
	int startPoint = 0;
	int endPoint = 0;
	int fullStrLen = 0;
	int lastLen = 0;
	int endIndex = 0;
	int length = 0;
	int retVal = 0;
	short splitStatus = 0;
	unsigned char tag;

	struct ASN1 asnList[256];
	int asnObjectCounter = 0;
	int indexList[256];
	int indexListSize = 0;
	int i = 0;

	memset(byteData,0x00,sizeof(byteData));

	strcpy(hexStrCopy,hexStr);
	strcpy(data,hexStrCopy);

	convertHexStringToByte(data,byteData);

	strcpy(data,hexStrCopy);

	fullStrLen = strlen(hexStrCopy);

	while (1) {
		struct ASN1 asn;
		splitStatus = 0;

		tag = byteData[startindex];

		switch(byteData[startindex]){
		case 0x30:
			retVal = read_generic_tag(data,&asn,0x30,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x02:
			retVal = read_generic_tag(data,&asn,0x02,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x06:
			retVal = read_generic_tag(data,&asn,0x06,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x31:
			retVal = read_generic_tag(data,&asn,0x31,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x13:
			retVal = read_generic_tag(data,&asn,0x13,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x17:
			retVal = read_generic_tag(data,&asn,0x17,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x18:
			retVal = read_generic_tag(data,&asn,0x18,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x03:
			retVal = read_generic_tag(data,&asn,0x03,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x04:
			retVal = read_generic_tag(data,&asn,0x04,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x14:
			retVal = read_generic_tag(data,&asn,0x14,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		case 0x01:
			retVal = read_generic_tag(data,&asn,0x01,&startPoint,&endPoint,&length);
			splitStatus = 1;
			break;
		default:
			sprintf(tmpChar,"%02X",byteData[startindex]);

			if('A' == tmpChar[0]){
				retVal = read_generic_tag(data,&asn,byteData[startindex],&startPoint,&endPoint,&length);
				splitStatus = 1;
			}
			break;
		}

		if(splitStatus){

			startindex 	= ((lastLen+startPoint)/2);
			endIndex 	= ((lastLen+endPoint)/2);

			if(RET_TAG_NEXT_SAME_LEVEL_OBJECT_CONTAINS == retVal){
				indexList[indexListSize] = endIndex;
				indexListSize++;
			}

			asnList[asnObjectCounter] = asn;
			asnObjectCounter++;

			if(0x04 == tag || 0x03 == tag || 0x02 == tag || 0x01 == tag || 0x14 == tag || 0x17 == tag || 0x18 == tag){

				if(fullStrLen == (endIndex*2)){
					break;
				}

				if(0 < indexListSize){
					indexListSize--;
					startindex = indexList[indexListSize];
					length = fullStrLen - (startindex*2);

				}else{
					break;
				}

			}

		}else{
			if(0 < indexListSize){
				indexListSize--;
				startindex = indexList[indexListSize];
				length = fullStrLen - (startindex*2);

			}else{
				break;
			}
		}

		lastLen = (startindex*2);

		strcpy(data,"");
		memcpy(data,hexStrCopy+(startindex*2),length);
		data[length] = '\0';

	}

	return 0;
}


Thank you.
Topic archived. No new replies allowed.