1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| #include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
int create_socket(int port)
{
int s;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("Erreur création socket");
exit(EXIT_FAILURE);
}
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Echec bind");
exit(EXIT_FAILURE);
}
if (listen(s, 1) < 0) {
perror("Echec listen");
exit(EXIT_FAILURE);
}
int opt = 1;
if (setsockopt((int) s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) != 0) {
printf("Echéc de paramètrage: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
return s;
}
void init_openssl()
{
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
}
void cleanup_openssl()
{
EVP_cleanup();
}
SSL_CTX *create_context()
{
const SSL_METHOD *method;
SSL_CTX *ctx;
method = SSLv23_server_method();
ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
void configure_context(SSL_CTX *ctx)
{
SSL_CTX_set_ecdh_auto(ctx, 1);
if (SSL_CTX_use_certificate_file(ctx, "cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_CTX_use_PrivateKey_file(ctx, "privkey.pem", SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
}
void main(int argc, char **argv)
{
int sock;
SSL_CTX *ctx;
init_openssl();
ctx = create_context();
printf(" configure\n");fflush(stdout);
configure_context(ctx);
printf(" configure OK\n");fflush(stdout);
srand (time (NULL));
int port=(int) 4400L+(((100L* rand())/ (long) RAND_MAX));
printf("port %d\n",port);fflush(stdout);
sock = create_socket(port);
struct sockaddr_in addr;
uint len = sizeof(addr);
SSL *ssl;
const char reply[] = "HTTPS/1.1 200 OK\r\nContent-length:1000000000\r\nContent-Type: text/html\r\n\r\n";
int client = accept(sock, (struct sockaddr*)&addr, &len);
if (client < 0) {
perror("Unable to accept");
exit(EXIT_FAILURE);
}
printf(" client OK\n");fflush(stdout);
//---------------------Négociation SSL-------------------------
printf(" SSL new\n");fflush(stdout);
ssl = SSL_new(ctx);
printf(" SSL set fd\n");fflush(stdout);
printf("%d\n",SSL_set_fd(ssl, client));
printf(" SSL set fd OK\n");fflush(stdout);
//------------------Ce qui pose problème------------------------------
int rep=SSL_accept(ssl);
if (rep <= 0) {
printf("%s",strerror(SSL_get_error(ssl,rep)));fflush(stdout);
SSL_shutdown(ssl);
SSL_free(ssl);
close(client);
close(sock);
SSL_CTX_free(ctx);
cleanup_openssl();
return;
// Erreurs diverses operation not permitted, input/output error, cypher incompatibles
}
//----------------------------------------------------------
void write(SSL* ssl,const char* buff,int l)
{
int pos=0;
while (pos<l)
{
pos+=SSL_write(ssl,&buff[pos],l-pos);
}
}
write(ssl, reply, strlen(reply));
printf(" SSL send OK\n");fflush(stdout);
char buff[1000];
SSL_read(ssl,buff,1000);
printf("%s\n",buff);fflush(stdout);
sprintf(buff,"<html> Bonjour\r\n\n\n</html>");
write(ssl,buff,strlen(buff));
sleep(30);
SSL_shutdown(ssl);
SSL_free(ssl);
close(client);
close(sock);
SSL_CTX_free(ctx);
cleanup_openssl();
return;
} |
Partager