d1_msg.c 2.1 KB

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