【问题标题】:Peer to Peer linux authentication in CC中的点对点linux身份验证
【发布时间】:2012-04-08 03:43:26
【问题描述】:

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

当两台计算机相互通信时,它们首先需要验证对方的身份并加密连接。我正在尝试使用 openssl 来完成身份验证和加密,但我不完全确定该怎么做。 所有点对点问题都与其他语言相关或与 openssl 无关。 我一直在尝试修改 An Introduction to OpenSSL Programming 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");
}

我的调试定位函数如下所示。

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 语言开发嵌入式系统并需要一些身份验证的任何人都非常有帮助。

安东尼

【问题讨论】:

    标签: c authentication openssl p2p embedded-linux


    【解决方案1】:

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

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

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

    【讨论】:

    • 在考虑问题时有两种方法。我可以 1. fork,exec(ssh),然后解析结果,或者 2. 使用 openssl 创建一个安全套接字。我选择 openssl 是因为它似乎有更多选项,而且在 C 程序中运行 exec(ssh) 似乎不是一个好习惯。这是一个糟糕的设计决定吗?我使用 ssh 在我的嵌入式系统上工作,因为它们没有显示器或键盘。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2014-12-25
      • 2021-06-10
      • 1970-01-01
      • 2020-09-26
      • 2018-12-02
      • 1970-01-01
      相关资源
      最近更新 更多