📄 sslrec.c
字号:
/* *********************************************************************
File: sslrec.c
SSLRef 3.0 Final -- 11/19/96
Copyright (c)1996 by Netscape Communications Corp.
By retrieving this software you are bound by the licensing terms
disclosed in the file "LICENSE.txt". Please read it, and if you don't
accept the terms, delete this software.
SSLRef 3.0 was developed by Netscape Communications Corp. of Mountain
View, California <http://home.netscape.com/> and Consensus Development
Corporation of Berkeley, California <http://www.consensus.com/>.
*********************************************************************
File: sslrec.c Encryption, decryption and MACing of data
All the transformations which occur between plaintext and the
secured, authenticated data that goes out over the wire. Also,
detects incoming SSL 2 hello messages and hands them off to the SSL 2
record layer (and hands all SSL 2 reading & writing off to the SSL 2
layer).
****************************************************************** */
#ifndef _SSL_H_
#include "ssl.h"
#endif
#ifndef _SSLREC_H_
#include "sslrec.h"
#endif
#ifndef _SSLALLOC_H_
#include "sslalloc.h"
#endif
#ifndef _CRYPTYPE_H_
#include "cryptype.h"
#endif
#ifndef _SSLCTX_H_
#include "sslctx.h"
#endif
#ifndef _SSLALERT_H_
#include "sslalert.h"
#endif
#ifndef _SSL2_H_
#include "ssl2.h"
#endif
#include <string.h>
static SSLErr DecryptSSLRecord(uint8 type, SSLBuffer *payload, SSLContext *ctx);
static SSLErr VerifyMAC(uint8 type, SSLBuffer data, uint8 *compareMAC, SSLContext *ctx);
static SSLErr ComputeMAC(uint8 type, SSLBuffer data, SSLBuffer mac, uint64 seqNo, SSLBuffer secret, HashReference *macHash, SSLContext *ctx);
static uint8* SSLEncodeUInt64(uint8 *p, uint64 value);
/* ReadSSLRecord
* Attempt to read & decrypt an SSL record.
*/
SSLErr
SSLReadRecord(SSLRecord *rec, SSLContext *ctx)
{ SSLErr err;
uint32 len, contentLen;
uint8 *progress;
SSLBuffer readData, cipherFragment;
if (!ctx->partialReadBuffer.data || ctx->partialReadBuffer.length < 5)
{ if (ctx->partialReadBuffer.data)
if ((err = SSLFreeBuffer(&ctx->partialReadBuffer, &ctx->sysCtx)) != 0)
{ SSLFatalSessionAlert(alert_close_notify, ctx);
return ERR(err);
}
if ((err = SSLAllocBuffer(&ctx->partialReadBuffer, DEFAULT_BUFFER_SIZE, &ctx->sysCtx)) != 0)
{ SSLFatalSessionAlert(alert_close_notify, ctx);
return ERR(err);
}
}
if (ctx->protocolVersion == SSL_Version_Undetermined ||
ctx->protocolVersion == SSL_Version_3_0_With_2_0_Hello)
if (ctx->amountRead < 1)
{ readData.length = 1 - ctx->amountRead;
readData.data = ctx->partialReadBuffer.data + ctx->amountRead;
len = readData.length;
if (ERR(err = ctx->ioCtx.read(readData, &len, ctx->ioCtx.ioRef)) != 0)
{ if (err == SSLWouldBlockErr)
ctx->amountRead += len;
else
SSLFatalSessionAlert(alert_close_notify, ctx);
return err;
}
ctx->amountRead += len;
}
/* In undetermined cases, if the first byte isn't in the range of SSL 3.0
* record types, this is an SSL 2.0 record
*/
switch (ctx->protocolVersion)
{ case SSL_Version_Undetermined:
case SSL_Version_3_0_With_2_0_Hello:
if (ctx->partialReadBuffer.data[0] < SSL_smallest_3_0_type ||
ctx->partialReadBuffer.data[0] > SSL_largest_3_0_type)
return SSL2ReadRecord(rec, ctx);
else
break;
case SSL_Version_2_0:
return SSL2ReadRecord(rec, ctx);
default:
break;
}
if (ctx->amountRead < 5)
{ readData.length = 5 - ctx->amountRead;
readData.data = ctx->partialReadBuffer.data + ctx->amountRead;
len = readData.length;
if (ERR(err = ctx->ioCtx.read(readData, &len, ctx->ioCtx.ioRef)) != 0)
{ if (err == SSLWouldBlockErr)
ctx->amountRead += len;
else
SSLFatalSessionAlert(alert_close_notify, ctx);
return err;
}
ctx->amountRead += len;
}
ASSERT(ctx->amountRead >= 5);
progress = ctx->partialReadBuffer.data;
rec->contentType = *progress++;
if (rec->contentType < SSL_smallest_3_0_type ||
rec->contentType > SSL_largest_3_0_type)
return ERR(SSLProtocolErr);
rec->protocolVersion = (SSLProtocolVersion)SSLDecodeInt(progress, 2);
progress += 2;
contentLen = SSLDecodeInt(progress, 2);
progress += 2;
if (contentLen > (16384 + 2048)) /* Maximum legal length of an SSLCipherText payload */
{ SSLFatalSessionAlert(alert_unexpected_message, ctx);
return ERR(SSLProtocolErr);
}
if (ctx->partialReadBuffer.length < 5 + contentLen)
{ if ((err = SSLReallocBuffer(&ctx->partialReadBuffer, 5 + contentLen, &ctx->sysCtx)) != 0)
{ SSLFatalSessionAlert(alert_close_notify, ctx);
return ERR(err);
}
}
if (ctx->amountRead < 5 + contentLen)
{ readData.length = 5 + contentLen - ctx->amountRead;
readData.data = ctx->partialReadBuffer.data + ctx->amountRead;
len = readData.length;
if (ERR(err = ctx->ioCtx.read(readData, &len, ctx->ioCtx.ioRef)) != 0)
{ if (err == SSLWouldBlockErr)
ctx->amountRead += len;
else
SSLFatalSessionAlert(alert_close_notify, ctx);
return err;
}
ctx->amountRead += len;
}
ASSERT(ctx->amountRead >= 5 + contentLen);
cipherFragment.data = ctx->partialReadBuffer.data + 5;
cipherFragment.length = contentLen;
/* Decrypt the payload & check the MAC, modifying the length of the buffer to indicate the
* amount of plaintext data after adjusting for the block size and removing the MAC
* (this function generates its own alerts)
*/
if ((err = DecryptSSLRecord(rec->contentType, &cipherFragment, ctx)) != 0)
return err;
/* We appear to have sucessfully received a record; increment the sequence number */
IncrementUInt64(&ctx->readCipher.sequenceNum);
/* Allocate a buffer to return the plaintext in and return it */
if ((err = SSLAllocBuffer(&rec->contents, cipherFragment.length, &ctx->sysCtx)) != 0)
{ SSLFatalSessionAlert(alert_close_notify, ctx);
return ERR(err);
}
memcpy(rec->contents.data, cipherFragment.data, cipherFragment.length);
ctx->amountRead = 0; /* We've used all the data in the cache */
return SSLNoErr;
}
/* SSLWriteRecord does not send alerts on failure, out of the assumption/fear
* that this might result in a loop (since sending an alert causes SSLWriteRecord
* to be called).
*/
SSLErr
SSLWriteRecord(SSLRecord rec, SSLContext *ctx)
{ SSLErr err;
int padding = 0, i;
WaitingRecord *out, *queue;
SSLBuffer buf, payload, secret, mac;
uint8 *progress;
uint16 payloadSize,blockSize;
if (rec.protocolVersion == SSL_Version_2_0)
return SSL2WriteRecord(rec, ctx);
ASSERT(rec.protocolVersion == SSL_Version_3_0);
ASSERT(rec.contents.length <= 16384);
out = 0;
/* Allocate a WaitingRecord to store our ready-to-send record in */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -