Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/ssl_sock.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "ssl_sock_test.h"
20
21 /* Test direct certificate loading.
22 * For OpenSSL backend only and TEST_LOAD_FROM_FILES must be 1.
23 */
24 #define TEST_LOAD_DIRECT 1
25
26
27 #if INCLUDE_SSLSOCK_TEST
28
29 /* Global vars */
30 static int clients_num;
31
32 struct send_key {
33 pj_ioqueue_op_key_t op_key;
34 };
35
36
37 static int get_cipher_list(void) {
38 pj_status_t status;
39 pj_ssl_cipher ciphers[PJ_SSL_SOCK_MAX_CIPHERS];
40 unsigned cipher_num;
41 unsigned i;
42
43 cipher_num = PJ_ARRAY_SIZE(ciphers);
44 status = pj_ssl_cipher_get_availables(ciphers, &cipher_num);
45 if (status != PJ_SUCCESS) {
46 app_perror("...FAILED to get available ciphers", status);
47 return status;
48 }
49
50 PJ_LOG(3, ("", "...Found %u ciphers:", cipher_num));
51 for (i = 0; i < cipher_num; ++i) {
52 const char* st;
53 st = pj_ssl_cipher_name(ciphers[i]);
54 if (st == NULL)
55 st = "[Unknown]";
56
57 PJ_LOG(3, ("", "...%3u: 0x%08x=%s", i+1, ciphers[i], st));
58 }
59
60 return PJ_SUCCESS;
61 }
62
63
64 struct test_state
65 {
66 pj_pool_t *pool; /* pool */
67 pj_ioqueue_t *ioqueue; /* ioqueue */
68 pj_ssl_sock_t *accepted_ssock; /* accepted server-side socket */
69 pj_bool_t is_server; /* server role flag */
70 pj_bool_t is_verbose; /* verbose flag, e.g: cert info */
71 pj_bool_t echo; /* echo received data */
72 pj_status_t err; /* error flag */
73 pj_size_t sent; /* bytes sent */
74 pj_size_t recv; /* bytes received */
75 pj_uint8_t read_buf[256]; /* read buffer */
76 pj_bool_t done; /* test done flag */
77 char *send_str; /* data to send once connected */
78 pj_size_t send_str_len; /* send data length */
79 pj_bool_t check_echo; /* flag to compare sent & echoed data */
80 const char *check_echo_ptr; /* pointer/cursor for comparing data */
81 struct send_key send_key; /* send op key */
82 };
83
84 static void dump_ssl_info(const pj_ssl_sock_info *si)
85 {
86 const char *tmp_st;
87
88 /* Print cipher name */
89 tmp_st = pj_ssl_cipher_name(si->cipher);
90 if (tmp_st == NULL)
91 tmp_st = "[Unknown]";
92 PJ_LOG(3, ("", ".....Cipher: %s", tmp_st));
93
94 /* Print remote certificate info and verification result */
95 if (si->remote_cert_info && si->remote_cert_info->subject.info.slen)
96 {
97 char buf[2048];
98 const char *verif_msgs[32];
99 unsigned verif_msg_cnt;
100
101 /* Dump remote TLS certificate info */
102 PJ_LOG(3, ("", ".....Remote certificate info:"));
103 pj_ssl_cert_info_dump(si->remote_cert_info, " ", buf, sizeof(buf));
104 PJ_LOG(3,("", "\n%s", buf));
105
106 /* Dump remote TLS certificate verification result */
107 verif_msg_cnt = PJ_ARRAY_SIZE(verif_msgs);
108 pj_ssl_cert_get_verify_status_strings(si->verify_status,
109 verif_msgs, &verif_msg_cnt);
110 PJ_LOG(3,("", ".....Remote certificate verification result: %s",
111 (verif_msg_cnt == 1? verif_msgs[0]:"")));
112 if (verif_msg_cnt > 1) {
113 unsigned i;
114 for (i = 0; i < verif_msg_cnt; ++i)
115 PJ_LOG(3,("", "..... - %s", verif_msgs[i]));
116 }
117 }
118 }
119
120
121 static pj_bool_t ssl_on_connect_complete(pj_ssl_sock_t *ssock,
122 pj_status_t status)
123 {
124 struct test_state *st = (struct test_state*)
125 pj_ssl_sock_get_user_data(ssock);
126 void *read_buf[1];
127 pj_ssl_sock_info info;
128 char buf1[64], buf2[64];
129
130 if (status != PJ_SUCCESS) {
131 app_perror("...ERROR ssl_on_connect_complete()", status);
132 goto on_return;
133 }
134
135 status = pj_ssl_sock_get_info(ssock, &info);
136 if (status != PJ_SUCCESS) {
137 app_perror("...ERROR pj_ssl_sock_get_info()", status);
138 goto on_return;
139 }
140
141 pj_sockaddr_print((pj_sockaddr_t*)&info.local_addr, buf1, sizeof(buf1), 1);
142 pj_sockaddr_print((pj_sockaddr_t*)&info.remote_addr, buf2, sizeof(buf2), 1);
143 PJ_LOG(3, ("", "...Connected %s -> %s!", buf1, buf2));
144
145 if (st->is_verbose)
146 dump_ssl_info(&info);
147
148 /* Start reading data */
149 read_buf[0] = st->read_buf;
150 status = pj_ssl_sock_start_read2(ssock, st->pool, sizeof(st->read_buf), (void**)read_buf, 0);
151 if (status != PJ_SUCCESS) {
152 app_perror("...ERROR pj_ssl_sock_start_read2()", status);
153 goto on_return;
154 }
155
156 /* Start sending data */
157 while (st->sent < st->send_str_len) {
158 pj_ssize_t size;
159
160 size = st->send_str_len - st->sent;
161 status = pj_ssl_sock_send(ssock, (pj_ioqueue_op_key_t*)&st->send_key,
162 st->send_str + st->sent, &size, 0);
163 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
164 app_perror("...ERROR pj_ssl_sock_send()", status);
165 goto on_return;
166 }
167
168 if (status == PJ_SUCCESS) {
169 st->sent += size;
170 } else {
171 if (status == PJ_EPENDING)
172 status = PJ_SUCCESS;
173 break;
174 }
175 }
176
177 on_return:
178 st->err = status;
179
180 if (st->err != PJ_SUCCESS) {
181 pj_ssl_sock_close(ssock);
182 clients_num--;
183 return PJ_FALSE;
184 }
185
186 return PJ_TRUE;
187 }
188
189
190 static pj_bool_t ssl_on_accept_complete(pj_ssl_sock_t *ssock,
191 pj_ssl_sock_t *newsock,
192 const pj_sockaddr_t *src_addr,
193 int src_addr_len,
194 pj_status_t accept_status)
195 {
196 struct test_state *parent_st = (struct test_state*)
197 pj_ssl_sock_get_user_data(ssock);
198 struct test_state *st;
199 void *read_buf[1];
200 pj_ssl_sock_info info;
201 char buf[64];
202 pj_status_t status;
203
204 PJ_UNUSED_ARG(src_addr_len);
205
206 if (accept_status != PJ_SUCCESS) {
207 if (newsock) {
208 st = (struct test_state*) pj_ssl_sock_get_user_data(newsock);
209 st->err = accept_status;
210 }
211 return PJ_FALSE;
212 }
213
214 /* Duplicate parent test state to newly accepted test state */
215 st = (struct test_state*)pj_pool_zalloc(parent_st->pool, sizeof(struct test_state));
216 *st = *parent_st;
217 pj_ssl_sock_set_user_data(newsock, st);
218
219 /* Track accepted socket for cleanup */
220 parent_st->accepted_ssock = newsock;
221
222 status = pj_ssl_sock_get_info(newsock, &info);
223 if (status != PJ_SUCCESS) {
224 app_perror("...ERROR pj_ssl_sock_get_info()", status);
225 goto on_return;
226 }
227
228 pj_sockaddr_print(src_addr, buf, sizeof(buf), 1);
229 PJ_LOG(3, ("", "...Accepted connection from %s", buf));
230
231 if (st->is_verbose)
232 dump_ssl_info(&info);
233
234 /* Start reading data */
235 read_buf[0] = st->read_buf;
236 status = pj_ssl_sock_start_read2(newsock, st->pool, sizeof(st->read_buf), (void**)read_buf, 0);
237 if (status != PJ_SUCCESS) {
238 app_perror("...ERROR pj_ssl_sock_start_read2()", status);
239 goto on_return;
240 }
241
242 /* Start sending data */
243 while (st->sent < st->send_str_len) {
244 pj_ssize_t size;
245
246 size = st->send_str_len - st->sent;
247 status = pj_ssl_sock_send(newsock, (pj_ioqueue_op_key_t*)&st->send_key,
248 st->send_str + st->sent, &size, 0);
249 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
250 app_perror("...ERROR pj_ssl_sock_send()", status);
251 goto on_return;
252 }
253
254 if (status == PJ_SUCCESS) {
255 st->sent += size;
256 } else {
257 if (status == PJ_EPENDING)
258 status = PJ_SUCCESS;
259 break;
260 }
261 }
262
263 on_return:
264 st->err = status;
265
266 if (st->err != PJ_SUCCESS) {
267 pj_ssl_sock_close(newsock);
268 return PJ_FALSE;
269 }
270
271 return PJ_TRUE;
272 }
273
274 static pj_bool_t ssl_on_data_read(pj_ssl_sock_t *ssock,
275 void *data,
276 pj_size_t size,
277 pj_status_t status,
278 pj_size_t *remainder)
279 {
280 struct test_state *st = (struct test_state*)
281 pj_ssl_sock_get_user_data(ssock);
282
283 PJ_UNUSED_ARG(remainder);
284 PJ_UNUSED_ARG(data);
285
286 if (size > 0) {
287 pj_size_t consumed;
288
289 /* Set random remainder */
290 *remainder = pj_rand() % 100;
291
292 /* Apply zero remainder if:
293 * - remainder is less than size, or
294 * - connection closed/error
295 * - echo/check_eco set
296 */
297 if (*remainder > size || status != PJ_SUCCESS || st->echo || st->check_echo)
298 *remainder = 0;
299
300 consumed = size - *remainder;
301 st->recv += consumed;
302
303 //printf("%.*s", consumed, (char*)data);
304
305 pj_memmove(data, (char*)data + consumed, *remainder);
306
307 /* Echo data when specified to */
308 if (st->echo) {
309 pj_ssize_t size_ = consumed;
310 status = pj_ssl_sock_send(ssock, (pj_ioqueue_op_key_t*)&st->send_key, data, &size_, 0);
311 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
312 app_perror("...ERROR pj_ssl_sock_send()", status);
313 goto on_return;
314 }
315
316 if (status == PJ_SUCCESS) {
317 st->sent += size_;
318 } else if (status == PJ_EPENDING) {
319 status = PJ_SUCCESS;
320 }
321 }
322
323 /* Verify echoed data when specified to */
324 if (st->check_echo) {
325 if (!st->check_echo_ptr)
326 st->check_echo_ptr = st->send_str;
327
328 if (pj_memcmp(st->check_echo_ptr, data, consumed)) {
329 status = PJ_EINVAL;
330 app_perror("...ERROR echoed data not exact", status);
331 goto on_return;
332 }
333 st->check_echo_ptr += consumed;
334
335 /* Echo received completely */
336 if (st->send_str_len == st->recv) {
337 pj_ssl_sock_info info;
338 char buf[64];
339
340 status = pj_ssl_sock_get_info(ssock, &info);
341 if (status != PJ_SUCCESS) {
342 app_perror("...ERROR pj_ssl_sock_get_info()", status);
343 goto on_return;
344 }
345
346 pj_sockaddr_print((pj_sockaddr_t*)&info.local_addr, buf, sizeof(buf), 1);
347 PJ_LOG(3, ("", "...%s successfully recv %lu bytes echo", buf,
348 (unsigned long)st->recv));
349 st->done = PJ_TRUE;
350 }
351 }
352 }
353
354 if (status != PJ_SUCCESS) {
355 if (status == PJ_EEOF) {
356 status = PJ_SUCCESS;
357 st->done = PJ_TRUE;
358 } else {
359 app_perror("...ERROR ssl_on_data_read()", status);
360 }
361 }
362
363 on_return:
364 st->err = status;
365
366 if (st->err != PJ_SUCCESS || st->done) {
367 pj_ssl_sock_close(ssock);
368 if (!st->is_server)
369 clients_num--;
370 return PJ_FALSE;
371 }
372
373 return PJ_TRUE;
374 }
375
376 static pj_bool_t ssl_on_data_sent(pj_ssl_sock_t *ssock,
377 pj_ioqueue_op_key_t *op_key,
378 pj_ssize_t sent)
379 {
380 struct test_state *st = (struct test_state*)
381 pj_ssl_sock_get_user_data(ssock);
382 PJ_UNUSED_ARG(op_key);
383
384 if (sent < 0) {
385 st->err = (pj_status_t)-sent;
386 } else {
387 st->sent += sent;
388
389 /* Send more if any */
390 while (st->sent < st->send_str_len) {
391 pj_ssize_t size;
392 pj_status_t status;
393
394 size = st->send_str_len - st->sent;
395 status = pj_ssl_sock_send(ssock, (pj_ioqueue_op_key_t*)&st->send_key,
396 st->send_str + st->sent, &size, 0);
397 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
398 app_perror("...ERROR pj_ssl_sock_send()", status);
399 st->err = status;
400 break;
401 }
402
403 if (status == PJ_SUCCESS)
404 st->sent += size;
405 else
406 break;
407 }
408 }
409
410 if (st->err != PJ_SUCCESS) {
411 pj_ssl_sock_close(ssock);
412 if (!st->is_server)
413 clients_num--;
414 return PJ_FALSE;
415 }
416
417 return PJ_TRUE;
418 }
419
420 #define HTTP_SERVER_ADDR "www.pjsip.org"
421 #define HTTP_SERVER_PORT 443
422 #define HTTP_REQ "GET https://" HTTP_SERVER_ADDR "/ HTTP/1.0\r\n\r\n";
423
424 static int https_client_test(unsigned ms_timeout)
425 {
426 pj_pool_t *pool = NULL;
427 pj_ioqueue_t *ioqueue = NULL;
428 pj_timer_heap_t *timer = NULL;
429 pj_ssl_sock_t *ssock = NULL;
430 pj_ssl_sock_param param;
431 pj_status_t status;
432 struct test_state state = {0};
433 pj_sockaddr local_addr, rem_addr;
434 pj_str_t tmp_st;
435
436 pool = pj_pool_create(mem, "https_get", 256, 256, NULL);
437
438 status = pj_ioqueue_create(pool, 4, &ioqueue);
439 if (status != PJ_SUCCESS) {
440 goto on_return;
441 }
442
443 status = pj_timer_heap_create(pool, 4, &timer);
444 if (status != PJ_SUCCESS) {
445 goto on_return;
446 }
447
448 state.pool = pool;
449 state.send_str = HTTP_REQ;
450 state.send_str_len = pj_ansi_strlen(state.send_str);
451 state.is_verbose = PJ_TRUE;
452
453 pj_ssl_sock_param_default(&param);
454 param.cb.on_connect_complete = &ssl_on_connect_complete;
455 param.cb.on_data_read = &ssl_on_data_read;
456 param.cb.on_data_sent = &ssl_on_data_sent;
457 param.ioqueue = ioqueue;
458 param.user_data = &state;
459 param.server_name = pj_str((char*)HTTP_SERVER_ADDR);
460 param.timer_heap = timer;
461 param.timeout.sec = 0;
462 param.timeout.msec = ms_timeout;
463 pj_time_val_normalize(&param.timeout);
464
465 status = pj_ssl_sock_create(pool, &param, &ssock);
466 if (status != PJ_SUCCESS) {
467 goto on_return;
468 }
469
470 pj_sockaddr_init(PJ_AF_INET, &local_addr, pj_strset2(&tmp_st, "0.0.0.0"), 0);
471 pj_sockaddr_init(PJ_AF_INET, &rem_addr, pj_strset2(&tmp_st, HTTP_SERVER_ADDR), HTTP_SERVER_PORT);
472 status = pj_ssl_sock_start_connect(ssock, pool, &local_addr, &rem_addr, pj_sockaddr_get_len(&local_addr));
473 if (status == PJ_SUCCESS) {
474 ssl_on_connect_complete(ssock, PJ_SUCCESS);
475 } else if (status == PJ_EPENDING) {
476 status = PJ_SUCCESS;
477 } else {
478 goto on_return;
479 }
480
481 /* Wait until everything has been sent/received */
482 while (state.err == PJ_SUCCESS && !state.done) {
483 #ifdef PJ_SYMBIAN
484 pj_symbianos_poll(-1, 1000);
485 #else
486 pj_time_val delay = {0, 100};
487 pj_ioqueue_poll(ioqueue, &delay);
488 pj_timer_heap_poll(timer, &delay);
489 #endif
490 }
491
492 if (state.err) {
493 status = state.err;
494 goto on_return;
495 }
496
497 PJ_LOG(3, ("", "...Done!"));
498 PJ_LOG(3, ("", ".....Sent/recv: %lu/%lu bytes", (unsigned long)state.sent,
499 (unsigned long)state.recv));
500
501 on_return:
502 if (ssock && !state.err && !state.done)
503 pj_ssl_sock_close(ssock);
504 if (ioqueue)
505 pj_ioqueue_destroy(ioqueue);
506 if (timer)
507 pj_timer_heap_destroy(timer);
508 if (pool)
509 pj_pool_release(pool);
510
511 return status;
512 }
513
514 #if !(defined(TEST_LOAD_FROM_FILES) && TEST_LOAD_FROM_FILES==1)
515 static pj_status_t load_cert_to_buf(pj_pool_t *pool, const pj_str_t *file_name,
516 pj_ssl_cert_buffer *buf)
517 {
518 pj_status_t status;
519 pj_oshandle_t fd = 0;
520 pj_ssize_t size = (pj_ssize_t)pj_file_size(file_name->ptr);
521
522 status = pj_file_open(pool, file_name->ptr, PJ_O_RDONLY, &fd);
523 if (status != PJ_SUCCESS)
524 return status;
525
526 buf->ptr = (char*)pj_pool_zalloc(pool, size+1);
527 status = pj_file_read(fd, buf->ptr, &size);
528 buf->slen = size;
529
530 pj_file_close(fd);
531 fd = NULL;
532 return status;
533 }
534 #endif
535
536 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL)
537
538 static pj_status_t load_cert_from_store(pj_pool_t *pool,
539 pj_ssl_cert_t **p_cert)
540 {
541 #if 0
542 /* To test loading certificate from the store, follow these steps:
543 * 1. Install the certificate & the private-key pair to the store,
544 * and optionally set the friendly-name for it.
545 * 2. Update the lookup criteria below (field & keyword).
546 */
547 pj_ssl_cert_lookup_criteria crit = {0};
548
549 /* Lookup by subject */
550 crit.type = PJ_SSL_CERT_LOOKUP_SUBJECT;
551 pj_cstr(&crit.keyword, "test.pjsip.org");
552
553 /* Lookup by friendly-name */
554 //crit.type = PJ_SSL_CERT_LOOKUP_FRIENDLY_NAME;
555 //pj_cstr(&crit.keyword, "schannel-test");
556
557 /* Lookup by fingerprint */
558 //crit.type = PJ_SSL_CERT_LOOKUP_FINGERPRINT;
559 //pj_cstr(&crit.keyword, "\x08\x3a\x6c\xdc\xd0\x19\x59\xec\x28\xc3"
560 // "\x81\xb8\xc0\x21\x09\xe9\xd5\xf6\x57\x7d");
561
562 return pj_ssl_cert_load_from_store(pool, &crit, p_cert);
563 #else
564 /* Set no certificate, Schannel will use self-signed cert */
565 PJ_UNUSED_ARG(pool);
566 PJ_UNUSED_ARG(p_cert);
567 return PJ_ENOTFOUND;
568 #endif
569 }
570
571 #endif
572
573 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
574
575 #include <openssl/bio.h>
576 #include <openssl/pem.h>
577
578 static int pass_cb(char *buf, int size, int rwflag, void *u)
579 {
580 pj_str_t pass = pj_str(CERT_PRIVKEY_PASS);
581
582 PJ_UNUSED_ARG(rwflag);
583 PJ_UNUSED_ARG(u);
584
585 if (pass.slen > size)
586 pass.slen = size;
587 pj_memcpy(buf, pass.ptr, pass.slen);
588
589 return (int)pass.slen;
590 }
591
592 static pj_status_t load_cert_direct(pj_pool_t *pool,
593 pj_bool_t with_cert,
594 pj_bool_t with_privkey,
595 pj_ssl_cert_t **p_cert)
596 {
597 BIO *in;
598 pj_ssl_cert_direct cd;
599
600 PJ_UNUSED_ARG(pool);
601
602 /* Init credential */
603 pj_bzero(&cd, sizeof(cd));
604
605 /* Load private key */
606 if (with_privkey) {
607 EVP_PKEY *key = NULL;
608
609 in = BIO_new_file(CERT_PRIVKEY_FILE, "r");
610 if (!in)
611 return PJ_ENOTFOUND;
612
613 key = PEM_read_bio_PrivateKey(in, NULL, &pass_cb, NULL);
614 BIO_free(in);
615 if (!key)
616 return PJ_EINVAL;
617
618 cd.type |= PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY;
619 cd.privkey = key;
620 }
621
622 /* Load certificate */
623 if (with_cert) {
624 X509* x = NULL;
625
626 in = BIO_new_file(CERT_FILE, "r");
627 if (!in)
628 return PJ_ENOTFOUND;
629
630 x = PEM_read_bio_X509(in, NULL, 0, NULL);
631 BIO_free(in);
632 if (!x)
633 return PJ_EINVAL;
634
635 cd.type |= PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT;
636 cd.cert = x;
637 }
638
639 /* Create credential */
640 return pj_ssl_cert_load_direct(pool, &cd, p_cert);
641 }
642 #else
643 # define load_cert_direct(pool,p_cert)
644 #endif
645
646 static int echo_test(pj_ssl_sock_proto srv_proto, pj_ssl_sock_proto cli_proto,
647 pj_ssl_cipher srv_cipher, pj_ssl_cipher cli_cipher,
648 pj_bool_t req_client_cert, pj_bool_t client_provide_cert)
649 {
650 pj_pool_t *pool = NULL;
651 pj_ioqueue_t *ioqueue = NULL;
652 pj_timer_heap_t *timer = NULL;
653 pj_ssl_sock_t *ssock_serv = NULL;
654 pj_ssl_sock_t *ssock_cli = NULL;
655 pj_ssl_sock_param param;
656 struct test_state state_serv = { 0 };
657 struct test_state state_cli = { 0 };
658 pj_sockaddr addr, listen_addr;
659 pj_ssl_cipher ciphers[1];
660 pj_ssl_cert_t *cert = NULL;
661 pj_status_t status;
662
663 pool = pj_pool_create(mem, "ssl_echo", 256, 256, NULL);
664
665 status = pj_ioqueue_create(pool, 4, &ioqueue);
666 if (status != PJ_SUCCESS) {
667 goto on_return;
668 }
669
670 status = pj_timer_heap_create(pool, 4, &timer);
671 if (status != PJ_SUCCESS) {
672 goto on_return;
673 }
674
675 pj_ssl_sock_param_default(&param);
676 param.cb.on_accept_complete2 = &ssl_on_accept_complete;
677 param.cb.on_connect_complete = &ssl_on_connect_complete;
678 param.cb.on_data_read = &ssl_on_data_read;
679 param.cb.on_data_sent = &ssl_on_data_sent;
680 param.ioqueue = ioqueue;
681 param.timer_heap = timer;
682 param.ciphers = ciphers;
683
684 /* Init default bind address */
685 {
686 pj_str_t tmp_st;
687 pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0);
688 }
689
690 /* === SERVER === */
691 param.proto = srv_proto;
692 param.user_data = &state_serv;
693 param.ciphers_num = (srv_cipher == -1)? 0 : 1;
694 param.require_client_cert = req_client_cert;
695 ciphers[0] = srv_cipher;
696
697 state_serv.pool = pool;
698 state_serv.echo = PJ_TRUE;
699 state_serv.is_server = PJ_TRUE;
700 state_serv.is_verbose = PJ_TRUE;
701
702 status = pj_ssl_sock_create(pool, &param, &ssock_serv);
703 if (status != PJ_SUCCESS) {
704 goto on_return;
705 }
706
707 /* Set server cert */
708 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL)
709 /* Schannel backend currently can only load certificates from
710 * OS cert store. If the certificate loading fails, we'll skip setting
711 * certificate, so the SSL socket will create & use a self-signed cert.
712 */
713 status = load_cert_from_store(pool, &cert);
714 if (status == PJ_SUCCESS) {
715 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
716 if (status != PJ_SUCCESS) {
717 goto on_return;
718 }
719 }
720 #else
721 {
722 pj_str_t ca_file = pj_str(CERT_CA_FILE);
723 pj_str_t cert_file = pj_str(CERT_FILE);
724 pj_str_t privkey_file = pj_str(CERT_PRIVKEY_FILE);
725 pj_str_t privkey_pass = pj_str(CERT_PRIVKEY_PASS);
726
727 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_APPLE)
728 /* We store private key in Keychain. */
729 privkey_file = pj_str("");
730 privkey_pass = pj_str("");
731 #endif
732
733 #if (defined(TEST_LOAD_FROM_FILES) && TEST_LOAD_FROM_FILES==1)
734
735 # if TEST_LOAD_DIRECT && (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
736 /* Certificate & private key is not loaded from files, but directly
737 * using OpenSSL objects.
738 */
739 status = load_cert_direct(pool, PJ_TRUE, PJ_TRUE, &cert);
740
741 /* Skip setting privkey & certificate files below */
742 privkey_file.slen = 0;
743 cert_file.slen = 0;
744 # endif
745
746 status = pj_ssl_cert_load_from_files(pool, &ca_file, &cert_file,
747 &privkey_file, &privkey_pass,
748 &cert);
749 #else
750 pj_ssl_cert_buffer ca_buf, cert_buf, privkey_buf;
751
752 status = load_cert_to_buf(pool, &ca_file, &ca_buf);
753 if (status != PJ_SUCCESS) {
754 goto on_return;
755 }
756
757 status = load_cert_to_buf(pool, &cert_file, &cert_buf);
758 if (status != PJ_SUCCESS) {
759 goto on_return;
760 }
761
762 status = load_cert_to_buf(pool, &privkey_file, &privkey_buf);
763 if (status != PJ_SUCCESS) {
764 goto on_return;
765 }
766
767 status = pj_ssl_cert_load_from_buffer(pool, &ca_buf, &cert_buf,
768 &privkey_buf, &privkey_pass,
769 &cert);
770 #endif
771 if (status != PJ_SUCCESS) {
772 goto on_return;
773 }
774
775 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
776 if (status != PJ_SUCCESS) {
777 goto on_return;
778 }
779 }
780 #endif
781
782 status = pj_ssl_sock_start_accept(ssock_serv, pool, &addr, pj_sockaddr_get_len(&addr));
783 if (status != PJ_SUCCESS) {
784 goto on_return;
785 }
786
787 /* Get listener address */
788 {
789 pj_ssl_sock_info info;
790
791 pj_ssl_sock_get_info(ssock_serv, &info);
792 pj_sockaddr_cp(&listen_addr, &info.local_addr);
793 }
794
795 /* === CLIENT === */
796 param.proto = cli_proto;
797 param.user_data = &state_cli;
798 param.ciphers_num = (cli_cipher == -1)? 0 : 1;
799 ciphers[0] = cli_cipher;
800
801 state_cli.pool = pool;
802 state_cli.check_echo = PJ_TRUE;
803 state_cli.is_verbose = PJ_TRUE;
804
805 {
806 /* srand() should be done centrally (blp)
807 pj_time_val now;
808
809 pj_gettimeofday(&now);
810 pj_srand((unsigned)now.sec);
811 */
812 state_cli.send_str_len = (pj_rand() % 5 + 1) * 1024 + pj_rand() % 1024;
813 }
814 state_cli.send_str = (char*)pj_pool_alloc(pool, state_cli.send_str_len);
815 {
816 unsigned i;
817 for (i = 0; i < state_cli.send_str_len; ++i)
818 state_cli.send_str[i] = (char)(pj_rand() % 256);
819 }
820
821 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
822 if (status != PJ_SUCCESS) {
823 goto on_return;
824 }
825
826 /* Set cert for client.
827 * Reusing certificate for server above, but if client_provide_cert
828 * is not set, override the certificate with CA certificate.
829 */
830 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL)
831 if (client_provide_cert) {
832 status = load_cert_from_store(pool, &cert);
833 if (status == PJ_SUCCESS)
834 status = pj_ssl_sock_set_certificate(ssock_cli, pool, cert);
835
836 if (status != PJ_SUCCESS) {
837 goto on_return;
838 }
839 }
840 #else
841 {
842 if (!client_provide_cert) {
843 pj_str_t ca_file = pj_str(CERT_CA_FILE);
844 pj_str_t null_str = pj_str("");
845 pj_ssl_cert_direct cert_direct;
846
847 #if (defined(TEST_LOAD_FROM_FILES) && TEST_LOAD_FROM_FILES==1)
848 /* Reset any certificate & private keys previously set */
849 status = pj_ssl_cert_load_from_files(pool, &ca_file, &null_str,
850 &null_str, &null_str, &cert);
851
852 /* Also reset any direct backend objects */
853 pj_bzero(&cert_direct, sizeof(cert_direct));
854 pj_ssl_cert_load_direct(pool, &cert_direct, &cert);
855 #else
856 pj_ssl_cert_buffer null_buf, ca_buf;
857
858 null_buf.slen = 0;
859
860 status = load_cert_to_buf(pool, &ca_file, &ca_buf);
861 if (status != PJ_SUCCESS) {
862 goto on_return;
863 }
864
865 status = pj_ssl_cert_load_from_buffer(pool, &ca_buf, &null_buf,
866 &null_buf, &null_str, &cert);
867 #endif
868 if (status != PJ_SUCCESS) {
869 goto on_return;
870 }
871
872 }
873
874 status = pj_ssl_sock_set_certificate(ssock_cli, pool, cert);
875 if (status != PJ_SUCCESS) {
876 goto on_return;
877 }
878 }
879 #endif
880
881 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr, &listen_addr, pj_sockaddr_get_len(&addr));
882 if (status == PJ_SUCCESS) {
883 ssl_on_connect_complete(ssock_cli, PJ_SUCCESS);
884 } else if (status == PJ_EPENDING) {
885 status = PJ_SUCCESS;
886 } else {
887 goto on_return;
888 }
889
890 /* Wait until everything has been sent/received or error */
891 while (!state_serv.err && !state_cli.err && !state_serv.done && !state_cli.done)
892 {
893 #ifdef PJ_SYMBIAN
894 pj_symbianos_poll(-1, 1000);
895 #else
896 pj_time_val delay = {0, 100};
897 pj_ioqueue_poll(ioqueue, &delay);
898 #endif
899 }
900
901 /* Clean up sockets */
902 {
903 /* The delay must be at least PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT. */
904 pj_time_val delay = {0, 500};
905 while (pj_ioqueue_poll(ioqueue, &delay) > 0);
906 pj_timer_heap_poll(timer, &delay);
907 }
908
909 if (state_serv.err || state_cli.err) {
910 if (state_serv.err != PJ_SUCCESS)
911 status = state_serv.err;
912 else
913 status = state_cli.err;
914
915 goto on_return;
916 }
917
918 PJ_LOG(3, ("", "...Done!"));
919 PJ_LOG(3, ("", ".....Sent/recv: %lu/%lu bytes", (unsigned long)state_cli.sent,
920 (unsigned long)state_cli.recv));
921
922 on_return:
923 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_DARWIN) || \
924 (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_APPLE)
925 if (status != PJ_SUCCESS) {
926 PJ_LOG(3, ("", "Apple SSL requires the private key to be "
927 "inside the Keychain. So double click on "
928 "the file pjlib/build/privkey.p12 to "
929 "place it in the Keychain. "
930 "The password is \"pjsip\"."));
931 }
932 #endif
933
934 if (ssock_serv)
935 pj_ssl_sock_close(ssock_serv);
936 if (ssock_cli && !state_cli.err && !state_cli.done)
937 pj_ssl_sock_close(ssock_cli);
938 if (ioqueue)
939 pj_ioqueue_destroy(ioqueue);
940 if (timer)
941 pj_timer_heap_destroy(timer);
942 if (pool)
943 pj_pool_release(pool);
944
945 return status;
946 }
947
948
949 static pj_bool_t asock_on_data_read(pj_activesock_t *asock,
950 void *data,
951 pj_size_t size,
952 pj_status_t status,
953 pj_size_t *remainder)
954 {
955 struct test_state *st = (struct test_state*)
956 pj_activesock_get_user_data(asock);
957
958 PJ_UNUSED_ARG(data);
959 PJ_UNUSED_ARG(size);
960 PJ_UNUSED_ARG(remainder);
961
962 if (status != PJ_SUCCESS) {
963 if (status == PJ_EEOF) {
964 status = PJ_SUCCESS;
965 st->done = PJ_TRUE;
966 } else {
967 app_perror("...ERROR asock_on_data_read()", status);
968 }
969 }
970
971 st->err = status;
972
973 if (st->err != PJ_SUCCESS || st->done) {
974 pj_activesock_close(asock);
975 if (!st->is_server)
976 clients_num--;
977 return PJ_FALSE;
978 }
979
980 return PJ_TRUE;
981 }
982
983
984 static pj_bool_t asock_on_connect_complete(pj_activesock_t *asock,
985 pj_status_t status)
986 {
987 struct test_state *st = (struct test_state*)
988 pj_activesock_get_user_data(asock);
989
990 if (status == PJ_SUCCESS) {
991 void *read_buf[1];
992
993 /* Start reading data */
994 read_buf[0] = st->read_buf;
995 status = pj_activesock_start_read2(asock, st->pool, sizeof(st->read_buf), (void**)read_buf, 0);
996 if (status != PJ_SUCCESS) {
997 app_perror("...ERROR pj_ssl_sock_start_read2()", status);
998 }
999 }
1000
1001 st->err = status;
1002
1003 if (st->err != PJ_SUCCESS) {
1004 pj_activesock_close(asock);
1005 if (!st->is_server)
1006 clients_num--;
1007 return PJ_FALSE;
1008 }
1009
1010 return PJ_TRUE;
1011 }
1012
1013 static pj_bool_t asock_on_accept_complete(pj_activesock_t *asock,
1014 pj_sock_t newsock,
1015 const pj_sockaddr_t *src_addr,
1016 int src_addr_len)
1017 {
1018 struct test_state *st;
1019 void *read_buf[1];
1020 pj_activesock_t *new_asock;
1021 pj_activesock_cb asock_cb = { 0 };
1022 pj_status_t status;
1023
1024 PJ_UNUSED_ARG(src_addr);
1025 PJ_UNUSED_ARG(src_addr_len);
1026
1027 st = (struct test_state*) pj_activesock_get_user_data(asock);
1028
1029 asock_cb.on_data_read = &asock_on_data_read;
1030 status = pj_activesock_create(st->pool, newsock, pj_SOCK_STREAM(), NULL,
1031 st->ioqueue, &asock_cb, st, &new_asock);
1032 if (status != PJ_SUCCESS) {
1033 goto on_return;
1034 }
1035
1036 /* Start reading data */
1037 read_buf[0] = st->read_buf;
1038 status = pj_activesock_start_read2(new_asock, st->pool,
1039 sizeof(st->read_buf),
1040 (void**)read_buf, 0);
1041 if (status != PJ_SUCCESS) {
1042 app_perror("...ERROR pj_ssl_sock_start_read2()", status);
1043 }
1044
1045 on_return:
1046 st->err = status;
1047
1048 if (st->err != PJ_SUCCESS)
1049 pj_activesock_close(new_asock);
1050
1051 return PJ_TRUE;
1052 }
1053
1054
1055 /* Raw TCP socket try to connect to SSL socket server, once
1056 * connection established, it will just do nothing, SSL socket
1057 * server should be able to close the connection after specified
1058 * timeout period (set ms_timeout to 0 to disable timer).
1059 */
1060 static int client_non_ssl(unsigned ms_timeout)
1061 {
1062 pj_pool_t *pool = NULL;
1063 pj_ioqueue_t *ioqueue = NULL;
1064 pj_timer_heap_t *timer = NULL;
1065 pj_ssl_sock_t *ssock_serv = NULL;
1066 pj_activesock_t *asock_cli = NULL;
1067 pj_activesock_cb asock_cb = { 0 };
1068 pj_sock_t sock = PJ_INVALID_SOCKET;
1069 pj_ssl_sock_param param;
1070 struct test_state state_serv = { 0 };
1071 struct test_state state_cli = { 0 };
1072 pj_sockaddr listen_addr;
1073 pj_ssl_cert_t *cert = NULL;
1074 pj_status_t status;
1075
1076 pool = pj_pool_create(mem, "ssl_accept_raw_tcp", 256, 256, NULL);
1077
1078 status = pj_ioqueue_create(pool, 4, &ioqueue);
1079 if (status != PJ_SUCCESS) {
1080 goto on_return;
1081 }
1082
1083 status = pj_timer_heap_create(pool, 4, &timer);
1084 if (status != PJ_SUCCESS) {
1085 goto on_return;
1086 }
1087
1088 pj_ssl_sock_param_default(&param);
1089 param.cb.on_accept_complete2 = &ssl_on_accept_complete;
1090 param.cb.on_data_read = &ssl_on_data_read;
1091 param.cb.on_data_sent = &ssl_on_data_sent;
1092 param.ioqueue = ioqueue;
1093 param.timer_heap = timer;
1094 param.timeout.sec = 0;
1095 param.timeout.msec = ms_timeout;
1096 pj_time_val_normalize(&param.timeout);
1097
1098 /* SERVER */
1099 param.user_data = &state_serv;
1100 state_serv.pool = pool;
1101 state_serv.is_server = PJ_TRUE;
1102 state_serv.is_verbose = PJ_TRUE;
1103
1104 status = pj_ssl_sock_create(pool, &param, &ssock_serv);
1105 if (status != PJ_SUCCESS) {
1106 goto on_return;
1107 }
1108
1109 /* Set cert */
1110 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL)
1111 /* Schannel backend currently can only load certificates from
1112 * OS cert store. If the certificate loading fails, we'll skip setting
1113 * certificate, so the SSL socket will create & use a self-signed cert.
1114 */
1115 status = load_cert_from_store(pool, &cert);
1116 if (status == PJ_SUCCESS) {
1117 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
1118 if (status != PJ_SUCCESS) {
1119 goto on_return;
1120 }
1121 }
1122 #else
1123 {
1124 pj_str_t ca_file = pj_str(CERT_CA_FILE);
1125 pj_str_t cert_file = pj_str(CERT_FILE);
1126 pj_str_t privkey_file = pj_str(CERT_PRIVKEY_FILE);
1127 pj_str_t privkey_pass = pj_str(CERT_PRIVKEY_PASS);
1128
1129 #if (defined(TEST_LOAD_FROM_FILES) && TEST_LOAD_FROM_FILES==1)
1130 status = pj_ssl_cert_load_from_files(pool, &ca_file, &cert_file,
1131 &privkey_file, &privkey_pass,
1132 &cert);
1133 #else
1134 pj_ssl_cert_buffer ca_buf, cert_buf, privkey_buf;
1135
1136 status = load_cert_to_buf(pool, &ca_file, &ca_buf);
1137 if (status != PJ_SUCCESS) {
1138 goto on_return;
1139 }
1140
1141 status = load_cert_to_buf(pool, &cert_file, &cert_buf);
1142 if (status != PJ_SUCCESS) {
1143 goto on_return;
1144 }
1145
1146 status = load_cert_to_buf(pool, &privkey_file, &privkey_buf);
1147 if (status != PJ_SUCCESS) {
1148 goto on_return;
1149 }
1150
1151 status = pj_ssl_cert_load_from_buffer(pool, &ca_buf, &cert_buf,
1152 &privkey_buf, &privkey_pass,
1153 &cert);
1154 #endif
1155 if (status != PJ_SUCCESS) {
1156 goto on_return;
1157 }
1158 }
1159
1160 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
1161 if (status != PJ_SUCCESS) {
1162 goto on_return;
1163 }
1164 #endif
1165
1166 /* Init bind address */
1167 {
1168 pj_str_t tmp_st;
1169 pj_sockaddr_init(PJ_AF_INET, &listen_addr, pj_strset2(&tmp_st, "127.0.0.1"), 0);
1170 }
1171
1172 status = pj_ssl_sock_start_accept(ssock_serv, pool, &listen_addr, pj_sockaddr_get_len(&listen_addr));
1173 if (status != PJ_SUCCESS) {
1174 goto on_return;
1175 }
1176
1177 /* Update listener address */
1178 {
1179 pj_ssl_sock_info info;
1180
1181 pj_ssl_sock_get_info(ssock_serv, &info);
1182 pj_sockaddr_cp(&listen_addr, &info.local_addr);
1183 }
1184
1185 /* CLIENT */
1186 state_cli.pool = pool;
1187 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock);
1188 if (status != PJ_SUCCESS) {
1189 goto on_return;
1190 }
1191
1192 asock_cb.on_connect_complete = &asock_on_connect_complete;
1193 asock_cb.on_data_read = &asock_on_data_read;
1194 status = pj_activesock_create(pool, sock, pj_SOCK_STREAM(), NULL,
1195 ioqueue, &asock_cb, &state_cli, &asock_cli);
1196 if (status != PJ_SUCCESS) {
1197 goto on_return;
1198 }
1199
1200 status = pj_activesock_start_connect(asock_cli, pool, (pj_sockaddr_t*)&listen_addr,
1201 pj_sockaddr_get_len(&listen_addr));
1202 if (status == PJ_SUCCESS) {
1203 asock_on_connect_complete(asock_cli, PJ_SUCCESS);
1204 } else if (status == PJ_EPENDING) {
1205 status = PJ_SUCCESS;
1206 } else {
1207 goto on_return;
1208 }
1209
1210 /* Wait until everything has been sent/received or error */
1211 while (!state_serv.err && !state_cli.err && !state_serv.done && !state_cli.done)
1212 {
1213 #ifdef PJ_SYMBIAN
1214 pj_symbianos_poll(-1, 1000);
1215 #else
1216 pj_time_val delay = {0, 100};
1217 pj_ioqueue_poll(ioqueue, &delay);
1218 pj_timer_heap_poll(timer, &delay);
1219 #endif
1220 }
1221
1222 if (state_serv.err || state_cli.err) {
1223 if (state_serv.err != PJ_SUCCESS)
1224 status = state_serv.err;
1225 else
1226 status = state_cli.err;
1227
1228 goto on_return;
1229 }
1230
1231 PJ_LOG(3, ("", "...Done!"));
1232
1233 on_return:
1234 if (asock_cli && !state_cli.err && !state_cli.done)
1235 pj_activesock_close(asock_cli);
1236 if (state_serv.accepted_ssock)
1237 pj_ssl_sock_close(state_serv.accepted_ssock);
1238 if (ssock_serv)
1239 pj_ssl_sock_close(ssock_serv);
1240
1241 /* Poll to let deferred socket destruction complete */
1242 if (ioqueue) {
1243 pj_time_val delay = {0, 500};
1244 int n = 50;
1245 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
1246 ;
1247 }
1248
1249 if (timer)
1250 pj_timer_heap_destroy(timer);
1251 if (ioqueue)
1252 pj_ioqueue_destroy(ioqueue);
1253 if (pool)
1254 pj_pool_release(pool);
1255
1256 return status;
1257 }
1258
1259
1260 /* SSL socket try to connect to raw TCP socket server, once
1261 * connection established, SSL socket will try to perform SSL
1262 * handshake. SSL client socket should be able to close the
1263 * connection after specified timeout period (set ms_timeout to
1264 * 0 to disable timer).
1265 */
1266 static int server_non_ssl(unsigned ms_timeout)
1267 {
1268 pj_pool_t *pool = NULL;
1269 pj_ioqueue_t *ioqueue = NULL;
1270 pj_timer_heap_t *timer = NULL;
1271 pj_activesock_t *asock_serv = NULL;
1272 pj_ssl_sock_t *ssock_cli = NULL;
1273 pj_activesock_cb asock_cb = { 0 };
1274 pj_sock_t sock = PJ_INVALID_SOCKET;
1275 pj_ssl_sock_param param;
1276 struct test_state state_serv = { 0 };
1277 struct test_state state_cli = { 0 };
1278 pj_sockaddr addr, listen_addr;
1279 pj_status_t status;
1280
1281 pool = pj_pool_create(mem, "ssl_connect_raw_tcp", 256, 256, NULL);
1282
1283 status = pj_ioqueue_create(pool, 4, &ioqueue);
1284 if (status != PJ_SUCCESS) {
1285 goto on_return;
1286 }
1287
1288 status = pj_timer_heap_create(pool, 4, &timer);
1289 if (status != PJ_SUCCESS) {
1290 goto on_return;
1291 }
1292
1293 /* SERVER */
1294 state_serv.pool = pool;
1295 state_serv.ioqueue = ioqueue;
1296
1297 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock);
1298 if (status != PJ_SUCCESS) {
1299 goto on_return;
1300 }
1301
1302 /* Init bind address */
1303 {
1304 pj_str_t tmp_st;
1305 pj_sockaddr_init(PJ_AF_INET, &listen_addr, pj_strset2(&tmp_st, "127.0.0.1"), 0);
1306 }
1307
1308 status = pj_sock_bind(sock, (pj_sockaddr_t*)&listen_addr,
1309 pj_sockaddr_get_len((pj_sockaddr_t*)&listen_addr));
1310 if (status != PJ_SUCCESS) {
1311 goto on_return;
1312 }
1313
1314 status = pj_sock_listen(sock, PJ_SOMAXCONN);
1315 if (status != PJ_SUCCESS) {
1316 goto on_return;
1317 }
1318
1319 asock_cb.on_accept_complete = &asock_on_accept_complete;
1320 status = pj_activesock_create(pool, sock, pj_SOCK_STREAM(), NULL,
1321 ioqueue, &asock_cb, &state_serv, &asock_serv);
1322 if (status != PJ_SUCCESS) {
1323 goto on_return;
1324 }
1325
1326 status = pj_activesock_start_accept(asock_serv, pool);
1327 if (status != PJ_SUCCESS)
1328 goto on_return;
1329
1330 /* Update listener address */
1331 {
1332 int addr_len;
1333
1334 addr_len = sizeof(listen_addr);
1335 pj_sock_getsockname(sock, (pj_sockaddr_t*)&listen_addr, &addr_len);
1336 }
1337
1338 /* CLIENT */
1339 pj_ssl_sock_param_default(&param);
1340 param.cb.on_connect_complete = &ssl_on_connect_complete;
1341 param.cb.on_data_read = &ssl_on_data_read;
1342 param.cb.on_data_sent = &ssl_on_data_sent;
1343 param.ioqueue = ioqueue;
1344 param.timer_heap = timer;
1345 param.timeout.sec = 0;
1346 param.timeout.msec = ms_timeout;
1347 pj_time_val_normalize(&param.timeout);
1348 param.user_data = &state_cli;
1349
1350 state_cli.pool = pool;
1351 state_cli.is_server = PJ_FALSE;
1352 state_cli.is_verbose = PJ_TRUE;
1353
1354 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
1355 if (status != PJ_SUCCESS) {
1356 goto on_return;
1357 }
1358
1359 /* Init default bind address */
1360 {
1361 pj_str_t tmp_st;
1362 pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0);
1363 }
1364
1365 status = pj_ssl_sock_start_connect(ssock_cli, pool,
1366 (pj_sockaddr_t*)&addr,
1367 (pj_sockaddr_t*)&listen_addr,
1368 pj_sockaddr_get_len(&listen_addr));
1369 if (status != PJ_EPENDING) {
1370 goto on_return;
1371 }
1372
1373 /* Wait until everything has been sent/received or error */
1374 while ((!state_serv.err && !state_serv.done) || (!state_cli.err && !state_cli.done))
1375 {
1376 #ifdef PJ_SYMBIAN
1377 pj_symbianos_poll(-1, 1000);
1378 #else
1379 pj_time_val delay = {0, 100};
1380 pj_ioqueue_poll(ioqueue, &delay);
1381 pj_timer_heap_poll(timer, &delay);
1382 #endif
1383 }
1384
1385 if (state_serv.err || state_cli.err) {
1386 if (state_cli.err != PJ_SUCCESS)
1387 status = state_cli.err;
1388 else
1389 status = state_serv.err;
1390
1391 goto on_return;
1392 }
1393
1394 PJ_LOG(3, ("", "...Done!"));
1395
1396 on_return:
1397 if (asock_serv) {
1398 pj_activesock_close(asock_serv);
1399 } else if (sock != PJ_INVALID_SOCKET) {
1400 pj_sock_close(sock);
1401 }
1402 if (ssock_cli && !state_cli.err && !state_cli.done)
1403 pj_ssl_sock_close(ssock_cli);
1404 if (timer)
1405 pj_timer_heap_destroy(timer);
1406 if (ioqueue)
1407 pj_ioqueue_destroy(ioqueue);
1408 if (pool)
1409 pj_pool_release(pool);
1410
1411 return status;
1412 }
1413
1414
1415 #if WITH_BENCHMARK
1416 /* Test will perform multiple clients trying to connect to single server.
1417 * Once SSL connection established, echo test will be performed.
1418 */
1419 static int perf_test(unsigned clients, unsigned ms_handshake_timeout)
1420 {
1421 pj_pool_t *pool = NULL;
1422 pj_ioqueue_t *ioqueue = NULL;
1423 pj_timer_heap_t *timer = NULL;
1424 pj_ssl_sock_t *ssock_serv = NULL;
1425 pj_ssl_sock_t **ssock_cli = NULL;
1426 pj_ssl_sock_param param;
1427 struct test_state state_serv = { 0 };
1428 struct test_state *state_cli = NULL;
1429 pj_sockaddr addr, listen_addr;
1430 pj_ssl_cert_t *cert = NULL;
1431 pj_status_t status;
1432 unsigned i, cli_err = 0;
1433 pj_size_t tot_sent = 0, tot_recv = 0;
1434 pj_time_val start;
1435
1436 pool = pj_pool_create(mem, "ssl_perf", 256, 256, NULL);
1437
1438 status = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue);
1439 if (status != PJ_SUCCESS) {
1440 goto on_return;
1441 }
1442
1443 status = pj_timer_heap_create(pool, PJ_IOQUEUE_MAX_HANDLES, &timer);
1444 if (status != PJ_SUCCESS) {
1445 goto on_return;
1446 }
1447
1448 pj_ssl_sock_param_default(&param);
1449 param.cb.on_accept_complete2 = &ssl_on_accept_complete;
1450 param.cb.on_connect_complete = &ssl_on_connect_complete;
1451 param.cb.on_data_read = &ssl_on_data_read;
1452 param.cb.on_data_sent = &ssl_on_data_sent;
1453 param.ioqueue = ioqueue;
1454 param.timer_heap = timer;
1455 param.timeout.sec = 0;
1456 param.timeout.msec = ms_handshake_timeout;
1457 pj_time_val_normalize(&param.timeout);
1458
1459 /* Init default bind address */
1460 {
1461 pj_str_t tmp_st;
1462 pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0);
1463 }
1464
1465 /* SERVER */
1466 param.user_data = &state_serv;
1467
1468 state_serv.pool = pool;
1469 state_serv.echo = PJ_TRUE;
1470 state_serv.is_server = PJ_TRUE;
1471
1472 status = pj_ssl_sock_create(pool, &param, &ssock_serv);
1473 if (status != PJ_SUCCESS) {
1474 goto on_return;
1475 }
1476
1477 /* Set cert */
1478 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL)
1479 /* Schannel backend currently can only load certificates from
1480 * OS cert store. If the certificate loading fails, we'll skip setting
1481 * certificate, so the SSL socket will create & use a self-signed cert.
1482 */
1483 status = load_cert_from_store(pool, &cert);
1484 if (status == PJ_SUCCESS) {
1485 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
1486 if (status != PJ_SUCCESS) {
1487 goto on_return;
1488 }
1489 }
1490 #else
1491 {
1492 pj_str_t ca_file = pj_str(CERT_CA_FILE);
1493 pj_str_t cert_file = pj_str(CERT_FILE);
1494 pj_str_t privkey_file = pj_str(CERT_PRIVKEY_FILE);
1495 pj_str_t privkey_pass = pj_str(CERT_PRIVKEY_PASS);
1496
1497 #if (defined(TEST_LOAD_FROM_FILES) && TEST_LOAD_FROM_FILES==1)
1498
1499 # if TEST_LOAD_DIRECT && (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
1500 /* Certificate & private key is not loaded from files, but directly
1501 * using OpenSSL objects.
1502 */
1503 status = load_cert_direct(pool, PJ_TRUE, PJ_TRUE, &cert);
1504
1505 /* Skip setting privkey & certificate files below */
1506 privkey_file.slen = 0;
1507 cert_file.slen = 0;
1508 # endif
1509
1510 status = pj_ssl_cert_load_from_files(pool, &ca_file, &cert_file,
1511 &privkey_file, &privkey_pass,
1512 &cert);
1513 #else
1514 pj_ssl_cert_buffer ca_buf, cert_buf, privkey_buf;
1515
1516 status = load_cert_to_buf(pool, &ca_file, &ca_buf);
1517 if (status != PJ_SUCCESS) {
1518 goto on_return;
1519 }
1520
1521 status = load_cert_to_buf(pool, &cert_file, &cert_buf);
1522 if (status != PJ_SUCCESS) {
1523 goto on_return;
1524 }
1525
1526 status = load_cert_to_buf(pool, &privkey_file, &privkey_buf);
1527 if (status != PJ_SUCCESS) {
1528 goto on_return;
1529 }
1530
1531 status = pj_ssl_cert_load_from_buffer(pool, &ca_buf, &cert_buf,
1532 &privkey_buf, &privkey_pass,
1533 &cert);
1534 #endif
1535 if (status != PJ_SUCCESS) {
1536 goto on_return;
1537 }
1538 }
1539
1540 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
1541 if (status != PJ_SUCCESS) {
1542 goto on_return;
1543 }
1544 #endif
1545
1546 status = pj_ssl_sock_start_accept(ssock_serv, pool, &addr, pj_sockaddr_get_len(&addr));
1547 if (status != PJ_SUCCESS) {
1548 goto on_return;
1549 }
1550
1551 /* Get listening address for clients to connect to */
1552 {
1553 pj_ssl_sock_info info;
1554 char buf[64];
1555
1556 pj_ssl_sock_get_info(ssock_serv, &info);
1557 pj_sockaddr_cp(&listen_addr, &info.local_addr);
1558
1559 pj_sockaddr_print((pj_sockaddr_t*)&listen_addr, buf, sizeof(buf), 1);
1560 PJ_LOG(3, ("", "...Listener ready at %s", buf));
1561 }
1562
1563
1564 /* CLIENTS */
1565 clients_num = clients;
1566 param.timeout.sec = 0;
1567 param.timeout.msec = 0;
1568
1569 /* Allocate SSL socket pointers and test state */
1570 ssock_cli = (pj_ssl_sock_t**)pj_pool_calloc(pool, clients, sizeof(pj_ssl_sock_t*));
1571 state_cli = (struct test_state*)pj_pool_calloc(pool, clients, sizeof(struct test_state));
1572
1573 /* Get start timestamp */
1574 pj_gettimeofday(&start);
1575
1576 /* Setup clients */
1577 for (i = 0; i < clients; ++i) {
1578 param.user_data = &state_cli[i];
1579
1580 state_cli[i].pool = pool;
1581 state_cli[i].check_echo = PJ_TRUE;
1582 state_cli[i].send_str_len = (pj_rand() % 5 + 1) * 1024 + pj_rand() % 1024;
1583 state_cli[i].send_str = (char*)pj_pool_alloc(pool, state_cli[i].send_str_len);
1584 {
1585 unsigned j;
1586 for (j = 0; j < state_cli[i].send_str_len; ++j)
1587 state_cli[i].send_str[j] = (char)(pj_rand() % 256);
1588 }
1589
1590 status = pj_ssl_sock_create(pool, &param, &ssock_cli[i]);
1591 if (status != PJ_SUCCESS) {
1592 app_perror("...ERROR pj_ssl_sock_create()", status);
1593 cli_err++;
1594 clients_num--;
1595 continue;
1596 }
1597
1598 status = pj_ssl_sock_start_connect(ssock_cli[i], pool, &addr, &listen_addr, pj_sockaddr_get_len(&addr));
1599 if (status == PJ_SUCCESS) {
1600 ssl_on_connect_complete(ssock_cli[i], PJ_SUCCESS);
1601 } else if (status == PJ_EPENDING) {
1602 status = PJ_SUCCESS;
1603 } else {
1604 app_perror("...ERROR pj_ssl_sock_create()", status);
1605 pj_ssl_sock_close(ssock_cli[i]);
1606 ssock_cli[i] = NULL;
1607 clients_num--;
1608 cli_err++;
1609 continue;
1610 }
1611
1612 /* Give chance to server to accept this client */
1613 {
1614 unsigned n = 5;
1615
1616 #ifdef PJ_SYMBIAN
1617 while(n && pj_symbianos_poll(-1, 1000))
1618 n--;
1619 #else
1620 pj_time_val delay = {0, 100};
1621 while(n && pj_ioqueue_poll(ioqueue, &delay) > 0)
1622 n--;
1623 #endif
1624 }
1625 }
1626
1627 /* Wait until everything has been sent/received or error */
1628 while (clients_num)
1629 {
1630 #ifdef PJ_SYMBIAN
1631 pj_symbianos_poll(-1, 1000);
1632 #else
1633 pj_time_val delay = {0, 100};
1634 pj_ioqueue_poll(ioqueue, &delay);
1635 pj_timer_heap_poll(timer, &delay);
1636 #endif
1637 }
1638
1639 /* Clean up sockets */
1640 {
1641 pj_time_val delay = {0, 500};
1642 while (pj_ioqueue_poll(ioqueue, &delay) > 0);
1643 }
1644
1645 if (state_serv.err != PJ_SUCCESS) {
1646 status = state_serv.err;
1647 goto on_return;
1648 }
1649
1650 PJ_LOG(3, ("", "...Done!"));
1651
1652 /* SSL setup and data transfer duration */
1653 {
1654 pj_time_val stop;
1655
1656 pj_gettimeofday(&stop);
1657 PJ_TIME_VAL_SUB(stop, start);
1658
1659 PJ_LOG(3, ("", ".....Setup & data transfer duration: %ld.%03lds", stop.sec, stop.msec));
1660 }
1661
1662 /* Check clients status */
1663 for (i = 0; i < clients; ++i) {
1664 if (state_cli[i].err != PJ_SUCCESS)
1665 cli_err++;
1666
1667 tot_sent += state_cli[i].sent;
1668 tot_recv += state_cli[i].recv;
1669 }
1670
1671 PJ_LOG(3, ("", ".....Clients: %d (%d errors)", clients, cli_err));
1672 PJ_LOG(3, ("", ".....Total sent/recv: %lu/%lu bytes",
1673 (unsigned long)tot_sent, (unsigned long)tot_recv));
1674
1675 on_return:
1676 if (ssock_serv)
1677 pj_ssl_sock_close(ssock_serv);
1678
1679 if (ssock_cli && state_cli) {
1680 for (i = 0; i < clients; ++i) {
1681 if (ssock_cli[i] && !state_cli[i].err && !state_cli[i].done)
1682 pj_ssl_sock_close(ssock_cli[i]);
1683 }
1684 }
1685 if (ioqueue)
1686 pj_ioqueue_destroy(ioqueue);
1687 if (pool)
1688 pj_pool_release(pool);
1689
1690 return status;
1691 }
1692 #endif
1693
1694 /* Stress tests (send_load, close_pending, bidir, mt_send_load) are in
1695 * ssl_sock_stress.c, registered as ssl_sock_stress_test().
1696 */
1697
1698
1699 /*
1700 * Large message test: send 64KB data (multiple TLS records),
1701 * verify byte-for-byte echo. Uses dedicated state/callbacks with
1702 * larger buffers than the echo_test infrastructure.
1703 */
1704 #define LARGE_MSG_SIZE (64 * 1024)
1705 #define LARGE_MSG_BUF_SIZE 8192
1706
1707 struct large_msg_state
1708 {
1709 pj_pool_t *pool;
1710 pj_ssl_sock_t *accepted_ssock;
1711 pj_bool_t is_server;
1712 pj_bool_t echo;
1713 pj_status_t err;
1714 pj_size_t sent;
1715 pj_size_t recv;
1716 pj_uint8_t read_buf[LARGE_MSG_BUF_SIZE];
1717 pj_bool_t done;
1718 char *send_str;
1719 pj_size_t send_str_len;
1720 const char *check_echo_ptr;
1721 pj_ioqueue_op_key_t send_key;
1722 };
1723
1724 static pj_bool_t lm_on_connect_complete(pj_ssl_sock_t *ssock,
1725 pj_status_t status)
1726 {
1727 struct large_msg_state *st = (struct large_msg_state *)
1728 pj_ssl_sock_get_user_data(ssock);
1729 void *read_buf[1];
1730
1731 if (status != PJ_SUCCESS) {
1732 st->err = status;
1733 return PJ_FALSE;
1734 }
1735
1736 read_buf[0] = st->read_buf;
1737 status = pj_ssl_sock_start_read2(ssock, st->pool,
1738 sizeof(st->read_buf),
1739 (void **)read_buf, 0);
1740 if (status != PJ_SUCCESS) {
1741 st->err = status;
1742 return PJ_FALSE;
1743 }
1744
1745 /* Start sending */
1746 while (st->sent < st->send_str_len) {
1747 pj_ssize_t size = st->send_str_len - st->sent;
1748
1749 status = pj_ssl_sock_send(ssock, &st->send_key,
1750 st->send_str + st->sent, &size, 0);
1751 if (status == PJ_SUCCESS) {
1752 st->sent += size;
1753 } else {
1754 if (status != PJ_EPENDING)
1755 st->err = status;
1756 break;
1757 }
1758 }
1759
1760 return (st->err == PJ_SUCCESS) ? PJ_TRUE : PJ_FALSE;
1761 }
1762
1763 static pj_bool_t lm_on_accept_complete(pj_ssl_sock_t *ssock,
1764 pj_ssl_sock_t *newsock,
1765 const pj_sockaddr_t *src_addr,
1766 int src_addr_len,
1767 pj_status_t accept_status)
1768 {
1769 struct large_msg_state *parent_st = (struct large_msg_state *)
1770 pj_ssl_sock_get_user_data(ssock);
1771 struct large_msg_state *st;
1772 void *read_buf[1];
1773 pj_status_t status;
1774
1775 PJ_UNUSED_ARG(src_addr);
1776 PJ_UNUSED_ARG(src_addr_len);
1777
1778 if (accept_status != PJ_SUCCESS)
1779 return PJ_FALSE;
1780
1781 st = (struct large_msg_state *)pj_pool_zalloc(parent_st->pool,
1782 sizeof(struct large_msg_state));
1783 *st = *parent_st;
1784 st->sent = 0;
1785 st->recv = 0;
1786 st->done = PJ_FALSE;
1787 st->err = PJ_SUCCESS;
1788 pj_ssl_sock_set_user_data(newsock, st);
1789
1790 /* Track accepted socket for cleanup */
1791 parent_st->accepted_ssock = newsock;
1792
1793 read_buf[0] = st->read_buf;
1794 status = pj_ssl_sock_start_read2(newsock, st->pool,
1795 sizeof(st->read_buf),
1796 (void **)read_buf, 0);
1797 if (status != PJ_SUCCESS) {
1798 st->err = status;
1799 return PJ_FALSE;
1800 }
1801
1802 return PJ_TRUE;
1803 }
1804
1805 static pj_bool_t lm_on_data_read(pj_ssl_sock_t *ssock,
1806 void *data,
1807 pj_size_t size,
1808 pj_status_t status,
1809 pj_size_t *remainder)
1810 {
1811 struct large_msg_state *st = (struct large_msg_state *)
1812 pj_ssl_sock_get_user_data(ssock);
1813
1814 if (remainder)
1815 *remainder = 0;
1816
1817 if (size > 0) {
1818 st->recv += size;
1819
1820 /* Server: echo data back */
1821 if (st->echo) {
1822 pj_ssize_t sz = (pj_ssize_t)size;
1823 pj_status_t s;
1824
1825 s = pj_ssl_sock_send(ssock, &st->send_key, data, &sz, 0);
1826 if (s == PJ_SUCCESS)
1827 st->sent += sz;
1828 else if (s != PJ_EPENDING)
1829 st->err = s;
1830 }
1831
1832 /* Client: verify echoed data byte-for-byte */
1833 if (st->check_echo_ptr) {
1834 if (pj_memcmp(st->check_echo_ptr, data, size)) {
1835 PJ_LOG(1, ("", "...ERROR echoed data mismatch at "
1836 "offset %lu",
1837 (unsigned long)(st->check_echo_ptr -
1838 st->send_str)));
1839 st->err = PJ_EINVAL;
1840 }
1841 st->check_echo_ptr += size;
1842
1843 if (st->recv >= st->send_str_len)
1844 st->done = PJ_TRUE;
1845 }
1846 }
1847
1848 if (status != PJ_SUCCESS) {
1849 if (status == PJ_EEOF)
1850 st->done = PJ_TRUE;
1851 else
1852 st->err = status;
1853 }
1854
1855 if (st->err != PJ_SUCCESS || st->done)
1856 return PJ_FALSE;
1857
1858 return PJ_TRUE;
1859 }
1860
1861 static pj_bool_t lm_on_data_sent(pj_ssl_sock_t *ssock,
1862 pj_ioqueue_op_key_t *op_key,
1863 pj_ssize_t sent)
1864 {
1865 struct large_msg_state *st = (struct large_msg_state *)
1866 pj_ssl_sock_get_user_data(ssock);
1867 PJ_UNUSED_ARG(op_key);
1868
1869 if (sent < 0) {
1870 st->err = (pj_status_t)-sent;
1871 return PJ_FALSE;
1872 }
1873
1874 st->sent += sent;
1875
1876 /* Client: continue sending remaining data */
1877 if (st->send_str && st->sent < st->send_str_len) {
1878 while (st->sent < st->send_str_len) {
1879 pj_ssize_t size = st->send_str_len - st->sent;
1880 pj_status_t s;
1881
1882 s = pj_ssl_sock_send(ssock, &st->send_key,
1883 st->send_str + st->sent, &size, 0);
1884 if (s == PJ_SUCCESS) {
1885 st->sent += size;
1886 } else {
1887 if (s != PJ_EPENDING)
1888 st->err = s;
1889 break;
1890 }
1891 }
1892 }
1893
1894 return (st->err == PJ_SUCCESS) ? PJ_TRUE : PJ_FALSE;
1895 }
1896
1897 static int large_msg_test(void)
1898 {
1899 pj_pool_t *pool = NULL;
1900 pj_ioqueue_t *ioqueue = NULL;
1901 pj_timer_heap_t *timer = NULL;
1902 pj_ssl_sock_t *ssock_serv = NULL;
1903 pj_ssl_sock_t *ssock_cli = NULL;
1904 pj_ssl_sock_param param;
1905 struct large_msg_state state_serv;
1906 struct large_msg_state state_cli;
1907 pj_sockaddr addr, listen_addr;
1908 pj_status_t status;
1909 char *large_buf;
1910 int i;
1911
1912 pool = pj_pool_create(mem, "ssl_large", 256000, 4096, NULL);
1913
1914 pj_bzero(&state_serv, sizeof(state_serv));
1915 pj_bzero(&state_cli, sizeof(state_cli));
1916
1917 status = pj_ioqueue_create(pool, 4, &ioqueue);
1918 if (status != PJ_SUCCESS) {
1919 app_perror("...large_msg_test: ioqueue create", status);
1920 goto on_return;
1921 }
1922
1923 status = pj_timer_heap_create(pool, 4, &timer);
1924 if (status != PJ_SUCCESS) {
1925 app_perror("...large_msg_test: timer create", status);
1926 goto on_return;
1927 }
1928
1929 pj_ssl_sock_param_default(&param);
1930 param.cb.on_accept_complete2 = &lm_on_accept_complete;
1931 param.cb.on_connect_complete = &lm_on_connect_complete;
1932 param.cb.on_data_read = &lm_on_data_read;
1933 param.cb.on_data_sent = &lm_on_data_sent;
1934 param.ioqueue = ioqueue;
1935 param.timer_heap = timer;
1936 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
1937 param.ciphers_num = 0;
1938
1939 {
1940 pj_str_t tmp_st;
1941 pj_sockaddr_init(PJ_AF_INET, &addr,
1942 pj_strset2(&tmp_st, "127.0.0.1"), 0);
1943 }
1944
1945 /* Fill send data with deterministic pattern */
1946 large_buf = (char *)pj_pool_alloc(pool, LARGE_MSG_SIZE);
1947 for (i = 0; i < LARGE_MSG_SIZE; i++)
1948 large_buf[i] = (char)(i & 0xFF);
1949
1950 /* SERVER */
1951 state_serv.pool = pool;
1952 state_serv.echo = PJ_TRUE;
1953 state_serv.is_server = PJ_TRUE;
1954 param.user_data = &state_serv;
1955 param.require_client_cert = PJ_FALSE;
1956
1957 listen_addr = addr;
1958 status = ssl_test_create_server(pool, &param, "large_msg_test",
1959 &ssock_serv, &listen_addr);
1960 if (status != PJ_SUCCESS)
1961 goto on_return;
1962
1963 /* CLIENT */
1964 state_cli.pool = pool;
1965 state_cli.is_server = PJ_FALSE;
1966 state_cli.send_str = large_buf;
1967 state_cli.send_str_len = LARGE_MSG_SIZE;
1968 state_cli.check_echo_ptr = large_buf;
1969 param.user_data = &state_cli;
1970
1971 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
1972 if (status != PJ_SUCCESS) {
1973 app_perror("...large_msg_test: client create", status);
1974 goto on_return;
1975 }
1976
1977 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr,
1978 &listen_addr,
1979 pj_sockaddr_get_len(&addr));
1980 if (status == PJ_SUCCESS) {
1981 lm_on_connect_complete(ssock_cli, PJ_SUCCESS);
1982 } else if (status != PJ_EPENDING) {
1983 app_perror("...large_msg_test: connect", status);
1984 goto on_return;
1985 }
1986
1987 /* Poll until done */
1988 {
1989 pj_timestamp t_start, t_now;
1990 pj_uint32_t elapsed;
1991
1992 pj_get_timestamp(&t_start);
1993 while (!state_cli.err && !state_cli.done) {
1994 pj_time_val delay = {0, 100};
1995 pj_ioqueue_poll(ioqueue, &delay);
1996 pj_timer_heap_poll(timer, NULL);
1997
1998 pj_get_timestamp(&t_now);
1999 elapsed = pj_elapsed_msec(&t_start, &t_now);
2000 if (elapsed > 30000) {
2001 PJ_LOG(1, ("", "...large_msg_test TIMEOUT after 30s"));
2002 status = PJ_ETIMEDOUT;
2003 goto on_return;
2004 }
2005 }
2006 }
2007
2008 if (state_cli.err) {
2009 status = state_cli.err;
2010 app_perror("...large_msg_test client error", status);
2011 goto on_return;
2012 }
2013
2014 PJ_LOG(3, ("", "...large_msg_test: sent=%lu, recv=%lu",
2015 (unsigned long)state_cli.sent,
2016 (unsigned long)state_cli.recv));
2017
2018 if (state_cli.recv < (pj_size_t)LARGE_MSG_SIZE) {
2019 PJ_LOG(1, ("", "...large_msg_test: recv=%lu < expected=%d",
2020 (unsigned long)state_cli.recv, LARGE_MSG_SIZE));
2021 status = PJ_EINVAL;
2022 goto on_return;
2023 }
2024
2025 status = PJ_SUCCESS;
2026
2027 on_return:
2028 if (ssock_cli)
2029 pj_ssl_sock_close(ssock_cli);
2030 if (state_serv.accepted_ssock)
2031 pj_ssl_sock_close(state_serv.accepted_ssock);
2032 if (ssock_serv)
2033 pj_ssl_sock_close(ssock_serv);
2034
2035 /* Poll to drain pending events after close */
2036 if (ioqueue) {
2037 pj_time_val delay = {0, 500};
2038 int n = 50;
2039 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
2040 ;
2041 }
2042
2043 if (timer)
2044 pj_timer_heap_destroy(timer);
2045 if (ioqueue)
2046 pj_ioqueue_destroy(ioqueue);
2047 if (pool)
2048 pj_pool_release(pool);
2049
2050 return (status == PJ_SUCCESS) ? 0 : -1;
2051 }
2052
2053
2054
2055 int ssl_sock_test(void)
2056 {
2057 int ret;
2058
2059 PJ_LOG(3,("", "..get cipher list test"));
2060 ret = get_cipher_list();
2061 if (ret != 0)
2062 return ret;
2063
2064 PJ_LOG(3,("", "..https client test"));
2065 ret = https_client_test(30000);
2066 if (ret != 0)
2067 return ret;
2068
2069 #ifndef PJ_SYMBIAN
2070
2071 /* On Symbian platforms, SSL socket is implemented using CSecureSocket,
2072 * and it hasn't supported server mode, so exclude the following tests,
2073 * which require SSL server, for now.
2074 */
2075
2076 /* Schannel backend notes:
2077 * - currently it does not support ciphers settings, so we exclude tests
2078 * whose ciphers setting is specified.
2079 * - TLS protocol older than 1.0 is not supported, TLS 1.0 & 1.1 will be
2080 * disabled soon, TLS 1.3 is supported since Windows 11, so for now
2081 * we only include tests with TLS 1.2.
2082 */
2083
2084 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2085 PJ_LOG(3,("", "..echo test w/ TLSv1.2 and PJ_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 cipher"));
2086 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1_2, PJ_SSL_SOCK_PROTO_TLS1_2,
2087 PJ_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
2088 PJ_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
2089 PJ_FALSE, PJ_FALSE);
2090 if (ret != 0)
2091 return ret;
2092
2093 PJ_LOG(3,("", "..echo test w/ TLSv1.3 and PJ_TLS_AES_128_GCM_SHA256 cipher"));
2094 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1_3, PJ_SSL_SOCK_PROTO_TLS1_3,
2095 PJ_TLS_AES_128_GCM_SHA256, PJ_TLS_AES_128_GCM_SHA256,
2096 PJ_FALSE, PJ_FALSE);
2097 if (ret != 0)
2098 return ret;
2099 #endif
2100
2101 PJ_LOG(3,("", "..echo test w/ compatible proto: server TLSv1.2 vs client TLSv1.2"));
2102 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1_2, PJ_SSL_SOCK_PROTO_TLS1_2,
2103 -1, -1,
2104 PJ_FALSE, PJ_FALSE);
2105 if (ret != 0)
2106 return ret;
2107
2108 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2109 PJ_LOG(3,("", "..echo test w/ compatible proto: server TLSv1.2+1.3 vs client TLSv1.3"));
2110 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1_2 | PJ_SSL_SOCK_PROTO_TLS1_3, PJ_SSL_SOCK_PROTO_TLS1_3,
2111 -1, -1,
2112 PJ_FALSE, PJ_FALSE);
2113 if (ret != 0)
2114 return ret;
2115
2116 PJ_LOG(3,("", "..echo test w/ incompatible proto: server TLSv1.3 vs client TLSv1.2"));
2117 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1_3, PJ_SSL_SOCK_PROTO_TLS1_2,
2118 PJ_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
2119 PJ_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
2120 PJ_FALSE, PJ_FALSE);
2121 if (ret == 0)
2122 return PJ_EBUG;
2123 #endif
2124
2125 /* We can't set min/max proto for TLS protocol higher than 1.0. */
2126 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_DARWIN && PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2127 PJ_LOG(3,("", "..echo test w/ incompatible proto: server TLSv1.2 vs client TLSv1.3"));
2128 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1_2, PJ_SSL_SOCK_PROTO_TLS1_3,
2129 -1, -1,
2130 PJ_FALSE, PJ_FALSE);
2131 if (ret == 0)
2132 return PJ_EBUG;
2133 #endif
2134
2135 /* With Apple SSL, we can't seem to enable certain ciphers only.
2136 * SSLSetEnabledCiphers() is deprecated and we only have
2137 * sec_protocol_options_append_tls_ciphersuite(),
2138 * but there's no API to remove certain or all ciphers.
2139 */
2140 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_APPLE && PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2141 PJ_LOG(3,("", "..echo test w/ incompatible ciphers"));
2142 ret = echo_test(PJ_SSL_SOCK_PROTO_DEFAULT, PJ_SSL_SOCK_PROTO_DEFAULT,
2143 PJ_TLS_RSA_WITH_DES_CBC_SHA, PJ_TLS_RSA_WITH_AES_256_CBC_SHA,
2144 PJ_FALSE, PJ_FALSE);
2145 if (ret == 0)
2146 return PJ_EBUG;
2147 #endif
2148
2149 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2150 PJ_LOG(3,("", "..echo test w/ client cert required but not provided"));
2151 ret = echo_test(PJ_SSL_SOCK_PROTO_DEFAULT, PJ_SSL_SOCK_PROTO_DEFAULT,
2152 -1, -1,
2153 PJ_TRUE, PJ_FALSE);
2154 if (ret == 0)
2155 return PJ_EBUG;
2156
2157 PJ_LOG(3,("", "..echo test w/ client cert required and provided"));
2158 ret = echo_test(PJ_SSL_SOCK_PROTO_DEFAULT, PJ_SSL_SOCK_PROTO_DEFAULT,
2159 -1, -1,
2160 PJ_TRUE, PJ_TRUE);
2161 if (ret != 0)
2162 return ret;
2163 #endif
2164
2165 PJ_LOG(3,("", "..large message test"));
2166 ret = large_msg_test();
2167 if (ret != 0)
2168 return ret;
2169
2170 #if WITH_BENCHMARK
2171 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_MBEDTLS)
2172 PJ_LOG(3,("", "..performance test"));
2173 ret = perf_test(PJ_IOQUEUE_MAX_HANDLES/2 - 1, 0);
2174 if (ret != 0)
2175 return ret;
2176 #else
2177 PJ_UNUSED_ARG(perf_test);
2178 #endif
2179 #endif
2180
2181 PJ_LOG(3,("", "..client non-SSL (handshake timeout 5 secs)"));
2182 ret = client_non_ssl(5000);
2183 if (ret != PJ_ETIMEDOUT)
2184 return ret;
2185
2186 #endif
2187
2188 PJ_LOG(3,("", "..server non-SSL (handshake timeout 5 secs)"));
2189 ret = server_non_ssl(5000);
2190 if (ret != PJ_ETIMEDOUT)
2191 return ret;
2192
2193 return 0;
2194 }
2195
2196 #else /* INCLUDE_SSLSOCK_TEST */
2197 /* To prevent warning about "translation unit is empty"
2198 * when this test is disabled.
2199 */
2200 int dummy_ssl_sock_test;
2201 #endif /* INCLUDE_SSLSOCK_TEST */
2202