Esempio n. 1
0
/*
	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;
}
Esempio n. 2
0
int do_matrixssl_recv(FILE * fp)
{
	int rc = -1, ret = -1, in_buf_size = 0, out_buf_size = 0;
	sslBuf_t in, out;
	unsigned char error = 0, alertLevel = 0, alertDescription = 0;
	unsigned char *in_buf = NULL, *out_buf = NULL;;
	long more = 0;

	matrixssl_buf *pbuf = matrixssl_findbuf((int)fp);

	if (NULL == pbuf)
		return -1;

	in_buf_size = out_buf_size = SSL_MAX_PLAINTEXT_LEN;
	in_buf = (char *)malloc(SSL_MAX_PLAINTEXT_LEN);
	out_buf = (char *)malloc(SSL_MAX_PLAINTEXT_LEN);

	MATRIXSSL_RSTBUF(pbuf);

	while (1) {

		in.start = in.end = in.buf = in_buf;
		in.size = in_buf_size;
		memset(in.buf, 0, in_buf_size);

		out.buf = out.start = out.end = out_buf;
		out.size = out_buf_size;
		memset(out.buf, 0, out_buf_size);

		ret =
		    recv((int)fp, in.end, (int)((in.buf + in.size) - in.end),
			 0);
#ifdef DEBUG_MATRIXSSL
		printf("do_matrixssl_recv - %d bytes read from socket\n", ret);
#endif
		if (ret <= 0)
			break;

		in.end += ret;
		in.size = ret;

	      decodeMore:
		error = 0;
		alertLevel = 0;
		alertDescription = 0;
		rc = matrixSslDecode(pbuf->ssl, &in, &out, &error, &alertLevel,
				     &alertDescription);
#ifdef DEBUG_MATRIXSSL
		printf
		    ("do_matrixssl_recv - %d bytes in buffer, %d bytes out buffer\n",
		     in.end - in.start, out.end - out.start);
#endif

		switch (rc) {
/*
	Successfully decoded an application data record, and placed in out buf
*/
		case SSL_SUCCESS:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_SUCCESS\n");
#endif
			if (matrixSslHandshakeIsComplete(pbuf->ssl) == 0
			    && in.end <= in.start)
				continue;

			if (in.end > in.start)
				goto decodeMore;
			else
				continue;

		case SSL_PROCESS_DATA:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_PROCESS_DATA\n");
#endif
			MATRIXSSL_ADDRBUF(out.end - out.start, pbuf, out.start);
			out.start = out.end;

			ioctl((int)fp, FIONREAD, (unsigned long *)&more);

#ifdef DEBUG_MATRIXSSL
			fprintf(stderr,
				"SSL_PROCESS_DATA - received %d bytes, %d more to get\n",
				(int)pbuf->ssl_recv_cur, (int)more);
#endif

/*      if (more > 0){
        memmove(in.buf, in.end, in.size - (in.end - in.start));
	in.start = in.buf;
	in.end = in.buf + in.size - (in.end - in.start);
	in.size = in_buf_size;
      	ret = recv((int)fp, in.end, (int)(in.size - (in.end - in.start)), 0);   
	in.end += ret;
        in.size += ret; 
      }
*/
			if (in.end - in.start > 0) {
#ifdef DEBUG_MATRIXSSL
				fprintf(stderr,
					"SSL_PROCESS_DATA - in.size %d bytes, in.end - in.start %d bytes\n",
					in.size, in.end - in.start);
#endif
				goto decodeMore;
			} else
				ret = pbuf->ssl_recv_cur;

			goto matrixssl_recv_done;
/*
	We've decoded a record that requires a response
	The out buffer contains the encoded response that we must send back
	before decoding any more data
*/
		case SSL_SEND_RESPONSE:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_SEND_RESPONSE\n");
#endif
			while (out.start < out.end) {
				rc = send((int)fp, out.start,
					  (int)(out.end - out.start), 0);

				if (rc <= 0)
					goto matrixssl_recv_done;
				out.start += rc;
			}

			out.buf = out.start = out.end = out_buf;
			out.size = out_buf_size;
			memset(out.buf, 0, out_buf_size);
			goto decodeMore;

/*
	We have a partial record, need to read more data from socket and
	try decoding again.
*/
		case SSL_PARTIAL:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_PARTIAL\n");
#endif
			ioctl((int)fp, FIONREAD, (unsigned long *)&more);

			while (more > 0) {

				if (in_buf_size - (in.end - in.buf) <= more) {
					in.start = in.buf = in_buf =
					    (char *)realloc(in_buf,
							    in_buf_size + more);
					in.end = in.buf + in.size;
					in_buf_size += more;
				}
				ret =
				    recv((int)fp, in.end,
					 in_buf_size - (in.end - in.buf), 0);

#ifdef DEBUG_MATRIXSSL
				printf("SSL_PARTIAL - recv %d bytes\n", ret);
#endif

				if (ret <= 0)
					break;

				in.end += ret;
				//in.size += ret;
				ioctl((int)fp, FIONREAD,
				      (unsigned long *)&more);
			}

			goto decodeMore;
/*
	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:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_FULL\n");
#endif

			out_buf_size *= 2;
			out.buf = out.start = out.end = out_buf =
			    (char *)realloc(out_buf, out_buf_size);
			out.size = out_buf_size;

#ifdef DEBUG_MATRIXSSL
			printf("SSL_FULL - out_buf is %d bytes\n",
			       out_buf_size);
#endif

			goto decodeMore;
/*
	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:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_ERROR - error code is %d\n", error);
#endif
/*
	We've decoded an alert.  The level and description passed into
	matrixSslDecode are filled in with the specifics.
*/
		case SSL_ALERT:
#ifdef DEBUG_MATRIXSSL
			printf("SSL_ALERT\n");
#endif
			if (alertDescription != SSL_ALERT_CLOSE_NOTIFY) {
				fprintf(stderr,
					"Closing connection on alert level %d, description %d.\n",
					alertLevel, alertDescription);
			}

		default:
#ifdef DEBUG_MATRIXSSL
			printf("do_matrixssl_recv - return\n");
#endif
			MATRIXSSL_RSTBUF(pbuf);
			ret = -1;
			goto matrixssl_recv_done;
		}
	}

matrixssl_recv_done:
#ifdef DEBUG_MATRIXSSL
	printf("matrixssl_recv - returning %d\n", ret);
#endif
	pbuf->ssl_recv_cur = 0;
	free(out_buf);
	free(in_buf);
	return ret;
}
Esempio n. 3
0
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 (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;
  }
  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;
    }
  }
  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;
  }
  cp->inbuf.start = cp->inbuf.end = cp->inbuf.buf = malloc(len);
  cp->inbuf.size = len;
 decodeMore:
  error = 0;
  alertLevel = 0;
  alertDescription = 0;
  
  rc = matrixSslDecode(cp->ssl, &cp->insock, &cp->inbuf, &error, &alertLevel, 
		       &alertDescription);
  switch (rc) {
  case SSL_SUCCESS:
    return 0;
  case SSL_PROCESS_DATA:
    rc = (int)(cp->inbuf.end - cp->inbuf.start);
    rc = min(rc, len);
    memcpy(buf, cp->inbuf.start, rc);
    cp->inbuf.start += rc;
    return rc;
  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) {
      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);
      setSocketNonblock(cp->fd);
    }
    cp->inbuf.start = cp->inbuf.end = cp->inbuf.buf;
    return 0;
  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;
  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;
  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;
    }
  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;
  }
 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;
}
Esempio n. 4
0
static int _ssl_read(SSL *ssl, char *buf, int len) {
  int bytes, rc, remaining;
  unsigned char error, alertLevel, alertDescription, performRead;

#if(_debug_)
  syslog(LOG_DEBUG, "MatrixSSL %s", __FUNCTION__);
#endif

  ssl->status = 0;

  if (ssl->ssl == NULL || len <= 0) {
#if(_debug_)
    syslog(LOG_DEBUG, "MatrixSSL %s null", __FUNCTION__);
#endif
    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 (ssl->inbuf.buf) {
    if (ssl->inbuf.start < ssl->inbuf.end) {
      remaining = (int)(ssl->inbuf.end - ssl->inbuf.start);
      bytes = (int)min(len, remaining);
      memcpy(buf, ssl->inbuf.start, bytes);
      ssl->inbuf.start += bytes;
      return bytes;
    }
    free(ssl->inbuf.buf);
    ssl->inbuf.buf = NULL;
  }
  /*
    Pack the buffered socket data (if any) so that start is at zero.
  */
  if (ssl->insock.buf < ssl->insock.start) {
    if (ssl->insock.start == ssl->insock.end) {
      ssl->insock.start = ssl->insock.end = ssl->insock.buf;
    } else {
      memmove(ssl->insock.buf, ssl->insock.start, ssl->insock.end - ssl->insock.start);
      ssl->insock.end -= (ssl->insock.start - ssl->insock.buf);
      ssl->insock.start = ssl->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 (ssl->insock.end == ssl->insock.start || performRead) {
    performRead = 1;
    bytes = recv(ssl->fd, (char *)ssl->insock.end,
		 (int)((ssl->insock.buf + ssl->insock.size) - ssl->insock.end), MSG_NOSIGNAL);
    if (bytes == -1) {
      ssl->status = errno;

      syslog(LOG_WARNING, "%s: recv()", strerror(errno));

      if (errno == EWOULDBLOCK || errno == EAGAIN)
	return 0;

      return -1;
    }
    if (bytes == 0) {
      ssl->status = SSL_SOCKET_EOF;
      return 0;
    }
    ssl->insock.end += bytes;
  }
  /*
    Define a temporary sslBuf
  */
  syslog(LOG_DEBUG, "SSL buffer sized %d", len);
  ssl->inbuf.start = ssl->inbuf.end = ssl->inbuf.buf = (unsigned char *)malloc(len);
  ssl->inbuf.size = len;
  /*
    Decode the data we just read from the socket
  */
decodeMore:
  error = 0;
  alertLevel = 0;
  alertDescription = 0;

  rc = matrixSslDecode(ssl->ssl, &ssl->insock, &ssl->inbuf, &error, &alertLevel,
		       &alertDescription);
  switch (rc) {
    /*
      Successfully decoded a record that did not return data or require a response.
    */
    case SSL_SUCCESS:
      syslog(LOG_DEBUG, "SSL_SUCCESS");
      return 0;
      /*
        Successfully decoded an application data record, and placed in tmp buf
      */
    case SSL_PROCESS_DATA:
      syslog(LOG_DEBUG, "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)(ssl->inbuf.end - ssl->inbuf.start);
      rc = min(rc, len);
      memcpy(buf, ssl->inbuf.start, rc);
      ssl->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:
      syslog(LOG_DEBUG, "SSL_SEND_RESPONSE");
      bytes = send(ssl->fd, (char *)ssl->inbuf.start,
                   (int)(ssl->inbuf.end - ssl->inbuf.start), MSG_NOSIGNAL);
      if (bytes == -1) {
        ssl->status = errno;
        if (ssl->status != EAGAIN) {
          goto readError;
        }
        ssl->status = 0;
      }
      ssl->inbuf.start += bytes;
      if (ssl->inbuf.start < ssl->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.
        */
        _ssl_setSocketBlock(ssl->fd);
        bytes = send(ssl->fd, (char *)ssl->inbuf.start,
                     (int)(ssl->inbuf.end - ssl->inbuf.start), MSG_NOSIGNAL);
        if (bytes == -1) {
          ssl->status = errno;
          goto readError;
        }
        ssl->inbuf.start += bytes;
        /*
          Can safely set back to non-blocking because we wouldn't
          have got here if this socket wasn't non-blocking to begin with.
        */
        _ssl_setSocketNonblock(ssl->fd);
      }
      ssl->inbuf.start = ssl->inbuf.end = ssl->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:
      syslog(LOG_DEBUG, "SSL_ERROR");
      syslog(LOG_DEBUG, "ssl error");
      if (ssl->inbuf.start < ssl->inbuf.end) {
        _ssl_setSocketNonblock(ssl->fd);
        bytes = send(ssl->fd, (char *)ssl->inbuf.start,
                     (int)(ssl->inbuf.end - ssl->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:
      syslog(LOG_DEBUG, "SSL_ALERT");
      syslog(LOG_DEBUG, "ssl alert");
      if (alertDescription == SSL_ALERT_CLOSE_NOTIFY) {
        ssl->status = SSL_SOCKET_CLOSE_NOTIFY;
        goto readZero;
      }
      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:
      syslog(LOG_DEBUG, "SSL_PARTIAL");
      if (ssl->insock.start == ssl->insock.buf && ssl->insock.end ==
          (ssl->insock.buf + ssl->insock.size)) {
        if (ssl->insock.size > SSL_MAX_BUF_SIZE) {
          goto readError;
        }
        ssl->insock.size *= 2;
        ssl->insock.start = ssl->insock.buf =
            (unsigned char *)realloc(ssl->insock.buf, ssl->insock.size);
        ssl->insock.end = ssl->insock.buf + (ssl->insock.size / 2);
      }
      if (!performRead) {
        performRead = 1;
        free(ssl->inbuf.buf);
        ssl->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:
      syslog(LOG_DEBUG, "SSL_FULL");
      ssl->inbuf.size *= 2;
      if (ssl->inbuf.buf != (unsigned char*)buf) {
        free(ssl->inbuf.buf);
        ssl->inbuf.buf = NULL;
      }
      ssl->inbuf.start = ssl->inbuf.end = ssl->inbuf.buf =
          (unsigned char *)malloc(ssl->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 (ssl->inbuf.buf == (unsigned char*)buf) {
    ssl->inbuf.buf = NULL;
  }
  return 0;
readError:
  if (ssl->inbuf.buf == (unsigned char*)buf) {
    ssl->inbuf.buf = NULL;
  }
  return -1;
}
Esempio n. 5
0
/*
	Caller has received data from the network and is notifying the SSL layer
 */
int32 matrixSslReceivedData(ssl_t *ssl, uint32 bytes, unsigned char **ptbuf,
							uint32 *ptlen)
{
	unsigned char	*buf, *prevBuf;
	int32			rc, decodeRet, size, sanity, decodeErr;
	uint32			processed, start, len, reqLen;
	unsigned char	alertLevel, alertDesc;
	unsigned char	*p;

	if (!ssl || !ptbuf || !ptlen) {
		return PS_ARG_FAIL;
	}

	psAssert(ssl->outsize > 0 && ssl->outbuf != NULL);
	psAssert(ssl->insize > 0 && ssl->inbuf != NULL);
	*ptbuf = NULL;
	*ptlen = 0;
	ssl->inlen += bytes;
	if (ssl->inlen == 0) {
		return PS_SUCCESS; /* Nothing to do.  Basically a poll */
	}
	/* This is outside the loop b/c we may want to parse within inbuf later */
	buf = ssl->inbuf;
DECODE_MORE:
	/* Parameterized sanity check to avoid infinite loops */
	if (matrixSslHandshakeIsComplete(ssl)) {
		/* Minimum possible record size once negotiated */
		sanity = ssl->inlen / (SSL3_HEADER_LEN + MD5_HASH_SIZE);
	} else {
		/* Even with an SSLv2 hello, the sanity check will let 1 pass through */
		sanity = ssl->inlen / (SSL3_HEADER_LEN + SSL3_HANDSHAKE_HEADER_LEN);
	}
	if (sanity-- < 0) {
		return PS_PROTOCOL_FAIL;	/* We've tried to decode too many times */
	}
	len = ssl->inlen;
	size = ssl->insize - (buf - ssl->inbuf);
	prevBuf = buf;
	decodeRet = matrixSslDecode(ssl, &buf, &len, size, &start, &reqLen,
						 &decodeErr, &alertLevel, &alertDesc);

/*
	Convenience for the cases that expect buf to have moved
		- calculate the number of encoded bytes that were decoded
*/
	processed = buf - prevBuf;
	rc = PS_PROTOCOL_FAIL;
	switch (decodeRet) {

	case MATRIXSSL_SUCCESS:

		ssl->inlen -= processed;
		if (ssl->inlen > 0) {
			psAssert(buf > ssl->inbuf);
/*
			Pack ssl->inbuf so there is immediate maximum room for potential
			outgoing data that needs to be written
*/
			memmove(ssl->inbuf, buf, ssl->inlen);
			buf = ssl->inbuf;
			goto DECODE_MORE;	/* More data in buffer to process */
		}
/*
		In this case, we've parsed a finished message and no additional data is
		available to parse. We let the client know the handshake is complete,
		which can be used as a trigger to begin for example a HTTP request.
*/
		if (!(ssl->bFlags & BFLAG_HS_COMPLETE)) {
			if (matrixSslHandshakeIsComplete(ssl)) {
				ssl->bFlags |= BFLAG_HS_COMPLETE;
#ifdef USE_CLIENT_SIDE_SSL
				matrixSslGetSessionId(ssl, ssl->sid);
#endif /* USE_CLIENT_SIDE_SSL */
				rc = MATRIXSSL_HANDSHAKE_COMPLETE;
			} else {
				rc = MATRIXSSL_REQUEST_RECV; /* Need to recv more handshake data */
			}
		} else {
#ifdef USE_DTLS
			rc = MATRIXSSL_REQUEST_RECV; /* Got FINISHED without CCS */
#else
			/* This is an error - we shouldn't get here */
#endif
		}
		break;

#ifdef USE_DTLS
	case DTLS_RETRANSMIT:
		/* Only request a resend if last record in buffer */
		ssl->inlen -= processed;
		if (ssl->inlen > 0) {
			psAssert(buf > ssl->inbuf);
/*
			Pack ssl->inbuf so there is immediate maximum room for potential
			outgoing data that needs to be written
*/
			memmove(ssl->inbuf, buf, ssl->inlen);
			buf = ssl->inbuf;
			goto DECODE_MORE;	/* More data in buffer to process */
		}

		/* Flight will be rebuilt when matrixDtlsGetOutdata is called while
		outbuf is empty.  This is the return case where we are actually
		seeing a repeat handshake message so we know something was lost in
		flight. */
		return MATRIXSSL_REQUEST_SEND;
#endif

	case SSL_SEND_RESPONSE:
#ifdef ENABLE_FALSE_START
		/*
			If FALSE START is supported, there may be APPLICATION_DATA directly
			following the FINISHED message, even though we haven't sent our
			CHANGE_CIPHER_SPEC or FINISHED message. This is signalled by buf
			having been moved forward, and our response being put directly into
			ssl->outbuf, rather than in buf (ssl->inbuf). Return a REQUEST_SEND
			so that the data in outbuf is flushed before the remaining data in
			ssl->inbuf is parsed.
		 */
		if ((ssl->flags & SSL_FLAGS_FALSE_START) && buf != prevBuf) {
			ssl->inlen -= processed;
			psAssert(ssl->inlen > 0);
			psAssert((uint32)ssl->inlen == start);
			psAssert(buf > ssl->inbuf);
			memmove(ssl->inbuf, buf, ssl->inlen);	/* Pack ssl->inbuf */
			buf = ssl->inbuf;
			return MATRIXSSL_REQUEST_SEND;
		}
#endif
		/*
			This must be handshake data (or alert) or we'd be in PROCESS_DATA
			so there is no way there is anything left inside inbuf to process.
			...so processed isn't valid because the output params are outbuf
			related and we simply reset inlen
		*/
		ssl->inlen = 0;

		/* If alert, close connection after sending */
		if (alertDesc != SSL_ALERT_NONE) {
			ssl->bFlags |= BFLAG_CLOSE_AFTER_SENT;
		}
		psAssert(prevBuf == buf);
		psAssert(ssl->insize >= (int32)len);
		psAssert(start == 0);
		psAssert(buf == ssl->inbuf);
		if (ssl->outlen > 0) {
			/* If data's in outbuf, append inbuf.  This is a corner case that
				can happen if application data is queued but then incoming data
				is processed and discovered to be a re-handshake request.
				matrixSslDecode will have constructed the response flight but
				we don't want to forget about the app data we haven't sent */
			if (ssl->outlen + (int32)len > ssl->outsize) {
				if ((p = psRealloc(ssl->outbuf, ssl->outlen + len,
						ssl->bufferPool)) == NULL) {
					return PS_MEM_FAIL;
				}
				ssl->outbuf = p;
				ssl->outsize = ssl->outlen + len;
			}
			memcpy(ssl->outbuf + ssl->outlen, ssl->inbuf, len);
			ssl->outlen += len;
		} else { /* otherwise, swap inbuf and outbuf */
			buf = ssl->outbuf; ssl->outbuf = ssl->inbuf; ssl->inbuf = buf;
			ssl->outlen = len;
			len = ssl->outsize; ssl->outsize = ssl->insize; ssl->insize = len;
			buf = ssl->inbuf;
			len = ssl->outlen;
		}
		rc = MATRIXSSL_REQUEST_SEND;	/* We queued data to send out */
		break;

	case MATRIXSSL_ERROR:
		if (decodeErr >= 0) {
			//printf("THIS SHOULD BE A NEGATIVE VALUE?\n");
		}
		return decodeErr; /* Will be a negative value */

	case SSL_ALERT:
		if (alertLevel == SSL_ALERT_LEVEL_FATAL) {
			psTraceIntInfo("Received FATAL alert %d.\n", alertDesc);
		} else {
			/* Closure notify is the normal case */
			if (alertDesc == SSL_ALERT_CLOSE_NOTIFY) {
				psTraceInfo("Normal SSL closure alert\n");
			} else {
				psTraceIntInfo("Received WARNING alert %d\n", alertDesc);
			}
		}
		/* Let caller access the 2 data bytes (severity and description) */
#ifdef USE_TLS_1_1
		/* Been ignoring the explicit IV up to this final return point. */
		if ((ssl->flags & SSL_FLAGS_READ_SECURE) &&
				(ssl->flags & SSL_FLAGS_TLS_1_1) &&	(ssl->enBlockSize > 1)) {
			prevBuf += ssl->enBlockSize;
		}
#endif /* USE_TLS_1_1 */
		psAssert(len == 2);
		*ptbuf = prevBuf;
		*ptlen = len;
		ssl->inlen -= processed;
		return MATRIXSSL_RECEIVED_ALERT;

	case SSL_PARTIAL:
		if (reqLen > SSL_MAX_BUF_SIZE) {
			return PS_MEM_FAIL;
		}
		if (reqLen > (uint32)ssl->insize) {
			if ((p = psRealloc(ssl->inbuf, reqLen, ssl->bufferPool)) == NULL) {
				return PS_MEM_FAIL;
			}
			ssl->inbuf = p;
			ssl->insize = reqLen;
			buf = ssl->inbuf;
			/* Don't need to change inlen */
		}


		rc = MATRIXSSL_REQUEST_RECV;	/* Expecting more data */
		break;

	/* We've got outgoing data that's larger than our buffer */
	case SSL_FULL:
		if (reqLen > SSL_MAX_BUF_SIZE) {
			return PS_MEM_FAIL;
		}
		/* We balk if we get a large handshake message */
		if (reqLen > SSL_MAX_PLAINTEXT_LEN &&
				!matrixSslHandshakeIsComplete(ssl)) {
			if (reqLen > SSL_MAX_PLAINTEXT_LEN) {
				return PS_MEM_FAIL;
			}
		}
		/*
			Can't envision any possible case where there is remaining data
			in inbuf to process and are getting SSL_FULL.
		*/
		ssl->inlen = 0;

		/* Grow inbuf */
		if (reqLen > (uint32)ssl->insize) {
			len = ssl->inbuf - buf;
			if ((p = psRealloc(ssl->inbuf, reqLen, ssl->bufferPool)) == NULL) {
				return PS_MEM_FAIL;
			}
			ssl->inbuf = p;
			ssl->insize = reqLen;
			buf = ssl->inbuf + len;
			/* Note we leave inlen untouched here */
		} else {
			psTraceInfo("Encoding error. Possible wrong flight messagSize\n");
			return PS_PROTOCOL_FAIL;	/* error in our encoding */
		}
		goto DECODE_MORE;

	case SSL_PROCESS_DATA:
/*
		Possible we received a finished message and app data in the same
		flight. In this case, the caller is not notified that the handshake
		is complete, but rather is notified that there is application data to
		process.
 */
		if (!(ssl->bFlags & BFLAG_HS_COMPLETE) &&
			matrixSslHandshakeIsComplete(ssl)) {
			ssl->bFlags |= BFLAG_HS_COMPLETE;
#ifdef USE_CLIENT_SIDE_SSL
			matrixSslGetSessionId(ssl, ssl->sid);
#endif /* USE_CLIENT_SIDE_SSL */
		}
/*
		 .	prevbuf points to start of unencrypted data
		 .	buf points to start of any remaining unencrypted data
		 .	start is length of remaining encrypted data yet to decode
		 .	len is length of unencrypted data ready for user processing
 */
		ssl->inlen -= processed;
		psAssert((uint32)ssl->inlen == start);

		/* Call user plaintext data handler */
#ifdef USE_TLS_1_1
		/* Been ignoring the explicit IV up to this final return point. */
		/* NOTE: This test has been on enBlockSize for a very long time but
			it looks like it should be on deBlockSize since this a decryption.
			Changed and added an assert to see if these ever don't match */
		psAssert(ssl->enBlockSize == ssl->deBlockSize);
		if ((ssl->flags & SSL_FLAGS_READ_SECURE) &&
				(ssl->flags & SSL_FLAGS_TLS_1_1) &&	(ssl->deBlockSize > 1)) {
			len -= ssl->deBlockSize;
			prevBuf += ssl->deBlockSize;
		}
		/* END enBlockSize to deBlockSize change */
#endif /* USE_TLS_1_1 */
		*ptbuf = prevBuf;
		*ptlen = len;
#ifdef USE_DTLS
/*
		This flag is used in conjuction with flightDone in the buffer
		management API set to determine whether we are still in a handshake
		state for attempting flight resends. If we are getting app data we
		know for certain we are out of the hs states. Testing HandshakeComplete
		is not enough because you never know if the other side got FINISHED.
*/
		if (ssl->flags & SSL_FLAGS_DTLS) {
			ssl->appDataExch = 1;
		}
#endif
#ifdef USE_ZLIB_COMPRESSION
		if (ssl->compression > 0) {
			return MATRIXSSL_APP_DATA_COMPRESSED;
		}
#endif
		return MATRIXSSL_APP_DATA;
	} /* switch decodeRet */

	if (ssl->inlen > 0 && (buf != ssl->inbuf)) {
		psAssert(0);
	}
/*
	Shrink inbuf to default size once inlen < default size, and we aren't
	expecting any more data in the buffer. If SSL_PARTIAL, don't shrink the
	buffer, since we expect to fill it up shortly.
*/
	if (decodeRet != SSL_PARTIAL) {
		revertToDefaultBufsize(ssl, SSL_INBUF);
	}

	return rc;
}
Esempio n. 6
0
void decode(void) {
  do {
    if (getdec) {
      len =decin.size -(decin.end -decin.buf);
      if ((len =read(fdstdin, decin.end, len)) < 0)
        fatal("unable to read from network");
      if (len == 0) {
        if (verbose > 2) info("eof reading from network");
        close(fdstdin); close(decpipe[1]);
        fdstdin =decpipe[1] =-1;
        return;
      }
      if (verbose > 2) infou("read bytes: ", len);
      bytesin +=len;
      decin.end +=len;
      getdec =0;
    }
    for (;;) {
      rc =matrixSslDecode(ssl, &decin, &decou, &error, &alvl, &adesc);
      if (rc == SSL_SUCCESS) break;
      if (rc == SSL_ERROR) {
        if (decou.end > decou.start)
          if (write(fdstdou, decou.start, decou.end -decou.start)
              != decou.end -decou.start) warn("unable to write to network");
        close(fdstdou); fdstdou =-1;
        fatals("ssl decode error", error);
      }
      if (rc == SSL_PROCESS_DATA) {
        if (write(decpipe[1], decou.start, decou.end -decou.start)
            != decou.end -decou.start) fatal("unable to write to prog");
        decou.start =decou.end =decou.buf;
        if (decin.start > decin.buf) { /* align */
          byte_copy(decin.buf, decin.end -decin.start, decin.start);
          decin.end -=decin.start -decin.buf;
          decin.start =decin.buf;
        }
        break;
      }
      if (rc == SSL_SEND_RESPONSE) {
        if (write(fdstdou, decou.start, decou.end -decou.start)
            != (decou.end -decou.start))
          fatal("unable to send ssl response");
        bytesou +=decou.end -decou.start;
        if (verbose > 2) info("sending ssl handshake response");
        if (verbose > 2) infou("write bytes: ", decou.end -decou.start);
        decou.start =decou.end =decou.buf;
        break;
      }
      if (rc == SSL_ALERT) {
        close(fdstdou); fdstdou =-1;
        if (adesc != SSL_ALERT_CLOSE_NOTIFY)
          fatals("ssl alert from peer", adesc);
        if (verbose > 2) info("ssl close notify from peer");
        finish();
        _exit(0);
      }
      if (rc == SSL_PARTIAL) {
        getdec =1;
        if (decin.size -(decin.end -decin.buf) < bufsizein) {
          if (! blowup(&decin, &decinbuf, bufsizein)) die_nomem();
          if (verbose > 1) infou("decode input buffer size: ", decin.size);
        }
        break;
      }
      if (rc == SSL_FULL) {
        if (! blowup(&decou, &decoubuf, bufsizeou)) die_nomem();
        if (verbose > 1) infou("decode output buffer size: ", decou.size);
        continue;
      }
    }
    if (decin.start == decin.end) {
      decin.start =decin.end =decin.buf;
      getdec =1;
    }
  } while (getdec == 0);
  if (handshake)
    if (matrixSslHandshakeIsComplete(ssl)) {
      handshake =0;
      if (verbose > 2) info("ssl handshake complete");
    }
}