44 { NULL, 0 }, { NULL, 0 }
60 { NULL, 0 }, { NULL, 0 }
84 if( envelopeInfoPtr->buffer == NULL || \
85 envelopeInfoPtr->
bufPos < 0 || \
128 memcpy( authEncInfo->authEncParamData, queryInfo->authEncParamData,
131 authEncInfo->encParamData = authEncInfo->authEncParamData +
\
134 authEncInfo->macParamData = authEncInfo->authEncParamData +
\
149 memcpy( encrInfo->saltOrIV, queryInfo->iv, queryInfo->
ivLength );
157 static
int initPkcContentInfo(
CONTENT_LIST *contentListItem,
166 assert(
isReadPtr(
object, objectSize ) );
177 contentListItem->clSigInfo.hashAlgo = queryInfo->
hashAlgo;
183 contentListItem->issuerAndSerialNumber = objectPtr + queryInfo->
iAndSStart;
190 memcpy( contentListItem->keyID, queryInfo->keyID,
196 contentListItem->payload = objectPtr + queryInfo->
dataStart;
215 static
int initEncKeyContentInfo(
CONTENT_LIST *contentListItem,
217 IN_BUFFER( objectSize )
const void *
object,
221 const BYTE *objectPtr = object;
225 assert(
isReadPtr(
object, objectSize ) );
242 memcpy( encrInfo->saltOrIV, queryInfo->salt,
253 contentListItem->payload = objectPtr + queryInfo->
dataStart;
272 void *contentListObjectPtr = NULL;
274 const BOOLEAN infoProvidedExternally = \
275 ( externalQueryInfo != NULL ) ?
TRUE :
FALSE;
276 int objectSize = 0,
status;
279 assert( ( stream == NULL && \
282 externalQueryInfo == NULL ) );
283 assert(
isWritePtr( itemSize,
sizeof(
int ) ) );
285 REQUIRES( ( stream == NULL && externalQueryInfo != NULL ) || \
286 ( stream != NULL && externalQueryInfo == NULL ) );
297 if( !infoProvidedExternally )
302 status = queryAsn1Object( stream, &queryInfo );
305 objectSize = ( int ) queryInfo.
size;
317 status = sSkip( stream, objectSize );
326 if( ( contentListObjectPtr =
clAlloc(
"addContentListItem", \
327 objectSize ) ) == NULL )
329 status = sread( stream, contentListObjectPtr, objectSize );
332 clFree(
"addContentListItem", contentListObjectPtr );
339 memcpy( &queryInfo, externalQueryInfo,
sizeof(
QUERY_INFO ) );
341 ENSURES( infoProvidedExternally || \
342 ( queryInfo.
size > 0 && \
360 status = createContentListItem( &contentListItem,
361 envelopeInfoPtr->memPoolState, contentType,
366 if( contentListObjectPtr != NULL )
367 clFree(
"addContentListItem", contentListObjectPtr );
370 if( infoProvidedExternally )
375 status = initExternalContentInfo( contentListItem, contentType,
387 status = initPkcContentInfo( contentListItem, &queryInfo,
388 contentListObjectPtr, objectSize );
396 status = initEncKeyContentInfo( contentListItem, &queryInfo,
397 contentListObjectPtr, objectSize );
402 status = appendContentListItem( envelopeInfoPtr, contentListItem );
405 deleteContentList( envelopeInfoPtr->memPoolState,
407 if( contentListObjectPtr != NULL )
408 clFree(
"addContentListItem", contentListObjectPtr );
411 *itemSize = ( int ) queryInfo.
size;
439 status = readCMSheader( stream, envelopeOIDinfo,
441 &envelopeInfoPtr->payloadSize,
487 status = readGenericAlgoID( stream,
OID_ZLIB,
492 #ifdef USE_COMPRESSION
527 status = readCMSencrHeader( stream, nestedContentOIDinfo,
534 envelopeInfoPtr->contentType =
status;
535 envelopeInfoPtr->payloadSize = queryInfo.
size;
540 if( envelopeInfoPtr->actionList == NULL )
547 return( addContentListItem( envelopeInfoPtr, NULL, &queryInfo,
550 REQUIRES( envelopeInfoPtr->actionList != NULL && \
561 return( initEnvelopeEncryption( envelopeInfoPtr,
562 envelopeInfoPtr->actionList->iCryptHandle,
582 status = readContextAlgoID( stream, &iHashContext, NULL,
DEFAULT_TAG,
593 for( actionListPtr = envelopeInfoPtr->actionList, iterationCount = 0;
595 actionListPtr = actionListPtr->
next, iterationCount++ )
614 status = addAction( &envelopeInfoPtr->actionList,
615 envelopeInfoPtr->memPoolState,
622 ENSURES( envelopeInfoPtr->actionList != NULL && \
623 ( envelopeInfoPtr->actionList->action ==
ACTION_HASH || \
624 envelopeInfoPtr->actionList->action ==
ACTION_MAC ) );
650 if( ( status = checkEOC( stream ) ) !=
TRUE || \
651 ( status = checkEOC( stream ) ) !=
TRUE )
663 if( ( status = checkEOC( stream ) ) ==
TRUE )
664 status = checkEOC( stream );
686 if( envelopeInfoPtr->dataLeft > 0 )
690 status = envelopeInfoPtr->processExtraData( envelopeInfoPtr,
691 envelopeInfoPtr->buffer,
692 envelopeInfoPtr->dataLeft );
698 return( envelopeInfoPtr->processExtraData( envelopeInfoPtr,
"", 0 ) );
716 status = processPayloadEOCs( envelopeInfoPtr, stream );
721 tag = peekTag( stream );
730 status = completePayloadProcessing( envelopeInfoPtr );
763 status = processPayloadEOCs( envelopeInfoPtr, stream );
775 status = completePayloadProcessing( envelopeInfoPtr );
780 ENSURES( actionListPtr != NULL );
805 static
int processEOCTrailer(
const ENVELOPE_INFO *envelopeInfoPtr,
824 switch( envelopeInfoPtr->
usage )
853 for( i = 0; i < noEOCs; i++ )
855 const int value = checkEOC( stream );
888 int remainder, streamPos = 0, iterationCount, status =
CRYPT_OK;
892 REQUIRES( sanityCheck( envelopeInfoPtr ) );
894 sMemConnect( &stream, envelopeInfoPtr->buffer, envelopeInfoPtr->
bufPos );
900 status = processEnvelopeHeader( envelopeInfoPtr, &stream, &state );
903 sMemDisconnect( &stream );
906 "Invalid CMS envelope header" ) );
910 streamPos = stell( &stream );
920 for( iterationCount = 0;
935 status = readLongSet( &stream, &setLongLength );
939 "Invalid SET OF RecipientInfo header", 35 );
942 envelopeInfoPtr->hdrSetLength = setLongLength;
948 streamPos = stell( &stream );
956 status = readSetI( &stream, &setLength );
960 "Invalid SET OF DigestAlgorithmIdentifier "
964 envelopeInfoPtr->hdrSetLength = setLength;
970 streamPos = stell( &stream );
978 int contentItemLength;
981 status = addContentListItem( envelopeInfoPtr, &stream, NULL,
982 &contentItemLength );
986 "Invalid EncryptionKeyInfo key exchange "
993 streamPos = stell( &stream );
996 if( contentItemLength > envelopeInfoPtr->hdrSetLength )
1001 envelopeInfoPtr->hdrSetLength -= contentItemLength;
1002 if( envelopeInfoPtr->hdrSetLength <= 0 )
1004 state = ( envelopeInfoPtr->usage ==
ACTION_MAC ) ? \
1010 const int value = checkEOC( &stream );
1018 state = ( envelopeInfoPtr->usage ==
ACTION_MAC ) ? \
1027 status = processEncryptionHeader( envelopeInfoPtr, &stream );
1036 "Invalid encrypted content header", 32 );
1042 streamPos = stell( &stream );
1044 if( envelopeInfoPtr->actionList == NULL )
1058 status = processHashHeader( envelopeInfoPtr, &stream );
1062 "Invalid MACd content header", 27 );
1067 streamPos = stell( &stream );
1075 status = processHashHeader( envelopeInfoPtr, &stream );
1079 "Invalid hashed content header", 29 );
1087 const int hashInfoLength = stell( &stream ) - streamPos;
1088 if( hashInfoLength < 0 || \
1089 hashInfoLength > envelopeInfoPtr->hdrSetLength )
1094 envelopeInfoPtr->hdrSetLength -= hashInfoLength;
1095 streamPos = stell( &stream );
1096 if( envelopeInfoPtr->hdrSetLength <= 0 )
1101 const int value = checkEOC( &stream );
1117 status = contentType = \
1118 readCMSheader( &stream, nestedContentOIDinfo,
1120 &envelopeInfoPtr->payloadSize,
1125 "Invalid encapsulated content header", 35 );
1128 envelopeInfoPtr->contentType = contentType;
1133 if( envelopeInfoPtr->payloadSize == 0 && \
1138 streamPos = stell( &stream );
1139 state = ( envelopeInfoPtr->payloadSize == 0 && \
1141 ENVELOPE_ATTRONLY ) ) ) ? \
1148 if( envelopeInfoPtr->actionList == NULL )
1171 status = envelopeInfoPtr->syncDeenvelopeData( envelopeInfoPtr,
1176 "Couldn't synchronise envelope state prior "
1177 "to data payload processing", 68 );
1186 ENSURES( checkActions( envelopeInfoPtr ) );
1189 sMemDisconnect( &stream );
1190 if( iterationCount >= FAILSAFE_ITERATIONS_LARGE )
1196 envelopeInfoPtr->deenvState = state;
1199 envelopeInfoPtr->bufPos - streamPos >= 0 );
1203 remainder = envelopeInfoPtr->bufPos - streamPos;
1205 streamPos + remainder <= envelopeInfoPtr->
bufSize );
1206 if( remainder > 0 && streamPos > 0 )
1209 envelopeInfoPtr->bufSize ) );
1210 memmove( envelopeInfoPtr->buffer, envelopeInfoPtr->buffer + streamPos,
1213 envelopeInfoPtr->bufPos = remainder;
1214 ENSURES( sanityCheck( envelopeInfoPtr ) );
1231 int remainder, streamPos = 0, iterationCount, status =
CRYPT_OK;
1235 REQUIRES( sanityCheck( envelopeInfoPtr ) );
1253 if( envelopeInfoPtr->bufPos - envelopeInfoPtr->dataLeft < 2 )
1257 sMemConnect( &stream, envelopeInfoPtr->buffer + envelopeInfoPtr->dataLeft,
1258 envelopeInfoPtr->
bufPos - envelopeInfoPtr->dataLeft );
1264 switch( envelopeInfoPtr->usage )
1267 status = processSignedTrailer( envelopeInfoPtr, &stream,
1300 status = processMacTrailer( envelopeInfoPtr, &stream,
1322 sMemDisconnect( &stream );
1325 "Invalid CMS signed/MACd data trailer" ) );
1329 streamPos = stell( &stream );
1334 for( iterationCount = 0;
1350 status = getStreamObjectLength( &stream, &certSetLength );
1354 "Invalid signing certificate chain header",
1358 if( envelopeInfoPtr->auxBuffer == NULL )
1365 if( ( envelopeInfoPtr->auxBuffer = \
1366 clAlloc(
"processPostamble", certSetLength ) ) == NULL )
1371 envelopeInfoPtr->auxBufSize = certSetLength;
1373 ENSURES( envelopeInfoPtr->auxBufSize == certSetLength );
1374 status = sread( &stream, envelopeInfoPtr->auxBuffer,
1375 envelopeInfoPtr->auxBufSize );
1380 streamPos = stell( &stream );
1390 status = readSetI( &stream, &setLength );
1394 "Invalid SET OF Signature header", 31 );
1397 envelopeInfoPtr->hdrSetLength = setLength;
1403 streamPos = stell( &stream );
1410 int contentItemLength;
1413 status = addContentListItem( envelopeInfoPtr, &stream, NULL,
1414 &contentItemLength );
1418 "Invalid CMS signature record", 28 );
1424 streamPos = stell( &stream );
1427 if( contentItemLength < 0 || \
1428 contentItemLength > envelopeInfoPtr->hdrSetLength )
1433 envelopeInfoPtr->hdrSetLength -= contentItemLength;
1434 if( envelopeInfoPtr->hdrSetLength <= 0 )
1436 state = ( envelopeInfoPtr->payloadSize ==
CRYPT_UNUSED ) ? \
1442 const int value = checkEOC( &stream );
1450 state = ( envelopeInfoPtr->payloadSize ==
CRYPT_UNUSED ) ? \
1459 status = processEOCTrailer( envelopeInfoPtr, &stream );
1463 "Invalid CMS EOC trailer", 23 );
1468 streamPos = stell( &stream );
1473 sMemDisconnect( &stream );
1474 if( iterationCount >= FAILSAFE_ITERATIONS_MED )
1484 envelopeInfoPtr->deenvState = state;
1499 remainder = envelopeInfoPtr->bufPos - \
1500 ( envelopeInfoPtr->dataLeft + streamPos );
1502 envelopeInfoPtr->dataLeft + streamPos + \
1503 remainder <= envelopeInfoPtr->bufPos );
1504 if( remainder > 0 && streamPos > 0 )
1507 remainder, envelopeInfoPtr->bufPos ) );
1508 memmove( envelopeInfoPtr->buffer + envelopeInfoPtr->dataLeft,
1509 envelopeInfoPtr->buffer + envelopeInfoPtr->dataLeft + streamPos,
1512 envelopeInfoPtr->bufPos = envelopeInfoPtr->dataLeft + remainder;
1513 ENSURES( sanityCheck( envelopeInfoPtr ) );
1551 envelopeInfoPtr->processPreambleFunction = processPreamble;
1552 envelopeInfoPtr->processPostambleFunction = processPostamble;