d1_msg.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2005-2023 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. #include "internal/ssl_unwrap.h"
  11. int dtls1_write_app_data_bytes(SSL *s, uint8_t type, const void *buf_,
  12. size_t len, size_t *written)
  13. {
  14. int i;
  15. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
  16. if (sc == NULL)
  17. return -1;
  18. if (SSL_in_init(s) && !ossl_statem_get_in_handshake(sc)) {
  19. i = sc->handshake_func(s);
  20. if (i < 0)
  21. return i;
  22. if (i == 0) {
  23. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  24. return -1;
  25. }
  26. }
  27. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  28. ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
  29. return -1;
  30. }
  31. return dtls1_write_bytes(sc, type, buf_, len, written);
  32. }
  33. int dtls1_dispatch_alert(SSL *ssl)
  34. {
  35. int i, j;
  36. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  37. unsigned char buf[DTLS1_AL_HEADER_LENGTH];
  38. unsigned char *ptr = &buf[0];
  39. size_t written;
  40. SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  41. if (s == NULL)
  42. return 0;
  43. s->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
  44. memset(buf, 0, sizeof(buf));
  45. *ptr++ = s->s3.send_alert[0];
  46. *ptr++ = s->s3.send_alert[1];
  47. i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), &written);
  48. if (i <= 0) {
  49. s->s3.alert_dispatch = 1;
  50. /* fprintf(stderr, "not done with alert\n"); */
  51. } else {
  52. (void)BIO_flush(s->wbio);
  53. if (s->msg_callback)
  54. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
  55. 2, ssl, s->msg_callback_arg);
  56. if (s->info_callback != NULL)
  57. cb = s->info_callback;
  58. else if (ssl->ctx->info_callback != NULL)
  59. cb = ssl->ctx->info_callback;
  60. if (cb != NULL) {
  61. j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
  62. cb(ssl, SSL_CB_WRITE_ALERT, j);
  63. }
  64. }
  65. return i;
  66. }