summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS4
-rw-r--r--configure.in12
-rw-r--r--lib/Makefile.am4
-rw-r--r--lib/decoding.c67
-rw-r--r--lib/defines.h12
-rw-r--r--lib/element.c43
-rw-r--r--lib/int.h4
-rw-r--r--lib/parser_aux.c53
-rw-r--r--lib/structure.c16
-rw-r--r--src/CertificateExample.c220
-rw-r--r--src/CrlExample.c134
-rw-r--r--src/asn1Coding.c20
-rw-r--r--src/asn1Decoding.c35
-rw-r--r--src/asn1Parser.c14
-rw-r--r--tests/Test_parser.c6
-rw-r--r--tests/Test_tree.c157
16 files changed, 468 insertions, 333 deletions
diff --git a/NEWS b/NEWS
index 778b63a..f4a6e49 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+Version 0.2.0
+- Support for other platforms
+- Change asn1_create_element function interface (dest_name not needed any more)
+
Version 0.1.2
- Added GeneralString type
- Fixed a DER encoding bug when nested tags are used
diff --git a/configure.in b/configure.in
index 6ba2bc2..a2c2303 100644
--- a/configure.in
+++ b/configure.in
@@ -11,8 +11,8 @@ AC_DEFINE_UNQUOTED(T_OS, "$target_os")
dnl libtasn1 Version
ASN1_MAJOR_VERSION=0
-ASN1_MINOR_VERSION=1
-ASN1_MICRO_VERSION=2
+ASN1_MINOR_VERSION=2
+ASN1_MICRO_VERSION=0
ASN1_VERSION=$ASN1_MAJOR_VERSION.$ASN1_MINOR_VERSION.$ASN1_MICRO_VERSION
AC_DEFINE_UNQUOTED(ASN1_VERSION, "$ASN1_VERSION")
@@ -21,9 +21,9 @@ AM_INIT_AUTOMAKE(libtasn1, $ASN1_VERSION)
AM_CONFIG_HEADER(config.h)
dnl This is the library version
-ASN1_MOST_RECENT_INTERFACE=0
+ASN1_MOST_RECENT_INTERFACE=1
ASN1_CURRENT_INTERFACE_IMPLEMENTATION_NUMBER=$ASN1_MICRO_VERSION
-ASN1_OLDEST_INTERFACE=0
+ASN1_OLDEST_INTERFACE=1
AC_SUBST(ASN1_MAJOR_VERSION)
@@ -51,6 +51,7 @@ AC_PROG_LN_S
dnl Checks for programs.
AC_PROG_INSTALL
+AM_MISSING_PROG(PERL,perl,$missing_dir)
AC_MSG_RESULT([***
@@ -100,8 +101,9 @@ AC_MSG_RESULT([***
])
AC_HEADER_STDC
-AC_CHECK_HEADERS(getopt.h unistd.h strings.h)
+AC_CHECK_HEADERS(getopt.h unistd.h strings.h inttypes.h stdint.h)
AC_CHECK_FUNCS(bzero memset memmove bcopy,,)
+AC_CHECK_FUNCS(getopt_long)
AC_FUNC_ALLOCA
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 6f33662..15d24b5 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2,7 +2,7 @@ include_HEADERS = libtasn1.h
EXTRA_DIST = libtasn1.h der.h asn1-api.tex mem.h gstr.h errors.h \
defines.h int.h parser_aux.h structure.h errors_int.h \
- element.h
+ element.h asn1-api.tex
lib_LTLIBRARIES = libtasn1.la
@@ -17,7 +17,7 @@ libtasn1_la_LDFLAGS = -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE)
asn1-api.tex: $(COBJECTS)
@echo "% \\newpage" > asn1-api.tex
@for i in $(COBJECTS); \
- do echo -n "Creating documentation for file $$i... " && ../doc/scripts/gdoc -tex $$i >> asn1-api.tex \
+ do echo -n "Creating documentation for file $$i... " && $(PERL) ../doc/scripts/gdoc -tex $$i >> asn1-api.tex \
&& echo "ok"; \
done
diff --git a/lib/decoding.c b/lib/decoding.c
index 6e079bb..ce62097 100644
--- a/lib/decoding.c
+++ b/lib/decoding.c
@@ -421,7 +421,6 @@ asn1_der_decoding(ASN1_TYPE *element,const unsigned char *der,int len,
while(p3){
ris=_asn1_extract_tag_der(p3,der+counter,&len2);
if(ris==ASN1_SUCCESS) break;
- //if(ris==ASN1_ERROR_TYPE_ANY) return ASN1_ERROR_TYPE_ANY;
p3=p3->right;
}
}
@@ -430,7 +429,6 @@ asn1_der_decoding(ASN1_TYPE *element,const unsigned char *der,int len,
p=p2;
break;
}
- //else if(ris==ASN1_ERROR_TYPE_ANY) return ASN1_ERROR_TYPE_ANY;
}
p2=p2->right;
}
@@ -474,7 +472,6 @@ asn1_der_decoding(ASN1_TYPE *element,const unsigned char *der,int len,
if(ris==ASN1_SUCCESS) ris=_asn1_extract_tag_der(p,der+counter,&len2);
if(ris!=ASN1_SUCCESS){
- //if(ris==ASN1_ERROR_TYPE_ANY) return ASN1_ERROR_TYPE_ANY;
if(p->type&CONST_OPTION){
p->type|=CONST_NOT_USED;
move=RIGHT;
@@ -484,7 +481,6 @@ asn1_der_decoding(ASN1_TYPE *element,const unsigned char *der,int len,
move=RIGHT;
}
else {
- //return (type_field(p->type)!=TYPE_ANY)?ASN1_TAG_ERROR:ASN1_ERROR_TYPE_ANY;
if (errorDescription!=NULL)
_asn1_error_description_tag_error(p,errorDescription);
@@ -653,7 +649,7 @@ asn1_der_decoding(ASN1_TYPE *element,const unsigned char *der,int len,
* asn1_der_decoding_element - Fill the element named ELEMENTNAME of the structure STRUCTURE with values of a DER encoding string.
* @structure: pointer to an ASN1 structure
* @elementName: name of the element to fill
- * @der: vector that contains the DER encoding.
+ * @der: vector that contains the DER encoding of the whole structure.
* @len: number of bytes of *der: der[0]..der[len-1]
* @errorDescription: null-terminated string contains details when an arror accured.
*
@@ -661,14 +657,15 @@ asn1_der_decoding(ASN1_TYPE *element,const unsigned char *der,int len,
*
* Fill the element named ELEMENTNAME with values of a DER encoding string.
* The sructure must just be created with function 'create_stucture'.
- * If an error accurs during de decoding procedure, the *STRUCTURE is deleted
+ * The DER vector must contain the encoding string of the whole STRUCTURE.
+ * If an error accurs during the decoding procedure, the *STRUCTURE is deleted
* and set equal to ASN1_TYPE_EMPTY.
*
* Returns:
*
* ASN1_SUCCESS\: DER encoding OK
*
- * ASN1_ELEMENT_NOT_FOUND\: ELEMENT is ASN1_TYPE_EMPTY.
+ * ASN1_ELEMENT_NOT_FOUND\: ELEMENT is ASN1_TYPE_EMPTY or elementName == NULL.
*
* ASN1_TAG_ERROR,ASN1_DER_ERROR\: the der encoding doesn't match the structure STRUCTURE. *ELEMENT deleted.
**/
@@ -678,7 +675,7 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
{
node_asn *node,*p,*p2,*p3,*nodeFound=ASN1_TYPE_EMPTY;
char temp[128],currentName[MAX_NAME_SIZE*10],*dot_p,*char_p;
- int nameLen=100,state;
+ int nameLen=MAX_NAME_SIZE*10-1,state;
int counter,len2,len3,len4,move,ris;
unsigned char class,*temp2;
unsigned int tag;
@@ -687,25 +684,42 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
if(node==ASN1_TYPE_EMPTY) return ASN1_ELEMENT_NOT_FOUND;
+ if(elementName == NULL){
+ asn1_delete_structure(structure);
+ return ASN1_ELEMENT_NOT_FOUND;
+ }
+
if(node->type&CONST_OPTION){
asn1_delete_structure(structure);
return ASN1_GENERIC_ERROR;
}
- nameLen-=strlen((*structure)->name);
- if(nameLen>0) strcpy(currentName,(*structure)->name);
- else{
- asn1_delete_structure(structure);
+ if((*structure)->name){ /* Has *structure a name? */
+ nameLen-=strlen((*structure)->name);
+ if(nameLen>0) strcpy(currentName,(*structure)->name);
+ else{
+ asn1_delete_structure(structure);
return ASN1_MEM_ERROR;
+ }
+ if(!(strcmp(currentName,elementName))){
+ state=FOUND;
+ nodeFound=*structure;
+ }
+ else if(!memcmp(currentName,elementName,strlen(currentName)))
+ state=SAME_BRANCH;
+ else
+ state=OTHER_BRANCH;
}
- if(!(strcmp(currentName,elementName))){
- state=FOUND;
- nodeFound=*structure;
+ else{ /* *structure doesn't have a name? */
+ currentName[0]=0;
+ if(elementName[0]==0){
+ state=FOUND;
+ nodeFound=*structure;
+ }
+ else{
+ state=SAME_BRANCH;
+ }
}
- else if(!memcmp(currentName,elementName,strlen(currentName)))
- state=SAME_BRANCH;
- else
- state=OTHER_BRANCH;
counter=0;
move=DOWN;
@@ -737,7 +751,6 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
while(p3){
ris=_asn1_extract_tag_der(p3,der+counter,&len2);
if(ris==ASN1_SUCCESS) break;
- //if(ris==ASN1_ERROR_TYPE_ANY) return ASN1_ERROR_TYPE_ANY;
p3=p3->right;
}
}
@@ -746,7 +759,6 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
p=p2;
break;
}
- //else if(ris==ASN1_ERROR_TYPE_ANY) return ASN1_ERROR_TYPE_ANY;
}
p2=p2->right;
}
@@ -790,7 +802,6 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
if(ris==ASN1_SUCCESS) ris=_asn1_extract_tag_der(p,der+counter,&len2);
if(ris!=ASN1_SUCCESS){
- //if(ris==ASN1_ERROR_TYPE_ANY) return ASN1_ERROR_TYPE_ANY;
if(p->type&CONST_OPTION){
p->type|=CONST_NOT_USED;
move=RIGHT;
@@ -800,12 +811,10 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
move=RIGHT;
}
else {
- //return (type_field(p->type)!=TYPE_ANY)?ASN1_TAG_ERROR:ASN1_ERROR_TYPE_ANY;
if (errorDescription!=NULL)
_asn1_error_description_tag_error(p,errorDescription);
asn1_delete_structure(structure);
-
return ASN1_TAG_ERROR;
}
}
@@ -1018,7 +1027,7 @@ asn1_der_decoding_element(ASN1_TYPE *structure,const char *elementName,
if(state != FOUND){
nameLen-=strlen(p->name)+1;
if(nameLen>0){
- strcat(currentName,".");
+ if(currentName[0]) strcat(currentName,".");
strcat(currentName,p->name);
}
else{
@@ -1428,9 +1437,9 @@ asn1_expand_any_defined_by(ASN1_TYPE definitions,ASN1_TYPE *element)
strcpy(name,definitionsName);
strcat(name,p2->name);
- result=asn1_create_element(definitions,name,&aux,p->name);
+ result=asn1_create_element(definitions,name,&aux);
if(result == ASN1_SUCCESS){
-
+ _asn1_set_name(aux,p->name);
len2=_asn1_get_length_der(p->value,&len3);
result=asn1_der_decoding(&aux,p->value+len3,len2,
@@ -1595,9 +1604,9 @@ asn1_expand_octet_string(ASN1_TYPE definitions,ASN1_TYPE *element,
strcat(name,".");
strcat(name,p2->name);
- result=asn1_create_element(definitions,name,&aux,octetNode->name);
+ result=asn1_create_element(definitions,name,&aux);
if(result == ASN1_SUCCESS){
-
+ _asn1_set_name(aux,octetNode->name);
len2=_asn1_get_length_der(octetNode->value,&len3);
result=asn1_der_decoding(&aux,octetNode->value+len3,len2,
diff --git a/lib/defines.h b/lib/defines.h
index 1511954..fc779a7 100644
--- a/lib/defines.h
+++ b/lib/defines.h
@@ -21,6 +21,8 @@
#ifndef DEFINES_H
# define DEFINES_H
+#include <config.h>
+
#ifndef __GNUC__
# if HAVE_ALLOCA_H
# include <alloca.h>
@@ -36,8 +38,6 @@ char *alloca();
#endif
-#include <config.h>
-
#ifdef STDC_HEADERS
# include <string.h>
# include <stdlib.h>
@@ -54,6 +54,12 @@ char *alloca();
#endif
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#else
+# if HAVE_STDINT_H
+# include <stdint.h>
+# else
#if SIZEOF_UNSIGNED_LONG_INT == 4
typedef unsigned long int uint32;
typedef signed long int sint32;
@@ -88,6 +94,8 @@ typedef signed char int8;
# error "Neither memmove nor bcopy exists on your system."
# endif
#endif
+# endif
+#endif
#endif /* defines_h */
diff --git a/lib/element.c b/lib/element.c
index fcedf18..37a3272 100644
--- a/lib/element.c
+++ b/lib/element.c
@@ -75,19 +75,15 @@ asn1_retCode
_asn1_convert_integer(const char *value,unsigned char *value_out,int value_out_size, int *len)
{
char negative;
- unsigned char val[SIZEOF_UNSIGNED_LONG_INT],temp;
+ unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
+ long valtmp;
int k,k2;
- *((long*)val)=strtol(value,NULL,10);
-
-#ifndef WORDS_BIGENDIAN
- /* change to big-endian byte ordering */
- for(k=0;k<SIZEOF_UNSIGNED_LONG_INT/2;k++){
- temp=val[k];
- val[k]=val[SIZEOF_UNSIGNED_LONG_INT-k-1];
- val[SIZEOF_UNSIGNED_LONG_INT-k-1]=temp;
+ valtmp=strtol(value,NULL,10);
+
+ for(k=0;k<SIZEOF_UNSIGNED_LONG_INT;k++){
+ val[SIZEOF_UNSIGNED_LONG_INT-k-1]=(valtmp >> (8*k)) & 0xFF;
}
-#endif
if(val[0]&0x80) negative=1;
else negative=0;
@@ -145,6 +141,7 @@ _asn1_append_sequence_set(node_asn *node)
_asn1_ltostr(n,temp+1);
}
_asn1_set_name(p2,temp);
+ p2->type |= CONST_OPTION;
return ASN1_SUCCESS;
}
@@ -211,7 +208,7 @@ _asn1_append_sequence_set(node_asn *node)
* value="$\backslash$x01$\backslash$x02$\backslash$x03" , len=3 -> three bytes octet string
*
* \item GeneralString\: VALUE contains the generalstring and LEN is the number of octet.
- * value="$\backslash$x01$\backslash$x02$\backslash$x03" , len=3 -> three bytes octet string
+ * value="$\backslash$x01$\backslash$x02$\backslash$x03" , len=3 -> three bytes generalstring
*
* \item BIT STRING\: VALUE contains the bit string organized by bytes and LEN is the number of bits.
* value="$\backslash$xCF" , len=6 -> bit string="110011" (six bits)
@@ -261,6 +258,16 @@ asn1_write_value(node_asn *node_root,const char *name,
return ASN1_SUCCESS;
}
+ if((type_field(node->type) == TYPE_SEQUENCE_OF) && (value == NULL) && (len==0)){
+ p=node->down;
+ while((type_field(p->type)==TYPE_TAG) || (type_field(p->type)==TYPE_SIZE)) p=p->right;
+
+ while(p->right)
+ asn1_delete_structure(&p->right);
+
+ return ASN1_SUCCESS;
+ }
+
switch(type_field(node->type)){
case TYPE_BOOLEAN:
if(!strcmp(value,"TRUE")){
@@ -419,6 +426,8 @@ asn1_write_value(node_asn *node_root,const char *name,
}
break;
case TYPE_OCTET_STRING:
+ if(len==0)
+ len=strlen(value);
_asn1_length_der(len,NULL,&len2);
temp=(unsigned char *)_asn1_alloca(len+len2);
if (temp==NULL) return ASN1_MEM_ERROR;
@@ -428,6 +437,8 @@ asn1_write_value(node_asn *node_root,const char *name,
_asn1_afree(temp);
break;
case TYPE_GENERALSTRING:
+ if(len==0)
+ len=strlen(value);
_asn1_length_der(len,NULL,&len2);
temp=(unsigned char *)_asn1_alloca(len+len2);
if (temp==NULL) return ASN1_MEM_ERROR;
@@ -437,6 +448,8 @@ asn1_write_value(node_asn *node_root,const char *name,
_asn1_afree(temp);
break;
case TYPE_BIT_STRING:
+ if(len==0)
+ len=strlen(value);
_asn1_length_der((len>>3)+2,NULL,&len2);
temp=(unsigned char *)_asn1_alloca((len>>3)+2+len2);
if (temp==NULL) return ASN1_MEM_ERROR;
@@ -620,16 +633,13 @@ asn1_read_value(node_asn *root,const char *name,unsigned char *value, int *len)
case TYPE_OBJECT_ID:
if(node->type&CONST_ASSIGN){
value[0]=0;
- // _asn1_str_cpy(value, *len, "");
p=node->down;
while(p){
if(type_field(p->type)==TYPE_CONSTANT){
- // ADD_STR_VALUE( value, value_size, p->value);
value_size-=strlen(p->value)+1;
if(value_size<1) return ASN1_MEM_ERROR;
strcat(value,p->value);
if(p->right) {
- // ADD_STR_VALUE( value, value_size, " ");
strcat(value," ");
}
}
@@ -668,3 +678,8 @@ asn1_read_value(node_asn *root,const char *name,unsigned char *value, int *len)
}
return ASN1_SUCCESS;
}
+
+
+
+
+
diff --git a/lib/int.h b/lib/int.h
index 6684de6..bdf1734 100644
--- a/lib/int.h
+++ b/lib/int.h
@@ -136,7 +136,7 @@ typedef struct static_struct_asn ASN1_ARRAY_TYPE;
#define CONST_GENERALIZED (1<<23)
#define CONST_UTC (1<<24)
-// #define CONST_IMPORTS (1<<25)
+/* #define CONST_IMPORTS (1<<25) */
#define CONST_NOT_USED (1<<26)
#define CONST_SET (1<<27)
@@ -150,7 +150,7 @@ typedef struct static_struct_asn ASN1_ARRAY_TYPE;
asn1_retCode asn1_delete_structure(ASN1_TYPE *structure);
asn1_retCode asn1_create_element(ASN1_TYPE definitions,const char *source_name,
- ASN1_TYPE *element,const char *dest_name);
+ ASN1_TYPE *element);
asn1_retCode asn1_read_value(ASN1_TYPE element,const char *name,
unsigned char *value,int *len);
diff --git a/lib/parser_aux.c b/lib/parser_aux.c
index 96f6b24..a0f7775 100644
--- a/lib/parser_aux.c
+++ b/lib/parser_aux.c
@@ -84,8 +84,10 @@ _asn1_add_node(unsigned int type)
/* Function : _asn1_find_mode */
/* Description: searches an element called NAME starting from */
/* POINTER. The name is composed by differents */
-/* identifiers separated by dot.The first identifier */
-/* must be the name of *POINTER. */
+/* identifiers separated by dots.When *POINTER has a */
+/* name, the first identifier must be the name of */
+/* *POINTER, otherwise it must be the name of one */
+/* child of *POINTER. */
/* Parameters: */
/* pointer: NODE_ASN element pointer. */
/* name: null terminated string with the element's name to find.*/
@@ -95,31 +97,40 @@ node_asn *
_asn1_find_node(node_asn *pointer,const char *name)
{
node_asn *p;
- char *n_end,n[128];
+ char *n_end,n[MAX_NAME_SIZE+1];
const char *n_start;
- if((name==NULL) || (name[0]==0)) return NULL;
+ if(pointer == NULL) return NULL;
- n_start=name;
- n_end=strchr(n_start,'.'); /* search the first dot */
- if(n_end){
- memcpy(n,n_start,n_end-n_start);
- n[n_end-n_start]=0;
- n_start=n_end;
- n_start++;
- }
- else{
- _asn1_str_cpy(n,sizeof(n),n_start);
- n_start=NULL;
- }
+ if(name==NULL) return NULL;
p=pointer;
- while(p){
- if((p->name) && (!strcmp(p->name,n))) break;
- else p=p->right;
- } /* while */
+ n_start=name;
- if(p==NULL) return NULL;
+ if(p->name != NULL){ /* has *pointer a name ? */
+ n_end=strchr(n_start,'.'); /* search the first dot */
+ if(n_end){
+ memcpy(n,n_start,n_end-n_start);
+ n[n_end-n_start]=0;
+ n_start=n_end;
+ n_start++;
+ }
+ else{
+ _asn1_str_cpy(n,sizeof(n),n_start);
+ n_start=NULL;
+ }
+
+ while(p){
+ if((p->name) && (!strcmp(p->name,n))) break;
+ else p=p->right;
+ } /* while */
+
+ if(p==NULL) return NULL;
+ }
+ else{ /* *pointer doesn't have a name */
+ if(n_start[0]==0)
+ return p;
+ }
while(n_start){ /* Has the end of NAME been reached? */
n_end=strchr(n_start,'.'); /* search the next dot */
diff --git a/lib/structure.c b/lib/structure.c
index fb7b10f..e187ef0 100644
--- a/lib/structure.c
+++ b/lib/structure.c
@@ -321,7 +321,7 @@ _asn1_copy_structure3(node_asn *source_node)
if(p_s->value){
switch(type_field(p_s->type)){
case TYPE_OCTET_STRING: case TYPE_BIT_STRING: case TYPE_GENERALSTRING:
- case TYPE_INTEGER: // case TYPE_DEFAULT:
+ case TYPE_INTEGER:
len2=-1;
len=_asn1_get_length_der(p_s->value,&len2);
_asn1_set_value(p_d,p_s->value,len+len2);
@@ -518,11 +518,10 @@ _asn1_expand_identifier(node_asn **node,node_asn *root)
/**
- * asn1_create_element - Creates a structure called DEST_NAME of type SOURCE_NAME.
+ * asn1_create_element - Creates a structure of type SOURCE_NAME.
* @definitions: pointer to the structure returned by "parser_asn1" function
* @source_name: the name of the type of the new structure (must be inside p_structure).
* @element: pointer to the structure created.
- * @dest_name: the name of the new structure.
* Description:
*
* Creates a structure called DEST_NAME of type SOURCE_NAME.
@@ -531,8 +530,6 @@ _asn1_expand_identifier(node_asn **node,node_asn *root)
*
* ASN1_SUCCESS\: creation OK
*
- * ASN1_ELEMENT_NOT_EMPTY\: *POINTER not ASN1_TYPE_EMPTY
- *
* ASN1_ELEMENT_NOT_FOUND\: SOURCE_NAME isn't known
*
* Example: using "pkix.asn"
@@ -540,19 +537,20 @@ _asn1_expand_identifier(node_asn **node,node_asn *root)
**/
asn1_retCode
asn1_create_element(ASN1_TYPE definitions,const char *source_name,
- ASN1_TYPE *element,const char *dest_name)
+ ASN1_TYPE *element)
{
node_asn *dest_node;
int res;
- if(*element!=ASN1_TYPE_EMPTY)
- return ASN1_ELEMENT_NOT_EMPTY;
+ if(*element!=ASN1_TYPE_EMPTY){
+ asn1_delete_structure(element);
+ }
dest_node=_asn1_copy_structure2(definitions,source_name);
if(dest_node==NULL) return ASN1_ELEMENT_NOT_FOUND;
- _asn1_set_name(dest_node,dest_name);
+ _asn1_set_name(dest_node,"");
res=_asn1_expand_identifier(&dest_node,definitions);
_asn1_type_choice_config(dest_node);
diff --git a/src/CertificateExample.c b/src/CertificateExample.c
index 549804a..2f3bf96 100644
--- a/src/CertificateExample.c
+++ b/src/CertificateExample.c
@@ -105,15 +105,13 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
str2,&len);
if(!strcmp(str,str2)){
asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationName",
- &value,"certificate2-subject-C");
+ &value);
len = sizeof(str) - 1;
asn1_read_value(cert,name3,str,&len);
asn1_der_decoding(&value,str,len,errorDescription);
- strcpy(name3,"certificate2-subject-C");
len = sizeof(str) - 1;
- asn1_read_value(value,name3,str,&len); /* CHOICE */
- strcat(name3,".");
- strcat(name3,str);
+ asn1_read_value(value,"",str,&len); /* CHOICE */
+ strcpy(name3,str);
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
@@ -127,15 +125,13 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
,str2,&len);
if(!strcmp(str,str2)){
asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationName"
- ,&value,"certificate2-subject-O");
+ ,&value);
len = sizeof(str) - 1;
asn1_read_value(cert,name3,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 */
- strcat(name3,".");
- strcat(name3,str);
+ asn1_read_value(value,"",str,&len); /* CHOICE */
+ strcpy(name3,str);
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
@@ -147,15 +143,13 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
len = sizeof(str2) - 1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationalUnitName",str2,&len);
if(!strcmp(str,str2)){
- asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value,"certificate2-subject-OU");
+ asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value);
len = sizeof(str) - 1;
asn1_read_value(cert,name3,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 */
- strcat(name3,".");
- strcat(name3,str);
+ asn1_read_value(value,"",str,&len); /* CHOICE */
+ strcpy(name3,str);
len = sizeof(str) - 1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
@@ -193,214 +187,212 @@ create_certificate(node_asn *cert_def,unsigned char *der,int *der_len)
ASN1_TYPE constr=ASN1_TYPE_EMPTY;
char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
- result=asn1_create_element(cert_def,"PKIX1Implicit88.Certificate",&cert1,"certificate1");
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.Certificate",&cert1);
/* Use the next 3 lines to visit the empty certificate */
/* printf("-----------------\n");
- asn1_visit_tree(cert1,"certificate1");
+ asn1_visit_tree(cert1,"");
printf("-----------------\n"); */
/* version: v3(2) */
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.version","v3",0);
+ result=asn1_write_value(cert1,"tbsCertificate.version","v3",0);
/* serialNumber: 17 */
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.serialNumber","17",0);
+ result=asn1_write_value(cert1,"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",
+ result=asn1_write_value(cert1,"tbsCertificate.signature.algorithm",
str,1);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.signature.parameters",
+ result=asn1_write_value(cert1,"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,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.issuer.rdnSequence","NEW",1);
+ result=asn1_write_value(cert1,"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_write_value(cert1,"tbsCertificate.issuer.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_element(cert_def,"PKIX1Implicit88.X520countryName",
- &value,"countryName");
- result=asn1_write_value(value,"countryName","US",2);
- result=asn1_der_coding(value,"countryName",der,der_len,errorDescription);
+ &value);
+ result=asn1_write_value(value,"","US",2);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.issuer.rdnSequence","NEW",1);
+ result=asn1_write_value(cert1,"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_write_value(cert1,"tbsCertificate.issuer.rdnSequence.?LAST.?LAST.type",str,1);
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_der_coding(value,"OrgName",der,der_len,errorDescription);
+ &value);
+ result=asn1_write_value(value,"","printableString",1);
+ result=asn1_write_value(value,"printableString","gov",3);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.issuer.rdnSequence","NEW",1);
+ result=asn1_write_value(cert1,"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_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_der_coding(value,"OrgUnitName",der,der_len,errorDescription);
+ result=asn1_write_value(cert1,"tbsCertificate.issuer.rdnSequence.?LAST.?LAST.type",str,1);
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value);
+ result=asn1_write_value(value,"","printableString",1);
+ result=asn1_write_value(value,"printableString","nist",4);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(cert1,"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,"tbsCertificate.validity.notBefore","utcTime",1);
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.validity.notAfter","utcTime",1);
+ result=asn1_write_value(cert1,"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,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.subject.rdnSequence","NEW",1);
+ result=asn1_write_value(cert1,"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_write_value(cert1,"tbsCertificate.subject.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_element(cert_def,"PKIX1Implicit88.X520countryName",
- &value,"countryName");
- result=asn1_write_value(value,"countryName","US",2);
- result=asn1_der_coding(value,"countryName",der,der_len,errorDescription);
+ &value);
+ result=asn1_write_value(value,"","US",2);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.subject.rdnSequence","NEW",4);
+ result=asn1_write_value(cert1,"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_write_value(cert1,"tbsCertificate.subject.rdnSequence.?LAST.?LAST.type",str,1);
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_der_coding(value,"OrgName",der,der_len,errorDescription);
+ &value);
+ result=asn1_write_value(value,"","printableString",1);
+ result=asn1_write_value(value,"printableString","gov",3);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.subject.rdnSequence","NEW",4);
+ result=asn1_write_value(cert1,"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_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_der_coding(value,"OrgUnitName",der,der_len,errorDescription);
+ result=asn1_write_value(cert1,"tbsCertificate.subject.rdnSequence.?LAST.?LAST.type",str,1);
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value);
+ result=asn1_write_value(value,"","printableString",1);
+ result=asn1_write_value(value,"printableString","nist",4);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.subject.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(cert1,"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_element(cert_def,"PKIX1Implicit88.Dss-Parms",&param,"parameters");
+ result=asn1_write_value(cert1,"tbsCertificate.subjectPublicKeyInfo.algorithm.algorithm",str,1);
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.Dss-Parms",&param);
str2="\xd4\x38"; /* only an example */
- result=asn1_write_value(param,"parameters.p",str2,128);
+ result=asn1_write_value(param,"p",str2,128);
str2="\xd4\x38"; /* only an example */
- result=asn1_write_value(param,"parameters.q",str2,20);
+ result=asn1_write_value(param,"q",str2,20);
str2="\xd4\x38"; /* only an example */
- result=asn1_write_value(param,"parameters.g",str2,128);
- result=asn1_der_coding(param,"parameters",der,der_len,errorDescription);
+ result=asn1_write_value(param,"g",str2,128);
+ result=asn1_der_coding(param,"",der,der_len,errorDescription);
asn1_delete_structure(&param);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.subjectPublicKeyInfo.algorithm.parameters",der,*der_len);
+ result=asn1_write_value(cert1,"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,"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 */
+ result=asn1_write_value(cert1,"tbsCertificate.issuerUniqueID",NULL,0); /* NO OPTION */
+ result=asn1_write_value(cert1,"tbsCertificate.subjectUniqueID",NULL,0); /* NO OPTION */
/* extensions */
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions","NEW",1);
+ result=asn1_write_value(cert1,"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_element(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_der_coding(constr,"basicConstraints1",der,der_len,errorDescription);
+ result=asn1_write_value(cert1,"tbsCertificate.extensions.?LAST.extnID",str,1); /* basicConstraints */
+ result=asn1_write_value(cert1,"tbsCertificate.extensions.?LAST.critical","TRUE",1);
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.BasicConstraints",&constr);
+ result=asn1_write_value(constr,"cA","TRUE",1);
+ result=asn1_write_value(constr,"pathLenConstraint",NULL,0);
+ result=asn1_der_coding(constr,"",der,der_len,errorDescription);
result=asn1_delete_structure(&constr);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions.?LAST.extnValue",der,*der_len);
+ result=asn1_write_value(cert1,"tbsCertificate.extensions.?LAST.extnValue",der,*der_len);
- result=asn1_write_value(cert1,"certificate1.tbsCertificate.extensions","NEW",1);
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"tbsCertificate.extensions.?LAST.extnID",str,1); /* subjectKeyIdentifier */
+ result=asn1_write_value(cert1,"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);
+ result=asn1_write_value(cert1,"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 */
+ result=asn1_write_value(cert1,"signatureAlgorithm.algorithm",str,1);
+ result=asn1_write_value(cert1,"signatureAlgorithm.parameters",NULL,0); /* NO OPTION */
/* signature */
- result=asn1_der_coding(cert1,"certificate1.tbsCertificate",der,der_len
+ result=asn1_der_coding(cert1,"tbsCertificate",der,der_len
,errorDescription);
if(result!=ASN1_SUCCESS){
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 */
+ result=asn1_write_value(cert1,"signature",str2,368); /* dsa-with-sha */
/* Use the next 3 lines to visit the certificate */
/* printf("-----------------\n");
- asn1_visit_tree(cert1,"certificate1");
+ asn1_visit_tree(cert1,"");
printf("-----------------\n"); */
- result=asn1_der_coding(cert1,"certificate1",der,der_len,errorDescription);
+ result=asn1_der_coding(cert1,"",der,der_len,errorDescription);
if(result!=ASN1_SUCCESS){
- printf("\n'certificate1' encoding creation: ERROR\n");
+ printf("\n'certificate' encoding creation: ERROR\n");
return;
}
/* Print the 'Certificate1' DER encoding */
- printf("-----------------\nCertificate1 Encoding:\nNumber of bytes=%i\n",*der_len);
+ printf("-----------------\nCertificate Encoding:\nNumber of bytes=%i\n",*der_len);
for(k=0;k<*der_len;k++) printf("%02x ",der[k]);
printf("\n-----------------\n");
@@ -427,7 +419,7 @@ get_certificate(node_asn *cert_def,unsigned char *der,int der_len)
ASN1_TYPE cert2=ASN1_TYPE_EMPTY;
char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
- asn1_create_element(cert_def,"PKIX1Implicit88.Certificate",&cert2,"certificate2");
+ asn1_create_element(cert_def,"PKIX1Implicit88.Certificate",&cert2);
result=asn1_der_decoding(&cert2,der,der_len,errorDescription);
@@ -438,16 +430,16 @@ get_certificate(node_asn *cert_def,unsigned char *der,int der_len)
/* issuer */
- get_Name_type(cert_def,cert2,"certificate2.tbsCertificate.issuer",str);
- printf("certificate2:\nissuer =%s\n",str);
+ get_Name_type(cert_def,cert2,"tbsCertificate.issuer",str);
+ printf("certificate:\nissuer :%s\n",str);
/* subject */
- get_Name_type(cert_def,cert2,"certificate2.tbsCertificate.subject",str);
- printf("subject=%s\n",str);
+ get_Name_type(cert_def,cert2,"tbsCertificate.subject",str);
+ printf("subject:%s\n",str);
/* Verify sign */
len = sizeof(str) - 1;
- result=asn1_read_value(cert2,"certificate2.signatureAlgorithm.algorithm"
+ result=asn1_read_value(cert2,"signatureAlgorithm.algorithm"
,str,&len);
len = sizeof(str2) - 1;
@@ -455,19 +447,19 @@ get_certificate(node_asn *cert_def,unsigned char *der,int der_len)
if(!strcmp(str,str2)){ /* dsa-with-sha */
result=asn1_der_decoding_startEnd(cert2,der,der_len,
- "certificate2.tbsCertificate",&start,&end);
+ "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);
+ result=asn1_read_value(cert2,"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");
+ asn1_visit_tree(cert2,"");
printf("-----------------\n"); */
diff --git a/src/CrlExample.c b/src/CrlExample.c
index b88abb0..1aafbd7 100644
--- a/src/CrlExample.c
+++ b/src/CrlExample.c
@@ -110,17 +110,15 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
str2,&len);
if(!strcmp(str,str2)){
asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationName",
- &value,"certificate2-subject-C");
+ &value);
len = sizeof(str)-1;
asn1_read_value(cert,name3,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,"",str,&len); /* CHOICE */
- strcat(name3,".");
- strcat(name3,str);
+ strcpy(name3,str);
len = sizeof(str)-1;
asn1_read_value(value,name3,str,&len);
@@ -136,16 +134,14 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
,str2,&len);
if(!strcmp(str,str2)){
asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationName"
- ,&value,"certificate2-subject-O");
+ ,&value);
len = sizeof(str)-1;
asn1_read_value(cert,name3,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 */
- strcat(name3,".");
- strcat(name3,str);
+ asn1_read_value(value,"",str,&len); /* CHOICE */
+ strcpy(name3,str);
len = sizeof(str)-1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
@@ -157,15 +153,13 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer)
len = sizeof(str2);
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-at-organizationalUnitName",str2,&len);
if(!strcmp(str,str2)){
- asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value,"certificate2-subject-OU");
+ asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value);
len = sizeof(str)-1;
asn1_read_value(cert,name3,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 */
- strcat(name3,".");
- strcat(name3,str);
+ asn1_read_value(value,"",str,&len); /* CHOICE */
+ strcpy(name3,str);
len = sizeof(str)-1;
asn1_read_value(value,name3,str,&len);
str[len]=0;
@@ -201,126 +195,126 @@ create_CRL(node_asn *cert_def, unsigned char *der,int *der_len)
ASN1_TYPE value=ASN1_TYPE_EMPTY;
char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
- result=asn1_create_element(cert_def,"PKIX1Implicit88.CertificateList",&crl,"crl1");
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.CertificateList",&crl);
/* Use the next 3 lines to visit the empty certificate */
/* printf("-----------------\n");
- asn1_visit_tree(crl,"crl1");
+ asn1_visit_tree(crl,"");
printf("-----------------\n"); */
/* version: v2(1) */
- result=asn1_write_value(crl,"crl1.tbsCertList.version","v2",0);
+ result=asn1_write_value(crl,"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);
+ result=asn1_write_value(crl,"tbsCertList.signature.algorithm",str,1);
+ result=asn1_write_value(crl,"tbsCertList.signature.parameters",NULL,0);
/* issuer: Country="US" Organization="gov" OrganizationUnit="nist" */
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer","rdnSequence",1);
+ result=asn1_write_value(crl,"tbsCertList.issuer","rdnSequence",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence","NEW",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST","NEW",1);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence","NEW",1);
+ result=asn1_write_value(crl,"tbsCertList.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(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,1);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,1);
result=asn1_create_element(cert_def,"PKIX1Implicit88.X520countryName",
- &value,"countryName");
- result=asn1_write_value(value,"countryName","US",2);
- result=asn1_der_coding(value,"countryName",der,der_len,errorDescription);
+ &value);
+ result=asn1_write_value(value,"","US",2);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence","NEW",4);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST","NEW",4);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence","NEW",4);
+ result=asn1_write_value(crl,"tbsCertList.issuer.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(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,8);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,8);
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_der_coding(value,"OrgName",der,der_len,errorDescription);
+ &value);
+ result=asn1_write_value(value,"","printableString",1);
+ result=asn1_write_value(value,"printableString","gov",3);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence","NEW",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST","NEW",1);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence","NEW",1);
+ result=asn1_write_value(crl,"tbsCertList.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(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,1);
- 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_der_coding(value,"OrgUnitName",der,der_len,errorDescription);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence.?LAST.?LAST.type",str,1);
+ result=asn1_create_element(cert_def,"PKIX1Implicit88.X520OrganizationalUnitName",&value);
+ result=asn1_write_value(value,"","printableString",1);
+ result=asn1_write_value(value,"printableString","nist",4);
+ result=asn1_der_coding(value,"",der,der_len,errorDescription);
asn1_delete_structure(&value);
- result=asn1_write_value(crl,"crl1.tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
+ result=asn1_write_value(crl,"tbsCertList.issuer.rdnSequence.?LAST.?LAST.value",der,*der_len);
/* validity */
- result=asn1_write_value(crl,"crl1.tbsCertList.thisUpdate","utcTime",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.thisUpdate.utcTime","970801000000Z",1);
+ result=asn1_write_value(crl,"tbsCertList.thisUpdate","utcTime",1);
+ result=asn1_write_value(crl,"tbsCertList.thisUpdate.utcTime","970801000000Z",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.nextUpdate","utcTime",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.nextUpdate.utcTime","970808000000Z",1);
+ result=asn1_write_value(crl,"tbsCertList.nextUpdate","utcTime",1);
+ result=asn1_write_value(crl,"tbsCertList.nextUpdate.utcTime","970808000000Z",1);
/* revokedCertificates */
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates","NEW",1);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates","NEW",1);
str[0]=18;
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.userCertificate",str,1);
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.revocationDate","utcTime",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.revocationDate.utcTime","970731000000Z",1);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.userCertificate",str,1);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.revocationDate","utcTime",1);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.revocationDate.utcTime","970731000000Z",1);
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.crlEntryExtensions","NEW",1);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.crlEntryExtensions","NEW",1);
len = sizeof(str)-1;
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-ce-cRLReasons",
str,&len);
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.crlEntryExtensions.?LAST.extnID",str,1); /* reasonCode */
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.crlEntryExtensions.?LAST.critical","FALSE",1);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.crlEntryExtensions.?LAST.extnID",str,1); /* reasonCode */
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.crlEntryExtensions.?LAST.critical","FALSE",1);
str2="\x0a\x01\x01";
- result=asn1_write_value(crl,"crl1.tbsCertList.revokedCertificates.?LAST.crlEntryExtensions.?LAST.extnValue",str2,3);
+ result=asn1_write_value(crl,"tbsCertList.revokedCertificates.?LAST.crlEntryExtensions.?LAST.extnValue",str2,3);
/* crlExtensions */
- result=asn1_write_value(crl,"crl1.tbsCertList.crlExtensions",NULL,0);
+ result=asn1_write_value(crl,"tbsCertList.crlExtensions",NULL,0);
/* 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(crl,"crl1.signatureAlgorithm.algorithm",str,1);
- result=asn1_write_value(crl,"crl1.signatureAlgorithm.parameters",NULL,0); /* NO OPTION */
+ result=asn1_write_value(crl,"signatureAlgorithm.algorithm",str,1);
+ result=asn1_write_value(crl,"signatureAlgorithm.parameters",NULL,0); /* NO OPTION */
/* signature */
- result=asn1_der_coding(crl,"crl1.tbsCertList",der,der_len,errorDescription);
+ result=asn1_der_coding(crl,"tbsCertList",der,der_len,errorDescription);
if(result!=ASN1_SUCCESS){
printf("\n'tbsCertList' 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(crl,"crl1.signature",str2,46*8);
+ result=asn1_write_value(crl,"signature",str2,46*8);
/* Use the next 3 lines to visit the certificate */
/* printf("-----------------\n");
- asn1_visit_tree(crl,"crl1");
+ asn1_visit_tree(crl,"");
printf("-----------------\n"); */
- result=asn1_der_coding(crl,"crl1",der,der_len,errorDescription);
+ result=asn1_der_coding(crl,"",der,der_len,errorDescription);
if(result!=ASN1_SUCCESS){
printf("\n'crl1' encoding creation: ERROR\n");
return;
@@ -355,7 +349,7 @@ get_CRL(node_asn *cert_def,unsigned char *der,int der_len)
char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
- asn1_create_element(cert_def,"PKIX1Implicit88.CertificateList",&crl2,"crl2");
+ asn1_create_element(cert_def,"PKIX1Implicit88.CertificateList",&crl2);
result=asn1_der_decoding(&crl2,der,der_len,errorDescription);
@@ -366,30 +360,30 @@ get_CRL(node_asn *cert_def,unsigned char *der,int der_len)
/* issuer */
- get_Name_type(cert_def,crl2,"crl2.tbsCertList.issuer",str);
- printf("crl2:\nissuer =%s\n",str);
+ get_Name_type(cert_def,crl2,"tbsCertList.issuer",str);
+ printf("crl2:\nissuer: %s\n",str);
/* Verify sign */
len = sizeof(str)-1;
- result=asn1_read_value(crl2,"crl2.signatureAlgorithm.algorithm",str,&len);
+ result=asn1_read_value(crl2,"signatureAlgorithm.algorithm",str,&len);
result=asn1_read_value(cert_def,"PKIX1Implicit88.id-dsa-with-sha1",str2,&len);
if(!strcmp(str,str2)){ /* dsa-with-sha */
result=asn1_der_decoding_startEnd(crl2,der,der_len,
- "crl2.tbsCertList",&start,&end);
+ "tbsCertList",&start,&end);
/* add the lines to calculate the sha on der[start]..der[end] */
- result=asn1_read_value(crl2,"crl2.signature",str,&len);
+ result=asn1_read_value(crl2,"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(crl2,"crl2");
+ asn1_visit_tree(crl2,"");
printf("-----------------\n"); */
diff --git a/src/asn1Coding.c b/src/asn1Coding.c
index 5d5f13d..2688332 100644
--- a/src/asn1Coding.c
+++ b/src/asn1Coding.c
@@ -31,6 +31,10 @@
#include <malloc.h>
#include <config.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
@@ -44,7 +48,7 @@ char help_man[] = "asn1Coding generates a DER encoding from a file\n"
" <file1> file with ASN1 definitions.\n"
" <file2> file with assignments.\n"
"\n"
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
"Operation modes:\n"
" -h, --help shows this message and exit.\n"
" -v, --version shows version information and exit.\n"
@@ -121,7 +125,7 @@ int
main(int argc,char *argv[])
{
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
@@ -130,10 +134,10 @@ main(int argc,char *argv[])
{"output", required_argument, 0, 'o'},
{0, 0, 0, 0}
};
+ int option_index=0;
#endif
int option_result;
- int option_index = 0;
char *outputFileName=NULL;
char *inputFileAsnName=NULL;
char *inputFileAssignmentName=NULL;
@@ -146,7 +150,6 @@ main(int argc,char *argv[])
FILE *inputFile;
char varName[1024];
char value[1024];
- char structureName[1024];
unsigned char der[1024];
int der_len;
int k;
@@ -157,7 +160,7 @@ main(int argc,char *argv[])
while(1){
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index);
#else
option_result=getopt(argc,argv,"hvco:");
@@ -260,8 +263,7 @@ main(int argc,char *argv[])
while(readAssignment(inputFile,varName,value) == ASSIGNMENT_SUCCESS){
printf("var=%s, value=%s\n",varName,value);
if(structure==ASN1_TYPE_EMPTY){
- asn1_result=asn1_create_element(definitions,value,&structure,varName);
- strcpy(structureName,varName);
+ asn1_result=asn1_create_element(definitions,value,&structure);
}
else
asn1_result=asn1_write_value(structure,varName,value,0);
@@ -282,9 +284,9 @@ main(int argc,char *argv[])
fclose(inputFile);
printf("\n");
- asn1_print_structure(stdout,structure,structureName,ASN1_PRINT_NAME_TYPE_VALUE);
+ asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
- asn1_result=asn1_der_coding(structure,structureName,der,&der_len,
+ asn1_result=asn1_der_coding(structure,"",der,&der_len,
errorDescription);
printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result));
if(asn1_result!=ASN1_SUCCESS){
diff --git a/src/asn1Decoding.c b/src/asn1Decoding.c
index 928cdfb..780ab9c 100644
--- a/src/asn1Decoding.c
+++ b/src/asn1Decoding.c
@@ -31,6 +31,10 @@
#include <malloc.h>
#include <config.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
@@ -45,7 +49,7 @@ char help_man[] = "asn1Decoding generates an ASN1 type from a file\n"
" <file2> file with a DER coding.\n"
" <type> ASN1 type name\n"
"\n"
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
"Operation modes:\n"
" -h, --help shows this message and exit.\n"
" -v, --version shows version information and exit.\n"
@@ -67,7 +71,7 @@ int
main(int argc,char *argv[])
{
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
@@ -75,14 +79,13 @@ main(int argc,char *argv[])
{"check", no_argument, 0, 'c'},
{0, 0, 0, 0}
};
+ int option_index = 0;
#endif
int option_result;
- int option_index = 0;
char *inputFileAsnName=NULL;
char *inputFileDerName=NULL;
char *typeName=NULL;
- char *varName=NULL;
int checkSyntaxOnly=0;
ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
ASN1_TYPE structure=ASN1_TYPE_EMPTY;
@@ -91,7 +94,6 @@ main(int argc,char *argv[])
FILE *inputFile;
unsigned char der[1024];
int der_len=0;
- char *dot_p,*char_p;
opterr=0; /* disable error messages from getopt */
@@ -99,7 +101,7 @@ main(int argc,char *argv[])
while(1){
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
#else
option_result=getopt(argc,argv,"hvc");
@@ -205,20 +207,9 @@ main(int argc,char *argv[])
der_len++;
fclose(inputFile);
-
- /* varName creation */
- dot_p=typeName;
- char_p=typeName;
- while((char_p=strchr(char_p,'.'))){
- char_p++;
- dot_p=char_p;
- }
-
- /* varName= inputFileName after the last '.' */
- varName=(char *)malloc(strlen(typeName)-(dot_p-typeName)+1);
- strcpy(varName,dot_p);
- asn1_result=asn1_create_element(definitions,typeName,&structure,varName);
+ asn1_result=asn1_create_element(definitions,typeName,&structure);
+
if(asn1_result != ASN1_SUCCESS){
printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
asn1_delete_structure(&definitions);
@@ -226,7 +217,6 @@ main(int argc,char *argv[])
free(inputFileAsnName);
free(inputFileDerName);
free(typeName);
- free(varName);
exit(1);
}
@@ -236,7 +226,7 @@ main(int argc,char *argv[])
printf("asn1Decoding: %s\n",errorDescription);
printf("\nDECODING RESULT:\n");
- asn1_print_structure(stdout,structure,varName,ASN1_PRINT_NAME_TYPE_VALUE);
+ asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
asn1_delete_structure(&definitions);
@@ -244,8 +234,7 @@ main(int argc,char *argv[])
free(inputFileAsnName);
free(inputFileDerName);
- free(typeName);
- free(varName);
+ free(typeName);
if(asn1_result != ASN1_SUCCESS)
exit(1);
diff --git a/src/asn1Parser.c b/src/asn1Parser.c
index 52f08bf..e071ad9 100644
--- a/src/asn1Parser.c
+++ b/src/asn1Parser.c
@@ -31,6 +31,10 @@
#include <malloc.h>
#include <config.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
@@ -42,7 +46,7 @@ char help_man[] = "asn1Parser reads files with ASN1 definitions and\n"
"\n"
"Usage: asn1Parser [options] file\n"
"\n"
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
"Operation modes:\n"
" -h, --help shows this message and exit\n"
" -v, --version shows version information and exit.\n"
@@ -70,7 +74,7 @@ int
main(int argc,char *argv[])
{
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
@@ -80,10 +84,10 @@ main(int argc,char *argv[])
{"name", required_argument, 0, 'n'},
{0, 0, 0, 0}
};
+ int option_index = 0;
#endif
int option_result;
- int option_index = 0;
char *outputFileName=NULL;
char *inputFileName=NULL;
char *vectorName=NULL;
@@ -98,7 +102,7 @@ main(int argc,char *argv[])
while(1){
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
#else
option_result=getopt(argc,argv,"hvco:n:");
@@ -108,7 +112,7 @@ main(int argc,char *argv[])
switch(option_result){
case 0:
-#ifdef HAVE_GETOPT_H
+#ifdef HAVE_GETOPT_LONG
printf("option %s",long_options[option_index].name);
if(optarg) printf(" with arg %s",optarg);
printf("\n");
diff --git a/tests/Test_parser.c b/tests/Test_parser.c
index 7c3a397..31755c6 100644
--- a/tests/Test_parser.c
+++ b/tests/Test_parser.c
@@ -77,8 +77,8 @@ test_type test_array[]={
ASN1_SYNTAX_ERROR,_FILE_":12: parse error near '}'"},
{12,"Integer ::= INTEGER {v1(0), 1}",
ASN1_SYNTAX_ERROR,_FILE_":12: parse error near '1'"},
- {12,"const1 INTEGER ::= -10",ASN1_SUCCESS,""},
- {12,"const1 INTEGER ::= 10",ASN1_SUCCESS,""},
+ {12,"const1 INTEGER ::= -1",ASN1_SUCCESS,""},
+ {12,"const1 INTEGER ::= 1",ASN1_SUCCESS,""},
{12,"const1 INTEGER ::= v1",
ASN1_SYNTAX_ERROR,_FILE_":12: parse error near 'v1'"},
{16," generic generalstring",
@@ -152,7 +152,7 @@ main(int argc,char *argv[])
}
/* Only for Test */
- //asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL);
+ /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
/* Clear the definitions structures */
asn1_delete_structure(&definitions);
diff --git a/tests/Test_tree.c b/tests/Test_tree.c
index d5613de..bfd58c8 100644
--- a/tests/Test_tree.c
+++ b/tests/Test_tree.c
@@ -45,8 +45,10 @@
#define ACT_DECODING 7
#define ACT_PRINT_DER 8
#define ACT_EXPAND_ANY 9
-#define ACT_DECODING_ELEMENT 10
-#define ACT_EXPAND_OCTET 11
+#define ACT_DECODING_ELEMENT 10
+#define ACT_EXPAND_OCTET 11
+#define ACT_NUMBER_OF_ELEMENTS 12
+#define ACT_DECODING_START_END 13
typedef struct{
int action;
@@ -60,20 +62,78 @@ typedef struct{
test_type test_array[]={
{ACT_DELETE,"","",0,ASN1_ELEMENT_NOT_FOUND},
- {ACT_CREATE,"TEST_TREE.AnyTest2","any",0,ASN1_SUCCESS},
- {ACT_WRITE,"any","int",0,ASN1_SUCCESS},
- {ACT_WRITE,"any.int","10",0,ASN1_SUCCESS},
- {ACT_ENCODING,"any",0,0,ASN1_SUCCESS},
+ /* Test: Integer */
+ {ACT_CREATE,"TEST_TREE.AnyTest2",0,0,ASN1_SUCCESS},
+ {ACT_WRITE,"","int",0,ASN1_SUCCESS},
+ {ACT_WRITE,"int","0",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_WRITE,"int","-1",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_WRITE,"int","1",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_WRITE,"int","2000000000",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_WRITE,"int","-2000000000",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_WRITE,"int","-20000000000",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_WRITE,"int","20000000000",0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
{ACT_DELETE,"","",0,ASN1_SUCCESS},
- {ACT_CREATE,"TEST_TREE.SequenceTestAny","Seq",0,ASN1_SUCCESS},
- // {ACT_WRITE,"Seq.id","2 5 29 2",0,ASN1_SUCCESS},
- //{ACT_WRITE,"Seq.any1",0,0,ASN1_SUCCESS},
- //{ACT_WRITE,"Seq.any1","DER",0,ASN1_SUCCESS},
+ /* Test: elements without names */
+ {ACT_CREATE,"TEST_TREE.Sequence1",0,0,ASN1_SUCCESS},
+ {ACT_WRITE,"int2","10",0,ASN1_SUCCESS},
+ {ACT_WRITE,"seq","NEW",1,ASN1_SUCCESS},
+ {ACT_WRITE,"seq.?LAST","1",0,ASN1_SUCCESS},
+ {ACT_WRITE,"seq","NEW",1,ASN1_SUCCESS},
+ {ACT_WRITE,"seq.?LAST","2",0,ASN1_SUCCESS},
+ {ACT_NUMBER_OF_ELEMENTS,"seq","",2,ASN1_SUCCESS},
+ {ACT_WRITE,"id","1 2 3 4",0,ASN1_SUCCESS},
+ {ACT_WRITE,"oct","\x30\x03\x02\x01\x15",5,ASN1_SUCCESS},
+ {ACT_ENCODING,"int2",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_ENCODING,"seq.?2",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_ENCODING,"",0,0,ASN1_SUCCESS},
+ {ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
+ {ACT_CREATE,"TEST_TREE.Sequence1",0,0,ASN1_SUCCESS},
+ {ACT_DECODING_ELEMENT,"int2",0,0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_READ,"int2","\x0a",1,ASN1_SUCCESS},
+ {ACT_CREATE,"TEST_TREE.Sequence1",0,0,ASN1_SUCCESS},
+ {ACT_DECODING,0,0,0,ASN1_SUCCESS},
+ {ACT_DECODING_START_END,"seq.?2","START",10,ASN1_SUCCESS},
+ {ACT_EXPAND_OCTET,"oct","id",0,ASN1_SUCCESS},
+ {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS},
+ {ACT_DELETE,"","",0,ASN1_SUCCESS},
+
+
+ /*
+ {ACT_CREATE,"TEST_TREE.SequenceTestAny","Seq",0,ASN1_SUCCESS},
+
+ {ACT_WRITE,"Seq.id","2 5 29 2",0,ASN1_SUCCESS},
+ {ACT_WRITE,"Seq.any1",0,0,ASN1_SUCCESS},
+ {ACT_WRITE,"Seq.any1","DER",0,ASN1_SUCCESS},
- //{ACT_WRITE,"Seq.any2","NEW",1,ASN1_SUCCESS},
- //{ACT_WRITE,"Seq.any2.?LAST","DER",0,ASN1_SUCCESS},
+ {ACT_WRITE,"Seq.any2","NEW",1,ASN1_SUCCESS},
+ {ACT_WRITE,"Seq.any2.?LAST","DER",0,ASN1_SUCCESS},
+
{ACT_WRITE,"Seq.i","10",0,ASN1_SUCCESS},
{ACT_WRITE,"Seq.subjectPublicKeyInfo.algorithm.algorithm","1 2 3 4 5"
@@ -88,25 +148,27 @@ test_type test_array[]={
{ACT_DELETE,"","",0,ASN1_SUCCESS},
{ACT_CREATE,"TEST_TREE.SequenceTestAny","Seq",0,ASN1_SUCCESS},
- //{ACT_DECODING,0,0,0,ASN1_SUCCESS},
+
+ {ACT_DECODING,0,0,0,ASN1_SUCCESS},
+
+ {ACT_DECODING_ELEMENT,"Seq.any2",0,0,ASN1_SUCCESS},
- // {ACT_DECODING_ELEMENT,"Seq.any2",0,0,ASN1_SUCCESS},
{ACT_DECODING_ELEMENT,"Seq.subjectPublicKeyInfo",0,0,ASN1_SUCCESS},
- //{ACT_DECODING_ELEMENT,"Seq.id",0,0,ASN1_SUCCESS},
- //{ACT_EXPAND_ANY,0,0,0,ASN1_SUCCESS},
- //{ACT_EXPAND_OCTET,"Seq.oct1","Seq.id",0,ASN1_SUCCESS},
+ {ACT_DECODING_ELEMENT,"Seq.id",0,0,ASN1_SUCCESS},
+ {ACT_EXPAND_ANY,0,0,0,ASN1_SUCCESS},
- /*
+ {ACT_EXPAND_OCTET,"Seq.oct1","Seq.id",0,ASN1_SUCCESS},
+
{ACT_CREATE,"TEST_TREE.Sequence1","Seq",0,ASN1_SUCCESS},
{ACT_WRITE,"Seq.int1","1",0,ASN1_SUCCESS},
{ACT_READ,"Seq.int1","\x01",1,ASN1_SUCCESS},
- */
{ACT_VISIT,"Seq","",ASN1_PRINT_ALL,ASN1_SUCCESS},
{ACT_DELETE,"","",0,ASN1_SUCCESS},
-
+ */
/* Test GeneralString */
+ /*
{ACT_CREATE,"TEST_TREE.Test3","test",0,ASN1_SUCCESS},
{ACT_WRITE,"test.a","1234",0,ASN1_SUCCESS},
{ACT_WRITE,"test.b","prova",5,ASN1_SUCCESS},
@@ -114,12 +176,12 @@ test_type test_array[]={
{ACT_PRINT_DER,0,0,0,ASN1_SUCCESS},
{ACT_DELETE,"","",0,ASN1_SUCCESS},
{ACT_CREATE,"TEST_TREE.Test3","test",0,ASN1_SUCCESS},
- //{ACT_DECODING,0,0,0,ASN1_SUCCESS},
+ {ACT_DECODING,0,0,0,ASN1_SUCCESS},
{ACT_DECODING_ELEMENT,"test.b",0,0,ASN1_SUCCESS},
{ACT_READ,"test.b","prova",5,ASN1_SUCCESS},
{ACT_VISIT,"test","",ASN1_PRINT_ALL,ASN1_SUCCESS},
{ACT_DELETE,"","",0,ASN1_SUCCESS},
-
+ */
/* end */
{ACT_NULL}
@@ -140,6 +202,7 @@ main(int argc,char *argv[])
unsigned char value[1024],der[1024];
int valueLen;
int k;
+ int start,end;
printf("\n\n/****************************************/\n");
printf( "/* Test sequence : Test_tree */\n");
@@ -166,10 +229,12 @@ main(int argc,char *argv[])
exit(1);
}
+ /*
if(1){
asn1_print_structure(out,definitions,"TEST_TREE",ASN1_PRINT_ALL);
fprintf(out,"\n");
}
+ */
test=test_array;
@@ -178,8 +243,7 @@ main(int argc,char *argv[])
switch(test->action){
case ACT_CREATE:
- result=asn1_create_element(definitions,test->par1,&asn1_element,
- test->par2);
+ result=asn1_create_element(definitions,test->par1,&asn1_element);
break;
case ACT_DELETE:
result=asn1_delete_structure(&asn1_element);
@@ -205,6 +269,10 @@ main(int argc,char *argv[])
result=asn1_der_decoding_element(&asn1_element,test->par1,der,der_len,
errorDescription);
break;
+ case ACT_DECODING_START_END:
+ result=asn1_der_decoding_startEnd(asn1_element,der,der_len,test->par1,
+ &start,&end);
+ break;
case ACT_EXPAND_ANY:
result=asn1_expand_any_defined_by(definitions,&asn1_element);
break;
@@ -225,6 +293,9 @@ main(int argc,char *argv[])
printf("\n\n");
result=ASN1_SUCCESS;
break;
+ case ACT_NUMBER_OF_ELEMENTS:
+ result=asn1_number_of_elements(asn1_element,test->par1,&valueLen);
+ break;
default:
break;
}
@@ -237,7 +308,6 @@ main(int argc,char *argv[])
case ACT_VISIT:
case ACT_ENCODING:
case ACT_DECODING:
- case ACT_DECODING_ELEMENT:
case ACT_PRINT_DER:
case ACT_EXPAND_ANY:
case ACT_EXPAND_OCTET:
@@ -250,6 +320,43 @@ main(int argc,char *argv[])
printf(" Error detected: %s\n\n",libtasn1_strerror(result));
}
break;
+ case ACT_DECODING_ELEMENT:
+ if(result != test->errorNumber){
+ errorCounter++;
+ printf("ERROR N. %d:\n",errorCounter);
+ printf(" Action %d - %s - %s - %d\n",test->action,test->par1,
+ test->par2,test->par3);
+ printf(" Error expected: %s\n",libtasn1_strerror(test->errorNumber));
+ printf(" Error detected: %s\n",libtasn1_strerror(result));
+ printf(" Error description : %s\n\n",errorDescription);
+ }
+ break;
+ case ACT_NUMBER_OF_ELEMENTS:
+ if((result != test->errorNumber) ||
+ (valueLen != test->par3)){
+ errorCounter++;
+ printf("ERROR N. %d:\n",errorCounter);
+ printf(" Action %d - %s\n",test->action,test->par1);
+ printf(" Error expected: %s - %d\n",libtasn1_strerror(test->errorNumber),
+ test->par3);
+ printf("\n Error detected: %s - %d\n\n",libtasn1_strerror(result),
+ valueLen);
+ }
+ break;
+ case ACT_DECODING_START_END:
+ if((result != test->errorNumber) ||
+ ((!strcmp(test->par2,"START")) && (start != test->par3)) ||
+ ((!strcmp(test->par2,"END")) && (end != test->par3)) ){
+ errorCounter++;
+ printf("ERROR N. %d:\n",errorCounter);
+ printf(" Action %d - %s - %d\n",test->action,test->par1,
+ test->par3);
+ printf(" Error expected: %s - %s - %d\n",libtasn1_strerror(test->errorNumber),
+ test->par2,test->par3);
+ printf(" Error detected: %s - %d - %d\n\n",libtasn1_strerror(result),
+ start,end);
+ }
+ break;
case ACT_READ: