📄 res_denv.c
字号:
for( contentListPtr = envelopeInfoPtr->contentList;
contentListPtr != NULL && contentListPtr->envInfo != envInfo;
contentListPtr = contentListPtr->next );
if( contentListPtr == NULL )
return( CRYPT_ARGERROR_VALUE );
}
/* Make sure that the information we're adding matches the currently
required information object. The one exception to this is that we
can be passed password information when we require a private key if
the private key is encrypted */
if( contentListPtr->envInfo != envInfo && \
!( contentListPtr->envInfo == CRYPT_ENVINFO_PRIVATEKEY && \
envInfo == CRYPT_ENVINFO_PASSWORD ) )
return( CRYPT_ARGERROR_VALUE );
/* If it's a signature object, check the signature and exit. Anything
left after this point is a keying object */
if( envInfo == CRYPT_ENVINFO_SIGNATURE )
{
CONTENT_SIG_INFO *sigInfo = &contentListPtr->clSigInfo;
/* If we've already processed this entry, return the saved processing
result */
if( contentListPtr->flags & CONTENTLIST_PROCESSED )
return( sigInfo->processingResult );
/* Find the hash action we need to check this signature */
for( actionListPtr = envelopeInfoPtr->actionList;
actionListPtr != NULL; actionListPtr = actionListPtr->next )
{
int cryptAlgo;
/* Check to see if it's the one we want */
if( cryptStatusOK( \
krnlSendMessage( actionListPtr->iCryptHandle,
IMESSAGE_GETATTRIBUTE, &cryptAlgo,
CRYPT_CTXINFO_ALGO ) ) && \
cryptAlgo == sigInfo->hashAlgo )
break;
}
/* If we can't find a hash action to match this signature, return a
bad signature error since something must have altered the
algorithm ID for the hash */
if( actionListPtr == NULL || actionListPtr->action != ACTION_HASH )
{
contentListPtr->flags |= CONTENTLIST_PROCESSED;
sigInfo->processingResult = CRYPT_ERROR_SIGNATURE;
return( CRYPT_ERROR_SIGNATURE );
}
/* Check the signature */
if( contentListPtr->formatType == CRYPT_FORMAT_CMS )
{
/* If it's CMS signed data then the sig.check key should be
included with the signed data as a cert chain, however it's
possible (though unlikely) that the certs may be unrelated to
the signature, in which case the caller will have provided
the sig.check key from an external source */
status = iCryptCheckSignatureEx( contentListPtr->object,
contentListPtr->objectSize, CRYPT_FORMAT_CMS,
( sigInfo->iSigCheckKey == CRYPT_ERROR ) ? \
cryptHandle : sigInfo->iSigCheckKey,
actionListPtr->iCryptHandle,
&sigInfo->iExtraData );
/* If there are authenticated attributes present we have to
perform an extra check here to make sure that the content-type
specified in the authenticated attributes matches the actual
data content type */
if( cryptStatusOK( status ) && \
sigInfo->iExtraData != CRYPT_ERROR )
{
int contentType;
status = krnlSendMessage( sigInfo->iExtraData,
IMESSAGE_GETATTRIBUTE, &contentType,
CRYPT_CERTINFO_CMS_CONTENTTYPE );
if( cryptStatusError( status ) || \
envelopeInfoPtr->contentType != contentType )
status = CRYPT_ERROR_SIGNATURE;
}
/* If there are unauthenticated attributes present, process
them. We don't record the processing status for these to
ensure that some random error in the non signature-related
attributes doesn't invalidate an otherwise OK signature */
if( cryptStatusOK( status ) && \
sigInfo->extraData2 != NULL )
processUnauthAttributes( contentListPtr, sigInfo->extraData2,
sigInfo->extraData2Length );
}
else
{
status = iCryptCheckSignatureEx( contentListPtr->object,
contentListPtr->objectSize,
contentListPtr->formatType, cryptHandle,
actionListPtr->iCryptHandle, NULL );
/* If it's a format that includes signing key info, remember the
key that was used to check the signature in case the user
wants to query it later */
if( contentListPtr->formatType != CRYPT_FORMAT_PGP )
{
krnlSendNotifier( cryptHandle, IMESSAGE_INCREFCOUNT );
sigInfo->iSigCheckKey = cryptHandle;
if( !valueLength )
contentListPtr->flags |= CONTENTLIST_EXTERNALKEY;
}
}
/* Remember the processing result so that we don't have to repeat the
processing if queried again. Since we don't need the encoded
signature data any more after this point, we free it to make the
memory available for reuse */
clFree( "addDeenvelopeInfo", contentListPtr->object );
contentListPtr->object = NULL;
contentListPtr->objectSize = 0;
contentListPtr->flags |= CONTENTLIST_PROCESSED;
sigInfo->processingResult = cryptArgError( status ) ? \
CRYPT_ERROR_SIGNATURE : status;
return( status );
}
/* If we need private key information and we've been given a password,
it's the password required to decrypt the key so we treat this
specially */
if( contentListPtr->envInfo == CRYPT_ENVINFO_PRIVATEKEY && \
envInfo == CRYPT_ENVINFO_PASSWORD )
{
MESSAGE_KEYMGMT_INFO getkeyInfo;
/* Make sure that there's a keyset available to pull the key from */
if( envelopeInfoPtr->iDecryptionKeyset == CRYPT_ERROR )
{
setErrorInfo( envelopeInfoPtr, CRYPT_ENVINFO_KEYSET_DECRYPT,
CRYPT_ERRTYPE_ATTR_ABSENT );
return( CRYPT_ERROR_NOTINITED );
}
/* Try and get the key information */
if( contentListPtr->issuerAndSerialNumber == NULL )
{
setMessageKeymgmtInfo( &getkeyInfo,
( contentListPtr->formatType == CRYPT_FORMAT_PGP ) ? \
CRYPT_IKEYID_PGPKEYID : CRYPT_IKEYID_KEYID,
contentListPtr->keyID, contentListPtr->keyIDsize,
( void * ) value, valueLength, KEYMGMT_FLAG_USAGE_CRYPT );
}
else
{
setMessageKeymgmtInfo( &getkeyInfo,
CRYPT_IKEYID_ISSUERANDSERIALNUMBER,
contentListPtr->issuerAndSerialNumber,
contentListPtr->issuerAndSerialNumberSize,
( void * ) value, valueLength, KEYMGMT_FLAG_USAGE_CRYPT );
}
status = krnlSendMessage( envelopeInfoPtr->iDecryptionKeyset,
IMESSAGE_KEY_GETKEY, &getkeyInfo,
KEYMGMT_ITEM_PRIVATEKEY );
/* If we managed to get the private key, push it into the envelope.
If the call succeeds, this will import the session key and delete
the required-information list */
if( status == CRYPT_OK )
{
status = addDeenvelopeInfo( envelopeInfoPtr,
CRYPT_ENVINFO_PRIVATEKEY,
&getkeyInfo.cryptHandle, 0 );
krnlSendNotifier( getkeyInfo.cryptHandle, IMESSAGE_DECREFCOUNT );
}
return( status );
}
/* If we've been given a password, create the appropriate encryption
context for it and derive the key from the password */
if( envInfo == CRYPT_ENVINFO_PASSWORD )
{
const CONTENT_ENCR_INFO *encrInfo = &contentListPtr->clEncrInfo;
MESSAGE_CREATEOBJECT_INFO createInfo;
/* Create the appropriate encryption context and derive the key into
it */
setMessageCreateObjectInfo( &createInfo, encrInfo->cryptAlgo );
status = krnlSendMessage( SYSTEM_OBJECT_HANDLE,
IMESSAGE_DEV_CREATEOBJECT, &createInfo,
OBJECT_TYPE_CONTEXT );
if( cryptStatusError( status ) )
return( status );
status = krnlSendMessage( createInfo.cryptHandle, IMESSAGE_SETATTRIBUTE,
( void * ) &encrInfo->cryptMode,
CRYPT_CTXINFO_MODE );
if( cryptStatusOK( status ) )
{
#ifdef USE_PGP
if( envelopeInfoPtr->type == CRYPT_FORMAT_PGP )
status = pgpPasswordToKey( createInfo.cryptHandle, CRYPT_UNUSED,
value, valueLength, encrInfo->keySetupAlgo,
encrInfo->saltOrIVsize > 0 ? \
encrInfo->saltOrIV : NULL,
encrInfo->keySetupIterations );
else
#endif /* USE_PGP */
{
RESOURCE_DATA msgData;
/* Load the derivation information into the context */
status = krnlSendMessage( createInfo.cryptHandle,
IMESSAGE_SETATTRIBUTE,
( void * ) &encrInfo->keySetupIterations,
CRYPT_CTXINFO_KEYING_ITERATIONS );
if( cryptStatusOK( status ) )
{
setMessageData( &msgData, ( void * ) encrInfo->saltOrIV,
encrInfo->saltOrIVsize );
status = krnlSendMessage( createInfo.cryptHandle,
IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_SALT );
}
if( cryptStatusOK( status ) )
{
setMessageData( &msgData, ( void * ) value, valueLength );
status = krnlSendMessage( createInfo.cryptHandle,
IMESSAGE_SETATTRIBUTE_S, &msgData,
CRYPT_CTXINFO_KEYING_VALUE );
}
}
}
if( cryptStatusError( status ) )
{
krnlSendNotifier( createInfo.cryptHandle, IMESSAGE_DECREFCOUNT );
return( status );
}
/* Recover the session key using the password context and destroy it
when we're done with it */
if( envelopeInfoPtr->type != CRYPT_FORMAT_PGP )
{
status = importSessionKey( envelopeInfoPtr, contentListPtr,
createInfo.cryptHandle, &iNewContext );
krnlSendNotifier( createInfo.cryptHandle, IMESSAGE_DECREFCOUNT );
if( cryptStatusError( status ) )
return( status );
}
else
/* In PGP there isn't any encrypted session key, so the context
created from the password becomes the bulk encryption
context */
iNewContext = createInfo.cryptHandle;
}
/* If we've been given a KEK (symmetric or asymmetric), recreate the
session key by importing it using the KEK */
if( envInfo == CRYPT_ENVINFO_PRIVATEKEY || \
envInfo == CRYPT_ENVINFO_KEY )
{
/* Import the session key using the KEK */
status = importSessionKey( envelopeInfoPtr, contentListPtr,
cryptHandle, &iNewContext );
if( cryptStatusError( status ) )
return( status );
}
/* At this point we have the session key, either by recovering it from a
key exchange action or by having it passed to us directly. If we've
been given it directly then we must have reached the encrypted data
so we take a copy and set up the decryption with it */
if( envInfo == CRYPT_ENVINFO_SESSIONKEY )
{
const CONTENT_ENCR_INFO *encrInfo = &contentListPtr->clEncrInfo;
status = initEnvelopeEncryption( envelopeInfoPtr, cryptHandle,
encrInfo->cryptAlgo, encrInfo->cryptMode,
encrInfo->saltOrIV, encrInfo->saltOrIVsize, TRUE );
if( cryptStatusError( status ) )
return( status );
/* The session key context is the newly-created internal one */
iNewContext = envelopeInfoPtr->iCryptContext;
}
else
{
/* We've recovered the session key from a key exchange action. If
we got as far as the encrypted data (indicated by the fact that
there's content info present), we set up the decryption. If we
didn't get this far, it'll be set up by the deenveloping code
when we reach it */
for( contentListPtr = envelopeInfoPtr->contentList;
contentListPtr != NULL && \
contentListPtr->envInfo != CRYPT_ENVINFO_SESSIONKEY;
contentListPtr = contentListPtr->next );
if( contentListPtr != NULL )
{
const CONTENT_ENCR_INFO *encrInfo = &contentListPtr->clEncrInfo;
/* We got to the encrypted data, set up the decryption */
status = initEnvelopeEncryption( envelopeInfoPtr, iNewContext,
encrInfo->cryptAlgo, encrInfo->cryptMode,
encrInfo->saltOrIV, encrInfo->saltOrIVsize, FALSE );
if( cryptStatusError( status ) )
return( status );
}
}
/* Add the recovered session encryption action to the action list */
actionResult = checkAction( envelopeInfoPtr->actionList, ACTION_CRYPT,
iNewContext );
if( actionResult == ACTION_RESULT_ERROR || \
actionResult == ACTION_RESULT_INITED )
return( CRYPT_ERROR_INITED );
if( addAction( &envelopeInfoPtr->actionList,
envelopeInfoPtr->memPoolState, ACTION_CRYPT,
iNewContext ) == NULL )
return( CRYPT_ERROR_MEMORY );
/* Notify the kernel that the session key context is attached to the
envelope. This is an internal object used only by the envelope so we
tell the kernel not to increment its reference count when it attaches
it */
krnlSendMessage( envelopeInfoPtr->objectHandle, IMESSAGE_SETDEPENDENT,
&iNewContext, SETDEP_OPTION_NOINCREF );
/* Destroy the content list, which at this point will contain only (now-
irrelevant) key exchange items */
deleteContentList( envelopeInfoPtr->memPoolState,
&envelopeInfoPtr->contentList );
envelopeInfoPtr->contentList = envelopeInfoPtr->contentListCurrent = NULL;
/* If the only error was an information required error, we've now
resolved the problem and can continue */
if( envelopeInfoPtr->errorState == CRYPT_ENVELOPE_RESOURCE )
envelopeInfoPtr->errorState = CRYPT_OK;
return( status );
}
/****************************************************************************
* *
* Envelope Access Routines *
* *
****************************************************************************/
void initDenvResourceHandling( ENVELOPE_INFO *envelopeInfoPtr )
{
assert( envelopeInfoPtr->flags & ENVELOPE_ISDEENVELOPE );
/* Set the access method pointers */
envelopeInfoPtr->addInfo = addDeenvelopeInfo;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -