diff options
Diffstat (limited to 'src/CrlExample.c')
-rw-r--r-- | src/CrlExample.c | 161 |
1 files changed, 95 insertions, 66 deletions
diff --git a/src/CrlExample.c b/src/CrlExample.c index cbb6156..8b3f0e5 100644 --- a/src/CrlExample.c +++ b/src/CrlExample.c @@ -27,8 +27,38 @@ #include <stdio.h> #include <string.h> -#include "../lib/asn1.h" -#include "../lib/der.h" +#include "libasn1.h" + + + +char * +my_ltostr(long v,char *str) +{ + long d,r; + char temp[20]; + int count,k,start; + + if(v<0){ + str[0]='-'; + start=1; + v=-v; + } + else start=0; + + count=0; + do{ + d=v/10; + r=v-d*10; + temp[start+count]='0'+(char)r; + count++; + v=d; + }while(v); + + for(k=0;k<count;k++) str[k+start]=temp[start+count-k-1]; + str[count+start]=0; + return str; +} + /******************************************************/ /* Function : get_name_type */ @@ -43,29 +73,30 @@ 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; + ASN1_TYPE value=ASN1_TYPE_EMPTY; + char errorDescription[MAX_ERROR_DESCRIPTION_SIZE]; answer[0]=0; k=1; do{ strcpy(name,root); strcat(name,".rdnSequence.?"); - _asn1_ltostr(k,counter); + my_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; + if(result==ASN1_ELEMENT_NOT_FOUND) break; k2=1; do{ strcpy(name2,name); strcat(name2,".?"); - _asn1_ltostr(k2,counter); + my_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; + if(result==ASN1_ELEMENT_NOT_FOUND) break; strcpy(name3,name2); strcat(name3,".type"); @@ -73,20 +104,21 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer) result=asn1_read_value(cert,name3,str,&len); strcpy(name3,name2); strcat(name3,".value"); - if(result==ASN_OK){ + if(result==ASN1_SUCCESS){ len = sizeof(str2); result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-countryName", str2,&len); if(!strcmp(str,str2)){ - asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName", + asn1_create_element(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); + result=asn1_der_decoding(&value,str,len,errorDescription); strcpy(name3,"certificate2-subject-C"); - + len = sizeof(str)-1; - asn1_read_value(value,name3,str,&len); /* CHOICE */ + asn1_read_value(value,name3,str,&len); /* CHOICE */ + strcat(name3,"."); strcat(name3,str); @@ -95,19 +127,20 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer) str[len]=0; strcat(answer," C="); strcat(answer,str); - asn1_delete_structure(value); + + asn1_delete_structure(&value); } else{ len = sizeof(str2); result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationName" ,str2,&len); if(!strcmp(str,str2)){ - asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName" + asn1_create_element(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); + asn1_der_decoding(&value,str,len,errorDescription); strcpy(name3,"certificate2-subject-O"); len = sizeof(str)-1; asn1_read_value(value,name3,str,&len); /* CHOICE */ @@ -118,16 +151,16 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer) str[len]=0; strcat(answer," O="); strcat(answer,str); - asn1_delete_structure(value); + asn1_delete_structure(&value); } else{ len = sizeof(str2); 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"); + asn1_create_element(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); + asn1_der_decoding(&value,str,len,errorDescription); strcpy(name3,"certificate2-subject-OU"); len = sizeof(str)-1; asn1_read_value(value,name3,str,&len); /* CHOICE */ @@ -138,7 +171,7 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer) str[len]=0; strcat(answer," OU="); strcat(answer,str); - asn1_delete_structure(value); + asn1_delete_structure(&value); } } } @@ -164,9 +197,11 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) { int result,k,len; unsigned char str[1024],*str2; - node_asn *crl,*value; + ASN1_TYPE crl=ASN1_TYPE_EMPTY; + ASN1_TYPE value=ASN1_TYPE_EMPTY; + char errorDescription[MAX_ERROR_DESCRIPTION_SIZE]; - result=asn1_create_structure(cert_def,"PKIX1Implicit88.CertificateList",&crl,"crl1"); + result=asn1_create_element(cert_def,"PKIX1Implicit88.CertificateList",&crl,"crl1"); /* Use the next 3 lines to visit the empty certificate */ /* printf("-----------------\n"); @@ -177,11 +212,11 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) /* version: v2(1) */ result=asn1_write_value(crl,"crl1.tbsCertList.version","v2",0); + /* signature: dsa-with-sha */ len = sizeof(str)-1; result=asn1_read_value(cert_def,"PKIX1Implicit88.id-dsa-with-sha1",str,&len); result=asn1_write_value(crl,"crl1.tbsCertList.signature.algorithm",str,1); - result=asn1_write_value(crl,"crl1.tbsCertList.signature.parameters",NULL,0); @@ -194,11 +229,12 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) len = sizeof(str)-1; result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-countryName",str,&len); result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,1); - result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520countryName", + result=asn1_create_element(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_der_coding(value,"countryName",der,der_len,errorDescription); + + asn1_delete_structure(&value); result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len); @@ -208,12 +244,12 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) len = sizeof(str)-1; result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationName",str,&len); result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,8); - result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationName", + result=asn1_create_element(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_der_coding(value,"OrgName",der,der_len,errorDescription); + asn1_delete_structure(&value); result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len); @@ -224,11 +260,11 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationalUnitName", str,&len); result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,1); - result=asn1_create_structure(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value,"OrgUnitName"); + result=asn1_create_element(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_der_coding(value,"OrgUnitName",der,der_len,errorDescription); + asn1_delete_structure(&value); result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len); @@ -268,8 +304,8 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) result=asn1_write_value(crl,"crl1.signatureAlgorithm.parameters",NULL,0); /* NO OPTION */ /* signature */ - result=asn1_create_der(crl,"crl1.tbsCertList",der,der_len); - if(result!=ASN_OK){ + result=asn1_der_coding(crl,"crl1.tbsCertList",der,der_len,errorDescription); + if(result!=ASN1_SUCCESS){ printf("\n'tbsCertList' encoding creation: ERROR\n"); return; } @@ -284,8 +320,8 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) printf("-----------------\n"); */ - result=asn1_create_der(crl,"crl1",der,der_len); - if(result!=ASN_OK){ + result=asn1_der_coding(crl,"crl1",der,der_len,errorDescription); + if(result!=ASN1_SUCCESS){ printf("\n'crl1' encoding creation: ERROR\n"); return; } @@ -296,7 +332,7 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len) printf("\n-----------------\n"); /* Clear the "certificate1" structure */ - asn1_delete_structure(crl); + asn1_delete_structure(&crl); } @@ -315,14 +351,15 @@ get_CRL(node_asn *cert_def,unsigned char *der,int der_len) { int result,len,start,end; unsigned char str[1024],str2[1024]; - node_asn *crl2; + ASN1_TYPE crl2=ASN1_TYPE_EMPTY; + char errorDescription[MAX_ERROR_DESCRIPTION_SIZE]; - asn1_create_structure(cert_def,"PKIX1Implicit88.CertificateList",&crl2,"crl2"); + asn1_create_element(cert_def,"PKIX1Implicit88.CertificateList",&crl2,"crl2"); - result=asn1_get_der(crl2,der,der_len); - - if(result!=ASN_OK){ + result=asn1_der_decoding(&crl2,der,der_len,errorDescription); + + if(result!=ASN1_SUCCESS){ printf("Problems with DER encoding\n"); return; } @@ -340,7 +377,7 @@ get_CRL(node_asn *cert_def,unsigned char *der,int der_len) 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(crl2,der,der_len, + result=asn1_der_decoding_startEnd(crl2,der,der_len, "crl2.tbsCertList",&start,&end); /* add the lines to calculate the sha on der[start]..der[end] */ @@ -357,9 +394,10 @@ get_CRL(node_asn *cert_def,unsigned char *der,int der_len) /* Clear the "crl2" structure */ - asn1_delete_structure(crl2); + asn1_delete_structure(&crl2); } +#include "pkix_asn1_tab.c" /********************************************************/ /* Function : main */ @@ -373,29 +411,19 @@ 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(argc==2) strcpy(file_name,argv[1]); - else file_name[0]=0; - - strcat(file_name,"pkix.asn"); - result=asn1_parser_asn1(file_name,&PKIX1Implicit88); - - if(result==ASN_FILE_NOT_FOUND){ - printf("FILE NOT FOUND\n"); - return 1; - } - else if(result==ASN_SYNTAX_ERROR){ - printf("PARSE ERROR\n"); - return 1; + ASN1_TYPE PKIX1Implicit88=ASN1_TYPE_EMPTY; + char errorDescription[MAX_ERROR_DESCRIPTION_SIZE]; + + if(1) + result=asn1_array2tree(pkix_asn1_tab,&PKIX1Implicit88,errorDescription); + else + result=asn1_parser2tree("pkix.asn",&PKIX1Implicit88,errorDescription); + + if(result != ASN1_SUCCESS){ + libasn1_perror(result); + printf("%s\n",errorDescription); + exit(1); } - else if(result==ASN_IDENTIFIER_NOT_FOUND){ - printf("IDENTIFIER NOT FOUND\n"); - return 1; - } - /* Use the following 3 lines to visit the PKIX1Implicit structures */ /* printf("-----------------\n"); @@ -405,10 +433,11 @@ main(int argc,char *argv[]) create_CRL(PKIX1Implicit88,der,&der_len); + get_CRL(PKIX1Implicit88,der,der_len); /* Clear the "PKIX1Implicit88" structures */ - asn1_delete_structure(PKIX1Implicit88); + asn1_delete_structure(&PKIX1Implicit88); return 0; } |