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

asn1Decoding.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: asn1Deoding.c                               */
00024 /* Description: program to generate an ASN1 type from*/
00025 /*              a DER coding.                        */   
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 /* Function : main                                      */
00068 /* Description:                                         */
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  /* FILE *outputFile; */ 
00098 
00099  opterr=0; /* disable error messages from getopt */
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':  /* HELP */
00115      printf("%s\n",help_man);
00116      exit(0);
00117      break;
00118    case 'v':  /* VERSION */
00119      printf("%s\n",version_man);
00120      exit(0);
00121      break;
00122    case 'c':  /* CHECK SYNTAX */
00123      checkSyntaxOnly = 1;
00124      break;
00125    case '?':  /* UNKNOW OPTION */
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  /* ONLY FOR TEST                         */
00210  /*****************************************/
00211  /*
00212  der_len=0;
00213  outputFile=fopen("data.p12","w");
00214  while(fscanf(inputFile,"%c",der+der_len) != EOF){
00215    if((der_len>=0x11) && (der_len<=(0xe70)))
00216      fprintf(outputFile,"%c",der[der_len]);
00217    der_len++;
00218  }
00219  fclose(outputFile);
00220  fclose(inputFile);
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  /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
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  /* ONLY FOR TEST                         */
00254  /*****************************************/
00255  /*
00256  der_len=10000;
00257  option_index=0;
00258  asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
00259  outputFile=fopen("encryptedData.p12","w");
00260  while(der_len>0){
00261    fprintf(outputFile,"%c",der[option_index]);
00262    der_len--;
00263    option_index++;
00264  }
00265  fclose(outputFile);
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 

Generated by  Doxygen 1.5.1