From bda42ff22772e285cd5c55f4043e1c7968d27115 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 14 Apr 2011 00:40:06 -0300 Subject: [PATCH] =?utf8?q?Testando=20comunica=C3=A7=C3=A3o=20SSL.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit O ReceitaNet usa um handshake seguido de um handshake SSL. Esse pequeno servidor implementa o handshake do ReceitaNet e usa gnutls para o suporte a SSL. --- rnet.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 rnet.c diff --git a/rnet.c b/rnet.c new file mode 100644 index 0000000..9aee769 --- /dev/null +++ b/rnet.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2011 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define DH_BITS 1024 +static void * get_creds(char *certfile, char *keyfile) +{ + static gnutls_certificate_credentials_t cred; + gnutls_dh_params_t dh_params; + gnutls_dh_params_init(&dh_params); + gnutls_dh_params_generate2(dh_params, DH_BITS); + gnutls_certificate_allocate_credentials(&cred); + gnutls_certificate_set_x509_key_file(cred, certfile, keyfile, + GNUTLS_X509_FMT_PEM); + gnutls_certificate_set_dh_params(cred, dh_params); + return cred; +} + +static void session_new(gnutls_session_t *session) +{ + static void *cred; + cred = get_creds("cert.pem", "key.pem"); + gnutls_init(session, GNUTLS_SERVER); + gnutls_set_default_priority(*session); + gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, cred); + gnutls_dh_set_prime_bits(*session, DH_BITS); +} +#undef DH_BITS + +int main(int argc, char **argv) +{ + int s; + struct sockaddr_in saddr; + int c; + int r; + char buffer[256]; + gnutls_session_t session; + gnutls_global_init(); + session_new(&session); + s = socket(PF_INET, SOCK_STREAM, 0); + saddr.sin_family = AF_INET; + saddr.sin_port = htons(3456); + saddr.sin_addr.s_addr = htonl(INADDR_ANY); + bind(s, (struct sockaddr *) &saddr, sizeof(saddr)); + listen(s, 5); + c = accept(s, NULL, NULL); + close(s); + gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c); + r = read(c, buffer, 1); + if (r == 1 && buffer[0] == 1) + write(c, "E", 1); + r = read(c, buffer, 14); + if (r == 14 && !memcmp(buffer, "00000000000000", 14)) + write(c, "14032011002200", 14); + if ((r = gnutls_handshake(session)) < 0) + fprintf(stderr, "error in handshake: %s\n", + gnutls_strerror(r)); + else + fprintf(stderr, "handshake ok\n"); + close(c); + gnutls_global_deinit(); + return 0; +} -- 2.20.1