OpenSSL库应用的一个例子
时间:2006-12-30 来源:liuzhixian
OpenSSL库应用的一个例子
/* 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 <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <openssl/ssl.h>
#include <assert.h>
void die(const char *ptr){
assert(ptr);
puts(ptr);
exit(-1);
}
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, (struct sockaddr*)&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;
}
// This code is provided "as is" with NO WARRANTY expressed or
// implied. You may use it freely at your own risk.
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <openssl/ssl.h>
#include <assert.h>
void die(const char *ptr){
assert(ptr);
puts(ptr);
exit(-1);
}
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, (struct sockaddr*)&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;
}
/* ssl_main.c */ // This code is provided "as is" with NO WARRANTY expressed or // implied. You may use it freely at your own risk. #include <stdio.h> #include <stdlib.h> #include <openssl/ssl.h> extern void die(char *s); extern char *formRequest(); extern char *getHost(char *hostname, int *portNum, char *arg); extern int SSLsocket(SSL *ssl, char *host, int port); extern int writeToSSLSocket(SSL *ssl, char *request); extern void printSSLServerResponse(SSL *ssl); int main(int argc, char *argv[]) { char hostName[128]; int i; int port; int result; char *request; SSL *ssl = NULL; SSL_CTX *ssl_ctx = NULL; if (argc < 2) { die("Usage: paul2 hostname:port"); } strcpy(hostName, argv[1]); { char *ptr; ptr = strchr(hostName, ':'); if(ptr){ *ptr = '\0'; port = atoi(ptr + 1); }else{ port = 443; } } // init SSL socket SSL_load_error_strings(); SSLeay_add_ssl_algorithms(); ssl_ctx = SSL_CTX_new(SSLv23_client_method()); ssl = SSL_new(ssl_ctx); result = SSLsocket(ssl, hostName, port); char buf[1024]; while(fgets(buf, 1024, stdin)){ result = writeToSSLSocket(ssl, buf); if(strlen(buf) == 1){ buf[0] = '\n'; break; } } printSSLServerResponse(ssl); SSL_shutdown(ssl); free(ssl); if (ssl_ctx != NULL) SSL_CTX_free(ssl_ctx); return 0; }
相关阅读 更多 +