| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <pj/ssl_sock.h> |
| 19 |
#include <pj/assert.h> |
| 20 |
#include <pj/errno.h> |
| 21 |
#include <pj/log.h> |
| 22 |
#include <pj/math.h> |
| 23 |
#include <pj/os.h> |
| 24 |
#include <pj/pool.h> |
| 25 |
#include <pj/string.h> |
| 26 |
|
| 27 |
#include "ssl_sock_imp_common.h" |
| 28 |
|
| 29 |
|
| 30 |
#ifndef PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT |
| 31 |
# define PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT 500 |
| 32 |
#endif |
| 33 |
|
| 34 |
enum { MAX_BIND_RETRY = 100 }; |
| 35 |
|
| 36 |
#ifndef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 37 |
static pj_bool_t asock_on_data_read (pj_activesock_t *asock, |
| 38 |
void *data, |
| 39 |
pj_size_t size, |
| 40 |
pj_status_t status, |
| 41 |
pj_size_t *remainder); |
| 42 |
|
| 43 |
static pj_bool_t asock_on_data_sent (pj_activesock_t *asock, |
| 44 |
pj_ioqueue_op_key_t *send_key, |
| 45 |
pj_ssize_t sent); |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifdef SSL_SOCK_IMP_USE_CIRC_BUF |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
static pj_size_t next_pow2(pj_size_t n) |
| 56 |
{ |
| 57 |
|
| 58 |
pj_size_t power = 1; |
| 59 |
while (power < n && power < 0x8000000) { |
| 60 |
power <<= 1; |
| 61 |
} |
| 62 |
return power; |
| 63 |
} |
| 64 |
|
| 65 |
static pj_status_t circ_init(pj_pool_factory *factory, |
| 66 |
circ_buf_t *cb, pj_size_t cap) |
| 67 |
{ |
| 68 |
|
| 69 |
cap = next_pow2(cap); |
| 70 |
|
| 71 |
cb->cap = cap; |
| 72 |
cb->readp = 0; |
| 73 |
cb->writep = 0; |
| 74 |
cb->size = 0; |
| 75 |
|
| 76 |
|
| 77 |
cb->pool = pj_pool_create(factory, "tls-circ%p", cap, cap, NULL); |
| 78 |
if (!cb->pool) |
| 79 |
return PJ_ENOMEM; |
| 80 |
|
| 81 |
|
| 82 |
cb->buf = pj_pool_alloc(cb->pool, cap); |
| 83 |
if (!cb->buf) { |
| 84 |
pj_pool_secure_release(&cb->pool); |
| 85 |
return PJ_ENOMEM; |
| 86 |
} |
| 87 |
|
| 88 |
return PJ_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
static void circ_reset(circ_buf_t* cb) |
| 92 |
{ |
| 93 |
cb->readp = 0; |
| 94 |
cb->writep = 0; |
| 95 |
cb->size = 0; |
| 96 |
} |
| 97 |
|
| 98 |
static void circ_deinit(circ_buf_t *cb) |
| 99 |
{ |
| 100 |
if (cb->pool) { |
| 101 |
pj_pool_secure_release(&cb->pool); |
| 102 |
cb->pool = NULL; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static pj_bool_t circ_empty(const circ_buf_t *cb) |
| 107 |
{ |
| 108 |
return cb->size == 0; |
| 109 |
} |
| 110 |
|
| 111 |
static pj_size_t circ_size(const circ_buf_t *cb) |
| 112 |
{ |
| 113 |
return cb->size; |
| 114 |
} |
| 115 |
|
| 116 |
static pj_size_t circ_avail(const circ_buf_t *cb) |
| 117 |
{ |
| 118 |
return cb->cap - cb->size; |
| 119 |
} |
| 120 |
|
| 121 |
static void circ_read(circ_buf_t *cb, pj_uint8_t *dst, pj_size_t len) |
| 122 |
{ |
| 123 |
pj_size_t size_after = cb->cap - cb->readp; |
| 124 |
pj_size_t tbc = PJ_MIN(size_after, len); |
| 125 |
pj_size_t rem = len - tbc; |
| 126 |
|
| 127 |
pj_assert(cb->size >= len); |
| 128 |
|
| 129 |
pj_memcpy(dst, cb->buf + cb->readp, tbc); |
| 130 |
pj_memcpy(dst + tbc, cb->buf, rem); |
| 131 |
|
| 132 |
cb->readp += len; |
| 133 |
cb->readp &= (cb->cap - 1); |
| 134 |
|
| 135 |
cb->size -= len; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static void circ_read_cancel(circ_buf_t* cb, pj_size_t len) |
| 142 |
{ |
| 143 |
pj_assert(cb->cap - cb->size >= len); |
| 144 |
|
| 145 |
if (cb->readp < len) |
| 146 |
cb->readp = cb->cap - (len - cb->readp); |
| 147 |
else |
| 148 |
cb->readp -= len; |
| 149 |
|
| 150 |
cb->size += len; |
| 151 |
} |
| 152 |
|
| 153 |
static pj_status_t circ_write(circ_buf_t *cb, |
| 154 |
const pj_uint8_t *src, pj_size_t len) |
| 155 |
{ |
| 156 |
|
| 157 |
if (len > circ_avail(cb)) { |
| 158 |
|
| 159 |
pj_size_t min_cap = len + cb->size; |
| 160 |
|
| 161 |
|
| 162 |
min_cap = next_pow2(min_cap); |
| 163 |
|
| 164 |
|
| 165 |
pj_pool_t *pool = pj_pool_create(cb->pool->factory, "tls-circ%p", |
| 166 |
min_cap, min_cap, NULL); |
| 167 |
if (!pool) |
| 168 |
return PJ_ENOMEM; |
| 169 |
|
| 170 |
|
| 171 |
pj_uint8_t *buf = pj_pool_alloc(pool, min_cap); |
| 172 |
if (!buf) { |
| 173 |
pj_pool_release(pool); |
| 174 |
return PJ_ENOMEM; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
pj_size_t old_size = cb->size; |
| 179 |
|
| 180 |
|
| 181 |
circ_read(cb, buf, cb->size); |
| 182 |
|
| 183 |
|
| 184 |
cb->size = old_size; |
| 185 |
|
| 186 |
|
| 187 |
pj_pool_secure_release(&cb->pool); |
| 188 |
|
| 189 |
|
| 190 |
cb->pool = pool; |
| 191 |
cb->buf = buf; |
| 192 |
cb->readp = 0; |
| 193 |
cb->writep = cb->size; |
| 194 |
cb->cap = min_cap; |
| 195 |
} |
| 196 |
|
| 197 |
pj_size_t size_after = cb->cap - cb->writep; |
| 198 |
pj_size_t tbc = PJ_MIN(size_after, len); |
| 199 |
pj_size_t rem = len - tbc; |
| 200 |
|
| 201 |
pj_memcpy(cb->buf + cb->writep, src, tbc); |
| 202 |
pj_memcpy(cb->buf, src + tbc, rem); |
| 203 |
|
| 204 |
cb->writep += len; |
| 205 |
cb->writep &= (cb->cap - 1); |
| 206 |
|
| 207 |
cb->size += len; |
| 208 |
|
| 209 |
return PJ_SUCCESS; |
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
#ifndef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 220 |
|
| 221 |
|
| 222 |
static int get_ip_addr_ver(const pj_str_t *host) |
| 223 |
{ |
| 224 |
pj_in_addr dummy; |
| 225 |
pj_in6_addr dummy6; |
| 226 |
|
| 227 |
|
| 228 |
if (host->slen == 0) |
| 229 |
return 0; |
| 230 |
|
| 231 |
|
| 232 |
if (pj_inet_pton(pj_AF_INET(), host, &dummy) == PJ_SUCCESS) |
| 233 |
return 4; |
| 234 |
|
| 235 |
|
| 236 |
if (pj_inet_pton(pj_AF_INET6(), host, &dummy6) == PJ_SUCCESS) |
| 237 |
return 6; |
| 238 |
|
| 239 |
|
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
static void ssl_close_sockets(pj_ssl_sock_t *ssock) |
| 245 |
{ |
| 246 |
pj_activesock_t *asock; |
| 247 |
pj_sock_t sock; |
| 248 |
|
| 249 |
|
| 250 |
if (!ssock->write_mutex) |
| 251 |
return; |
| 252 |
|
| 253 |
pj_lock_acquire(ssock->write_mutex); |
| 254 |
asock = ssock->asock; |
| 255 |
if (asock) { |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
ssock->sock = PJ_INVALID_SOCKET; |
| 261 |
} |
| 262 |
sock = ssock->sock; |
| 263 |
if (sock != PJ_INVALID_SOCKET) |
| 264 |
ssock->sock = PJ_INVALID_SOCKET; |
| 265 |
pj_lock_release(ssock->write_mutex); |
| 266 |
|
| 267 |
if (asock) |
| 268 |
pj_activesock_close(asock); |
| 269 |
|
| 270 |
if (sock != PJ_INVALID_SOCKET) |
| 271 |
pj_sock_close(sock); |
| 272 |
} |
| 273 |
#endif |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static pj_bool_t on_handshake_complete(pj_ssl_sock_t *ssock, |
| 281 |
pj_status_t status) |
| 282 |
{ |
| 283 |
pj_lock_acquire(ssock->write_mutex); |
| 284 |
if (ssock->handshake_status != PJ_EUNKNOWN) { |
| 285 |
pj_lock_release(ssock->write_mutex); |
| 286 |
return (ssock->handshake_status == PJ_SUCCESS)? PJ_TRUE: PJ_FALSE; |
| 287 |
} |
| 288 |
ssock->handshake_status = status; |
| 289 |
pj_lock_release(ssock->write_mutex); |
| 290 |
|
| 291 |
|
| 292 |
if (ssock->timer.id == TIMER_HANDSHAKE_TIMEOUT) { |
| 293 |
pj_timer_heap_cancel(ssock->param.timer_heap, &ssock->timer); |
| 294 |
ssock->timer.id = TIMER_NONE; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
if (status == PJ_SUCCESS) |
| 299 |
ssl_update_certs_info(ssock); |
| 300 |
|
| 301 |
|
| 302 |
if (ssock->is_server) { |
| 303 |
pj_bool_t ret = PJ_TRUE; |
| 304 |
|
| 305 |
if (status != PJ_SUCCESS) { |
| 306 |
|
| 307 |
|
| 308 |
char buf1[PJ_INET6_ADDRSTRLEN+10]; |
| 309 |
char buf2[PJ_INET6_ADDRSTRLEN+10]; |
| 310 |
|
| 311 |
if (pj_sockaddr_has_addr(&ssock->local_addr)) |
| 312 |
pj_sockaddr_print(&ssock->local_addr, buf1, sizeof(buf1), 3); |
| 313 |
else |
| 314 |
pj_ansi_snprintf(buf1, sizeof(buf1), "(unknown)"); |
| 315 |
|
| 316 |
if (pj_sockaddr_has_addr(&ssock->rem_addr)) |
| 317 |
pj_sockaddr_print(&ssock->rem_addr, buf2, sizeof(buf2), 3); |
| 318 |
else |
| 319 |
pj_ansi_snprintf(buf2, sizeof(buf2), "(unknown)"); |
| 320 |
|
| 321 |
PJ_PERROR(3,(ssock->pool->obj_name, status, |
| 322 |
"Handshake failed on %s in accepting %s", buf1, buf2)); |
| 323 |
|
| 324 |
if (ssock->param.cb.on_accept_complete2) { |
| 325 |
(*ssock->param.cb.on_accept_complete2) |
| 326 |
(ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr, |
| 327 |
pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr), |
| 328 |
status); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
if (ssock->parent->param.grp_lock) { |
| 333 |
pj_grp_lock_dec_ref(ssock->parent->param.grp_lock); |
| 334 |
ssock->parent = NULL; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
#if 1 |
| 344 |
if (ssock->param.timer_heap) { |
| 345 |
pj_time_val interval = {0, PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT}; |
| 346 |
pj_status_t status1; |
| 347 |
|
| 348 |
ssock->ssl_state = SSL_STATE_NULL; |
| 349 |
ssl_close_sockets(ssock); |
| 350 |
|
| 351 |
if (ssock->timer.id != TIMER_NONE) { |
| 352 |
pj_timer_heap_cancel(ssock->param.timer_heap, |
| 353 |
&ssock->timer); |
| 354 |
} |
| 355 |
pj_time_val_normalize(&interval); |
| 356 |
status1 = pj_timer_heap_schedule_w_grp_lock( |
| 357 |
ssock->param.timer_heap, |
| 358 |
&ssock->timer, |
| 359 |
&interval, |
| 360 |
TIMER_CLOSE, |
| 361 |
ssock->param.grp_lock); |
| 362 |
if (status1 != PJ_SUCCESS) { |
| 363 |
PJ_PERROR(3,(ssock->pool->obj_name, status, |
| 364 |
"Failed to schedule a delayed close. " |
| 365 |
"Race condition may occur.")); |
| 366 |
ssock->timer.id = TIMER_NONE; |
| 367 |
pj_ssl_sock_close(ssock); |
| 368 |
} |
| 369 |
} else { |
| 370 |
pj_ssl_sock_close(ssock); |
| 371 |
} |
| 372 |
#else |
| 373 |
{ |
| 374 |
pj_ssl_sock_close(ssock); |
| 375 |
} |
| 376 |
#endif |
| 377 |
|
| 378 |
return PJ_FALSE; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
if (ssock->param.cb.on_accept_complete2) { |
| 383 |
ret = (*ssock->param.cb.on_accept_complete2) |
| 384 |
(ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr, |
| 385 |
pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr), |
| 386 |
status); |
| 387 |
} else if (ssock->param.cb.on_accept_complete) { |
| 388 |
ret = (*ssock->param.cb.on_accept_complete) |
| 389 |
(ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr, |
| 390 |
pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr)); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
if (ssock->parent->param.grp_lock) { |
| 397 |
pj_grp_lock_dec_ref(ssock->parent->param.grp_lock); |
| 398 |
ssock->parent = NULL; |
| 399 |
} |
| 400 |
|
| 401 |
if (ret == PJ_FALSE) |
| 402 |
return PJ_FALSE; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
else { |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
if (status != PJ_SUCCESS) { |
| 411 |
char buf1[PJ_INET6_ADDRSTRLEN+10]; |
| 412 |
char buf2[PJ_INET6_ADDRSTRLEN+10]; |
| 413 |
|
| 414 |
if (pj_sockaddr_has_addr(&ssock->local_addr)) |
| 415 |
pj_sockaddr_print(&ssock->local_addr, buf1, sizeof(buf1), 3); |
| 416 |
else |
| 417 |
pj_ansi_snprintf(buf1, sizeof(buf1), "(unknown)"); |
| 418 |
|
| 419 |
if (pj_sockaddr_has_addr(&ssock->rem_addr)) |
| 420 |
pj_sockaddr_print(&ssock->rem_addr, buf2, sizeof(buf2), 3); |
| 421 |
else |
| 422 |
pj_ansi_snprintf(buf2, sizeof(buf2), "(unknown)"); |
| 423 |
|
| 424 |
PJ_PERROR(3,(ssock->pool->obj_name, status, |
| 425 |
"Handshake failed on %s in connecting to %s", |
| 426 |
buf1, buf2)); |
| 427 |
|
| 428 |
|
| 429 |
ssl_reset_sock_state(ssock); |
| 430 |
} |
| 431 |
if (ssock->param.cb.on_connect_complete) { |
| 432 |
pj_bool_t ret; |
| 433 |
ret = (*ssock->param.cb.on_connect_complete)(ssock, status); |
| 434 |
if (ret == PJ_FALSE) |
| 435 |
return PJ_FALSE; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return PJ_TRUE; |
| 440 |
} |
| 441 |
|
| 442 |
static ssl_send_op_t* alloc_send_op(pj_ssl_sock_t *ssock, pj_size_t enc_len) |
| 443 |
{ |
| 444 |
ssl_send_op_t *op; |
| 445 |
pj_pool_t *op_pool; |
| 446 |
pj_size_t alloc_len; |
| 447 |
|
| 448 |
|
| 449 |
if (enc_len < PJ_SSL_SEND_OP_MIN_BUF_SIZE) |
| 450 |
enc_len = PJ_SSL_SEND_OP_MIN_BUF_SIZE; |
| 451 |
|
| 452 |
|
| 453 |
if (!pj_list_empty(&ssock->send_op_free)) { |
| 454 |
ssl_send_op_t *p = ssock->send_op_free.next; |
| 455 |
while (p != &ssock->send_op_free) { |
| 456 |
if (p->enc_buf_cap >= enc_len) { |
| 457 |
pj_list_erase(p); |
| 458 |
ssock->send_op_free_cnt--; |
| 459 |
return p; |
| 460 |
} |
| 461 |
p = p->next; |
| 462 |
} |
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
alloc_len = sizeof(ssl_send_op_t) - 1 + enc_len; |
| 471 |
op_pool = pj_pool_create(ssock->pool->factory, "ssl_sop", |
| 472 |
alloc_len + 256, 0, NULL); |
| 473 |
if (!op_pool) |
| 474 |
return NULL; |
| 475 |
|
| 476 |
op = (ssl_send_op_t *)pj_pool_alloc(op_pool, alloc_len); |
| 477 |
if (!op) { |
| 478 |
pj_pool_release(op_pool); |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
pj_bzero(op, sizeof(ssl_send_op_t) - 1); |
| 482 |
op->pool = op_pool; |
| 483 |
op->enc_buf_cap = enc_len; |
| 484 |
return op; |
| 485 |
} |
| 486 |
|
| 487 |
static void free_send_op(pj_ssl_sock_t *ssock, ssl_send_op_t *op) |
| 488 |
{ |
| 489 |
pj_list_erase(op); |
| 490 |
if (ssock->send_op_active_cnt > 0) |
| 491 |
ssock->send_op_active_cnt--; |
| 492 |
|
| 493 |
if (ssock->send_op_free_cnt < PJ_SSL_SEND_OP_FREE_LIST_MAX) { |
| 494 |
pj_list_push_back(&ssock->send_op_free, op); |
| 495 |
ssock->send_op_free_cnt++; |
| 496 |
} else { |
| 497 |
|
| 498 |
pj_pool_secure_release(&op->pool); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
static pj_status_t ssl_do_handshake_and_flush(pj_ssl_sock_t *ssock); |
| 504 |
static pj_bool_t on_handshake_complete(pj_ssl_sock_t *ssock, |
| 505 |
pj_status_t status); |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
static pj_status_t enqueue_ssl_write_buf(pj_ssl_sock_t *ssock, |
| 512 |
pj_ioqueue_op_key_t *send_key, |
| 513 |
pj_size_t orig_len, |
| 514 |
unsigned flags, |
| 515 |
pj_bool_t *p_drain) |
| 516 |
{ |
| 517 |
pj_ssize_t len; |
| 518 |
ssl_send_op_t *op; |
| 519 |
|
| 520 |
*p_drain = PJ_FALSE; |
| 521 |
|
| 522 |
if (io_empty(ssock, &ssock->ssl_write_buf)) |
| 523 |
return PJ_SUCCESS; |
| 524 |
|
| 525 |
len = io_size(ssock, &ssock->ssl_write_buf); |
| 526 |
if (len == 0) |
| 527 |
return PJ_SUCCESS; |
| 528 |
|
| 529 |
op = alloc_send_op(ssock, len); |
| 530 |
if (!op) |
| 531 |
return PJ_ENOMEM; |
| 532 |
|
| 533 |
pj_ioqueue_op_key_init(&op->key, sizeof(pj_ioqueue_op_key_t)); |
| 534 |
op->key.user_data = op; |
| 535 |
op->app_key = send_key; |
| 536 |
op->plain_data_len = orig_len; |
| 537 |
op->enc_len = len; |
| 538 |
op->flags = flags; |
| 539 |
io_read(ssock, &ssock->ssl_write_buf, (pj_uint8_t *)op->enc_data, len); |
| 540 |
|
| 541 |
pj_list_push_back(&ssock->send_op_active, op); |
| 542 |
ssock->send_op_active_cnt++; |
| 543 |
|
| 544 |
if (ssock->sending) { |
| 545 |
|
| 546 |
return PJ_EPENDING; |
| 547 |
} |
| 548 |
|
| 549 |
ssock->sending = PJ_TRUE; |
| 550 |
*p_drain = PJ_TRUE; |
| 551 |
return PJ_EPENDING; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
f) |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
static pj_status_t drain_send_queue(pj_ssl_sock_t *ssock, |
| 564 |
pj_bool_t suppress_first_cb) |
| 565 |
{ |
| 566 |
pj_status_t first_status = PJ_SUCCESS; |
| 567 |
pj_bool_t is_first = suppress_first_cb; |
| 568 |
|
| 569 |
for (;;) { |
| 570 |
ssl_send_op_t *op; |
| 571 |
pj_ioqueue_op_key_t *app_key; |
| 572 |
pj_ssize_t plain_data_len; |
| 573 |
pj_ssize_t len; |
| 574 |
pj_status_t status; |
| 575 |
|
| 576 |
at |
| 577 |
d. |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
pj_lock_acquire(ssock->write_mutex); |
| 583 |
if (pj_list_empty(&ssock->send_op_active)) { |
| 584 |
ssock->sending = PJ_FALSE; |
| 585 |
pj_lock_release(ssock->write_mutex); |
| 586 |
break; |
| 587 |
} |
| 588 |
op = ssock->send_op_active.next; |
| 589 |
pj_list_erase(op); |
| 590 |
pj_assert(ssock->send_op_inflight == NULL); |
| 591 |
ssock->send_op_inflight = op; |
| 592 |
pj_lock_release(ssock->write_mutex); |
| 593 |
|
| 594 |
*/ |
| 595 |
len = op->enc_len; |
| 596 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 597 |
status = network_send(ssock, &op->key, op->enc_data, &len, |
| 598 |
op->flags); |
| 599 |
#else |
| 600 |
if (ssock->param.sock_type == pj_SOCK_STREAM()) { |
| 601 |
status = pj_activesock_send(ssock->asock, &op->key, |
| 602 |
op->enc_data, &len, op->flags); |
| 603 |
} else { |
| 604 |
status = pj_activesock_sendto(ssock->asock, &op->key, |
| 605 |
op->enc_data, &len, op->flags, |
| 606 |
(pj_sockaddr_t*)&ssock->rem_addr, |
| 607 |
ssock->addr_len); |
| 608 |
} |
| 609 |
#endif |
| 610 |
|
| 611 |
if (status == PJ_EPENDING) { |
| 612 |
g. |
| 613 |
|
| 614 |
|
| 615 |
if (is_first) |
| 616 |
first_status = PJ_EPENDING; |
| 617 |
break; |
| 618 |
} |
| 619 |
|
| 620 |
*/ |
| 621 |
app_key = op->app_key; |
| 622 |
plain_data_len = (pj_ssize_t)op->plain_data_len; |
| 623 |
|
| 624 |
pj_lock_acquire(ssock->write_mutex); |
| 625 |
ssock->send_op_inflight = NULL; |
| 626 |
free_send_op(ssock, op); |
| 627 |
pj_lock_release(ssock->write_mutex); |
| 628 |
|
| 629 |
if (status != PJ_SUCCESS) { |
| 630 |
*/ |
| 631 |
pj_lock_acquire(ssock->write_mutex); |
| 632 |
ssock->sending = PJ_FALSE; |
| 633 |
pj_lock_release(ssock->write_mutex); |
| 634 |
|
| 635 |
if (is_first) { |
| 636 |
first_status = status; |
| 637 |
} else if (app_key != &ssock->handshake_op_key && |
| 638 |
app_key != &ssock->shutdown_op_key && |
| 639 |
ssock->param.cb.on_data_sent) |
| 640 |
{ |
| 641 |
re |
| 642 |
|
| 643 |
|
| 644 |
pj_bool_t ret; |
| 645 |
ret = (*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 646 |
-(pj_ssize_t)status); |
| 647 |
if (!ret) { |
| 648 |
return first_status; |
| 649 |
} |
| 650 |
} |
| 651 |
break; |
| 652 |
} |
| 653 |
|
| 654 |
if (is_first) { |
| 655 |
first_status = PJ_SUCCESS; |
| 656 |
is_first = PJ_FALSE; |
| 657 |
continue; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
if (ssock->ssl_state == SSL_STATE_HANDSHAKING) { |
| 664 |
pj_status_t hs; |
| 665 |
hs = ssl_do_handshake_and_flush(ssock); |
| 666 |
if (hs != PJ_EPENDING) { |
| 667 |
if (!on_handshake_complete(ssock, hs)) |
| 668 |
return first_status; |
| 669 |
} |
| 670 |
} else if (app_key != &ssock->handshake_op_key && |
| 671 |
app_key != &ssock->shutdown_op_key) |
| 672 |
{ |
| 673 |
if (ssock->param.cb.on_data_sent) { |
| 674 |
pj_bool_t ret; |
| 675 |
ret = (*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 676 |
plain_data_len); |
| 677 |
if (!ret) |
| 678 |
return first_status; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
if (!pj_list_empty(&ssock->write_pending)) { |
| 688 |
flush_delayed_send(ssock); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
return first_status; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
static pj_status_t flush_ssl_write_buf(pj_ssl_sock_t *ssock, |
| 697 |
pj_ioqueue_op_key_t *send_key, |
| 698 |
pj_size_t orig_len, unsigned flags) |
| 699 |
{ |
| 700 |
pj_bool_t should_drain; |
| 701 |
pj_status_t status; |
| 702 |
|
| 703 |
pj_lock_acquire(ssock->write_mutex); |
| 704 |
status = enqueue_ssl_write_buf(ssock, send_key, orig_len, flags, |
| 705 |
&should_drain); |
| 706 |
pj_lock_release(ssock->write_mutex); |
| 707 |
|
| 708 |
if (should_drain) |
| 709 |
status = drain_send_queue(ssock, PJ_TRUE); |
| 710 |
|
| 711 |
return status; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
static pj_status_t ssl_do_handshake_and_flush(pj_ssl_sock_t *ssock) |
| 719 |
{ |
| 720 |
pj_status_t status; |
| 721 |
pj_status_t flush_status; |
| 722 |
|
| 723 |
status = ssl_do_handshake(ssock); |
| 724 |
|
| 725 |
|
| 726 |
SL |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
flush_status = flush_ssl_write_buf(ssock, |
| 731 |
&ssock->handshake_op_key, 0, 0); |
| 732 |
if (status == PJ_SUCCESS || status == PJ_EPENDING) { |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
if (flush_status != PJ_SUCCESS && flush_status != PJ_EPENDING) { |
| 737 |
status = flush_status; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
return status; |
| 742 |
} |
| 743 |
|
| 744 |
static void on_timer(pj_timer_heap_t *th, struct pj_timer_entry *te) |
| 745 |
{ |
| 746 |
pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)te->user_data; |
| 747 |
int timer_id = te->id; |
| 748 |
|
| 749 |
te->id = TIMER_NONE; |
| 750 |
|
| 751 |
PJ_UNUSED_ARG(th); |
| 752 |
|
| 753 |
switch (timer_id) { |
| 754 |
case TIMER_HANDSHAKE_TIMEOUT: |
| 755 |
PJ_LOG(1,(ssock->pool->obj_name, "SSL timeout after %ld.%lds", |
| 756 |
ssock->param.timeout.sec, ssock->param.timeout.msec)); |
| 757 |
|
| 758 |
on_handshake_complete(ssock, PJ_ETIMEDOUT); |
| 759 |
break; |
| 760 |
case TIMER_CLOSE: |
| 761 |
pj_ssl_sock_close(ssock); |
| 762 |
break; |
| 763 |
default: |
| 764 |
pj_assert(!"Unknown timer"); |
| 765 |
break; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
static void cancel_pending_sends(pj_ssl_sock_t *ssock) |
| 775 |
{ |
| 776 |
in |
| 777 |
|
| 778 |
|
| 779 |
if (ssock->send_op_inflight) { |
| 780 |
ssl_send_op_t *op = ssock->send_op_inflight; |
| 781 |
pj_ioqueue_op_key_t *app_key = op->app_key; |
| 782 |
|
| 783 |
ssock->send_op_inflight = NULL; |
| 784 |
|
| 785 |
if (app_key != &ssock->handshake_op_key && |
| 786 |
app_key != &ssock->shutdown_op_key && |
| 787 |
ssock->param.cb.on_data_sent) |
| 788 |
{ |
| 789 |
(*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 790 |
-PJ_ECANCELLED); |
| 791 |
} |
| 792 |
|
| 793 |
pj_lock_acquire(ssock->write_mutex); |
| 794 |
free_send_op(ssock, op); |
| 795 |
pj_lock_release(ssock->write_mutex); |
| 796 |
} |
| 797 |
|
| 798 |
*/ |
| 799 |
while (!pj_list_empty(&ssock->send_op_active)) { |
| 800 |
ssl_send_op_t *op = ssock->send_op_active.next; |
| 801 |
pj_ioqueue_op_key_t *app_key = op->app_key; |
| 802 |
|
| 803 |
pj_list_erase(op); |
| 804 |
|
| 805 |
if (app_key != &ssock->handshake_op_key && |
| 806 |
app_key != &ssock->shutdown_op_key && |
| 807 |
ssock->param.cb.on_data_sent) |
| 808 |
{ |
| 809 |
(*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 810 |
-PJ_ECANCELLED); |
| 811 |
} |
| 812 |
|
| 813 |
pj_lock_acquire(ssock->write_mutex); |
| 814 |
free_send_op(ssock, op); |
| 815 |
pj_lock_release(ssock->write_mutex); |
| 816 |
} |
| 817 |
|
| 818 |
*/ |
| 819 |
while (!pj_list_empty(&ssock->write_pending)) { |
| 820 |
write_data_t *wp = ssock->write_pending.next; |
| 821 |
pj_ioqueue_op_key_t *app_key = wp->app_key; |
| 822 |
|
| 823 |
pj_list_erase(wp); |
| 824 |
|
| 825 |
if (app_key != &ssock->handshake_op_key && |
| 826 |
app_key != &ssock->shutdown_op_key && |
| 827 |
ssock->param.cb.on_data_sent) |
| 828 |
{ |
| 829 |
(*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 830 |
-PJ_ECANCELLED); |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
static void ssl_on_destroy(void *arg) |
| 836 |
{ |
| 837 |
pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)arg; |
| 838 |
|
| 839 |
ssl_destroy(ssock); |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
if (ssock->send_op_inflight) { |
| 847 |
pj_pool_secure_release(&ssock->send_op_inflight->pool); |
| 848 |
ssock->send_op_inflight = NULL; |
| 849 |
} |
| 850 |
while (!pj_list_empty(&ssock->send_op_active)) { |
| 851 |
ssl_send_op_t *op = ssock->send_op_active.next; |
| 852 |
pj_list_erase(op); |
| 853 |
pj_pool_secure_release(&op->pool); |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
while (!pj_list_empty(&ssock->send_op_free)) { |
| 858 |
ssl_send_op_t *op = ssock->send_op_free.next; |
| 859 |
pj_list_erase(op); |
| 860 |
pj_pool_secure_release(&op->pool); |
| 861 |
} |
| 862 |
ssock->send_op_free_cnt = 0; |
| 863 |
|
| 864 |
if (ssock->ssl_read_buf_mutex) { |
| 865 |
pj_lock_destroy(ssock->ssl_read_buf_mutex); |
| 866 |
ssock->ssl_read_buf_mutex = NULL; |
| 867 |
} |
| 868 |
|
| 869 |
if (ssock->ssl_write_buf_mutex) { |
| 870 |
pj_lock_destroy(ssock->ssl_write_buf_mutex); |
| 871 |
ssock->ssl_write_buf_mutex = NULL; |
| 872 |
ssock->write_mutex = NULL; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
pj_pool_secure_release(&ssock->info_pool); |
| 877 |
pj_pool_secure_release(&ssock->pool); |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
#define OFFSET_OF_READ_DATA_PTR(ssock, asock_rbuf) \ |
| 897 |
(read_data_t**) \ |
| 898 |
((pj_int8_t*)(asock_rbuf) + \ |
| 899 |
ssock->param.read_buffer_size) |
| 900 |
|
| 901 |
static pj_bool_t ssock_on_data_read (pj_ssl_sock_t *ssock, |
| 902 |
void *data, |
| 903 |
pj_size_t size, |
| 904 |
pj_status_t status, |
| 905 |
pj_size_t *remainder) |
| 906 |
{ |
| 907 |
if (status != PJ_SUCCESS) |
| 908 |
goto on_error; |
| 909 |
|
| 910 |
if (data && size > 0) { |
| 911 |
pj_status_t status_; |
| 912 |
|
| 913 |
|
| 914 |
if (ssock->ssl_read_buf_mutex) |
| 915 |
pj_lock_acquire(ssock->ssl_read_buf_mutex); |
| 916 |
status_ = io_write(ssock,&ssock->ssl_read_buf, data, size); |
| 917 |
if (ssock->ssl_read_buf_mutex) |
| 918 |
pj_lock_release(ssock->ssl_read_buf_mutex); |
| 919 |
if (status_ != PJ_SUCCESS) { |
| 920 |
status = status_; |
| 921 |
goto on_error; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
if (ssock->ssl_state == SSL_STATE_HANDSHAKING) { |
| 927 |
pj_bool_t ret = PJ_TRUE; |
| 928 |
|
| 929 |
if (status == PJ_SUCCESS) |
| 930 |
status = ssl_do_handshake_and_flush(ssock); |
| 931 |
|
| 932 |
|
| 933 |
if (status != PJ_EPENDING) |
| 934 |
ret = on_handshake_complete(ssock, status); |
| 935 |
|
| 936 |
return ret; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
if (data && ssock->read_started) { |
| 941 |
do { |
| 942 |
read_data_t *buf = *(OFFSET_OF_READ_DATA_PTR(ssock, data)); |
| 943 |
void *data_ = (pj_int8_t*)buf->data + buf->len; |
| 944 |
int size_ = (int)(ssock->read_size - buf->len); |
| 945 |
pj_status_t status_; |
| 946 |
|
| 947 |
status_ = ssl_read(ssock, data_, &size_); |
| 948 |
|
| 949 |
if (size_ > 0) { |
| 950 |
if (ssock->param.cb.on_data_read) { |
| 951 |
pj_bool_t ret; |
| 952 |
pj_size_t remainder_ = 0; |
| 953 |
|
| 954 |
buf->len += size_; |
| 955 |
|
| 956 |
ret = (*ssock->param.cb.on_data_read)(ssock, buf->data, |
| 957 |
buf->len, status, |
| 958 |
&remainder_); |
| 959 |
if (!ret) { |
| 960 |
|
| 961 |
return PJ_FALSE; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
buf->len = remainder_; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
} else if (status_ == PJ_SUCCESS) { |
| 983 |
break; |
| 984 |
} else if (status_ == PJ_ETRYAGAIN) { |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
flush_ssl_write_buf(ssock, &ssock->handshake_op_key, |
| 990 |
0, 0); |
| 991 |
status = ssl_do_handshake_and_flush(ssock); |
| 992 |
if (status == PJ_SUCCESS) { |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
ssl_update_certs_info(ssock); |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
status = flush_delayed_send(ssock); |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
if (status == PJ_EBUSY) |
| 1006 |
status = PJ_SUCCESS; |
| 1007 |
|
| 1008 |
if (status != PJ_SUCCESS && status != PJ_EPENDING) { |
| 1009 |
PJ_PERROR(1,(ssock->pool->obj_name, status, |
| 1010 |
"Failed to flush delayed send")); |
| 1011 |
goto on_error; |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
if (status == PJ_SUCCESS) |
| 1016 |
continue; |
| 1017 |
|
| 1018 |
} else if (status != PJ_EPENDING) { |
| 1019 |
PJ_PERROR(1,(ssock->pool->obj_name, status, |
| 1020 |
"Renegotiation failed")); |
| 1021 |
goto on_error; |
| 1022 |
} |
| 1023 |
|
| 1024 |
break; |
| 1025 |
} else { |
| 1026 |
|
| 1027 |
status = status_; |
| 1028 |
goto on_error; |
| 1029 |
} |
| 1030 |
|
| 1031 |
} while (1); |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
flush_ssl_write_buf(ssock, &ssock->handshake_op_key, 0, 0); |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
if (!pj_list_empty(&ssock->write_pending)) { |
| 1047 |
flush_delayed_send(ssock); |
| 1048 |
} |
| 1049 |
|
| 1050 |
return PJ_TRUE; |
| 1051 |
|
| 1052 |
on_error: |
| 1053 |
if (ssock->ssl_state == SSL_STATE_HANDSHAKING) |
| 1054 |
return on_handshake_complete(ssock, status); |
| 1055 |
|
| 1056 |
if (ssock->read_started && ssock->param.cb.on_data_read) { |
| 1057 |
pj_bool_t ret; |
| 1058 |
ret = (*ssock->param.cb.on_data_read)(ssock, NULL, 0, status, |
| 1059 |
remainder); |
| 1060 |
if (!ret) { |
| 1061 |
|
| 1062 |
return PJ_FALSE; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
ssl_reset_sock_state(ssock); |
| 1067 |
return PJ_FALSE; |
| 1068 |
} |
| 1069 |
|
| 1070 |
static pj_bool_t ssock_on_data_sent (pj_ssl_sock_t *ssock, |
| 1071 |
pj_ioqueue_op_key_t *send_key, |
| 1072 |
pj_ssize_t sent) |
| 1073 |
{ |
| 1074 |
ssl_send_op_t *op; |
| 1075 |
pj_ioqueue_op_key_t *app_key; |
| 1076 |
pj_ssize_t sent_len; |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
if (ssock->is_closing) |
| 1083 |
return PJ_FALSE; |
| 1084 |
|
| 1085 |
op = (ssl_send_op_t *)send_key->user_data; |
| 1086 |
app_key = op->app_key; |
| 1087 |
sent_len = (sent > 0) ? (pj_ssize_t)op->plain_data_len : sent; |
| 1088 |
|
| 1089 |
|
| 1090 |
pj_lock_acquire(ssock->write_mutex); |
| 1091 |
ssock->send_op_inflight = NULL; |
| 1092 |
free_send_op(ssock, op); |
| 1093 |
pj_lock_release(ssock->write_mutex); |
| 1094 |
op = NULL; |
| 1095 |
|
| 1096 |
if (ssock->ssl_state == SSL_STATE_HANDSHAKING) { |
| 1097 |
*/ |
| 1098 |
pj_status_t status; |
| 1099 |
|
| 1100 |
status = ssl_do_handshake_and_flush(ssock); |
| 1101 |
if (status != PJ_EPENDING) { |
| 1102 |
pj_bool_t ret = on_handshake_complete(ssock, status); |
| 1103 |
if (!ret) |
| 1104 |
return PJ_FALSE; |
| 1105 |
|
| 1106 |
} |
| 1107 |
|
| 1108 |
} else if (app_key != &ssock->handshake_op_key && |
| 1109 |
app_key != &ssock->shutdown_op_key) |
| 1110 |
{ |
| 1111 |
*/ |
| 1112 |
if (ssock->param.cb.on_data_sent) { |
| 1113 |
pj_bool_t ret; |
| 1114 |
ret = (*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 1115 |
sent_len); |
| 1116 |
if (!ret) { |
| 1117 |
|
| 1118 |
return PJ_FALSE; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
if (!pj_list_empty(&ssock->write_pending)) { |
| 1125 |
flush_delayed_send(ssock); |
| 1126 |
} |
| 1127 |
|
| 1128 |
ll |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
drain_send_queue(ssock, PJ_FALSE); |
| 1133 |
|
| 1134 |
return PJ_TRUE; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static pj_status_t get_localaddr(pj_ssl_sock_t *ssock, |
| 1138 |
pj_sockaddr_t *addr, |
| 1139 |
int *namelen) |
| 1140 |
{ |
| 1141 |
PJ_UNUSED_ARG(addr); |
| 1142 |
PJ_UNUSED_ARG(namelen); |
| 1143 |
|
| 1144 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 1145 |
return network_get_localaddr(ssock, &ssock->local_addr, |
| 1146 |
&ssock->addr_len); |
| 1147 |
#else |
| 1148 |
return pj_sock_getsockname(ssock->sock, &ssock->local_addr, |
| 1149 |
&ssock->addr_len); |
| 1150 |
#endif |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
static pj_bool_t ssock_on_accept_complete (pj_ssl_sock_t *ssock_parent, |
| 1155 |
pj_sock_t newsock, |
| 1156 |
void *newconn, |
| 1157 |
const pj_sockaddr_t *src_addr, |
| 1158 |
int src_addr_len, |
| 1159 |
pj_status_t accept_status) |
| 1160 |
{ |
| 1161 |
pj_ssl_sock_t *ssock = NULL; |
| 1162 |
#ifndef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 1163 |
pj_activesock_cb asock_cb; |
| 1164 |
pj_activesock_cfg asock_cfg; |
| 1165 |
#endif |
| 1166 |
unsigned i; |
| 1167 |
pj_status_t status; |
| 1168 |
|
| 1169 |
#ifndef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 1170 |
PJ_UNUSED_ARG(newconn); |
| 1171 |
#endif |
| 1172 |
|
| 1173 |
|
| 1174 |
if (ssock_parent->newsock_param.sock_cloexec) |
| 1175 |
pj_set_cloexec_flag((int)newsock); |
| 1176 |
|
| 1177 |
if (accept_status != PJ_SUCCESS) { |
| 1178 |
if (ssock_parent->param.cb.on_accept_complete2) { |
| 1179 |
(*ssock_parent->param.cb.on_accept_complete2)(ssock_parent, NULL, |
| 1180 |
src_addr, |
| 1181 |
src_addr_len, |
| 1182 |
accept_status); |
| 1183 |
} |
| 1184 |
return PJ_TRUE; |
| 1185 |
} |
| 1186 |
|
| 1187 |
|
| 1188 |
status = pj_ssl_sock_create(ssock_parent->pool, |
| 1189 |
&ssock_parent->newsock_param, &ssock); |
| 1190 |
if (status != PJ_SUCCESS) |
| 1191 |
goto on_return; |
| 1192 |
|
| 1193 |
|
| 1194 |
ssock->parent = ssock_parent; |
| 1195 |
if (ssock->parent->param.grp_lock) |
| 1196 |
pj_grp_lock_add_ref(ssock->parent->param.grp_lock); |
| 1197 |
|
| 1198 |
|
| 1199 |
ssock->sock = newsock; |
| 1200 |
ssock->is_server = PJ_TRUE; |
| 1201 |
if (ssock_parent->cert) { |
| 1202 |
status = pj_ssl_sock_set_certificate(ssock, ssock->pool, |
| 1203 |
ssock_parent->cert); |
| 1204 |
if (status != PJ_SUCCESS) |
| 1205 |
goto on_return; |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
ssock->addr_len = src_addr_len; |
| 1210 |
pj_sockaddr_cp(&ssock->local_addr, &ssock_parent->local_addr); |
| 1211 |
|
| 1212 |
|
| 1213 |
pj_sockaddr_cp(&ssock->rem_addr, src_addr); |
| 1214 |
|
| 1215 |
|
| 1216 |
status = ssl_create(ssock); |
| 1217 |
if (status != PJ_SUCCESS) |
| 1218 |
goto on_return; |
| 1219 |
|
| 1220 |
|
| 1221 |
ssl_set_peer_name(ssock); |
| 1222 |
|
| 1223 |
|
| 1224 |
ssock->asock_rbuf = (void**)pj_pool_calloc(ssock->pool, |
| 1225 |
ssock->param.async_cnt, |
| 1226 |
sizeof(void*)); |
| 1227 |
if (!ssock->asock_rbuf) { |
| 1228 |
status = PJ_ENOMEM; |
| 1229 |
goto on_return; |
| 1230 |
} |
| 1231 |
|
| 1232 |
for (i = 0; i<ssock->param.async_cnt; ++i) { |
| 1233 |
ssock->asock_rbuf[i] = (void*) pj_pool_alloc( |
| 1234 |
ssock->pool, |
| 1235 |
ssock->param.read_buffer_size + |
| 1236 |
sizeof(read_data_t*)); |
| 1237 |
if (!ssock->asock_rbuf[i]) { |
| 1238 |
status = PJ_ENOMEM; |
| 1239 |
goto on_return; |
| 1240 |
} |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
if (ssock_parent->param.grp_lock) { |
| 1247 |
pj_grp_lock_t *glock; |
| 1248 |
|
| 1249 |
status = pj_grp_lock_create(ssock->pool, NULL, &glock); |
| 1250 |
if (status != PJ_SUCCESS) |
| 1251 |
goto on_return; |
| 1252 |
|
| 1253 |
pj_grp_lock_add_ref(glock); |
| 1254 |
ssock->param.grp_lock = glock; |
| 1255 |
pj_grp_lock_add_handler(ssock->param.grp_lock, ssock->pool, ssock, |
| 1256 |
ssl_on_destroy); |
| 1257 |
} |
| 1258 |
|
| 1259 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 1260 |
status = network_setup_connection(ssock, newconn); |
| 1261 |
if (status != PJ_SUCCESS) |
| 1262 |
goto on_return; |
| 1263 |
|
| 1264 |
#else |
| 1265 |
|
| 1266 |
status = pj_sock_apply_qos2(ssock->sock, ssock->param.qos_type, |
| 1267 |
&ssock->param.qos_params, 1, |
| 1268 |
ssock->pool->obj_name, NULL); |
| 1269 |
if (status != PJ_SUCCESS && !ssock->param.qos_ignore_error) |
| 1270 |
goto on_return; |
| 1271 |
|
| 1272 |
|
| 1273 |
if (ssock->param.sockopt_params.cnt) { |
| 1274 |
status = pj_sock_setsockopt_params(ssock->sock, |
| 1275 |
&ssock->param.sockopt_params); |
| 1276 |
if (status != PJ_SUCCESS && !ssock->param.sockopt_ignore_error) |
| 1277 |
goto on_return; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
pj_activesock_cfg_default(&asock_cfg); |
| 1282 |
asock_cfg.grp_lock = ssock->param.grp_lock; |
| 1283 |
asock_cfg.async_cnt = ssock->param.async_cnt; |
| 1284 |
asock_cfg.concurrency = ssock->param.concurrency; |
| 1285 |
asock_cfg.whole_data = PJ_TRUE; |
| 1286 |
|
| 1287 |
pj_bzero(&asock_cb, sizeof(asock_cb)); |
| 1288 |
asock_cb.on_data_read = asock_on_data_read; |
| 1289 |
asock_cb.on_data_sent = asock_on_data_sent; |
| 1290 |
|
| 1291 |
status = pj_activesock_create(ssock->pool, |
| 1292 |
ssock->sock, |
| 1293 |
ssock->param.sock_type, |
| 1294 |
&asock_cfg, |
| 1295 |
ssock->param.ioqueue, |
| 1296 |
&asock_cb, |
| 1297 |
ssock, |
| 1298 |
&ssock->asock); |
| 1299 |
|
| 1300 |
if (status != PJ_SUCCESS) |
| 1301 |
goto on_return; |
| 1302 |
|
| 1303 |
|
| 1304 |
status = pj_activesock_start_read2(ssock->asock, ssock->pool, |
| 1305 |
(unsigned)ssock->param.read_buffer_size, |
| 1306 |
ssock->asock_rbuf, |
| 1307 |
PJ_IOQUEUE_ALWAYS_ASYNC); |
| 1308 |
if (status != PJ_SUCCESS) |
| 1309 |
goto on_return; |
| 1310 |
#endif |
| 1311 |
|
| 1312 |
|
| 1313 |
status = get_localaddr(ssock, &ssock->local_addr, &ssock->addr_len); |
| 1314 |
if (status != PJ_SUCCESS) { |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
pj_sockaddr_cp(&ssock->local_addr, &ssock_parent->local_addr); |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
if (ssock->param.timer_heap && (ssock->param.timeout.sec != 0 || |
| 1323 |
ssock->param.timeout.msec != 0)) |
| 1324 |
{ |
| 1325 |
pj_assert(ssock->timer.id == TIMER_NONE); |
| 1326 |
status = pj_timer_heap_schedule_w_grp_lock(ssock->param.timer_heap, |
| 1327 |
&ssock->timer, |
| 1328 |
&ssock->param.timeout, |
| 1329 |
TIMER_HANDSHAKE_TIMEOUT, |
| 1330 |
ssock->param.grp_lock); |
| 1331 |
if (status != PJ_SUCCESS) { |
| 1332 |
ssock->timer.id = TIMER_NONE; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
pj_lock_acquire(ssock->write_mutex); |
| 1346 |
ssock->ssl_state = SSL_STATE_HANDSHAKING; |
| 1347 |
ssl_set_state(ssock, PJ_TRUE); |
| 1348 |
status = ssl_do_handshake_and_flush(ssock); |
| 1349 |
pj_lock_release(ssock->write_mutex); |
| 1350 |
|
| 1351 |
on_return: |
| 1352 |
if (ssock && status != PJ_EPENDING) { |
| 1353 |
on_handshake_complete(ssock, status); |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
return PJ_TRUE; |
| 1358 |
} |
| 1359 |
|
| 1360 |
static pj_bool_t ssock_on_connect_complete (pj_ssl_sock_t *ssock, |
| 1361 |
pj_status_t status) |
| 1362 |
{ |
| 1363 |
unsigned i; |
| 1364 |
|
| 1365 |
if (status != PJ_SUCCESS) |
| 1366 |
goto on_return; |
| 1367 |
|
| 1368 |
|
| 1369 |
ssock->addr_len = sizeof(pj_sockaddr); |
| 1370 |
status = get_localaddr(ssock, &ssock->local_addr, &ssock->addr_len); |
| 1371 |
if (status != PJ_SUCCESS) |
| 1372 |
goto on_return; |
| 1373 |
|
| 1374 |
|
| 1375 |
status = ssl_create(ssock); |
| 1376 |
if (status != PJ_SUCCESS) |
| 1377 |
goto on_return; |
| 1378 |
|
| 1379 |
|
| 1380 |
ssock->asock_rbuf = (void**)pj_pool_calloc(ssock->pool, |
| 1381 |
ssock->param.async_cnt, |
| 1382 |
sizeof(void*)); |
| 1383 |
if (!ssock->asock_rbuf) { |
| 1384 |
status = PJ_ENOMEM; |
| 1385 |
goto on_return; |
| 1386 |
} |
| 1387 |
|
| 1388 |
for (i = 0; i<ssock->param.async_cnt; ++i) { |
| 1389 |
ssock->asock_rbuf[i] = (void*) pj_pool_alloc( |
| 1390 |
ssock->pool, |
| 1391 |
ssock->param.read_buffer_size + |
| 1392 |
sizeof(read_data_t*)); |
| 1393 |
if (!ssock->asock_rbuf[i]) { |
| 1394 |
status = PJ_ENOMEM; |
| 1395 |
goto on_return; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 1401 |
status = network_start_read(ssock, ssock->param.async_cnt, |
| 1402 |
(unsigned)ssock->param.read_buffer_size, |
| 1403 |
ssock->asock_rbuf, 0); |
| 1404 |
#else |
| 1405 |
status = pj_activesock_start_read2(ssock->asock, ssock->pool, |
| 1406 |
(unsigned)ssock->param.read_buffer_size, |
| 1407 |
ssock->asock_rbuf, |
| 1408 |
PJ_IOQUEUE_ALWAYS_ASYNC); |
| 1409 |
#endif |
| 1410 |
if (status != PJ_SUCCESS) |
| 1411 |
goto on_return; |
| 1412 |
|
| 1413 |
|
| 1414 |
ssl_set_peer_name(ssock); |
| 1415 |
|
| 1416 |
|
| 1417 |
ssock->ssl_state = SSL_STATE_HANDSHAKING; |
| 1418 |
ssl_set_state(ssock, PJ_FALSE); |
| 1419 |
|
| 1420 |
status = ssl_do_handshake_and_flush(ssock); |
| 1421 |
if (status != PJ_EPENDING) |
| 1422 |
goto on_return; |
| 1423 |
|
| 1424 |
return PJ_TRUE; |
| 1425 |
|
| 1426 |
on_return: |
| 1427 |
return on_handshake_complete(ssock, status); |
| 1428 |
} |
| 1429 |
|
| 1430 |
#ifndef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 1431 |
static pj_bool_t asock_on_data_read (pj_activesock_t *asock, |
| 1432 |
void *data, |
| 1433 |
pj_size_t size, |
| 1434 |
pj_status_t status, |
| 1435 |
pj_size_t *remainder) |
| 1436 |
{ |
| 1437 |
pj_ssl_sock_t *ssock = (pj_ssl_sock_t*) |
| 1438 |
pj_activesock_get_user_data(asock); |
| 1439 |
|
| 1440 |
return ssock_on_data_read(ssock, data, size, status, remainder); |
| 1441 |
} |
| 1442 |
|
| 1443 |
static pj_bool_t asock_on_data_sent (pj_activesock_t *asock, |
| 1444 |
pj_ioqueue_op_key_t *send_key, |
| 1445 |
pj_ssize_t sent) |
| 1446 |
{ |
| 1447 |
pj_ssl_sock_t *ssock = (pj_ssl_sock_t*) |
| 1448 |
pj_activesock_get_user_data(asock); |
| 1449 |
|
| 1450 |
return ssock_on_data_sent(ssock, send_key, sent); |
| 1451 |
} |
| 1452 |
|
| 1453 |
static pj_bool_t asock_on_accept_complete2(pj_activesock_t *asock, |
| 1454 |
pj_sock_t newsock, |
| 1455 |
const pj_sockaddr_t *src_addr, |
| 1456 |
int src_addr_len, |
| 1457 |
pj_status_t status) |
| 1458 |
{ |
| 1459 |
pj_ssl_sock_t *ssock = (pj_ssl_sock_t*) |
| 1460 |
pj_activesock_get_user_data(asock); |
| 1461 |
|
| 1462 |
return ssock_on_accept_complete(ssock, newsock, NULL, |
| 1463 |
src_addr, src_addr_len, status); |
| 1464 |
} |
| 1465 |
|
| 1466 |
static pj_bool_t asock_on_connect_complete (pj_activesock_t *asock, |
| 1467 |
pj_status_t status) |
| 1468 |
{ |
| 1469 |
pj_ssl_sock_t *ssock = (pj_ssl_sock_t*) |
| 1470 |
pj_activesock_get_user_data(asock); |
| 1471 |
|
| 1472 |
return ssock_on_connect_complete(ssock, status); |
| 1473 |
} |
| 1474 |
#endif |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
PJ_DEF(pj_status_t) pj_ssl_cipher_get_availables(pj_ssl_cipher ciphers[], |
| 1484 |
unsigned *cipher_num) |
| 1485 |
{ |
| 1486 |
unsigned i; |
| 1487 |
|
| 1488 |
PJ_ASSERT_RETURN(ciphers && cipher_num, PJ_EINVAL); |
| 1489 |
|
| 1490 |
ssl_ciphers_populate(); |
| 1491 |
|
| 1492 |
if (ssl_cipher_num == 0) { |
| 1493 |
*cipher_num = 0; |
| 1494 |
return PJ_ENOTFOUND; |
| 1495 |
} |
| 1496 |
|
| 1497 |
*cipher_num = PJ_MIN(*cipher_num, ssl_cipher_num); |
| 1498 |
|
| 1499 |
for (i = 0; i < *cipher_num; ++i) |
| 1500 |
ciphers[i] = ssl_ciphers[i].id; |
| 1501 |
|
| 1502 |
return PJ_SUCCESS; |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
PJ_DEF(const char*) pj_ssl_cipher_name(pj_ssl_cipher cipher) |
| 1507 |
{ |
| 1508 |
unsigned i; |
| 1509 |
|
| 1510 |
ssl_ciphers_populate(); |
| 1511 |
|
| 1512 |
for (i = 0; i < ssl_cipher_num; ++i) { |
| 1513 |
if (cipher == ssl_ciphers[i].id) |
| 1514 |
return ssl_ciphers[i].name; |
| 1515 |
} |
| 1516 |
|
| 1517 |
return NULL; |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
PJ_DEF(pj_ssl_cipher) pj_ssl_cipher_id(const char *cipher_name) |
| 1522 |
{ |
| 1523 |
unsigned i; |
| 1524 |
|
| 1525 |
ssl_ciphers_populate(); |
| 1526 |
|
| 1527 |
for (i = 0; i < ssl_cipher_num; ++i) { |
| 1528 |
if (!pj_ansi_stricmp(ssl_ciphers[i].name, cipher_name)) |
| 1529 |
return ssl_ciphers[i].id; |
| 1530 |
} |
| 1531 |
|
| 1532 |
return PJ_TLS_UNKNOWN_CIPHER; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
PJ_DEF(pj_bool_t) pj_ssl_cipher_is_supported(pj_ssl_cipher cipher) |
| 1537 |
{ |
| 1538 |
unsigned i; |
| 1539 |
|
| 1540 |
ssl_ciphers_populate(); |
| 1541 |
|
| 1542 |
for (i = 0; i < ssl_cipher_num; ++i) { |
| 1543 |
if (cipher == ssl_ciphers[i].id) |
| 1544 |
return PJ_TRUE; |
| 1545 |
} |
| 1546 |
|
| 1547 |
return PJ_FALSE; |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
PJ_DEF(pj_status_t) pj_ssl_curve_get_availables(pj_ssl_curve curves[], |
| 1552 |
unsigned *curve_num) |
| 1553 |
{ |
| 1554 |
unsigned i; |
| 1555 |
|
| 1556 |
PJ_ASSERT_RETURN(curves && curve_num, PJ_EINVAL); |
| 1557 |
|
| 1558 |
ssl_ciphers_populate(); |
| 1559 |
|
| 1560 |
if (ssl_curves_num == 0) { |
| 1561 |
*curve_num = 0; |
| 1562 |
return PJ_ENOTFOUND; |
| 1563 |
} |
| 1564 |
|
| 1565 |
*curve_num = PJ_MIN(*curve_num, ssl_curves_num); |
| 1566 |
|
| 1567 |
for (i = 0; i < *curve_num; ++i) |
| 1568 |
curves[i] = ssl_curves[i].id; |
| 1569 |
|
| 1570 |
return PJ_SUCCESS; |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
PJ_DEF(const char*) pj_ssl_curve_name(pj_ssl_curve curve) |
| 1575 |
{ |
| 1576 |
unsigned i; |
| 1577 |
|
| 1578 |
ssl_ciphers_populate(); |
| 1579 |
|
| 1580 |
for (i = 0; i < ssl_curves_num; ++i) { |
| 1581 |
if (curve == ssl_curves[i].id) |
| 1582 |
return ssl_curves[i].name; |
| 1583 |
} |
| 1584 |
|
| 1585 |
return NULL; |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
PJ_DEF(pj_ssl_curve) pj_ssl_curve_id(const char *curve_name) |
| 1590 |
{ |
| 1591 |
unsigned i; |
| 1592 |
|
| 1593 |
ssl_ciphers_populate(); |
| 1594 |
|
| 1595 |
for (i = 0; i < ssl_curves_num; ++i) { |
| 1596 |
if (ssl_curves[i].name && |
| 1597 |
!pj_ansi_stricmp(ssl_curves[i].name, curve_name)) |
| 1598 |
{ |
| 1599 |
return ssl_curves[i].id; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
return PJ_TLS_UNKNOWN_CURVE; |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
PJ_DEF(pj_bool_t) pj_ssl_curve_is_supported(pj_ssl_curve curve) |
| 1608 |
{ |
| 1609 |
unsigned i; |
| 1610 |
|
| 1611 |
ssl_ciphers_populate(); |
| 1612 |
|
| 1613 |
for (i = 0; i < ssl_curves_num; ++i) { |
| 1614 |
if (curve == ssl_curves[i].id) |
| 1615 |
return PJ_TRUE; |
| 1616 |
} |
| 1617 |
|
| 1618 |
return PJ_FALSE; |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
PJ_DEF(pj_status_t) pj_ssl_sock_create (pj_pool_t *pool, |
| 1625 |
const pj_ssl_sock_param *param, |
| 1626 |
pj_ssl_sock_t **p_ssock) |
| 1627 |
{ |
| 1628 |
pj_ssl_sock_t *ssock; |
| 1629 |
pj_status_t status; |
| 1630 |
pj_pool_t *info_pool; |
| 1631 |
|
| 1632 |
PJ_ASSERT_RETURN(pool && param && p_ssock, PJ_EINVAL); |
| 1633 |
PJ_ASSERT_RETURN((param->sock_type & 0xF) == pj_SOCK_STREAM(), PJ_ENOTSUP); |
| 1634 |
|
| 1635 |
info_pool = pj_pool_create(pool->factory, "ssl_chain%p", 512, 512, NULL); |
| 1636 |
pool = pj_pool_create(pool->factory, "ssl%p", 512, 512, NULL); |
| 1637 |
|
| 1638 |
|
| 1639 |
ssock = ssl_alloc(pool); |
| 1640 |
if (!ssock) |
| 1641 |
return PJ_ENOMEM; |
| 1642 |
ssock->pool = pool; |
| 1643 |
ssock->info_pool = info_pool; |
| 1644 |
ssock->sock = PJ_INVALID_SOCKET; |
| 1645 |
ssock->ssl_state = SSL_STATE_NULL; |
| 1646 |
ssock->ssl_read_buf.owner = ssock; |
| 1647 |
ssock->ssl_write_buf.owner = ssock; |
| 1648 |
ssock->handshake_status = PJ_EUNKNOWN; |
| 1649 |
pj_list_init(&ssock->write_pending); |
| 1650 |
pj_list_init(&ssock->write_pending_empty); |
| 1651 |
pj_list_init(&ssock->send_op_active); |
| 1652 |
pj_list_init(&ssock->send_op_free); |
| 1653 |
pj_timer_entry_init(&ssock->timer, 0, ssock, &on_timer); |
| 1654 |
pj_ioqueue_op_key_init(&ssock->handshake_op_key, |
| 1655 |
sizeof(pj_ioqueue_op_key_t)); |
| 1656 |
pj_ioqueue_op_key_init(&ssock->shutdown_op_key, |
| 1657 |
sizeof(pj_ioqueue_op_key_t)); |
| 1658 |
|
| 1659 |
|
| 1660 |
status = pj_lock_create_recursive_mutex(pool, pool->obj_name, |
| 1661 |
&ssock->ssl_write_buf_mutex); |
| 1662 |
ssock->write_mutex = ssock->ssl_write_buf_mutex; |
| 1663 |
if (status != PJ_SUCCESS) |
| 1664 |
return status; |
| 1665 |
|
| 1666 |
|
| 1667 |
status = pj_lock_create_recursive_mutex(pool, pool->obj_name, |
| 1668 |
&ssock->ssl_read_buf_mutex); |
| 1669 |
if (status != PJ_SUCCESS) |
| 1670 |
return status; |
| 1671 |
|
| 1672 |
|
| 1673 |
ssock->sending = PJ_FALSE; |
| 1674 |
|
| 1675 |
|
| 1676 |
pj_ssl_sock_param_copy(pool, &ssock->param, param); |
| 1677 |
|
| 1678 |
if (ssock->param.grp_lock) { |
| 1679 |
pj_grp_lock_add_ref(ssock->param.grp_lock); |
| 1680 |
pj_grp_lock_add_handler(ssock->param.grp_lock, pool, ssock, |
| 1681 |
ssl_on_destroy); |
| 1682 |
} |
| 1683 |
|
| 1684 |
ssock->param.read_buffer_size = ((ssock->param.read_buffer_size+7)>>3)<<3; |
| 1685 |
if (!ssock->param.timer_heap) { |
| 1686 |
PJ_LOG(3,(ssock->pool->obj_name, "Warning: timer heap is not " |
| 1687 |
"available. It is recommended to supply one to avoid " |
| 1688 |
"a race condition if more than one worker threads " |
| 1689 |
"are used.")); |
| 1690 |
} |
| 1691 |
|
| 1692 |
|
| 1693 |
*p_ssock = ssock; |
| 1694 |
|
| 1695 |
return PJ_SUCCESS; |
| 1696 |
} |
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
PJ_DEF(pj_status_t) pj_ssl_sock_close(pj_ssl_sock_t *ssock) |
| 1704 |
{ |
| 1705 |
PJ_ASSERT_RETURN(ssock, PJ_EINVAL); |
| 1706 |
|
| 1707 |
if (!ssock->pool || ssock->is_closing) |
| 1708 |
return PJ_SUCCESS; |
| 1709 |
|
| 1710 |
ssock->is_closing = PJ_TRUE; |
| 1711 |
|
| 1712 |
if (ssock->timer.id != TIMER_NONE) { |
| 1713 |
pj_timer_heap_cancel(ssock->param.timer_heap, &ssock->timer); |
| 1714 |
ssock->timer.id = TIMER_NONE; |
| 1715 |
} |
| 1716 |
|
| 1717 |
ssl_reset_sock_state(ssock); |
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
cancel_pending_sends(ssock); |
| 1725 |
|
| 1726 |
|
| 1727 |
if (ssock->cert) { |
| 1728 |
pj_ssl_cert_wipe_keys(ssock->cert); |
| 1729 |
|
| 1730 |
} |
| 1731 |
|
| 1732 |
if (ssock->param.grp_lock) { |
| 1733 |
pj_grp_lock_dec_ref(ssock->param.grp_lock); |
| 1734 |
} else { |
| 1735 |
ssl_on_destroy(ssock); |
| 1736 |
} |
| 1737 |
|
| 1738 |
return PJ_SUCCESS; |
| 1739 |
} |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
PJ_DEF(pj_status_t) pj_ssl_sock_set_user_data(pj_ssl_sock_t *ssock, |
| 1746 |
void *user_data) |
| 1747 |
{ |
| 1748 |
PJ_ASSERT_RETURN(ssock, PJ_EINVAL); |
| 1749 |
|
| 1750 |
ssock->param.user_data = user_data; |
| 1751 |
return PJ_SUCCESS; |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
PJ_DEF(void*) pj_ssl_sock_get_user_data(pj_ssl_sock_t *ssock) |
| 1760 |
{ |
| 1761 |
PJ_ASSERT_RETURN(ssock, NULL); |
| 1762 |
|
| 1763 |
return ssock->param.user_data; |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
PJ_DEF(pj_status_t) pj_ssl_sock_get_info (pj_ssl_sock_t *ssock, |
| 1770 |
pj_ssl_sock_info *info) |
| 1771 |
{ |
| 1772 |
pj_bzero(info, sizeof(*info)); |
| 1773 |
|
| 1774 |
|
| 1775 |
info->established = (ssock->ssl_state == SSL_STATE_ESTABLISHED); |
| 1776 |
|
| 1777 |
|
| 1778 |
info->proto = ssock->param.proto; |
| 1779 |
|
| 1780 |
|
| 1781 |
pj_sockaddr_cp(&info->local_addr, &ssock->local_addr); |
| 1782 |
|
| 1783 |
|
| 1784 |
info->local_cert_info = &ssock->local_cert_info; |
| 1785 |
info->remote_cert_info = &ssock->remote_cert_info; |
| 1786 |
|
| 1787 |
|
| 1788 |
if (pj_sockaddr_has_addr(&ssock->rem_addr)) |
| 1789 |
pj_sockaddr_cp(&info->remote_addr, &ssock->rem_addr); |
| 1790 |
|
| 1791 |
if (info->established) { |
| 1792 |
info->cipher = ssl_get_cipher(ssock); |
| 1793 |
} |
| 1794 |
|
| 1795 |
|
| 1796 |
info->verify_status = ssock->verify_status; |
| 1797 |
|
| 1798 |
|
| 1799 |
info->last_native_err = ssock->last_err; |
| 1800 |
|
| 1801 |
|
| 1802 |
info->grp_lock = ssock->param.grp_lock; |
| 1803 |
|
| 1804 |
|
| 1805 |
#if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0 && \ |
| 1806 |
(PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) |
| 1807 |
{ |
| 1808 |
ossl_sock_t *ossock = (ossl_sock_t *)ssock; |
| 1809 |
info->native_ssl = ossock->ossl_ssl; |
| 1810 |
} |
| 1811 |
#endif |
| 1812 |
|
| 1813 |
return PJ_SUCCESS; |
| 1814 |
} |
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_read (pj_ssl_sock_t *ssock, |
| 1821 |
pj_pool_t *pool, |
| 1822 |
unsigned buff_size, |
| 1823 |
pj_uint32_t flags) |
| 1824 |
{ |
| 1825 |
void **readbuf; |
| 1826 |
unsigned i; |
| 1827 |
|
| 1828 |
PJ_ASSERT_RETURN(ssock && pool && buff_size, PJ_EINVAL); |
| 1829 |
|
| 1830 |
if (ssock->ssl_state != SSL_STATE_ESTABLISHED) |
| 1831 |
return PJ_EINVALIDOP; |
| 1832 |
|
| 1833 |
readbuf = (void**) pj_pool_calloc(pool, ssock->param.async_cnt, |
| 1834 |
sizeof(void*)); |
| 1835 |
if (!readbuf) |
| 1836 |
return PJ_ENOMEM; |
| 1837 |
|
| 1838 |
for (i=0; i<ssock->param.async_cnt; ++i) { |
| 1839 |
readbuf[i] = pj_pool_alloc(pool, buff_size); |
| 1840 |
if (!readbuf[i]) |
| 1841 |
return PJ_ENOMEM; |
| 1842 |
} |
| 1843 |
|
| 1844 |
return pj_ssl_sock_start_read2(ssock, pool, buff_size, |
| 1845 |
readbuf, flags); |
| 1846 |
} |
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_read2 (pj_ssl_sock_t *ssock, |
| 1855 |
pj_pool_t *pool, |
| 1856 |
unsigned buff_size, |
| 1857 |
void *readbuf[], |
| 1858 |
pj_uint32_t flags) |
| 1859 |
{ |
| 1860 |
unsigned i; |
| 1861 |
|
| 1862 |
PJ_ASSERT_RETURN(ssock && pool && buff_size && readbuf, PJ_EINVAL); |
| 1863 |
|
| 1864 |
if (ssock->ssl_state != SSL_STATE_ESTABLISHED) |
| 1865 |
return PJ_EINVALIDOP; |
| 1866 |
|
| 1867 |
|
| 1868 |
ssock->ssock_rbuf = (read_data_t*)pj_pool_calloc(pool, |
| 1869 |
ssock->param.async_cnt, |
| 1870 |
sizeof(read_data_t)); |
| 1871 |
if (!ssock->ssock_rbuf) |
| 1872 |
return PJ_ENOMEM; |
| 1873 |
|
| 1874 |
|
| 1875 |
for (i=0; i<ssock->param.async_cnt; ++i) { |
| 1876 |
read_data_t **p_ssock_rbuf = |
| 1877 |
OFFSET_OF_READ_DATA_PTR(ssock, ssock->asock_rbuf[i]); |
| 1878 |
|
| 1879 |
ssock->ssock_rbuf[i].data = readbuf[i]; |
| 1880 |
ssock->ssock_rbuf[i].len = 0; |
| 1881 |
|
| 1882 |
*p_ssock_rbuf = &ssock->ssock_rbuf[i]; |
| 1883 |
} |
| 1884 |
|
| 1885 |
ssock->read_size = buff_size; |
| 1886 |
ssock->read_started = PJ_TRUE; |
| 1887 |
ssock->read_flags = flags; |
| 1888 |
|
| 1889 |
for (i=0; i<ssock->param.async_cnt; ++i) { |
| 1890 |
if (ssock->asock_rbuf[i]) { |
| 1891 |
pj_size_t remainder = 0; |
| 1892 |
ssock_on_data_read(ssock, ssock->asock_rbuf[i], 0, |
| 1893 |
PJ_SUCCESS, &remainder); |
| 1894 |
} |
| 1895 |
} |
| 1896 |
|
| 1897 |
return PJ_SUCCESS; |
| 1898 |
} |
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_recvfrom (pj_ssl_sock_t *ssock, |
| 1907 |
pj_pool_t *pool, |
| 1908 |
unsigned buff_size, |
| 1909 |
pj_uint32_t flags) |
| 1910 |
{ |
| 1911 |
PJ_UNUSED_ARG(ssock); |
| 1912 |
PJ_UNUSED_ARG(pool); |
| 1913 |
PJ_UNUSED_ARG(buff_size); |
| 1914 |
PJ_UNUSED_ARG(flags); |
| 1915 |
|
| 1916 |
return PJ_ENOTSUP; |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_recvfrom2 (pj_ssl_sock_t *ssock, |
| 1926 |
pj_pool_t *pool, |
| 1927 |
unsigned buff_size, |
| 1928 |
void *readbuf[], |
| 1929 |
pj_uint32_t flags) |
| 1930 |
{ |
| 1931 |
PJ_UNUSED_ARG(ssock); |
| 1932 |
PJ_UNUSED_ARG(pool); |
| 1933 |
PJ_UNUSED_ARG(buff_size); |
| 1934 |
PJ_UNUSED_ARG(readbuf); |
| 1935 |
PJ_UNUSED_ARG(flags); |
| 1936 |
|
| 1937 |
return PJ_ENOTSUP; |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
static pj_status_t ssl_send (pj_ssl_sock_t *ssock, |
| 1943 |
pj_ioqueue_op_key_t *send_key, |
| 1944 |
const void *data, |
| 1945 |
pj_ssize_t size, |
| 1946 |
unsigned flags) |
| 1947 |
{ |
| 1948 |
pj_status_t status; |
| 1949 |
int nwritten = 0; |
| 1950 |
pj_bool_t should_drain = PJ_FALSE; |
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
pj_lock_acquire(ssock->write_mutex); |
| 1957 |
status = ssl_write(ssock, data, size, &nwritten); |
| 1958 |
|
| 1959 |
if (status == PJ_SUCCESS && nwritten == size) { |
| 1960 |
*/ |
| 1961 |
status = enqueue_ssl_write_buf(ssock, send_key, size, flags, |
| 1962 |
&should_drain); |
| 1963 |
pj_lock_release(ssock->write_mutex); |
| 1964 |
|
| 1965 |
if (should_drain) |
| 1966 |
status = drain_send_queue(ssock, PJ_TRUE); |
| 1967 |
|
| 1968 |
} else if (status == PJ_ETRYAGAIN) { |
| 1969 |
*/ |
| 1970 |
status = enqueue_ssl_write_buf(ssock, &ssock->handshake_op_key, |
| 1971 |
0, 0, &should_drain); |
| 1972 |
pj_lock_release(ssock->write_mutex); |
| 1973 |
|
| 1974 |
if (should_drain) { |
| 1975 |
pj_status_t ds = drain_send_queue(ssock, PJ_TRUE); |
| 1976 |
if (ds == PJ_SUCCESS || ds == PJ_EPENDING) |
| 1977 |
status = PJ_EBUSY; |
| 1978 |
else |
| 1979 |
status = ds; |
| 1980 |
} else { |
| 1981 |
status = PJ_EBUSY; |
| 1982 |
} |
| 1983 |
} else { |
| 1984 |
pj_lock_release(ssock->write_mutex); |
| 1985 |
} |
| 1986 |
|
| 1987 |
return status; |
| 1988 |
} |
| 1989 |
|
| 1990 |
|
| 1991 |
static pj_status_t flush_delayed_send(pj_ssl_sock_t *ssock) |
| 1992 |
{ |
| 1993 |
|
| 1994 |
if (ssock->flushing_write_pend) |
| 1995 |
return PJ_EBUSY; |
| 1996 |
|
| 1997 |
pj_lock_acquire(ssock->write_mutex); |
| 1998 |
|
| 1999 |
|
| 2000 |
if (ssock->flushing_write_pend) { |
| 2001 |
pj_lock_release(ssock->write_mutex); |
| 2002 |
return PJ_EBUSY; |
| 2003 |
} |
| 2004 |
|
| 2005 |
|
| 2006 |
ssock->flushing_write_pend = PJ_TRUE; |
| 2007 |
|
| 2008 |
while (!pj_list_empty(&ssock->write_pending)) { |
| 2009 |
write_data_t *wp; |
| 2010 |
pj_ioqueue_op_key_t *app_key; |
| 2011 |
pj_ssize_t plain_data_len; |
| 2012 |
pj_status_t status; |
| 2013 |
|
| 2014 |
wp = ssock->write_pending.next; |
| 2015 |
app_key = wp->app_key; |
| 2016 |
plain_data_len = wp->plain_data_len; |
| 2017 |
|
| 2018 |
|
| 2019 |
pj_lock_release(ssock->write_mutex); |
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
status = ssl_send(ssock, app_key, wp->data.ptr, |
| 2025 |
plain_data_len, wp->flags); |
| 2026 |
|
| 2027 |
if (status == PJ_EPENDING) { |
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
pj_lock_acquire(ssock->write_mutex); |
| 2032 |
pj_list_erase(wp); |
| 2033 |
pj_list_push_back(&ssock->write_pending_empty, wp); |
| 2034 |
pj_lock_release(ssock->write_mutex); |
| 2035 |
|
| 2036 |
ssock->flushing_write_pend = PJ_FALSE; |
| 2037 |
return PJ_EPENDING; |
| 2038 |
} |
| 2039 |
|
| 2040 |
if (status != PJ_SUCCESS) { |
| 2041 |
|
| 2042 |
ssock->flushing_write_pend = PJ_FALSE; |
| 2043 |
return status; |
| 2044 |
} |
| 2045 |
|
| 2046 |
|
| 2047 |
pj_lock_acquire(ssock->write_mutex); |
| 2048 |
pj_list_erase(wp); |
| 2049 |
pj_list_push_back(&ssock->write_pending_empty, wp); |
| 2050 |
pj_lock_release(ssock->write_mutex); |
| 2051 |
|
| 2052 |
om |
| 2053 |
|
| 2054 |
|
| 2055 |
if (ssock->param.cb.on_data_sent) { |
| 2056 |
pj_bool_t ret; |
| 2057 |
ret = (*ssock->param.cb.on_data_sent)(ssock, app_key, |
| 2058 |
plain_data_len); |
| 2059 |
if (!ret) { |
| 2060 |
|
| 2061 |
return PJ_SUCCESS; |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
pj_lock_acquire(ssock->write_mutex); |
| 2066 |
} |
| 2067 |
|
| 2068 |
|
| 2069 |
ssock->flushing_write_pend = PJ_FALSE; |
| 2070 |
|
| 2071 |
pj_lock_release(ssock->write_mutex); |
| 2072 |
|
| 2073 |
return PJ_SUCCESS; |
| 2074 |
} |
| 2075 |
|
| 2076 |
|
| 2077 |
static pj_status_t delay_send (pj_ssl_sock_t *ssock, |
| 2078 |
pj_ioqueue_op_key_t *send_key, |
| 2079 |
const void *data, |
| 2080 |
pj_ssize_t size, |
| 2081 |
unsigned flags) |
| 2082 |
{ |
| 2083 |
write_data_t *wp; |
| 2084 |
|
| 2085 |
pj_lock_acquire(ssock->write_mutex); |
| 2086 |
|
| 2087 |
|
| 2088 |
if (!pj_list_empty(&ssock->write_pending_empty)) { |
| 2089 |
wp = ssock->write_pending_empty.next; |
| 2090 |
pj_list_erase(wp); |
| 2091 |
} else { |
| 2092 |
wp = PJ_POOL_ZALLOC_T(ssock->pool, write_data_t); |
| 2093 |
} |
| 2094 |
|
| 2095 |
wp->app_key = send_key; |
| 2096 |
wp->plain_data_len = size; |
| 2097 |
wp->data.ptr = data; |
| 2098 |
wp->flags = flags; |
| 2099 |
|
| 2100 |
pj_list_push_back(&ssock->write_pending, wp); |
| 2101 |
|
| 2102 |
pj_lock_release(ssock->write_mutex); |
| 2103 |
|
| 2104 |
|
| 2105 |
return PJ_EPENDING; |
| 2106 |
} |
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
PJ_DEF(pj_status_t) pj_ssl_sock_send (pj_ssl_sock_t *ssock, |
| 2113 |
pj_ioqueue_op_key_t *send_key, |
| 2114 |
const void *data, |
| 2115 |
pj_ssize_t *size, |
| 2116 |
unsigned flags) |
| 2117 |
{ |
| 2118 |
pj_status_t status; |
| 2119 |
|
| 2120 |
PJ_ASSERT_RETURN(ssock && data && size && (*size>0), PJ_EINVAL); |
| 2121 |
|
| 2122 |
if (ssock->ssl_state != SSL_STATE_ESTABLISHED) |
| 2123 |
return PJ_EINVALIDOP; |
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
status = flush_delayed_send(ssock); |
| 2132 |
if (status == PJ_EBUSY || status == PJ_EPENDING) { |
| 2133 |
|
| 2134 |
status = delay_send(ssock, send_key, data, *size, flags); |
| 2135 |
goto on_return; |
| 2136 |
} else if (status != PJ_SUCCESS) { |
| 2137 |
goto on_return; |
| 2138 |
} |
| 2139 |
|
| 2140 |
|
| 2141 |
ce |
| 2142 |
|
| 2143 |
|
| 2144 |
#if PJ_SSL_SEND_OP_ACTIVE_MAX > 0 |
| 2145 |
if (ssock->send_op_active_cnt >= PJ_SSL_SEND_OP_ACTIVE_MAX) { |
| 2146 |
status = PJ_EBUSY; |
| 2147 |
goto on_return; |
| 2148 |
} |
| 2149 |
#endif |
| 2150 |
|
| 2151 |
|
| 2152 |
status = ssl_send(ssock, send_key, data, *size, flags); |
| 2153 |
if (status == PJ_EBUSY) { |
| 2154 |
|
| 2155 |
status = delay_send(ssock, send_key, data, *size, flags); |
| 2156 |
} |
| 2157 |
|
| 2158 |
on_return: |
| 2159 |
|
| 2160 |
return status; |
| 2161 |
} |
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
PJ_DEF(pj_status_t) pj_ssl_sock_sendto (pj_ssl_sock_t *ssock, |
| 2168 |
pj_ioqueue_op_key_t *send_key, |
| 2169 |
const void *data, |
| 2170 |
pj_ssize_t *size, |
| 2171 |
unsigned flags, |
| 2172 |
const pj_sockaddr_t *addr, |
| 2173 |
int addr_len) |
| 2174 |
{ |
| 2175 |
PJ_UNUSED_ARG(ssock); |
| 2176 |
PJ_UNUSED_ARG(send_key); |
| 2177 |
PJ_UNUSED_ARG(data); |
| 2178 |
PJ_UNUSED_ARG(size); |
| 2179 |
PJ_UNUSED_ARG(flags); |
| 2180 |
PJ_UNUSED_ARG(addr); |
| 2181 |
PJ_UNUSED_ARG(addr_len); |
| 2182 |
|
| 2183 |
return PJ_ENOTSUP; |
| 2184 |
} |
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_accept (pj_ssl_sock_t *ssock, |
| 2191 |
pj_pool_t *pool, |
| 2192 |
const pj_sockaddr_t *localaddr, |
| 2193 |
int addr_len) |
| 2194 |
{ |
| 2195 |
return pj_ssl_sock_start_accept2(ssock, pool, localaddr, addr_len, |
| 2196 |
&ssock->param); |
| 2197 |
} |
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
PJ_DEF(pj_status_t) |
| 2205 |
pj_ssl_sock_start_accept2(pj_ssl_sock_t *ssock, |
| 2206 |
pj_pool_t *pool, |
| 2207 |
const pj_sockaddr_t *localaddr, |
| 2208 |
int addr_len, |
| 2209 |
const pj_ssl_sock_param *newsock_param) |
| 2210 |
{ |
| 2211 |
pj_status_t status; |
| 2212 |
#ifndef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 2213 |
pj_activesock_cb asock_cb; |
| 2214 |
pj_activesock_cfg asock_cfg; |
| 2215 |
#endif |
| 2216 |
|
| 2217 |
PJ_ASSERT_RETURN(ssock && pool && localaddr && addr_len, PJ_EINVAL); |
| 2218 |
|
| 2219 |
|
| 2220 |
if (newsock_param->grp_lock != ssock->param.grp_lock || |
| 2221 |
newsock_param->sock_af != ssock->param.sock_af || |
| 2222 |
newsock_param->sock_type != ssock->param.sock_type) |
| 2223 |
{ |
| 2224 |
return PJ_EINVAL; |
| 2225 |
} |
| 2226 |
|
| 2227 |
ssock->is_server = PJ_TRUE; |
| 2228 |
|
| 2229 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 2230 |
status = network_start_accept(ssock, pool, localaddr, addr_len, |
| 2231 |
newsock_param); |
| 2232 |
if (status != PJ_SUCCESS) |
| 2233 |
goto on_error; |
| 2234 |
#else |
| 2235 |
|
| 2236 |
if (ssock->param.sock_cloexec) |
| 2237 |
ssock->param.sock_type |= pj_SOCK_CLOEXEC(); |
| 2238 |
status = pj_sock_socket(ssock->param.sock_af, ssock->param.sock_type, 0, |
| 2239 |
&ssock->sock); |
| 2240 |
if (status != PJ_SUCCESS) |
| 2241 |
goto on_error; |
| 2242 |
|
| 2243 |
|
| 2244 |
if (ssock->param.reuse_addr) { |
| 2245 |
int enabled = 1; |
| 2246 |
status = pj_sock_setsockopt(ssock->sock, pj_SOL_SOCKET(), |
| 2247 |
pj_SO_REUSEADDR(), |
| 2248 |
&enabled, sizeof(enabled)); |
| 2249 |
if (status != PJ_SUCCESS) { |
| 2250 |
PJ_PERROR(4,(ssock->pool->obj_name, status, |
| 2251 |
"Warning: error applying SO_REUSEADDR")); |
| 2252 |
} |
| 2253 |
} |
| 2254 |
|
| 2255 |
|
| 2256 |
status = pj_sock_apply_qos2(ssock->sock, ssock->param.qos_type, |
| 2257 |
&ssock->param.qos_params, 2, |
| 2258 |
ssock->pool->obj_name, NULL); |
| 2259 |
if (status != PJ_SUCCESS && !ssock->param.qos_ignore_error) |
| 2260 |
goto on_error; |
| 2261 |
|
| 2262 |
|
| 2263 |
if (ssock->param.sockopt_params.cnt) { |
| 2264 |
status = pj_sock_setsockopt_params(ssock->sock, |
| 2265 |
&ssock->param.sockopt_params); |
| 2266 |
|
| 2267 |
if (status != PJ_SUCCESS && !ssock->param.sockopt_ignore_error) |
| 2268 |
goto on_error; |
| 2269 |
} |
| 2270 |
|
| 2271 |
|
| 2272 |
status = pj_sock_bind(ssock->sock, localaddr, addr_len); |
| 2273 |
if (status != PJ_SUCCESS) |
| 2274 |
goto on_error; |
| 2275 |
|
| 2276 |
|
| 2277 |
status = pj_sock_listen(ssock->sock, PJ_SOMAXCONN); |
| 2278 |
if (status != PJ_SUCCESS) |
| 2279 |
goto on_error; |
| 2280 |
|
| 2281 |
|
| 2282 |
pj_activesock_cfg_default(&asock_cfg); |
| 2283 |
asock_cfg.async_cnt = ssock->param.async_cnt; |
| 2284 |
asock_cfg.concurrency = ssock->param.concurrency; |
| 2285 |
asock_cfg.whole_data = PJ_FALSE; |
| 2286 |
asock_cfg.grp_lock = ssock->param.grp_lock; |
| 2287 |
|
| 2288 |
pj_bzero(&asock_cb, sizeof(asock_cb)); |
| 2289 |
|
| 2290 |
asock_cb.on_accept_complete2 = asock_on_accept_complete2; |
| 2291 |
|
| 2292 |
status = pj_activesock_create(pool, |
| 2293 |
ssock->sock, |
| 2294 |
ssock->param.sock_type, |
| 2295 |
&asock_cfg, |
| 2296 |
ssock->param.ioqueue, |
| 2297 |
&asock_cb, |
| 2298 |
ssock, |
| 2299 |
&ssock->asock); |
| 2300 |
|
| 2301 |
if (status != PJ_SUCCESS) |
| 2302 |
goto on_error; |
| 2303 |
|
| 2304 |
|
| 2305 |
pj_ssl_sock_param_copy(pool, &ssock->newsock_param, newsock_param); |
| 2306 |
ssock->newsock_param.grp_lock = NULL; |
| 2307 |
status = pj_activesock_start_accept(ssock->asock, pool); |
| 2308 |
if (status != PJ_SUCCESS) |
| 2309 |
goto on_error; |
| 2310 |
|
| 2311 |
|
| 2312 |
ssock->addr_len = addr_len; |
| 2313 |
status = pj_sock_getsockname(ssock->sock, &ssock->local_addr, |
| 2314 |
&ssock->addr_len); |
| 2315 |
if (status != PJ_SUCCESS) |
| 2316 |
pj_sockaddr_cp(&ssock->local_addr, localaddr); |
| 2317 |
#endif |
| 2318 |
|
| 2319 |
return PJ_SUCCESS; |
| 2320 |
|
| 2321 |
on_error: |
| 2322 |
ssl_reset_sock_state(ssock); |
| 2323 |
return status; |
| 2324 |
} |
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_connect(pj_ssl_sock_t *ssock, |
| 2331 |
pj_pool_t *pool, |
| 2332 |
const pj_sockaddr_t *localaddr, |
| 2333 |
const pj_sockaddr_t *remaddr, |
| 2334 |
int addr_len) |
| 2335 |
{ |
| 2336 |
pj_ssl_start_connect_param param; |
| 2337 |
param.pool = pool; |
| 2338 |
param.localaddr = localaddr; |
| 2339 |
param.local_port_range = 0; |
| 2340 |
param.remaddr = remaddr; |
| 2341 |
param.addr_len = addr_len; |
| 2342 |
|
| 2343 |
return pj_ssl_sock_start_connect2(ssock, ¶m); |
| 2344 |
} |
| 2345 |
|
| 2346 |
PJ_DEF(pj_status_t) pj_ssl_sock_start_connect2( |
| 2347 |
pj_ssl_sock_t *ssock, |
| 2348 |
pj_ssl_start_connect_param *connect_param) |
| 2349 |
{ |
| 2350 |
pj_status_t status; |
| 2351 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 2352 |
status = network_start_connect(ssock, connect_param); |
| 2353 |
if (status != PJ_EPENDING) |
| 2354 |
goto on_error; |
| 2355 |
#else |
| 2356 |
pj_activesock_cb asock_cb; |
| 2357 |
pj_activesock_cfg asock_cfg; |
| 2358 |
|
| 2359 |
pj_pool_t *pool = connect_param->pool; |
| 2360 |
const pj_sockaddr_t *localaddr = connect_param->localaddr; |
| 2361 |
pj_uint16_t port_range = connect_param->local_port_range; |
| 2362 |
const pj_sockaddr_t *remaddr = connect_param->remaddr; |
| 2363 |
int addr_len = connect_param->addr_len; |
| 2364 |
|
| 2365 |
PJ_ASSERT_RETURN(ssock && pool && localaddr && remaddr && addr_len, |
| 2366 |
PJ_EINVAL); |
| 2367 |
|
| 2368 |
|
| 2369 |
if (ssock->param.sock_cloexec) |
| 2370 |
ssock->param.sock_type |= pj_SOCK_CLOEXEC(); |
| 2371 |
status = pj_sock_socket(ssock->param.sock_af, ssock->param.sock_type, 0, |
| 2372 |
&ssock->sock); |
| 2373 |
if (status != PJ_SUCCESS) |
| 2374 |
goto on_error; |
| 2375 |
|
| 2376 |
|
| 2377 |
status = pj_sock_apply_qos2(ssock->sock, ssock->param.qos_type, |
| 2378 |
&ssock->param.qos_params, 2, |
| 2379 |
ssock->pool->obj_name, NULL); |
| 2380 |
if (status != PJ_SUCCESS && !ssock->param.qos_ignore_error) |
| 2381 |
goto on_error; |
| 2382 |
|
| 2383 |
|
| 2384 |
if (ssock->param.sockopt_params.cnt) { |
| 2385 |
status = pj_sock_setsockopt_params(ssock->sock, |
| 2386 |
&ssock->param.sockopt_params); |
| 2387 |
|
| 2388 |
if (status != PJ_SUCCESS && !ssock->param.sockopt_ignore_error) |
| 2389 |
goto on_error; |
| 2390 |
} |
| 2391 |
|
| 2392 |
|
| 2393 |
if (port_range) { |
| 2394 |
pj_uint16_t max_bind_retry = MAX_BIND_RETRY; |
| 2395 |
if (port_range && port_range < max_bind_retry) |
| 2396 |
{ |
| 2397 |
max_bind_retry = port_range; |
| 2398 |
} |
| 2399 |
status = pj_sock_bind_random(ssock->sock, localaddr, port_range, |
| 2400 |
max_bind_retry); |
| 2401 |
} else { |
| 2402 |
status = pj_sock_bind(ssock->sock, localaddr, addr_len); |
| 2403 |
} |
| 2404 |
|
| 2405 |
if (status != PJ_SUCCESS) |
| 2406 |
goto on_error; |
| 2407 |
|
| 2408 |
|
| 2409 |
pj_activesock_cfg_default(&asock_cfg); |
| 2410 |
asock_cfg.async_cnt = ssock->param.async_cnt; |
| 2411 |
asock_cfg.concurrency = ssock->param.concurrency; |
| 2412 |
asock_cfg.whole_data = PJ_TRUE; |
| 2413 |
asock_cfg.grp_lock = ssock->param.grp_lock; |
| 2414 |
|
| 2415 |
pj_bzero(&asock_cb, sizeof(asock_cb)); |
| 2416 |
asock_cb.on_connect_complete = asock_on_connect_complete; |
| 2417 |
asock_cb.on_data_read = asock_on_data_read; |
| 2418 |
asock_cb.on_data_sent = asock_on_data_sent; |
| 2419 |
|
| 2420 |
status = pj_activesock_create(pool, |
| 2421 |
ssock->sock, |
| 2422 |
ssock->param.sock_type, |
| 2423 |
&asock_cfg, |
| 2424 |
ssock->param.ioqueue, |
| 2425 |
&asock_cb, |
| 2426 |
ssock, |
| 2427 |
&ssock->asock); |
| 2428 |
|
| 2429 |
if (status != PJ_SUCCESS) |
| 2430 |
goto on_error; |
| 2431 |
|
| 2432 |
|
| 2433 |
pj_sockaddr_cp(&ssock->rem_addr, remaddr); |
| 2434 |
|
| 2435 |
status = pj_activesock_start_connect(ssock->asock, pool, remaddr, |
| 2436 |
addr_len); |
| 2437 |
|
| 2438 |
if (status == PJ_SUCCESS) { |
| 2439 |
|
| 2440 |
|
| 2441 |
er |
| 2442 |
|
| 2443 |
|
| 2444 |
asock_on_connect_complete(ssock->asock, PJ_SUCCESS); |
| 2445 |
return PJ_EPENDING; |
| 2446 |
} else if (status != PJ_EPENDING) { |
| 2447 |
goto on_error; |
| 2448 |
} |
| 2449 |
|
| 2450 |
|
| 2451 |
ssock->addr_len = addr_len; |
| 2452 |
status = pj_sock_getsockname(ssock->sock, &ssock->local_addr, |
| 2453 |
&ssock->addr_len); |
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
#endif |
| 2463 |
|
| 2464 |
|
| 2465 |
if (ssock->param.timer_heap && |
| 2466 |
(ssock->param.timeout.sec != 0 || ssock->param.timeout.msec != 0)) |
| 2467 |
{ |
| 2468 |
pj_assert(ssock->timer.id == TIMER_NONE); |
| 2469 |
status = pj_timer_heap_schedule_w_grp_lock(ssock->param.timer_heap, |
| 2470 |
&ssock->timer, |
| 2471 |
&ssock->param.timeout, |
| 2472 |
TIMER_HANDSHAKE_TIMEOUT, |
| 2473 |
ssock->param.grp_lock); |
| 2474 |
if (status != PJ_SUCCESS) { |
| 2475 |
ssock->timer.id = TIMER_NONE; |
| 2476 |
status = PJ_SUCCESS; |
| 2477 |
} |
| 2478 |
} |
| 2479 |
|
| 2480 |
|
| 2481 |
ssock->is_server = PJ_FALSE; |
| 2482 |
|
| 2483 |
return PJ_EPENDING; |
| 2484 |
|
| 2485 |
on_error: |
| 2486 |
ssl_reset_sock_state(ssock); |
| 2487 |
return status; |
| 2488 |
} |
| 2489 |
|
| 2490 |
|
| 2491 |
PJ_DEF(pj_status_t) pj_ssl_sock_renegotiate(pj_ssl_sock_t *ssock) |
| 2492 |
{ |
| 2493 |
pj_status_t status; |
| 2494 |
|
| 2495 |
PJ_ASSERT_RETURN(ssock, PJ_EINVAL); |
| 2496 |
|
| 2497 |
if (ssock->ssl_state != SSL_STATE_ESTABLISHED) |
| 2498 |
return PJ_EINVALIDOP; |
| 2499 |
|
| 2500 |
status = ssl_renegotiate(ssock); |
| 2501 |
if (status == PJ_SUCCESS) { |
| 2502 |
status = ssl_do_handshake_and_flush(ssock); |
| 2503 |
} |
| 2504 |
|
| 2505 |
return status; |
| 2506 |
} |
| 2507 |
|
| 2508 |
static void wipe_buf(pj_str_t *buf) |
| 2509 |
{ |
| 2510 |
volatile char *p = buf->ptr; |
| 2511 |
pj_ssize_t len = buf->slen; |
| 2512 |
while (len--) *p++ = 0; |
| 2513 |
buf->slen = 0; |
| 2514 |
} |
| 2515 |
|
| 2516 |
PJ_DEF(void) pj_ssl_cert_wipe_keys(pj_ssl_cert_t *cert) |
| 2517 |
{ |
| 2518 |
if (cert) { |
| 2519 |
#if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL) |
| 2520 |
wipe_buf(&cert->CA_file); |
| 2521 |
wipe_buf(&cert->CA_path); |
| 2522 |
wipe_buf(&cert->cert_file); |
| 2523 |
wipe_buf(&cert->privkey_file); |
| 2524 |
wipe_buf(&cert->privkey_pass); |
| 2525 |
wipe_buf(&cert->CA_buf); |
| 2526 |
wipe_buf(&cert->cert_buf); |
| 2527 |
wipe_buf(&cert->privkey_buf); |
| 2528 |
#else |
| 2529 |
cert->criteria.type = PJ_SSL_CERT_LOOKUP_NONE; |
| 2530 |
wipe_buf(&cert->criteria.keyword); |
| 2531 |
#endif |
| 2532 |
|
| 2533 |
#if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) |
| 2534 |
ssl_free_cert(cert); |
| 2535 |
#endif |
| 2536 |
} |
| 2537 |
} |
| 2538 |
|
| 2539 |
|
| 2540 |
PJ_DEF(pj_status_t) pj_ssl_cert_load_from_files (pj_pool_t *pool, |
| 2541 |
const pj_str_t *CA_file, |
| 2542 |
const pj_str_t *cert_file, |
| 2543 |
const pj_str_t *privkey_file, |
| 2544 |
const pj_str_t *privkey_pass, |
| 2545 |
pj_ssl_cert_t **p_cert) |
| 2546 |
{ |
| 2547 |
return pj_ssl_cert_load_from_files2(pool, CA_file, NULL, cert_file, |
| 2548 |
privkey_file, privkey_pass, p_cert); |
| 2549 |
} |
| 2550 |
|
| 2551 |
PJ_DEF(pj_status_t) pj_ssl_cert_load_from_files2(pj_pool_t *pool, |
| 2552 |
const pj_str_t *CA_file, |
| 2553 |
const pj_str_t *CA_path, |
| 2554 |
const pj_str_t *cert_file, |
| 2555 |
const pj_str_t *privkey_file, |
| 2556 |
const pj_str_t *privkey_pass, |
| 2557 |
pj_ssl_cert_t **p_cert) |
| 2558 |
{ |
| 2559 |
#if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL) |
| 2560 |
pj_ssl_cert_t *cert; |
| 2561 |
|
| 2562 |
PJ_ASSERT_RETURN(pool && p_cert && |
| 2563 |
(CA_file || CA_path || cert_file || privkey_file), |
| 2564 |
PJ_EINVAL); |
| 2565 |
|
| 2566 |
cert = *p_cert; |
| 2567 |
if (!cert) |
| 2568 |
cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t); |
| 2569 |
if (!cert) |
| 2570 |
return PJ_ENOMEM; |
| 2571 |
|
| 2572 |
if (CA_file) { |
| 2573 |
pj_strdup_with_null(pool, &cert->CA_file, CA_file); |
| 2574 |
} |
| 2575 |
if (CA_path) { |
| 2576 |
pj_strdup_with_null(pool, &cert->CA_path, CA_path); |
| 2577 |
} |
| 2578 |
if (cert_file) { |
| 2579 |
pj_strdup_with_null(pool, &cert->cert_file, cert_file); |
| 2580 |
} |
| 2581 |
if (privkey_file) { |
| 2582 |
pj_strdup_with_null(pool, &cert->privkey_file, privkey_file); |
| 2583 |
} |
| 2584 |
if (privkey_pass) { |
| 2585 |
pj_strdup_with_null(pool, &cert->privkey_pass, privkey_pass); |
| 2586 |
} |
| 2587 |
|
| 2588 |
*p_cert = cert; |
| 2589 |
|
| 2590 |
return PJ_SUCCESS; |
| 2591 |
#else |
| 2592 |
PJ_UNUSED_ARG(pool); |
| 2593 |
PJ_UNUSED_ARG(CA_file); |
| 2594 |
PJ_UNUSED_ARG(CA_path); |
| 2595 |
PJ_UNUSED_ARG(cert_file); |
| 2596 |
PJ_UNUSED_ARG(privkey_file); |
| 2597 |
PJ_UNUSED_ARG(privkey_pass); |
| 2598 |
PJ_UNUSED_ARG(p_cert); |
| 2599 |
return PJ_ENOTSUP; |
| 2600 |
#endif |
| 2601 |
} |
| 2602 |
|
| 2603 |
PJ_DEF(pj_status_t) pj_ssl_cert_load_from_buffer(pj_pool_t *pool, |
| 2604 |
const pj_ssl_cert_buffer *CA_buf, |
| 2605 |
const pj_ssl_cert_buffer *cert_buf, |
| 2606 |
const pj_ssl_cert_buffer *privkey_buf, |
| 2607 |
const pj_str_t *privkey_pass, |
| 2608 |
pj_ssl_cert_t **p_cert) |
| 2609 |
{ |
| 2610 |
#if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL) |
| 2611 |
pj_ssl_cert_t *cert; |
| 2612 |
|
| 2613 |
PJ_ASSERT_RETURN(pool && p_cert && |
| 2614 |
(CA_buf || cert_buf || privkey_buf), |
| 2615 |
PJ_EINVAL); |
| 2616 |
|
| 2617 |
cert = *p_cert; |
| 2618 |
if (!cert) |
| 2619 |
cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t); |
| 2620 |
if (!cert) |
| 2621 |
return PJ_ENOMEM; |
| 2622 |
|
| 2623 |
if (CA_buf) { |
| 2624 |
pj_strdup(pool, &cert->CA_buf, CA_buf); |
| 2625 |
} |
| 2626 |
if (cert_buf) { |
| 2627 |
pj_strdup(pool, &cert->cert_buf, cert_buf); |
| 2628 |
} |
| 2629 |
if (privkey_buf) { |
| 2630 |
pj_strdup(pool, &cert->privkey_buf, privkey_buf); |
| 2631 |
} |
| 2632 |
if (privkey_pass) { |
| 2633 |
pj_strdup_with_null(pool, &cert->privkey_pass, privkey_pass); |
| 2634 |
} |
| 2635 |
|
| 2636 |
*p_cert = cert; |
| 2637 |
|
| 2638 |
return PJ_SUCCESS; |
| 2639 |
#else |
| 2640 |
PJ_UNUSED_ARG(pool); |
| 2641 |
PJ_UNUSED_ARG(CA_buf); |
| 2642 |
PJ_UNUSED_ARG(cert_buf); |
| 2643 |
PJ_UNUSED_ARG(privkey_buf); |
| 2644 |
PJ_UNUSED_ARG(privkey_pass); |
| 2645 |
PJ_UNUSED_ARG(p_cert); |
| 2646 |
return PJ_ENOTSUP; |
| 2647 |
#endif |
| 2648 |
} |
| 2649 |
|
| 2650 |
|
| 2651 |
PJ_DEF(pj_status_t) pj_ssl_cert_load_from_store( |
| 2652 |
pj_pool_t *pool, |
| 2653 |
const pj_ssl_cert_lookup_criteria *criteria, |
| 2654 |
pj_ssl_cert_t **p_cert) |
| 2655 |
{ |
| 2656 |
#if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL) |
| 2657 |
pj_ssl_cert_t *cert; |
| 2658 |
|
| 2659 |
PJ_ASSERT_RETURN(pool && p_cert && criteria, PJ_EINVAL); |
| 2660 |
|
| 2661 |
cert = *p_cert; |
| 2662 |
if (!cert) |
| 2663 |
cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t); |
| 2664 |
if (!cert) |
| 2665 |
return PJ_ENOMEM; |
| 2666 |
|
| 2667 |
pj_memcpy(&cert->criteria, criteria, sizeof(*criteria)); |
| 2668 |
pj_strdup_with_null(pool, &cert->criteria.keyword, &criteria->keyword); |
| 2669 |
|
| 2670 |
*p_cert = cert; |
| 2671 |
|
| 2672 |
return PJ_SUCCESS; |
| 2673 |
#else |
| 2674 |
PJ_UNUSED_ARG(pool); |
| 2675 |
PJ_UNUSED_ARG(criteria); |
| 2676 |
PJ_UNUSED_ARG(p_cert); |
| 2677 |
return PJ_ENOTSUP; |
| 2678 |
#endif |
| 2679 |
} |
| 2680 |
|
| 2681 |
|
| 2682 |
PJ_DEF(pj_status_t) pj_ssl_cert_load_direct( |
| 2683 |
pj_pool_t *pool, |
| 2684 |
pj_ssl_cert_direct *cert_direct, |
| 2685 |
pj_ssl_cert_t **p_cert) |
| 2686 |
{ |
| 2687 |
#if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) |
| 2688 |
pj_ssl_cert_t *cert; |
| 2689 |
|
| 2690 |
PJ_ASSERT_RETURN(pool && p_cert && cert_direct, PJ_EINVAL); |
| 2691 |
|
| 2692 |
cert = *p_cert; |
| 2693 |
if (!cert) |
| 2694 |
cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t); |
| 2695 |
if (!cert) |
| 2696 |
return PJ_ENOMEM; |
| 2697 |
|
| 2698 |
cert->direct = *cert_direct; |
| 2699 |
|
| 2700 |
*p_cert = cert; |
| 2701 |
|
| 2702 |
return PJ_SUCCESS; |
| 2703 |
#else |
| 2704 |
PJ_UNUSED_ARG(pool); |
| 2705 |
PJ_UNUSED_ARG(cert_direct); |
| 2706 |
PJ_UNUSED_ARG(p_cert); |
| 2707 |
return PJ_ENOTSUP; |
| 2708 |
#endif |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
PJ_DEF(pj_status_t) pj_ssl_sock_set_certificate( |
| 2714 |
pj_ssl_sock_t *ssock, |
| 2715 |
pj_pool_t *pool, |
| 2716 |
const pj_ssl_cert_t *cert) |
| 2717 |
{ |
| 2718 |
pj_ssl_cert_t *cert_; |
| 2719 |
|
| 2720 |
PJ_ASSERT_RETURN(ssock && pool && cert, PJ_EINVAL); |
| 2721 |
|
| 2722 |
cert_ = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t); |
| 2723 |
pj_memcpy(cert_, cert, sizeof(pj_ssl_cert_t)); |
| 2724 |
|
| 2725 |
#if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL) |
| 2726 |
pj_strdup_with_null(pool, &cert_->CA_file, &cert->CA_file); |
| 2727 |
pj_strdup_with_null(pool, &cert_->CA_path, &cert->CA_path); |
| 2728 |
pj_strdup_with_null(pool, &cert_->cert_file, &cert->cert_file); |
| 2729 |
pj_strdup_with_null(pool, &cert_->privkey_file, &cert->privkey_file); |
| 2730 |
pj_strdup_with_null(pool, &cert_->privkey_pass, &cert->privkey_pass); |
| 2731 |
|
| 2732 |
pj_strdup(pool, &cert_->CA_buf, &cert->CA_buf); |
| 2733 |
pj_strdup(pool, &cert_->cert_buf, &cert->cert_buf); |
| 2734 |
pj_strdup(pool, &cert_->privkey_buf, &cert->privkey_buf); |
| 2735 |
|
| 2736 |
|
| 2737 |
# if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) && \ |
| 2738 |
(OPENSSL_VERSION_NUMBER >= 0x30000000L) |
| 2739 |
if ((cert_->direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY) && |
| 2740 |
cert_->direct.privkey) |
| 2741 |
{ |
| 2742 |
EVP_PKEY_up_ref(cert_->direct.privkey); |
| 2743 |
} |
| 2744 |
|
| 2745 |
if ((cert_->direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT) && |
| 2746 |
cert_->direct.cert) |
| 2747 |
{ |
| 2748 |
X509_up_ref(cert_->direct.cert); |
| 2749 |
} |
| 2750 |
# endif |
| 2751 |
|
| 2752 |
#else |
| 2753 |
pj_strdup_with_null(pool, &cert_->criteria.keyword, |
| 2754 |
&cert->criteria.keyword); |
| 2755 |
#endif |
| 2756 |
|
| 2757 |
ssock->cert = cert_; |
| 2758 |
|
| 2759 |
return PJ_SUCCESS; |
| 2760 |
} |
| 2761 |
|