欢迎来到天天文库
浏览记录
ID:34724814
大小:196.18 KB
页数:22页
时间:2019-03-10
《openssl一个简单的实例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、1.安装openssl库:sudosudoapt-getinstalllibssl-dev2.服务器端实现代码#include#include#include#include#include#include#include#include#include#include#include#include2、r.h>#defineMAXBUF1024intmain(intargc,char**argv){intsockfd,new_fd;socklen_tlen;structsockaddr_inmy_addr,their_addr;unsignedintmyport,lisnum;charbuf[MAXBUF+1];SSL_CTX*ctx;if(argv[1])myport=atoi(argv[1]);elsemyport=7838;if(argv[2])lisnum=atoi(argv[2]);elselisnum=2;/*SSL库初始化*/SSL_library_init(3、);/*载入所有SSL算法*/OpenSSL_add_all_algorithms();/*载入所有SSL错误消息*/SSL_load_error_strings();/*以SSLV2和V3标准兼容方式产生一个SSL_CTX,即SSLContentText*/ctx=SSL_CTX_new(SSLv23_server_method());/*也可以用SSLv2_server_method()或SSLv3_server_method()单独表示V2或V3标准*/if(ctx==NULL){ERR_print_errors_fp(stdout);exit(1);}/*载入用户的数4、字证书,此证书用来发送给客户端。证书里包含有公钥*/if(SSL_CTX_use_certificate_file(ctx,argv[3],SSL_FILETYPE_PEM)<=0){ERR_print_errors_fp(stdout);exit(1);}/*载入用户私钥*/if(SSL_CTX_use_PrivateKey_file(ctx,argv[4],SSL_FILETYPE_PEM)<=0){ERR_print_errors_fp(stdout);exit(1);}/*检查用户私钥是否正确*/if(!SSL_CTX_check_private_key(ctx)){5、ERR_print_errors_fp(stdout);exit(1);}/*开启一个socket监听*/if((sockfd=socket(PF_INET,SOCK_STREAM,0))==-1){perror("socket");exit(1);}elseprintf("socketcreated");bzero(&my_addr,sizeof(my_addr));my_addr.sin_family=PF_INET;my_addr.sin_port=htons(myport);my_addr.sin_addr.s_addr=INADDR_ANY;if(bind(so6、ckfd,(structsockaddr*)&my_addr,sizeof(structsockaddr))==-1){perror("bind");exit(1);}elseprintf("binded");if(listen(sockfd,lisnum)==-1){perror("listen");exit(1);}elseprintf("beginlisten");while(1){SSL*ssl;len=sizeof(structsockaddr);/*等待客户端连上来*/if((new_fd=accept(sockfd,(structsockaddr*)&t7、heir_addr,&len))==-1){perror("accept");exit(errno);}elseprintf("server:gotconnectionfrom%s,port%d,socket%d",inet_ntoa(their_addr.sin_addr),ntohs(their_addr.sin_port),new_fd);/*基于ctx产生一个新的SSL*/ssl=SSL_new(ctx);/*将连接用户的socket加入到SSL*/SSL_set_fd(ssl,new_fd
2、r.h>#defineMAXBUF1024intmain(intargc,char**argv){intsockfd,new_fd;socklen_tlen;structsockaddr_inmy_addr,their_addr;unsignedintmyport,lisnum;charbuf[MAXBUF+1];SSL_CTX*ctx;if(argv[1])myport=atoi(argv[1]);elsemyport=7838;if(argv[2])lisnum=atoi(argv[2]);elselisnum=2;/*SSL库初始化*/SSL_library_init(
3、);/*载入所有SSL算法*/OpenSSL_add_all_algorithms();/*载入所有SSL错误消息*/SSL_load_error_strings();/*以SSLV2和V3标准兼容方式产生一个SSL_CTX,即SSLContentText*/ctx=SSL_CTX_new(SSLv23_server_method());/*也可以用SSLv2_server_method()或SSLv3_server_method()单独表示V2或V3标准*/if(ctx==NULL){ERR_print_errors_fp(stdout);exit(1);}/*载入用户的数
4、字证书,此证书用来发送给客户端。证书里包含有公钥*/if(SSL_CTX_use_certificate_file(ctx,argv[3],SSL_FILETYPE_PEM)<=0){ERR_print_errors_fp(stdout);exit(1);}/*载入用户私钥*/if(SSL_CTX_use_PrivateKey_file(ctx,argv[4],SSL_FILETYPE_PEM)<=0){ERR_print_errors_fp(stdout);exit(1);}/*检查用户私钥是否正确*/if(!SSL_CTX_check_private_key(ctx)){
5、ERR_print_errors_fp(stdout);exit(1);}/*开启一个socket监听*/if((sockfd=socket(PF_INET,SOCK_STREAM,0))==-1){perror("socket");exit(1);}elseprintf("socketcreated");bzero(&my_addr,sizeof(my_addr));my_addr.sin_family=PF_INET;my_addr.sin_port=htons(myport);my_addr.sin_addr.s_addr=INADDR_ANY;if(bind(so
6、ckfd,(structsockaddr*)&my_addr,sizeof(structsockaddr))==-1){perror("bind");exit(1);}elseprintf("binded");if(listen(sockfd,lisnum)==-1){perror("listen");exit(1);}elseprintf("beginlisten");while(1){SSL*ssl;len=sizeof(structsockaddr);/*等待客户端连上来*/if((new_fd=accept(sockfd,(structsockaddr*)&t
7、heir_addr,&len))==-1){perror("accept");exit(errno);}elseprintf("server:gotconnectionfrom%s,port%d,socket%d",inet_ntoa(their_addr.sin_addr),ntohs(their_addr.sin_port),new_fd);/*基于ctx产生一个新的SSL*/ssl=SSL_new(ctx);/*将连接用户的socket加入到SSL*/SSL_set_fd(ssl,new_fd
此文档下载收益归作者所有