d1_msg.c 2.1 KB

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