C中的Peer to Peer linux身份验证

时间:2012-04-08 03:43:27

标签: c authentication openssl p2p embedded-linux

我有两个运行Angstrom Linux的嵌入式系统,它们通过以太网交叉电缆连接。我正在开发一个允许两个系统相互通信的C程序。

当两台计算机相互通信时,他们首先需要验证另一台计算机的身份并加密连接。我正在尝试使用openssl来完成身份验证和加密,但我不确定该怎么做。 所有点对点问题都与其他语言相关或与openssl无关。 我一直在尝试修改OpenSSL编程简介http://www.linuxjournal.com/article/4822中的代码,以使我的嵌入式系统正常工作,但还没有成功。在common.c中的SSL_CTX * initialize_ctx和server.c中的load_dh_params(ctx,file)似乎是问题所在。这是我的common.c的代码,我的一些修改。

SSL_CTX *initialize_ctx(keyfile,password)
char *keyfile;
char *password;
{
SSL_METHOD *meth;
SSL_CTX *ctx;
char buffer[200];
if (!bio_err)
{
    /* Global system initialization*/
    SSL_library_init();
    SSL_load_error_strings();

    /* An error write context */
    bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
}

debuglocation(__LINE__,__FILE__);
/* Set up a SIGPIPE handler */
signal(SIGPIPE,sigpipe_handle);

/* Create our context*/
meth=SSLv23_method();
ctx=SSL_CTX_new(meth);  

debuglocation(__LINE__,__FILE__);
/* Load our keys and certificates*/


//    if (!(SSL_CTX_use_certificate_chain_file(ctx,keyfile)))
//        berr_exit("Can't read certificate file");

debuglocation(__LINE__,__FILE__);

pass=password;
/* TODO need to put a password on the key*/
//SSL_CTX_set_default_passwd_cb(ctx,password_cb);
//if (!(SSL_CTX_use_PrivateKey_file(ctx,keyfile,SSL_FILETYPE_PEM)))

//http://www.openssl.org/docs/ssl/SSL_CTX_use_certificate.html#NOTES

if(!(SSL_CTX_use_RSAPrivateKey_file(ctx,"private.pem", SSL_FILETYPE_PEM)))
    berr_exit("Can't read priveate rsa");

debuglocation(__LINE__,__FILE__);
//berr_exit("Can't read key file");

//    /* Load the CAs we trust*/
//    if (!(SSL_CTX_load_verify_locations(ctx,
//                                        CA_LIST,0)))
//        berr_exit("Can't read CA list");
#if (OPENSSL_VERSION_NUMBER < 0x00905100L)
SSL_CTX_set_verify_depth(ctx,1);
#endif

return ctx;
}

这是server.c

void load_dh_params(ctx,file)
SSL_CTX *ctx;
char *file;
{
DH *ret=0;
BIO *bio;

//http://www.openssl.org/docs/crypto/BIO_s_file.html
// opens a file just like fopen with the second parameter as the type of open. Here it is read 'r'.
if ((bio=BIO_new_file(file,"r")) == NULL)
 berr_exit("Couldn't open DH file");

//http://www.openssl.org/docs/crypto/pem.html
ret=PEM_read_bio_DHparams(bio,NULL,NULL,
 NULL);
BIO_free(bio);


if(SSL_CTX_set_tmp_dh(ctx,ret)<0)
 berr_exit("Couldn't set DH parameters");
}

我的debuglocation函数看起来像这样。

int debuglocation(int line, char * file)
{
static char c = 'A';
printf("Made it to line %d in %s call it %c\n",line,file, c);
c++;
return 0;
}

所以,当我运行从服务器获取的所有内容时。

2535:error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher:s3_srvr.c:1075:

这是来自客户端。

SSL connect error
2616:error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure:s23_clnt.c:596:

此外,我不确定使用什么ssl命令来制作所需的证书。 如果两个嵌入式设备都有一个公钥和一个私钥,RSA似乎会运行良好,所以我尝试了http://www.devco.net/archives/2006/02/13/public_-_private_key_encryption_using_openssl.php 并为我制作了一个剧本。

openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -outform PEM -pubout

提前感谢您的帮助。如果您需要更多信息,请告诉我。我认为对于需要一些身份验证的嵌入式系统的C语言开发人员来说,这个问题的答案可能非常有用。

安东尼

2 个答案:

答案 0 :(得分:0)

作为运行comm进程的用户,请执行ssh_keygen。

将输出的公共部分id_rsa.pub附加到另一台机器上的〜/ .ssh / authorized_keys。现在,您无需登录即可使用ssh运行远程程序。

编辑。我提出了上述建议,因为使用证书很麻烦。您需要拥有一个信任存储,正确的目录权限等。我认为您首先缺少的是加载data on trusted certificates。请参阅有关如何执行此操作的链接。使用openssl中的命令行工具检查授权更容易,然后调试程序并同时设置SSL。

答案 1 :(得分:0)

我最终使用ssh而不是尝试使用openssl。它确实让生活变得更加简单。也许当我有更多时间时,我会想到另一种方式。