我是新手,刚开始学习openssl,网上找了一段使用C++编写的代码,如下,是一段服务器端的实例代码:
/*所有需要的参数信息都在此处以#define的形式提供*/
#define CERTF "server.crt" /*服务端的证书(需经CA签名)*/
#define KEYF "server.key" /*服务端的私钥(建议加密存储)*/
#define CACERT "ca.crt" /*CA 的证书*/
#define PORT 1111 /*准备绑定的端口*/
#define CHK_NULL(x) if ((x)==NULL) exit (1)
#define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
#define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
int main()
{
int err;
int listen_sd;
int sd;
struct sockaddr_in sa_serv;
struct sockaddr_in sa_cli;
int client_len;
SSL_CTX* ctx;
SSL* ssl;
X509* client_cert;
char* str;
char buf[4096];
SSL_METHOD *meth;
WSADATA wsaData;
int reterr = 0;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0){
printf("WSAStartup()fail:%d/n", GetLastError());
return -1;
}
SSL_load_error_strings(); /*为打印调试信息作准备*/
OpenSSL_add_ssl_algorithms(); /*初始化*/
//meth = TLSv1_server_method(); /*采用什么协议(SSLv2/SSLv3/TLSv1)在此指定*/
//ctx = SSL_CTX_new(meth);
ctx = SSL_CTX_new(TLSv1_server_method());
CHK_NULL(ctx);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); /*验证与否*/
reterr = SSL_CTX_load_verify_locations(ctx, CACERT, NULL); /*若验证,则放置CA证书*/
if ((reterr = SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM)) <= 0) {
ERR_print_errors_fp(stderr);
exit(3);
}
if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(4);
}
if (!SSL_CTX_check_private_key(ctx)) {
printf("Private key does not match the certificate public key/n");
exit(5);
}
SSL_CTX_set_cipher_list(ctx, "RC4-MD5");
/*开始正常的TCP socket过程.................................*/
printf("Begin TCP socket.../n");
listen_sd = socket(AF_INET, SOCK_STREAM, 0);
CHK_ERR(listen_sd, "socket");
memset(&sa_serv, '/0', sizeof(sa_serv));
sa_serv.sin_family = AF_INET;
sa_serv.sin_addr.s_addr = INADDR_ANY;
sa_serv.sin_port = htons(PORT);
err = bind(listen_sd, (struct sockaddr*) &sa_serv,
sizeof(sa_serv));
CHK_ERR(err, "bind");
/*接受TCP链接*/
err = listen(listen_sd, 5);
CHK_ERR(err, "listen");
client_len = sizeof(sa_cli);
sd = accept(listen_sd, (struct sockaddr*) &sa_cli, &client_len);
CHK_ERR(sd, "accept");
closesocket(listen_sd);
printf("Connection from %lx, port %x/n",
sa_cli.sin_addr.s_addr, sa_cli.sin_port);
/*TCP连接已建立,进行服务端的SSL过程. */
printf("Begin server side SSL/n");
ssl = SSL_new(ctx);
CHK_NULL(ssl);
SSL_set_fd(ssl, sd);
err = SSL_accept(ssl);
// 到这里就返回错误,无法继续进行了返回值是-1,获取错误ma是SSL_ERROR_SSL(值为1)
每次运行到SSL_accept就返回错误无法继续下去了,获取错误码是SSL_ERROR_SSL(值为1)