-
Notifications
You must be signed in to change notification settings - Fork 2
/
client1.c
210 lines (181 loc) · 4.98 KB
/
client1.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include "pthread.h"
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define MAX_T 1 //set pthread num
#define CLIENTCERT "client/client-cert.pem"
#define CLIENTKEY "client/client-key.pem"
#define CAFILE "ca/ca-cert.pem"
#define SOCKET int
#define SOCKET_ERROR -1
static pthread_mutex_t *lock_cs;
static long *lock_count;
pthread_t pthreads_thread_id(void){
pthread_t ret;
ret=pthread_self();
return(ret);
}
void pthreads_locking_callback(int mode, int type, char *file,int line){
if (mode & CRYPTO_LOCK){
pthread_mutex_lock(&(lock_cs[type]));
lock_count[type]++;
} else {
pthread_mutex_unlock(&(lock_cs[type]));
}
}
int verify_callback(int ok, X509_STORE_CTX *ctx){
printf("verify_callback\n");
return ok;
}
int SSL_CTX_use_PrivateKey_file_pass(SSL_CTX *ctx, char *filename, char *pass){
EVP_PKEY *pkey=NULL;
BIO *key=NULL;
key=BIO_new(BIO_s_file());
BIO_read_filename(key,filename);
pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,pass);
if(pkey==NULL) {
printf("PEM_read_bio_PrivateKey err");
return -1;
}
if (SSL_CTX_use_PrivateKey(ctx,pkey) <= 0){
printf("SSL_CTX_use_PrivateKey err\n");
return -1;
}
BIO_free(key);
return 1;
}
static int s_client_verify=SSL_VERIFY_NONE;
void* thread_main(void *arg){
int err,buflen,read;
int sd;
SSL_CTX *ctx=(SSL_CTX *)arg;
struct sockaddr_in dest_sin;
SOCKET sock;
SSL *ssl;
X509 *server_cert;
char *str;
char buf[1024];
SSL_METHOD *method;
FILE *fp;
sock = socket(AF_INET, SOCK_STREAM, 0);
dest_sin.sin_family = AF_INET;
dest_sin.sin_addr.s_addr = inet_addr( "127.0.0.1" );
dest_sin.sin_port = htons( 11111 );
again:
err=connect( sock,(struct sockaddr *) &dest_sin, sizeof( dest_sin));
if(err<0){
//Sleep(1);
sleep(1);
goto again;
}
ssl = SSL_new (ctx);
if(ssl==NULL){
printf("ss new err\n");
return NULL;
}
SSL_set_fd(ssl,sock);
err = SSL_connect (ssl);
if(err<0) {
printf("SSL_connect err\n");
return NULL;
}
printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
server_cert = SSL_get_peer_certificate (ssl);
printf ("Server certificate:\n");
str = X509_NAME_oneline (X509_get_subject_name (server_cert),0,0);
printf ("\t subject: %s\n", str);
OPENSSL_free (str);
str = X509_NAME_oneline (X509_get_issuer_name (server_cert),0,0);
printf ("\t issuer: %s\n", str);
OPENSSL_free (str);
X509_free (server_cert);
//new add
while(1){
char ch_array[1024];
scanf("%s",ch_array);
err = SSL_write(ssl,ch_array,strlen(ch_array));
#if 1
memset(buf,0,1024);
err = SSL_read (ssl, buf, sizeof(buf) - 1);
if(err<0){
printf("ssl read err\n");
return NULL;
}
buf[err] = '\0';
printf ("Got %d chars from server:'%s'\n", err, buf);
#endif
}
//new add
if(err<0) {
printf("ssl write err\n");
return NULL;
}
SSL_shutdown (ssl); /* send SSL/TLS close_notify */
SSL_free (ssl);
close(sock);
}
int main (){
int err,buflen,read;
int sd;
struct sockaddr_in dest_sin;
SOCKET sock;
SSL_CTX *ctx;
SSL *ssl;
X509 *server_cert;
char *str;
char buf[1024];
const SSL_METHOD *method;
int i;
pthread_t pid[MAX_T];
SSLeay_add_ssl_algorithms();
method = TLS_client_method();
SSL_load_error_strings();
ctx = SSL_CTX_new (method);
if(ctx==NULL){
printf("ssl ctx new err\n");
return -1;
}
if (SSL_CTX_use_certificate_file(ctx, CLIENTCERT, SSL_FILETYPE_PEM) <= 0 ){
ERR_print_errors_fp(stderr);
exit(3);
}
if (SSL_CTX_use_PrivateKey_file_pass(ctx, CLIENTKEY, "robot") <= 0){
ERR_print_errors_fp(stderr);
exit(4);
}
s_client_verify=SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE;
SSL_CTX_set_verify(ctx,s_client_verify,verify_callback);
SSL_CTX_load_verify_locations(ctx,CAFILE,NULL);
lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
for (i = 0; i < CRYPTO_num_locks(); i++) {
lock_count[i] = 0;
pthread_mutex_init(&(lock_cs[i]),NULL);
}
CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
for(i = 0; i < MAX_T; i++){
err=pthread_create(&(pid[i]),NULL,&thread_main,(void *)ctx);
if(err!=0){
printf("pthread_create err\n");
continue;
}
}
for (i=0; i<MAX_T; i++){
pthread_join(pid[i],NULL);
}
SSL_CTX_free(ctx);
printf("test ok\n");
return 0;
}