- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 GnuTLS 中的这些示例来运行使用 PSK 进行身份验证的服务器和客户端:
/* Server */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <gnutls/gnutls.h>
#define KEYFILE "key.pem"
#define CERTFILE "cert.pem"
#define CAFILE "/etc/ssl/certs/ca-certificates.crt"
#define CRLFILE "crl.pem"
/* This is a sample TLS echo server, supporting X.509 and PSK
authentication.
*/
#define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
#define MAX_BUF 1024
#define PORT 5556 /* listen to 5556 port */
#define DH_BITS 1024
/* These are global */
gnutls_certificate_credentials_t x509_cred;
gnutls_psk_server_credentials_t psk_cred;
gnutls_priority_t priority_cache;
static gnutls_session_t
initialize_tls_session (void)
{
gnutls_session_t session;
gnutls_init (&session, GNUTLS_SERVER);
gnutls_priority_set (session, priority_cache);
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
gnutls_credentials_set (session, GNUTLS_CRD_PSK, psk_cred);
/* request client certificate if any.
*/
gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
return session;
}
static gnutls_dh_params_t dh_params;
static int
generate_dh_params (void)
{
/* Generate Diffie-Hellman parameters - for use with DHE
* kx algorithms. When short bit length is used, it might
* be wise to regenerate parameters.
*
* Check the ex-serv-export.c example for using static
* parameters.
*/
gnutls_dh_params_init (&dh_params);
gnutls_dh_params_generate2 (dh_params, DH_BITS);
return 0;
}
static int
pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
{
printf ("psk: username %s\n", username);
key->data = gnutls_malloc (4);
key->data[0] = 0xDE;
key->data[1] = 0xAD;
key->data[2] = 0xBE;
key->data[3] = 0xEF;
key->size = 4;
return 0;
}
int
main (void)
{
int err, listen_sd;
int sd, ret;
struct sockaddr_in sa_serv;
struct sockaddr_in sa_cli;
socklen_t client_len;
char topbuf[512];
gnutls_session_t session;
char buffer[MAX_BUF + 1];
int optval = 1;
int kx;
/* this must be called once in the program
*/
gnutls_global_init ();
gnutls_certificate_allocate_credentials (&x509_cred);
gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE,
GNUTLS_X509_FMT_PEM);
gnutls_certificate_set_x509_crl_file (x509_cred, CRLFILE,
GNUTLS_X509_FMT_PEM);
gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
GNUTLS_X509_FMT_PEM);
gnutls_psk_allocate_server_credentials (&psk_cred);
gnutls_psk_set_server_credentials_function (psk_cred, pskfunc);
generate_dh_params ();
gnutls_priority_init (&priority_cache, "NORMAL:+PSK:+ECDHE-PSK:+DHE-PSK", NULL);
gnutls_certificate_set_dh_params (x509_cred, dh_params);
/* Socket operations
*/
listen_sd = socket (AF_INET, SOCK_STREAM, 0);
SOCKET_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); /* Server Port number */
setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
sizeof (int));
err = bind (listen_sd, (struct sockaddr *) & sa_serv, sizeof (sa_serv));
SOCKET_ERR (err, "bind");
err = listen (listen_sd, 1024);
SOCKET_ERR (err, "listen");
printf ("Server ready. Listening to port '%d'.\n\n", PORT);
client_len = sizeof (sa_cli);
for (;;)
{
session = initialize_tls_session ();
sd = accept (listen_sd, (struct sockaddr *) & sa_cli, &client_len);
printf ("- connection from %s, port %d\n",
inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
sizeof (topbuf)), ntohs (sa_cli.sin_port));
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
ret = gnutls_handshake (session);
if (ret < 0)
{
close (sd);
gnutls_deinit (session);
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
gnutls_strerror (ret));
continue;
}
printf ("- Handshake was completed\n");
kx = gnutls_kx_get(session);
if (kx == GNUTLS_KX_PSK || kx == GNUTLS_KX_DHE_PSK ||
kx == GNUTLS_KX_ECDHE_PSK)
{
printf("- User %s was connected\n", gnutls_psk_server_get_username(session));
}
/* see the Getting peer's information example */
/* print_info(session); */
for (;;)
{
memset (buffer, 0, MAX_BUF + 1);
ret = gnutls_record_recv (session, buffer, MAX_BUF);
if (ret == 0)
{
printf ("\n- Peer has closed the GnuTLS connection\n");
break;
}
else if (ret < 0)
{
fprintf (stderr, "\n*** Received corrupted "
"data(%d). Closing the connection.\n\n", ret);
break;
}
else if (ret > 0)
{
/* echo data back to the client
*/
gnutls_record_send (session, buffer, strlen (buffer));
}
}
printf ("\n");
/* do not wait for the peer to close the connection.
*/
gnutls_bye (session, GNUTLS_SHUT_WR);
close (sd);
gnutls_deinit (session);
}
close (listen_sd);
gnutls_certificate_free_credentials (x509_cred);
gnutls_psk_free_server_credentials (psk_cred);
gnutls_priority_deinit (priority_cache);
gnutls_global_deinit ();
return 0;
}
/* Client */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <gnutls/gnutls.h>
/* A very basic TLS client, with PSK authentication.
*/
#define MAX_BUF 1024
#define MSG "GET / HTTP/1.0\r\n\r\n"
extern int tcp_connect (void);
extern void tcp_close (int sd);
int
main (void)
{
int ret, sd, ii;
gnutls_session_t session;
char buffer[MAX_BUF + 1];
const char *err;
gnutls_psk_client_credentials_t pskcred;
const gnutls_datum_t key = { (void *) "DEADBEEF", 8 };
gnutls_global_init ();
gnutls_psk_allocate_client_credentials (&pskcred);
gnutls_psk_set_client_credentials (pskcred, "test", &key,
GNUTLS_PSK_KEY_HEX);
/* Initialize TLS session
*/
gnutls_init (&session, GNUTLS_CLIENT);
/* Use default priorities */
ret = gnutls_priority_set_direct (session, "PERFORMANCE", &err);
if (ret < 0)
{
if (ret == GNUTLS_E_INVALID_REQUEST)
{
fprintf (stderr, "Syntax error at: %s\n", err);
}
exit (1);
}
/* put the x509 credentials to the current session
*/
gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
/* connect to the peer
*/
sd = tcp_connect ();
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
/* Perform the TLS handshake
*/
do
{
ret = gnutls_handshake (session);
}
while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
if (ret < 0)
{
fprintf (stderr, "*** Handshake failed\n");
gnutls_perror (ret);
goto end;
}
else
{
printf ("- Handshake was completed\n");
}
gnutls_record_send (session, MSG, strlen (MSG));
ret = gnutls_record_recv (session, buffer, MAX_BUF);
if (ret == 0)
{
printf ("- Peer has closed the TLS connection\n");
goto end;
}
else if (ret < 0)
{
fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
goto end;
}
printf ("- Received %d bytes: ", ret);
for (ii = 0; ii < ret; ii++)
{
fputc (buffer[ii], stdout);
}
fputs ("\n", stdout);
gnutls_bye (session, GNUTLS_SHUT_RDWR);
end:
tcp_close (sd);
gnutls_deinit (session);
gnutls_psk_free_client_credentials (pskcred);
gnutls_global_deinit ();
return 0;
}
我使用这些命令生成了自签名证书:
certtool --generate-privkey --outfile key.pem
certtool --generate-self-signed --load-privkey key.pem --outfile cert.pem
我编译了示例 ex-serv-psk.c
和 ex-client-psk.c
。当我尝试运行它们时出现此错误:
[root@localhost test]# ./server
Server ready. Listening to port '5556'.
- connection from 127.0.0.1, port 38184
*** Handshake has failed (The TLS connection was non-properly terminated.)
[root@localhost test]# ./client
*** Handshake failed
GnuTLS error: Insufficient credentials for that request.
[root@localhost test]#
我还需要其他东西来运行这些示例吗?我修改了一些代码
#define KEYFILE "key.pem"
#define CERTFILE "cert.pem"
#define CAFILE "/etc/ssl/certs/ca-certificates.crt"
#define CRLFILE "crl.pem"
我必须如何创建 CAFILE 和 CRLFILE?我已经创建了自签名证书。在这种情况下我需要这些文件吗?
最佳答案
您使用此命令生成 crl 文件。 certtool --generate-crl --load-ca-privkey key.pem --load-ca-certificate cert.pem
您不需要生成 CA 文件,据我所知,您甚至不需要它,只需删除对“gnutls_certificate_set_x509_trust_file”的调用即可
关于c - 如何为 GnuTLS 生成 PSK key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9531247/
查看“mysqldump -d”并看到一个键是 KEY,而不是“PRIMARY KEY”或“FOREIGN KEY” 什么是关键? 示例: CREATE TABLE IF NOT EXISTS `TA
在我开始使用 Python 的过程中尝试找出最佳编码实践。我用 Pandas 写了一个 csv 到数据框阅读器。它使用格式: dataframe = read_csv(csv_input, useco
在 Flutter 中,用一个例子可以清楚地解释什么? 我的困惑是关于 key,如下面的代码所示。 MyHomepage({Key key, this.title}) : super(key: key
我在我的 Android 应用程序中使用 GCM。要使用 GCM 服务,我们需要创建 Google API key 。因此,我为 android、服务器和浏览器 key 创建了 API key 。似乎
我想在 azure key 保管库中创建一个 secret ,该 key 将具有多个 key (例如 JSON)。 例如- { "storageAccountKey":"XXXXX", "Co
尝试通过带有 encodeforURL() 的 url 发送 key 时,我不断收到错误消息和 decodefromUrl() .代码示例如下。 这是我的入口页面: key = generateSec
是否有检查雪花变体字段中是否存在键的函数? 最佳答案 您可以使用 IS_NULL_VALUE 来查看 key 是否存在。如果键不存在,则结果将为 NULL。如果键存在,如果值为 JSON null,则
我正在尝试运行此命令: sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A 但我收到一个错误: Execu
我有一个 csv 文件,我正在尝试对 row[3] 进行计数,然后将其与 row[0] 连接 row[0] row[3] 'A01' 'a' 'B02'
如何编写具有这种形式的函数: A(key, B(key, C(key, ValFactory(key)))) 其中 A、B 和 C 具有此签名: TResult GetOrAdd(string key
审查 this method我很好奇为什么它使用 Object.keys(this).map(key => (this as any)[key])? 只调用 Object.keys(this).ind
我有一个奇怪的情况。我有一个字典,self.containing_dict。使用调试器,我看到了字典的内容,并且可以看到 self 是其中的一个键。但是看看这个: >>> self in self.c
我需要在我的 Google Apps 脚本中使用 RSA-SHA256 和公钥签署消息。 我正在尝试使用 Utilities.computeRsaSha256Signature(value, key)
我是 React 的初学者开发人员,几天前我看到了一些我不理解的有趣语法。 View组件上有{...{key}},我会写成 key={key} ,它完全一样吗?你有链接或解释吗? render()
代理 key 、合成 key 和人工 key 之间有什么区别吗? 我不清楚确切的区别。 最佳答案 代理键、合成键和人工键是同义词。技术关键是另一个。它们都表示“没有商业意义的主键”。它们不同于具有超出
问题陈述:在 Web/控制台 C# 应用程序中以编程方式检索并使用存储在 Azure Key Vault 中的敏感值(例如数据库连接字符串)。 据我所知,您可以在 AAD 中注册应用,并使用其客户端
问题陈述:在 Web/控制台 C# 应用程序中以编程方式检索并使用存储在 Azure Key Vault 中的敏感值(例如数据库连接字符串)。 据我所知,您可以在 AAD 中注册应用,并使用其客户端
我正在寻找 Perl 警告的解决方案 “引用键是实验性的” 我从这样的代码中得到这个: foreach my $f (keys($normal{$nuc}{$e})) {#x, y, and z 我在
我正在为 HSM 实现 JCE 提供程序 JCE中有没有机制指定 key 生成类型例如: session key 或永久 key KeyGenerator keygen = KeyGener
我在 Facebook 上创建了一个应用程序。我已经正确添加了 keyhash 并且应用程序运行良好但是当我今天来并尝试再次运行它时它给了我这个错误。 这已经是第二次了。 Previsouly 当我收
我是一名优秀的程序员,十分优秀!