Crypt-MatrixSSL
view release on metacpan or search on metacpan
matrixssl-1-8-6-open/examples/sslSocket.c view on Meta::CPAN
Reading handshake records should always return 0 bytes, we aren't
expecting any data yet.
*/
if (rc == 0) {
if (status == SSLSOCKET_EOF || status == SSLSOCKET_CLOSE_NOTIFY) {
goto error;
}
if (matrixSslHandshakeIsComplete(conn->ssl) == 0) {
goto readMore;
}
} else if (rc > 0) {
fprintf(stderr, "sslRead got %d data in sslDoHandshake %s\n", rc, buf);
goto readMore;
} else {
fprintf(stderr, "sslRead error in sslDoHandhake\n");
goto error;
}
return conn;
error:
sslFreeConnection(&conn);
return NULL;
}
/******************************************************************************/
/*
An example socket sslRead implementation that handles the ssl handshake
transparently. Caller passes in allocated buf and length.
Return codes are as follows:
-1 return code is an error. If a socket level error, error code is
contained in status parameter. If using a non-blocking socket
implementation the caller should check for non-fatal errors such as
WOULD_BLOCK before closing the connection. A zero value
in status indicates an error with this routine.
A positive integer return code is the number of bytes successfully read
into the supplied buffer. User can call sslRead again on the updated
buffer is there is more to be read.
0 return code indicates the read was successful, but there was no data
to be returned. If status is set to zero, this is a case internal
to the sslAccept and sslConnect functions that a handshake
message has been exchanged. If status is set to SOCKET_EOF
the connection has been closed by the other side.
*/
int sslRead(sslConn_t *cp, char *buf, int len, int *status)
{
int bytes, rc, remaining;
unsigned char error, alertLevel, alertDescription, performRead;
*status = 0;
if (cp->ssl == NULL || len <= 0) {
return -1;
}
/*
If inbuf is valid, then we have previously decoded data that must be
returned, return as much as possible. Once all buffered data is
returned, free the inbuf.
*/
if (cp->inbuf.buf) {
if (cp->inbuf.start < cp->inbuf.end) {
remaining = (int)(cp->inbuf.end - cp->inbuf.start);
bytes = (int)min(len, remaining);
memcpy(buf, cp->inbuf.start, bytes);
cp->inbuf.start += bytes;
return bytes;
}
free(cp->inbuf.buf);
cp->inbuf.buf = NULL;
}
/*
Pack the buffered socket data (if any) so that start is at zero.
*/
if (cp->insock.buf < cp->insock.start) {
if (cp->insock.start == cp->insock.end) {
cp->insock.start = cp->insock.end = cp->insock.buf;
} else {
memmove(cp->insock.buf, cp->insock.start, cp->insock.end - cp->insock.start);
cp->insock.end -= (cp->insock.start - cp->insock.buf);
cp->insock.start = cp->insock.buf;
}
}
/*
Read up to as many bytes as there are remaining in the buffer. We could
Have encrypted data already cached in conn->insock, but might as well read more
if we can.
*/
performRead = 0;
readMore:
if (cp->insock.end == cp->insock.start || performRead) {
performRead = 1;
bytes = recv(cp->fd, (char *)cp->insock.end,
(int)((cp->insock.buf + cp->insock.size) - cp->insock.end), MSG_NOSIGNAL);
if (bytes == SOCKET_ERROR) {
*status = getSocketError();
return -1;
}
if (bytes == 0) {
*status = SSLSOCKET_EOF;
return 0;
}
cp->insock.end += bytes;
}
/*
Define a temporary sslBuf
*/
cp->inbuf.start = cp->inbuf.end = cp->inbuf.buf = malloc(len);
cp->inbuf.size = len;
/*
Decode the data we just read from the socket
*/
decodeMore:
error = 0;
alertLevel = 0;
alertDescription = 0;
rc = matrixSslDecode(cp->ssl, &cp->insock, &cp->inbuf, &error, &alertLevel,
&alertDescription);
switch (rc) {
/*
Successfully decoded a record that did not return data or require a response.
*/
case SSL_SUCCESS:
return 0;
/*
Successfully decoded an application data record, and placed in tmp buf
*/
case SSL_PROCESS_DATA:
/*
Copy as much as we can from the temp buffer into the caller's buffer
and leave the remainder in conn->inbuf until the next call to read
It is possible that len > data in buffer if the encoded record
was longer than len, but the decoded record isn't!
*/
rc = (int)(cp->inbuf.end - cp->inbuf.start);
rc = min(rc, len);
memcpy(buf, cp->inbuf.start, rc);
cp->inbuf.start += rc;
return rc;
/*
We've decoded a record that requires a response into tmp
If there is no data to be flushed in the out buffer, we can write out
the contents of the tmp buffer. Otherwise, we need to append the data
to the outgoing data buffer and flush it out.
*/
case SSL_SEND_RESPONSE:
bytes = send(cp->fd, (char *)cp->inbuf.start,
(int)(cp->inbuf.end - cp->inbuf.start), MSG_NOSIGNAL);
if (bytes == SOCKET_ERROR) {
*status = getSocketError();
if (*status != WOULD_BLOCK) {
fprintf(stdout, "Socket send error: %d\n", *status);
goto readError;
}
*status = 0;
}
cp->inbuf.start += bytes;
if (cp->inbuf.start < cp->inbuf.end) {
/*
This must be a non-blocking socket since it didn't all get sent
out and there was no error. We want to finish the send here
simply because we are likely in the SSL handshake.
*/
setSocketBlock(cp->fd);
bytes = send(cp->fd, (char *)cp->inbuf.start,
(int)(cp->inbuf.end - cp->inbuf.start), MSG_NOSIGNAL);
if (bytes == SOCKET_ERROR) {
*status = getSocketError();
goto readError;
}
cp->inbuf.start += bytes;
socketAssert(cp->inbuf.start == cp->inbuf.end);
/*
Can safely set back to non-blocking because we wouldn't
have got here if this socket wasn't non-blocking to begin with.
*/
setSocketNonblock(cp->fd);
}
cp->inbuf.start = cp->inbuf.end = cp->inbuf.buf;
return 0;
/*
There was an error decoding the data, or encoding the out buffer.
There may be a response data in the out buffer, so try to send.
We try a single hail-mary send of the data, and then close the socket.
Since we're closing on error, we don't worry too much about a clean flush.
*/
case SSL_ERROR:
fprintf(stderr, "SSL: Closing on protocol error %d\n", error);
if (cp->inbuf.start < cp->inbuf.end) {
setSocketNonblock(cp->fd);
bytes = send(cp->fd, (char *)cp->inbuf.start,
(int)(cp->inbuf.end - cp->inbuf.start), MSG_NOSIGNAL);
}
goto readError;
/*
We've decoded an alert. The level and description passed into
matrixSslDecode are filled in with the specifics.
*/
case SSL_ALERT:
if (alertDescription == SSL_ALERT_CLOSE_NOTIFY) {
*status = SSLSOCKET_CLOSE_NOTIFY;
goto readZero;
}
fprintf(stderr, "SSL: Closing on client alert %d: %d\n",
alertLevel, alertDescription);
goto readError;
/*
We have a partial record, we need to read more data off the socket.
If we have a completely full conn->insock buffer, we'll need to grow it
here so that we CAN read more data when called the next time.
*/
case SSL_PARTIAL:
if (cp->insock.start == cp->insock.buf && cp->insock.end ==
(cp->insock.buf + cp->insock.size)) {
if (cp->insock.size > SSL_MAX_BUF_SIZE) {
goto readError;
}
cp->insock.size *= 2;
cp->insock.start = cp->insock.buf =
(unsigned char *)realloc(cp->insock.buf, cp->insock.size);
cp->insock.end = cp->insock.buf + (cp->insock.size / 2);
}
if (!performRead) {
performRead = 1;
free(cp->inbuf.buf);
cp->inbuf.buf = NULL;
goto readMore;
} else {
goto readZero;
}
/*
The out buffer is too small to fit the decoded or response
data. Increase the size of the buffer and call decode again
*/
case SSL_FULL:
cp->inbuf.size *= 2;
if (cp->inbuf.buf != (unsigned char*)buf) {
free(cp->inbuf.buf);
cp->inbuf.buf = NULL;
}
cp->inbuf.start = cp->inbuf.end = cp->inbuf.buf =
(unsigned char *)malloc(cp->inbuf.size);
goto decodeMore;
}
/*
We consolidated some of the returns here because we must ensure
that conn->inbuf is cleared if pointing at caller's buffer, otherwise
it will be freed later on.
*/
readZero:
if (cp->inbuf.buf == (unsigned char*)buf) {
cp->inbuf.buf = NULL;
}
return 0;
readError:
if (cp->inbuf.buf == (unsigned char*)buf) {
cp->inbuf.buf = NULL;
}
return -1;
}
/******************************************************************************/
/*
Example sslWrite functionality. Takes care of encoding the input buffer
and sending it out on the connection.
Return codes are as follows:
-1 return code is an error. If a socket level error, error code is
contained in status. If using a non-blocking socket
implementation the caller should check for non-fatal errors such as
WOULD_BLOCK before closing the connection. A zero value
in status indicates an error with this routine.
A positive integer return value indicates the number of bytes succesfully
written on the connection. Should always match the len parameter.
0 return code indicates the write must be called again with the same
parameters.
*/
int sslWrite(sslConn_t *cp, char *buf, int len, int *status)
{
int rc;
*status = 0;
/*
Pack the buffered socket data (if any) so that start is at zero.
*/
if (cp->outsock.buf < cp->outsock.start) {
if (cp->outsock.start == cp->outsock.end) {
cp->outsock.start = cp->outsock.end = cp->outsock.buf;
} else {
( run in 0.658 second using v1.01-cache-2.11-cpan-e93a5daba3e )