28 #ifdef ALLOW_SSLV2_HELLO
61 "Invalid legacy SSLv2 hello packet length %d", length ) );
68 status = hashHSPacketRead( handshakeInfo, &stream );
70 value = sgetc( &stream );
73 sMemDisconnect( &stream );
76 "Unexpected legacy SSLv2 packet type %d, should be %d",
79 status = processVersionInfo( sessionInfoPtr, &stream,
83 sMemDisconnect( &stream );
86 length -= stell( &stream );
87 sMemDisconnect( &stream );
90 status = sread( &sessionInfoPtr->
stream, sessionInfoPtr->receiveBuffer,
94 sNetGetErrorInfo( &sessionInfoPtr->
stream,
98 if( status != length )
104 "Timeout during legacy SSLv2 hello packet read, only got "
105 "%d of %d bytes", status, length ) );
109 sMemConnect( &stream, sessionInfoPtr->receiveBuffer, length );
110 status = hashHSPacketRead( handshakeInfo, &stream );
111 sMemDisconnect( &stream );
117 handshakeInfo->isSSLv2 =
TRUE;
136 const int packetType;
137 const char *packetName;
139 static const PACKET_NAME_INFO packetNameInfo[] = {
149 REQUIRES_EXT( ( packetType >= 0 && packetType <= 0xFF ),
150 "<Internal error>" );
152 for( i = 0; packetNameInfo[ i ].packetType != packetType && \
157 PACKET_NAME_INFO ) ),
158 "<Internal error>" );
160 return( packetNameInfo[ i ].packetName );
167 const int packetType;
168 const char *packetName;
170 static const PACKET_NAME_INFO packetNameInfo[] = {
186 REQUIRES_EXT( ( packetType >= 0 && packetType <= 0xFF ),
187 "<Internal error>" );
189 for( i = 0; packetNameInfo[ i ].packetType != packetType && \
194 PACKET_NAME_INFO ) ),
195 "<Internal error>" );
197 return( packetNameInfo[ i ].packetName );
213 assert( clientVersion == NULL || \
217 if( clientVersion != NULL )
221 version = sgetc( stream );
226 "Invalid major version number %d, should be 3", version ) );
232 version = sgetc( stream );
233 if( clientVersion == NULL )
235 if( version != sessionInfoPtr->version )
239 "Invalid version number 3.%d, should be 3.%d",
240 version, sessionInfoPtr->version ) );
285 "Invalid protocol version 3.%d", version ) );
290 if( sessionInfoPtr->sessionSSL->minVersion > 0 && \
291 version < sessionInfoPtr->
sessionSSL->minVersion )
295 "Invalid version number 3.%d, should be at least 3.%d",
296 version, sessionInfoPtr->sessionSSL->minVersion ) );
299 *clientVersion = version;
331 const int packetType,
335 SSL_INFO *sslInfo = sessionInfoPtr->sessionSSL;
336 const int expectedPacketType = \
343 assert(
isWritePtr( packetLength,
sizeof(
int ) ) );
349 minLength == 0 ) || \
357 value = sgetc( stream );
358 if( value != expectedPacketType )
379 "Unexpected %s (%d) packet, expected %s (%d)",
382 expectedPacketType ) );
385 status = processVersionInfo( sessionInfoPtr, stream,
391 length = readUint16( stream );
394 if( length < sslInfo->ivSize + minLength + \
395 sessionInfoPtr->authBlocksize || \
397 sessionInfoPtr->authBlocksize + 256 || \
411 "Invalid packet length %d for %s (%d) packet",
421 status = loadExplicitIV( sessionInfoPtr, stream, &ivLength );
426 "Error loading TLS explicit IV" ) );
429 ENSURES( length >= minLength + sessionInfoPtr->authBlocksize && \
430 length <= maxLength );
451 assert(
isWritePtr( packetLength,
sizeof(
int ) ) );
453 return( checkPacketHeader( sessionInfoPtr, stream, packetLength,
455 sessionInfoPtr->receiveBufSize ) );
470 assert(
isWritePtr( packetLength,
sizeof(
int ) ) );
487 "Invalid handshake packet header" ) );
492 type = sgetc( stream );
493 if( type != packetType )
497 "Invalid handshake packet %s (%d), expected %s (%d)",
501 length = readUint24( stream );
503 length > sMemDataLeft( stream ) )
507 "Invalid length %d for %s (%d) handshake packet",
511 DEBUG_PRINT((
"Read %s (%d) handshake packet, length %ld.\n",
566 assert(
isWritePtr( dataLength,
sizeof(
int ) ) );
568 REQUIRES( dataMaxLength >= sessionInfoPtr->authBlocksize && \
570 sessionInfoPtr->authBlocksize + 256 );
577 if( sessionInfoPtr->cryptBlocksize > 1 && \
578 ( dataMaxLength % sessionInfoPtr->cryptBlocksize ) )
582 "Invalid encrypted packet length %d relative to cipher "
583 "block size %d for %s (%d) packet", dataMaxLength,
584 sessionInfoPtr->cryptBlocksize,
592 SSL_INFO *sslInfo = sessionInfoPtr->sessionSSL;
595 length -= sessionInfoPtr->authBlocksize;
600 "Invalid payload length %d for %s (%d) packet",
607 sslInfo->
readSeqNo, sessionInfoPtr->version,
608 length, packetType );
617 status = decryptData( sessionInfoPtr, data, length, &length );
636 length =
min( dataMaxLength,
637 MAX_PACKET_SIZE + sessionInfoPtr->authBlocksize );
649 payloadLength = length - sessionInfoPtr->authBlocksize;
650 if( payloadLength < 0 || payloadLength > MAX_PACKET_SIZE )
659 data = dummyDataBuffer;
661 length = sessionInfoPtr->authBlocksize;
662 memset( data, 0, length );
670 status = checkMacSSL( sessionInfoPtr, data, length, payloadLength,
671 packetType, badDecrypt );
673 status = checkMacTLS( sessionInfoPtr, data, length, payloadLength,
674 packetType, badDecrypt );
700 const int localPacketType = \
706 assert( ( handshakeInfo == NULL ) || \
708 assert(
isWritePtr( packetLength,
sizeof(
int ) ) );
715 sessionInfoPtr->receiveBufStartOfs < \
722 status = readFixedHeaderAtomic( sessionInfoPtr, headerBuffer,
723 sessionInfoPtr->receiveBufStartOfs );
741 "Other side unexpectedly closed the connection, "
742 "probably due to incorrect encryption keys being "
743 "negotiated during the handshake: " ) );
751 return( processAlert( sessionInfoPtr, headerBuffer,
752 sessionInfoPtr->receiveBufStartOfs ) );
759 #ifdef ALLOW_SSLV2_HELLO
762 status = length = handleSSLv2Header( sessionInfoPtr, handshakeInfo,
771 "Client sent obsolete handshake for the insecure SSLv2 "
775 sMemConnect( &stream, headerBuffer, sessionInfoPtr->receiveBufStartOfs );
776 status = checkPacketHeader( sessionInfoPtr, &stream, &bytesToRead,
780 sessionInfoPtr->receiveBufSize );
781 sMemDisconnect( &stream );
787 sread( &sessionInfoPtr->stream, sessionInfoPtr->receiveBuffer,
791 sNetGetErrorInfo( &sessionInfoPtr->stream,
792 &sessionInfoPtr->errorInfo );
795 if( length != bytesToRead )
801 "Timed out reading packet data for %s (%d) packet, only "
803 localPacketType, length, bytesToRead ) );
805 sessionInfoPtr->receiveBufPos = 0;
806 sessionInfoPtr->receiveBufEnd =
length;
807 if( handshakeInfo != NULL )
809 sMemConnect( &stream, sessionInfoPtr->receiveBuffer, length );
810 status = hashHSPacketRead( handshakeInfo, &stream );
811 sMemDisconnect( &stream );
826 STREAM *stream = &handshakeInfo->stream;
834 length = sMemDataLeft( stream );
843 "Invalid handshake packet data length %d", length ) );
850 sMemDisconnect( stream );
851 status = readHSPacketSSL( sessionInfoPtr, handshakeInfo, &length,
855 return( sMemConnect( stream, sessionInfoPtr->receiveBuffer, length ) );
870 const int cryptlibError;
873 const static ALERT_INFO alertInfo[] = {
934 assert(
isReadPtr( header, headerLength ) );
936 REQUIRES( headerLength > 0 && headerLength < MAX_INTLENGTH );
939 sMemConnect( &stream, header, headerLength );
940 status = checkPacketHeader( sessionInfoPtr, &stream, &length,
942 sessionInfoPtr->receiveBufSize );
945 sMemDisconnect( &stream );
950 if( length < ALERTINFO_SIZE || length > 256 )
958 sMemDisconnect( &stream );
963 "Invalid alert message length %d", length ) );
967 status = sread( &sessionInfoPtr->stream, buffer, length );
970 sNetGetErrorInfo( &sessionInfoPtr->stream,
971 &sessionInfoPtr->errorInfo );
974 if( status != length )
980 sendCloseAlert( sessionInfoPtr,
TRUE );
984 "Timed out reading alert message, only got %d of %d "
985 "bytes", status, length ) );
1001 sessionInfoPtr->receiveBufEnd =
length;
1002 status = unwrapPacketSSL( sessionInfoPtr, buffer, length, &length,
1006 sendCloseAlert( sessionInfoPtr,
TRUE );
1013 sendCloseAlert( sessionInfoPtr,
TRUE );
1038 "Invalid alert message level %d", buffer[ 0 ] ) );
1041 for( i = 0; alertInfo[ i ].type !=
CRYPT_ERROR && \
1042 alertInfo[ i ].type != type && \
1049 "Unknown alert message type %d at alert level %d",
1050 type, buffer[ 0 ] ) );
1052 retExtStr( alertInfo[ i ].cryptlibError,
1056 "Received SSL alert message: " : \
1057 "Received TLS alert message: " ) );