Sourcecode and documentation for libtasn1-2 version 0.2.10-3sarge1
show bar | Show file versions
Search packages:
| Sourcecode archive home

asn1Coding.c

00001 /*
00002  *      Copyright (C) 2002 Fabio Fiorina
00003  *
00004  * This file is part of LIBASN1.
00005  *
00006  * LIBASN1 is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * LIBASN1 is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00019  */
00020 
00021 
00022 /*****************************************************/
00023 /* File: asn1Coding.c                                */
00024 /* Description: program to generate a DER coding     */
00025 /*              of an ASN1 definition.               */   
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[] = "asn1Coding (GNU libtasn1) " VERSION;
00043 
00044 char help_man[] = "asn1Coding generates a DER encoding from a file\n"
00045                   "with ASN1 definitions and another one with assignments.\n"
00046                   "\n"
00047                   "Usage: asn1Coding [options] <file1> <file2>\n"
00048                   " <file1> file with ASN1 definitions.\n"
00049                   " <file2> file with assignments.\n"
00050                   "\n"
00051 #ifdef HAVE_GETOPT_LONG
00052                   "Operation modes:\n"
00053                   "  -h, --help    shows this message and exit.\n"
00054                   "  -v, --version shows version information and exit.\n"
00055                   "  -c, --check   checks the syntax only.\n"
00056                   "\n"
00057                   "Output:\n"
00058                   "  -o <file>, --output <file>  output file.\n";
00059 #else
00060                   "Operation modes:\n"
00061                   "  -h    shows this message and exit.\n"
00062                   "  -v    shows version information and exit.\n"
00063                   "  -c    checks the syntax only.\n"
00064                   "\n"
00065                   "Output:\n"
00066                   "  -o <file>  output file.\n";
00067 #endif
00068 
00069 
00070 #define ASSIGNMENT_SUCCESS 1
00071 #define ASSIGNMENT_ERROR   2
00072 #define ASSIGNMENT_EOF     3
00073 
00074 int readAssignment(FILE *file,char *varName, char *value){
00075 
00076   int ret;
00077 
00078   ret=fscanf(file,"%s",varName);
00079   if(ret==EOF) return ASSIGNMENT_EOF;
00080   if(!strcmp(varName,"''")) varName[0]=0;
00081 
00082   ret=fscanf(file,"%s",value);
00083   if(ret==EOF) return ASSIGNMENT_ERROR;
00084 
00085   return ASSIGNMENT_SUCCESS;
00086 }
00087 
00088 
00089 
00090 void createFileName(char *inputFileName, char **outputFileName)
00091 {
00092  char *char_p,*slash_p,*dot_p;
00093 
00094  /* searching the last '/' and '.' in inputFileAssignmentName */
00095  char_p=inputFileName;
00096  slash_p=inputFileName;
00097  while((char_p=strchr(char_p,'/'))){
00098    char_p++;
00099      slash_p=char_p;
00100  }
00101  
00102  char_p=slash_p;
00103  dot_p=inputFileName+strlen(inputFileName);
00104  
00105  while((char_p=strchr(char_p,'.'))){
00106    dot_p=char_p;
00107    char_p++;
00108  }
00109  
00110  /* outputFileName= inputFileName + .out */
00111  *outputFileName=(char *)malloc(dot_p-inputFileName+1+
00112                          strlen(".out"));
00113  memcpy(*outputFileName,inputFileName,
00114           dot_p-inputFileName);
00115  (*outputFileName)[dot_p-inputFileName]=0;
00116  strcat(*outputFileName,".out");
00117  return;
00118 }
00119 
00120 
00121 /********************************************************/
00122 /* Function : main                                      */
00123 /* Description:                                         */
00124 /********************************************************/
00125 int
00126 main(int argc,char *argv[])
00127 {
00128 
00129 #ifdef HAVE_GETOPT_LONG
00130   static struct option long_options[] =
00131   {
00132     {"help",    no_argument,       0, 'h'},
00133     {"version", no_argument,       0, 'v'},
00134     {"check",   no_argument,       0, 'c'},
00135     {"output",  required_argument, 0, 'o'},
00136     {0, 0, 0, 0}
00137   };
00138   int option_index=0;
00139 #endif
00140 
00141  int option_result;
00142  char *outputFileName=NULL;
00143  char *inputFileAsnName=NULL;
00144  char *inputFileAssignmentName=NULL;
00145  int checkSyntaxOnly=0;
00146  ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
00147  ASN1_TYPE structure=ASN1_TYPE_EMPTY;
00148  char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
00149  int asn1_result=ASN1_SUCCESS;
00150  FILE *outputFile;
00151  FILE *inputFile;
00152  char varName[1024];
00153  char value[1024];
00154  unsigned char der[1024];
00155  int  der_len;
00156  int  k;
00157 
00158  opterr=0; /* disable error messages from getopt */
00159 
00160  printf("\n");
00161 
00162  while(1){
00163 
00164 #ifdef HAVE_GETOPT_LONG
00165    option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index);
00166 #else
00167    option_result=getopt(argc,argv,"hvco:");
00168 #endif
00169 
00170    if(option_result == -1) break;
00171 
00172    switch(option_result){
00173    case 'h':  /* HELP */
00174      printf("%s\n",help_man);
00175 
00176      if(outputFileName) free(outputFileName);
00177      exit(0);
00178      break;
00179    case 'v':  /* VERSION */
00180      printf("%s\n",version_man);
00181 
00182      if(outputFileName) free(outputFileName);
00183      exit(0);
00184      break;
00185    case 'c':  /* CHECK SYNTAX */
00186      checkSyntaxOnly = 1;
00187      break;
00188    case 'o':  /* OUTPUT */
00189      outputFileName=(char *)malloc(strlen(optarg)+1);
00190      strcpy(outputFileName,optarg);
00191      break;
00192    case '?':  /* UNKNOW OPTION */
00193      fprintf(stderr,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
00194      printf("%s\n",help_man);
00195 
00196      if(outputFileName) free(outputFileName);
00197      exit(1);
00198      break;
00199    default:
00200      fprintf(stderr,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result);
00201    }
00202  }
00203 
00204  if(optind == argc){
00205    fprintf(stderr,"asn1Coding: input file with ASN1 definitions missing.\n");
00206    fprintf(stderr,"            input file with assignments missing.\n\n");
00207    printf("%s\n",help_man);
00208 
00209    if(outputFileName) free(outputFileName);
00210    exit(1);
00211  }
00212 
00213  if(optind == argc-1){
00214    fprintf(stderr,"asn1Coding: input file with assignments missing.\n\n");
00215    printf("%s\n",help_man);
00216 
00217    if(outputFileName) free(outputFileName);
00218    exit(1);
00219  }
00220 
00221  inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
00222  strcpy(inputFileAsnName,argv[optind]);
00223 
00224  inputFileAssignmentName=(char *)malloc(strlen(argv[optind+1])+1);
00225  strcpy(inputFileAssignmentName,argv[optind+1]);
00226 
00227  asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
00228 
00229  switch(asn1_result){
00230  case ASN1_SUCCESS:
00231    printf("Parse: done.\n");
00232    break;
00233  case ASN1_FILE_NOT_FOUND:
00234    printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName);
00235    break;
00236  case ASN1_SYNTAX_ERROR: 
00237  case ASN1_IDENTIFIER_NOT_FOUND: 
00238  case ASN1_NAME_TOO_LONG:
00239    printf("asn1Coding: %s\n",errorDescription);
00240    break;
00241  default:
00242    printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
00243  }
00244 
00245  if(asn1_result != ASN1_SUCCESS){
00246    free(inputFileAsnName);
00247    free(inputFileAssignmentName);
00248    exit(1);
00249  }
00250 
00251 
00252  inputFile=fopen(inputFileAssignmentName,"r");
00253  
00254  if(inputFile==NULL){
00255    printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName);
00256    free(inputFileAsnName);
00257    free(inputFileAssignmentName);
00258    exit(1);
00259  }
00260 
00261 
00262  printf("\n");
00263 
00264  while(readAssignment(inputFile,varName,value) == ASSIGNMENT_SUCCESS){
00265    printf("var=%s, value=%s\n",varName,value);
00266    if(structure==ASN1_TYPE_EMPTY){
00267      asn1_result=asn1_create_element(definitions,value,&structure);
00268    }
00269    else
00270      asn1_result=asn1_write_value(structure,varName,value,0); 
00271 
00272    if(asn1_result != ASN1_SUCCESS){
00273      printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
00274      
00275      asn1_delete_structure(&definitions);
00276      asn1_delete_structure(&structure);
00277      
00278      free(inputFileAsnName);
00279      free(inputFileAssignmentName);
00280      
00281      fclose(inputFile);
00282      exit(1);
00283    }
00284  }
00285  fclose(inputFile);
00286 
00287  printf("\n");
00288  asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
00289 
00290  der_len=1024;
00291  asn1_result=asn1_der_coding(structure,"",der,&der_len,
00292                              errorDescription);
00293  printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result));
00294  if(asn1_result!=ASN1_SUCCESS){
00295    printf("asn1Coding: %s\n",errorDescription);
00296 
00297    asn1_delete_structure(&definitions);
00298    asn1_delete_structure(&structure);
00299    
00300    free(inputFileAsnName);
00301    free(inputFileAssignmentName);
00302   
00303    exit(1);
00304  }
00305 
00306  /* Print the 'Certificate1' DER encoding */ 
00307  printf("-----------------\nNumber of bytes=%i\n",der_len);
00308  for(k=0;k<der_len;k++) printf("%02x ",der[k]);  
00309  printf("\n-----------------\n");
00310 
00311  asn1_delete_structure(&definitions);
00312  asn1_delete_structure(&structure);
00313  
00314 
00315  if(outputFileName==NULL)
00316    createFileName(inputFileAssignmentName,&outputFileName);
00317 
00318  printf("\nOutputFile=%s\n",outputFileName);
00319 
00320  outputFile=fopen(outputFileName,"w");
00321  
00322  if(outputFile==NULL){
00323    printf("asn1Coding: output file '%s' not available\n",outputFileName);
00324    free(inputFileAsnName);
00325    free(inputFileAssignmentName);
00326    free(outputFileName);
00327    exit(1);
00328  }
00329 
00330  for(k=0;k<der_len;k++) 
00331    fprintf(outputFile,"%c",der[k]);  
00332  fclose(outputFile);
00333  printf("\nWriting: done.\n");
00334 
00335 
00336  free(inputFileAsnName);
00337  free(inputFileAssignmentName);
00338  free(outputFileName);
00339 
00340  exit(0);
00341 }
00342 
00343 
00344 
00345 
00346 

Generated by  Doxygen 1.5.1