📄 user_cfg.c
字号:
OPTION_INFO *optionInfoPtr;
assert( isWritePtr( configOptions,
sizeof( OPTION_INFO ) * \
( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) ) );
REQUIRES( option > CRYPT_OPTION_FIRST && option < CRYPT_OPTION_LAST );
REQUIRES( value >= 0 && value < MAX_INTLENGTH );
/* Get a pointer to the option information and make sure that everything
is OK */
optionInfoPtr = getOptionInfo( configOptions, option );
ENSURES( optionInfoPtr != NULL );
builtinOptionInfoPtr = optionInfoPtr->builtinOptionInfo;
ENSURES( builtinOptionInfoPtr != NULL && \
( builtinOptionInfoPtr->type == OPTION_NUMERIC || \
builtinOptionInfoPtr->type == OPTION_BOOLEAN ) );
/* If the stored value is the same as the new one, there's nothing to
do */
if( optionInfoPtr->intValue == value )
return( CRYPT_OK );
/* If we're forcing a commit by returning the configuration-changed flag
to its ground state, write any changed options to backing store */
if( option == CRYPT_OPTION_CONFIGCHANGED )
{
/* When a non-configuration option (for example a certificate trust
option) is changed then we need to write the updated
configuration data to backing store, but there's no way to tell
that this is required because the configuration options are
unchanged. To allow the caller to signal this change they can
explicitly set the configuration-changed setting to TRUE
(normally this is done implicitly by when another configuration
setting is changed). This explicit setting can only be done by
the higher-level configuration-update code because the kernel
blocks any attempts to set it to a value other than FALSE */
if( value )
{
optionInfoPtr->intValue = TRUE;
return( CRYPT_OK );
}
/* Make sure that there's something to write. We do this to avoid
problems with programs that always try to update the
configuration whether it's necessary or not, which can cause
problems with media with limited writeability */
if( !optionInfoPtr->intValue )
return( CRYPT_OK );
/* We don't do anything to write the configuration data at this
level since we currently have the user object locked and don't
want to stall all operations that depend on it while we're
updating the data, so all that we do is signal the user object
to perform the necessary operations */
return( OK_SPECIAL );
}
/* If we're forcing a self-test by changing the value of the self-test
status, perform an algorithm test */
if( option == CRYPT_OPTION_SELFTESTOK )
{
/* The self-test can take some time to complete. While it's running
we don't want to leave the user object locked since this will
block most other threads, which all eventually read some sort of
configuration option. To get around this problem we set the
result value to an undefined status and unlock the user object
around the call, then re-lock it and set its actual value via
setOptionSpecial() once the self-test is done */
if( optionInfoPtr->intValue == CRYPT_ERROR )
return( CRYPT_ERROR_TIMEOUT );
optionInfoPtr->intValue = CRYPT_ERROR;
return( OK_SPECIAL );
}
/* Set the value and remember that the configuration options have been
changed */
if( builtinOptionInfoPtr->type == OPTION_BOOLEAN )
{
/* Turn a generic zero/nonzero boolean into TRUE or FALSE */
optionInfoPtr->intValue = ( value ) ? TRUE : FALSE;
}
else
optionInfoPtr->intValue = value;
optionInfoPtr->dirty = TRUE;
setConfigChanged( configOptions );
return( CRYPT_OK );
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int setOptionSpecial( INOUT_ARRAY( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) \
TYPECAST( OPTION_INFO * ) void *configOptions,
IN_RANGE_FIXED( CRYPT_OPTION_SELFTESTOK ) \
const CRYPT_ATTRIBUTE_TYPE option,
IN_INT const int value )
{
OPTION_INFO *optionInfoPtr;
/* The update of the self-test status is performed in two phases. When
we begin the self-test, triggered by the user setting the
CRYPT_OPTION_SELFTESTOK, it's set to an undefined value via
setOption(). In other words the user can only ever set this to the
self-test-in-progress state. Once the self-test completes it's set
to the test result value via setOptionSpecial(), which can only be
accessed from inside the user object */
REQUIRES( option == CRYPT_OPTION_SELFTESTOK );
/* Get a pointer to the option information and make sure that everything
is OK */
optionInfoPtr = getOptionInfo( configOptions, option );
ENSURES( optionInfoPtr != NULL && \
optionInfoPtr->intValue == CRYPT_ERROR );
optionInfoPtr->intValue = value;
return( CRYPT_OK );
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
int setOptionString( INOUT_ARRAY( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) \
TYPECAST( OPTION_INFO * ) void *configOptions,
IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE option,
IN_BUFFER( valueLength ) const char *value,
IN_LENGTH_SHORT const int valueLength )
{
const BUILTIN_OPTION_INFO *builtinOptionInfoPtr;
OPTION_INFO *optionInfoPtr;
char *valuePtr;
assert( isWritePtr( configOptions,
sizeof( OPTION_INFO ) * \
( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) ) );
assert( isReadPtr( value, valueLength ) );
REQUIRES( option > CRYPT_OPTION_FIRST && option < CRYPT_OPTION_LAST );
REQUIRES( valueLength > 0 && valueLength < MAX_INTLENGTH_SHORT );
/* Get a pointer to the option information and make sure that everything
is OK */
optionInfoPtr = getOptionInfo( configOptions, option );
ENSURES( optionInfoPtr != NULL );
builtinOptionInfoPtr = optionInfoPtr->builtinOptionInfo;
ENSURES( builtinOptionInfoPtr != NULL && \
builtinOptionInfoPtr->type == OPTION_STRING );
/* If the value is the same as the current one, there's nothing to do */
if( optionInfoPtr->strValue != NULL && \
optionInfoPtr->intValue == valueLength && \
!memcmp( optionInfoPtr->strValue, value, valueLength ) )
return( CRYPT_OK );
/* If we're resetting a value to its default setting, just reset the
string pointers rather than storing the value */
if( builtinOptionInfoPtr->strDefault != NULL && \
builtinOptionInfoPtr->intDefault == valueLength && \
!memcmp( builtinOptionInfoPtr->strDefault, value, valueLength ) )
{
if( optionInfoPtr->strValue != NULL && \
optionInfoPtr->strValue != builtinOptionInfoPtr->strDefault )
{
zeroise( optionInfoPtr->strValue, optionInfoPtr->intValue );
clFree( "setOptionString", optionInfoPtr->strValue );
}
optionInfoPtr->strValue = ( char * ) builtinOptionInfoPtr->strDefault;
optionInfoPtr->dirty = TRUE;
setConfigChanged( configOptions );
return( CRYPT_OK );
}
/* Try and allocate room for the new option */
if( ( valuePtr = clAlloc( "setOptionString", valueLength ) ) == NULL )
return( CRYPT_ERROR_MEMORY );
memcpy( valuePtr, value, valueLength );
/* If the string value that's currently set isn't the default setting,
clear and free it */
if( optionInfoPtr->strValue != NULL && \
optionInfoPtr->strValue != builtinOptionInfoPtr->strDefault )
{
zeroise( optionInfoPtr->strValue, optionInfoPtr->intValue );
clFree( "setOptionString", optionInfoPtr->strValue );
}
/* Set the value and remember that the configuration options have been
changed */
optionInfoPtr->strValue = valuePtr;
optionInfoPtr->intValue = valueLength;
optionInfoPtr->dirty = TRUE;
setConfigChanged( configOptions );
return( CRYPT_OK );
}
/****************************************************************************
* *
* Misc.Configuration Options Routines *
* *
****************************************************************************/
/* Delete an option */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int deleteOption( INOUT_ARRAY( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) \
TYPECAST( OPTION_INFO * ) void *configOptions,
IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE option )
{
const BUILTIN_OPTION_INFO *builtinOptionInfoPtr;
OPTION_INFO *optionInfoPtr;
assert( isWritePtr( configOptions,
sizeof( OPTION_INFO ) * \
( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) ) );
REQUIRES( option > CRYPT_OPTION_FIRST && option < CRYPT_OPTION_LAST );
/* Get a pointer to the option information and make sure that everything
is OK */
optionInfoPtr = getOptionInfo( configOptions, option );
ENSURES( optionInfoPtr != NULL );
builtinOptionInfoPtr = optionInfoPtr->builtinOptionInfo;
ENSURES( builtinOptionInfoPtr != NULL && \
builtinOptionInfoPtr->type == OPTION_STRING );
/* If we're deleting an option it can only be a string option without a
built-in default to fall back on (enforced by the kernel). Since
these options don't have default values we check for a setting of
NULL rather than equivalence to a default string value */
ENSURES( builtinOptionInfoPtr->strDefault == NULL );
if( optionInfoPtr->strValue == NULL )
return( CRYPT_ERROR_NOTFOUND );
zeroise( optionInfoPtr->strValue, optionInfoPtr->intValue );
clFree( "setOptionString", optionInfoPtr->strValue );
optionInfoPtr->strValue = NULL;
optionInfoPtr->intValue = 0;
optionInfoPtr->dirty = TRUE;
setConfigChanged( configOptions );
return( CRYPT_OK );
}
/* Initialise/shut down the configuration option handling */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int initOptions( OUT_PTR void **configOptionsPtr )
{
OPTION_INFO *optionList;
int i;
/* Perform a consistency check on the options */
FORALL( i, 0, CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST - 1,
builtinOptionInfo[ i ].option == i + CRYPT_OPTION_FIRST + 1 );
/* Allocate storage for the variable configuration data */
if( ( optionList = clAlloc( "initOptions", OPTION_INFO_SIZE ) ) == NULL )
return( CRYPT_ERROR_MEMORY );
memset( optionList, 0, OPTION_INFO_SIZE );
/* Walk through the configuration option list setting up each option to
contain its default value */
for( i = 0; builtinOptionInfo[ i ].option != CRYPT_ATTRIBUTE_NONE && \
i < FAILSAFE_ARRAYSIZE( builtinOptionInfo, BUILTIN_OPTION_INFO );
i++ )
{
const BUILTIN_OPTION_INFO *builtinOptionInfoPtr = &builtinOptionInfo[ i ];
OPTION_INFO *optionInfoPtr = &optionList[ i ];
if( builtinOptionInfoPtr->type == OPTION_STRING )
optionInfoPtr->strValue = ( char * ) builtinOptionInfoPtr->strDefault;
optionInfoPtr->intValue = builtinOptionInfoPtr->intDefault;
optionInfoPtr->builtinOptionInfo = builtinOptionInfoPtr;
}
ENSURES( i < FAILSAFE_ARRAYSIZE( builtinOptionInfo, BUILTIN_OPTION_INFO ) );
*configOptionsPtr = optionList;
return( CRYPT_OK );
}
STDC_NONNULL_ARG( ( 1 ) ) \
void endOptions( INOUT_ARRAY( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) \
TYPECAST( OPTION_INFO * ) void *configOptions )
{
OPTION_INFO *optionList = configOptions;
int i;
assert( isReadPtr( configOptions,
sizeof( OPTION_INFO ) * \
( CRYPT_OPTION_LAST - CRYPT_OPTION_FIRST ) ) );
/* Walk through the configuration option list clearing and freeing each
option */
for( i = 0; builtinOptionInfo[ i ].option != CRYPT_ATTRIBUTE_NONE && \
i < FAILSAFE_ARRAYSIZE( builtinOptionInfo, BUILTIN_OPTION_INFO );
i++ )
{
const BUILTIN_OPTION_INFO *builtinOptionInfoPtr = &builtinOptionInfo[ i ];
OPTION_INFO *optionInfoPtr = &optionList[ i ];
if( builtinOptionInfoPtr->type == OPTION_STRING )
{
/* If the string value that's currently set isn't the default
setting, clear and free it */
if( optionInfoPtr->strValue != builtinOptionInfoPtr->strDefault )
{
zeroise( optionInfoPtr->strValue, optionInfoPtr->intValue );
clFree( "endOptions", optionInfoPtr->strValue );
}
}
}
ENSURES_V( i < FAILSAFE_ARRAYSIZE( builtinOptionInfo, BUILTIN_OPTION_INFO ) );
/* Clear and free the configuration option list */
memset( optionList, 0, OPTION_INFO_SIZE );
clFree( "endOptions", optionList );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -