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
|
/*
* Copyright (C) 2000,2001 Fabio Fiorina
*
* This file is part of GNUTLS.
*
* GNUTLS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNUTLS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/*****************************************************/
/* File: CertificateExample.c */
/* Description: An example on how to use the ASN1 */
/* parser with the Certificate.txt file */
/*****************************************************/
#include <stdio.h>
#include <string.h>
#include "../lib/x509_asn1.h"
#include "../lib/x509_der.h"
extern static_asn pkix_asn1_tab[];
/******************************************************/
/* Function : get_name_type */
/* Description: analyze a structure of type Name */
/* Parameters: */
/* char *root: the structure identifier */
/* char *answer: the string with elements like: */
/* "C=US O=gov" */
/******************************************************/
void
get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
{
int k,k2,result,len;
char name[128],str[1024],str2[1024],name2[128],counter[5],name3[128];
node_asn *value;
answer[0]=0;
k=1;
do{
strcpy(name,root);
strcat(name,".rdnSequence.?");
_asn1_ltostr(k,counter);
strcat(name,counter);
len = sizeof(str) - 1;
result=asn1_read_value(cert,name,str,&len);
if(result==ASN_ELEMENT_NOT_FOUND) break;
k2=1;
do{
strcpy(name2,name);
strcat(name2,".?");
_asn1_ltostr(k2,counter);
strcat(name2,counter);
len = sizeof(str) - 1;
result=asn1_read_value(cert,name2,str,&len);
if(result==ASN_ELEMENT_NOT_FOUND) break;
strcpy(name3,name2);
strcat(name3,".type");
len = sizeof(str) - 1;
result=asn1_read_value(cert,name3,str,&len);
strcpy(name3,name2);
strcat(name3,".value");
if(result==ASN_OK){
len = sizeof(str2) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-countryName",
str2,&len);
if(!strcmp(str,str2)){
asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName",
&value,"certificate2-subject-C");
len = sizeof(str) - 1;
asn1_read_value(cert,name3,str,&len);
asn1_get_der(value,str,len);
strcpy(name3,"certificate2-subject-C");
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len); /* CHOICE */
strcat(name3,".");
strcat(name3,str);
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
strcat(answer," C=");
strcat(answer,str);
asn1_delete_structure(value);
}
else{
len = sizeof(str2) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationName"
,str2,&len);
if(!strcmp(str,str2)){
asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName"
,&value,"certificate2-subject-O");
len = sizeof(str) - 1;
asn1_read_value(cert,name3,str,&len);
asn1_get_der(value,str,len);
strcpy(name3,"certificate2-subject-O");
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len); /* CHOICE */
strcat(name3,".");
strcat(name3,str);
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
strcat(answer," O=");
strcat(answer,str);
asn1_delete_structure(value);
}
else{
len = sizeof(str2) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationalUnitName",str2,&len);
if(!strcmp(str,str2)){
asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value,"certificate2-subject-OU");
len = sizeof(str) - 1;
asn1_read_value(cert,name3,str,&len);
asn1_get_der(value,str,len);
strcpy(name3,"certificate2-subject-OU");
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len); /* CHOICE */
strcat(name3,".");
strcat(name3,str);
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
strcat(answer," OU=");
strcat(answer,str);
asn1_delete_structure(value);
}
}
}
}
k2++;
}while(1);
k++;
}while(1);
}
/******************************************************/
/* Function : create_certificate */
/* Description: creates a certificate named */
/* "certificate1". Values are the same */
/* as in rfc2459 Appendix D.1 */
/* Parameters: */
/* unsigned char *der: contains the der encoding */
/* int *der_len: number of bytes of der string */
/******************************************************/
void
create_certificate(node_asn *cert_def,unsigned char *der,int *der_len)
{
int result,k,len;
unsigned char str[1024],*str2;
node_asn *cert1,*value,*param,*constr;
result=asn1_create_structure(cert_def,"PKIX1Implicit88.Certificate",&cert1,"certificate1");
/* Use the next 3 lines to visit the empty certificate */
/* printf("-----------------\n");
asn1_visit_tree(cert1,"certificate1");
printf("-----------------\n"); */
/* version: v3(2) */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.version","v3",0);
/* serialNumber: 17 */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.serialNumber","17",0);
/* signature: dsa-with-sha1 */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-dsa-with-sha1",str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.signature.algorithm",
str,1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.signature.parameters",
NULL,0);
/* issuer: Country="US" Organization="gov" OrganizationUnit="nist" */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer","rdnSequence",12);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence","NEW",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST","NEW",1);
/* C */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-countryName",str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520countryName",
&value,"countryName");
result=asn1_write_value(value,"countryName","US",2);
result=asn1_create_der(value,"countryName",der,der_len);
asn1_delete_structure(value);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence","NEW",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST","NEW",1);
/* O */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationName",str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName",
&value,"OrgName");
result=asn1_write_value(value,"OrgName","printableString",1);
result=asn1_write_value(value,"OrgName.printableString","gov",3);
result=asn1_create_der(value,"OrgName",der,der_len);
asn1_delete_structure(value);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence","NEW",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST","NEW",1);
/* OU */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationalUnitName",
str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value,"OrgUnitName");
result=asn1_write_value(value,"OrgUnitName","printableString",1);
result=asn1_write_value(value,"OrgUnitName.printableString","nist",4);
result=asn1_create_der(value,"OrgUnitName",der,der_len);
asn1_delete_structure(value);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
/* validity */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.validity.notBefore","utcTime",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.validity.notBefore.utcTime","970630000000Z",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.validity.notAfter","utcTime",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.validity.notAfter.utcTime","971231000000Z",1);
/* subject: Country="US" Organization="gov" OrganizationUnit="nist" */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject","rdnSequence",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence","NEW",1);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST","NEW",1);
/* C */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-countryName",str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520countryName",
&value,"countryName");
result=asn1_write_value(value,"countryName","US",2);
result=asn1_create_der(value,"countryName",der,der_len);
asn1_delete_structure(value);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.value",der,*der_len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence","NEW",4);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST","NEW",4);
/* O */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationName",str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName",
&value,"OrgName");
result=asn1_write_value(value,"OrgName","printableString",1);
result=asn1_write_value(value,"OrgName.printableString","gov",3);
result=asn1_create_der(value,"OrgName",der,der_len);
asn1_delete_structure(value);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.value",der,*der_len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence","NEW",4);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST","NEW",4);
/* OU */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationalUnitName",
str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value,"OrgUnitName");
result=asn1_write_value(value,"OrgUnitName","printableString",1);
result=asn1_write_value(value,"OrgUnitName.printableString","nist",4);
result=asn1_create_der(value,"OrgUnitName",der,der_len);
asn1_delete_structure(value);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.value",der,*der_len);
/* subjectPublicKeyInfo: dsa with parameters=Dss-Parms */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-dsa",str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subjectPublicKeyInfo.algorithm.algorithm",str,1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.Dss-Parms",¶m,"parameters");
str2="\xd4\x38"; /* only an example */
result=asn1_write_value(param,"parameters.p",str2,128);
str2="\xd4\x38"; /* only an example */
result=asn1_write_value(param,"parameters.q",str2,20);
str2="\xd4\x38"; /* only an example */
result=asn1_write_value(param,"parameters.g",str2,128);
result=asn1_create_der(param,"parameters",der,der_len);
asn1_delete_structure(param);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subjectPublicKeyInfo.algorithm.parameters",der,*der_len);
/* subjectPublicKey */
str2="\x02\x81"; /* only an example */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey",str2,1048);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuerUniqueID",NULL,0); /* NO OPTION */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.subjectUniqueID",NULL,0); /* NO OPTION */
/* extensions */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions","NEW",1);
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-ce-basicConstraints",
str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.extnID",str,1); /* basicConstraints */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.critical","TRUE",1);
result=asn1_create_structure(cert_def,"PKIX1Implicit88.BasicConstraints",&constr,
"basicConstraints1");
result=asn1_write_value(constr,"basicConstraints1.cA","TRUE",1);
result=asn1_write_value(constr,"basicConstraints1.pathLenConstraint",NULL,0);
result=asn1_create_der(constr,"basicConstraints1",der,der_len);
result=asn1_delete_structure(constr);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.extnValue",der,*der_len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions","NEW",1);
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-ce-subjectKeyIdentifier",
str,&len);
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.extnID",str,1); /* subjectKeyIdentifier */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.critical","FALSE",1);
str2="\x04\x14\xe7\x26\xc5"; /* only an example */
result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.extnValue",str2,22);
/* signatureAlgorithm: dsa-with-sha */
len = sizeof(str) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-dsa-with-sha1",str,&len);
result=asn1_write_value(cert1,"certificate1.signatureAlgorithm.algorithm",str,1);
result=asn1_write_value(cert1,"certificate1.signatureAlgorithm.parameters",NULL,0); /* NO OPTION */
/* signature */
result=asn1_create_der(cert1,"certificate1.tbsCertificate",der,der_len);
if(result!=ASN_OK){
printf("\n'tbsCertificate' encoding creation: ERROR\n");
// return;
}
/* add the lines for the signature on der[0]..der[der_len-1]: result in str2 */
result=asn1_write_value(cert1,"certificate1.signature",str2,368); /* dsa-with-sha */
/* Use the next 3 lines to visit the certificate */
/* printf("-----------------\n");
asn1_visit_tree(cert1,"certificate1");
printf("-----------------\n"); */
result=asn1_create_der(cert1,"certificate1",der,der_len);
if(result!=ASN_OK){
printf("\n'certificate1' encoding creation: ERROR\n");
return;
}
/* Print the 'Certificate1' DER encoding */
printf("-----------------\nCertificate1 Encoding:\nNumber of bytes=%i\n",*der_len);
for(k=0;k<*der_len;k++) printf("%02x ",der[k]);
printf("\n-----------------\n");
/* Clear the "certificate1" structure */
asn1_delete_structure(cert1);
}
/******************************************************/
/* Function : get_certificate */
/* Description: creates a certificate named */
/* "certificate2" from a der encoding */
/* string */
/* Parameters: */
/* unsigned char *der: the encoding string */
/* int der_len: number of bytes of der string */
/******************************************************/
void
get_certificate(node_asn *cert_def,unsigned char *der,int der_len)
{
int result,len,start,end;
unsigned char str[1024],str2[1024];
node_asn *cert2;
asn1_create_structure(cert_def,"PKIX1Implicit88.Certificate",&cert2,"certificate2");
result=asn1_get_der(cert2,der,der_len);
if(result!=ASN_OK){
printf("Problems with DER encoding\n");
return;
}
/* issuer */
get_Name_type(cert_def,cert2,"certificate2.tbsCertificate.issuer",str);
printf("certificate2:\nissuer =%s\n",str);
/* subject */
get_Name_type(cert_def,cert2,"certificate2.tbsCertificate.subject",str);
printf("subject=%s\n",str);
/* Verify sign */
len = sizeof(str) - 1;
result=asn1_read_value(cert2,"certificate2.signatureAlgorithm.algorithm"
,str,&len);
len = sizeof(str2) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-dsa-with-sha1",str2,&len);
if(!strcmp(str,str2)){ /* dsa-with-sha */
result=asn1_get_start_end_der(cert2,der,der_len,
"certificate2.tbsCertificate",&start,&end);
/* add the lines to calculate the sha on der[start]..der[end] */
len = sizeof(str) - 1;
result=asn1_read_value(cert2,"certificate2.signature",str,&len);
/* compare the previous value to signature ( with issuer public key) */
}
/* Use the next 3 lines to visit the certificate */
/* printf("-----------------\n");
asn1_visit_tree(cert2,"certificate2");
printf("-----------------\n"); */
/* Clear the "certificate2" structure */
asn1_delete_structure(cert2);
}
/********************************************************/
/* Function : main */
/* Description: reads the certificate description. */
/* Creates a certificate and calculate */
/* the der encoding. After that creates */
/* another certificate from der string */
/********************************************************/
int
main(int argc,char *argv[])
{
int result,der_len;
unsigned char der[1024];
char file_name[128];
node_asn *PKIX1Implicit88;
result=asn1_create_tree(pkix_asn1_tab,&PKIX1Implicit88);
if(result==ASN_FILE_NOT_FOUND){
printf("FILE NOT FOUND\n");
return;
}
else if(result==ASN_SYNTAX_ERROR){
printf("PARSE ERROR\n");
return;
}
else if(result==ASN_IDENTIFIER_NOT_FOUND){
printf("IDENTIFIER NOT FOUND\n");
return;
}
/* Use the following 3 lines to visit the PKIX1Implicit structures */
/* printf("-----------------\n");
asn1_visit_tree(PKIX1Implicit88,"PKIX1Implicit88");
printf("-----------------\n"); */
create_certificate(PKIX1Implicit88,der,&der_len);
get_certificate(PKIX1Implicit88,der,der_len);
/* Clear the "PKIX1Implicit88" structures */
asn1_delete_structure(PKIX1Implicit88);
return;
}
|