d1_msg.c 2.2 KB

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