c - C 中的点对点 linux 身份验证

标签 c authentication openssl p2p embedded-linux

我有两个运行 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");
}

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

安东尼

最佳答案

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

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

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

关于c - C 中的点对点 linux 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060218/

相关文章:

Python:无法使用请求模块进行身份验证

c - execvp 出现段错误

c - 内存释放模拟问题

php - 简单的 if() 没有按预期工作

rest - 将 SAML 响应从 Web 应用程序传递到 REST API 以进行身份​​验证?

c++ - 使用 XOR 加密字符串并使用 BSAFE(类似 OpenSSL 的库)对它们进行哈希处理

c++ - 使用二进制参数调用lua方法

c - 函数未生成正确的 openssl rsa key

c++ - 在不使用Abs函数或if语句的情况下获取绝对值

c++ - 如何使用 C 或 C++ 获取目录中的文件列表?