Enviado em 23/06/2015 - 19:27h
/* Por : Jonas Forte Silva*/
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#define MAX_REQUEST 1024
#define MAX_REPLY 1024
int getLine(int fd, char line[], int max);
int create_socket();
void ShowCerts(SSL* ssl);
int main(int argc, char **argv) {
const SSL_METHOD *method;
SSL_CTX *ctx;
SSL *ssl;
int server = 0;
int bytes, ret, i;
char *dest_url;
char request[MAX_REQUEST+1];
char reply[MAX_REPLY+1];
char command[10];
char msg[10];
char buf[10240];
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
if(SSL_library_init() < 0)
printf("Nao foi possivel inicializar a biblioteca SSL !\n");
method = SSLv3_client_method();
ctx = SSL_CTX_new(method);
if (ctx == NULL)
printf("Nao foi possivel criar novo contexto SSL.\n");
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
ssl = SSL_new(ctx);
server = create_socket();
if(server != 0)
printf("Conexao TCP criada com sucesso .\n");
SSL_set_fd(ssl, server);
if ( SSL_connect(ssl) != 1 )
printf("Erro: falha ao se conectar via SSL/TLS.\n");
else{
printf("Conexao SSL/TLS criada com sucesso.\n");
printf("Conectado com : %s \n", SSL_get_cipher(ssl));
ShowCerts(ssl); /* get any certs */
}
/* printf("Comando :");*/
bytes = SSL_read(ssl, buf, sizeof(buf));
buf[bytes] = '\0';
printf("Recebeu: \"%s\"\n", buf);
scanf("%s", msg);
SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
bytes = SSL_read(ssl, buf, sizeof(buf));
buf[bytes] = '\0';
printf("Recebeu: \"%s\"\n", buf);
SSL_free(ssl);
close(server);
SSL_CTX_free(ctx);
printf("conexao terminada.\n");
return(0);
}
void ShowCerts(SSL* ssl)
{ X509 *cert;
char *line;
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
if ( cert != NULL )
{
printf("Server certificates:\n");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %s\n", line);
free(line); /* free the malloc'ed string */
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %s\n", line);
free(line); /* free the malloc'ed string */
X509_free(cert); /* free the malloc'ed certificate copy */
}
else
printf("No certificates.\n");
}
int create_socket() {
int sock;
struct sockaddr_in server;
char mensagem[1000] , server_retorno[2000];
//Criar Socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Erro ao criar o socket");
}
puts("Socket criado");
server.sin_addr.s_addr = inet_addr("64.233.186.108");
server.sin_family = AF_INET;
server.sin_port = htons( 465 );
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("Erro ao conectar. Falha");
return 1;
}
puts("Conectado via TCP\n");
return sock;
}
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Visualizar câmeras IP ONVIF no Linux sem necessidade de instalar aplicativos
Atualizar Debian Online de uma Versão para outra
Dica para encontrar diversos jogos Indies criativos
Instalando Discord no Debian 13
Instalar driver Nvidia no Debian 13
Redimensionando, espelhando, convertendo e rotacionando imagens com script
Sincronização Horario Estação de trabalho máquinas domínio com samba N... (2)
Software livre - será que eu estou tão errado assim? (16)