/* ssl_sockets.c */ // This code is provided "as is" with NO WARRANTY expressed or // implied. You may use it freely at your own risk. #include #include #include #include #include int writeToSSLSocket(SSL *ssl, char *request) { int bytesWritten; bytesWritten = SSL_write (ssl, request, strlen(request)); return bytesWritten; } int readn(SSL *ssl, char *buf,int n) { unsigned char *p; int i; int nread; p = (unsigned char *)buf; i = 0; while(i < n) { nread = SSL_read(ssl, p, n-i); if(nread <= 0) return(i); p += nread; i += nread; } return(i); } void printSSLServerResponse(SSL *ssl) { int bytesRead; int readSize = 4096; char buf[readSize + 2]; memset (buf, 0, sizeof(buf)); while (bytesRead = readn(ssl, buf, readSize)) { printf(buf); memset (buf, 0, sizeof(buf)); } } int openSocket(char *host, int port) { long ipAddress; struct hostent* hostInfo; struct sockaddr_in sockInfo; int sock; memset(&sockInfo, 0, sizeof(sockInfo)); sockInfo.sin_family = AF_INET; sockInfo.sin_port = htons(port); ipAddress = inet_addr(host); if (ipAddress < 0) { hostInfo = gethostbyname(host); ipAddress = *(long *)*hostInfo->h_addr_list; } sockInfo.sin_addr.s_addr = ipAddress; // Open the socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) die("Can't open socket\n"); // And connect if (connect (sock, &sockInfo, sizeof(sockInfo)) == -1) die("Cannot connect to socket\n"); return sock; } int SSLsocket(SSL *ssl, char *host, int port) { int sock, result; sock = openSocket(host, port); SSL_set_fd(*ssl, sock); result = SSL_connect(*ssl); return result; }