rv |= PK11_DigestFinal(md5, hashes->md5, &outLength, MD5_LENGTH); PORT_Assert(rv != SECSuccess || outLength == MD5_LENGTH); if (rv != SECSuccess) { ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); rv = SECFailure; goto loser; }  PRINT_BUF(60, (NULL, "MD5 outer: result", hashes->md5, MD5_LENGTH));  if (!isTLS) { PRINT_BUF(95, (NULL, "SHA outer: MAC Pad 2", mac_pad_2, mac_defs[mac_sha].pad_size)); PRINT_BUF(95, (NULL, "SHA outer: SHA inner", sha_inner, SHA1_LENGTH));  rv |= PK11_DigestBegin(sha); rv |= PK11_DigestKey(sha,spec->master_secret); rv |= PK11_DigestOp(sha, mac_pad_2, mac_defs[mac_sha].pad_size); rv |= PK11_DigestOp(sha, sha_inner, SHA1_LENGTH); } rv |= PK11_DigestFinal(sha, hashes->sha, &outLength, SHA1_LENGTH); PORT_Assert(rv != SECSuccess || outLength == SHA1_LENGTH); if (rv != SECSuccess) { ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); rv = SECFailure; goto loser; }  PRINT_BUF(60, (NULL, "SHA outer: result", hashes->sha, SHA1_LENGTH));  rv = SECSuccess;  loser: if (md5StateBuf) { if (PK11_RestoreContext(ssl3->hs.md5, md5StateBuf, md5StateLen) != SECSuccess) { ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); rv = SECFailure; } if (md5StateBuf != md5StackBuf) { PORT_ZFree(md5StateBuf, md5StateLen); } } if (shaStateBuf) { if (PK11_RestoreContext(ssl3->hs.sha, shaStateBuf, shaStateLen) != SECSuccess) { ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); rv = SECFailure; } if (shaStateBuf != shaStackBuf) { PORT_ZFree(shaStateBuf, shaStateLen); } }
ssl3_ComputeHandshakeHashes(sslSocket *     ss,                            ssl3CipherSpec *spec,   /* uses ->master_secret */			    SSL3Hashes *    hashes, /* output goes here. */			    uint32          sender){    ssl3State *   ssl3      = ss->ssl3;    PK11Context * md5;    PK11Context * sha       = NULL;    SECStatus     rv        = SECSuccess;    unsigned int  outLength;    PRBool        isTLS;    SSL3Opaque    md5_inner[MAX_MAC_LENGTH];    SSL3Opaque    sha_inner[MAX_MAC_LENGTH];    unsigned char s[4];    unsigned char md5StackBuf[256];    unsigned char shaStackBuf[512];    unsigned char *md5StateBuf = NULL;    unsigned char *shaStateBuf = NULL;    unsigned int  md5StateLen, shaStateLen;    PORT_Assert( ssl_HaveSSL3HandshakeLock(ss) );    isTLS = (PRBool)(spec->version > SSL_LIBRARY_VERSION_3_0);    if (!spec->master_secret) {    	PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HANDSHAKE);	return SECFailure;    }    md5StateBuf = PK11_SaveContextAlloc(ssl3->hs.md5, md5StackBuf,                                        sizeof md5StackBuf, &md5StateLen);    if (md5StateBuf == NULL) {	ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE);	goto loser;    }    md5 = ssl3->hs.md5;    shaStateBuf = PK11_SaveContextAlloc(ssl3->hs.sha, shaStackBuf,                                        sizeof shaStackBuf, &shaStateLen);    if (shaStateBuf == NULL) {	ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);	goto loser;    }    sha = ssl3->hs.sha;    if (!isTLS) {	/* compute hashes for SSL3. */	s[0] = (unsigned char)(sender >> 24);	s[1] = (unsigned char)(sender >> 16);	s[2] = (unsigned char)(sender >> 8);	s[3] = (unsigned char)sender;	if (sender != 0) {	    rv |= PK11_DigestOp(md5, s, 4);	    PRINT_BUF(95, (NULL, "MD5 inner: sender", s, 4));	}	PRINT_BUF(95, (NULL, "MD5 inner: MAC Pad 1", mac_pad_1, mac_defs[mac_md5].pad_size));	rv |= PK11_DigestKey(md5,spec->master_secret);	rv |= PK11_DigestOp(md5, mac_pad_1, mac_defs[mac_md5].pad_size);	rv |= PK11_DigestFinal(md5, md5_inner, &outLength, MD5_LENGTH);	PORT_Assert(rv != SECSuccess || outLength == MD5_LENGTH);	if (rv != SECSuccess) {	    ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE);	    rv = SECFailure;	    goto loser;	}	PRINT_BUF(95, (NULL, "MD5 inner: result", md5_inner, outLength));	if (sender != 0) {	    rv |= PK11_DigestOp(sha, s, 4);	    PRINT_BUF(95, (NULL, "SHA inner: sender", s, 4));	}	PRINT_BUF(95, (NULL, "SHA inner: MAC Pad 1", mac_pad_1, mac_defs[mac_sha].pad_size));	rv |= PK11_DigestKey(sha, spec->master_secret);	rv |= PK11_DigestOp(sha, mac_pad_1, mac_defs[mac_sha].pad_size);	rv |= PK11_DigestFinal(sha, sha_inner, &outLength, SHA1_LENGTH);	PORT_Assert(rv != SECSuccess || outLength == SHA1_LENGTH);	if (rv != SECSuccess) {	    ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);	    rv = SECFailure;	    goto loser;	}	PRINT_BUF(95, (NULL, "SHA inner: result", sha_inner, outLength));	PRINT_BUF(95, (NULL, "MD5 outer: MAC Pad 2", mac_pad_2, mac_defs[mac_md5].pad_size));	PRINT_BUF(95, (NULL, "MD5 outer: MD5 inner", md5_inner, MD5_LENGTH));	rv |= PK11_DigestBegin(md5);	rv |= PK11_DigestKey(md5, spec->master_secret);	rv |= PK11_DigestOp(md5, mac_pad_2, mac_defs[mac_md5].pad_size);	rv |= PK11_DigestOp(md5, md5_inner, MD5_LENGTH);    }    rv |= PK11_DigestFinal(md5, hashes->md5, &outLength, MD5_LENGTH);    PORT_Assert(rv != SECSuccess || outLength == MD5_LENGTH);    if (rv != SECSuccess) {	ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE);	rv = SECFailure;	goto loser;    }    PRINT_BUF(60, (NULL, "MD5 outer: result", hashes->md5, MD5_LENGTH));    if (!isTLS) {	PRINT_BUF(95, (NULL, "SHA outer: MAC Pad 2", mac_pad_2, mac_defs[mac_sha].pad_size));	PRINT_BUF(95, (NULL, "SHA outer: SHA inner", sha_inner, SHA1_LENGTH));	rv |= PK11_DigestBegin(sha);	rv |= PK11_DigestKey(sha,spec->master_secret);	rv |= PK11_DigestOp(sha, mac_pad_2, mac_defs[mac_sha].pad_size);	rv |= PK11_DigestOp(sha, sha_inner, SHA1_LENGTH);    }    rv |= PK11_DigestFinal(sha, hashes->sha, &outLength, SHA1_LENGTH);    PORT_Assert(rv != SECSuccess || outLength == SHA1_LENGTH);    if (rv != SECSuccess) {	ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);	rv = SECFailure;	goto loser;    }    PRINT_BUF(60, (NULL, "SHA outer: result", hashes->sha, SHA1_LENGTH));    rv = SECSuccess;loser:    if (md5StateBuf) {	if (PK11_RestoreContext(ssl3->hs.md5, md5StateBuf, md5StateLen)	     != SECSuccess) 	{	    ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE);	    rv = SECFailure;	}	if (md5StateBuf != md5StackBuf) {	    PORT_ZFree(md5StateBuf, md5StateLen);	}    }    if (shaStateBuf) {	if (PK11_RestoreContext(ssl3->hs.sha, shaStateBuf, shaStateLen)	     != SECSuccess) 	{	    ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);	    rv = SECFailure;	}	if (shaStateBuf != shaStackBuf) {	    PORT_ZFree(shaStateBuf, shaStateLen);	}    }    return rv;}