Last active
April 14, 2016 21:48
-
-
Save promi/6c4222c4ac99c250fc2a5eb16085ff01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <netdb.h> | |
#include <stdarg.h> | |
#include <unistd.h> | |
#include <mbedtls/net.h> | |
#include <mbedtls/ssl.h> | |
#include <mbedtls/entropy.h> | |
#include <mbedtls/ctr_drbg.h> | |
#include <mbedtls/debug.h> | |
#include "Mumble.pb-c.h" | |
// https://tls.mbed.org/kb/how-to/mbedtls-tutorial | |
#define SERVER_PORT "10012" | |
#define SERVER_NAME "voice.mumbletreff.de" | |
struct mumble_packet_header | |
{ | |
uint16_t type; | |
uint32_t len; | |
} __attribute__((__packed__)); | |
void exit_with_message(int exit_code, const char *fmt, ...) | |
{ | |
va_list args1; | |
va_start(args1, fmt); | |
vfprintf(stderr, fmt, args1); | |
va_end(args1); | |
exit(exit_code); | |
} | |
static void my_debug( void *ctx, int level, | |
const char *file, int line, const char *str ) | |
{ | |
((void) level); | |
fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str ); | |
fflush( (FILE *) ctx ); | |
} | |
void read_bytes(mbedtls_ssl_context *ssl, uint8_t *buffer, ssize_t len) | |
{ | |
int n_read = 0; | |
uint8_t *cur = buffer; | |
int ret = 0; | |
while(n_read < len) | |
{ | |
ret = mbedtls_ssl_read(ssl, cur, len - n_read); | |
if(ret <= 0) | |
{ | |
exit_with_message(5, "failed\n ! ssl_read returned %d\n\n", ret); | |
} | |
n_read += ret; | |
cur += ret; | |
} | |
} | |
int main(void) | |
{ | |
// struct hostent *server_host; | |
mbedtls_net_context server_fd; | |
mbedtls_entropy_context entropy; | |
mbedtls_ctr_drbg_context ctr_drbg; | |
mbedtls_ssl_context ssl; | |
mbedtls_ssl_config conf; | |
mbedtls_x509_crt cacert; | |
const char *pers = "libmumble-net"; | |
mbedtls_net_init(&server_fd); | |
mbedtls_ssl_init(&ssl); | |
mbedtls_ssl_config_init(&conf); | |
mbedtls_x509_crt_init(&cacert); | |
mbedtls_ctr_drbg_init(&ctr_drbg); | |
mbedtls_entropy_init(&entropy); | |
int ret; | |
if((ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, | |
(const unsigned char *) pers, | |
strlen( pers ) ) ) != 0 ) | |
{ | |
exit_with_message(10, " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret); | |
} | |
if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME, | |
SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 ) | |
{ | |
printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); | |
exit(-2); | |
} | |
if( ( ret = mbedtls_ssl_config_defaults( &conf, | |
MBEDTLS_SSL_IS_CLIENT, | |
MBEDTLS_SSL_TRANSPORT_STREAM, | |
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) | |
{ | |
printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); | |
exit(-3); | |
} | |
mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE ); | |
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); | |
mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); | |
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) | |
{ | |
printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret); | |
exit(-7); | |
} | |
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); | |
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) | |
{ | |
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) | |
{ | |
printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); | |
//goto exit; | |
exit(-8); | |
} | |
} | |
unsigned char buf[1024]; | |
struct _MumbleProto__Version version = MUMBLE_PROTO__VERSION__INIT; | |
version.has_version = 1; | |
version.version = 0x00010300; | |
version.release = "Compiled by B00n"; | |
version.os = "Windoof"; | |
version.os_version = "1k"; | |
*((uint16_t *) buf) = htons(0); | |
*((uint32_t *) buf + 2) = htonl(sizeof(version)); | |
mumble_proto__version__pack(&version, buf + 6); | |
// memcpy((void *) (buf + 6), (void*) &version, sizeof(version)); | |
// int len = sprintf((char *) buf + 2, "foo"); | |
printf("X\n"); | |
while((ret = mbedtls_ssl_write(&ssl, buf, sizeof(version) + 6)) <= 0) | |
{ | |
if(ret != 0) | |
{ | |
exit_with_message(4, " failed\n ! write returned %d\n\n", ret); | |
} | |
} | |
int len = ret; | |
printf(" %d bytes written\n\n%s", len, (char *) buf); | |
struct _MumbleProto__Authenticate authenticate = | |
MUMBLE_PROTO__AUTHENTICATE__INIT; | |
authenticate.username = "Phobos1k"; | |
authenticate.password = ""; | |
authenticate.n_tokens = 0; | |
authenticate.tokens = NULL; | |
authenticate.n_celt_versions = 0; | |
authenticate.celt_versions = NULL; | |
authenticate.has_opus = 1; | |
authenticate.opus = 1; | |
*((uint16_t *) buf) = htons(2); | |
*((uint32_t *) buf + 2) = htonl(sizeof(authenticate)); | |
mumble_proto__authenticate__pack(&authenticate, buf + 6); | |
//memcpy((void *) (buf + 6), (void*) &authenticate, sizeof(authenticate)); | |
// int len = sprintf((char *) buf + 2, "foo"); | |
while((ret = mbedtls_ssl_write(&ssl, buf, sizeof(authenticate) + 6)) <= 0) | |
{ | |
if(ret != 0) | |
{ | |
exit_with_message(4, " failed\n ! write returned %d\n\n", ret); | |
} | |
} | |
len = ret; | |
printf(" %d bytes written\n\n%s", len, (char *) buf); | |
struct mumble_packet_header header; | |
//while(1) | |
//{ | |
read_bytes(&ssl, (uint8_t*) &header, sizeof(header)); | |
printf ("message type = %d\n", ntohs(header.type)); | |
printf ("message len = %d\n", ntohl(header.len)); | |
fflush(stdout); | |
//} | |
// = MUMBLE_PROTO__VERSION_INIT; | |
read_bytes(&ssl, (uint8_t*) buf, ntohl(header.len)); | |
struct _MumbleProto__Version *version2 = | |
mumble_proto__version__unpack(NULL, ntohl(header.len), buf); | |
//read_bytes(&ssl, (uint8_t*) &version2, header.len); | |
printf("version.has_version = %d\n", version2->has_version); | |
printf("version.version = %x\n", version2->version); | |
printf("version.release = %s\n", version2->release); | |
printf("version.os = %s\n", version2->os); | |
printf("version.os_version = %s\n", version2->os_version); | |
mbedtls_net_free( &server_fd ); | |
mbedtls_ssl_free( &ssl ); | |
mbedtls_ssl_config_free( &conf ); | |
mbedtls_ctr_drbg_free( &ctr_drbg ); | |
mbedtls_entropy_free( &entropy ); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment