s3_msg.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include "ssl_local.h"
  10. int ssl3_do_change_cipher_spec(SSL_CONNECTION *s)
  11. {
  12. int i;
  13. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  14. if (s->server)
  15. i = SSL3_CHANGE_CIPHER_SERVER_READ;
  16. else
  17. i = SSL3_CHANGE_CIPHER_CLIENT_READ;
  18. if (s->s3.tmp.key_block == NULL) {
  19. if (s->session == NULL || s->session->master_key_length == 0) {
  20. /* might happen if dtls1_read_bytes() calls this */
  21. ERR_raise(ERR_LIB_SSL, SSL_R_CCS_RECEIVED_EARLY);
  22. return 0;
  23. }
  24. s->session->cipher = s->s3.tmp.new_cipher;
  25. if (!ssl->method->ssl3_enc->setup_key_block(s)) {
  26. /* SSLfatal() already called */
  27. return 0;
  28. }
  29. }
  30. if (!ssl->method->ssl3_enc->change_cipher_state(s, i)) {
  31. /* SSLfatal() already called */
  32. return 0;
  33. }
  34. return 1;
  35. }
  36. int ssl3_send_alert(SSL_CONNECTION *s, int level, int desc)
  37. {
  38. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  39. /* Map tls/ssl alert value to correct one */
  40. if (SSL_CONNECTION_TREAT_AS_TLS13(s))
  41. desc = tls13_alert_code(desc);
  42. else
  43. desc = ssl->method->ssl3_enc->alert_value(desc);
  44. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
  45. desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
  46. * protocol_version alerts */
  47. if (desc < 0)
  48. return -1;
  49. if (s->shutdown & SSL_SENT_SHUTDOWN && desc != SSL_AD_CLOSE_NOTIFY)
  50. return -1;
  51. /* If a fatal one, remove from cache */
  52. if ((level == SSL3_AL_FATAL) && (s->session != NULL))
  53. SSL_CTX_remove_session(s->session_ctx, s->session);
  54. s->s3.alert_dispatch = SSL_ALERT_DISPATCH_PENDING;
  55. s->s3.send_alert[0] = level;
  56. s->s3.send_alert[1] = desc;
  57. if (!RECORD_LAYER_write_pending(&s->rlayer)) {
  58. /* data still being written out? */
  59. return ssl->method->ssl_dispatch_alert(ssl);
  60. }
  61. /*
  62. * else data is still being written out, we will get written some time in
  63. * the future
  64. */
  65. return -1;
  66. }
  67. int ssl3_dispatch_alert(SSL *s)
  68. {
  69. int i, j;
  70. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  71. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  72. OSSL_RECORD_TEMPLATE templ;
  73. if (sc == NULL)
  74. return -1;
  75. if (sc->rlayer.wrlmethod == NULL) {
  76. /* No write record layer so we can't sent and alert. We just ignore it */
  77. sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
  78. return 1;
  79. }
  80. templ.type = SSL3_RT_ALERT;
  81. templ.version = (sc->version == TLS1_3_VERSION) ? TLS1_2_VERSION
  82. : sc->version;
  83. if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO
  84. && !sc->renegotiate
  85. && TLS1_get_version(s) > TLS1_VERSION
  86. && sc->hello_retry_request == SSL_HRR_NONE) {
  87. templ.version = TLS1_VERSION;
  88. }
  89. templ.buf = &sc->s3.send_alert[0];
  90. templ.buflen = 2;
  91. if (RECORD_LAYER_write_pending(&sc->rlayer)) {
  92. if (sc->s3.alert_dispatch != SSL_ALERT_DISPATCH_RETRY) {
  93. /*
  94. * We have a write pending but it wasn't from a previous call to
  95. * this function! Can we ever get here? Maybe via API misuse??
  96. * Give up.
  97. */
  98. sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
  99. return -1;
  100. }
  101. /* Retry what we've already got pending */
  102. i = HANDLE_RLAYER_WRITE_RETURN(sc,
  103. sc->rlayer.wrlmethod->retry_write_records(sc->rlayer.wrl));
  104. if (i <= 0) {
  105. /* Could be NBIO. Keep alert_dispatch as SSL_ALERT_DISPATCH_RETRY */
  106. return -1;
  107. }
  108. sc->rlayer.wpend_tot = 0;
  109. sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
  110. return 1;
  111. }
  112. i = HANDLE_RLAYER_WRITE_RETURN(sc,
  113. sc->rlayer.wrlmethod->write_records(sc->rlayer.wrl, &templ, 1));
  114. if (i <= 0) {
  115. sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_RETRY;
  116. sc->rlayer.wpend_tot = templ.buflen;
  117. sc->rlayer.wpend_type = templ.type;
  118. sc->rlayer.wpend_buf = templ.buf;
  119. } else {
  120. /*
  121. * Alert sent to BIO - now flush. If the message does not get sent due
  122. * to non-blocking IO, we will not worry too much.
  123. */
  124. (void)BIO_flush(sc->wbio);
  125. sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
  126. if (sc->msg_callback)
  127. sc->msg_callback(1, sc->version, SSL3_RT_ALERT, sc->s3.send_alert,
  128. 2, s, sc->msg_callback_arg);
  129. if (sc->info_callback != NULL)
  130. cb = sc->info_callback;
  131. else if (s->ctx->info_callback != NULL)
  132. cb = s->ctx->info_callback;
  133. if (cb != NULL) {
  134. j = (sc->s3.send_alert[0] << 8) | sc->s3.send_alert[1];
  135. cb(s, SSL_CB_WRITE_ALERT, j);
  136. }
  137. }
  138. return i;
  139. }