Meet Paul the Second
--#--
Our main() function must now do a little managing of the
structures in addition to opening the socket, writing on it,
reading from and closing down. We also have to perform a couple
of OpenSSL/SSLeay housekeeping functions. These should be pretty
much self-explanatory.
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
Otherwise, most things work the way they did with Paul the First,
except for managing the structure references.
#include
#include
#include
extern void die(char *s);
extern char *formRequest();
extern char *getHost(char *hostname, int *portNum, char *arg);
extern void SSLsocket(SSL *ssl, char *host, int port);
extern int writeToSSLSocket(SSL *ssl, char *request, int len);
extern void printSSLServerResponse(SSL *ssl);
int main(int argc, char *argv[]) {
char hostName[128];
char dieString[64];
char *request;
int port;
int result = 0;
SSL *ssl = NULL;
SSL_CTX *ssl_ctx = NULL;
int sock;
if (argc < 2) {
die("Usage: paul2 hostname:port");
}
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
request = formRequest();
getHost(hostName, &port, argv[1]);
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
ssl = SSL_new(ssl_ctx);
result = SSLsocket(ssl, hostName, port);
if (result < 0 ) {
sprintf(dieString, "Could not get secure socket on %s:%d", hostName, port);
die(dieString);
}
result = writeToSSLSocket(ssl, request, strlen(request));
if (result < 0) {
sprintf(dieString, "Could not get write to socket on %s:%d", hostName, port);
die(dieString);
}
printSSLServerResponse(ssl);
// we're done. Let's close everything down
SSL_shutdown(ssl);
if (ssl != NULL) free(ssl);
if (ssl_ctx !=NULL) free(ssl_ctx);
close(sock);
free(request);
return 0;
}
Compile this like you did Paul the First, and you'll have a
command-line client that makes SSLv3 or SSLv2 connections to
almost any site on the Web.