quic-rcidm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <openssl/ssl.h>
  11. #include <openssl/err.h>
  12. #include <openssl/bio.h>
  13. #include "fuzzer.h"
  14. #include "internal/quic_rcidm.h"
  15. #include "internal/packet.h"
  16. int FuzzerInitialize(int *argc, char ***argv)
  17. {
  18. FuzzerSetRand();
  19. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
  20. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
  21. ERR_clear_error();
  22. return 1;
  23. }
  24. /*
  25. * Fuzzer input "protocol":
  26. * Big endian
  27. * Zero or more of:
  28. * RESET_WITH_ODCID u8(0x00) u8(cidl):cid
  29. * RESET_WITHOUT_ODCID u8(0x01)
  30. * (free and reallocate)
  31. * ADD_FROM_INITIAL u8(0x02) u8(cidl):cid
  32. * ADD_FROM_SERVER_RETRY u8(0x03) u8(cidl):cid
  33. * ADD_FROM_NCID u8(0x04) u64(seq_num)
  34. * u64(retire_prior_to) u8(cidl):cid
  35. * ON_HANDSHAKE_COMPLETE u8(0x05)
  36. * ON_PACKET_SENT u8(0x06) u64(num_pkt)
  37. * REQUEST_ROLL u8(0x07)
  38. * POP_RETIRE_SEQ_NUM u8(0x08)
  39. * PEEK_RETIRE_SEQ_NUM u8(0x09)
  40. * GET_PREFERRED_TX_DCID u8(0x0A)
  41. * GET_PREFERRED_TX_DCID_CHANGED u8(0x0B) u8(clear)
  42. */
  43. enum {
  44. CMD_RESET_WITH_ODCID,
  45. CMD_RESET_WITHOUT_ODCID,
  46. CMD_ADD_FROM_INITIAL,
  47. CMD_ADD_FROM_SERVER_RETRY,
  48. CMD_ADD_FROM_NCID,
  49. CMD_ON_HANDSHAKE_COMPLETE,
  50. CMD_ON_PACKET_SENT,
  51. CMD_REQUEST_ROLL,
  52. CMD_POP_RETIRE_SEQ_NUM,
  53. CMD_PEEK_RETIRE_SEQ_NUM,
  54. CMD_GET_PREFERRED_TX_DCID,
  55. CMD_GET_PREFERRED_TX_DCID_CHANGED
  56. };
  57. static int get_cid(PACKET *pkt, QUIC_CONN_ID *cid)
  58. {
  59. unsigned int cidl;
  60. if (!PACKET_get_1(pkt, &cidl)
  61. || cidl > QUIC_MAX_CONN_ID_LEN
  62. || !PACKET_copy_bytes(pkt, cid->id, cidl))
  63. return 0;
  64. cid->id_len = (unsigned char)cidl;
  65. return 1;
  66. }
  67. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  68. {
  69. int rc = 0;
  70. QUIC_RCIDM *rcidm = NULL;
  71. PACKET pkt;
  72. uint64_t seq_num_out, arg_num_pkt;
  73. unsigned int cmd, arg_clear;
  74. QUIC_CONN_ID arg_cid, cid_out;
  75. OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame;
  76. if (!PACKET_buf_init(&pkt, buf, len))
  77. goto err;
  78. if ((rcidm = ossl_quic_rcidm_new(NULL)) == NULL)
  79. goto err;
  80. while (PACKET_remaining(&pkt) > 0) {
  81. if (!PACKET_get_1(&pkt, &cmd))
  82. goto err;
  83. switch (cmd) {
  84. case CMD_RESET_WITH_ODCID:
  85. if (!get_cid(&pkt, &arg_cid)) {
  86. rc = -1;
  87. goto err;
  88. }
  89. ossl_quic_rcidm_free(rcidm);
  90. if ((rcidm = ossl_quic_rcidm_new(&arg_cid)) == NULL)
  91. goto err;
  92. break;
  93. case CMD_RESET_WITHOUT_ODCID:
  94. ossl_quic_rcidm_free(rcidm);
  95. if ((rcidm = ossl_quic_rcidm_new(NULL)) == NULL)
  96. goto err;
  97. break;
  98. case CMD_ADD_FROM_INITIAL:
  99. if (!get_cid(&pkt, &arg_cid)) {
  100. rc = -1;
  101. goto err;
  102. }
  103. ossl_quic_rcidm_add_from_initial(rcidm, &arg_cid);
  104. break;
  105. case CMD_ADD_FROM_SERVER_RETRY:
  106. if (!get_cid(&pkt, &arg_cid)) {
  107. rc = -1;
  108. goto err;
  109. }
  110. ossl_quic_rcidm_add_from_server_retry(rcidm, &arg_cid);
  111. break;
  112. case CMD_ADD_FROM_NCID:
  113. if (!PACKET_get_net_8(&pkt, &ncid_frame.seq_num)
  114. || !PACKET_get_net_8(&pkt, &ncid_frame.retire_prior_to)
  115. || !get_cid(&pkt, &ncid_frame.conn_id)) {
  116. rc = -1;
  117. goto err;
  118. }
  119. ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame);
  120. break;
  121. case CMD_ON_HANDSHAKE_COMPLETE:
  122. ossl_quic_rcidm_on_handshake_complete(rcidm);
  123. break;
  124. case CMD_ON_PACKET_SENT:
  125. if (!PACKET_get_net_8(&pkt, &arg_num_pkt)) {
  126. rc = -1;
  127. goto err;
  128. }
  129. ossl_quic_rcidm_on_packet_sent(rcidm, arg_num_pkt);
  130. break;
  131. case CMD_REQUEST_ROLL:
  132. ossl_quic_rcidm_request_roll(rcidm);
  133. break;
  134. case CMD_POP_RETIRE_SEQ_NUM:
  135. ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out);
  136. break;
  137. case CMD_PEEK_RETIRE_SEQ_NUM:
  138. ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out);
  139. break;
  140. case CMD_GET_PREFERRED_TX_DCID:
  141. ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &cid_out);
  142. break;
  143. case CMD_GET_PREFERRED_TX_DCID_CHANGED:
  144. if (!PACKET_get_1(&pkt, &arg_clear)) {
  145. rc = -1;
  146. goto err;
  147. }
  148. ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, arg_clear);
  149. break;
  150. default:
  151. rc = -1;
  152. goto err;
  153. }
  154. }
  155. err:
  156. ossl_quic_rcidm_free(rcidm);
  157. return rc;
  158. }
  159. void FuzzerCleanup(void)
  160. {
  161. FuzzerClearRand();
  162. }