@@ -578,6 +578,9 @@ static HandshakeInfo* HandshakeInfoNew(void* heap)
578578 newHs->encryptId = ID_NONE;
579579 newHs->macId = ID_NONE;
580580 newHs->blockSz = MIN_BLOCK_SZ;
581+ newHs->peerEncryptId = ID_NONE;
582+ newHs->peerMacId = ID_NONE;
583+ newHs->peerBlockSz = MIN_BLOCK_SZ;
581584 newHs->eSz = (word32)sizeof(newHs->e);
582585 newHs->xSz = (word32)sizeof(newHs->x);
583586#ifndef WOLFSSH_NO_DH_GEX_SHA256
@@ -4422,6 +4425,22 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
44224425 ret = WS_MATCH_ENC_ALGO_E;
44234426 }
44244427 }
4428+ if (ret == WS_SUCCESS) {
4429+ ssh->handshake->peerEncryptId = algoId;
4430+ ssh->handshake->peerAeadMode = AeadModeForId(algoId);
4431+ ssh->handshake->peerBlockSz = BlockSzForId(algoId);
4432+ ssh->handshake->peerKeys.encKeySz = KeySzForId(algoId);
4433+ if (!ssh->handshake->peerAeadMode) {
4434+ ssh->handshake->peerKeys.ivSz = ssh->handshake->peerBlockSz;
4435+ }
4436+ else {
4437+ /* Reaching here requires peerAeadMode==1, which requires an AEAD
4438+ * cipher ID, which requires WOLFSSH_NO_AES_GCM to be unset, which
4439+ * means WOLFSSH_NO_AEAD is also unset (see internal.h). */
4440+ ssh->handshake->peerKeys.ivSz = AEAD_NONCE_SZ;
4441+ ssh->handshake->peerMacSz = ssh->handshake->peerBlockSz;
4442+ }
4443+ }
44254444
44264445 /* Enc Algorithms - Server to Client */
44274446 if (ret == WS_SUCCESS) {
@@ -4430,7 +4449,13 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
44304449 ret = GetNameList(list, &listSz, buf, len, &begin);
44314450 }
44324451 if (ret == WS_SUCCESS) {
4433- algoId = MatchIdLists(side, list, listSz, &algoId, 1);
4452+ cannedAlgoNamesSz = AlgoListSz(ssh->algoListCipher);
4453+ cannedListSz = (word32)sizeof(cannedList);
4454+ ret = GetNameListRaw(cannedList, &cannedListSz,
4455+ (const byte*)ssh->algoListCipher, cannedAlgoNamesSz);
4456+ }
4457+ if (ret == WS_SUCCESS) {
4458+ algoId = MatchIdLists(side, list, listSz, cannedList, cannedListSz);
44344459 if (algoId == ID_UNKNOWN) {
44354460 WLOG(WS_LOG_DEBUG, "Unable to negotiate Encryption Algo S2C");
44364461 ret = WS_MATCH_ENC_ALGO_E;
@@ -4440,21 +4465,14 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
44404465 ssh->handshake->encryptId = algoId;
44414466 ssh->handshake->aeadMode = AeadModeForId(algoId);
44424467 ssh->handshake->blockSz = BlockSzForId(algoId);
4443- ssh->handshake->keys.encKeySz =
4444- ssh->handshake->peerKeys.encKeySz =
4445- KeySzForId(algoId);
4468+ ssh->handshake->keys.encKeySz = KeySzForId(algoId);
44464469 if (!ssh->handshake->aeadMode) {
4447- ssh->handshake->keys.ivSz =
4448- ssh->handshake->peerKeys.ivSz =
4449- ssh->handshake->blockSz;
4470+ ssh->handshake->keys.ivSz = ssh->handshake->blockSz;
44504471 }
44514472 else {
4452- #ifndef WOLFSSH_NO_AEAD
4453- ssh->handshake->keys.ivSz =
4454- ssh->handshake->peerKeys.ivSz =
4455- AEAD_NONCE_SZ;
4473+ /* Same invariant: aeadMode==1 implies !WOLFSSH_NO_AEAD. */
4474+ ssh->handshake->keys.ivSz = AEAD_NONCE_SZ;
44564475 ssh->handshake->macSz = ssh->handshake->blockSz;
4457- #endif
44584476 }
44594477 }
44604478
@@ -4464,7 +4482,7 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
44644482 listSz = (word32)sizeof(list);
44654483 ret = GetNameList(list, &listSz, buf, len, &begin);
44664484 }
4467- if (ret == WS_SUCCESS && !ssh->handshake->aeadMode ) {
4485+ if (ret == WS_SUCCESS && !ssh->handshake->peerAeadMode ) {
44684486 cannedAlgoNamesSz = AlgoListSz(ssh->algoListMac);
44694487 cannedListSz = (word32)sizeof(cannedList);
44704488 ret = GetNameListRaw(cannedList, &cannedListSz,
@@ -4476,6 +4494,11 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
44764494 WLOG(WS_LOG_DEBUG, "Unable to negotiate MAC Algo C2S");
44774495 ret = WS_MATCH_MAC_ALGO_E;
44784496 }
4497+ else {
4498+ ssh->handshake->peerMacId = algoId;
4499+ ssh->handshake->peerMacSz = MacSzForId(algoId);
4500+ ssh->handshake->peerKeys.macKeySz = KeySzForId(algoId);
4501+ }
44794502 }
44804503 }
44814504
@@ -4486,17 +4509,21 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
44864509 ret = GetNameList(list, &listSz, buf, len, &begin);
44874510 }
44884511 if (ret == WS_SUCCESS && !ssh->handshake->aeadMode) {
4489- algoId = MatchIdLists(side, list, listSz, &algoId, 1);
4490- if (algoId == ID_UNKNOWN) {
4491- WLOG(WS_LOG_DEBUG, "Unable to negotiate MAC Algo S2C");
4492- ret = WS_MATCH_MAC_ALGO_E;
4493- }
4494- else {
4495- ssh->handshake->macId = algoId;
4496- ssh->handshake->macSz = MacSzForId(algoId);
4497- ssh->handshake->keys.macKeySz =
4498- ssh->handshake->peerKeys.macKeySz =
4499- KeySzForId(algoId);
4512+ cannedAlgoNamesSz = AlgoListSz(ssh->algoListMac);
4513+ cannedListSz = (word32)sizeof(cannedList);
4514+ ret = GetNameListRaw(cannedList, &cannedListSz,
4515+ (const byte*)ssh->algoListMac, cannedAlgoNamesSz);
4516+ if (ret == WS_SUCCESS) {
4517+ algoId = MatchIdLists(side, list, listSz, cannedList, cannedListSz);
4518+ if (algoId == ID_UNKNOWN) {
4519+ WLOG(WS_LOG_DEBUG, "Unable to negotiate MAC Algo S2C");
4520+ ret = WS_MATCH_MAC_ALGO_E;
4521+ }
4522+ else {
4523+ ssh->handshake->macId = algoId;
4524+ ssh->handshake->macSz = MacSzForId(algoId);
4525+ ssh->handshake->keys.macKeySz = KeySzForId(algoId);
4526+ }
45004527 }
45014528 }
45024529
@@ -6238,11 +6265,11 @@ static int DoNewKeys(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
62386265 }
62396266
62406267 if (ret == WS_SUCCESS) {
6241- ssh->peerEncryptId = ssh->handshake->encryptId ;
6242- ssh->peerMacId = ssh->handshake->macId ;
6243- ssh->peerBlockSz = ssh->handshake->blockSz ;
6244- ssh->peerMacSz = ssh->handshake->macSz ;
6245- ssh->peerAeadMode = ssh->handshake->aeadMode ;
6268+ ssh->peerEncryptId = ssh->handshake->peerEncryptId ;
6269+ ssh->peerMacId = ssh->handshake->peerMacId ;
6270+ ssh->peerBlockSz = ssh->handshake->peerBlockSz ;
6271+ ssh->peerMacSz = ssh->handshake->peerMacSz ;
6272+ ssh->peerAeadMode = ssh->handshake->peerAeadMode ;
62466273 WMEMCPY(&ssh->peerKeys, &ssh->handshake->peerKeys, sizeof(Keys));
62476274
62486275 switch (ssh->peerEncryptId) {
0 commit comments