statem_quic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2019 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 "statem_local.h"
  11. #include "internal/cryptlib.h"
  12. int quic_get_message(SSL *s, int *mt, size_t *len)
  13. {
  14. size_t l;
  15. QUIC_DATA *qd = s->quic_input_data_head;
  16. uint8_t *p;
  17. if (qd == NULL) {
  18. s->rwstate = SSL_READING;
  19. *mt = *len = 0;
  20. return 0;
  21. }
  22. if (!ossl_assert(qd->length >= SSL3_HM_HEADER_LENGTH)) {
  23. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_GET_MESSAGE,
  24. SSL_R_BAD_LENGTH);
  25. *mt = *len = 0;
  26. return 0;
  27. }
  28. /* This is where we check for the proper level, not when data is given */
  29. if (qd->level != s->quic_read_level) {
  30. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_GET_MESSAGE,
  31. SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
  32. *mt = *len = 0;
  33. return 0;
  34. }
  35. if (!BUF_MEM_grow_clean(s->init_buf, (int)qd->length)) {
  36. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_GET_MESSAGE,
  37. ERR_R_BUF_LIB);
  38. *mt = *len = 0;
  39. return 0;
  40. }
  41. /* Copy buffered data */
  42. memcpy(s->init_buf->data, s->quic_buf->data + qd->start, qd->length);
  43. s->init_buf->length = qd->length;
  44. s->quic_input_data_head = qd->next;
  45. if (s->quic_input_data_head == NULL)
  46. s->quic_input_data_tail = NULL;
  47. OPENSSL_free(qd);
  48. s->s3->tmp.message_type = *mt = *(s->init_buf->data);
  49. p = (uint8_t*)s->init_buf->data + 1;
  50. n2l3(p, l);
  51. s->init_num = s->s3->tmp.message_size = *len = l;
  52. s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
  53. /* No CCS in QUIC/TLSv1.3? */
  54. if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  55. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  56. SSL_F_QUIC_GET_MESSAGE,
  57. SSL_R_CCS_RECEIVED_EARLY);
  58. *len = 0;
  59. return 0;
  60. }
  61. /* No KeyUpdate in QUIC */
  62. if (*mt == SSL3_MT_KEY_UPDATE) {
  63. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_QUIC_GET_MESSAGE,
  64. SSL_R_UNEXPECTED_MESSAGE);
  65. *len = 0;
  66. return 0;
  67. }
  68. /*
  69. * If receiving Finished, record MAC of prior handshake messages for
  70. * Finished verification.
  71. */
  72. if (*mt == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
  73. /* SSLfatal() already called */
  74. *len = 0;
  75. return 0;
  76. }
  77. /*
  78. * We defer feeding in the HRR until later. We'll do it as part of
  79. * processing the message
  80. * The TLsv1.3 handshake transcript stops at the ClientFinished
  81. * message.
  82. */
  83. #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
  84. /* KeyUpdate and NewSessionTicket do not need to be added */
  85. if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET
  86. && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE) {
  87. if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO
  88. || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
  89. || memcmp(hrrrandom,
  90. s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
  91. SSL3_RANDOM_SIZE) != 0) {
  92. if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  93. s->init_num + SSL3_HM_HEADER_LENGTH)) {
  94. /* SSLfatal() already called */
  95. *len = 0;
  96. return 0;
  97. }
  98. }
  99. }
  100. if (s->msg_callback)
  101. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
  102. (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
  103. s->msg_callback_arg);
  104. return 1;
  105. }