Skip to content

Instantly share code, notes, and snippets.

@kalloc
Last active August 29, 2015 14:14
Show Gist options
  • Save kalloc/c7201c729e442650a6f1 to your computer and use it in GitHub Desktop.
Save kalloc/c7201c729e442650a6f1 to your computer and use it in GitHub Desktop.
static int checkEncapsulate( FILE *inFile, const int length ) {
ASN1_ITEM nestedItem;
const int currentPos = fPos;
int diffPos;
/* If we're not looking for encapsulated objects, return */
if( !checkEncaps )
return( FALSE );
/* Read the details of the next item in the input stream */
getItem( inFile, &nestedItem );
diffPos = fPos - currentPos;
fPos = currentPos;
fseek( inFile, -diffPos, SEEK_CUR );
/* If it's not a standard tag class, don't try and dig down into it */
if( ( nestedItem.id & CLASS_MASK ) != UNIVERSAL && \
( nestedItem.id & CLASS_MASK ) != CONTEXT )
return( FALSE );
/* If it doesn't fit exactly within the current item it's not an-
encapsulated object */
if( nestedItem.length != length - diffPos )
return( FALSE );
/* If it doesn't have a valid-looking tag, don't try and go any further */
if( nestedItem.tag <= 0 || nestedItem.tag > 0x31 )
return( FALSE );
/* Now things get a bit complicated because it's possible to get some-
(very rare) false positives, for example if a NUMERICSTRING of
exactly the right length is nested within an OCTET STRING, since-
numeric values all look like constructed tags of some kind. To-
handle this we look for nested constructed items that should really
be primitive */
if( ( nestedItem.id & FORM_MASK ) == PRIMITIVE )
return( TRUE );
/* It's constructed, make sure that it's something for which it makes
sense as a constructed object. At worst this will give some false
negatives for really wierd objects (nested constructed strings inside
OCTET STRINGs), but these should probably never occur anyway */
if( nestedItem.tag == SEQUENCE || \
nestedItem.tag == SET )
return( TRUE );
return( FALSE );
}
static int checkEncapsulate( FILE *inFile, const int tag, const int length )
{
ASN1_ITEM nestedItem;
const int currentPos = fPos;
int diffPos;
/* If we're not looking for encapsulated objects, return */
if( !checkEncaps )
return( FALSE );
/* Read the details of the next item in the input stream */
getItem( inFile, &nestedItem );
diffPos = fPos - currentPos;
fPos = currentPos;
fseek( inFile, -diffPos, SEEK_CUR );
/* If it fits exactly within the current item and has a valid-looking
tag, treat it as nested data */
if( ( ( nestedItem.id & CLASS_MASK ) == UNIVERSAL || \
( nestedItem.id & CLASS_MASK ) == CONTEXT ) && \
( nestedItem.tag > 0 && nestedItem.tag <= 0x31 ) && \
nestedItem.length == length - diffPos )
return( TRUE );
return( FALSE );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment