00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <libtasn1.h>
00031 #include <stdlib.h>
00032 #include <config.h>
00033
00034 #ifdef HAVE_UNISTD_H
00035 #include <unistd.h>
00036 #endif
00037
00038 #ifdef HAVE_GETOPT_H
00039 #include <getopt.h>
00040 #endif
00041
00042 char version_man[] = "asn1Decoding (GNU libtasn1) " VERSION;
00043
00044 char help_man[] = "asn1Decoding generates an ASN1 type from a file\n"
00045 "with ASN1 definitions and another one with a DER encoding.\n"
00046 "\n"
00047 "Usage: asn1Decoding [options] <file1> <file2> <type>\n"
00048 " <file1> file with ASN1 definitions.\n"
00049 " <file2> file with a DER coding.\n"
00050 " <type> ASN1 type name\n"
00051 "\n"
00052 #ifdef HAVE_GETOPT_LONG
00053 "Operation modes:\n"
00054 " -h, --help shows this message and exit.\n"
00055 " -v, --version shows version information and exit.\n"
00056 " -c, --check checks the syntax only.\n";
00057 #else
00058 "Operation modes:\n"
00059 " -h shows this message and exit.\n"
00060 " -v shows version information and exit.\n"
00061 " -c checks the syntax only.\n";
00062 #endif
00063
00064
00065
00066
00067
00068
00069
00070 int
00071 main(int argc,char *argv[])
00072 {
00073
00074 #ifdef HAVE_GETOPT_LONG
00075 static struct option long_options[] =
00076 {
00077 {"help", no_argument, 0, 'h'},
00078 {"version", no_argument, 0, 'v'},
00079 {"check", no_argument, 0, 'c'},
00080 {0, 0, 0, 0}
00081 };
00082 int option_index = 0;
00083 #endif
00084
00085 int option_result;
00086 char *inputFileAsnName=NULL;
00087 char *inputFileDerName=NULL;
00088 char *typeName=NULL;
00089 int checkSyntaxOnly=0;
00090 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
00091 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
00092 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
00093 int asn1_result=ASN1_SUCCESS;
00094 FILE *inputFile;
00095 unsigned char der[100*1024];
00096 int der_len=0;
00097
00098
00099 opterr=0;
00100
00101 printf("\n");
00102
00103 while(1){
00104
00105 #ifdef HAVE_GETOPT_LONG
00106 option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
00107 #else
00108 option_result=getopt(argc,argv,"hvc");
00109 #endif
00110
00111 if(option_result == -1) break;
00112
00113 switch(option_result){
00114 case 'h':
00115 printf("%s\n",help_man);
00116 exit(0);
00117 break;
00118 case 'v':
00119 printf("%s\n",version_man);
00120 exit(0);
00121 break;
00122 case 'c':
00123 checkSyntaxOnly = 1;
00124 break;
00125 case '?':
00126 fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
00127 printf("%s\n",help_man);
00128
00129 exit(1);
00130 break;
00131 default:
00132 fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result);
00133 }
00134 }
00135
00136 if(optind == argc){
00137 fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n");
00138 fprintf(stderr," input file with DER coding missing.\n");
00139 fprintf(stderr," ASN1 type name missing.\n\n");
00140 printf("%s\n",help_man);
00141
00142 exit(1);
00143 }
00144
00145 if(optind == argc-1){
00146 fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n");
00147 fprintf(stderr," ASN1 type name missing.\n\n");
00148 printf("%s\n",help_man);
00149
00150 exit(1);
00151 }
00152
00153 if(optind == argc-2){
00154 fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n");
00155 printf("%s\n",help_man);
00156
00157 exit(1);
00158 }
00159
00160 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
00161 strcpy(inputFileAsnName,argv[optind]);
00162
00163 inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1);
00164 strcpy(inputFileDerName,argv[optind+1]);
00165
00166 typeName=(char *)malloc(strlen(argv[optind+2])+1);
00167 strcpy(typeName,argv[optind+2]);
00168
00169 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
00170
00171 switch(asn1_result){
00172 case ASN1_SUCCESS:
00173 printf("Parse: done.\n");
00174 break;
00175 case ASN1_FILE_NOT_FOUND:
00176 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName);
00177 break;
00178 case ASN1_SYNTAX_ERROR:
00179 case ASN1_IDENTIFIER_NOT_FOUND:
00180 case ASN1_NAME_TOO_LONG:
00181 printf("asn1Decoding: %s\n",errorDescription);
00182 break;
00183 default:
00184 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
00185 }
00186
00187 if(asn1_result != ASN1_SUCCESS){
00188 free(inputFileAsnName);
00189 free(inputFileDerName);
00190 free(typeName);
00191 exit(1);
00192 }
00193
00194
00195 inputFile=fopen(inputFileDerName,"r");
00196
00197 if(inputFile==NULL){
00198 printf("asn1Decoding: file '%s' not found\n",inputFileDerName);
00199 asn1_delete_structure(&definitions);
00200
00201 free(inputFileAsnName);
00202 free(inputFileDerName);
00203 free(typeName);
00204 exit(1);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 while(fscanf(inputFile,"%c",der+der_len) != EOF){
00224 der_len++;
00225 }
00226 fclose(inputFile);
00227
00228
00229 asn1_result=asn1_create_element(definitions,typeName,&structure);
00230
00231
00232
00233
00234 if(asn1_result != ASN1_SUCCESS){
00235 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
00236 asn1_delete_structure(&definitions);
00237
00238 free(inputFileAsnName);
00239 free(inputFileDerName);
00240 free(typeName);
00241 exit(1);
00242 }
00243
00244 asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription);
00245 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result));
00246 if(asn1_result != ASN1_SUCCESS)
00247 printf("asn1Decoding: %s\n",errorDescription);
00248
00249 printf("\nDECODING RESULT:\n");
00250 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 asn1_delete_structure(&definitions);
00269 asn1_delete_structure(&structure);
00270
00271 free(inputFileAsnName);
00272 free(inputFileDerName);
00273 free(typeName);
00274
00275 if(asn1_result != ASN1_SUCCESS)
00276 exit(1);
00277
00278 exit(0);
00279 }
00280
00281
00282
00283
00284
00285
00286