25 #define MIN_MSGIMPRINT_SIZE ( 2 + 10 + 16 )
26 #define MAX_MSGIMPRINT_SIZE ( 32 + CRYPT_MAX_HASHSIZE )
30 #define TSP_CONTENT_TYPE_REQ "application/timestamp-query"
31 #define TSP_CONTENT_TYPE_REQ_LEN 27
32 #define TSP_CONTENT_TYPE_RESP "application/timestamp-reply"
33 #define TSP_CONTENT_TYPE_RESP_LEN 27
40 #ifdef USE_CMP_TRANSPORT
43 #define TSP_HEADER_SIZE 5
45 enum { TSP_MESSAGE_REQUEST, TSP_MESSAGE_POLLREP, TSP_MESSAGE_POLLREQ,
46 TSP_MESSAGE_NEGPOLLREP, TSP_MESSAGE_PARTIALMSGREP, TSP_MESSAGE_RESPONSE,
54 #define OID_TSP_POLICY MKOID( "\x06\x0B\x2B\x06\x01\x04\x01\x97\x55\x36\xDD\x24\x36" )
68 BUFFER( MAX_MSGIMPRINT_SIZE, msgImprintSize )
\
69 BYTE msgImprint[ MAX_MSGIMPRINT_SIZE + 8 ];
106 assert(
isWritePtr( protocolInfo,
sizeof( TSP_PROTOCOL_INFO ) ) );
113 readSequence( stream, NULL );
119 "Invalid TSP request header" ) );
124 status = getStreamObjectLength( stream, &length );
126 status = sMemGetDataBlock( stream, &dataPtr, length );
129 if( length < MIN_MSGIMPRINT_SIZE || \
130 length > MAX_MSGIMPRINT_SIZE || \
138 "Invalid TSP message imprint data" ) );
141 memcpy( protocolInfo->msgImprint, dataPtr, length );
142 protocolInfo->msgImprintSize =
length;
155 sMemConnect( &msgImprintStream, protocolInfo->msgImprint,
156 protocolInfo->msgImprintSize );
157 readSequence( &msgImprintStream, NULL );
158 status = readAlgoID( &msgImprintStream, &msgImprintHashAlgo,
161 status = readOctetStringHole( &msgImprintStream, NULL, 16,
163 sMemDisconnect( &msgImprintStream );
168 "Invalid TSP message imprint content" ) );
174 protocolInfo->hashAlgo = msgImprintHashAlgo;
182 status = readUniversal( stream );
191 status = readRawObject( stream, protocolInfo->nonce,
196 status =
readBoolean( stream, &protocolInfo->includeSigCerts );
221 status = readUniversal( stream );
227 "Invalid TSP request additional information fields" ) );
235 static
int signTSToken(
OUT_BUFFER( tsaRespMaxLength, *tsaRespLength )
BYTE *tsaResp,
252 assert(
isWritePtr( tsaResp, tsaRespMaxLength ) );
253 assert(
isWritePtr( tsaRespLength,
sizeof(
int ) ) );
254 assert(
isReadPtr( tstInfo, tstInfoLength ) );
256 REQUIRES( tsaRespMaxLength >= 64 && \
265 memset( tsaResp, 0,
min( 16, tsaRespMaxLength ) );
277 status = dynCreate( &essCertDB, privateKey, CRYPT_IATTRIBUTE_ESSCERTID );
284 dynDestroy( &essCertDB );
335 CRYPT_IATTRIBUTE_INCLUDESIGCERT );
358 *tsaRespLength = msgData.
length;
375 INOUT TSP_PROTOCOL_INFO *protocolInfo )
377 TSP_INFO *tspInfo = sessionInfoPtr->sessionTSP;
379 #ifdef USE_CMP_TRANSPORT
380 BYTE *bufPtr = sessionInfoPtr->receiveBuffer;
386 assert(
isWritePtr( protocolInfo,
sizeof( TSP_PROTOCOL_INFO ) ) );
400 protocolInfo->msgImprintSize = \
403 ENSURES( protocolInfo->msgImprintSize > 0 && \
404 protocolInfo->msgImprintSize <= MAX_MSGIMPRINT_SIZE );
405 sMemOpen( &stream, sessionInfoPtr->receiveBuffer, 1024 );
407 protocolInfo->msgImprintSize + \
408 ( protocolInfo->includeSigCerts ? \
410 writeShortInteger( &stream, TSP_VERSION,
DEFAULT_TAG );
411 status = sMemGetDataBlock( &stream, &msgImprintPtr,
412 protocolInfo->msgImprintSize );
414 status = writeMessageDigest( &stream, tspInfo->
imprintAlgo,
416 memcpy( protocolInfo->msgImprint, msgImprintPtr,
417 protocolInfo->msgImprintSize );
418 if( protocolInfo->includeSigCerts )
421 sessionInfoPtr->receiveBufEnd = stell( &stream );
422 sMemDisconnect( &stream );
426 sessionInfoPtr->receiveBufEnd );
428 #ifdef USE_CMP_TRANSPORT
435 memmove( bufPtr + TSP_HEADER_SIZE, bufPtr,
436 sessionInfoPtr->receiveBufEnd );
437 mputLong( bufPtr, sessionInfoPtr->receiveBufEnd + 1 );
438 *bufPtr = TSP_MESSAGE_REQUEST;
439 sessionInfoPtr->receiveBufEnd += TSP_HEADER_SIZE;
444 return( writePkiDatagram( sessionInfoPtr, TSP_CONTENT_TYPE_REQ,
445 TSP_CONTENT_TYPE_REQ_LEN ) );
452 INOUT TSP_PROTOCOL_INFO *protocolInfo )
455 #ifdef USE_CMP_TRANSPORT
456 const int oldBufSize = sessionInfoPtr->receiveBufSize;
461 assert(
isWritePtr( protocolInfo,
sizeof( TSP_PROTOCOL_INFO ) ) );
465 sessionInfoPtr->receiveBufEnd = sessionInfoPtr->receiveBufPos = 0;
467 #ifdef USE_CMP_TRANSPORT
480 status = sread( &sessionInfoPtr->stream, buffer, TSP_HEADER_SIZE );
483 sNetGetErrorInfo( &sessionInfoPtr->stream,
484 &sessionInfoPtr->errorInfo );
488 if( packetLength < 16 || \
489 packetLength > sessionInfoPtr->receiveBufSize || \
490 ( *bufPtr != TSP_MESSAGE_REQUEST && \
491 *bufPtr != TSP_MESSAGE_RESPONSE ) )
495 "Invalid TSP socket protocol data" ) );
504 sessionInfoPtr->receiveBufSize = ( int ) packetLength - 1;
509 status = readPkiDatagram( sessionInfoPtr );
510 #ifdef USE_CMP_TRANSPORT
514 sessionInfoPtr->receiveBufSize = oldBufSize;
529 sMemConnect( &stream, sessionInfoPtr->receiveBuffer,
530 sessionInfoPtr->receiveBufEnd );
531 readSequence( &stream, NULL );
532 status = readPkiStatusInfo( &stream,
FALSE,
533 &sessionInfoPtr->errorInfo );
538 sMemDisconnect( &stream );
544 sessionInfoPtr->receiveBufPos = stell( &stream );
552 readSequence( &stream, NULL );
553 readUniversal( &stream );
554 readConstructed( &stream, NULL, 0 );
555 readSequence( &stream, NULL );
557 readUniversal( &stream );
558 readSequence( &stream, NULL );
559 readUniversal( &stream );
560 readConstructed( &stream, NULL, 0 );
561 readOctetStringHole( &stream, NULL, 16,
563 readSequence( &stream, NULL );
565 status = readUniversal( &stream );
572 status = sMemGetDataBlock( &stream, &msgImprintPtr,
573 protocolInfo->msgImprintSize );
575 memcmp( protocolInfo->msgImprint, msgImprintPtr,
576 protocolInfo->msgImprintSize ) )
579 sMemDisconnect( &stream );
586 "Invalid timestamp data" : \
587 "Returned timestamp message imprint doesn't match "
588 "original message imprint" ) );
603 #ifdef USE_CMP_TRANSPORT
605 #define respSize( data ) ( data[ 3 ] + 4 )
608 0x00, 0x00, 0x00, 0x08,
610 0x30, 0x05, 0x30, 0x03, 0x02, 0x01, 0x02
613 0x00, 0x00, 0x00, 0x0C,
615 0x30, 0x09, 0x30, 0x07, 0x02, 0x01, 0x02, 0x03,
619 0x00, 0x00, 0x00, 0x0E,
621 0x30, 0x0B, 0x30, 0x09, 0x02, 0x01, 0x02, 0x03,
622 0x04, 0x07, 0x00, 0x00, 0x80
626 #define respSize( data ) ( data[ 1 ] + 2 )
637 0x03, 0x02, 0x05, 0x20
643 0x03, 0x04, 0x07, 0x00, 0x00, 0x80
649 const BYTE *errorResponse,
660 #ifdef USE_CMP_TRANSPORT
663 memcpy( sessionInfoPtr->receiveBuffer, errorResponse,
664 respSize( errorResponse ) );
665 sessionInfoPtr->receiveBufEnd = respSize( errorResponse );
670 memcpy( sessionInfoPtr->receiveBuffer, errorResponse,
671 respSize( errorResponse ) );
672 sessionInfoPtr->receiveBufEnd = respSize( errorResponse );
674 ( void ) writePkiDatagram( sessionInfoPtr, TSP_CONTENT_TYPE_RESP,
675 TSP_CONTENT_TYPE_RESP_LEN );
683 INOUT TSP_PROTOCOL_INFO *protocolInfo )
686 #ifdef USE_CMP_TRANSPORT
687 BYTE *bufPtr = sessionInfoPtr->receiveBuffer;
688 const int oldBufSize = sessionInfoPtr->receiveBufSize;
693 assert(
isWritePtr( protocolInfo,
sizeof( TSP_PROTOCOL_INFO ) ) );
695 #ifdef USE_CMP_TRANSPORT
704 status = sread( &sessionInfoPtr->stream, bufPtr, TSP_HEADER_SIZE );
707 sNetGetErrorInfo( &sessionInfoPtr->stream,
708 &sessionInfoPtr->errorInfo );
712 if( packetLength < 16 || \
713 packetLength > sessionInfoPtr->receiveBufSize || \
714 ( *bufPtr != TSP_MESSAGE_REQUEST && \
715 *bufPtr != TSP_MESSAGE_RESPONSE ) )
719 "Invalid TSP socket protocol data" ) );
727 sessionInfoPtr->receiveBufSize = ( int ) packetLength - 1;
732 status = readPkiDatagram( sessionInfoPtr );
733 #ifdef USE_CMP_TRANSPORT
737 sessionInfoPtr->receiveBufSize = oldBufSize;
741 return( sendErrorResponse( sessionInfoPtr, respBadGeneric, status ) );
742 sMemConnect( &stream, sessionInfoPtr->receiveBuffer,
743 sessionInfoPtr->receiveBufEnd );
744 status = readTSPRequest( &stream, protocolInfo,
746 sMemDisconnect( &stream );
749 return( sendErrorResponse( sessionInfoPtr, \
753 respBadGeneric, status ) );
762 INOUT TSP_PROTOCOL_INFO *protocolInfo )
766 BYTE tstBuffer[ 1024 ];
767 BYTE serialNo[ 16 + 8 ];
768 BYTE *bufPtr = sessionInfoPtr->receiveBuffer;
769 const time_t currentTime =
getReliableTime( sessionInfoPtr->privateKey );
770 #ifdef USE_CMP_TRANSPORT
779 assert(
isWritePtr( protocolInfo,
sizeof( TSP_PROTOCOL_INFO ) ) );
787 &msgData, CRYPT_IATTRIBUTE_RANDOM_NONCE );
790 sMemOpen( &stream, tstBuffer, 1024 );
792 sizeofOID( OID_TSP_POLICY ) + protocolInfo->msgImprintSize + \
794 protocolInfo->nonceSize );
796 writeOID( &stream, OID_TSP_POLICY );
797 swrite( &stream, protocolInfo->msgImprint, protocolInfo->msgImprintSize );
798 writeInteger( &stream, serialNo, 16,
DEFAULT_TAG );
799 status = writeGeneralizedTime( &stream, currentTime,
DEFAULT_TAG );
800 if( protocolInfo->nonceSize > 0 )
801 status = swrite( &stream, protocolInfo->nonce,
802 protocolInfo->nonceSize );
804 tstLength = stell( &stream );
805 sMemDisconnect( &stream );
807 return( sendErrorResponse( sessionInfoPtr, respBadGeneric, status ) );
812 status = signTSToken( sessionInfoPtr->receiveBuffer + headerOfs + 9,
813 min( sessionInfoPtr->receiveBufSize, \
815 protocolInfo->hashAlgo, tstBuffer, tstLength,
816 sessionInfoPtr->privateKey,
817 protocolInfo->includeSigCerts );
819 return( sendErrorResponse( sessionInfoPtr, respBadGeneric, status ) );
821 sessionInfoPtr->receiveBuffer + headerOfs + 9,
824 #ifdef USE_CMP_TRANSPORT
831 bufPtr = sessionInfoPtr->receiveBuffer;
832 mputLong( bufPtr, 1 + 9 + responseLength );
833 *bufPtr++ = TSP_MESSAGE_RESPONSE;
841 REQUIRES( responseLength >= 256 && \
843 sMemOpen( &stream, bufPtr, 4 + 5 );
844 writeSequence( &stream, 5 + responseLength );
845 swrite( &stream,
"\x30\x03\x02\x01\x00", 5 );
846 sMemDisconnect( &stream );
847 sessionInfoPtr->receiveBufEnd = headerOfs + 9 + responseLength;
848 return( writePkiDatagram( sessionInfoPtr, TSP_CONTENT_TYPE_RESP,
849 TSP_CONTENT_TYPE_RESP_LEN ) );
869 if( sessionInfoPtr->sessionTSP->imprintSize == 0 )
877 memset( &protocolInfo, 0,
sizeof( TSP_PROTOCOL_INFO ) );
878 status = sendClientRequest( sessionInfoPtr, &protocolInfo );
880 status = readServerResponse( sessionInfoPtr, &protocolInfo );
893 memset( &protocolInfo, 0,
sizeof( TSP_PROTOCOL_INFO ) );
894 status = readClientRequest( sessionInfoPtr, &protocolInfo );
896 status = sendServerResponse( sessionInfoPtr, &protocolInfo );
914 const int dataSize = sessionInfoPtr->receiveBufEnd -
\
923 type == CRYPT_IATTRIBUTE_ENC_TIMESTAMP );
928 if( sessionInfoPtr->receiveBufPos <= 0 )
933 if( type == CRYPT_IATTRIBUTE_ENC_TIMESTAMP )
936 sessionInfoPtr->receiveBufEnd ) );
938 sessionInfoPtr->receiveBuffer + sessionInfoPtr->receiveBufPos,
964 sessionInfoPtr->receiveBufPos, dataSize );
975 sessionInfoPtr->iCertResponse = createInfo.
cryptHandle;
979 *cryptEnvelopePtr = sessionInfoPtr->iCertResponse;
989 TSP_INFO *tspInfo = sessionInfoPtr->sessionTSP;
993 assert(
isReadPtr( data,
sizeof(
int ) ) );
1020 IN const void *data,
1027 assert(
isReadPtr( data,
sizeof(
int ) ) );
1075 #ifdef USE_CMP_TRANSPORT
1092 SESSION_NEEDS_PRIVKEYCERT,
1097 #ifdef USE_CMP_TRANSPORT
1107 sessionInfoPtr->transactFunction = serverTransact;
1109 sessionInfoPtr->transactFunction = clientTransact;
1110 sessionInfoPtr->getAttributeFunction = getAttributeFunction;
1111 sessionInfoPtr->setAttributeFunction = setAttributeFunction;
1112 sessionInfoPtr->checkAttributeFunction = checkAttributeFunction;