s3_msg.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #define USE_SOCKETS
  10. #include "ssl_locl.h"
  11. int ssl3_do_change_cipher_spec(SSL *s)
  12. {
  13. int i;
  14. size_t finish_md_len;
  15. const char *sender;
  16. size_t slen;
  17. if (s->server)
  18. i = SSL3_CHANGE_CIPHER_SERVER_READ;
  19. else
  20. i = SSL3_CHANGE_CIPHER_CLIENT_READ;
  21. if (s->s3->tmp.key_block == NULL) {
  22. if (s->session == NULL || s->session->master_key_length == 0) {
  23. /* might happen if dtls1_read_bytes() calls this */
  24. SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
  25. return (0);
  26. }
  27. s->session->cipher = s->s3->tmp.new_cipher;
  28. if (!s->method->ssl3_enc->setup_key_block(s))
  29. return (0);
  30. }
  31. if (!s->method->ssl3_enc->change_cipher_state(s, i))
  32. return (0);
  33. /*
  34. * we have to record the message digest at this point so we can get it
  35. * before we read the finished message
  36. */
  37. if (!s->server) {
  38. sender = s->method->ssl3_enc->server_finished_label;
  39. slen = s->method->ssl3_enc->server_finished_label_len;
  40. } else {
  41. sender = s->method->ssl3_enc->client_finished_label;
  42. slen = s->method->ssl3_enc->client_finished_label_len;
  43. }
  44. finish_md_len = s->method->ssl3_enc->final_finish_mac(s, sender, slen,
  45. s->s3->tmp.peer_finish_md);
  46. if (finish_md_len == 0) {
  47. SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
  48. return 0;
  49. }
  50. s->s3->tmp.peer_finish_md_len = finish_md_len;
  51. return (1);
  52. }
  53. int ssl3_send_alert(SSL *s, int level, int desc)
  54. {
  55. /* Map tls/ssl alert value to correct one */
  56. if (SSL_TREAT_AS_TLS13(s))
  57. desc = tls13_alert_code(desc);
  58. else
  59. desc = s->method->ssl3_enc->alert_value(desc);
  60. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
  61. desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
  62. * protocol_version alerts */
  63. if (desc < 0)
  64. return -1;
  65. /* If a fatal one, remove from cache */
  66. if ((level == SSL3_AL_FATAL) && (s->session != NULL))
  67. SSL_CTX_remove_session(s->session_ctx, s->session);
  68. s->s3->alert_dispatch = 1;
  69. s->s3->send_alert[0] = level;
  70. s->s3->send_alert[1] = desc;
  71. if (!RECORD_LAYER_write_pending(&s->rlayer)) {
  72. /* data still being written out? */
  73. return s->method->ssl_dispatch_alert(s);
  74. }
  75. /*
  76. * else data is still being written out, we will get written some time in
  77. * the future
  78. */
  79. return -1;
  80. }
  81. int ssl3_dispatch_alert(SSL *s)
  82. {
  83. int i, j;
  84. size_t alertlen;
  85. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  86. size_t written;
  87. s->s3->alert_dispatch = 0;
  88. alertlen = 2;
  89. i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0,
  90. &written);
  91. if (i <= 0) {
  92. s->s3->alert_dispatch = 1;
  93. } else {
  94. /*
  95. * Alert sent to BIO. If it is important, flush it now. If the
  96. * message does not get sent due to non-blocking IO, we will not
  97. * worry too much.
  98. */
  99. if (s->s3->send_alert[0] == SSL3_AL_FATAL)
  100. (void)BIO_flush(s->wbio);
  101. if (s->msg_callback)
  102. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
  103. 2, s, s->msg_callback_arg);
  104. if (s->info_callback != NULL)
  105. cb = s->info_callback;
  106. else if (s->ctx->info_callback != NULL)
  107. cb = s->ctx->info_callback;
  108. if (cb != NULL) {
  109. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  110. cb(s, SSL_CB_WRITE_ALERT, j);
  111. }
  112. }
  113. return i;
  114. }