Newer
Older
Daniel Stenberg
committed
};
while((biomem->data[j] == ' ') && (j<(size_t)biomem->length))
Daniel Stenberg
committed
j++;
if(j<(size_t)biomem->length)
ptr+=snprintf(ptr, sizeof(buf)-(ptr-buf), "%s%c", sep,
biomem->data[j]);
Daniel Stenberg
committed
}
infof(data, " %s\n", buf);
push_certinfo(data, certnum, namebuf, buf);
BIO_free(bio_out);
}
return 0; /* all is fine */
}
static void X509_signature(struct SessionHandle *data,
int numcert,
ASN1_STRING *sig)
{
char buf[1024];
char *ptr = buf;
int i;
for(i=0; i<sig->length; i++)
Daniel Stenberg
committed
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
ptr+=snprintf(ptr, sizeof(buf)-(ptr-buf), "%02x:", sig->data[i]);
infof(data, " Signature: %s\n", buf);
push_certinfo(data, numcert, "Signature", buf);
}
static void dumpcert(struct SessionHandle *data, X509 *x, int numcert)
{
BIO *bio_out = BIO_new(BIO_s_mem());
BUF_MEM *biomem;
/* this outputs the cert in this 64 column wide style with newlines and
-----BEGIN CERTIFICATE----- texts and more */
PEM_write_bio_X509(bio_out, x);
BIO_get_mem_ptr(bio_out, &biomem);
infof(data, "%s\n", biomem->data);
push_certinfo_len(data, numcert, "Cert", biomem->data, biomem->length);
BIO_free(bio_out);
}
static int init_certinfo(struct SessionHandle *data,
int num)
{
struct curl_certinfo *ci = &data->info.certs;
struct curl_slist **table;
Curl_ssl_free_certinfo(data);
ci->num_of_certs = num;
table = calloc((size_t)num, sizeof(struct curl_slist *));
Daniel Stenberg
committed
if(!table)
return 1;
ci->certinfo = table;
return 0;
}
/*
* This size was previously 512 which has been reported "too small" without
* any specifics, so it was enlarged to allow more data to get shown uncut.
* The "perfect" size is yet to figure out.
*/
#define CERTBUFFERSIZE 8192
Daniel Stenberg
committed
static CURLcode get_cert_chain(struct connectdata *conn,
struct ssl_connect_data *connssl)
{
STACK_OF(X509) *sk;
int i;
Daniel Stenberg
committed
struct SessionHandle *data = conn->data;
int numcerts;
bufp = malloc(CERTBUFFERSIZE);
if(!bufp)
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
sk = SSL_get_peer_cert_chain(connssl->handle);
if(!sk) {
free(bufp);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
numcerts = sk_X509_num(sk);
if(init_certinfo(data, numcerts)) {
free(bufp);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
infof(data, "--- Certificate chain\n");
for(i=0; i<numcerts; i++) {
Daniel Stenberg
committed
long value;
ASN1_INTEGER *num;
ASN1_TIME *certdate;
/* get the certs in "importance order" */
#if 0
X509 *x = sk_X509_value(sk, numcerts - i - 1);
#else
X509 *x = sk_X509_value(sk, i);
#endif
X509_CINF *cinf;
EVP_PKEY *pubkey=NULL;
int j;
char *ptr;
(void)x509_name_oneline(X509_get_subject_name(x), bufp, CERTBUFFERSIZE);
infof(data, "%2d Subject: %s\n", i, bufp);
push_certinfo(data, i, "Subject", bufp);
Daniel Stenberg
committed
(void)x509_name_oneline(X509_get_issuer_name(x), bufp, CERTBUFFERSIZE);
infof(data, " Issuer: %s\n", bufp);
push_certinfo(data, i, "Issuer", bufp);
Daniel Stenberg
committed
value = X509_get_version(x);
infof(data, " Version: %lu (0x%lx)\n", value+1, value);
snprintf(bufp, CERTBUFFERSIZE, "%lx", value);
push_certinfo(data, i, "Version", bufp); /* hex */
Daniel Stenberg
committed
num=X509_get_serialNumber(x);
if(num->length <= 4) {
Daniel Stenberg
committed
value = ASN1_INTEGER_get(num);
infof(data," Serial Number: %ld (0x%lx)\n", value, value);
snprintf(bufp, CERTBUFFERSIZE, "%lx", value);
Daniel Stenberg
committed
}
else {
Daniel Stenberg
committed
Daniel Stenberg
committed
*ptr++ = 0;
if(num->type == V_ASN1_NEG_INTEGER)
*ptr++='-';
for(j=0; (j<num->length) && (left>=4); j++) {
Daniel Stenberg
committed
/* TODO: length restrictions */
snprintf(ptr, 3, "%02x%c",num->data[j],
((j+1 == num->length)?'\n':':'));
ptr += 3;
Daniel Stenberg
committed
}
if(num->length)
infof(data," Serial Number: %s\n", bufp);
Daniel Stenberg
committed
else
Daniel Stenberg
committed
}
if(bufp[0])
push_certinfo(data, i, "Serial Number", bufp); /* hex */
Daniel Stenberg
committed
cinf = x->cert_info;
j = asn1_object_dump(cinf->signature->algorithm, bufp, CERTBUFFERSIZE);
Daniel Stenberg
committed
if(!j) {
infof(data, " Signature Algorithm: %s\n", bufp);
push_certinfo(data, i, "Signature Algorithm", bufp);
Daniel Stenberg
committed
}
certdate = X509_get_notBefore(x);
asn1_output(certdate, bufp, CERTBUFFERSIZE);
infof(data, " Start date: %s\n", bufp);
push_certinfo(data, i, "Start date", bufp);
Daniel Stenberg
committed
certdate = X509_get_notAfter(x);
asn1_output(certdate, bufp, CERTBUFFERSIZE);
infof(data, " Expire date: %s\n", bufp);
push_certinfo(data, i, "Expire date", bufp);
Daniel Stenberg
committed
j = asn1_object_dump(cinf->key->algor->algorithm, bufp, CERTBUFFERSIZE);
Daniel Stenberg
committed
if(!j) {
infof(data, " Public Key Algorithm: %s\n", bufp);
push_certinfo(data, i, "Public Key Algorithm", bufp);
Daniel Stenberg
committed
}
pubkey = X509_get_pubkey(x);
if(!pubkey)
infof(data, " Unable to load public key\n");
else {
switch(pubkey->type) {
case EVP_PKEY_RSA:
infof(data, " RSA Public Key (%d bits)\n",
BN_num_bits(pubkey->pkey.rsa->n));
snprintf(bufp, CERTBUFFERSIZE, "%d", BN_num_bits(pubkey->pkey.rsa->n));
push_certinfo(data, i, "RSA Public Key", bufp);
Daniel Stenberg
committed
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
print_pubkey_BN(rsa, n, i);
print_pubkey_BN(rsa, e, i);
print_pubkey_BN(rsa, d, i);
print_pubkey_BN(rsa, p, i);
print_pubkey_BN(rsa, q, i);
print_pubkey_BN(rsa, dmp1, i);
print_pubkey_BN(rsa, dmq1, i);
print_pubkey_BN(rsa, iqmp, i);
break;
case EVP_PKEY_DSA:
print_pubkey_BN(dsa, p, i);
print_pubkey_BN(dsa, q, i);
print_pubkey_BN(dsa, g, i);
print_pubkey_BN(dsa, priv_key, i);
print_pubkey_BN(dsa, pub_key, i);
break;
case EVP_PKEY_DH:
print_pubkey_BN(dh, p, i);
print_pubkey_BN(dh, g, i);
print_pubkey_BN(dh, priv_key, i);
print_pubkey_BN(dh, pub_key, i);
break;
#if 0
case EVP_PKEY_EC: /* symbol not present in OpenSSL 0.9.6 */
/* left TODO */
break;
#endif
}
EVP_PKEY_free(pubkey);
Daniel Stenberg
committed
}
X509V3_ext(data, i, cinf->extensions);
X509_signature(data, i, x->signature);
dumpcert(data, x, i);
}
Daniel Stenberg
committed
return CURLE_OK;
}
Daniel Stenberg
committed
/*
* Get the server cert, verify it and show it etc, only call failf() if the
* 'strict' argument is TRUE as otherwise all this is for informational
* purposes only!
*
* We check certificates to authenticate the server; otherwise we risk
* man-in-the-middle attack.
*/
static CURLcode servercert(struct connectdata *conn,
struct ssl_connect_data *connssl,
bool strict)
Daniel Stenberg
committed
{
CURLcode retcode = CURLE_OK;
Daniel Stenberg
committed
int rc;
Daniel Stenberg
committed
long lerr;
ASN1_TIME *certdate;
struct SessionHandle *data = conn->data;
Daniel Stenberg
committed
X509 *issuer;
FILE *fp;
Daniel Stenberg
committed
char buffer[256];
if(data->set.ssl.certinfo)
/* we've been asked to gather certificate info! */
(void)get_cert_chain(conn, connssl);
Daniel Stenberg
committed
Daniel Stenberg
committed
data->set.ssl.certverifyresult = !X509_V_OK;
Daniel Stenberg
committed
connssl->server_cert = SSL_get_peer_certificate(connssl->handle);
if(!connssl->server_cert) {
Daniel Stenberg
committed
if(strict)
failf(data, "SSL: couldn't get peer certificate!");
return CURLE_PEER_FAILED_VERIFICATION;
}
infof (data, "Server certificate:\n");
Daniel Stenberg
committed
Daniel Stenberg
committed
rc = x509_name_oneline(X509_get_subject_name(connssl->server_cert),
buffer, sizeof(buffer));
if(rc) {
Daniel Stenberg
committed
if(strict)
failf(data, "SSL: couldn't get X509-subject!");
Daniel Stenberg
committed
X509_free(connssl->server_cert);
connssl->server_cert = NULL;
Daniel Stenberg
committed
return CURLE_SSL_CONNECT_ERROR;
Daniel Stenberg
committed
infof(data, "\t subject: %s\n", buffer);
Daniel Stenberg
committed
certdate = X509_get_notBefore(connssl->server_cert);
Daniel Stenberg
committed
asn1_output(certdate, buffer, sizeof(buffer));
infof(data, "\t start date: %s\n", buffer);
Daniel Stenberg
committed
certdate = X509_get_notAfter(connssl->server_cert);
Daniel Stenberg
committed
asn1_output(certdate, buffer, sizeof(buffer));
infof(data, "\t expire date: %s\n", buffer);
if(data->set.ssl.verifyhost) {
Daniel Stenberg
committed
retcode = verifyhost(conn, connssl->server_cert);
if(retcode) {
Daniel Stenberg
committed
X509_free(connssl->server_cert);
connssl->server_cert = NULL;
return retcode;
}
Daniel Stenberg
committed
}
Daniel Stenberg
committed
rc = x509_name_oneline(X509_get_issuer_name(connssl->server_cert),
buffer, sizeof(buffer));
if(rc) {
Daniel Stenberg
committed
if(strict)
failf(data, "SSL: couldn't get X509-issuer name!");
retcode = CURLE_SSL_CONNECT_ERROR;
else {
Daniel Stenberg
committed
infof(data, "\t issuer: %s\n", buffer);
/* We could do all sorts of certificate verification stuff here before
deallocating the certificate. */
Daniel Stenberg
committed
Daniel Stenberg
committed
/* e.g. match issuer name with provided issuer certificate */
if(data->set.str[STRING_SSL_ISSUERCERT]) {
fp=fopen(data->set.str[STRING_SSL_ISSUERCERT],"r");
if(!fp) {
if(strict)
Daniel Stenberg
committed
failf(data, "SSL: Unable to open issuer cert (%s)\n",
data->set.str[STRING_SSL_ISSUERCERT]);
X509_free(connssl->server_cert);
connssl->server_cert = NULL;
return CURLE_SSL_ISSUER_ERROR;
Daniel Stenberg
committed
}
issuer = PEM_read_X509(fp,NULL,ZERO_NULL,NULL);
if(!issuer) {
if(strict)
Daniel Stenberg
committed
failf(data, "SSL: Unable to read issuer cert (%s)\n",
data->set.str[STRING_SSL_ISSUERCERT]);
X509_free(connssl->server_cert);
X509_free(issuer);
fclose(fp);
return CURLE_SSL_ISSUER_ERROR;
Daniel Stenberg
committed
}
fclose(fp);
if(X509_check_issued(issuer,connssl->server_cert) != X509_V_OK) {
if(strict)
Daniel Stenberg
committed
failf(data, "SSL: Certificate issuer check failed (%s)\n",
data->set.str[STRING_SSL_ISSUERCERT]);
X509_free(connssl->server_cert);
X509_free(issuer);
connssl->server_cert = NULL;
Daniel Stenberg
committed
return CURLE_SSL_ISSUER_ERROR;
}
infof(data, "\t SSL certificate issuer check ok (%s)\n",
Daniel Stenberg
committed
data->set.str[STRING_SSL_ISSUERCERT]);
Daniel Stenberg
committed
X509_free(issuer);
}
Daniel Stenberg
committed
lerr = data->set.ssl.certverifyresult=
SSL_get_verify_result(connssl->handle);
if(data->set.ssl.certverifyresult != X509_V_OK) {
if(data->set.ssl.verifypeer) {
/* We probably never reach this, because SSL_connect() will fail
Daniel Stenberg
committed
and we return earlier if verifypeer is set? */
Daniel Stenberg
committed
if(strict)
failf(data, "SSL certificate verify result: %s (%ld)",
X509_verify_cert_error_string(lerr), lerr);
retcode = CURLE_PEER_FAILED_VERIFICATION;
}
else
Daniel Stenberg
committed
infof(data, "\t SSL certificate verify result: %s (%ld),"
Daniel Stenberg
committed
" continuing anyway.\n",
X509_verify_cert_error_string(lerr), lerr);
Daniel Stenberg
committed
}
else
Daniel Stenberg
committed
infof(data, "\t SSL certificate verify ok.\n");
Daniel Stenberg
committed
X509_free(connssl->server_cert);
connssl->server_cert = NULL;
Daniel Stenberg
committed
connssl->connecting_state = ssl_connect_done;
Daniel Stenberg
committed
return retcode;
}
static CURLcode
ossl_connect_step3(struct connectdata *conn,
int sockindex)
Daniel Stenberg
committed
{
CURLcode retcode = CURLE_OK;
void *old_ssl_sessionid=NULL;
Daniel Stenberg
committed
struct SessionHandle *data = conn->data;
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
int incache;
SSL_SESSION *our_ssl_sessionid;
Daniel Stenberg
committed
DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
#ifdef HAVE_SSL_GET1_SESSION
our_ssl_sessionid = SSL_get1_session(connssl->handle);
Daniel Stenberg
committed
/* SSL_get1_session() will increment the reference
count and the session will stay in memory until explicitly freed with
SSL_SESSION_free(3), regardless of its state.
This function was introduced in openssl 0.9.5a. */
Daniel Stenberg
committed
#else
our_ssl_sessionid = SSL_get_session(connssl->handle);
Daniel Stenberg
committed
/* if SSL_get1_session() is unavailable, use SSL_get_session().
This is an inferior option because the session can be flushed
at any time by openssl. It is included only so curl compiles
under versions of openssl < 0.9.5a.
Daniel Stenberg
committed
WARNING: How curl behaves if it's session is flushed is
untested.
*/
Daniel Stenberg
committed
#endif
incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
if(incache) {
if(old_ssl_sessionid != our_ssl_sessionid) {
infof(data, "old SSL session ID is stale, removing\n");
Curl_ssl_delsessionid(conn, old_ssl_sessionid);
incache = FALSE;
}
}
Daniel Stenberg
committed
retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
0 /* unknown size */);
if(retcode) {
failf(data, "failed to store ssl session");
return retcode;
}
}
#ifdef HAVE_SSL_GET1_SESSION
else {
/* Session was incache, so refcount already incremented earlier.
* Avoid further increments with each SSL_get1_session() call.
* This does not free the session as refcount remains > 0
*/
SSL_SESSION_free(our_ssl_sessionid);
}
#endif
Daniel Stenberg
committed
/*
* We check certificates to authenticate the server; otherwise we risk
* man-in-the-middle attack; NEVERTHELESS, if we're told explicitly not to
* verify the peer ignore faults and failures from the server cert
* operations.
*/
if(!data->set.ssl.verifypeer)
(void)servercert(conn, connssl, FALSE);
else
retcode = servercert(conn, connssl, TRUE);
if(CURLE_OK == retcode)
connssl->connecting_state = ssl_connect_done;
Daniel Stenberg
committed
return retcode;
Daniel Stenberg
committed
static Curl_recv ossl_recv;
static Curl_send ossl_send;
Daniel Stenberg
committed
static CURLcode
ossl_connect_common(struct connectdata *conn,
int sockindex,
bool nonblocking,
bool *done)
Daniel Stenberg
committed
{
CURLcode retcode;
struct SessionHandle *data = conn->data;
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
curl_socket_t sockfd = conn->sock[sockindex];
long timeout_ms;
Daniel Stenberg
committed
/* check if the connection has already been established */
if(ssl_connection_complete == connssl->state) {
*done = TRUE;
return CURLE_OK;
}
Daniel Stenberg
committed
if(ssl_connect_1==connssl->connecting_state) {
/* Find out how much more time we're allowed */
timeout_ms = Curl_timeleft(data, NULL, TRUE);
if(timeout_ms < 0) {
/* no need to continue if time already is up */
failf(data, "SSL connection timeout");
return CURLE_OPERATION_TIMEDOUT;
}
retcode = ossl_connect_step1(conn, sockindex);
Daniel Stenberg
committed
if(retcode)
Daniel Stenberg
committed
return retcode;
}
Daniel Stenberg
committed
while(ssl_connect_2 == connssl->connecting_state ||
ssl_connect_2_reading == connssl->connecting_state ||
ssl_connect_2_writing == connssl->connecting_state) {
Daniel Stenberg
committed
/* check allowed time left */
timeout_ms = Curl_timeleft(data, NULL, TRUE);
if(timeout_ms < 0) {
/* no need to continue if time already is up */
failf(data, "SSL connection timeout");
return CURLE_OPERATION_TIMEDOUT;
}
Daniel Stenberg
committed
/* if ssl is expecting something, check if it's available. */
Daniel Stenberg
committed
if(connssl->connecting_state == ssl_connect_2_reading
Daniel Stenberg
committed
|| connssl->connecting_state == ssl_connect_2_writing) {
Daniel Stenberg
committed
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
Daniel Stenberg
committed
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
if(what < 0) {
/* fatal error */
failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
return CURLE_SSL_CONNECT_ERROR;
}
else if(0 == what) {
if(nonblocking) {
*done = FALSE;
return CURLE_OK;
Daniel Stenberg
committed
}
else {
/* timeout */
failf(data, "SSL connection timeout");
return CURLE_OPERATION_TIMEDOUT;
Daniel Stenberg
committed
}
}
/* socket is readable or writable */
Daniel Stenberg
committed
}
/* Run transaction, and return to the caller if it failed or if this
* connection is done nonblocking and this loop would execute again. This
* permits the owner of a multi handle to abort a connection attempt
* before step2 has completed while ensuring that a client using select()
* or epoll() will always have a valid fdset to wait on.
retcode = ossl_connect_step2(conn, sockindex);
if(retcode || (nonblocking &&
(ssl_connect_2 == connssl->connecting_state ||
ssl_connect_2_reading == connssl->connecting_state ||
ssl_connect_2_writing == connssl->connecting_state)))
Daniel Stenberg
committed
return retcode;
} /* repeat step2 until all transactions are done. */
Daniel Stenberg
committed
if(ssl_connect_3==connssl->connecting_state) {
retcode = ossl_connect_step3(conn, sockindex);
Daniel Stenberg
committed
if(retcode)
Daniel Stenberg
committed
return retcode;
}
Daniel Stenberg
committed
if(ssl_connect_done==connssl->connecting_state) {
Daniel Stenberg
committed
connssl->state = ssl_connection_complete;
conn->recv[sockindex] = ossl_recv;
conn->send[sockindex] = ossl_send;
Daniel Stenberg
committed
*done = TRUE;
}
Daniel Stenberg
committed
else
Daniel Stenberg
committed
*done = FALSE;
Daniel Stenberg
committed
/* Reset our connect state machine */
connssl->connecting_state = ssl_connect_1;
Daniel Stenberg
committed
return CURLE_OK;
}
CURLcode
Curl_ossl_connect_nonblocking(struct connectdata *conn,
int sockindex,
bool *done)
{
return ossl_connect_common(conn, sockindex, TRUE, done);
Daniel Stenberg
committed
}
CURLcode
Curl_ossl_connect(struct connectdata *conn,
int sockindex)
{
CURLcode retcode;
bool done = FALSE;
retcode = ossl_connect_common(conn, sockindex, FALSE, &done);
Daniel Stenberg
committed
if(retcode)
Daniel Stenberg
committed
return retcode;
DEBUGASSERT(done);
Daniel Stenberg
committed
return CURLE_OK;
}
Daniel Stenberg
committed
bool Curl_ossl_data_pending(const struct connectdata *conn,
int connindex)
{
if(conn->ssl[connindex].handle)
/* SSL is in use */
return (0 != SSL_pending(conn->ssl[connindex].handle)) ? TRUE : FALSE;
Daniel Stenberg
committed
else
return FALSE;
}
static ssize_t ossl_send(struct connectdata *conn,
int sockindex,
const void *mem,
size_t len,
CURLcode *curlcode)
Daniel Stenberg
committed
{
/* SSL_write() is said to return 'int' while write() and send() returns
'size_t' */
int err;
char error_buffer[120]; /* OpenSSL documents that this must be at least 120
bytes long. */
unsigned long sslerror;
Yang Tse
committed
int memlen;
int rc;
ERR_clear_error();
Yang Tse
committed
memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
rc = SSL_write(conn->ssl[sockindex].handle, mem, memlen);
Daniel Stenberg
committed
if(rc < 0) {
err = SSL_get_error(conn->ssl[sockindex].handle, rc);
switch(err) {
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
/* The operation did not complete; the same TLS/SSL I/O function
should be called again later. This is basically an EWOULDBLOCK
Daniel Stenberg
committed
equivalent. */
*curlcode = CURLE_AGAIN;
Daniel Stenberg
committed
case SSL_ERROR_SYSCALL:
Daniel Stenberg
committed
failf(conn->data, "SSL_write() returned SYSCALL, errno = %d",
SOCKERRNO);
*curlcode = CURLE_SEND_ERROR;
Daniel Stenberg
committed
return -1;
case SSL_ERROR_SSL:
/* A failure in the SSL library occurred, usually a protocol error.
The OpenSSL error queue contains more information on the error. */
sslerror = ERR_get_error();
Daniel Stenberg
committed
failf(conn->data, "SSL_write() error: %s",
Daniel Stenberg
committed
ERR_error_string(sslerror, error_buffer));
*curlcode = CURLE_SEND_ERROR;
Daniel Stenberg
committed
return -1;
}
/* a true error */
Daniel Stenberg
committed
failf(conn->data, "SSL_write() return error %d", err);
*curlcode = CURLE_SEND_ERROR;
Daniel Stenberg
committed
return -1;
}
Daniel Stenberg
committed
return (ssize_t)rc; /* number of bytes */
Daniel Stenberg
committed
}
static ssize_t ossl_recv(struct connectdata *conn, /* connection data */
int num, /* socketindex */
char *buf, /* store read data here */
size_t buffersize, /* max amount to read */
CURLcode *curlcode)
Daniel Stenberg
committed
{
char error_buffer[120]; /* OpenSSL documents that this must be at
least 120 bytes long. */
unsigned long sslerror;
Yang Tse
committed
ssize_t nread;
int buffsize;
ERR_clear_error();
Yang Tse
committed
buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
nread = (ssize_t)SSL_read(conn->ssl[num].handle, buf, buffsize);
Daniel Stenberg
committed
if(nread < 0) {
/* failed SSL_read */
int err = SSL_get_error(conn->ssl[num].handle, (int)nread);
switch(err) {
case SSL_ERROR_NONE: /* this is not an error */
case SSL_ERROR_ZERO_RETURN: /* no more data */
break;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
/* there's data pending, re-invoke SSL_read() */
*curlcode = CURLE_AGAIN;
Daniel Stenberg
committed
default:
/* openssl/ssl.h says "look at error stack/return value/errno" */
sslerror = ERR_get_error();
failf(conn->data, "SSL read: %s, errno %d",
ERR_error_string(sslerror, error_buffer),
SOCKERRNO);
*curlcode = CURLE_RECV_ERROR;
Daniel Stenberg
committed
return -1;
}
}
return nread;
}
size_t Curl_ossl_version(char *buffer, size_t size)
{
Daniel Stenberg
committed
#ifdef YASSL_VERSION
/* yassl provides an OpenSSL API compatibility layer so it looks identical
Daniel Stenberg
committed
to OpenSSL in all other aspects */
return snprintf(buffer, size, "yassl/%s", YASSL_VERSION);
Daniel Stenberg
committed
#else /* YASSL_VERSION */
Daniel Stenberg
committed
#if(SSLEAY_VERSION_NUMBER >= 0x905000)
Daniel Stenberg
committed
{
char sub[2];
unsigned long ssleay_value;
sub[1]='\0';
ssleay_value=SSLeay();
if(ssleay_value < 0x906000) {
ssleay_value=SSLEAY_VERSION_NUMBER;
sub[0]='\0';
}
else {
if(ssleay_value&0xff0) {
Daniel Stenberg
committed
}
else
sub[0]='\0';
}
return snprintf(buffer, size, "OpenSSL/%lx.%lx.%lx%s",
Daniel Stenberg
committed
(ssleay_value>>28)&0xf,
(ssleay_value>>20)&0xff,
(ssleay_value>>12)&0xff,
sub);
}
#else /* SSLEAY_VERSION_NUMBER is less than 0.9.5 */
Daniel Stenberg
committed
#if(SSLEAY_VERSION_NUMBER >= 0x900000)
return snprintf(buffer, size, "OpenSSL/%lx.%lx.%lx",
Daniel Stenberg
committed
(SSLEAY_VERSION_NUMBER>>28)&0xff,
(SSLEAY_VERSION_NUMBER>>20)&0xff,
(SSLEAY_VERSION_NUMBER>>12)&0xf);
#else /* (SSLEAY_VERSION_NUMBER >= 0x900000) */
{
char sub[2];
sub[1]='\0';
if(SSLEAY_VERSION_NUMBER&0x0f) {
sub[0]=(SSLEAY_VERSION_NUMBER&0x0f) + 'a' -1;
}
else
sub[0]='\0';
return snprintf(buffer, size, "SSL/%x.%x.%x%s",
Daniel Stenberg
committed
(SSLEAY_VERSION_NUMBER>>12)&0xff,
(SSLEAY_VERSION_NUMBER>>8)&0xf,
(SSLEAY_VERSION_NUMBER>>4)&0xf, sub);
}
#endif /* (SSLEAY_VERSION_NUMBER >= 0x900000) */
#endif /* SSLEAY_VERSION_NUMBER is less than 0.9.5 */
Daniel Stenberg
committed
#endif /* YASSL_VERSION */
Daniel Stenberg
committed
}
#endif /* USE_SSLEAY */