46 readSequence( stream, NULL );
47 status = readEncodedOID( stream, oid,
MAX_OID_SIZE, &length,
58 protocolInfo->isCryptlib =
TRUE;
59 return( readUniversal( stream ) );
71 readSet( stream, NULL );
72 readSequence( stream, NULL );
73 readSequence( stream, NULL );
74 status = readSequence( stream, &length );
77 endPos = stell( stream ) +
length;
82 if( protocolInfo->certIDsize !=
KEYID_SIZE || \
83 memcmp( certID, protocolInfo->certID,
KEYID_SIZE ) )
87 memcpy( protocolInfo->certID, certID,
KEYID_SIZE );
89 protocolInfo->certIDchanged =
TRUE;
91 if( stell( stream ) < endPos )
97 status = readUniversal( stream );
103 return( readUniversal( stream ) );
119 status = readSequence( stream, &length );
122 endPos = stell( stream ) +
length;
123 for( iterationCount = 0;
124 stell( stream ) < endPos && \
127 status = readGeneralInfoAttribute( stream, protocolInfo );
131 ENSURES( iterationCount < FAILSAFE_ITERATIONS_MED );
141 const BOOLEAN isServerInitialMessage )
152 if( !isServerInitialMessage )
153 return( readUniversal( stream ) );
169 if( protocolInfo->userIDsize == userIDsize && \
170 !memcmp( protocolInfo->userID, userID, userIDsize ) )
173 protocolInfo->isServer ?
"SVR" :
"CLI" ));
175 protocolInfo->userID, protocolInfo->userIDsize );
181 memcpy( protocolInfo->userID, userID, userIDsize );
182 protocolInfo->userIDsize = userIDsize;
183 protocolInfo->userIDchanged =
TRUE;
191 protocolInfo->isServer ?
"SVR" :
"CLI" ));
193 protocolInfo->userID, protocolInfo->userIDsize );
201 static
int readTransactionID(
INOUT STREAM *stream,
203 const BOOLEAN isServerInitialMessage )
213 if( isServerInitialMessage )
216 &protocolInfo->transIDsize,
221 protocolInfo->isServer ?
"SVR" :
"CLI" ));
223 protocolInfo->transID, protocolInfo->transIDsize );
236 protocolInfo->isServer ?
"SVR" :
"CLI" ));
238 protocolInfo->transID, protocolInfo->transIDsize );
239 if( protocolInfo->transIDsize != length || \
240 memcmp( protocolInfo->transID, buffer, length ) )
249 static
int readProtectionAlgo(
INOUT STREAM *stream,
265 streamPos = stell( stream );
266 status = readAlgoIDex( stream, &cryptAlgo, &hashAlgo, &hashParam,
278 protocolInfo->useMACreceive =
FALSE;
288 sClearError( stream );
289 protocolInfo->macInfoPos = streamPos;
290 status = readUniversal( stream );
291 protocolInfo->useMACreceive =
TRUE;
302 const BOOLEAN isServerInitialMessage,
320 if(
isServer( sessionInfoPtr ) && protocolInfo->userIDsize == 9 )
323 int encodedUserIDLength;
326 &encodedUserIDLength,
327 protocolInfo->userID,
328 protocolInfo->userIDsize, 3 );
331 status = updateSessionInfo( &sessionInfoPtr->attributeList,
340 if( isServerInitialMessage && useMAC )
344 "User ID provided by client isn't a cryptlib user "
350 status = updateSessionInfo( &sessionInfoPtr->attributeList,
352 protocolInfo->userID,
353 protocolInfo->userIDsize,
362 if( isServerInitialMessage && useMAC )
363 return( initServerAuthentMAC( sessionInfoPtr, protocolInfo ) );
371 const BOOLEAN isServerInitialMessage )
378 status = addSessionInfoS( &sessionInfoPtr->attributeList,
380 protocolInfo->certID,
381 protocolInfo->certIDsize );
388 if( isServerInitialMessage )
389 return( initServerAuthentSign( sessionInfoPtr, protocolInfo ) );
408 findSessionInfo( sessionInfoPtr->attributeList,
410 BYTE macKey[ 64 + 8 ];
411 const BYTE *macKeyPtr = macKey;
412 const int streamPos = stell( stream );
417 sseek( stream, protocolInfo->macInfoPos );
421 status = decodePKIUserValue( macKey, 64, &macKeyLength,
428 macKeyPtr = passwordPtr->value;
431 status = readMacInfo( stream, protocolInfo, macKeyPtr,
436 sseek( stream, streamPos );
476 const BOOLEAN isServerInitialMessage )
485 protocolInfo->userIDchanged = protocolInfo->certIDchanged =
\
488 protocolInfo->senderDNPtr = NULL;
489 protocolInfo->senderDNlength = 0;
490 protocolInfo->headerRead =
FALSE;
495 status = readSequence( stream, &length );
498 endPos = stell( stream ) +
length;
500 if( !protocolInfo->isCryptlib )
515 status = readConstructed( stream, &protocolInfo->senderDNlength, 4 );
516 if(
cryptStatusOK( status ) && protocolInfo->senderDNlength > 0 )
518 status = sMemGetDataBlock( stream, &protocolInfo->senderDNPtr,
519 protocolInfo->senderDNlength );
521 status = readUniversal( stream );
528 status = readUniversal( stream );
531 status = readUniversal( stream );
533 status = readUniversal( stream );
538 "Invalid DN information in PKI header" ) );
547 "Message was sent without integrity protection" ) );
549 status = readProtectionAlgo( stream, protocolInfo );
554 "Invalid integrity protection information in PKI "
559 status = readUserID( stream, protocolInfo, isServerInitialMessage );
564 "Invalid PKI user ID in PKI header" ) );
571 if( isServerInitialMessage && protocolInfo->userIDsize <= 0 )
575 "Missing PKI user ID in PKI header" ) );
579 readUniversal( stream );
593 "Missing transaction ID in PKI header" ) );
595 status = readTransactionID( stream, protocolInfo,
596 isServerInitialMessage );
603 "Returned message transaction ID doesn't match our "
605 "Invalid transaction ID in PKI header" ) );
615 if( stell( stream ) < endPos && \
620 &protocolInfo->recipNonceSize,
627 "Invalid sender nonce in PKI header" ) );
630 if( stell( stream ) < endPos && \
634 status = readUniversal( stream );
640 "Invalid recipient nonce in PKI header" ) );
646 protocolInfo->headerRead =
TRUE;
650 if( stell( stream ) < endPos && \
653 status = readUniversal( stream );
657 if( stell( stream ) < endPos && \
660 status = readGeneralInfo( stream, protocolInfo );
665 "Invalid generalInfo information in PKI header" ) );
695 ERROR_INFO *errorInfo = &sessionInfoPtr->errorInfo;
697 READMESSAGE_FUNCTION readMessageFunction;
699 const BOOLEAN isServerInitialMessage = \
714 protocolInfo->isServer ?
"SVR" :
"CLI", messageType ));
717 sMemConnect( &stream, sessionInfoPtr->receiveBuffer,
718 sessionInfoPtr->receiveBufEnd );
719 status = readSequence( &stream, NULL );
722 protPartStart = stell( &stream );
724 isServerInitialMessage );
728 sMemDisconnect( &stream );
731 ENSURES( protocolInfo->transIDsize > 0 && \
736 if( protocolInfo->isCryptlib )
761 if( protocolInfo->useMACreceive )
768 if( protocolInfo->userIDsize <= 0 && \
769 !( protocolInfo->isCryptlib && \
772 sMemDisconnect( &stream );
773 protocolInfo->headerRead =
FALSE;
776 "Missing PKI user ID for MAC authentication of PKI "
789 if( protocolInfo->certIDsize <= 0 && \
791 protocolInfo->senderDNlength <= 0 ) && \
792 !( protocolInfo->isCryptlib && \
795 sMemDisconnect( &stream );
796 protocolInfo->headerRead =
FALSE;
799 "Missing certificate ID for signature authentication "
800 "of PKI messages" ) );
809 if( protocolInfo->userIDchanged )
811 status = updateUserID( sessionInfoPtr, protocolInfo,
812 isServerInitialMessage,
813 protocolInfo->useMACreceive );
816 sMemDisconnect( &stream );
820 if( protocolInfo->certIDchanged )
822 status = updateCertID( sessionInfoPtr, protocolInfo,
823 isServerInitialMessage );
826 sMemDisconnect( &stream );
838 tag = peekTag( &stream );
845 ENSURES( readMessageFunction != NULL );
847 status = readSequence( &stream, &length );
849 status = readMessageFunction( &stream, sessionInfoPtr,
852 sMemDisconnect( &stream );
865 protocolInfo->operation = messageType =
tag;
868 sMemDisconnect( &stream );
872 "Client sent invalid initial message type %d",
879 if( tag != messageType )
881 sMemDisconnect( &stream );
885 "Invalid message type, expected %d, got %d",
886 messageType, tag ) );
895 if( protocolInfo->useMACreceive )
897 status = updateMacInfo( sessionInfoPtr, protocolInfo, &stream );
900 sMemDisconnect( &stream );
907 status = readConstructed( &stream, &length, messageType );
910 bodyStart = stell( &stream );
911 status = sSkip( &stream, length );
915 sMemDisconnect( &stream );
919 "Invalid message body start" ) );
923 protPartSize = stell( &stream ) - protPartStart;
925 status = readConstructed( &stream, &integrityInfoLength,
929 status = sMemGetDataBlock( &stream, &integrityInfoPtr,
930 integrityInfoLength );
938 sMemDisconnect( &stream );
941 "Signature/MAC data is missing or truncated" ) );
943 if( tag ==
CTAG_PB_IR && !protocolInfo->useMACreceive )
948 sMemDisconnect( &stream );
952 "Received signed ir, should be MAC'd" ) );
954 if( tag ==
CTAG_PB_RR && protocolInfo->useMACreceive )
970 sMemDisconnect( &stream );
974 "Received MAC'd rr, should be signed" ) );
984 if( protocolInfo->useMACreceive )
986 status = checkMessageMAC( &stream, protocolInfo,
987 sessionInfoPtr->receiveBuffer + protPartStart,
991 sMemDisconnect( &stream );
994 "Bad message MAC" ) );
999 status = checkMessageSignature( protocolInfo,
1000 sessionInfoPtr->receiveBuffer + protPartStart,
1001 protPartSize, integrityInfoPtr, integrityInfoLength,
1002 sessionInfoPtr->iAuthInContext );
1005 sMemDisconnect( &stream );
1012 "Message signature key doesn't match our "
1013 "signature check key, signature can't be "
1018 "Bad message signature" ) );
1021 sseek( &stream, bodyStart );
1028 sMemDisconnect( &stream );
1033 status = readSequence( &stream, &length );
1039 if( readMessageFunction == NULL )
1041 DEBUG_DIAG((
"No message-read function available for message "
1042 "type %d", messageType ));
1046 "Unexpected message type %d", messageType ) );
1048 status = readMessageFunction( &stream, sessionInfoPtr, protocolInfo,
1049 messageType, length );
1050 sMemDisconnect( &stream );