LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 604|回复: 0

OpenSSL库应用的一个例子

[复制链接]
发表于 2005-5-19 11:09:52 | 显示全部楼层 |阅读模式
OpenSSL的文档超级不全,找来了这个demo,抛砖引玉!

  1. /*  ssl_sockets.c  */

  2. // This code is provided "as is" with NO WARRANTY expressed or
  3. // implied. You may use it freely at your own risk.

  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <stdio.h>
  8. #include <openssl/ssl.h>
  9. #include <assert.h>

  10. void die(const char *ptr){
  11.         assert(ptr);
  12.         puts(ptr);
  13.         exit(-1);
  14. }

  15. int writeToSSLSocket(SSL *ssl, char *request) {
  16.    int bytesWritten;

  17.    bytesWritten = SSL_write (ssl, request, strlen(request));
  18.    return bytesWritten;
  19. }

  20. int readn(SSL *ssl, char *buf,int n) {
  21.    unsigned char *p;
  22.    int i;
  23.    int nread;

  24.    p = (unsigned char *)buf;
  25.    i = 0;
  26.    while(i < n) {
  27.       nread = SSL_read(ssl, p, n-i);
  28.       if(nread <= 0) return(i);
  29.       p += nread;
  30.       i += nread;
  31.    }
  32.    return(i);
  33. }

  34. void printSSLServerResponse(SSL *ssl) {
  35.    int bytesRead;
  36.    int readSize = 4096;
  37.    char buf[readSize + 2];

  38.    memset (buf, 0, sizeof(buf));
  39.    while (bytesRead = readn(ssl, buf, readSize)) {
  40.       printf(buf);
  41.       memset (buf, 0, sizeof(buf));
  42.    }
  43. }

  44. int openSocket(char *host, int port) {
  45.    long ipAddress;
  46.    struct hostent* hostInfo;
  47.    struct sockaddr_in sockInfo;
  48.    int sock;

  49.    memset(&sockInfo, 0, sizeof(sockInfo));
  50.    sockInfo.sin_family = AF_INET;
  51.    sockInfo.sin_port = htons(port);
  52.    ipAddress = inet_addr(host);
  53.    if (ipAddress < 0) {
  54.       hostInfo = gethostbyname(host);
  55.       ipAddress = *(long *)*hostInfo->h_addr_list;
  56.    }
  57.    sockInfo.sin_addr.s_addr = ipAddress;

  58.    // Open the socket
  59.    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  60.       die("Can't open socket\n");

  61.    // And connect
  62.    if (connect (sock, (struct sockaddr*)&sockInfo, sizeof(sockInfo)) == -1)
  63.       die("Cannot connect to socket\n");

  64.    return sock;
  65. }

  66. int SSLsocket(SSL *ssl, char *host, int port) {
  67.    int sock, result;

  68.    sock = openSocket(host, port);
  69.    SSL_set_fd(ssl, sock);
  70.    result = SSL_connect(ssl);
  71.    return result;
  72. }
复制代码

  1. /* ssl_main.c */
  2. // This code is provided "as is" with NO WARRANTY expressed or
  3. // implied. You may use it freely at your own risk.

  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <openssl/ssl.h>

  7. extern void die(char *s);
  8. extern char *formRequest();
  9. extern char *getHost(char *hostname, int *portNum, char *arg);
  10. extern int SSLsocket(SSL *ssl, char *host, int port);
  11. extern int writeToSSLSocket(SSL *ssl, char *request);
  12. extern void printSSLServerResponse(SSL *ssl);

  13. int main(int argc, char *argv[]) {
  14.    char hostName[128];
  15.    int i;
  16.    int port;
  17.    int result;
  18.    char *request;
  19.    SSL *ssl = NULL;
  20.    SSL_CTX *ssl_ctx = NULL;

  21.    if (argc < 2) {
  22.       die("Usage: paul2 hostname:port");
  23.    }

  24.    strcpy(hostName, argv[1]);
  25.    {
  26.            char *ptr;
  27.            ptr = strchr(hostName, ':');
  28.            if(ptr){
  29.                    *ptr = '\0';
  30.                    port = atoi(ptr + 1);
  31.            }else{
  32.                    port = 443;
  33.            }
  34.    }

  35.    // init SSL socket
  36.    SSL_load_error_strings();
  37.    SSLeay_add_ssl_algorithms();
  38.    ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  39.    ssl = SSL_new(ssl_ctx);
  40.    result = SSLsocket(ssl, hostName, port);

  41.    char buf[1024];
  42.    while(fgets(buf, 1024, stdin)){
  43.                 result = writeToSSLSocket(ssl, buf);
  44.                 if(strlen(buf) == 1){
  45.                         buf[0] = '\n';
  46.                         break;
  47.                 }
  48.    }
  49.    printSSLServerResponse(ssl);

  50.    SSL_shutdown(ssl);
  51.    free(ssl);
  52.    if (ssl_ctx != NULL) SSL_CTX_free(ssl_ctx);

  53.    return 0;
  54. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表