1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/* The GPL applies to this program.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/md5.h>
#include "mssl.h"
extern char last_error[];
BIO *bio_err=0;
char close_ssl_connection(SSL *ssl_h, int socket_h)
{
int rc = SSL_shutdown(ssl_h);
if (!rc)
{
shutdown(socket_h, 1);
rc = SSL_shutdown(ssl_h);
}
/* rc == 0 means try again but it seems to be fine
* to ignore that is what I read from the manpage
*/
if (rc == -1)
return -1;
else
return 0;
}
int READ_SSL(SSL *ssl_h, char *whereto, int len)
{
int cnt=len;
while(len>0)
{
int rc;
rc = SSL_read(ssl_h, whereto, len);
if (rc == -1)
{
if (errno != EINTR && errno != EAGAIN)
{
sprintf(last_error, "READ_SSL: io-error: %s\n", strerror(errno));
return -1;
}
}
else if (rc == 0)
{
return 0;
}
else
{
whereto += rc;
len -= rc;
}
}
return cnt;
}
int WRITE_SSL(SSL *ssl_h, char *whereto, int len)
{
int cnt=len;
while(len>0)
{
int rc;
rc = SSL_write(ssl_h, whereto, len);
if (rc == -1)
{
if (errno != EINTR && errno != EAGAIN)
{
sprintf(last_error, "WRITE_SSL: io-error: %s\n", strerror(errno));
return -1;
}
}
else if (rc == 0)
{
return 0;
}
else
{
whereto += rc;
len -= rc;
}
}
return cnt;
}
int connect_ssl(int socket_h, SSL_CTX *client_ctx, SSL **ssl_h, BIO **s_bio, int timeout)
{
int dummy;
#if 0
int rc;
struct timeval to;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(socket_h, &rfds);
to.tv_sec = timeout / 1000;
to.tv_usec = (timeout - (to.tv_sec * 1000)) * 1000;
/* wait for connection */
rc = select(socket_h + 1, &rfds, NULL, NULL, &to);
if (rc == 0)
return -2; /* timeout */
else if (rc == -1)
{
if (errno == EINTR)
return -3; /* ^C pressed */
else
return -1; /* error */
}
#endif
*ssl_h = SSL_new(client_ctx);
*s_bio = BIO_new_socket(socket_h, BIO_NOCLOSE);
SSL_set_bio(*ssl_h, *s_bio, *s_bio);
dummy = SSL_connect(*ssl_h);
if (dummy <= 0)
{
sprintf(last_error, "problem starting SSL connection: %d\n", SSL_get_error(*ssl_h, dummy));
return -1;
}
return 0;
}
SSL_CTX * initialize_ctx(void)
{
SSL_METHOD *meth;
if (!bio_err)
{
SSL_library_init();
SSL_load_error_strings();
/* error write context */
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
}
/* create context */
meth = SSLv23_method();
return SSL_CTX_new(meth);
}
char * get_fingerprint(SSL *ssl_h)
{
char *string = NULL;
unsigned char fp_digest[EVP_MAX_MD_SIZE];
X509 *x509_data = SSL_get_peer_certificate(ssl_h);
if (x509_data)
{
unsigned int fp_digest_size = sizeof(fp_digest);
memset(fp_digest, 0x00, sizeof(fp_digest));
if (X509_digest(x509_data, EVP_md5(), fp_digest, &fp_digest_size))
{
string = (char *)malloc(MD5_DIGEST_LENGTH * 3 + 1);
if (string)
{
int loop, pos =0;
for(loop=0; loop<MD5_DIGEST_LENGTH; loop++)
{
if (loop)
pos += sprintf(&string[pos], ":%02x", fp_digest[loop]);
else
pos = sprintf(&string[pos], "%02x", fp_digest[loop]);
}
}
}
X509_free(x509_data);
}
return string;
}
|