Changeset 3106 for pjproject/trunk/pjlib/src/pjlib-test/ssl_sock.c
- Timestamp:
- Feb 24, 2010 5:43:34 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pjlib-test/ssl_sock.c
r3018 r3106 23 23 24 24 #define CERT_DIR "../build/" 25 #define CERT_CA_FILE NULL25 #define CERT_CA_FILE CERT_DIR "cacert.pem" 26 26 #define CERT_FILE CERT_DIR "cacert.pem" 27 27 #define CERT_PRIVKEY_FILE CERT_DIR "privkey.pem" … … 84 84 }; 85 85 86 static void dump_ cert_info(const char *prefix, const pj_ssl_cert_info *ci)86 static void dump_ssl_info(const pj_ssl_sock_info *si) 87 87 { 88 const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 89 pj_parsed_time pt1; 90 pj_parsed_time pt2; 91 92 pj_time_decode(&ci->validity_start, &pt1); 93 pj_time_decode(&ci->validity_end, &pt2); 94 95 PJ_LOG(3, ("", "%sSubject : %.*s", prefix, ci->subject.slen, ci->subject.ptr)); 96 PJ_LOG(3, ("", "%sIssuer : %.*s", prefix, ci->issuer.slen, ci->issuer.ptr)); 97 PJ_LOG(3, ("", "%sVersion : v%d", prefix, ci->version)); 98 PJ_LOG(3, ("", "%sValid from : %s %4d-%02d-%02d %02d:%02d:%02d.%03d %s", 99 prefix, wdays[pt1.wday], pt1.year, pt1.mon+1, pt1.day, 100 pt1.hour, pt1.min, pt1.sec, pt1.msec, 101 (ci->validity_use_gmt? "GMT":""))); 102 PJ_LOG(3, ("", "%sValid to : %s %4d-%02d-%02d %02d:%02d:%02d.%03d %s", 103 prefix, wdays[pt2.wday], pt2.year, pt2.mon+1, pt2.day, 104 pt2.hour, pt2.min, pt2.sec, pt2.msec, 105 (ci->validity_use_gmt? "GMT":""))); 88 const char *tmp_st; 89 90 /* Print cipher name */ 91 tmp_st = pj_ssl_cipher_name(si->cipher); 92 if (tmp_st == NULL) 93 tmp_st = "[Unknown]"; 94 PJ_LOG(3, ("", ".....Cipher: %s", tmp_st)); 95 96 /* Print remote certificate info and verification result */ 97 if (si->remote_cert_info && si->remote_cert_info->subject.info.slen) 98 { 99 char buf[2048]; 100 const char *verif_msgs[32]; 101 unsigned verif_msg_cnt; 102 103 /* Dump remote TLS certificate info */ 104 PJ_LOG(3, ("", ".....Remote certificate info:")); 105 pj_ssl_cert_info_dump(si->remote_cert_info, " ", buf, sizeof(buf)); 106 PJ_LOG(3,("", "\n%s", buf)); 107 108 /* Dump remote TLS certificate verification result */ 109 verif_msg_cnt = PJ_ARRAY_SIZE(verif_msgs); 110 pj_ssl_cert_verify_error_st(si->verify_status, 111 verif_msgs, &verif_msg_cnt); 112 PJ_LOG(3,("", ".....Remote certificate verification result: %s", 113 (verif_msg_cnt == 1? verif_msgs[0]:""))); 114 if (verif_msg_cnt > 1) { 115 unsigned i; 116 for (i = 0; i < verif_msg_cnt; ++i) 117 PJ_LOG(3,("", "..... - %s", verif_msgs[i])); 118 } 119 } 106 120 } 107 121 … … 131 145 PJ_LOG(3, ("", "...Connected %s -> %s!", buf1, buf2)); 132 146 133 if (st->is_verbose) { 134 const char *tmp_st; 135 136 /* Print cipher name */ 137 tmp_st = pj_ssl_cipher_name(info.cipher); 138 if (tmp_st == NULL) 139 tmp_st = "[Unknown]"; 140 PJ_LOG(3, ("", ".....Cipher: %s", tmp_st)); 141 142 /* Print certificates info */ 143 if (info.local_cert_info.subject.slen) { 144 PJ_LOG(3, ("", ".....Local certificate info:")); 145 dump_cert_info(".......", &info.local_cert_info); 146 } 147 if (info.remote_cert_info.subject.slen) { 148 PJ_LOG(3, ("", ".....Remote certificate info:")); 149 dump_cert_info(".......", &info.remote_cert_info); 150 } 151 } 147 if (st->is_verbose) 148 dump_ssl_info(&info); 152 149 153 150 /* Start reading data */ … … 199 196 struct test_state *st; 200 197 void *read_buf[1]; 198 pj_ssl_sock_info info; 199 char buf[64]; 201 200 pj_status_t status; 202 201 … … 208 207 pj_ssl_sock_set_user_data(newsock, st); 209 208 210 if (st->is_verbose) { 211 pj_ssl_sock_info info; 212 char buf[64]; 213 const char *tmp_st; 214 215 status = pj_ssl_sock_get_info(newsock, &info); 216 if (status != PJ_SUCCESS) { 217 app_perror("...ERROR pj_ssl_sock_get_info()", status); 218 goto on_return; 219 } 220 221 pj_sockaddr_print(src_addr, buf, sizeof(buf), 1); 222 PJ_LOG(3, ("", "...Accepted connection from %s", buf)); 223 224 /* Print cipher name */ 225 tmp_st = pj_ssl_cipher_name(info.cipher); 226 if (tmp_st == NULL) 227 tmp_st = "[Unknown]"; 228 PJ_LOG(3, ("", ".....Cipher: %s", tmp_st)); 229 230 /* Print certificates info */ 231 if (info.local_cert_info.subject.slen) { 232 PJ_LOG(3, ("", ".....Local certificate info:")); 233 dump_cert_info(".......", &info.local_cert_info); 234 } 235 if (info.remote_cert_info.subject.slen) { 236 PJ_LOG(3, ("", ".....Remote certificate info:")); 237 dump_cert_info(".......", &info.remote_cert_info); 238 } 239 } 209 status = pj_ssl_sock_get_info(newsock, &info); 210 if (status != PJ_SUCCESS) { 211 app_perror("...ERROR pj_ssl_sock_get_info()", status); 212 goto on_return; 213 } 214 215 pj_sockaddr_print(src_addr, buf, sizeof(buf), 1); 216 PJ_LOG(3, ("", "...Accepted connection from %s", buf)); 217 218 if (st->is_verbose) 219 dump_ssl_info(&info); 240 220 241 221 /* Start reading data */ … … 461 441 param.timeout.sec = 0; 462 442 param.timeout.msec = ms_timeout; 443 param.proto = PJ_SSL_SOCK_PROTO_SSL23; 463 444 pj_time_val_normalize(¶m.timeout); 464 445 … … 513 494 514 495 static int echo_test(pj_ssl_sock_proto srv_proto, pj_ssl_sock_proto cli_proto, 515 pj_ssl_cipher srv_cipher, pj_ssl_cipher cli_cipher) 496 pj_ssl_cipher srv_cipher, pj_ssl_cipher cli_cipher, 497 pj_bool_t req_client_cert, pj_bool_t client_provide_cert) 516 498 { 517 499 pj_pool_t *pool = NULL; … … 534 516 } 535 517 536 /* Set cert */ 518 pj_ssl_sock_param_default(¶m); 519 param.cb.on_accept_complete = &ssl_on_accept_complete; 520 param.cb.on_connect_complete = &ssl_on_connect_complete; 521 param.cb.on_data_read = &ssl_on_data_read; 522 param.cb.on_data_sent = &ssl_on_data_sent; 523 param.ioqueue = ioqueue; 524 param.ciphers = ciphers; 525 526 /* Init default bind address */ 527 { 528 pj_str_t tmp_st; 529 pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0); 530 } 531 532 /* === SERVER === */ 533 param.proto = srv_proto; 534 param.user_data = &state_serv; 535 param.ciphers_num = (srv_cipher == -1)? 0 : 1; 536 param.require_client_cert = req_client_cert; 537 ciphers[0] = srv_cipher; 538 539 state_serv.pool = pool; 540 state_serv.echo = PJ_TRUE; 541 state_serv.is_server = PJ_TRUE; 542 state_serv.is_verbose = PJ_TRUE; 543 544 status = pj_ssl_sock_create(pool, ¶m, &ssock_serv); 545 if (status != PJ_SUCCESS) { 546 goto on_return; 547 } 548 549 /* Set server cert */ 537 550 { 538 551 pj_str_t tmp1, tmp2, tmp3, tmp4; … … 547 560 goto on_return; 548 561 } 549 } 550 551 pj_ssl_sock_param_default(¶m); 552 param.cb.on_accept_complete = &ssl_on_accept_complete; 553 param.cb.on_connect_complete = &ssl_on_connect_complete; 554 param.cb.on_data_read = &ssl_on_data_read; 555 param.cb.on_data_sent = &ssl_on_data_sent; 556 param.ioqueue = ioqueue; 557 param.ciphers = ciphers; 558 559 /* Init default bind address */ 560 { 561 pj_str_t tmp_st; 562 pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0); 563 } 564 565 /* SERVER */ 566 param.proto = srv_proto; 567 param.user_data = &state_serv; 568 param.ciphers_num = (srv_cipher == -1)? 0 : 1; 569 ciphers[0] = srv_cipher; 570 571 state_serv.pool = pool; 572 state_serv.echo = PJ_TRUE; 573 state_serv.is_server = PJ_TRUE; 574 state_serv.is_verbose = PJ_TRUE; 575 576 status = pj_ssl_sock_create(pool, ¶m, &ssock_serv); 577 if (status != PJ_SUCCESS) { 578 goto on_return; 579 } 580 581 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert); 582 if (status != PJ_SUCCESS) { 583 goto on_return; 562 563 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert); 564 if (status != PJ_SUCCESS) { 565 goto on_return; 566 } 584 567 } 585 568 … … 597 580 } 598 581 599 /* CLIENT*/582 /* === CLIENT === */ 600 583 param.proto = cli_proto; 601 584 param.user_data = &state_cli; … … 624 607 if (status != PJ_SUCCESS) { 625 608 goto on_return; 609 } 610 611 /* Set cert for client */ 612 { 613 614 if (!client_provide_cert) { 615 pj_str_t tmp1, tmp2; 616 617 pj_strset2(&tmp1, (char*)CERT_CA_FILE); 618 pj_strset2(&tmp2, NULL); 619 status = pj_ssl_cert_load_from_files(pool, 620 &tmp1, &tmp2, &tmp2, &tmp2, 621 &cert); 622 if (status != PJ_SUCCESS) { 623 goto on_return; 624 } 625 } 626 627 status = pj_ssl_sock_set_certificate(ssock_cli, pool, cert); 628 if (status != PJ_SUCCESS) { 629 goto on_return; 630 } 626 631 } 627 632 … … 1014 1019 state_cli = pj_pool_calloc(pool, clients, sizeof(struct test_state)); 1015 1020 1021 /* Get start timestamp */ 1022 pj_gettimeofday(&start); 1023 1016 1024 /* Setup clients */ 1017 1025 for (i = 0; i < clients; ++i) { … … 1065 1073 } 1066 1074 1067 /* Get start timestamp */1068 pj_gettimeofday(&start);1069 1070 1075 /* Wait until everything has been sent/received or error */ 1071 1076 while (clients_num) … … 1151 1156 PJ_LOG(3,("", "..echo test w/ TLSv1 and TLS_RSA_WITH_DES_CBC_SHA cipher")); 1152 1157 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1, PJ_SSL_SOCK_PROTO_TLS1, 1153 TLS_RSA_WITH_DES_CBC_SHA, TLS_RSA_WITH_DES_CBC_SHA); 1158 TLS_RSA_WITH_DES_CBC_SHA, TLS_RSA_WITH_DES_CBC_SHA, 1159 PJ_FALSE, PJ_FALSE); 1154 1160 if (ret != 0) 1155 1161 return ret; … … 1157 1163 PJ_LOG(3,("", "..echo test w/ SSLv23 and TLS_RSA_WITH_AES_256_CBC_SHA cipher")); 1158 1164 ret = echo_test(PJ_SSL_SOCK_PROTO_SSL23, PJ_SSL_SOCK_PROTO_SSL23, 1159 TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA); 1165 TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, 1166 PJ_FALSE, PJ_FALSE); 1160 1167 if (ret != 0) 1161 1168 return ret; … … 1163 1170 PJ_LOG(3,("", "..echo test w/ incompatible proto")); 1164 1171 ret = echo_test(PJ_SSL_SOCK_PROTO_TLS1, PJ_SSL_SOCK_PROTO_SSL3, 1165 TLS_RSA_WITH_DES_CBC_SHA, TLS_RSA_WITH_DES_CBC_SHA); 1172 TLS_RSA_WITH_DES_CBC_SHA, TLS_RSA_WITH_DES_CBC_SHA, 1173 PJ_FALSE, PJ_FALSE); 1166 1174 if (ret == 0) 1167 1175 return PJ_EBUG; … … 1169 1177 PJ_LOG(3,("", "..echo test w/ incompatible ciphers")); 1170 1178 ret = echo_test(PJ_SSL_SOCK_PROTO_DEFAULT, PJ_SSL_SOCK_PROTO_DEFAULT, 1171 TLS_RSA_WITH_DES_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA); 1179 TLS_RSA_WITH_DES_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, 1180 PJ_FALSE, PJ_FALSE); 1172 1181 if (ret == 0) 1173 1182 return PJ_EBUG; 1183 1184 PJ_LOG(3,("", "..echo test w/ client cert required but not provided")); 1185 ret = echo_test(PJ_SSL_SOCK_PROTO_DEFAULT, PJ_SSL_SOCK_PROTO_DEFAULT, 1186 TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, 1187 PJ_TRUE, PJ_FALSE); 1188 if (ret == 0) 1189 return PJ_EBUG; 1190 1191 PJ_LOG(3,("", "..echo test w/ client cert required and provided")); 1192 ret = echo_test(PJ_SSL_SOCK_PROTO_DEFAULT, PJ_SSL_SOCK_PROTO_DEFAULT, 1193 TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, 1194 PJ_TRUE, PJ_TRUE); 1195 if (ret != 0) 1196 return ret; 1174 1197 1175 1198 PJ_LOG(3,("", "..client non-SSL (handshake timeout 5 secs)"));
Note: See TracChangeset
for help on using the changeset viewer.