diff options
author | Fabio Fiorina <fiorinaf@gnutls.org> | 2003-02-12 21:18:01 +0000 |
---|---|---|
committer | Fabio Fiorina <fiorinaf@gnutls.org> | 2003-02-12 21:18:01 +0000 |
commit | 53449a4560f3dbded7f48b2929427fa9be4dde54 (patch) | |
tree | e3af45856ee55ca3b0fee12fdc52863679cba7c4 | |
parent | 582238c1a8598bcf7bb820cfbc988082159b9ff5 (diff) | |
download | libtasn1_0_2_1.tar.gz |
add read_tag and get_structure_from_oid functionslibtasn1_0_2_1
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | configure.in | 2 | ||||
-rw-r--r-- | doc/asn1.tex | 2 | ||||
-rw-r--r-- | lib/decoding.c | 6 | ||||
-rw-r--r-- | lib/element.c | 26 | ||||
-rw-r--r-- | lib/int.h | 25 | ||||
-rw-r--r-- | lib/libtasn1.h | 33 | ||||
-rw-r--r-- | lib/structure.c | 66 | ||||
-rw-r--r-- | tests/Test_tree.asn | 11 | ||||
-rw-r--r-- | tests/Test_tree.c | 44 |
10 files changed, 193 insertions, 26 deletions
@@ -1,3 +1,7 @@ +Version 0.2.1 +- Add asn1_find_structure_from_oid function +- Add asn1_read_tag function + Version 0.2.0 - Support for other platforms - Change asn1_create_element function interface (dest_name not needed any more) diff --git a/configure.in b/configure.in index a2c2303..e0e2e2e 100644 --- a/configure.in +++ b/configure.in @@ -12,7 +12,7 @@ AC_DEFINE_UNQUOTED(T_OS, "$target_os") dnl libtasn1 Version ASN1_MAJOR_VERSION=0 ASN1_MINOR_VERSION=2 -ASN1_MICRO_VERSION=0 +ASN1_MICRO_VERSION=1 ASN1_VERSION=$ASN1_MAJOR_VERSION.$ASN1_MINOR_VERSION.$ASN1_MICRO_VERSION AC_DEFINE_UNQUOTED(ASN1_VERSION, "$ASN1_VERSION") diff --git a/doc/asn1.tex b/doc/asn1.tex index 937d453..ab42b6d 100644 --- a/doc/asn1.tex +++ b/doc/asn1.tex @@ -39,7 +39,7 @@ chapter entitled "GNU Free Documentation License". \chapter{ASN.1 structures handling} \section{Introduction} - This document describes the version 0.2.0 of library 'libtasn1' developed + This document describes the version 0.2.1 of library 'libtasn1' developed for ASN1 (Abstract Syntax Notation One) structures management. The main features of this library are: \begin{itemize} diff --git a/lib/decoding.c b/lib/decoding.c index 7d79563..811a476 100644 --- a/lib/decoding.c +++ b/lib/decoding.c @@ -1527,7 +1527,7 @@ asn1_expand_any_defined_by(ASN1_TYPE definitions,ASN1_TYPE *element) * @definitions: ASN1 definitions * @element: pointer to an ASN1 structure * @octetName: name of the OCTECT STRING field to expand. - * &objectName: name of the OBJECT IDENTIFIER field to use to define + * @objectName: name of the OBJECT IDENTIFIER field to use to define * the type for expansion. * * Description: @@ -1541,9 +1541,9 @@ asn1_expand_any_defined_by(ASN1_TYPE definitions,ASN1_TYPE *element) * * ASN1_SUCCESS\: substitution OK * - * ASN1_ELEMENT_NOT_FOUND: OBJECTNAME or OCTETNAME are not correct. + * ASN1_ELEMENT_NOT_FOUND\: OBJECTNAME or OCTETNAME are not correct. * - * ASN1_VALUE_NOT_VALID: wasn't possible to find the type to use + * ASN1_VALUE_NOT_VALID\: wasn't possible to find the type to use * for expansion. * * other errors\: result of der decoding process. diff --git a/lib/element.c b/lib/element.c index 26af4e6..c38c8b9 100644 --- a/lib/element.c +++ b/lib/element.c @@ -716,27 +716,26 @@ asn1_read_tag(node_asn *root,const char *name,int *tag, int *class) if(node->type&CONST_TAG){ while(p){ if(type_field(p->type)==TYPE_TAG){ - if(p->type&CONST_IMPLICIT) - pTag=p; - else - pTag=NULL; + if((p->type&CONST_IMPLICIT) && (pTag==NULL)) + pTag=p; + else if(p->type&CONST_EXPLICIT) + pTag=NULL; } p=p->right; } } if(pTag){ - *tag=strtoul(p->value,NULL,10); - /* - if(p->type&CONST_APPLICATION) *class=ASN1_CLASS_APPLICATION; - else if(p->type&CONST_UNIVERSAL) *class=ASN1_CLASS_UNIVERSAL; - else if(p->type&CONST_PRIVATE) *class=ASN1_CLASS_PRIVATE; + *tag=strtoul(pTag->value,NULL,10); + + if(pTag->type&CONST_APPLICATION) *class=ASN1_CLASS_APPLICATION; + else if(pTag->type&CONST_UNIVERSAL) *class=ASN1_CLASS_UNIVERSAL; + else if(pTag->type&CONST_PRIVATE) *class=ASN1_CLASS_PRIVATE; else *class=ASN1_CLASS_CONTEXT_SPECIFIC; - */ } else{ - /* - *class=ASN!_CLASS_UNIVERSAL; + *class=ASN1_CLASS_UNIVERSAL; + switch(type_field(node->type)){ case TYPE_NULL: *tag=ASN1_TAG_NULL;break; @@ -769,9 +768,8 @@ asn1_read_tag(node_asn *root,const char *name,int *tag, int *class) case TYPE_ANY: break; default: - break + break; } - */ } @@ -40,6 +40,31 @@ #define MAX_NAME_SIZE 128 /* maximum number of characters of a name inside an ASN1 file definitons */ #define MAX_ERROR_DESCRIPTION_SIZE 128 /* maximum number of characters of a description message */ +/*****************************************/ +/* Constants returned by asn1_read_tag */ +/*****************************************/ +#define ASN1_CLASS_UNIVERSAL 1 +#define ASN1_CLASS_APPLICATION 2 +#define ASN1_CLASS_CONTEXT_SPECIFIC 3 +#define ASN1_CLASS_PRIVATE 4 + + +/*****************************************/ +/* Constants returned by asn1_read_tag */ +/*****************************************/ +#define ASN1_TAG_BOOLEAN 0x01 +#define ASN1_TAG_INTEGER 0x02 +#define ASN1_TAG_SEQUENCE 0x10 +#define ASN1_TAG_SET 0x11 +#define ASN1_TAG_OCTET_STRING 0x04 +#define ASN1_TAG_BIT_STRING 0x03 +#define ASN1_TAG_UTCTime 0x17 +#define ASN1_TAG_GENERALIZEDTime 0x18 +#define ASN1_TAG_OBJECT_ID 0x06 +#define ASN1_TAG_ENUMERATED 0x0A +#define ASN1_TAG_NULL 0x05 +#define ASN1_TAG_GENERALSTRING 0x1B + /* define used for visiting trees */ #define UP 1 diff --git a/lib/libtasn1.h b/lib/libtasn1.h index 1342fc3..31ceee3 100644 --- a/lib/libtasn1.h +++ b/lib/libtasn1.h @@ -28,7 +28,7 @@ extern "C" { #endif -#define LIBASN1_VERSION "0.2.0" +#define LIBASN1_VERSION "0.2.1" #include <sys/types.h> #include <time.h> @@ -71,6 +71,31 @@ typedef int asn1_retCode; /* type returned by libasn1 functions */ #define ASN1_PRINT_NAME_TYPE_VALUE 3 #define ASN1_PRINT_ALL 4 +/*****************************************/ +/* Constants returned by asn1_read_tag */ +/*****************************************/ +#define ASN1_CLASS_UNIVERSAL 1 +#define ASN1_CLASS_APPLICATION 2 +#define ASN1_CLASS_CONTEXT_SPECIFIC 3 +#define ASN1_CLASS_PRIVATE 4 + + +/*****************************************/ +/* Constants returned by asn1_read_tag */ +/*****************************************/ +#define ASN1_TAG_BOOLEAN 0x01 +#define ASN1_TAG_INTEGER 0x02 +#define ASN1_TAG_SEQUENCE 0x10 +#define ASN1_TAG_SET 0x11 +#define ASN1_TAG_OCTET_STRING 0x04 +#define ASN1_TAG_BIT_STRING 0x03 +#define ASN1_TAG_UTCTime 0x17 +#define ASN1_TAG_GENERALIZEDTime 0x18 +#define ASN1_TAG_OBJECT_ID 0x06 +#define ASN1_TAG_ENUMERATED 0x0A +#define ASN1_TAG_NULL 0x05 +#define ASN1_TAG_GENERALSTRING 0x1B + /******************************************************/ /* Structure definition used for the node of the tree */ @@ -145,6 +170,12 @@ asn1_retCode asn1_expand_any_defined_by(ASN1_TYPE definitions, asn1_retCode asn1_expand_octet_string(ASN1_TYPE definitions,ASN1_TYPE *element, const char *octetName,const char *objectName); +asn1_retCode asn1_read_tag(node_asn *root,const char *name,int *tag, + int *class); + +asn1_retCode asn1_find_structure_from_oid(ASN1_TYPE definitions, + const char *oidValue,char *structureName); + const char* libtasn1_strerror(asn1_retCode error); void libtasn1_perror(asn1_retCode error); diff --git a/lib/structure.c b/lib/structure.c index e7bed80..7eb6fde 100644 --- a/lib/structure.c +++ b/lib/structure.c @@ -827,9 +827,9 @@ asn1_print_structure(FILE *out,ASN1_TYPE structure,const char *name,int mode) * * Returns: * - * ASN1_SUCCESS: creation OK - * ASN1_ELEMENT_NOT_FOUND: NAME isn't known - * ASN1_GENERIC_ERROR: pointer num equal to NULL + * ASN1_SUCCESS\: creation OK + * ASN1_ELEMENT_NOT_FOUND\: NAME isn't known + * ASN1_GENERIC_ERROR\: pointer num equal to NULL * **/ asn1_retCode @@ -855,8 +855,68 @@ asn1_number_of_elements(ASN1_TYPE element,const char *name,int *num) } +/** + * asn1_find_structure_from_oid - Search the structure that is defined just + * after an OID definition. + * @definitions: ASN1 definitions + * @oidValue: value of the OID to search (e.g. "1.2.3.4"). + * @structureName: name returned by the function, that is the structure + * defined just after the OID of value equal to OIDVALUE. + * It must be an array of MAX_NAME_SIZE char elements. + * + * Description: + * + * Search the structure that is defined just after an OID definition. + * + * Returns: + * + * ASN1_SUCCESS\: structure found. + * + * ASN1_ELEMENT_NOT_FOUND\: OID equal to OIDVALUE not found. + * + **/ +asn1_retCode +asn1_find_structure_from_oid(ASN1_TYPE definitions, + const char *oidValue,char *structureName) +{ + char definitionsName[MAX_NAME_SIZE],name[2*MAX_NAME_SIZE+1]; + char value[MAX_NAME_SIZE]; + ASN1_TYPE p; + int len; + asn1_retCode result; + + if((definitions==ASN1_TYPE_EMPTY) || (oidValue==NULL)) + return ASN1_ELEMENT_NOT_FOUND; + + + strcpy(definitionsName,definitions->name); + strcat(definitionsName,"."); + /* search the OBJECT_ID into definitions */ + p=definitions->down; + while(p){ + if((type_field(p->type)==TYPE_OBJECT_ID) && + (p->type & CONST_ASSIGN)){ + strcpy(name,definitionsName); + strcat(name,p->name); + + len=MAX_NAME_SIZE; + result=asn1_read_value(definitions,name,value,&len); + + if((result == ASN1_SUCCESS) && (!strcmp(oidValue,value))){ + p=p->right; + if(p==NULL) /* reach the end of ASN1 definitions */ + return ASN1_ELEMENT_NOT_FOUND; + + strcpy(structureName,p->name); + return ASN1_SUCCESS; + } + } + p=p->right; + } + return ASN1_ELEMENT_NOT_FOUND; +} diff --git a/tests/Test_tree.asn b/tests/Test_tree.asn index 9c3e963..7c36822 100644 --- a/tests/Test_tree.asn +++ b/tests/Test_tree.asn @@ -9,6 +9,15 @@ DEFINITIONS IMPLICIT TAGS ::= BEGIN +SequenceTestTag ::= SEQUENCE{ + int1 [2] EXPLICIT INTEGER, + int2 [3] IMPLICIT INTEGER, + str1 [1] IMPLICIT PrintableString, + str2 UniversalString, + str3 [2] EXPLICIT UniversalString +} + + Sequence1 ::= SEQUENCE{ int1 [0] INTEGER DEFAULT -5, int2 INTEGER, @@ -81,6 +90,8 @@ X520LocalityName ::= CHOICE { bmpString BMPString } +id-Test OBJECT IDENTIFIER ::= {1 2 29 2} + END diff --git a/tests/Test_tree.c b/tests/Test_tree.c index 1b9b2af..b810c06 100644 --- a/tests/Test_tree.c +++ b/tests/Test_tree.c @@ -51,6 +51,8 @@ #define ACT_DECODING_START_END 13 #define ACT_READ_DEFINITIONS 14 #define ACT_READ_TAG_CLASS 15 +#define ACT_OID_2_STRUCTURE 16 + typedef struct{ int action; @@ -65,6 +67,28 @@ test_type test_array[]={ {ACT_DELETE,"","",0,ASN1_ELEMENT_NOT_FOUND}, + /* Test: OID to STRUCTURE */ + {ACT_OID_2_STRUCTURE,"2.5.29.3","",0,ASN1_ELEMENT_NOT_FOUND}, + {ACT_OID_2_STRUCTURE,"1.2.29.2","",0,ASN1_ELEMENT_NOT_FOUND}, + {ACT_OID_2_STRUCTURE,"2.5.29.2","anyTest2",0,ASN1_SUCCESS}, + + /* Test: READ TAG and CLASS */ + {ACT_CREATE,"TEST_TREE.SequenceTestTag",0,0,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"int","",0,ASN1_ELEMENT_NOT_FOUND}, + {ACT_READ_TAG_CLASS,"int1","TAG",ASN1_TAG_INTEGER,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"int1","CLASS",ASN1_CLASS_UNIVERSAL,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"int2","TAG",3,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"int2","CLASS",ASN1_CLASS_CONTEXT_SPECIFIC,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"str1","TAG",1,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"str1","CLASS",ASN1_CLASS_CONTEXT_SPECIFIC,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"str2","TAG",28,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"str2","CLASS",ASN1_CLASS_UNIVERSAL,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"str3","TAG",28,ASN1_SUCCESS}, + {ACT_READ_TAG_CLASS,"str3","CLASS",ASN1_CLASS_UNIVERSAL,ASN1_SUCCESS}, + {ACT_VISIT,"","",ASN1_PRINT_ALL,ASN1_SUCCESS}, + {ACT_DELETE,"","",0,ASN1_SUCCESS}, + + /* Test: OBJECT IDENTIFIER elements */ {ACT_CREATE,"TEST_TREE.Sequence1",0,0,ASN1_SUCCESS}, {ACT_WRITE,"int2","0",0,ASN1_SUCCESS}, @@ -268,9 +292,7 @@ main(int argc,char *argv[]) result=asn1_read_value(definitions,test->par1,value,&valueLen); break; case ACT_READ_TAG_CLASS: - /* - result=asn1_read_value(asn1_element,test->par1,&tag,&class); - */ + result=asn1_read_tag(asn1_element,test->par1,&tag,&class); break; case ACT_ENCODING: result=asn1_der_coding(asn1_element,test->par1,der,&der_len, @@ -295,6 +317,9 @@ main(int argc,char *argv[]) result=asn1_expand_octet_string(definitions,&asn1_element,test->par1, test->par2); break; + case ACT_OID_2_STRUCTURE: + result=asn1_find_structure_from_oid(definitions,test->par1,value); + break; case ACT_VISIT: asn1_print_structure(out,asn1_element,test->par1,test->par3); fprintf(out,"\n"); @@ -358,6 +383,18 @@ main(int argc,char *argv[]) valueLen); } break; + case ACT_OID_2_STRUCTURE: + if((result != test->errorNumber) || + ((result == ASN1_SUCCESS) && (strcmp(value,test->par2)))){ + errorCounter++; + printf("ERROR N. %d:\n",errorCounter); + printf(" Action %d - %s\n",test->action,test->par1); + printf(" Error expected: %s - %s\n",libtasn1_strerror(test->errorNumber), + test->par2); + printf("\n Error detected: %s - %s\n\n",libtasn1_strerror(result), + value); + } + break; case ACT_DECODING_START_END: if((result != test->errorNumber) || ((!strcmp(test->par2,"START")) && (start != test->par3)) || @@ -377,6 +414,7 @@ main(int argc,char *argv[]) if((result != test->errorNumber) || ((!strcmp(test->par2,"TAG")) && (tag != test->par3)) || ((!strcmp(test->par2,"CLASS")) && (class != test->par3))){ + errorCounter++; printf("ERROR N. %d:\n",errorCounter); printf(" Action %d - %s - %d\n",test->action,test->par1, test->par3); |