28 #define ENCODED_ATTRIBUTE_SIZE 512
50 BUFFER_OPT( maxEncodedAttributeSize, encodedAttributeSize )
\
51 BYTE *encodedAttributes;
52 int maxEncodedAttributeSize;
56 int encodedAttributeSize;
59 #define initCmsAttributeInfo( attributeInfo, format, useDefault, cmsAttributes, messageHash, timeSource, tspSession ) \
60 memset( attributeInfo, 0, sizeof( CMS_ATTRIBUTE_INFO ) ); \
61 ( attributeInfo )->formatType = format; \
62 ( attributeInfo )->useDefaultAttributes = useDefault; \
63 ( attributeInfo )->iCmsAttributes = cmsAttributes; \
64 ( attributeInfo )->iMessageHash = messageHash; \
65 ( attributeInfo )->iTimeSource = timeSource; \
66 ( attributeInfo )->iTspSession = tspSession; \
67 ( attributeInfo )->maxEncodedAttributeSize = ENCODED_ATTRIBUTE_SIZE;
92 const void *attributes,
104 assert( ( attributes == NULL && attributeSize == 0 ) || \
105 isReadPtr( attributes, attributeSize ) );
106 assert(
isReadPtr( signature, signatureSize ) );
110 REQUIRES( ( attributes == NULL && attributeSize == 0 ) || \
111 ( attributes != NULL && \
119 return( sizeofHashAlgoID );
126 &msgData, CRYPT_IATTRIBUTE_ENC_TIMESTAMP );
129 timeStampSize = msgData.
length;
130 unsignedAttributeSize = ( int ) \
132 sizeofObject( timeStampSize ) );
134 status = dynCreate( &iAndSDB, certificate,
135 CRYPT_IATTRIBUTE_ISSUERANDSERIALNUMBER );
141 dynLength( iAndSDB ) + sizeofHashAlgoID + \
142 attributeSize + signatureSize + \
143 ( ( unsignedAttributeSize ) ? \
144 (
int ) sizeofObject( unsignedAttributeSize ) : 0 ) );
150 writeAlgoID( stream, hashAlgo );
151 if( attributeSize > 0 )
152 swrite( stream, attributes, attributeSize );
153 status = swrite( stream, signature, signatureSize );
154 dynDestroy( &iAndSDB );
163 writeConstructed( stream, unsignedAttributeSize, 1 );
165 sizeofObject( timeStampSize ) );
167 status = writeSet( stream, timeStampSize );
172 return( exportAttributeToStream( stream, unsignedAttrObject,
173 CRYPT_IATTRIBUTE_ENC_TIMESTAMP ) );
179 static
int createCmsCountersignature(
IN_BUFFER( dataSignatureSize ) \
180 const void *dataSignature,
190 assert(
isReadPtr( dataSignature, dataSignatureSize ) );
209 sMemConnect( &stream, dataSignature, dataSignatureSize );
210 readUniversal( &stream );
211 status = readOctetStringHole( &stream, &length, 16,
DEFAULT_TAG );
216 status = sMemGetDataBlock( &stream, &dataPtr, length );
223 sMemDisconnect( &stream );
254 static const SMIMECAP_INFO smimeCapInfo[] = {
339 smimeCapInfo[ i ].smimeCapability );
360 const BOOLEAN lengthCheckOnly )
367 assert(
isWritePtr( cmsAttributeInfo->encodedAttributes, \
368 cmsAttributeInfo->maxEncodedAttributeSize ) );
385 if( lengthCheckOnly )
414 if( !lengthCheckOnly )
416 const time_t currentTime = \
417 getReliableTime( cmsAttributeInfo->iTimeSource );
435 if( lengthCheckOnly )
440 cmsAttributeInfo->maxEncodedAttributeSize );
444 CRYPT_ICERTFORMAT_DATA );
447 cmsAttributeInfo->encodedAttributeSize = msgData.
length;
450 if( lengthCheckOnly )
456 temp = cmsAttributeInfo->encodedAttributes[ 0 ];
457 cmsAttributeInfo->encodedAttributes[ 0 ] =
BER_SET;
459 cmsAttributeInfo->encodedAttributes,
460 cmsAttributeInfo->encodedAttributeSize );
463 cmsAttributeInfo->encodedAttributes[ 0 ] = temp;
471 const BOOLEAN lengthCheckOnly )
478 assert(
isWritePtr( cmsAttributeInfo->attributeBuffer, \
479 cmsAttributeInfo->maxEncodedAttributeSize ) );
485 cmsAttributeInfo->useDefaultAttributes ==
FALSE ) || \
487 cmsAttributeInfo->useDefaultAttributes ==
TRUE ) || \
489 cmsAttributeInfo->useDefaultAttributes ==
FALSE ) );
494 REQUIRES( cmsAttributeInfo->encodedAttributes == NULL && \
495 cmsAttributeInfo->encodedAttributeSize == 0 );
502 cmsAttributeInfo->encodedAttributes = cmsAttributeInfo->attributeBuffer;
505 if( cmsAttributeInfo->useDefaultAttributes )
514 cmsAttributeInfo->iCmsAttributes = createInfo.
cryptHandle;
523 addSmimeCapabilities( cmsAttributeInfo->iCmsAttributes );
532 createdHashContext =
TRUE;
533 status = hashCmsAttributes( cmsAttributeInfo, createInfo.
cryptHandle,
536 if( cmsAttributeInfo->useDefaultAttributes )
544 if( createdHashContext )
590 BYTE *bufPtr = ( signature == NULL ) ? NULL : buffer;
594 assert( ( signature == NULL && sigMaxLength == 0 ) || \
596 assert(
isWritePtr( signatureLength,
sizeof(
int ) ) );
598 REQUIRES( ( signature == NULL && sigMaxLength == 0 ) || \
599 ( signature != NULL && \
605 useDefaultAuthAttr ==
FALSE ) || \
607 useDefaultAuthAttr ==
TRUE ) || \
609 useDefaultAuthAttr ==
FALSE ) );
616 *signatureLength = 0;
619 useDefaultAuthAttr, iAuthAttr, iHashContext,
620 signContext, iTspSession );
636 status = createCmsAttributes( &cmsAttributeInfo, &iCmsHashContext,
637 hashAlgo, ( signature == NULL ) ? \
644 status = createSignature( bufPtr, bufSize, &dataSignatureSize,
647 if( iCmsHashContext != iHashContext )
656 status = createCmsCountersignature( buffer, dataSignatureSize,
657 hashAlgo, iTspSession );
663 sMemOpenOpt( &stream, signature, ( signature == NULL ) ? 0 : sigMaxLength );
664 status = writeCmsSignerInfo( &stream, iSigningCert, hashAlgo,
665 cmsAttributeInfo.encodedAttributes,
666 cmsAttributeInfo.encodedAttributeSize,
667 buffer, dataSignatureSize,
670 length = stell( &stream );
671 sMemDisconnect( &stream );
707 *signatureLength =
length;
715 int checkSignatureCMS(
IN_BUFFER( signatureLength )
const void *signature,
732 assert(
isReadPtr( signature, signatureLength ) );
733 assert( ( iExtraData == NULL ) || \
741 if( iExtraData != NULL )
753 sMemConnect( &stream, signature, signatureLength );
754 status = queryAsn1Object( &stream, &queryInfo );
759 sMemDisconnect( &stream );
776 if( queryInfo.
hashAlgo != hashAlgo )
783 return( checkSignature( signature, signatureLength, sigCheckContext,
800 (
BYTE * ) setTag,
sizeof(
BYTE ) );
814 status = checkSignature( signature, signatureLength, sigCheckContext,
852 if( iExtraData != NULL )
853 *iExtraData = iLocalExtraData;