📄 ext.c
字号:
/****************************************************************************
* *
* Certificate Attribute Management Routines *
* Copyright Peter Gutmann 1996-2004 *
* *
****************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#if defined( INC_ALL )
#include "cert.h"
#include "certattr.h"
#include "asn1.h"
#elif defined( INC_CHILD )
#include "cert.h"
#include "certattr.h"
#include "../misc/asn1.h"
#else
#include "cert/cert.h"
#include "cert/certattr.h"
#include "misc/asn1.h"
#endif /* Compiler-specific includes */
/* Prototypes for functions in certdn.c */
int convertEmail( CERT_INFO *certInfoPtr, void **dnListHead,
const CRYPT_ATTRIBUTE_TYPE altNameType );
/****************************************************************************
* *
* Utility Functions *
* *
****************************************************************************/
/* Callback function used to provide external access to attribute list-
internal fields */
static const void *getAttrFunction( const void *attributePtr,
CRYPT_ATTRIBUTE_TYPE *groupID,
CRYPT_ATTRIBUTE_TYPE *attributeID,
CRYPT_ATTRIBUTE_TYPE *instanceID,
const ATTR_TYPE attrGetType )
{
const ATTRIBUTE_LIST *attributeListPtr = attributePtr;
assert( attributeListPtr == NULL || \
isReadPtr( attributeListPtr, sizeof( ATTRIBUTE_LIST ) ) );
/* Clear return values */
if( groupID != NULL )
*groupID = CRYPT_ATTRIBUTE_NONE;
if( attributeID != NULL )
*attributeID = CRYPT_ATTRIBUTE_NONE;
if( instanceID != NULL )
*instanceID = CRYPT_ATTRIBUTE_NONE;
/* Move to the next or previous attribute if required */
if( !isValidAttributeField( attributeListPtr ) )
return( NULL );
if( attrGetType == ATTR_PREV )
attributeListPtr = attributeListPtr->prev;
else
if( attrGetType == ATTR_NEXT )
attributeListPtr = attributeListPtr->next;
if( !isValidAttributeField( attributeListPtr ) )
return( NULL );
/* Return ID information to the caller */
if( groupID != NULL )
*groupID = attributeListPtr->attributeID;
if( attributeID != NULL )
*attributeID = attributeListPtr->fieldID;
if( instanceID != NULL )
*instanceID = attributeListPtr->subFieldID;
return( attributeListPtr );
}
/****************************************************************************
* *
* Attribute Type Mapping *
* *
****************************************************************************/
/* Get the attribute information for a given OID */
const ATTRIBUTE_INFO *oidToAttribute( const ATTRIBUTE_TYPE attributeType,
const BYTE *oid )
{
const ATTRIBUTE_INFO *attributeInfoPtr;
const int length = sizeofOID( oid );
assert( isReadPtr( selectAttributeInfo( attributeType ),
sizeof( ATTRIBUTE_INFO ) ) );
assert( isReadPtr( oid, sizeofOID( oid ) ) );
for( attributeInfoPtr = selectAttributeInfo( attributeType );
attributeInfoPtr->fieldID != CRYPT_ERROR;
attributeInfoPtr++ )
{
assert( isReadPtr( attributeInfoPtr, sizeof( ATTRIBUTE_INFO ) ) );
if( attributeInfoPtr->oid != NULL && \
sizeofOID( attributeInfoPtr->oid ) == length && \
!memcmp( attributeInfoPtr->oid, oid, length ) )
return( attributeInfoPtr );
}
/* It's an unknown attribute */
return( NULL );
}
/* Get the attribute and attributeID for a field ID */
const ATTRIBUTE_INFO *fieldIDToAttribute( const ATTRIBUTE_TYPE attributeType,
const CRYPT_ATTRIBUTE_TYPE fieldID,
const CRYPT_ATTRIBUTE_TYPE subFieldID,
CRYPT_ATTRIBUTE_TYPE *attributeID )
{
const ATTRIBUTE_INFO *attributeInfoPtr = \
selectAttributeInfo( attributeType );
int i;
assert( isReadPtr( attributeInfoPtr, sizeof( ATTRIBUTE_INFO ) ) );
assert( fieldID >= CRYPT_CERTINFO_FIRST_EXTENSION && \
fieldID <= CRYPT_CERTINFO_LAST );
assert( attributeID == NULL || \
isWritePtr( attributeID, sizeof( CRYPT_ATTRIBUTE_TYPE ) ) );
/* Clear the return value */
if( attributeID != NULL )
*attributeID = CRYPT_ATTRIBUTE_NONE;
/* Find the information on this attribute field */
for( i = 0; attributeInfoPtr[ i ].fieldID != CRYPT_ERROR; i++ )
{
assert( isReadPtr( attributeInfoPtr, sizeof( ATTRIBUTE_INFO ) ) );
/* If we're looking for an attribute ID and the previous entry
doesn't have more data following it, the current entry is the
start of a complete attribute and therefore contains the
attribute ID */
if( attributeID != NULL && \
( i == 0 || !( attributeInfoPtr[ i - 1 ].flags & FL_MORE ) ) )
{
int offset;
/* Usually the attribute ID is the fieldID for the first entry,
however in some cases the attributeID is the same as the
fieldID and isn't specified until later on. For example when
the attribute consists of a SEQUENCE OF field the first
entry is the SEQUENCE and the fieldID isn't given until the
second entry. This case is denoted by the fieldID being
FIELDID_FOLLOWS, if this happens we have to look ahead to
find the fieldID */
for( offset = 0;
attributeInfoPtr[ i + offset ].fieldID == FIELDID_FOLLOWS;
offset++ );
*attributeID = attributeInfoPtr[ i + offset ].fieldID;
}
/* Check whether the field ID for this entry matches the one that we
want */
if( attributeInfoPtr[ i ].fieldID == fieldID )
{
const ATTRIBUTE_INFO *altEncodingTable = \
attributeInfoPtr[ i ].extraData;
/* If we're after a subfield match as well, try and match the
subfield */
if( subFieldID != CRYPT_ATTRIBUTE_NONE && altEncodingTable != NULL )
{
for( i = 0; altEncodingTable[ i ].fieldID != CRYPT_ERROR; i++ )
if( altEncodingTable[ i ].fieldID == subFieldID )
return( &altEncodingTable[ i ] );
assert( NOTREACHED );
return( NULL );
}
return( &attributeInfoPtr[ i ] );
}
}
assert( NOTREACHED );
return( NULL );
}
/****************************************************************************
* *
* Attribute Location/Cursor Movement Routines *
* *
****************************************************************************/
/* Find the start of an attribute from a field within the attribute */
ATTRIBUTE_LIST *findAttributeStart( const ATTRIBUTE_LIST *attributeListPtr )
{
assert( attributeListPtr == NULL || \
isReadPtr( attributeListPtr, sizeof( ATTRIBUTE_LIST ) ) );
return( attributeFindStart( attributeListPtr, getAttrFunction ) );
}
/* Find an attribute in a list of certificate attributes by object identifier
(for blob-type attributes) or by field and subfield ID (for known
attributes), with extended handling for fields with default values */
ATTRIBUTE_LIST *findAttributeByOID( const ATTRIBUTE_LIST *attributeListPtr,
const BYTE *oid )
{
const int length = sizeofOID( oid );
assert( isReadPtr( attributeListPtr, sizeof( ATTRIBUTE_LIST ) ) );
assert( isReadPtr( oid, sizeofOID( oid ) ) );
/* Find the position of this component in the list */
while( attributeListPtr != NULL && \
( !isBlobAttribute( attributeListPtr ) || \
sizeofOID( attributeListPtr->oid ) != length || \
memcmp( attributeListPtr->oid, oid, length ) ) )
attributeListPtr = attributeListPtr->next;
return( ( ATTRIBUTE_LIST * ) attributeListPtr );
}
ATTRIBUTE_LIST *findAttributeField( const ATTRIBUTE_LIST *attributeListPtr,
const CRYPT_ATTRIBUTE_TYPE fieldID,
const CRYPT_ATTRIBUTE_TYPE subFieldID )
{
assert( attributeListPtr == NULL || \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -