/*
* Copyright (c) 2002 - 2003
* NetGroup, Politecnico di Torino (Italy)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Politecnico di Torino nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <config.h>
#ifdef HAVE_OPENSSL
#include <stdlib.h>
#include "sslutils.h"
#include "portability.h"
static const char *ssl_keyfile = ""; //!< file containing the private key in PEM format
static const char *ssl_certfile = ""; //!< file containing the server's certificate in PEM format
static const char *ssl_rootfile = ""; //!< file containing the list of CAs trusted by the client
// TODO: a way to set ssl_rootfile from the command line, or an envvar?
// Finish using an SSL handle; shut down the connection and free the
// handle.
void ssl_finish(SSL *ssl)
{
//
// We won't be using this again, so we can just send the
// shutdown alert and free up the handle, and have our
// caller close the socket.
//
// XXX - presumably, if the connection is shut down on
// our side, either our peer won't have a problem sending
// their shutdown alert or will not treat such a problem
// as an error. If this causes errors to be reported,
// fix that as appropriate.
//
SSL_shutdown(ssl);
SSL_free(ssl);
}
// Same return value as sock_send:
// 0 on OK, -1 on error but closed connection (-2).
int ssl_send(SSL *ssl, char const *buffer, int size, char *errbuf, size_t errbuflen)
{
int status = SSL_write(ssl, buffer, size);
if (status > 0)
{
// "SSL_write() will only return with success, when the complete contents (...) has been written."
return 0;
}
else
{
int ssl_err = SSL_get_error(ssl, status); // TODO: does it pop the error?
if (ssl_err == SSL_ERROR_ZERO_RETURN)
{
return -2;
}
else if (ssl_err == SSL_ERROR_SYSCALL)
{
#ifndef _WIN32
if (errno == ECONNRESET || errno == EPIPE) return -2;
#endif
}
snprintf(errbuf, errbuflen, "SSL_write(): %s",
ERR_error_string(ERR_get_error(), NULL));
return -1;
}
}
// Returns the number of bytes read, or -1 on syserror, or -2 on SSL error.
int ssl_recv(SSL *ssl, char *buffer, int size, char *errbuf, size_t errbuflen)
{
int status = SSL_read(ssl, buffer, size);
if (status <= 0)
{
int ssl_err = SSL_get_error(ssl, status);
if (ssl_err == SSL_ERROR_ZERO_RETURN)
{
return 0;
}
else if (ssl_err == SSL_ERROR_SYSCALL)
{
return -1;
}
else
{
// Should not happen
snprintf(errbuf, errbuflen, "SSL_read(): %s",
ERR_error_string(ERR_get_error(), NULL));
return -2;
}
}
else
{
return status;
}
}