📄 res_env.c
字号:
if( actionListPtr->action != ACTION_SIGN )
return( FALSE );
return( TRUE );
}
/* A standalone action can be either a single crypt, one or more hashes,
or nothing */
if( envelopeInfoPtr->actionList == NULL )
return( TRUE );
if( envelopeInfoPtr->actionList->action == ACTION_CRYPT )
return( envelopeInfoPtr->actionList->next == NULL ? TRUE : FALSE );
for( actionListPtr = envelopeInfoPtr->preActionList;
actionListPtr != NULL; actionListPtr = actionListPtr->next )
if( actionListPtr->action != ACTION_HASH )
return( FALSE );
return( TRUE );
}
#endif /* !NDEBUG */
/****************************************************************************
* *
* Misc.Enveloping Info Management Functions *
* *
****************************************************************************/
/* Set up the encryption for an envelope */
int initEnvelopeEncryption( ENVELOPE_INFO *envelopeInfoPtr,
const CRYPT_CONTEXT cryptContext,
const CRYPT_ALGO_TYPE algorithm,
const CRYPT_MODE_TYPE mode,
const BYTE *iv, const int ivLength,
const BOOLEAN copyContext )
{
CRYPT_CONTEXT iCryptContext = cryptContext;
CRYPT_ALGO_TYPE cryptAlgo;
CRYPT_MODE_TYPE cryptMode;
RESOURCE_DATA msgData;
int blockSize, status;
/* Extract the information we need to process data */
status = krnlSendMessage( cryptContext, IMESSAGE_GETATTRIBUTE,
&cryptAlgo, CRYPT_CTXINFO_ALGO );
if( cryptStatusOK( status ) )
status = krnlSendMessage( cryptContext, IMESSAGE_GETATTRIBUTE,
&cryptMode, CRYPT_CTXINFO_MODE );
if( cryptStatusOK( status ) )
status = krnlSendMessage( cryptContext, IMESSAGE_GETATTRIBUTE,
&blockSize, CRYPT_CTXINFO_BLOCKSIZE );
if( cryptStatusError( status ) )
return( status );
/* Make sure that the context is what's required */
if( algorithm != CRYPT_ALGO_NONE && \
( cryptAlgo != algorithm || cryptMode != mode ) )
/* This can only happen on deenveloping if the data is corrupted or
if the user is asked for a KEK and tries to supply a session key
instead */
return( CRYPT_ERROR_WRONGKEY );
/* If it's a user-supplied context, take a copy for our own use. This is
only done for non-idempotent user-supplied contexts, for everything
else we either use cryptlib's object management to handle things for
us or the context is a internal one created specifically for our own
use */
if( copyContext )
{
MESSAGE_CREATEOBJECT_INFO createInfo;
setMessageCreateObjectInfo( &createInfo, cryptAlgo );
status = krnlSendMessage( SYSTEM_OBJECT_HANDLE,
IMESSAGE_DEV_CREATEOBJECT, &createInfo,
OBJECT_TYPE_CONTEXT );
if( cryptStatusError( status ) )
return( status );
status = krnlSendMessage( iCryptContext, IMESSAGE_CLONE, NULL,
createInfo.cryptHandle );
if( cryptStatusError( status ) )
{
krnlSendNotifier( createInfo.cryptHandle, IMESSAGE_DECREFCOUNT );
return( status );
}
iCryptContext = createInfo.cryptHandle;
}
/* Load the IV into the context and set up the encryption information for
the envelope */
if( !isStreamCipher( cryptAlgo ) )
{
if( iv != NULL )
{
int ivSize;
status = krnlSendMessage( cryptContext, IMESSAGE_GETATTRIBUTE,
&ivSize, CRYPT_CTXINFO_IVSIZE );
setMessageData( &msgData, ( void * ) iv, min( ivLength, ivSize ) );
if( cryptStatusOK( status ) )
status = krnlSendMessage( iCryptContext, IMESSAGE_SETATTRIBUTE_S,
&msgData, CRYPT_CTXINFO_IV );
}
else
/* There's no IV specified, generate a new one */
status = krnlSendNotifier( iCryptContext, IMESSAGE_CTX_GENIV );
if( cryptStatusError( status ) )
{
if( copyContext )
/* Destroy the copy we created earlier */
krnlSendNotifier( iCryptContext, IMESSAGE_DECREFCOUNT );
return( status );
}
}
envelopeInfoPtr->iCryptContext = iCryptContext;
envelopeInfoPtr->blockSize = blockSize;
envelopeInfoPtr->blockSizeMask = ~( blockSize - 1 );
return( CRYPT_OK );
}
/* Add keyset information */
int addKeyset( ENVELOPE_INFO *envelopeInfoPtr,
const CRYPT_ATTRIBUTE_TYPE keysetFunction,
const CRYPT_KEYSET keyset )
{
CRYPT_KEYSET *iKeysetPtr;
/* Figure out which keyset we want to set */
switch( keysetFunction )
{
case CRYPT_ENVINFO_KEYSET_ENCRYPT:
iKeysetPtr = &envelopeInfoPtr->iEncryptionKeyset;
break;
case CRYPT_ENVINFO_KEYSET_DECRYPT:
iKeysetPtr = &envelopeInfoPtr->iDecryptionKeyset;
break;
case CRYPT_ENVINFO_KEYSET_SIGCHECK:
iKeysetPtr = &envelopeInfoPtr->iSigCheckKeyset;
break;
default:
assert( NOTREACHED );
return( CRYPT_ERROR_NOTAVAIL );
}
/* Make sure that the keyset hasn't already been set */
if( *iKeysetPtr != CRYPT_ERROR )
return( CRYPT_ERROR_INITED );
/* Remember the new keyset and increment its reference count */
*iKeysetPtr = keyset;
return( krnlSendNotifier( keyset, IMESSAGE_INCREFCOUNT ) );
}
/****************************************************************************
* *
* Enveloping Information Management Functions *
* *
****************************************************************************/
#ifdef USE_FORTEZZA
/* Check that an object being added is suitable for use with Fortezza data */
static int checkFortezzaUsage( const CRYPT_HANDLE cryptHandle,
const ENVELOPE_INFO *envelopeInfoPtr,
const CRYPT_ATTRIBUTE_TYPE envInfo )
{
CRYPT_ALGO_TYPE cryptAlgo;
int device1, device2, status;
/* Make sure that the new session key being added (if there's existing
originator info) or the existing one (if it's originator info being
added) is a Skipjack context */
status = krnlSendMessage( ( envInfo == CRYPT_ENVINFO_ORIGINATOR ) ? \
envelopeInfoPtr->iCryptContext : cryptHandle,
IMESSAGE_GETATTRIBUTE, &cryptAlgo,
CRYPT_CTXINFO_ALGO );
if( cryptStatusError( status ) || cryptAlgo != CRYPT_ALGO_SKIPJACK )
return( CRYPT_ARGERROR_NUM1 );
/* Make sure that both objects are present in the same device */
status = krnlSendMessage( cryptHandle, IMESSAGE_GETDEPENDENT, &device1,
OBJECT_TYPE_DEVICE );
if( cryptStatusOK( status ) )
status = krnlSendMessage( envelopeInfoPtr->iCryptContext,
IMESSAGE_GETDEPENDENT, &device2,
OBJECT_TYPE_DEVICE );
if( cryptStatusOK( status ) && ( device1 != device2 ) )
status = CRYPT_ARGERROR_NUM1;
return( status );
}
#endif /* USE_FORTEZZA */
/* Add enveloping information to an envelope */
static int addEnvelopeInfo( ENVELOPE_INFO *envelopeInfoPtr,
const CRYPT_ATTRIBUTE_TYPE envInfo,
const void *value, const int valueLength )
{
CRYPT_HANDLE cryptHandle = *( CRYPT_HANDLE * ) value;
ACTION_LIST *actionListPtr, **actionListHeadPtrPtr, *hashActionPtr;
ACTION_RESULT actionResult;
ACTION_TYPE actionType;
int algorithm, status;
/* If it's meta-information, remember the value */
if( envInfo == CRYPT_ENVINFO_DATASIZE )
{
envelopeInfoPtr->payloadSize = *( int * ) value;
return( CRYPT_OK );
}
if( envInfo == CRYPT_ENVINFO_CONTENTTYPE )
{
envelopeInfoPtr->contentType = *( int * ) value;
return( CRYPT_OK );
}
if( envInfo == CRYPT_ENVINFO_DETACHEDSIGNATURE || \
envInfo == CRYPT_ENVINFO_MAC )
{
/* Turn a generic zero/nonzero boolean into TRUE or FALSE */
const BOOLEAN flag = ( *( int * ) value ) ? TRUE : FALSE;
if( envInfo == CRYPT_ENVINFO_DETACHEDSIGNATURE )
{
if( flag )
{
if( envelopeInfoPtr->flags & ENVELOPE_ATTRONLY )
/* Detached-sig and attribute-only messages are mutually
exclusive */
return( CRYPT_ERROR_INITED );
envelopeInfoPtr->flags |= ENVELOPE_DETACHED_SIG;
}
else
envelopeInfoPtr->flags &= ~ENVELOPE_DETACHED_SIG;
}
else
{
/* The MAC flag is somewhat different from the detached-signature
one in that the latter is a modifier for an existing envelope
usage while the former changes the usage itself. Because of
this it can only be set to TRUE (if it could be reset the
caller could set non-MAC-compatible options by clearing the
flag and then setting it again afterwards), since the envelope
usage change occurs at a higher level all we do here is make
sure that the flag isn't being cleared */
if( !flag )
return( CRYPT_ARGERROR_NUM1 );
/* There are no known implementations of this content-type, so
for now we disallow any attempts to use it */
return( CRYPT_ERROR_NOTAVAIL );
}
return( CRYPT_OK );
}
if( envInfo == CRYPT_IATTRIBUTE_INCLUDESIGCERT )
{
/* This is on by default so we should only be turning it off */
assert( ( *( int * ) value ) == FALSE );
envelopeInfoPtr->flags |= ENVELOPE_NOSIGNINGCERTS;
return( CRYPT_OK );
}
if( envInfo == CRYPT_IATTRIBUTE_ATTRONLY )
{
/* This is off by default so we should only be turning it on */
assert( ( *( int * ) value ) == TRUE );
if( envelopeInfoPtr->flags & ENVELOPE_DETACHED_SIG )
/* Detached-sig and attribute-only messages are mutually
exclusive */
return( CRYPT_ERROR_INITED );
envelopeInfoPtr->flags |= ENVELOPE_ATTRONLY;
return( CRYPT_OK );
}
/* If it's keyset information, just keep a record of it for later use */
if( envInfo == CRYPT_ENVINFO_KEYSET_SIGCHECK || \
envInfo == CRYPT_ENVINFO_KEYSET_ENCRYPT || \
envInfo == CRYPT_ENVINFO_KEYSET_DECRYPT )
return( addKeyset( envelopeInfoPtr, envInfo, cryptHandle ) );
/* If it's an extra action for the signature, record it with the main
signature action */
if( envInfo == CRYPT_ENVINFO_SIGNATURE_EXTRADATA || \
envInfo == CRYPT_ENVINFO_TIMESTAMP )
{
CRYPT_HANDLE *iCryptHandlePtr;
/* Find the last signature action that was added and make sure
that it doesn't already have an action of this type attached to
it */
actionListPtr = envelopeInfoPtr->postActionList;
if( actionListPtr == NULL )
return( CRYPT_ERROR_NOTINITED );
while( actionListPtr->next != NULL && \
actionListPtr->next->action == ACTION_SIGN )
actionListPtr = actionListPtr->next;
iCryptHandlePtr = ( envInfo == CRYPT_ENVINFO_SIGNATURE_EXTRADATA ) ? \
&actionListPtr->iExtraData : \
&actionListPtr->iTspSession;
if( *iCryptHandlePtr != CRYPT_ERROR )
return( CRYPT_ERROR_INITED );
/* Increment its reference count and add it to the action */
krnlSendNotifier( cryptHandle, IMESSAGE_INCREFCOUNT );
*iCryptHandlePtr = cryptHandle;
return( CRYPT_OK );
}
/* If it's originator information, record it for the enveloped data
header */
if( envInfo == CRYPT_ENVINFO_ORIGINATOR )
{
#ifdef USE_FORTEZZA
/* If there's a session key present, make sure that it's consistent
with the originator info */
if( envelopeInfoPtr->iCryptContext != CRYPT_ERROR )
{
status = checkFortezzaUsage( cryptHandle, envelopeInfoPtr,
CRYPT_ENVINFO_ORIGINATOR );
if( cryptStatusError( status ) )
return( status );
}
/* Increment its reference count and add it to the action */
krnlSendNotifier( cryptHandle, IMESSAGE_INCREFCOUNT );
envelopeInfoPtr->iExtraCertChain = cryptHandle;
/* Since we're using Fortezza key management, we have to use Skipjack
as the data encryption algorithm */
envelopeInfoPtr->defaultAlgo = CRYPT_ALGO_SKIPJACK;
return( CRYPT_OK );
#else
return( CRYPT_ARGERROR_NUM1 );
#endif /* USE_FORTEZZA */
}
/* If it's compression information, set up the compression structures */
if( envInfo == CRYPT_ENVINFO_COMPRESSION )
{
#ifdef USE_COMPRESSION
assert( !( envelopeInfoPtr->flags & ENVELOPE_ZSTREAMINITED ) );
/* Initialize the compression */
if( deflateInit( &envelopeInfoPtr->zStream, \
Z_DEFAULT_COMPRESSION ) != Z_OK )
return( CRYPT_ERROR_MEMORY );
envelopeInfoPtr->flags |= ENVELOPE_ZSTREAMINITED;
return( CRYPT_OK );
#else
return( CRYPT_ARGERROR_NUM1 );
#endif /* USE_COMPRESSION */
}
/* If it's a password, derive a session key encryption context from it */
if( envInfo == CRYPT_ENVINFO_PASSWORD )
{
MESSAGE_CREATEOBJECT_INFO createInfo;
CRYPT_ALGO_TYPE cryptAlgo = envelopeInfoPtr->defaultAlgo;
/* PGP doesn't support both PKC and conventional key exchange
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -