| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include "os_port.h" |
| 35 |
#include "ssl.h" |
| 36 |
|
| 37 |
static const uint8_t g_hello_done[] = { HS_SERVER_HELLO_DONE, 0, 0, 0 }; |
| 38 |
static const uint8_t g_asn1_sha256[] = |
| 39 |
{ |
| 40 |
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 41 |
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 |
| 42 |
}; |
| 43 |
|
| 44 |
static int process_client_hello(SSL *ssl); |
| 45 |
static int send_server_hello_sequence(SSL *ssl); |
| 46 |
static int send_server_hello(SSL *ssl); |
| 47 |
static int send_server_hello_done(SSL *ssl); |
| 48 |
static int process_client_key_xchg(SSL *ssl); |
| 49 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 50 |
static int send_certificate_request(SSL *ssl); |
| 51 |
static int process_cert_verify(SSL *ssl); |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd) |
| 58 |
{ |
| 59 |
SSL *ssl; |
| 60 |
|
| 61 |
ssl = ssl_new(ssl_ctx, client_fd); |
| 62 |
ssl->next_state = HS_CLIENT_HELLO; |
| 63 |
|
| 64 |
#ifdef CONFIG_DEBUG |
| 65 |
if (ssl_ctx->chain_length == 0) |
| 66 |
printf("Warning - no server certificate defined\n"); TTY_FLUSH(); |
| 67 |
#endif |
| 68 |
|
| 69 |
return ssl; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len) |
| 76 |
{ |
| 77 |
int ret = SSL_OK; |
| 78 |
ssl->hs_status = SSL_NOT_OK; |
| 79 |
|
| 80 |
|
| 81 |
switch (handshake_type) |
| 82 |
{ |
| 83 |
case HS_CLIENT_HELLO: |
| 84 |
if ((ret = process_client_hello(ssl)) == SSL_OK) |
| 85 |
ret = send_server_hello_sequence(ssl); |
| 86 |
break; |
| 87 |
|
| 88 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 89 |
case HS_CERTIFICATE: |
| 90 |
ret = process_certificate(ssl, &ssl->x509_ctx); |
| 91 |
|
| 92 |
if (ret == SSL_OK) |
| 93 |
{ |
| 94 |
int cert_res; |
| 95 |
int pathLenConstraint = 0; |
| 96 |
|
| 97 |
cert_res = x509_verify(ssl->ssl_ctx->ca_cert_ctx, |
| 98 |
ssl->x509_ctx, &pathLenConstraint); |
| 99 |
ret = (cert_res == 0) ? SSL_OK : SSL_X509_ERROR(cert_res); |
| 100 |
} |
| 101 |
break; |
| 102 |
|
| 103 |
case HS_CERT_VERIFY: |
| 104 |
ret = process_cert_verify(ssl); |
| 105 |
add_packet(ssl, buf, hs_len); |
| 106 |
break; |
| 107 |
#endif |
| 108 |
case HS_CLIENT_KEY_XCHG: |
| 109 |
ret = process_client_key_xchg(ssl); |
| 110 |
break; |
| 111 |
|
| 112 |
case HS_FINISHED: |
| 113 |
ret = process_finished(ssl, buf, hs_len); |
| 114 |
disposable_free(ssl); |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static int process_client_hello(SSL *ssl) |
| 125 |
{ |
| 126 |
uint8_t *buf = ssl->bm_data; |
| 127 |
int pkt_size = ssl->bm_index; |
| 128 |
int i, j, cs_len, id_len, offset = 6 + SSL_RANDOM_SIZE; |
| 129 |
int ret = SSL_OK; |
| 130 |
|
| 131 |
uint8_t version = (buf[4] << 4) + buf[5]; |
| 132 |
ssl->version = ssl->client_version = version; |
| 133 |
|
| 134 |
if (version > SSL_PROTOCOL_VERSION_MAX) |
| 135 |
{ |
| 136 |
|
| 137 |
ssl->version = SSL_PROTOCOL_VERSION_MAX; |
| 138 |
} |
| 139 |
else if (version < SSL_PROTOCOL_MIN_VERSION) |
| 140 |
{ |
| 141 |
ret = SSL_ERROR_INVALID_VERSION; |
| 142 |
ssl_display_error(ret); |
| 143 |
goto error; |
| 144 |
} |
| 145 |
|
| 146 |
memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE); |
| 147 |
|
| 148 |
|
| 149 |
id_len = buf[offset++]; |
| 150 |
if (id_len > SSL_SESSION_ID_SIZE) |
| 151 |
{ |
| 152 |
return SSL_ERROR_INVALID_SESSION; |
| 153 |
} |
| 154 |
|
| 155 |
#ifndef CONFIG_SSL_SKELETON_MODE |
| 156 |
ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions, |
| 157 |
ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL); |
| 158 |
#endif |
| 159 |
|
| 160 |
offset += id_len; |
| 161 |
cs_len = (buf[offset]<<8) + buf[offset+1]; |
| 162 |
offset += 2; |
| 163 |
|
| 164 |
PARANOIA_CHECK(pkt_size, offset + cs_len); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
for (i = 0; i < cs_len; i += 2) |
| 169 |
{ |
| 170 |
|
| 171 |
if (buf[offset+i]) |
| 172 |
continue; |
| 173 |
|
| 174 |
for (j = 0; j < NUM_PROTOCOLS; j++) |
| 175 |
{ |
| 176 |
if (ssl_prot_prefs[j] == buf[offset+i+1]) |
| 177 |
{ |
| 178 |
ssl->cipher = ssl_prot_prefs[j]; |
| 179 |
goto do_compression; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
return SSL_ERROR_NO_CIPHER; |
| 186 |
|
| 187 |
|
| 188 |
do_compression: |
| 189 |
offset += cs_len; |
| 190 |
id_len = buf[offset++]; |
| 191 |
offset += id_len; |
| 192 |
PARANOIA_CHECK(pkt_size, offset + id_len); |
| 193 |
|
| 194 |
if (offset == pkt_size) |
| 195 |
{ |
| 196 |
|
| 197 |
goto error; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
id_len = buf[offset++] << 8; |
| 202 |
id_len += buf[offset++]; |
| 203 |
PARANOIA_CHECK(pkt_size, offset + id_len); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
while (offset < pkt_size) |
| 208 |
{ |
| 209 |
int ext, ext_len; |
| 210 |
|
| 211 |
ext = buf[offset++] << 8; |
| 212 |
ext += buf[offset++]; |
| 213 |
ext_len = buf[offset++] << 8; |
| 214 |
ext_len += buf[offset++]; |
| 215 |
PARANOIA_CHECK(pkt_size, offset + ext_len); |
| 216 |
|
| 217 |
if (ext == SSL_EXT_SIG_ALG) |
| 218 |
{ |
| 219 |
while (ext_len > 0) |
| 220 |
{ |
| 221 |
uint8_t hash_alg = buf[offset++]; |
| 222 |
uint8_t sig_alg = buf[offset++]; |
| 223 |
ext_len -= 2; |
| 224 |
|
| 225 |
if (sig_alg == SIG_ALG_RSA && |
| 226 |
(hash_alg == SIG_ALG_SHA1 || |
| 227 |
hash_alg == SIG_ALG_SHA256 || |
| 228 |
hash_alg == SIG_ALG_SHA384 || |
| 229 |
hash_alg == SIG_ALG_SHA512)) |
| 230 |
{ |
| 231 |
ssl->sig_algs[ssl->num_sig_algs++] = hash_alg; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
offset += ext_len; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
if (ssl->num_sig_algs == 0) |
| 243 |
{ |
| 244 |
ssl->sig_algs[ssl->num_sig_algs++] = SIG_ALG_SHA1; |
| 245 |
} |
| 246 |
|
| 247 |
error: |
| 248 |
return ret; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static int send_server_hello_sequence(SSL *ssl) |
| 255 |
{ |
| 256 |
int ret; |
| 257 |
|
| 258 |
if ((ret = send_server_hello(ssl)) == SSL_OK) |
| 259 |
{ |
| 260 |
#ifndef CONFIG_SSL_SKELETON_MODE |
| 261 |
|
| 262 |
if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME)) |
| 263 |
{ |
| 264 |
if ((ret = send_change_cipher_spec(ssl)) == SSL_OK) |
| 265 |
{ |
| 266 |
ret = send_finished(ssl); |
| 267 |
ssl->next_state = HS_FINISHED; |
| 268 |
} |
| 269 |
} |
| 270 |
else |
| 271 |
#endif |
| 272 |
if ((ret = send_certificate(ssl)) == SSL_OK) |
| 273 |
{ |
| 274 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 275 |
|
| 276 |
if (IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION)) |
| 277 |
{ |
| 278 |
if ((ret = send_certificate_request(ssl)) == SSL_OK) |
| 279 |
{ |
| 280 |
ret = send_server_hello_done(ssl); |
| 281 |
ssl->next_state = HS_CERTIFICATE; |
| 282 |
} |
| 283 |
} |
| 284 |
else |
| 285 |
#endif |
| 286 |
{ |
| 287 |
ret = send_server_hello_done(ssl); |
| 288 |
ssl->next_state = HS_CLIENT_KEY_XCHG; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return ret; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static int send_server_hello(SSL *ssl) |
| 300 |
{ |
| 301 |
uint8_t *buf = ssl->bm_data; |
| 302 |
int offset = 0; |
| 303 |
|
| 304 |
buf[0] = HS_SERVER_HELLO; |
| 305 |
buf[1] = 0; |
| 306 |
buf[2] = 0; |
| 307 |
|
| 308 |
buf[4] = 0x03; |
| 309 |
buf[5] = ssl->version & 0x0f; |
| 310 |
|
| 311 |
|
| 312 |
if (get_random(SSL_RANDOM_SIZE, &buf[6]) < 0) |
| 313 |
return SSL_NOT_OK; |
| 314 |
|
| 315 |
memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE); |
| 316 |
offset = 6 + SSL_RANDOM_SIZE; |
| 317 |
|
| 318 |
#ifndef CONFIG_SSL_SKELETON_MODE |
| 319 |
if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME)) |
| 320 |
{ |
| 321 |
|
| 322 |
buf[offset++] = SSL_SESSION_ID_SIZE; |
| 323 |
memcpy(&buf[offset], ssl->session->session_id, SSL_SESSION_ID_SIZE); |
| 324 |
memcpy(ssl->session_id, ssl->session->session_id, SSL_SESSION_ID_SIZE); |
| 325 |
ssl->sess_id_size = SSL_SESSION_ID_SIZE; |
| 326 |
offset += SSL_SESSION_ID_SIZE; |
| 327 |
} |
| 328 |
else |
| 329 |
#endif |
| 330 |
{ |
| 331 |
#ifndef CONFIG_SSL_SKELETON_MODE |
| 332 |
buf[offset++] = SSL_SESSION_ID_SIZE; |
| 333 |
get_random(SSL_SESSION_ID_SIZE, &buf[offset]); |
| 334 |
memcpy(ssl->session_id, &buf[offset], SSL_SESSION_ID_SIZE); |
| 335 |
ssl->sess_id_size = SSL_SESSION_ID_SIZE; |
| 336 |
|
| 337 |
|
| 338 |
if (ssl->ssl_ctx->num_sessions) |
| 339 |
{ |
| 340 |
memcpy(ssl->session->session_id, |
| 341 |
ssl->session_id, SSL_SESSION_ID_SIZE); |
| 342 |
} |
| 343 |
|
| 344 |
offset += SSL_SESSION_ID_SIZE; |
| 345 |
#else |
| 346 |
buf[offset++] = 0; |
| 347 |
#endif |
| 348 |
} |
| 349 |
|
| 350 |
buf[offset++] = 0; |
| 351 |
buf[offset++] = ssl->cipher; |
| 352 |
buf[offset++] = 0; |
| 353 |
buf[3] = offset - 4; |
| 354 |
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static int send_server_hello_done(SSL *ssl) |
| 361 |
{ |
| 362 |
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, |
| 363 |
g_hello_done, sizeof(g_hello_done)); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static int process_client_key_xchg(SSL *ssl) |
| 372 |
{ |
| 373 |
uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index]; |
| 374 |
int pkt_size = ssl->bm_index; |
| 375 |
int premaster_size, secret_length = (buf[2] << 8) + buf[3]; |
| 376 |
uint8_t premaster_secret[MAX_KEY_BYTE_SIZE]; |
| 377 |
RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx; |
| 378 |
int offset = 4; |
| 379 |
int ret = SSL_OK; |
| 380 |
|
| 381 |
if (rsa_ctx == NULL) |
| 382 |
{ |
| 383 |
ret = SSL_ERROR_NO_CERT_DEFINED; |
| 384 |
goto error; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
if ((secret_length - 2) == rsa_ctx->num_octets) |
| 389 |
offset += 2; |
| 390 |
|
| 391 |
PARANOIA_CHECK(pkt_size, rsa_ctx->num_octets+offset); |
| 392 |
|
| 393 |
|
| 394 |
SSL_CTX_LOCK(ssl->ssl_ctx->mutex); |
| 395 |
premaster_size = RSA_decrypt(rsa_ctx, &buf[offset], premaster_secret, |
| 396 |
sizeof(premaster_secret), 1); |
| 397 |
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex); |
| 398 |
|
| 399 |
if (premaster_size != SSL_SECRET_SIZE || |
| 400 |
premaster_secret[0] != 0x03 || |
| 401 |
|
| 402 |
premaster_secret[1] != (ssl->client_version & 0x0f)) |
| 403 |
{ |
| 404 |
|
| 405 |
if (get_random(SSL_SECRET_SIZE, premaster_secret) < 0) |
| 406 |
return SSL_NOT_OK; |
| 407 |
|
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
generate_master_secret(ssl, premaster_secret); |
| 412 |
|
| 413 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 414 |
ssl->next_state = IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION) ? |
| 415 |
HS_CERT_VERIFY : HS_FINISHED; |
| 416 |
#else |
| 417 |
ssl->next_state = HS_FINISHED; |
| 418 |
#endif |
| 419 |
|
| 420 |
ssl->dc->bm_proc_index += rsa_ctx->num_octets+offset; |
| 421 |
error: |
| 422 |
return ret; |
| 423 |
} |
| 424 |
|
| 425 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 426 |
static const uint8_t g_cert_request[] = { HS_CERT_REQ, 0, |
| 427 |
0, 0x0e, |
| 428 |
1, 1, |
| 429 |
0x00, 0x08, |
| 430 |
SIG_ALG_SHA256, SIG_ALG_RSA, |
| 431 |
SIG_ALG_SHA512, SIG_ALG_RSA, |
| 432 |
SIG_ALG_SHA384, SIG_ALG_RSA, |
| 433 |
SIG_ALG_SHA1, SIG_ALG_RSA, |
| 434 |
0, 0 |
| 435 |
}; |
| 436 |
|
| 437 |
static const uint8_t g_cert_request_v1[] = { HS_CERT_REQ, 0, 0, 4, 1, 0, 0, 0 }; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
static int send_certificate_request(SSL *ssl) |
| 443 |
{ |
| 444 |
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) |
| 445 |
{ |
| 446 |
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, |
| 447 |
g_cert_request, sizeof(g_cert_request)); |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, |
| 452 |
g_cert_request_v1, sizeof(g_cert_request_v1)); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static int process_cert_verify(SSL *ssl) |
| 461 |
{ |
| 462 |
uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index]; |
| 463 |
int pkt_size = ssl->bm_index; |
| 464 |
uint8_t dgst_buf[MAX_KEY_BYTE_SIZE]; |
| 465 |
uint8_t dgst[MD5_SIZE + SHA1_SIZE]; |
| 466 |
X509_CTX *x509_ctx = ssl->x509_ctx; |
| 467 |
int ret = SSL_OK; |
| 468 |
int offset = 6; |
| 469 |
int rsa_len; |
| 470 |
int n; |
| 471 |
|
| 472 |
DISPLAY_RSA(ssl, x509_ctx->rsa_ctx); |
| 473 |
|
| 474 |
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) |
| 475 |
{ |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
offset = 8; |
| 481 |
rsa_len = (buf[6] << 8) + buf[7]; |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
rsa_len = (buf[4] << 8) + buf[5]; |
| 486 |
} |
| 487 |
|
| 488 |
PARANOIA_CHECK(pkt_size, offset + rsa_len); |
| 489 |
|
| 490 |
|
| 491 |
SSL_CTX_LOCK(ssl->ssl_ctx->mutex); |
| 492 |
n = RSA_decrypt(x509_ctx->rsa_ctx, &buf[offset], dgst_buf, |
| 493 |
sizeof(dgst_buf), 0); |
| 494 |
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex); |
| 495 |
|
| 496 |
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) |
| 497 |
{ |
| 498 |
if (memcmp(dgst_buf, g_asn1_sha256, sizeof(g_asn1_sha256))) |
| 499 |
{ |
| 500 |
ret = SSL_ERROR_INVALID_KEY; |
| 501 |
goto error; |
| 502 |
} |
| 503 |
|
| 504 |
finished_digest(ssl, NULL, dgst); |
| 505 |
if (memcmp(&dgst_buf[sizeof(g_asn1_sha256)], dgst, SHA256_SIZE)) |
| 506 |
{ |
| 507 |
ret = SSL_ERROR_INVALID_KEY; |
| 508 |
goto error; |
| 509 |
} |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
if (n != SHA1_SIZE + MD5_SIZE) |
| 514 |
{ |
| 515 |
ret = SSL_ERROR_INVALID_KEY; |
| 516 |
goto end_cert_vfy; |
| 517 |
} |
| 518 |
|
| 519 |
finished_digest(ssl, NULL, dgst); |
| 520 |
if (memcmp(dgst_buf, dgst, MD5_SIZE + SHA1_SIZE)) |
| 521 |
{ |
| 522 |
ret = SSL_ERROR_INVALID_KEY; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
end_cert_vfy: |
| 527 |
ssl->next_state = HS_FINISHED; |
| 528 |
error: |
| 529 |
return ret; |
| 530 |
} |
| 531 |
|
| 532 |
#endif |