82 if( pendingPacketLength == 0 && pendingPacketRemaining == 0 )
86 if( pendingPacketLength < 0 || \
88 pendingPacketRemaining < 0 || \
232 assert(
isWritePtr( bytesRead,
sizeof(
int ) ) );
235 REQUIRES( sanityCheckRead( sessionInfoPtr ) );
250 if( sessionInfoPtr->pendingPacketLength <= 0 )
282 sessionInfoPtr->receiveBufEnd +=
length;
283 sessionInfoPtr->pendingPacketRemaining -=
length;
286 ENSURES( sessionInfoPtr->partialHeaderRemaining == 0 );
291 bytesLeft = sessionInfoPtr->receiveBufSize - sessionInfoPtr->receiveBufEnd;
292 if( bytesLeft < 1024 )
294 ENSURES( sanityCheckRead( sessionInfoPtr ) );
299 if( bytesLeft > sessionInfoPtr->pendingPacketRemaining )
302 bytesLeft = sessionInfoPtr->pendingPacketRemaining;
307 sessionInfoPtr->receiveBufSize ) );
309 sread( &sessionInfoPtr->stream,
310 sessionInfoPtr->receiveBuffer + sessionInfoPtr->receiveBufEnd,
314 sNetGetErrorInfo( &sessionInfoPtr->stream,
315 &sessionInfoPtr->errorInfo );
323 ENSURES( sanityCheckRead( sessionInfoPtr ) );
327 sessionInfoPtr->receiveBufEnd +=
length;
328 sessionInfoPtr->pendingPacketRemaining -=
length;
329 if( sessionInfoPtr->pendingPacketRemaining > 0 )
334 ENSURES( sanityCheckRead( sessionInfoPtr ) );
338 ENSURES( sessionInfoPtr->pendingPacketRemaining == 0 );
339 ENSURES( sanityCheckRead( sessionInfoPtr ) );
348 sessionInfoPtr->receiveBufEnd = sessionInfoPtr->receiveBufPos +
length;
349 sessionInfoPtr->receiveBufPos = sessionInfoPtr->receiveBufEnd;
350 sessionInfoPtr->pendingPacketLength = 0;
353 ENSURES( sanityCheckRead( sessionInfoPtr ) );
366 const int bytesToCopy =
min( length, sessionInfoPtr->receiveBufPos );
371 assert(
isWritePtr( bytesCopied,
sizeof(
int ) ) );
375 REQUIRES( sanityCheckRead( sessionInfoPtr ) );
378 memset( buffer, 0,
min( 16, length ) );
386 if( bytesToCopy > 0 )
388 const int remainder = sessionInfoPtr->receiveBufEnd - bytesToCopy;
392 memcpy( buffer, sessionInfoPtr->receiveBuffer, bytesToCopy );
398 sessionInfoPtr->receiveBufEnd ) );
399 memmove( sessionInfoPtr->receiveBuffer,
400 sessionInfoPtr->receiveBuffer + bytesToCopy, remainder );
401 sessionInfoPtr->receiveBufPos -= bytesToCopy;
402 sessionInfoPtr->receiveBufEnd = remainder;
408 sessionInfoPtr->receiveBufPos = sessionInfoPtr->receiveBufEnd = 0;
413 *bytesCopied = bytesToCopy;
414 if( bytesToCopy >= length )
416 ENSURES( sanityCheckRead( sessionInfoPtr ) );
421 ENSURES( sessionInfoPtr->receiveBufPos == 0 );
437 status = tryRead( sessionInfoPtr, &bytesRead, &readInfo );
444 if( *bytesCopied <= 0 )
447 sessionInfoPtr->readErrorState =
status;
460 sessionInfoPtr->pendingReadErrorState =
status;
478 sessionInfoPtr->pendingPacketRemaining <= \
479 sessionInfoPtr->receiveBufSize - sessionInfoPtr->receiveBufEnd )
484 ENSURES( sanityCheckRead( sessionInfoPtr ) );
493 ENSURES( sanityCheckRead( sessionInfoPtr ) );
504 ENSURES( sanityCheckRead( sessionInfoPtr ) );
519 assert(
isWritePtr( bytesCopied,
sizeof(
int ) ) );
522 REQUIRES( sanityCheckRead( sessionInfoPtr ) );
525 memset( data, 0,
min( 16, dataMaxLength ) );
533 REQUIRES( sessionInfoPtr->receiveBufPos == 0 );
535 status = sessionInfoPtr->readErrorState =
\
537 sessionInfoPtr->pendingReadErrorState =
CRYPT_OK;
544 sessionInfoPtr->readTimeout );
546 for( iterationCount = 0;
560 status = getData( sessionInfoPtr, dataPtr, dataLength, &count );
567 *bytesCopied += count;
577 ENSURES( iterationCount < FAILSAFE_ITERATIONS_MAX );
579 ENSURES( sanityCheckRead( sessionInfoPtr ) );
583 return( ( *bytesCopied > 0 || status ==
OK_SPECIAL ) ? \
616 assert(
isWritePtr( headerBuffer,
sizeof( headerLength ) ) );
620 REQUIRES( sanityCheckRead( sessionInfoPtr ) );
623 memset( headerBuffer, 0,
min( 16, headerLength ) );
627 sread( &sessionInfoPtr->stream, headerBuffer, headerLength );
638 sNetGetErrorInfo( &sessionInfoPtr->stream,
639 &sessionInfoPtr->errorInfo );
645 if( length != headerLength )
651 "Timeout during packet header read, only got %d of %d "
652 "bytes", length, headerLength ) );
654 ENSURES( sanityCheckRead( sessionInfoPtr ) );
663 const int headerLength )
665 BYTE *bufPtr = headerBuffer;
669 assert(
isWritePtr( headerBuffer,
sizeof( headerLength ) ) );
673 REQUIRES( sanityCheckRead( sessionInfoPtr ) );
680 if( sessionInfoPtr->partialHeaderRemaining <= 0 )
696 bufPtr += headerLength - sessionInfoPtr->partialHeaderRemaining;
697 bytesToRead = sessionInfoPtr->partialHeaderRemaining;
699 ENSURES( bytesToRead > 0 && bytesToRead <= headerLength );
700 ENSURES( sessionInfoPtr->partialHeaderRemaining > 0 && \
701 sessionInfoPtr->partialHeaderRemaining <= headerLength );
704 memset( bufPtr, 0,
min( 16, bytesToRead ) );
708 bytesToRead, headerLength ) );
710 sread( &sessionInfoPtr->stream, bufPtr, bytesToRead );
721 sNetGetErrorInfo( &sessionInfoPtr->stream,
722 &sessionInfoPtr->errorInfo );
725 sessionInfoPtr->partialHeaderRemaining -=
length;
728 if( sessionInfoPtr->partialHeaderRemaining > 0 )
730 ENSURES( sanityCheckRead( sessionInfoPtr ) );
736 ENSURES( sessionInfoPtr->partialHeaderRemaining == 0 );
738 ENSURES( sanityCheckRead( sessionInfoPtr ) );
761 static
int getRemainingBufferSpace(
const SESSION_INFO *sessionInfoPtr )
763 const int currentByteCount = sessionInfoPtr->
sendBufPos -
\
765 int remainingByteCount;
769 REQUIRES( currentByteCount >= 0 && \
770 currentByteCount <= sessionInfoPtr->maxPacketSize && \
772 remainingByteCount = sessionInfoPtr->
maxPacketSize - currentByteCount;
775 return( remainingByteCount );
840 REQUIRES( sanityCheckWrite( sessionInfoPtr ) );
874 status = length = swrite( &sessionInfoPtr->
stream,
875 sessionInfoPtr->sendBuffer + \
883 sNetGetErrorInfo( &sessionInfoPtr->
stream,
899 ENSURES( sanityCheckWrite( sessionInfoPtr ) );
903 if( length < bytesToWrite )
909 ENSURES( sanityCheckWrite( sessionInfoPtr ) );
914 ENSURES( length == bytesToWrite );
921 ENSURES( sanityCheckWrite( sessionInfoPtr ) );
936 assert( data == NULL ||
isReadPtr( data, dataLength ) );
937 assert(
isWritePtr( bytesCopied,
sizeof(
int ) ) );
939 REQUIRES( ( data == NULL && dataLength == 0 ) || \
942 REQUIRES( sanityCheckWrite( sessionInfoPtr ) );
952 REQUIRES( sessionInfoPtr->receiveBufPos == 0 );
954 status = sessionInfoPtr->writeErrorState =
\
956 sessionInfoPtr->pendingWriteErrorState =
CRYPT_OK;
963 sessionInfoPtr->writeTimeout );
971 const int oldBufPos = sessionInfoPtr->sendBufPartialBufPos;
974 status = flushData( sessionInfoPtr );
983 bytesWritten = sessionInfoPtr->sendBufPartialBufPos - oldBufPos;
984 if( bytesWritten > 0 )
988 "Timeout during flush, only %d bytes were written "
989 "before the timeout of %d seconds expired",
990 sessionInfoPtr->sendBufPartialBufPos,
991 sessionInfoPtr->writeTimeout ) );
995 "Timeout during flush, no data could be written before "
996 "the timeout of %d seconds expired",
997 sessionInfoPtr->writeTimeout ) );
1003 if( sessionInfoPtr->partialWrite )
1005 status = flushData( sessionInfoPtr );
1012 availableBuffer = getRemainingBufferSpace( sessionInfoPtr );
1014 return( availableBuffer );
1015 for( iterationCount = 0;
1019 ENSURES( availableBuffer >= 0 && availableBuffer <= length );
1030 if( availableBuffer > 0 )
1033 sessionInfoPtr->sendBufSize ) );
1034 memcpy( sessionInfoPtr->sendBuffer + sessionInfoPtr->sendBufPos,
1035 dataPtr, availableBuffer );
1036 sessionInfoPtr->sendBufPos += availableBuffer;
1037 dataPtr += availableBuffer;
1038 length -= availableBuffer;
1039 *bytesCopied += availableBuffer;
1041 status = flushData( sessionInfoPtr );
1048 ENSURES( sanityCheckWrite( sessionInfoPtr ) );
1056 if( *bytesCopied > 0 )
1058 sessionInfoPtr->pendingWriteErrorState =
status;
1060 ENSURES( sanityCheckWrite( sessionInfoPtr ) );
1071 availableBuffer = getRemainingBufferSpace( sessionInfoPtr );
1073 return( availableBuffer );
1075 ENSURES( iterationCount < FAILSAFE_ITERATIONS_LARGE );
1081 ENSURES( length < availableBuffer );
1084 sessionInfoPtr->maxPacketSize ) );
1085 memcpy( sessionInfoPtr->sendBuffer + sessionInfoPtr->sendBufPos,
1087 sessionInfoPtr->sendBufPos +=
length;
1091 ENSURES( sanityCheckWrite( sessionInfoPtr ) );
1115 sessionInfoPtr->receiveBufEnd = 0;
1116 #ifdef USE_CMP_TRANSPORT
1120 sessionInfoPtr->receiveBufSize );
1121 status = sread( &sessionInfoPtr->stream, &httpDataInfo,
1129 sread( &sessionInfoPtr->stream,
1130 sessionInfoPtr->receiveBuffer,
1131 sessionInfoPtr->receiveBufSize );
1135 sessionInfoPtr->receiveBufSize );
1136 status = sread( &sessionInfoPtr->stream, &httpDataInfo,
1141 sNetGetErrorInfo( &sessionInfoPtr->stream,
1142 &sessionInfoPtr->errorInfo );
1154 "Invalid PKI message length %d", status ) );
1162 status = length = checkObjectEncoding( sessionInfoPtr->receiveBuffer,
1168 "Invalid PKI message encoding" ) );
1170 sessionInfoPtr->receiveBufEnd =
length;
1177 const char *contentType,
1184 assert( contentType == NULL || \
1185 isReadPtr( contentType, contentTypeLength ) );
1187 REQUIRES( ( contentType == NULL && contentTypeLength ) || \
1188 ( contentType != NULL && \
1189 contentTypeLength > 0 && \
1191 REQUIRES( sessionInfoPtr->receiveBufEnd > 4 && \
1197 #ifdef USE_CMP_TRANSPORT
1201 sessionInfoPtr->receiveBufEnd );
1202 httpDataInfo.contentType = contentType;
1204 status = swrite( &sessionInfoPtr->stream, &httpDataInfo,
1209 status = swrite( &sessionInfoPtr->stream,
1210 sessionInfoPtr->receiveBuffer,
1211 sessionInfoPtr->receiveBufEnd );
1215 sessionInfoPtr->receiveBufEnd );
1216 httpDataInfo.contentType = contentType;
1218 status = swrite( &sessionInfoPtr->stream, &httpDataInfo,
1222 sNetGetErrorInfo( &sessionInfoPtr->stream,
1223 &sessionInfoPtr->errorInfo );
1224 sessionInfoPtr->receiveBufEnd = 0;